Merge "Exception for URI /restconf/operations/module_name:rpc ended with slash"
[controller.git] / opendaylight / netconf / config-persister-impl / src / main / java / org / opendaylight / controller / netconf / persist / impl / ConfigPusher.java
index 0b623baaa480dde170d6866d7fe1564242ed704e..ea2a46dba535f825d0baa7b5dfa58d4b6371c297 100644 (file)
@@ -8,24 +8,14 @@
 
 package org.opendaylight.controller.netconf.persist.impl;
 
-import io.netty.channel.EventLoopGroup;
-
-import java.io.IOException;
-import java.io.InputStream;
-import java.net.InetSocketAddress;
-import java.util.Collections;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Set;
-
-import javax.annotation.concurrent.Immutable;
-
+import com.google.common.base.Preconditions;
 import org.opendaylight.controller.config.api.ConflictingVersionException;
 import org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder;
 import org.opendaylight.controller.netconf.api.NetconfMessage;
 import org.opendaylight.controller.netconf.client.NetconfClient;
 import org.opendaylight.controller.netconf.client.NetconfClientDispatcher;
-import org.opendaylight.controller.netconf.util.messages.NetconfMessageAdditionalHeader;
+import org.opendaylight.controller.netconf.util.NetconfUtil;
+import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessageAdditionalHeader;
 import org.opendaylight.controller.netconf.util.xml.XmlElement;
 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
@@ -35,206 +25,292 @@ import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 import org.xml.sax.SAXException;
 
