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=2178d4eedffb1a280b8fa6bfe49a489d8be72138;hb=b5c49b7c32cae050b9a91ff07c0a001d7dfb0042;hp=2aa89ba2c48958d9990dada2f24f1f91ecb0f14a;hpb=0418d143fbc882b24becaa52ab115a4a42d3328c;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 2aa89ba2c4..2178d4eedf 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,11 +8,11 @@ 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; @@ -31,6 +31,7 @@ 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.mapping.api.SessionAwareNetconfOperation; import org.opendaylight.controller.netconf.util.xml.XmlUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -39,92 +40,62 @@ import org.w3c.dom.Document; public class NetconfOperationRouterImpl implements NetconfOperationRouter { private static final Logger LOG = LoggerFactory.getLogger(NetconfOperationRouterImpl.class); - private final NetconfOperationServiceSnapshot netconfOperationServiceSnapshot; - private Set allNetconfOperations; - - private NetconfOperationRouterImpl(final NetconfOperationServiceSnapshot netconfOperationServiceSnapshot) { - this.netconfOperationServiceSnapshot = netconfOperationServiceSnapshot; - } - - private synchronized void initNetconfOperations(final Set allOperations) { - allNetconfOperations = allOperations; - } + private final Collection allNetconfOperations; - /** - * 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); + public NetconfOperationRouterImpl(final NetconfOperationServiceSnapshot netconfOperationServiceSnapshot, final CapabilityProvider capabilityProvider, + final DefaultCommitNotificationProducer commitNotifier) { + this.netconfOperationServiceSnapshot = Preconditions.checkNotNull(netconfOperationServiceSnapshot); 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)); - - router.initNetconfOperations(getAllNetconfOperations(defaultNetconfOperations, netconfOperationServiceSnapshot)); - - return router; - } - - private static Set getAllNetconfOperations(final Set defaultNetconfOperations, - final NetconfOperationServiceSnapshot netconfOperationServiceSnapshot) { - Set result = new HashSet<>(); - result.addAll(defaultNetconfOperations); + final Set ops = new HashSet<>(); + ops.add(new DefaultGetSchema(capabilityProvider, sessionId)); + ops.add(new DefaultCloseSession(sessionId, this)); + ops.add(new DefaultStartExi(sessionId)); + ops.add(new DefaultStopExi(sessionId)); + ops.add(new DefaultCommit(commitNotifier, capabilityProvider, sessionId, this)); for (NetconfOperationService netconfOperationService : netconfOperationServiceSnapshot.getServices()) { - final Set netOpsFromService = netconfOperationService.getNetconfOperations(); - for (NetconfOperation netconfOperation : netOpsFromService) { - Preconditions.checkState(!result.contains(netconfOperation), + for (NetconfOperation netconfOperation : netconfOperationService.getNetconfOperations()) { + Preconditions.checkState(!ops.contains(netconfOperation), "Netconf operation %s already present", netconfOperation); - result.add(netconfOperation); + ops.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 NetconfDocumentedException { Preconditions.checkNotNull(allNetconfOperations, "Operation router was not initialized properly"); - NetconfOperationExecution netconfOperationExecution; - - String messageAsString = ""; + final NetconfOperationExecution netconfOperationExecution; try { - messageAsString = XmlUtil.toString(message); 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); - String errorMessage = String.format("Unable to handle rpc %s on session %s", messageAsString, session); - Map errorInfo = Maps.newHashMap(); - - NetconfDocumentedException.ErrorTag tag; + final NetconfDocumentedException.ErrorTag tag; if (e instanceof IllegalArgumentException) { - errorInfo.put(NetconfDocumentedException.ErrorTag.operation_not_supported.toString(), e.getMessage()); tag = NetconfDocumentedException.ErrorTag.operation_not_supported; } else { - errorInfo.put(NetconfDocumentedException.ErrorTag.operation_failed.toString(), e.getMessage()); tag = NetconfDocumentedException.ErrorTag.operation_failed; } - throw new NetconfDocumentedException(errorMessage, e, NetconfDocumentedException.ErrorType.application, - tag, NetconfDocumentedException.ErrorSeverity.error, errorInfo); + throw new NetconfDocumentedException( + String.format("Unable to handle rpc %s on session %s", messageAsString, session), + e, NetconfDocumentedException.ErrorType.application, + tag, NetconfDocumentedException.ErrorSeverity.error, + Collections.singletonMap(tag.toString(), e.getMessage())); } catch (RuntimeException e) { throw handleUnexpectedEx("Unexpected exception during netconf operation sort", e); } try { - return executeOperationWithHighestPriority(message, netconfOperationExecution, messageAsString); + return executeOperationWithHighestPriority(message, netconfOperationExecution); } catch (RuntimeException e) { throw handleUnexpectedEx("Unexpected exception during netconf operation execution", e); } @@ -135,21 +106,22 @@ 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()); + private static NetconfDocumentedException handleUnexpectedEx(final String s, final Exception e) throws NetconfDocumentedException { + LOG.error("{}", s, e); return new NetconfDocumentedException("Unexpected error", NetconfDocumentedException.ErrorType.application, NetconfDocumentedException.ErrorTag.operation_failed, - NetconfDocumentedException.ErrorSeverity.error, info); + NetconfDocumentedException.ErrorSeverity.error, + Collections.singletonMap(NetconfDocumentedException.ErrorSeverity.error.toString(), e.toString())); } private Document executeOperationWithHighestPriority(final Document message, - final NetconfOperationExecution netconfOperationExecution, final String messageAsString) + final NetconfOperationExecution netconfOperationExecution) throws NetconfDocumentedException { - LOG.debug("Forwarding netconf message {} to {}", messageAsString, netconfOperationExecution.netconfOperation); + if (LOG.isDebugEnabled()) { + LOG.debug("Forwarding netconf message {} to {}", XmlUtil.toString(message), netconfOperationExecution.netconfOperation); + } + return netconfOperationExecution.execute(message); } @@ -176,6 +148,9 @@ 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),