X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fnetconf%2Fnetconf-impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fnetconf%2Fimpl%2Fosgi%2FNetconfOperationRouterImpl.java;h=56acf0f6487e9e33abcaaac18c522afb0c6cf548;hb=refs%2Fchanges%2F13%2F23413%2F26;hp=e16c0c9d9d680b855fd4a2906169e9eeeab06076;hpb=39f1673d2b50ee26475bf48a29e002622eca6a86;p=controller.git diff --git a/opendaylight/netconf/netconf-impl/src/main/java/org/opendaylight/controller/netconf/impl/osgi/NetconfOperationRouterImpl.java b/opendaylight/netconf/netconf-impl/src/main/java/org/opendaylight/controller/netconf/impl/osgi/NetconfOperationRouterImpl.java index e16c0c9d9d..56acf0f648 100644 --- a/opendaylight/netconf/netconf-impl/src/main/java/org/opendaylight/controller/netconf/impl/osgi/NetconfOperationRouterImpl.java +++ b/opendaylight/netconf/netconf-impl/src/main/java/org/opendaylight/controller/netconf/impl/osgi/NetconfOperationRouterImpl.java @@ -8,21 +8,19 @@ 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 java.util.Collection; import java.util.Collections; import java.util.HashSet; -import java.util.Map; import java.util.NavigableMap; import java.util.Set; import java.util.TreeMap; -import org.opendaylight.controller.netconf.api.NetconfDocumentedException; -import org.opendaylight.controller.netconf.impl.DefaultCommitNotificationProducer; +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.CapabilityProvider; 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; @@ -30,8 +28,7 @@ import org.opendaylight.controller.netconf.mapping.api.HandlingPriority; import org.opendaylight.controller.netconf.mapping.api.NetconfOperation; import org.opendaylight.controller.netconf.mapping.api.NetconfOperationChainedExecution; import org.opendaylight.controller.netconf.mapping.api.NetconfOperationService; -import org.opendaylight.controller.netconf.mapping.api.NetconfOperationServiceSnapshot; -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; @@ -39,61 +36,25 @@ import org.w3c.dom.Document; public class NetconfOperationRouterImpl implements NetconfOperationRouter { private static final Logger LOG = LoggerFactory.getLogger(NetconfOperationRouterImpl.class); + private final NetconfOperationService netconfOperationServiceSnapshot; + private final Collection allNetconfOperations; - private final NetconfOperationServiceSnapshot netconfOperationServiceSnapshot; - private Set allNetconfOperations; + public NetconfOperationRouterImpl(final NetconfOperationService netconfOperationServiceSnapshot, + final NetconfMonitoringService netconfMonitoringService, final String sessionId) { + this.netconfOperationServiceSnapshot = Preconditions.checkNotNull(netconfOperationServiceSnapshot); - private NetconfOperationRouterImpl(final NetconfOperationServiceSnapshot netconfOperationServiceSnapshot) { - this.netconfOperationServiceSnapshot = netconfOperationServiceSnapshot; - } - - private synchronized void initNetconfOperations(final Set allOperations) { - allNetconfOperations = allOperations; - } - - /** - * Factory method to produce instance of NetconfOperationRouter - */ - public static NetconfOperationRouter createOperationRouter(final NetconfOperationServiceSnapshot netconfOperationServiceSnapshot, - final CapabilityProvider capabilityProvider, final DefaultCommitNotificationProducer commitNotifier) { - NetconfOperationRouterImpl router = new NetconfOperationRouterImpl(netconfOperationServiceSnapshot); - - Preconditions.checkNotNull(netconfOperationServiceSnapshot); - Preconditions.checkNotNull(capabilityProvider); - - final String sessionId = netconfOperationServiceSnapshot.getNetconfSessionIdForReporting(); - - final Set 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)); + final Set ops = new HashSet<>(); + ops.add(new DefaultCloseSession(sessionId, this)); + ops.add(new DefaultStartExi(sessionId)); + ops.add(new DefaultStopExi(sessionId)); - router.initNetconfOperations(getAllNetconfOperations(defaultNetconfOperations, netconfOperationServiceSnapshot)); + ops.addAll(netconfOperationServiceSnapshot.getNetconfOperations()); - return router; - } - - private static Set getAllNetconfOperations(final Set defaultNetconfOperations, - final NetconfOperationServiceSnapshot netconfOperationServiceSnapshot) { - Set result = new HashSet<>(); - result.addAll(defaultNetconfOperations); - - for (NetconfOperationService netconfOperationService : netconfOperationServiceSnapshot.getServices()) { - final Set netOpsFromService = netconfOperationService.getNetconfOperations(); - for (NetconfOperation netconfOperation : netOpsFromService) { - Preconditions.checkState(!result.contains(netconfOperation), - "Netconf operation %s already present", netconfOperation); - result.add(netconfOperation); - } - } - return Collections.unmodifiableSet(result); + allNetconfOperations = ImmutableSet.copyOf(ops); } @Override - public synchronized Document onNetconfMessage(final Document message, - final NetconfServerSession session) throws NetconfDocumentedException { + public Document onNetconfMessage(final Document message, final NetconfServerSession session) throws DocumentedException { Preconditions.checkNotNull(allNetconfOperations, "Operation router was not initialized properly"); final NetconfOperationExecution netconfOperationExecution; @@ -103,17 +64,17 @@ public class NetconfOperationRouterImpl implements NetconfOperationRouter { final String messageAsString = XmlUtil.toString(message); LOG.warn("Unable to handle rpc {} on session {}", messageAsString, session, e); - final NetconfDocumentedException.ErrorTag tag; + final DocumentedException.ErrorTag tag; if (e instanceof IllegalArgumentException) { - tag = NetconfDocumentedException.ErrorTag.operation_not_supported; + tag = DocumentedException.ErrorTag.operation_not_supported; } else { - tag = NetconfDocumentedException.ErrorTag.operation_failed; + tag = DocumentedException.ErrorTag.operation_failed; } - throw new NetconfDocumentedException( + throw new DocumentedException( String.format("Unable to handle rpc %s on session %s", messageAsString, session), - e, NetconfDocumentedException.ErrorType.application, - tag, NetconfDocumentedException.ErrorSeverity.error, + 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); @@ -131,20 +92,18 @@ public class NetconfOperationRouterImpl implements NetconfOperationRouter { netconfOperationServiceSnapshot.close(); } - private NetconfDocumentedException handleUnexpectedEx(final String s, final Exception e) throws NetconfDocumentedException { - LOG.error(s, e); - - Map info = Maps.newHashMap(); - info.put(NetconfDocumentedException.ErrorSeverity.error.toString(), e.toString()); - return new NetconfDocumentedException("Unexpected error", - NetconfDocumentedException.ErrorType.application, - NetconfDocumentedException.ErrorTag.operation_failed, - NetconfDocumentedException.ErrorSeverity.error, info); + 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())); } private Document executeOperationWithHighestPriority(final Document message, final NetconfOperationExecution netconfOperationExecution) - throws NetconfDocumentedException { + throws DocumentedException { if (LOG.isDebugEnabled()) { LOG.debug("Forwarding netconf message {} to {}", XmlUtil.toString(message), netconfOperationExecution.netconfOperation); } @@ -153,7 +112,7 @@ public class NetconfOperationRouterImpl implements NetconfOperationRouter { } private NetconfOperationExecution getNetconfOperationWithHighestPriority( - final Document message, final NetconfServerSession session) throws NetconfDocumentedException { + final Document message, final NetconfServerSession session) throws DocumentedException { NavigableMap sortedByPriority = getSortedNetconfOperationsWithCanHandle( message, session); @@ -167,7 +126,7 @@ public class NetconfOperationRouterImpl implements NetconfOperationRouter { } private TreeMap getSortedNetconfOperationsWithCanHandle(final Document message, - final NetconfServerSession session) throws NetconfDocumentedException { + final NetconfServerSession session) throws DocumentedException { TreeMap sortedPriority = Maps.newTreeMap(); for (NetconfOperation netconfOperation : allNetconfOperations) { @@ -175,11 +134,14 @@ public class NetconfOperationRouterImpl implements NetconfOperationRouter { if (netconfOperation instanceof DefaultNetconfOperation) { ((DefaultNetconfOperation) netconfOperation).setNetconfSession(session); } + 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", - NetconfOperation.class.getName(), message, 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); } } @@ -193,11 +155,11 @@ public class NetconfOperationRouterImpl implements NetconfOperationRouter { } @Override - public Document execute(final Document requestMessage) throws NetconfDocumentedException { - throw new NetconfDocumentedException("This execution represents the termination point in operation execution and cannot be executed itself", - NetconfDocumentedException.ErrorType.application, - NetconfDocumentedException.ErrorTag.operation_failed, - NetconfDocumentedException.ErrorSeverity.error); + 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); } }; @@ -216,7 +178,7 @@ public class NetconfOperationRouterImpl implements NetconfOperationRouter { } @Override - public Document execute(final Document message) throws NetconfDocumentedException { + public Document execute(final Document message) throws DocumentedException { return netconfOperation.handle(message, subsequentExecution); }