Remove netconf from commons/opendaylight pom
[controller.git] / opendaylight / netconf / netconf-impl / src / main / java / org / opendaylight / controller / netconf / impl / osgi / NetconfOperationRouterImpl.java
index a9bea344c9471853bc1654f708f8f9d99a61e619..56acf0f6487e9e33abcaaac18c522afb0c6cf548 100644 (file)
 package org.opendaylight.controller.netconf.impl.osgi;
 
 import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableSet;
 import com.google.common.collect.Maps;
-import com.google.common.collect.Sets;
-import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
-import org.opendaylight.controller.netconf.api.NetconfOperationRouter;
-import org.opendaylight.controller.netconf.impl.DefaultCommitNotificationProducer;
-import org.opendaylight.controller.netconf.impl.mapping.CapabilityProvider;
+import java.util.Collection;
+import java.util.Collections;
+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.controller.netconf.api.monitoring.NetconfMonitoringService;
+import org.opendaylight.controller.netconf.impl.NetconfServerSession;
 import org.opendaylight.controller.netconf.impl.mapping.operations.DefaultCloseSession;
-import org.opendaylight.controller.netconf.impl.mapping.operations.DefaultCommit;
-import org.opendaylight.controller.netconf.impl.mapping.operations.DefaultGetSchema;
+import org.opendaylight.controller.netconf.impl.mapping.operations.DefaultNetconfOperation;
+import org.opendaylight.controller.netconf.impl.mapping.operations.DefaultStartExi;
+import org.opendaylight.controller.netconf.impl.mapping.operations.DefaultStopExi;
 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.opendaylight.controller.netconf.mapping.api.SessionAwareNetconfOperation;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.w3c.dom.Document;
 
-import java.util.Collections;
-import java.util.HashSet;
-import java.util.Iterator;
-import java.util.LinkedList;
-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 final Set<NetconfOperation> allNetconfOperations;
-    private final TreeSet<NetconfOperationFilter> allSortedFilters;
+    private static final Logger LOG = LoggerFactory.getLogger(NetconfOperationRouterImpl.class);
+    private final NetconfOperationService netconfOperationServiceSnapshot;
+    private final Collection<NetconfOperation> allNetconfOperations;
 
