Merge "Simplify code using Java 8 features"
[netconf.git] / netconf / netconf-impl / src / main / java / org / opendaylight / netconf / impl / osgi / NetconfOperationRouterImpl.java
index 1e118129242522058a5deedef766136ae0d216ae..449b0db530585cf752744f92c6bbb3ba387b6435 100644 (file)
@@ -16,9 +16,9 @@ import java.util.HashSet;
 import java.util.NavigableMap;
 import java.util.Set;
 import java.util.TreeMap;
-import org.opendaylight.controller.config.util.xml.DocumentedException;
-import org.opendaylight.controller.config.util.xml.XmlUtil;
+import org.opendaylight.netconf.api.DocumentedException;
 import org.opendaylight.netconf.api.monitoring.NetconfMonitoringService;
+import org.opendaylight.netconf.api.xml.XmlUtil;
 import org.opendaylight.netconf.impl.NetconfServerSession;
 import org.opendaylight.netconf.impl.mapping.operations.DefaultCloseSession;
 import org.opendaylight.netconf.impl.mapping.operations.DefaultNetconfOperation;
@@ -53,8 +53,10 @@ public class NetconfOperationRouterImpl implements NetconfOperationRouter {
         allNetconfOperations = ImmutableSet.copyOf(ops);
     }
 
+    @SuppressWarnings("checkstyle:IllegalCatch")
     @Override
-    public Document onNetconfMessage(final Document message, final NetconfServerSession session) throws DocumentedException {
+    public Document onNetconfMessage(final Document message, final NetconfServerSession session) throws
+            DocumentedException {
         Preconditions.checkNotNull(allNetconfOperations, "Operation router was not initialized properly");
 
         final NetconfOperationExecution netconfOperationExecution;
@@ -72,10 +74,10 @@ public class NetconfOperationRouterImpl implements NetconfOperationRouter {
             }
 
             throw new DocumentedException(
-                String.format("Unable to handle rpc %s on session %s", messageAsString, session),
+                    String.format("Unable to handle rpc %s on session %s", messageAsString, session),
                     e, DocumentedException.ErrorType.APPLICATION,
                     tag, DocumentedException.ErrorSeverity.ERROR,
-                Collections.singletonMap(tag.toString(), e.getMessage()));
+                    Collections.singletonMap(tag.toString(), e.getMessage()));
         } catch (final RuntimeException e) {
             throw handleUnexpectedEx("Unexpected exception during netconf operation sort", e);
         }
@@ -88,24 +90,24 @@ public class NetconfOperationRouterImpl implements NetconfOperationRouter {
     }
 
     @Override