-import com.google.common.base.Optional;
-import com.google.common.base.Preconditions;
+import javax.annotation.concurrent.Immutable;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Set;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
 
 @Immutable
 public class ConfigPusher {
-    private static final Logger logger = LoggerFactory.getLogger(ConfigPersisterNotificationHandler.class);
-    private static final int NETCONF_SEND_ATTEMPT_MS_DELAY = 1000;
-    private static final int NETCONF_SEND_ATTEMPTS = 20;
-
-    private final InetSocketAddress address;
-    private final EventLoopGroup nettyThreadgroup;
-
+    private static final Logger logger = LoggerFactory.getLogger(ConfigPusher.class);
 
-    public static final long DEFAULT_TIMEOUT = 120000L;// 120 seconds until netconf must be stable
-    private final long timeout;
-
-    public ConfigPusher(InetSocketAddress address, EventLoopGroup nettyThreadgroup) {
-        this(address, DEFAULT_TIMEOUT, nettyThreadgroup);
+    private final ConfigPusherConfiguration configuration;
 
+    public ConfigPusher(ConfigPusherConfiguration configuration) {
+        this.configuration = configuration;
     }
 
-    public ConfigPusher(InetSocketAddress address, long timeout, EventLoopGroup nettyThreadgroup) {
-        this.address = address;
-        this.timeout = timeout;
-
-        this.nettyThreadgroup = nettyThreadgroup;
-    }
-
-    public synchronized NetconfClient init(List<ConfigSnapshotHolder> configs) throws InterruptedException {
+    public synchronized LinkedHashMap<ConfigSnapshotHolder, EditAndCommitResponseWithRetries> pushConfigs(
+            List<ConfigSnapshotHolder> configs) throws InterruptedException {
         logger.debug("Last config snapshots to be pushed to netconf: {}", configs);
-        return pushAllConfigs(configs);
-    }
 
-    private synchronized NetconfClient pushAllConfigs(List<ConfigSnapshotHolder> configs) throws InterruptedException {
-        NetconfClient netconfClient = makeNetconfConnection(Collections.<String>emptySet(), Optional.<NetconfClient>absent());
-        for (ConfigSnapshotHolder configSnapshotHolder: configs){
-            netconfClient = pushSnapshotWithRetries(configSnapshotHolder, Optional.of(netconfClient));
+        // first just make sure we can connect to netconf, even if nothing is being pushed
+        {
+            NetconfClient netconfClient = makeNetconfConnection(Collections.<String>emptySet());
+            Util.closeClientAndDispatcher(netconfClient);
+        }
+        LinkedHashMap<ConfigSnapshotHolder, EditAndCommitResponseWithRetries> result = new LinkedHashMap<>();
+        // start pushing snapshots:
+        for (ConfigSnapshotHolder configSnapshotHolder : configs) {
+            EditAndCommitResponseWithRetries editAndCommitResponseWithRetries = pushSnapshotWithRetries(configSnapshotHolder);
+            logger.debug("Config snapshot pushed successfully: {}, result: {}", configSnapshotHolder, result);
+            result.put(configSnapshotHolder, editAndCommitResponseWithRetries);
         }
-        return netconfClient;
+        logger.debug("All configuration snapshots have been pushed successfully.");
+        return result;
     }
 
-    private synchronized NetconfClient pushSnapshotWithRetries(ConfigSnapshotHolder configSnapshotHolder,
-                                                               Optional<NetconfClient> oldClientForPossibleReuse)
+    /**
+     * Checks for ConflictingVersionException and retries until optimistic lock succeeds or maximal
+     * number of attempts is reached.
+     */
+    private synchronized EditAndCommitResponseWithRetries pushSnapshotWithRetries(ConfigSnapshotHolder configSnapshotHolder)
             throws InterruptedException {
 
         ConflictingVersionException lastException = null;
-        int maxAttempts = 30;
-        for(int i = 0 ; i < maxAttempts; i++) {
-            NetconfClient netconfClient = makeNetconfConnection(configSnapshotHolder.getCapabilities(), oldClientForPossibleReuse);
-            final String configSnapshot = configSnapshotHolder.getConfigSnapshot();
-            logger.trace("Pushing following xml to netconf {}", configSnapshot);
+        int maxAttempts = configuration.netconfPushConfigAttempts;
+
+        for (int retryAttempt = 1; retryAttempt <= maxAttempts; retryAttempt++) {
+            NetconfClient netconfClient = makeNetconfConnection(configSnapshotHolder.getCapabilities());
+            logger.trace("Pushing following xml to netconf {}", configSnapshotHolder);
             try {
-                pushLastConfig(configSnapshotHolder, netconfClient);
-                return netconfClient;
-            } catch(ConflictingVersionException e) {
-                Util.closeClientAndDispatcher(netconfClient);
+                EditAndCommitResponse editAndCommitResponse = pushLastConfig(configSnapshotHolder, netconfClient);
+                return new EditAndCommitResponseWithRetries(editAndCommitResponse, retryAttempt);
+            } catch (ConflictingVersionException e) {
+                logger.debug("Conflicting version detected, will retry after timeout");
                 lastException = e;
-                Thread.sleep(1000);
-            } catch (SAXException | IOException e) {
-                throw new IllegalStateException("Unable to load last config", e);
+                Thread.sleep(configuration.netconfPushConfigDelayMs);
+            } catch (RuntimeException e) {
+                throw new IllegalStateException("Unable to load " + configSnapshotHolder, e);
+            } finally {
+                Util.closeClientAndDispatcher(netconfClient);
             }
         }
-        throw new IllegalStateException("Failed to push configuration, maximum attempt count has been reached: "
-                + maxAttempts, lastException);
+        throw new IllegalStateException("Maximum attempt count has been reached for pushing " + configSnapshotHolder,
+                lastException);
     }
 
     /**
      * @param expectedCaps capabilities that server hello must contain. Will retry until all are found or throws RuntimeException.
      *                     If empty set is provided, will only make sure netconf client successfuly connected to the server.
-     * @param oldClientForPossibleReuse if present, try to get expected capabilities from it before closing it and retrying with
-     *                                  new client connection.
      * @return NetconfClient that has all required capabilities from server.
      */
-    private synchronized NetconfClient makeNetconfConnection(Set<String> expectedCaps,
-                                                             Optional<NetconfClient> oldClientForPossibleReuse)
-            throws InterruptedException {
-
-        if (oldClientForPossibleReuse.isPresent()) {
-            NetconfClient oldClient = oldClientForPossibleReuse.get();
-            if (Util.isSubset(oldClient, expectedCaps)) {
-                return oldClient;
-            } else {
-                Util.closeClientAndDispatcher(oldClient);
-            }
-        }
+    private synchronized NetconfClient makeNetconfConnection(Set<String> expectedCaps) 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 pollingStartNanos = System.nanoTime();
+        final long deadlineNanos = pollingStartNanos + TimeUnit.MILLISECONDS.toNanos(configuration.netconfCapabilitiesWaitTimeoutMs);
         int attempt = 0;
 
-        long deadline = pollingStart + timeout;
+        NetconfHelloMessageAdditionalHeader additionalHeader = new NetconfHelloMessageAdditionalHeader("unknown",
+                configuration.netconfAddress.getAddress().getHostAddress(),
+                Integer.toString(configuration.netconfAddress.getPort()), "tcp", "persister");
 
-        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) {
+        Set<String> latestCapabilities = null;
+        while (System.nanoTime() < deadlineNanos) {
             attempt++;
-            NetconfClientDispatcher netconfClientDispatcher = new NetconfClientDispatcher(nettyThreadgroup,
-                    nettyThreadgroup, additionalHeader);
+            NetconfClientDispatcher netconfClientDispatcher = new NetconfClientDispatcher(configuration.eventLoopGroup,
+                    configuration.eventLoopGroup, additionalHeader, configuration.connectionAttemptTimeoutMs);
             NetconfClient netconfClient;
             try {
-                netconfClient = new NetconfClient(this.toString(), address, delay, netconfClientDispatcher);
+                netconfClient = new NetconfClient(this.toString(), configuration.netconfAddress, configuration.connectionAttemptDelayMs, netconfClientDispatcher);
             } catch (IllegalStateException e) {
-                logger.debug("Netconf {} was not initialized or is not stable, attempt {}", address, attempt, e);
+                logger.debug("Netconf {} was not initialized or is not stable, attempt {}", configuration.netconfAddress, attempt, e);
                 netconfClientDispatcher.close();
-                Thread.sleep(delay);
+                Thread.sleep(configuration.connectionAttemptDelayMs);
                 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);
+            Set<String> allNotFound = computeNotFoundCapabilities(expectedCaps, latestCapabilities);
+            logger.debug("Netconf server did not provide required capabilities. Attempt {}. " +
+                    "Expected but not found: {}, all expected {}, current {}",
+                    attempt, allNotFound, expectedCaps, latestCapabilities);
             Util.closeClientAndDispatcher(netconfClient);
-            Thread.sleep(delay);
+            Thread.sleep(configuration.connectionAttemptDelayMs);
         }
-        Set<String> allNotFound = new HashSet<>(expectedCaps);
-        allNotFound.removeAll(latestCapabilities);
+        if (latestCapabilities == null) {
+            logger.error("Could not connect to the server in {} ms", configuration.netconfCapabilitiesWaitTimeoutMs);
+            throw new RuntimeException("Could not connect to netconf server");
+        }
+        Set<String> allNotFound = computeNotFoundCapabilities(expectedCaps, latestCapabilities);
         logger.error("Netconf server did not provide required capabilities. Expected but not found: {}, all expected {}, current {}",
                 allNotFound, expectedCaps, latestCapabilities);
         throw new RuntimeException("Netconf server did not provide required capabilities. Expected but not found:" + allNotFound);
     }
 
+    private static Set<String> computeNotFoundCapabilities(Set<String> expectedCaps, Set<String> latestCapabilities) {
+        Set<String> allNotFound = new HashSet<>(expectedCaps);
+        allNotFound.removeAll(latestCapabilities);
+        return allNotFound;
+    }
 
-    private synchronized void pushLastConfig(ConfigSnapshotHolder configSnapshotHolder, NetconfClient netconfClient)
-            throws ConflictingVersionException, IOException, SAXException {
 
-        Element xmlToBePersisted = XmlUtil.readXmlToElement(configSnapshotHolder.getConfigSnapshot());
-        logger.info("Pushing last configuration to netconf: {}", configSnapshotHolder);
-        StringBuilder response = new StringBuilder("editConfig response = {");
+    /**
+     * Sends two RPCs to the netconf server: edit-config and commit.
+     *
+     * @param configSnapshotHolder
+     * @param netconfClient
+     * @throws ConflictingVersionException if commit fails on optimistic lock failure inside of config-manager
+     * @throws java.lang.RuntimeException  if edit-config or commit fails otherwise
+     */
+    private synchronized EditAndCommitResponse pushLastConfig(ConfigSnapshotHolder configSnapshotHolder, NetconfClient netconfClient)
+            throws ConflictingVersionException {
 
+        Element xmlToBePersisted;
+        try {
+            xmlToBePersisted = XmlUtil.readXmlToElement(configSnapshotHolder.getConfigSnapshot());
+        } catch (SAXException | IOException e) {
+            throw new IllegalStateException("Cannot parse " + configSnapshotHolder);
+        }
+        logger.trace("Pushing last configuration to netconf: {}", configSnapshotHolder);
 
-        NetconfMessage message = createEditConfigMessage(xmlToBePersisted, "/netconfOp/editConfig.xml");
+        NetconfMessage editConfigMessage = createEditConfigMessage(xmlToBePersisted);
 
         // sending message to netconf
-        NetconfMessage responseMessage = getResponse(message, netconfClient);
-
-        XmlElement element = XmlElement.fromDomDocument(responseMessage.getDocument());
-        Preconditions.checkState(element.getName().equals(XmlNetconfConstants.RPC_REPLY_KEY));
-        element = element.getOnlyChildElement();
-
-        Util.checkIsOk(element, responseMessage);
-        response.append(XmlUtil.toString(responseMessage.getDocument()));
-        response.append("}");
-        responseMessage = getResponse(getNetconfMessageFromResource("/netconfOp/commit.xml"), netconfClient);
-
-        element = XmlElement.fromDomDocument(responseMessage.getDocument());
-        Preconditions.checkState(element.getName().equals(XmlNetconfConstants.RPC_REPLY_KEY));
-        element = element.getOnlyChildElement();
-
-        Util.checkIsOk(element, responseMessage);
-        response.append("commit response = {");
-        response.append(XmlUtil.toString(responseMessage.getDocument()));
-        response.append("}");
-        logger.info("Last configuration loaded successfully");
-        logger.trace("Detailed message {}", response);
+        NetconfMessage editResponseMessage;
+        try {
+            editResponseMessage = sendRequestGetResponseCheckIsOK(editConfigMessage, netconfClient);
+        } catch (IOException e) {
+            throw new IllegalStateException("Edit-config failed on " + configSnapshotHolder, e);
+        }
+
+        // commit
+        NetconfMessage commitResponseMessage;
+        try {
+            commitResponseMessage = sendRequestGetResponseCheckIsOK(getCommitMessage(), netconfClient);
+        } catch (IOException e) {
+            throw new IllegalStateException("Edit commit succeeded, but commit failed on " + configSnapshotHolder, e);
+        }
+
+        if (logger.isTraceEnabled()) {
+            StringBuilder response = new StringBuilder("editConfig response = {");
+            response.append(XmlUtil.toString(editResponseMessage.getDocument()));
+            response.append("}");
+            response.append("commit response = {");
+            response.append(XmlUtil.toString(commitResponseMessage.getDocument()));
+            response.append("}");
+            logger.trace("Last configuration loaded successfully");
+            logger.trace("Detailed message {}", response);
+        }
+        return new EditAndCommitResponse(editResponseMessage, commitResponseMessage);
     }
 
-    private static NetconfMessage getResponse(NetconfMessage request, NetconfClient netconfClient) {
+
+    private NetconfMessage sendRequestGetResponseCheckIsOK(NetconfMessage request, NetconfClient netconfClient)
+            throws ConflictingVersionException, IOException {
         try {
-            return netconfClient.sendMessage(request, NETCONF_SEND_ATTEMPTS, NETCONF_SEND_ATTEMPT_MS_DELAY);
-        } catch(RuntimeException e) {
-            logger.error("Error while sending message {} to {}", request, netconfClient);
+            NetconfMessage netconfMessage = netconfClient.sendMessage(request,
+                    configuration.netconfSendMessageMaxAttempts, configuration.netconfSendMessageDelayMs);
+            NetconfUtil.checkIsMessageOk(netconfMessage);
+            return netconfMessage;
+        } catch(ConflictingVersionException e) {
+            logger.trace("conflicting version detected: {}", e.toString());
             throw e;
+        } catch (RuntimeException | ExecutionException | InterruptedException | TimeoutException e) { // TODO: change NetconfClient#sendMessage to throw checked exceptions
+            logger.debug("Error while executing netconf transaction {} to {}", request, netconfClient, e);
+            throw new IOException("Failed to execute netconf transaction", e);
         }
     }
 
-    private static NetconfMessage createEditConfigMessage(Element dataElement, String editConfigResourcename) {
-        try (InputStream stream = ConfigPersisterNotificationHandler.class.getResourceAsStream(editConfigResourcename)) {
-            Preconditions.checkNotNull(stream, "Unable to load resource " + editConfigResourcename);
+
+    // load editConfig.xml template, populate /rpc/edit-config/config with parameter
+    private static NetconfMessage createEditConfigMessage(Element dataElement) {
+        String editConfigResourcePath = "/netconfOp/editConfig.xml";
+        try (InputStream stream = ConfigPersisterNotificationHandler.class.getResourceAsStream(editConfigResourcePath)) {
+            Preconditions.checkNotNull(stream, "Unable to load resource " + editConfigResourcePath);
 
             Document doc = XmlUtil.readXmlToDocument(stream);
 
-            doc.getDocumentElement();
             XmlElement editConfigElement = XmlElement.fromDomDocument(doc).getOnlyChildElement();
             XmlElement configWrapper = editConfigElement.getOnlyChildElement(XmlNetconfConstants.CONFIG_KEY);
             editConfigElement.getDomElement().removeChild(configWrapper.getDomElement());
             for (XmlElement el : XmlElement.fromDomElement(dataElement).getChildElements()) {
-                configWrapper.appendChild((Element) doc.importNode(el.getDomElement(), true));
+                boolean deep = true;
+                configWrapper.appendChild((Element) doc.importNode(el.getDomElement(), deep));
             }
             editConfigElement.appendChild(configWrapper.getDomElement());
             return new NetconfMessage(doc);
         } catch (IOException | SAXException e) {
-            throw new RuntimeException("Unable to parse message from resources " + editConfigResourcename, e);
+            // error reading the xml file bundled into the jar
+            throw new RuntimeException("Error while opening local resource " + editConfigResourcePath, e);
         }
     }
 
-    private static NetconfMessage getNetconfMessageFromResource(String resource) {
+    private static NetconfMessage getCommitMessage() {
+        String resource = "/netconfOp/commit.xml";
         try (InputStream stream = ConfigPusher.class.getResourceAsStream(resource)) {
             Preconditions.checkNotNull(stream, "Unable to load resource " + resource);
             return new NetconfMessage(XmlUtil.readXmlToDocument(stream));
         } catch (SAXException | IOException e) {
-            throw new RuntimeException("Unable to parse message from resources " + resource, e);
+            // error reading the xml file bundled into the jar
+            throw new RuntimeException("Error while opening local resource " + resource, e);
         }
     }
+
+    static class EditAndCommitResponse {
+        private final NetconfMessage editResponse, commitResponse;
+
+        EditAndCommitResponse(NetconfMessage editResponse, NetconfMessage commitResponse) {
+            this.editResponse = editResponse;
+            this.commitResponse = commitResponse;
+        }
+
+        public NetconfMessage getEditResponse() {
+            return editResponse;
+        }
+
+        public NetconfMessage getCommitResponse() {
+            return commitResponse;
+        }
+
+        @Override
+        public String toString() {
+            return "EditAndCommitResponse{" +
+                    "editResponse=" + editResponse +
+                    ", commitResponse=" + commitResponse +
+                    '}';
+        }
+    }
+
+
+    static class EditAndCommitResponseWithRetries {
+        private final EditAndCommitResponse editAndCommitResponse;
+        private final int retries;
+
+        EditAndCommitResponseWithRetries(EditAndCommitResponse editAndCommitResponse, int retries) {
+            this.editAndCommitResponse = editAndCommitResponse;
+            this.retries = retries;
+        }
+
+        public int getRetries() {
+            return retries;
+        }
+
+        public EditAndCommitResponse getEditAndCommitResponse() {
+            return editAndCommitResponse;
+        }
+
+        @Override
+        public String toString() {
+            return "EditAndCommitResponseWithRetries{" +
+                    "editAndCommitResponse=" + editAndCommitResponse +
+                    ", retries=" + retries +
+                    '}';
+        }
+    }
+
 }