Merge "Resolve Bug:593. Persister should communicate via OSGi SR instead of TCP."
[controller.git] / opendaylight / netconf / netconf-impl / src / main / java / org / opendaylight / controller / netconf / impl / osgi / NetconfOperationRouterImpl.java
index 11b8dc623c8a07903df1d05da46407aef1770a47..a35851445381f8bdcfd3142d036b9c3c34280d13 100644 (file)
@@ -23,8 +23,7 @@ import org.opendaylight.controller.netconf.impl.mapping.operations.DefaultStopEx
 import org.opendaylight.controller.netconf.mapping.api.DefaultNetconfOperation;
 import org.opendaylight.controller.netconf.mapping.api.HandlingPriority;
 import org.opendaylight.controller.netconf.mapping.api.NetconfOperation;
-import org.opendaylight.controller.netconf.mapping.api.NetconfOperationFilter;
-import org.opendaylight.controller.netconf.mapping.api.NetconfOperationFilterChain;
+import org.opendaylight.controller.netconf.mapping.api.NetconfOperationChainedExecution;
 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationService;
 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
 import org.slf4j.Logger;
@@ -33,51 +32,47 @@ import org.w3c.dom.Document;
 
 import java.util.Collections;
 import java.util.HashSet;
-import java.util.Iterator;
-import java.util.LinkedList;
 import java.util.Map;
 import java.util.Set;
 import java.util.TreeMap;