-    public void close() throws Exception {
+    public void close() {
         netconfOperationServiceSnapshot.close();
     }
 
-    private static DocumentedException handleUnexpectedEx(final String s, final Exception e) throws DocumentedException {
-        LOG.error("{}", s, e);
+    private static DocumentedException handleUnexpectedEx(final String message, final Exception exception) {
+        LOG.error("{}", message, exception);
         return new DocumentedException("Unexpected error",
                 DocumentedException.ErrorType.APPLICATION,
                 DocumentedException.ErrorTag.OPERATION_FAILED,
                 DocumentedException.ErrorSeverity.ERROR,
-                Collections.singletonMap(DocumentedException.ErrorSeverity.ERROR.toString(), e.toString()));
+                Collections.singletonMap(DocumentedException.ErrorSeverity.ERROR.toString(), exception.toString()));
     }
 
-    private Document executeOperationWithHighestPriority(final Document message,
-            final NetconfOperationExecution netconfOperationExecution)
-            throws DocumentedException {
+    private static Document executeOperationWithHighestPriority(final Document message,
+            final NetconfOperationExecution netconfOperationExecution) throws DocumentedException {
         if (LOG.isDebugEnabled()) {
-            LOG.debug("Forwarding netconf message {} to {}", XmlUtil.toString(message), netconfOperationExecution.netconfOperation);
+            LOG.debug("Forwarding netconf message {} to {}", XmlUtil.toString(message), netconfOperationExecution
+                    .netconfOperation);
         }
 
         return netconfOperationExecution.execute(message);
@@ -114,19 +116,20 @@ public class NetconfOperationRouterImpl implements NetconfOperationRouter {
     private NetconfOperationExecution getNetconfOperationWithHighestPriority(
             final Document message, final NetconfServerSession session) throws DocumentedException {
 
-        final NavigableMap<HandlingPriority, NetconfOperation> sortedByPriority = getSortedNetconfOperationsWithCanHandle(
+        final NavigableMap<HandlingPriority, NetconfOperation> sortedByPriority =
+                getSortedNetconfOperationsWithCanHandle(
                 message, session);
 
         if (sortedByPriority.isEmpty()) {
             throw new IllegalArgumentException(String.format("No %s available to handle message %s",
-                NetconfOperation.class.getName(), XmlUtil.toString(message)));
+                    NetconfOperation.class.getName(), XmlUtil.toString(message)));
         }
 
         return NetconfOperationExecution.createExecutionChain(sortedByPriority, sortedByPriority.lastKey());
     }
 
-    private TreeMap<HandlingPriority, NetconfOperation> getSortedNetconfOperationsWithCanHandle(final Document message,
-            final NetconfServerSession session) throws DocumentedException {
+    private TreeMap<HandlingPriority, NetconfOperation> getSortedNetconfOperationsWithCanHandle(
+            final Document message, final NetconfServerSession session) throws DocumentedException {
         final TreeMap<HandlingPriority, NetconfOperation> sortedPriority = Maps.newTreeMap();
 
         for (final NetconfOperation netconfOperation : allNetconfOperations) {
@@ -134,25 +137,27 @@ public class NetconfOperationRouterImpl implements NetconfOperationRouter {
             if (netconfOperation instanceof DefaultNetconfOperation) {
                 ((DefaultNetconfOperation) netconfOperation).setNetconfSession(session);
             }
-            if(netconfOperation instanceof SessionAwareNetconfOperation) {
+            if (netconfOperation instanceof SessionAwareNetconfOperation) {
                 ((SessionAwareNetconfOperation) netconfOperation).setSession(session);
             }
             if (!handlingPriority.equals(HandlingPriority.CANNOT_HANDLE)) {
 
                 Preconditions.checkState(!sortedPriority.containsKey(handlingPriority),
                         "Multiple %s available to handle message %s with priority %s, %s and %s",
-                        NetconfOperation.class.getName(), message, handlingPriority, netconfOperation, sortedPriority.get(handlingPriority));
+                        NetconfOperation.class.getName(), message, handlingPriority, netconfOperation, sortedPriority
+                                .get(handlingPriority));
                 sortedPriority.put(handlingPriority, netconfOperation);
             }
         }
         return sortedPriority;
     }
 
-    private static class NetconfOperationExecution implements NetconfOperationChainedExecution {
+    private static final class NetconfOperationExecution implements NetconfOperationChainedExecution {
         private final NetconfOperation netconfOperation;
         private final NetconfOperationChainedExecution subsequentExecution;
 
-        private NetconfOperationExecution(final NetconfOperation netconfOperation, final NetconfOperationChainedExecution subsequentExecution) {
+        private NetconfOperationExecution(final NetconfOperation netconfOperation,
+                                          final NetconfOperationChainedExecution subsequentExecution) {
             this.netconfOperation = netconfOperation;
             this.subsequentExecution = subsequentExecution;
         }
@@ -168,7 +173,8 @@ public class NetconfOperationRouterImpl implements NetconfOperationRouter {
         }
 
         public static NetconfOperationExecution createExecutionChain(
-                final NavigableMap<HandlingPriority, NetconfOperation> sortedByPriority, final HandlingPriority handlingPriority) {
+                final NavigableMap<HandlingPriority, NetconfOperation> sortedByPriority,
+                final HandlingPriority handlingPriority) {
             final NetconfOperation netconfOperation = sortedByPriority.get(handlingPriority);
             final HandlingPriority subsequentHandlingPriority = sortedByPriority.lowerKey(handlingPriority);