BUG-1135 Improve error reporting in config pusher
[controller.git] / opendaylight / netconf / config-persister-impl / src / main / java / org / opendaylight / controller / netconf / persist / impl / ConfigPusherImpl.java
index c20007d397b704b130d0ff7f281c5465588b44ff..0e179ad7d58c92e019c9a8c98c6d1bd8d8563d78 100644 (file)
@@ -33,10 +33,10 @@ import org.opendaylight.controller.config.api.ConflictingVersionException;
 import org.opendaylight.controller.config.persist.api.ConfigPusher;
 import org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder;
 import org.opendaylight.controller.config.persist.api.Persister;
+import org.opendaylight.controller.netconf.api.Capability;
 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
 import org.opendaylight.controller.netconf.api.NetconfMessage;
 import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
-import org.opendaylight.controller.netconf.mapping.api.Capability;
 import org.opendaylight.controller.netconf.mapping.api.HandlingPriority;
 import org.opendaylight.controller.netconf.mapping.api.NetconfOperation;
 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationChainedExecution;
@@ -115,17 +115,20 @@ public class ConfigPusherImpl implements ConfigPusher {
      */
     private synchronized EditAndCommitResponse pushConfigWithConflictingVersionRetries(ConfigSnapshotHolder configSnapshotHolder) throws NetconfDocumentedException {
         ConflictingVersionException lastException;
-        Stopwatch stopwatch = new Stopwatch().start();
+        Stopwatch stopwatch = Stopwatch.createUnstarted();
         do {
             String idForReporting = configSnapshotHolder.toString();
             SortedSet<String> expectedCapabilities = checkNotNull(configSnapshotHolder.getCapabilities(),
                     "Expected capabilities must not be null - %s, check %s", idForReporting,
                     configSnapshotHolder.getClass().getName());
             try (NetconfOperationService operationService = getOperationServiceWithRetries(expectedCapabilities, idForReporting)) {
+                if(!stopwatch.isRunning()) {
+                    stopwatch.start();
+                }
                 return pushConfig(configSnapshotHolder, operationService);
             } catch (ConflictingVersionException e) {
                 lastException = e;
-                LOG.debug("Conflicting version detected, will retry after timeout");
+                LOG.info("Conflicting version detected, will retry after timeout");
                 sleep();
             }
         } while (stopwatch.elapsed(TimeUnit.MILLISECONDS) < conflictingVersionTimeoutMillis);
@@ -134,29 +137,66 @@ public class ConfigPusherImpl implements ConfigPusher {
     }
 
     private NetconfOperationService getOperationServiceWithRetries(Set<String> expectedCapabilities, String idForReporting) {
-        Stopwatch stopwatch = new Stopwatch().start();
-        NotEnoughCapabilitiesException lastException;
+        Stopwatch stopwatch = Stopwatch.createStarted();
+        ConfigPusherException lastException;
         do {
             try {
                 return getOperationService(expectedCapabilities, idForReporting);
-            } catch (NotEnoughCapabilitiesException e) {
+            } catch (ConfigPusherException e) {
                 LOG.debug("Not enough capabilities: {}", e.toString());
                 lastException = e;
                 sleep();
             }
         } while (stopwatch.elapsed(TimeUnit.MILLISECONDS) < maxWaitForCapabilitiesMillis);
-        throw new IllegalStateException("Max wait for capabilities reached." + lastException.getMessage(), lastException);
+
+        if(lastException instanceof NotEnoughCapabilitiesException) {
+            LOG.error("Unable to push configuration due to missing yang models." +
+                            " Yang models that are missing, but required by the configuration: {}." +
+                            " For each mentioned model check: " +
+                            " 1. that the mentioned yang model namespace/name/revision is identical to those in the yang model itself" +
+                            " 2. the yang file is present in the system" +
+                            " 3. the bundle with that yang file is present in the system and active" +
+                            " 4. the yang parser did not fail while attempting to parse that model",
+                    ((NotEnoughCapabilitiesException) lastException).getMissingCaps());
+            throw new IllegalStateException("Unable to push configuration due to missing yang models." +
+                    " Required yang models that are missing: "
+                    + ((NotEnoughCapabilitiesException) lastException).getMissingCaps(), lastException);
+        } else {
+            final String msg = "Unable to push configuration due to missing netconf service";
+            LOG.error(msg, lastException);
+            throw new IllegalStateException(msg, lastException);
+        }
     }
 
-    private static class NotEnoughCapabilitiesException extends Exception {
-        private static final long serialVersionUID = 1L;
+    private static class ConfigPusherException extends Exception {
 
-        private NotEnoughCapabilitiesException(String message, Throwable cause) {
+        public ConfigPusherException(final String message) {
+            super(message);
+        }
+
+        public ConfigPusherException(final String message, final Throwable cause) {
             super(message, cause);
         }
+    }
 
-        private NotEnoughCapabilitiesException(String message) {
+    private static class NotEnoughCapabilitiesException extends ConfigPusherException {
+        private static final long serialVersionUID = 1L;
+        private Set<String> missingCaps;
+
+        private NotEnoughCapabilitiesException(String message, Set<String> missingCaps) {
             super(message);
+            this.missingCaps = missingCaps;
+        }
+
+        public Set<String> getMissingCaps() {
+            return missingCaps;
+        }
+    }
+
+    private static final class NetconfServiceNotAvailableException extends ConfigPusherException {
+
+        public NetconfServiceNotAvailableException(final String s, final RuntimeException e) {
+            super(s, e);
         }
     }
 
@@ -167,27 +207,28 @@ public class ConfigPusherImpl implements ConfigPusher {
      * @param idForReporting
      * @return service if capabilities are present, otherwise absent value
      */
-    private NetconfOperationService getOperationService(Set<String> expectedCapabilities, String idForReporting) throws NotEnoughCapabilitiesException {
+    private NetconfOperationService getOperationService(Set<String> expectedCapabilities, String idForReporting) throws ConfigPusherException {
         NetconfOperationService serviceCandidate;
         try {
             serviceCandidate = configNetconfConnector.createService(idForReporting);
         } catch(RuntimeException e) {
-            throw new NotEnoughCapabilitiesException("Netconf service not stable for " + idForReporting, e);
+            throw new NetconfServiceNotAvailableException("Netconf service not stable for config pusher." +
+                    " Cannot push any configuration", e);
         }
-        Set<String> notFoundDiff = computeNotFoundCapabilities(expectedCapabilities, serviceCandidate);
+        Set<String> notFoundDiff = computeNotFoundCapabilities(expectedCapabilities, configNetconfConnector);
         if (notFoundDiff.isEmpty()) {
             return serviceCandidate;
         } else {
             serviceCandidate.close();
-            LOG.trace("Netconf server did not provide required capabilities for {} ", idForReporting,
+            LOG.debug("Netconf server did not provide required capabilities for {} ", idForReporting,
                     "Expected but not found: {}, all expected {}, current {}",
-                     notFoundDiff, expectedCapabilities, serviceCandidate.getCapabilities()
+                     notFoundDiff, expectedCapabilities, configNetconfConnector.getCapabilities()
             );
-            throw new NotEnoughCapabilitiesException("Not enough capabilities for " + idForReporting + ". Expected but not found: " + notFoundDiff);
+            throw new NotEnoughCapabilitiesException("Not enough capabilities for " + idForReporting + ". Expected but not found: " + notFoundDiff, notFoundDiff);
         }
     }
 
-    private static Set<String> computeNotFoundCapabilities(Set<String> expectedCapabilities, NetconfOperationService serviceCandidate) {
+    private static Set<String> computeNotFoundCapabilities(Set<String> expectedCapabilities, NetconfOperationServiceFactory serviceCandidate) {
         Collection<String> actual = Collections2.transform(serviceCandidate.getCapabilities(), new Function<Capability, String>() {
             @Override
             public String apply(@Nonnull final Capability input) {
@@ -199,8 +240,6 @@ public class ConfigPusherImpl implements ConfigPusher {
         return allNotFound;
     }
 
-
-
     private void sleep() {
         try {
             Thread.sleep(100);
@@ -227,7 +266,7 @@ public class ConfigPusherImpl implements ConfigPusher {
             throw new IllegalStateException("Cannot parse " + configSnapshotHolder);
         }
         LOG.trace("Pushing last configuration to netconf: {}", configSnapshotHolder);
-        Stopwatch stopwatch = new Stopwatch().start();
+        Stopwatch stopwatch = Stopwatch.createStarted();
         NetconfMessage editConfigMessage = createEditConfigMessage(xmlToBePersisted);
 
         Document editResponseMessage = sendRequestGetResponseCheckIsOK(editConfigMessage, operationService,