/*******************************************************************************
 * Copyright (c) 2011, 2016 Eurotech and others
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Eurotech
 *     Red Hat Inc - fix build warnings
 *******************************************************************************/
package org.eclipse.kura.linux.clock;

import java.io.IOException;
import java.net.InetAddress;
import java.util.Date;

import org.apache.commons.net.ntp.NTPUDPClient;
import org.apache.commons.net.ntp.TimeInfo;
import org.eclipse.kura.KuraErrorCode;
import org.eclipse.kura.KuraException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class JavaNtpClockSyncProvider extends AbstractNtpClockSyncProvider {

    private static final Logger s_logger = LoggerFactory.getLogger(JavaNtpClockSyncProvider.class);

    // ----------------------------------------------------------------
    //
    // Concrete Methods
    //
    // ----------------------------------------------------------------

    @Override
    protected boolean syncClock() throws KuraException {
        boolean ret = false;
        // connect and get the delta
        NTPUDPClient ntpClient = new NTPUDPClient();
        ntpClient.setDefaultTimeout(this.m_ntpTimeout);
        try {
            ntpClient.open();
            try {
                InetAddress ntpHostAddr = InetAddress.getByName(this.m_ntpHost);
                TimeInfo info = ntpClient.getTime(ntpHostAddr, this.m_ntpPort);
                this.m_lastSync = new Date();
                info.computeDetails();

                this.m_listener.onClockUpdate(info.getOffset());
                ret = true;
            } catch (IOException e) {
                ntpClient.close();
                s_logger.warn(
                        "Error while synchronizing System Clock with NTP host {}. Please verify network connectivity ...",
                        this.m_ntpHost);
            }
        } catch (Exception e) {
            throw new KuraException(KuraErrorCode.INTERNAL_ERROR, e);
        }
        return ret;
    }
}

@@ -1793,17 +1793,153 @@
 ails();%0A
-%0A
+                Long delayValue = info.getDelay();%0A                if(delayValue!=null && delayValue.longValue()%3C1000)%7B                %0A%09
         
@@ -1995,16 +1995,17 @@
 set());%0A
+%09
         
@@ -2024,16 +2024,182 @@
 = true;%0A
+                %7D%0A                else%7B%0A                    s_logger.error(%22Incorrect delay value(%7B%7D), clock will not be updated%22,info.getDelay());%0A                %7D%0A
         
@@ -2232,43 +2232,8 @@
 ) %7B%0A
-                ntpClient.close();%0A
     
@@ -2523,27 +2523,97 @@
 ode.
-INTERNAL_ERROR, e);
+CONNECTION_FAILED, e);%0A        %7D%0A        finally%7B%0A            ntpClient.close();        %09
 %0A   

Add test on getDelay before updating clock