Merge "Make configuration push timeout configurable"
authorEd Warnicke <eaw@cisco.com>
Thu, 23 Jan 2014 03:40:29 +0000 (03:40 +0000)
committerGerrit Code Review <gerrit@opendaylight.org>
Thu, 23 Jan 2014 03:40:29 +0000 (03:40 +0000)
1  2 
opendaylight/netconf/config-persister-impl/src/main/java/org/opendaylight/controller/netconf/persist/impl/ConfigPusher.java

index 17a2ced30ae547c27d506c1ed2e92435bb5f0e59,e45b092d080a6ec33555707d463a2d31aa9876e3..6e62a982d1588217215f999011b6cfba4a443e46
@@@ -35,6 -35,7 +35,7 @@@ import java.util.Collections
  import java.util.HashSet;
  import java.util.List;
  import java.util.Set;
+ import java.util.concurrent.TimeUnit;
  
  @Immutable
  public class ConfigPusher {
      private final InetSocketAddress address;
      private final EventLoopGroup nettyThreadgroup;
  
-     public static final long DEFAULT_TIMEOUT = 120000L;// 120 seconds until netconf must be stable
-     private final long timeout;
+     // Default timeout for netconf becoming stable
+     public static final long DEFAULT_TIMEOUT = TimeUnit.MINUTES.toNanos(2);
+     private final int delayMillis = 5000;
+     private final long timeoutNanos;
  
      public ConfigPusher(InetSocketAddress address, EventLoopGroup nettyThreadgroup) {
-         this(address, DEFAULT_TIMEOUT, nettyThreadgroup);
+         this(address, nettyThreadgroup, DEFAULT_TIMEOUT);
+     }
  
+     @Deprecated
+     public ConfigPusher(InetSocketAddress address, long timeoutMillis, EventLoopGroup nettyThreadgroup) {
+         this(address, nettyThreadgroup, TimeUnit.MILLISECONDS.toNanos(timeoutMillis));
      }
  
-     public ConfigPusher(InetSocketAddress address, long timeout, EventLoopGroup nettyThreadgroup) {
+     public ConfigPusher(InetSocketAddress address, EventLoopGroup nettyThreadgroup, long timeoutNanos) {
          this.address = address;
-         this.timeout = timeout;
          this.nettyThreadgroup = nettyThreadgroup;
+         this.timeoutNanos = timeoutNanos;
      }
  
      public synchronized NetconfClient init(List<ConfigSnapshotHolder> configs) throws InterruptedException {
          // TODO think about moving capability subset check to netconf client
          // could be utilized by integration tests
  
-         long pollingStart = System.currentTimeMillis();
-         int delay = 5000;
+         final long pollingStart = System.nanoTime();
+         final long deadline = pollingStart + timeoutNanos;
          int attempt = 0;
  
-         long deadline = pollingStart + timeout;
          String additionalHeader = NetconfMessageAdditionalHeader.toString("unknown", address.getAddress().getHostAddress(),
                  Integer.toString(address.getPort()), "tcp", Optional.of("persister"));
  
          Set<String> latestCapabilities = new HashSet<>();
-         while (System.currentTimeMillis() < deadline) {
+         while (System.nanoTime() < deadline) {
              attempt++;
              NetconfClientDispatcher netconfClientDispatcher = new NetconfClientDispatcher(nettyThreadgroup,
                      nettyThreadgroup, additionalHeader);
              NetconfClient netconfClient;
              try {
-                 netconfClient = new NetconfClient(this.toString(), address, delay, netconfClientDispatcher);
+                 netconfClient = new NetconfClient(this.toString(), address, delayMillis, netconfClientDispatcher);
              } catch (IllegalStateException e) {
                  logger.debug("Netconf {} was not initialized or is not stable, attempt {}", address, attempt, e);
                  netconfClientDispatcher.close();
-                 Thread.sleep(delay);
+                 Thread.sleep(delayMillis);
                  continue;
              }
              latestCapabilities = netconfClient.getCapabilities();
              if (Util.isSubset(netconfClient, expectedCaps)) {
                  logger.debug("Hello from netconf stable with {} capabilities", latestCapabilities);
 -                logger.info("Session id received from netconf server: {}", netconfClient.getClientSession());
 +                logger.trace("Session id received from netconf server: {}", netconfClient.getClientSession());
                  return netconfClient;
              }
              logger.debug("Polling hello from netconf, attempt {}, capabilities {}", attempt, latestCapabilities);
              Util.closeClientAndDispatcher(netconfClient);
-             Thread.sleep(delay);
+             Thread.sleep(delayMillis);
          }
          Set<String> allNotFound = new HashSet<>(expectedCaps);
          allNotFound.removeAll(latestCapabilities);
              throws ConflictingVersionException, IOException, SAXException {
  
          Element xmlToBePersisted = XmlUtil.readXmlToElement(configSnapshotHolder.getConfigSnapshot());
 -        logger.info("Pushing last configuration to netconf: {}", configSnapshotHolder);
 +        logger.trace("Pushing last configuration to netconf: {}", configSnapshotHolder);
          StringBuilder response = new StringBuilder("editConfig response = {");
  
          NetconfMessage message = createEditConfigMessage(xmlToBePersisted, "/netconfOp/editConfig.xml");
          response.append("commit response = {");
          response.append(XmlUtil.toString(responseMessage.getDocument()));
          response.append("}");
 -        logger.info("Last configuration loaded successfully");
 +        logger.trace("Last configuration loaded successfully");
          logger.trace("Detailed message {}", response);
      }