X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fnetconf%2Fnetconf-impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fnetconf%2Fimpl%2Fosgi%2FNetconfOperationRouterImpl.java;fp=opendaylight%2Fnetconf%2Fnetconf-impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fnetconf%2Fimpl%2Fosgi%2FNetconfOperationRouterImpl.java;h=0000000000000000000000000000000000000000;hb=4c0c091813aea131d32dc70c5121a450eb9b7291;hp=f36cf29afce118db242bad541fee400ee8b3fb81;hpb=a63cd90c78847178c8ab9a970b2189bdab572a80;p=netconf.git diff --git a/opendaylight/netconf/netconf-impl/src/main/java/org/opendaylight/netconf/impl/osgi/NetconfOperationRouterImpl.java b/opendaylight/netconf/netconf-impl/src/main/java/org/opendaylight/netconf/impl/osgi/NetconfOperationRouterImpl.java deleted file mode 100644 index f36cf29afc..0000000000 --- a/opendaylight/netconf/netconf-impl/src/main/java/org/opendaylight/netconf/impl/osgi/NetconfOperationRouterImpl.java +++ /dev/null @@ -1,207 +0,0 @@ -/* - * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v1.0 which accompanies this distribution, - * and is available at http://www.eclipse.org/legal/epl-v10.html - */ -package org.opendaylight.netconf.impl.osgi; - -import com.google.common.base.Preconditions; -import com.google.common.collect.ImmutableSet; -import com.google.common.collect.Maps; -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.netconf.mapping.api.HandlingPriority; -import org.opendaylight.netconf.mapping.api.NetconfOperation; -import org.opendaylight.netconf.mapping.api.NetconfOperationChainedExecution; -import org.opendaylight.netconf.mapping.api.NetconfOperationService; -import org.opendaylight.netconf.mapping.api.SessionAwareNetconfOperation; -import org.opendaylight.netconf.api.monitoring.NetconfMonitoringService; -import org.opendaylight.netconf.impl.NetconfServerSession; -import org.opendaylight.netconf.impl.mapping.operations.DefaultCloseSession; -import org.opendaylight.netconf.impl.mapping.operations.DefaultNetconfOperation; -import org.opendaylight.netconf.impl.mapping.operations.DefaultStartExi; -import org.opendaylight.netconf.impl.mapping.operations.DefaultStopExi; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -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; - - public NetconfOperationRouterImpl(final NetconfOperationService netconfOperationServiceSnapshot, - final NetconfMonitoringService netconfMonitoringService, final String sessionId) { - this.netconfOperationServiceSnapshot = Preconditions.checkNotNull(netconfOperationServiceSnapshot); - - final Set ops = new HashSet<>(); - ops.add(new DefaultCloseSession(sessionId, this)); - ops.add(new DefaultStartExi(sessionId)); - ops.add(new DefaultStopExi(sessionId)); - - ops.addAll(netconfOperationServiceSnapshot.getNetconfOperations()); - - allNetconfOperations = ImmutableSet.copyOf(ops); - } - - @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; - } - - 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); - } - - try { - return executeOperationWithHighestPriority(message, netconfOperationExecution); - } catch (RuntimeException e) { - throw handleUnexpectedEx("Unexpected exception during netconf operation execution", e); - } - } - - @Override - public void close() throws Exception { - netconfOperationServiceSnapshot.close(); - } - - 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 DocumentedException { - if (LOG.isDebugEnabled()) { - LOG.debug("Forwarding netconf message {} to {}", XmlUtil.toString(message), netconfOperationExecution.netconfOperation); - } - - return netconfOperationExecution.execute(message); - } - - private NetconfOperationExecution getNetconfOperationWithHighestPriority( - final Document message, final NetconfServerSession session) throws DocumentedException { - - NavigableMap sortedByPriority = getSortedNetconfOperationsWithCanHandle( - message, session); - - if (sortedByPriority.isEmpty()) { - throw new IllegalArgumentException(String.format("No %s available to handle message %s", - NetconfOperation.class.getName(), XmlUtil.toString(message))); - } - - return NetconfOperationExecution.createExecutionChain(sortedByPriority, sortedByPriority.lastKey()); - } - - private TreeMap getSortedNetconfOperationsWithCanHandle(final Document message, - final NetconfServerSession session) throws DocumentedException { - TreeMap 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)) { - - 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; - } - - public static final NetconfOperationChainedExecution EXECUTION_TERMINATION_POINT = new NetconfOperationChainedExecution() { - @Override - public boolean isExecutionTermination() { - return true; - } - - @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 static class NetconfOperationExecution implements NetconfOperationChainedExecution { - private final NetconfOperation netconfOperation; - private final NetconfOperationChainedExecution subsequentExecution; - - private NetconfOperationExecution(final NetconfOperation netconfOperation, final NetconfOperationChainedExecution subsequentExecution) { - this.netconfOperation = netconfOperation; - this.subsequentExecution = subsequentExecution; - } - - @Override - public boolean isExecutionTermination() { - return false; - } - - @Override - public Document execute(final Document message) throws DocumentedException { - 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); - } - } - - @Override - public String toString() { - return "NetconfOperationRouterImpl{" + "netconfOperationServiceSnapshot=" + netconfOperationServiceSnapshot - + '}'; - } -}