-    private final CapabilityProvider capabilityProvider;
+    public NetconfOperationRouterImpl(final NetconfOperationService netconfOperationServiceSnapshot,
+                                      final NetconfMonitoringService netconfMonitoringService, final String sessionId) {
+        this.netconfOperationServiceSnapshot = Preconditions.checkNotNull(netconfOperationServiceSnapshot);
 
-    public NetconfOperationRouterImpl(NetconfOperationServiceSnapshot netconfOperationServiceSnapshot,
-            CapabilityProvider capabilityProvider, DefaultCommitNotificationProducer commitNotifier) {
+        final Set<NetconfOperation> ops = new HashSet<>();
+        ops.add(new DefaultCloseSession(sessionId, this));
+        ops.add(new DefaultStartExi(sessionId));
+        ops.add(new DefaultStopExi(sessionId));
 
-        this.netconfOperationServiceSnapshot = netconfOperationServiceSnapshot;
+        ops.addAll(netconfOperationServiceSnapshot.getNetconfOperations());
 
-        this.capabilityProvider = capabilityProvider;
-
-        Set<NetconfOperation> defaultNetconfOperations = Sets.newHashSet();
-        defaultNetconfOperations.add(new DefaultGetSchema(capabilityProvider, netconfOperationServiceSnapshot
-                .getNetconfSessionIdForReporting()));
-        defaultNetconfOperations.add(new DefaultCloseSession(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);
+        allNetconfOperations = ImmutableSet.copyOf(ops);
     }
 
-    private static Set<NetconfOperation> getAllNetconfOperations(Set<NetconfOperation> defaultNetconfOperations,
-            NetconfOperationServiceSnapshot netconfOperationServiceSnapshot) {
-        Set<NetconfOperation> result = new HashSet<>();
-        result.addAll(defaultNetconfOperations);
-
-        for (NetconfOperationService netconfOperationService : netconfOperationServiceSnapshot.getServices()) {
-            final Set<NetconfOperation> netOpsFromService = netconfOperationService.getNetconfOperations();
-            for (NetconfOperation netconfOperation : netOpsFromService) {
-                Preconditions.checkState(result.contains(netconfOperation) == false,
-                        "Netconf operation %s already present", netconfOperation);
-                result.add(netconfOperation);
+    @Override
+    public Document onNetconfMessage(final Document message, final NetconfServerSession session) throws DocumentedException {
+        Preconditions.checkNotNull(allNetconfOperations, "Operation router was not initialized properly");
+
+        final NetconfOperationExecution netconfOperationExecution;
+        try {
+            netconfOperationExecution = getNetconfOperationWithHighestPriority(message, session);
+        } catch (IllegalArgumentException | IllegalStateException e) {
+            final String messageAsString = XmlUtil.toString(message);
+            LOG.warn("Unable to handle rpc {} on session {}", messageAsString, session, e);
+
+            final DocumentedException.ErrorTag tag;
+            if (e instanceof IllegalArgumentException) {
+                tag = DocumentedException.ErrorTag.operation_not_supported;
+            } else {
+                tag = DocumentedException.ErrorTag.operation_failed;
             }
-        }
-        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);
-            }
+            throw new DocumentedException(
+                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()));
+        } catch (RuntimeException e) {
+            throw handleUnexpectedEx("Unexpected exception during netconf operation sort", e);
         }
-        return result;
-    }
 
-    public CapabilityProvider getCapabilityProvider() {
-        return capabilityProvider;
+        try {
+            return executeOperationWithHighestPriority(message, netconfOperationExecution);
+        } catch (RuntimeException e) {
+            throw handleUnexpectedEx("Unexpected exception during netconf operation execution", e);
+        }
     }
 
     @Override
-    public synchronized Document onNetconfMessage(Document message) throws NetconfDocumentedException {
-        NetconfOperationExecution netconfOperationExecution = getNetconfOperationWithHighestPriority(message);
-        logger.debug("Forwarding netconf message {} to {}", XmlUtil.toString(message),
-                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);
+    public void close() throws Exception {
+        netconfOperationServiceSnapshot.close();
     }
 
-    private NetconfOperationExecution getNetconfOperationWithHighestPriority(Document message) {
+    private static DocumentedException handleUnexpectedEx(final String s, final Exception e) throws DocumentedException {
+        LOG.error("{}", s, e);
+        return new DocumentedException("Unexpected error",
+                DocumentedException.ErrorType.application,
+                DocumentedException.ErrorTag.operation_failed,
+                DocumentedException.ErrorSeverity.error,
+                Collections.singletonMap(DocumentedException.ErrorSeverity.error.toString(), e.toString()));
+    }
 
-        // TODO test
-        TreeMap<HandlingPriority, Set<NetconfOperation>> sortedPriority = getSortedNetconfOperationsWithCanHandle(message);
+    private Document executeOperationWithHighestPriority(final Document message,
+            final NetconfOperationExecution netconfOperationExecution)
+            throws DocumentedException {
+        if (LOG.isDebugEnabled()) {
+            LOG.debug("Forwarding netconf message {} to {}", XmlUtil.toString(message), netconfOperationExecution.netconfOperation);
+        }
 
-        Preconditions.checkState(sortedPriority.isEmpty() == false, "No %s available to handle message %s",
-                NetconfOperation.class.getName(), XmlUtil.toString(message));
+        return netconfOperationExecution.execute(message);
+    }
 
-        HandlingPriority highestFoundPriority = sortedPriority.lastKey();
+    private NetconfOperationExecution getNetconfOperationWithHighestPriority(
+            final Document message, final NetconfServerSession session) throws DocumentedException {
 
-        int netconfOperationsWithHighestPriority = sortedPriority.get(highestFoundPriority).size();
+        NavigableMap<HandlingPriority, NetconfOperation> sortedByPriority = getSortedNetconfOperationsWithCanHandle(
+                message, session);
 
-        Preconditions.checkState(netconfOperationsWithHighestPriority == 1,
-                "Multiple %s available to handle message %s", NetconfOperation.class.getName(), message);
+        if (sortedByPriority.isEmpty()) {
+            throw new IllegalArgumentException(String.format("No %s available to handle message %s",
+                NetconfOperation.class.getName(), XmlUtil.toString(message)));
+        }
 
-        return new NetconfOperationExecution(sortedPriority, highestFoundPriority);
+        return NetconfOperationExecution.createExecutionChain(sortedByPriority, sortedByPriority.lastKey());
     }
 
-    private TreeMap<HandlingPriority, Set<NetconfOperation>> getSortedNetconfOperationsWithCanHandle(Document message) {
-        TreeMap<HandlingPriority, Set<NetconfOperation>> sortedPriority = Maps.newTreeMap();
+    private TreeMap<HandlingPriority, NetconfOperation> getSortedNetconfOperationsWithCanHandle(final Document message,
+            final NetconfServerSession session) throws DocumentedException {
+        TreeMap<HandlingPriority, NetconfOperation> sortedPriority = Maps.newTreeMap();
 
         for (NetconfOperation netconfOperation : allNetconfOperations) {
             final HandlingPriority handlingPriority = netconfOperation.canHandle(message);
+            if (netconfOperation instanceof DefaultNetconfOperation) {
+                ((DefaultNetconfOperation) netconfOperation).setNetconfSession(session);
+            }
+            if(netconfOperation instanceof SessionAwareNetconfOperation) {
+                ((SessionAwareNetconfOperation) netconfOperation).setSession(session);
+            }
+            if (!handlingPriority.equals(HandlingPriority.CANNOT_HANDLE)) {
 
-            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),
+                        "Multiple %s available to handle message %s with priority %s, %s and %s",
+                        NetconfOperation.class.getName(), message, handlingPriority, netconfOperation, sortedPriority.get(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);
+    public static final NetconfOperationChainedExecution EXECUTION_TERMINATION_POINT = new NetconfOperationChainedExecution() {
+        @Override
+        public boolean isExecutionTermination() {
+            return true;
         }
-        return netconfOperations;
-    }
 
-    @Override
-    public void close() {
-        netconfOperationServiceSnapshot.close();
-    }
+        @Override
+        public Document execute(final Document requestMessage) throws DocumentedException {
+            throw new DocumentedException("This execution represents the termination point in operation execution and cannot be executed itself",
+                    DocumentedException.ErrorType.application,
+                    DocumentedException.ErrorTag.operation_failed,
+                    DocumentedException.ErrorSeverity.error);
+        }
+    };
 
-    private class NetconfOperationExecution implements NetconfOperationFilterChain {
-        private final NetconfOperation operationWithHighestPriority;
+    private static class NetconfOperationExecution implements NetconfOperationChainedExecution {
+        private final NetconfOperation netconfOperation;
+        private final NetconfOperationChainedExecution subsequentExecution;
 
-        private NetconfOperationExecution(NetconfOperation operationWithHighestPriority) {
-            this.operationWithHighestPriority = operationWithHighestPriority;
+        private NetconfOperationExecution(final NetconfOperation netconfOperation, final 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(final Document message) throws DocumentedException {
+            return netconfOperation.handle(message, subsequentExecution);
+        }
+
+        public static NetconfOperationExecution createExecutionChain(
+                final NavigableMap<HandlingPriority, NetconfOperation> sortedByPriority, final 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);
         }
     }