-import java.util.TreeSet;
 
 public class NetconfOperationRouterImpl implements NetconfOperationRouter {
 
     private static final Logger logger = LoggerFactory.getLogger(NetconfOperationRouterImpl.class);
 
     private final NetconfOperationServiceSnapshot netconfOperationServiceSnapshot;
+    private Set<NetconfOperation> allNetconfOperations;
 
-    private final Set<NetconfOperation> allNetconfOperations;
-    private final TreeSet<NetconfOperationFilter> allSortedFilters;
+    private NetconfOperationRouterImpl(NetconfOperationServiceSnapshot netconfOperationServiceSnapshot) {
+        this.netconfOperationServiceSnapshot = netconfOperationServiceSnapshot;
+    }
+
+    private void initNetconfOperations(Set<NetconfOperation> allOperations) {
+        allNetconfOperations = allOperations;
+    }
 
-    private final CapabilityProvider capabilityProvider;
+    /**
+     * Factory method to produce instance of NetconfOperationRouter
+     */
+    public static NetconfOperationRouter createOperationRouter(NetconfOperationServiceSnapshot netconfOperationServiceSnapshot,
+                                                               CapabilityProvider capabilityProvider, DefaultCommitNotificationProducer commitNotifier) {
+        NetconfOperationRouterImpl router = new NetconfOperationRouterImpl(netconfOperationServiceSnapshot);
 
+        Preconditions.checkNotNull(netconfOperationServiceSnapshot);
+        Preconditions.checkNotNull(capabilityProvider);
 
-    public NetconfOperationRouterImpl(NetconfOperationServiceSnapshot netconfOperationServiceSnapshot,
-            CapabilityProvider capabilityProvider,
-            DefaultCommitNotificationProducer commitNotifier) {
+        final String sessionId = netconfOperationServiceSnapshot.getNetconfSessionIdForReporting();
 
-        this.netconfOperationServiceSnapshot = netconfOperationServiceSnapshot;
+        final Set<NetconfOperation> defaultNetconfOperations = Sets.newHashSet();
+        defaultNetconfOperations.add(new DefaultGetSchema(capabilityProvider, sessionId));
+        defaultNetconfOperations.add(new DefaultCloseSession(sessionId, router));
+        defaultNetconfOperations.add(new DefaultStartExi(sessionId));
+        defaultNetconfOperations.add(new DefaultStopExi(sessionId));
+        defaultNetconfOperations.add(new DefaultCommit(commitNotifier, capabilityProvider, sessionId, router));
+
+        router.initNetconfOperations(getAllNetconfOperations(defaultNetconfOperations, netconfOperationServiceSnapshot));
 
-        this.capabilityProvider = capabilityProvider;
-
-        Set<NetconfOperation> defaultNetconfOperations = Sets.newHashSet();
-        defaultNetconfOperations.add(new DefaultGetSchema(capabilityProvider, netconfOperationServiceSnapshot
-                .getNetconfSessionIdForReporting()));
-        defaultNetconfOperations.add(new DefaultCloseSession(netconfOperationServiceSnapshot
-                .getNetconfSessionIdForReporting()));
-        defaultNetconfOperations.add(new DefaultStartExi(
-                netconfOperationServiceSnapshot
-                        .getNetconfSessionIdForReporting()));
-        defaultNetconfOperations.add(new DefaultStopExi(
-                netconfOperationServiceSnapshot
-                        .getNetconfSessionIdForReporting()));
-
-        allNetconfOperations = getAllNetconfOperations(defaultNetconfOperations, netconfOperationServiceSnapshot);
-
-        DefaultCommit defaultCommit = new DefaultCommit(commitNotifier, capabilityProvider,
-                netconfOperationServiceSnapshot.getNetconfSessionIdForReporting());
-        Set<NetconfOperationFilter> defaultFilters = Sets.<NetconfOperationFilter> newHashSet(defaultCommit);
-        allSortedFilters = getAllNetconfFilters(defaultFilters, netconfOperationServiceSnapshot);
+        return router;
     }
 
     private static Set<NetconfOperation> getAllNetconfOperations(Set<NetconfOperation> defaultNetconfOperations,
@@ -96,28 +91,12 @@ public class NetconfOperationRouterImpl implements NetconfOperationRouter {
         return Collections.unmodifiableSet(result);
     }
 
-    private static TreeSet<NetconfOperationFilter> getAllNetconfFilters(Set<NetconfOperationFilter> defaultFilters,
-            NetconfOperationServiceSnapshot netconfOperationServiceSnapshot) {
-        TreeSet<NetconfOperationFilter> result = new TreeSet<>(defaultFilters);
-        for (NetconfOperationService netconfOperationService : netconfOperationServiceSnapshot.getServices()) {
-            final Set<NetconfOperationFilter> filtersFromService = netconfOperationService.getFilters();
-            for (NetconfOperationFilter filter : filtersFromService) {
-                Preconditions.checkState(result.contains(filter) == false, "Filter %s already present", filter);
-                result.add(filter);
-            }
-        }
-        return result;
-    }
-
-    public CapabilityProvider getCapabilityProvider() {
-        return capabilityProvider;
-    }
-
     @Override
     public synchronized Document onNetconfMessage(Document message,
             NetconfSession session) throws NetconfDocumentedException {
-        NetconfOperationExecution netconfOperationExecution = null;
+        Preconditions.checkNotNull(allNetconfOperations, "Operation router was not initialized properly");
 
+        NetconfOperationExecution netconfOperationExecution;
         String messageAsString = XmlUtil.toString(message);
 
         try {
@@ -150,6 +129,11 @@ public class NetconfOperationRouterImpl implements NetconfOperationRouter {
         }
     }
 
+    @Override
+    public void close() {
+        netconfOperationServiceSnapshot.close();
+    }
+
     private NetconfDocumentedException handleUnexpectedEx(String s, Exception e) throws NetconfDocumentedException {
         logger.error(s, e);
 
@@ -161,99 +145,79 @@ public class NetconfOperationRouterImpl implements NetconfOperationRouter {
                 NetconfDocumentedException.ErrorSeverity.error, info);
     }
 
-    private Document executeOperationWithHighestPriority(Document message, NetconfOperationExecution netconfOperationExecution, String messageAsString) throws NetconfDocumentedException {
-        logger.debug("Forwarding netconf message {} to {}", messageAsString,
-                netconfOperationExecution.operationWithHighestPriority);
-
-        final LinkedList<NetconfOperationFilterChain> chain = new LinkedList<>();
-        chain.push(netconfOperationExecution);
-
-        for (Iterator<NetconfOperationFilter> it = allSortedFilters.descendingIterator(); it.hasNext();) {
-            final NetconfOperationFilter filter = it.next();
-            final NetconfOperationFilterChain prevItem = chain.getFirst();
-            NetconfOperationFilterChain currentItem = new NetconfOperationFilterChain() {
-                @Override
-                public Document execute(Document message, NetconfOperationRouter operationRouter)
-                        throws NetconfDocumentedException {
-                    logger.trace("Entering {}", filter);
-                    return filter.doFilter(message, operationRouter, prevItem);
-                }
-            };
-            chain.push(currentItem);
-        }
-        return chain.getFirst().execute(message, this);
+    private Document executeOperationWithHighestPriority(Document message,
+            NetconfOperationExecution netconfOperationExecution, String messageAsString)
+            throws NetconfDocumentedException {
+        logger.debug("Forwarding netconf message {} to {}", messageAsString, netconfOperationExecution.netconfOperation);
+        return netconfOperationExecution.execute(message);
     }
 
     private NetconfOperationExecution getNetconfOperationWithHighestPriority(
             Document message, NetconfSession session) {
 
-        // TODO test
-        TreeMap<HandlingPriority, Set<NetconfOperation>> sortedPriority = getSortedNetconfOperationsWithCanHandle(
+        TreeMap<HandlingPriority, NetconfOperation> sortedByPriority = getSortedNetconfOperationsWithCanHandle(
                 message, session);
 
-        Preconditions.checkArgument(sortedPriority.isEmpty() == false, "No %s available to handle message %s",
-                NetconfOperation.class.getName(), XmlUtil.toString(message));
-
-        HandlingPriority highestFoundPriority = sortedPriority.lastKey();
-
-        int netconfOperationsWithHighestPriority = sortedPriority.get(highestFoundPriority).size();
+        Preconditions.checkArgument(sortedByPriority.isEmpty() == false,
+                "No %s available to handleWithNoSubsequentOperations message %s", NetconfOperation.class.getName(),
+                XmlUtil.toString(message));
 
-        Preconditions.checkState(netconfOperationsWithHighestPriority == 1,
-                "Multiple %s available to handle message %s", NetconfOperation.class.getName(), message);
-
-        return new NetconfOperationExecution(sortedPriority, highestFoundPriority);
+        return NetconfOperationExecution.createExecutionChain(sortedByPriority, sortedByPriority.lastKey());
     }
 
-    private TreeMap<HandlingPriority, Set<NetconfOperation>> getSortedNetconfOperationsWithCanHandle(
-            Document message, NetconfSession session) {
-        TreeMap<HandlingPriority, Set<NetconfOperation>> sortedPriority = Maps.newTreeMap();
+    private TreeMap<HandlingPriority, NetconfOperation> getSortedNetconfOperationsWithCanHandle(Document message,
+            NetconfSession session) {
+        TreeMap<HandlingPriority, NetconfOperation> sortedPriority = Maps.newTreeMap();
 
         for (NetconfOperation netconfOperation : allNetconfOperations) {
             final HandlingPriority handlingPriority = netconfOperation.canHandle(message);
             if (netconfOperation instanceof DefaultNetconfOperation) {
-                ((DefaultNetconfOperation) netconfOperation)
-                        .setNetconfSession(session);
+                ((DefaultNetconfOperation) netconfOperation).setNetconfSession(session);
             }
             if (handlingPriority.equals(HandlingPriority.CANNOT_HANDLE) == false) {
-                Set<NetconfOperation> netconfOperations = sortedPriority.get(handlingPriority);
-                netconfOperations = checkIfNoOperationsOnPriority(sortedPriority, handlingPriority, netconfOperations);
-                netconfOperations.add(netconfOperation);
+
+                Preconditions.checkState(sortedPriority.containsKey(handlingPriority) == false,
+                        "Multiple %s available to handle message %s with priority %s",
+                        NetconfOperation.class.getName(), message, handlingPriority);
+                sortedPriority.put(handlingPriority, netconfOperation);
             }
         }
         return sortedPriority;
     }
 
-    private Set<NetconfOperation> checkIfNoOperationsOnPriority(
-            TreeMap<HandlingPriority, Set<NetconfOperation>> sortedPriority, HandlingPriority handlingPriority,
-            Set<NetconfOperation> netconfOperations) {
-        if (netconfOperations == null) {
-            netconfOperations = Sets.newHashSet();
-            sortedPriority.put(handlingPriority, netconfOperations);
-        }
-        return netconfOperations;
-    }
-
-    @Override
-    public void close() {
-        netconfOperationServiceSnapshot.close();
-    }
-
-    private class NetconfOperationExecution implements NetconfOperationFilterChain {
-        private final NetconfOperation operationWithHighestPriority;
+    private static class NetconfOperationExecution implements NetconfOperationChainedExecution {
+        private final NetconfOperation netconfOperation;
+        private NetconfOperationChainedExecution subsequentExecution;
 
-        private NetconfOperationExecution(NetconfOperation operationWithHighestPriority) {
-            this.operationWithHighestPriority = operationWithHighestPriority;
+        private NetconfOperationExecution(NetconfOperation netconfOperation, NetconfOperationChainedExecution subsequentExecution) {
+            this.netconfOperation = netconfOperation;
+            this.subsequentExecution = subsequentExecution;
         }
 
-        public NetconfOperationExecution(TreeMap<HandlingPriority, Set<NetconfOperation>> sortedPriority,
-                HandlingPriority highestFoundPriority) {
-            operationWithHighestPriority = sortedPriority.get(highestFoundPriority).iterator().next();
-            sortedPriority.remove(highestFoundPriority);
+        @Override
+        public boolean isExecutionTermination() {
+            return false;
         }
 
         @Override
-        public Document execute(Document message, NetconfOperationRouter router) throws NetconfDocumentedException {
-            return operationWithHighestPriority.handle(message, router);
+        public Document execute(Document message) throws NetconfDocumentedException {
+            return netconfOperation.handle(message, subsequentExecution);
+        }
+
+        public static NetconfOperationExecution createExecutionChain(
+                TreeMap<HandlingPriority, NetconfOperation> sortedByPriority, HandlingPriority handlingPriority) {
+            NetconfOperation netconfOperation = sortedByPriority.get(handlingPriority);
+            HandlingPriority subsequentHandlingPriority = sortedByPriority.lowerKey(handlingPriority);
+
+            NetconfOperationChainedExecution subsequentExecution = null;
+
+            if (subsequentHandlingPriority != null) {
+                subsequentExecution = createExecutionChain(sortedByPriority, subsequentHandlingPriority);
+            } else {
+                subsequentExecution = EXECUTION_TERMINATION_POINT;
+            }
+
+            return new NetconfOperationExecution(netconfOperation, subsequentExecution);
         }
     }
 
@@ -262,7 +226,4 @@ public class NetconfOperationRouterImpl implements NetconfOperationRouter {
         return "NetconfOperationRouterImpl{" + "netconfOperationServiceSnapshot=" + netconfOperationServiceSnapshot
                 + '}';
     }
-
-
-
 }