X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fnetconf%2Fnetconf-impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fnetconf%2Fimpl%2Fosgi%2FNetconfOperationRouterImpl.java;h=2178d4eedffb1a280b8fa6bfe49a489d8be72138;hp=d70a15c18bdf933d56df70d7f7e9be07df6653da;hb=83dfe301bf2a2b1eff6883a2af3282c95d5a752e;hpb=515e60d60ac7dd08aa2440468cd8dab42892e7d0 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 d70a15c18b..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,249 +8,208 @@ 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.NetconfSession; +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.netconf.api.NetconfDocumentedException; -import org.opendaylight.controller.netconf.api.NetconfOperationRouter; import org.opendaylight.controller.netconf.impl.DefaultCommitNotificationProducer; +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; -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.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; 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 static final Logger LOG = LoggerFactory.getLogger(NetconfOperationRouterImpl.class); private final NetconfOperationServiceSnapshot netconfOperationServiceSnapshot; + private final Collection allNetconfOperations; - private final Set allNetconfOperations; - private final TreeSet allSortedFilters; - - private final CapabilityProvider capabilityProvider; - - - public NetconfOperationRouterImpl(NetconfOperationServiceSnapshot netconfOperationServiceSnapshot, - CapabilityProvider capabilityProvider, DefaultCommitNotificationProducer commitNotifier) { - - this.netconfOperationServiceSnapshot = netconfOperationServiceSnapshot; + public NetconfOperationRouterImpl(final NetconfOperationServiceSnapshot netconfOperationServiceSnapshot, final CapabilityProvider capabilityProvider, + final DefaultCommitNotificationProducer commitNotifier) { + this.netconfOperationServiceSnapshot = Preconditions.checkNotNull(netconfOperationServiceSnapshot); - this.capabilityProvider = capabilityProvider; + final String sessionId = netconfOperationServiceSnapshot.getNetconfSessionIdForReporting(); - Set 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 defaultFilters = Sets. newHashSet(defaultCommit); - allSortedFilters = getAllNetconfFilters(defaultFilters, netconfOperationServiceSnapshot); - } - - private static Set getAllNetconfOperations(Set defaultNetconfOperations, - 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) == false, + 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); - } - private static TreeSet getAllNetconfFilters(Set defaultFilters, - NetconfOperationServiceSnapshot netconfOperationServiceSnapshot) { - TreeSet result = new TreeSet<>(defaultFilters); - for (NetconfOperationService netconfOperationService : netconfOperationServiceSnapshot.getServices()) { - final Set filtersFromService = netconfOperationService.getFilters(); - for (NetconfOperationFilter filter : filtersFromService) { - Preconditions.checkState(result.contains(filter) == false, - "Filter %s already present, all filters so far: %s", filter, result); - result.add(filter); - } - } - return result; - } - - public CapabilityProvider getCapabilityProvider() { - return capabilityProvider; + allNetconfOperations = ImmutableSet.copyOf(ops); } @Override - public synchronized Document onNetconfMessage(Document message, - NetconfSession session) throws NetconfDocumentedException { - NetconfOperationExecution netconfOperationExecution; - - String messageAsString = XmlUtil.toString(message); + public Document onNetconfMessage(final Document message, final NetconfServerSession session) throws NetconfDocumentedException { + Preconditions.checkNotNull(allNetconfOperations, "Operation router was not initialized properly"); + final NetconfOperationExecution netconfOperationExecution; try { netconfOperationExecution = getNetconfOperationWithHighestPriority(message, session); } catch (IllegalArgumentException | IllegalStateException e) { - logger.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(); + final String messageAsString = XmlUtil.toString(message); + LOG.warn("Unable to handle rpc {} on session {}", messageAsString, session, e); - NetconfDocumentedException.ErrorTag tag = null; + 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 if (e instanceof IllegalStateException) { - errorInfo.put(NetconfDocumentedException.ErrorTag.operation_failed.toString(), e.getMessage()); + } else { 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); } } - private NetconfDocumentedException handleUnexpectedEx(String s, Exception e) throws NetconfDocumentedException { - logger.error(s, e); + @Override + public void close() throws Exception { + netconfOperationServiceSnapshot.close(); + } - 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(Document message, NetconfOperationExecution netconfOperationExecution, String messageAsString) throws NetconfDocumentedException { - logger.debug("Forwarding netconf message {} to {}", messageAsString, - netconfOperationExecution.operationWithHighestPriority); - - final LinkedList chain = new LinkedList<>(); - chain.push(netconfOperationExecution); - - for (Iterator 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); + private Document executeOperationWithHighestPriority(final Document message, + final NetconfOperationExecution netconfOperationExecution) + throws NetconfDocumentedException { + if (LOG.isDebugEnabled()) { + LOG.debug("Forwarding netconf message {} to {}", XmlUtil.toString(message), netconfOperationExecution.netconfOperation); } - return chain.getFirst().execute(message, this); + + return netconfOperationExecution.execute(message); } private NetconfOperationExecution getNetconfOperationWithHighestPriority( - Document message, NetconfSession session) { + final Document message, final NetconfServerSession session) throws NetconfDocumentedException { - TreeMap> sortedPriority = getSortedNetconfOperationsWithCanHandle( + NavigableMap 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.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> getSortedNetconfOperationsWithCanHandle( - Document message, NetconfSession session) { - TreeMap> sortedPriority = Maps.newTreeMap(); + private TreeMap getSortedNetconfOperationsWithCanHandle(final Document message, + final NetconfServerSession session) throws NetconfDocumentedException { + TreeMap 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 netconfOperations = sortedPriority.get(handlingPriority); - netconfOperations = checkIfNoOperationsOnPriority(sortedPriority, handlingPriority, netconfOperations); - netconfOperations.add(netconfOperation); + 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); + sortedPriority.put(handlingPriority, netconfOperation); } } return sortedPriority; } - private Set checkIfNoOperationsOnPriority( - TreeMap> sortedPriority, HandlingPriority handlingPriority, - Set 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 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); + } + }; - 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> 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 NetconfDocumentedException { + return netconfOperation.handle(message, subsequentExecution); + } + + public static NetconfOperationExecution createExecutionChain( + final NavigableMap 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); } } @@ -259,7 +218,4 @@ public class NetconfOperationRouterImpl implements NetconfOperationRouter { return "NetconfOperationRouterImpl{" + "netconfOperationServiceSnapshot=" + netconfOperationServiceSnapshot + '}'; } - - - }