2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.netconf.impl.osgi;
10 import com.google.common.base.Preconditions;
11 import com.google.common.collect.ImmutableSet;
12 import com.google.common.collect.Maps;
13 import java.util.Collection;
14 import java.util.Collections;
15 import java.util.HashSet;
16 import java.util.NavigableMap;
18 import java.util.TreeMap;
19 import org.opendaylight.netconf.api.DocumentedException;
20 import org.opendaylight.netconf.api.monitoring.NetconfMonitoringService;
21 import org.opendaylight.netconf.api.xml.XmlUtil;
22 import org.opendaylight.netconf.impl.NetconfServerSession;
23 import org.opendaylight.netconf.impl.mapping.operations.DefaultCloseSession;
24 import org.opendaylight.netconf.impl.mapping.operations.DefaultNetconfOperation;
25 import org.opendaylight.netconf.impl.mapping.operations.DefaultStartExi;
26 import org.opendaylight.netconf.impl.mapping.operations.DefaultStopExi;
27 import org.opendaylight.netconf.mapping.api.HandlingPriority;
28 import org.opendaylight.netconf.mapping.api.NetconfOperation;
29 import org.opendaylight.netconf.mapping.api.NetconfOperationChainedExecution;
30 import org.opendaylight.netconf.mapping.api.NetconfOperationService;
31 import org.opendaylight.netconf.mapping.api.SessionAwareNetconfOperation;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.w3c.dom.Document;
36 public class NetconfOperationRouterImpl implements NetconfOperationRouter {
38 private static final Logger LOG = LoggerFactory.getLogger(NetconfOperationRouterImpl.class);
39 private final NetconfOperationService netconfOperationServiceSnapshot;
40 private final Collection<NetconfOperation> allNetconfOperations;
42 public NetconfOperationRouterImpl(final NetconfOperationService netconfOperationServiceSnapshot,
43 final NetconfMonitoringService netconfMonitoringService, final String sessionId) {
44 this.netconfOperationServiceSnapshot = Preconditions.checkNotNull(netconfOperationServiceSnapshot);
46 final Set<NetconfOperation> ops = new HashSet<>();
47 ops.add(new DefaultCloseSession(sessionId, this));
48 ops.add(new DefaultStartExi(sessionId));
49 ops.add(new DefaultStopExi(sessionId));
51 ops.addAll(netconfOperationServiceSnapshot.getNetconfOperations());
53 allNetconfOperations = ImmutableSet.copyOf(ops);
56 @SuppressWarnings("checkstyle:IllegalCatch")
58 public Document onNetconfMessage(final Document message, final NetconfServerSession session) throws
60 Preconditions.checkNotNull(allNetconfOperations, "Operation router was not initialized properly");
62 final NetconfOperationExecution netconfOperationExecution;
64 netconfOperationExecution = getNetconfOperationWithHighestPriority(message, session);
65 } catch (IllegalArgumentException | IllegalStateException e) {
66 final String messageAsString = XmlUtil.toString(message);
67 LOG.warn("Unable to handle rpc {} on session {}", messageAsString, session, e);
69 final DocumentedException.ErrorTag tag;
70 if (e instanceof IllegalArgumentException) {
71 tag = DocumentedException.ErrorTag.OPERATION_NOT_SUPPORTED;
73 tag = DocumentedException.ErrorTag.OPERATION_FAILED;
76 throw new DocumentedException(
77 String.format("Unable to handle rpc %s on session %s", messageAsString, session),
78 e, DocumentedException.ErrorType.APPLICATION,
79 tag, DocumentedException.ErrorSeverity.ERROR,
80 Collections.singletonMap(tag.toString(), e.getMessage()));
81 } catch (final RuntimeException e) {
82 throw handleUnexpectedEx("Unexpected exception during netconf operation sort", e);
86 return executeOperationWithHighestPriority(message, netconfOperationExecution);
87 } catch (final RuntimeException e) {
88 throw handleUnexpectedEx("Unexpected exception during netconf operation execution", e);
93 public void close() throws Exception {
94 netconfOperationServiceSnapshot.close();
97 private static DocumentedException handleUnexpectedEx(final String message, final Exception exception) throws
99 LOG.error("{}", message, exception);
100 return new DocumentedException("Unexpected error",
101 DocumentedException.ErrorType.APPLICATION,
102 DocumentedException.ErrorTag.OPERATION_FAILED,
103 DocumentedException.ErrorSeverity.ERROR,
104 Collections.singletonMap(DocumentedException.ErrorSeverity.ERROR.toString(), exception.toString()));
107 private static Document executeOperationWithHighestPriority(final Document message,
108 final NetconfOperationExecution netconfOperationExecution) throws DocumentedException {
109 if (LOG.isDebugEnabled()) {
110 LOG.debug("Forwarding netconf message {} to {}", XmlUtil.toString(message), netconfOperationExecution
114 return netconfOperationExecution.execute(message);
117 private NetconfOperationExecution getNetconfOperationWithHighestPriority(
118 final Document message, final NetconfServerSession session) throws DocumentedException {
120 final NavigableMap<HandlingPriority, NetconfOperation> sortedByPriority =
121 getSortedNetconfOperationsWithCanHandle(
124 if (sortedByPriority.isEmpty()) {
125 throw new IllegalArgumentException(String.format("No %s available to handle message %s",
126 NetconfOperation.class.getName(), XmlUtil.toString(message)));
129 return NetconfOperationExecution.createExecutionChain(sortedByPriority, sortedByPriority.lastKey());
132 private TreeMap<HandlingPriority, NetconfOperation> getSortedNetconfOperationsWithCanHandle(
133 final Document message, final NetconfServerSession session) throws DocumentedException {
134 final TreeMap<HandlingPriority, NetconfOperation> sortedPriority = Maps.newTreeMap();
136 for (final NetconfOperation netconfOperation : allNetconfOperations) {
137 final HandlingPriority handlingPriority = netconfOperation.canHandle(message);
138 if (netconfOperation instanceof DefaultNetconfOperation) {
139 ((DefaultNetconfOperation) netconfOperation).setNetconfSession(session);
141 if (netconfOperation instanceof SessionAwareNetconfOperation) {
142 ((SessionAwareNetconfOperation) netconfOperation).setSession(session);
144 if (!handlingPriority.equals(HandlingPriority.CANNOT_HANDLE)) {
146 Preconditions.checkState(!sortedPriority.containsKey(handlingPriority),
147 "Multiple %s available to handle message %s with priority %s, %s and %s",
148 NetconfOperation.class.getName(), message, handlingPriority, netconfOperation, sortedPriority
149 .get(handlingPriority));
150 sortedPriority.put(handlingPriority, netconfOperation);
153 return sortedPriority;
156 private static final class NetconfOperationExecution implements NetconfOperationChainedExecution {
157 private final NetconfOperation netconfOperation;
158 private final NetconfOperationChainedExecution subsequentExecution;
160 private NetconfOperationExecution(final NetconfOperation netconfOperation,
161 final NetconfOperationChainedExecution subsequentExecution) {
162 this.netconfOperation = netconfOperation;
163 this.subsequentExecution = subsequentExecution;
167 public boolean isExecutionTermination() {
172 public Document execute(final Document message) throws DocumentedException {
173 return netconfOperation.handle(message, subsequentExecution);
176 public static NetconfOperationExecution createExecutionChain(
177 final NavigableMap<HandlingPriority, NetconfOperation> sortedByPriority,
178 final HandlingPriority handlingPriority) {
179 final NetconfOperation netconfOperation = sortedByPriority.get(handlingPriority);
180 final HandlingPriority subsequentHandlingPriority = sortedByPriority.lowerKey(handlingPriority);
182 NetconfOperationChainedExecution subsequentExecution = null;
184 if (subsequentHandlingPriority != null) {
185 subsequentExecution = createExecutionChain(sortedByPriority, subsequentHandlingPriority);
187 subsequentExecution = EXECUTION_TERMINATION_POINT;
190 return new NetconfOperationExecution(netconfOperation, subsequentExecution);
195 public String toString() {
196 return "NetconfOperationRouterImpl{" + "netconfOperationServiceSnapshot=" + netconfOperationServiceSnapshot