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 static com.google.common.base.Preconditions.checkState;
11 import static java.util.Objects.requireNonNull;
13 import com.google.common.collect.ImmutableSet;
14 import java.util.Collection;
15 import java.util.HashSet;
17 import java.util.NavigableMap;
19 import java.util.TreeMap;
20 import org.opendaylight.netconf.api.DocumentedException;
21 import org.opendaylight.netconf.api.monitoring.NetconfMonitoringService;
22 import org.opendaylight.netconf.api.xml.XmlUtil;
23 import org.opendaylight.netconf.impl.NetconfServerSession;
24 import org.opendaylight.netconf.impl.mapping.operations.DefaultCloseSession;
25 import org.opendaylight.netconf.impl.mapping.operations.DefaultNetconfOperation;
26 import org.opendaylight.netconf.impl.mapping.operations.DefaultStartExi;
27 import org.opendaylight.netconf.impl.mapping.operations.DefaultStopExi;
28 import org.opendaylight.netconf.mapping.api.HandlingPriority;
29 import org.opendaylight.netconf.mapping.api.NetconfOperation;
30 import org.opendaylight.netconf.mapping.api.NetconfOperationChainedExecution;
31 import org.opendaylight.netconf.mapping.api.NetconfOperationService;
32 import org.opendaylight.netconf.mapping.api.SessionAwareNetconfOperation;
33 import org.opendaylight.yangtools.yang.common.ErrorSeverity;
34 import org.opendaylight.yangtools.yang.common.ErrorTag;
35 import org.opendaylight.yangtools.yang.common.ErrorType;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.w3c.dom.Document;
40 public class NetconfOperationRouterImpl implements NetconfOperationRouter {
42 private static final Logger LOG = LoggerFactory.getLogger(NetconfOperationRouterImpl.class);
43 private final NetconfOperationService netconfOperationServiceSnapshot;
44 private final Collection<NetconfOperation> allNetconfOperations;
46 public NetconfOperationRouterImpl(final NetconfOperationService netconfOperationServiceSnapshot,
47 final NetconfMonitoringService netconfMonitoringService, final String sessionId) {
48 this.netconfOperationServiceSnapshot = requireNonNull(netconfOperationServiceSnapshot);
50 final Set<NetconfOperation> ops = new HashSet<>();
51 ops.add(new DefaultCloseSession(sessionId, this));
52 ops.add(new DefaultStartExi(sessionId));
53 ops.add(new DefaultStopExi(sessionId));
55 ops.addAll(netconfOperationServiceSnapshot.getNetconfOperations());
57 allNetconfOperations = ImmutableSet.copyOf(ops);
60 @SuppressWarnings("checkstyle:IllegalCatch")
62 public Document onNetconfMessage(final Document message, final NetconfServerSession session) throws
64 requireNonNull(allNetconfOperations, "Operation router was not initialized properly");
66 final NetconfOperationExecution netconfOperationExecution;
68 netconfOperationExecution = getNetconfOperationWithHighestPriority(message, session);
69 } catch (IllegalArgumentException | IllegalStateException e) {
70 final String messageAsString = XmlUtil.toString(message);
71 LOG.warn("Unable to handle rpc {} on session {}", messageAsString, session, e);
73 final ErrorTag tag = e instanceof IllegalArgumentException ? ErrorTag.OPERATION_NOT_SUPPORTED
74 : ErrorTag.OPERATION_FAILED;
76 throw new DocumentedException(
77 String.format("Unable to handle rpc %s on session %s", messageAsString, session), e,
78 ErrorType.APPLICATION, tag, ErrorSeverity.ERROR,
79 // FIXME: i.e. in what namespace are we providing these tags? why is this not just:
81 // <java-throwable xmlns="org.opendaylight.something">
82 // <message>e.getMessage()</message>
85 // for each place where we are mapping Exception.getMessage() ? We probably do not want to propagate
86 // stack traces out, but suppressed exceptions and causal list might be interesting:
88 // <java-throwable xmlns="org.opendaylight.something">
89 // <message>reported exception</message>
91 // <java-throwable xmlns="org.opendaylight.something">
92 // <message>cause of reported exception</message>
94 // <java-throwable xmlns="org.opendaylight.something">
95 // <message>cause of cause of reported exception</message>
97 Map.of(tag.elementBody(), e.getMessage()));
98 } catch (final RuntimeException e) {
99 throw handleUnexpectedEx("sort", e);
103 return executeOperationWithHighestPriority(message, netconfOperationExecution);
104 } catch (final RuntimeException e) {
105 throw handleUnexpectedEx("execution", e);
110 public void close() {
111 netconfOperationServiceSnapshot.close();
114 private static DocumentedException handleUnexpectedEx(final String op, final Exception exception) {
115 LOG.error("Unexpected exception during netconf operation {}", op, exception);
116 return new DocumentedException("Unexpected error",
117 ErrorType.APPLICATION, ErrorTag.OPERATION_FAILED, ErrorSeverity.ERROR,
118 // FIXME: i.e. <error>exception.toString()</error>? That looks wrong on a few levels.
119 Map.of(ErrorSeverity.ERROR.elementBody(), exception.toString()));
122 private static Document executeOperationWithHighestPriority(final Document message,
123 final NetconfOperationExecution netconfOperationExecution) throws DocumentedException {
124 if (LOG.isDebugEnabled()) {
125 LOG.debug("Forwarding netconf message {} to {}", XmlUtil.toString(message), netconfOperationExecution
129 return netconfOperationExecution.execute(message);
132 private NetconfOperationExecution getNetconfOperationWithHighestPriority(
133 final Document message, final NetconfServerSession session) throws DocumentedException {
135 final NavigableMap<HandlingPriority, NetconfOperation> sortedByPriority =
136 getSortedNetconfOperationsWithCanHandle(
139 if (sortedByPriority.isEmpty()) {
140 throw new IllegalArgumentException(String.format("No %s available to handle message %s",
141 NetconfOperation.class.getName(), XmlUtil.toString(message)));
144 return NetconfOperationExecution.createExecutionChain(sortedByPriority, sortedByPriority.lastKey());
147 private TreeMap<HandlingPriority, NetconfOperation> getSortedNetconfOperationsWithCanHandle(
148 final Document message, final NetconfServerSession session) throws DocumentedException {
149 final TreeMap<HandlingPriority, NetconfOperation> sortedPriority = new TreeMap<>();
151 for (final NetconfOperation netconfOperation : allNetconfOperations) {
152 final HandlingPriority handlingPriority = netconfOperation.canHandle(message);
153 if (netconfOperation instanceof DefaultNetconfOperation defaultOperation) {
154 defaultOperation.setNetconfSession(session);
156 if (netconfOperation instanceof SessionAwareNetconfOperation sessionAwareOperation) {
157 sessionAwareOperation.setSession(session);
159 if (!handlingPriority.equals(HandlingPriority.CANNOT_HANDLE)) {
161 checkState(!sortedPriority.containsKey(handlingPriority),
162 "Multiple %s available to handle message %s with priority %s, %s and %s",
163 NetconfOperation.class.getName(), message, handlingPriority, netconfOperation, sortedPriority
164 .get(handlingPriority));
165 sortedPriority.put(handlingPriority, netconfOperation);
168 return sortedPriority;
171 private static final class NetconfOperationExecution implements NetconfOperationChainedExecution {
172 private final NetconfOperation netconfOperation;
173 private final NetconfOperationChainedExecution subsequentExecution;
175 private NetconfOperationExecution(final NetconfOperation netconfOperation,
176 final NetconfOperationChainedExecution subsequentExecution) {
177 this.netconfOperation = netconfOperation;
178 this.subsequentExecution = subsequentExecution;
182 public boolean isExecutionTermination() {
187 public Document execute(final Document message) throws DocumentedException {
188 return netconfOperation.handle(message, subsequentExecution);
191 public static NetconfOperationExecution createExecutionChain(
192 final NavigableMap<HandlingPriority, NetconfOperation> sortedByPriority,
193 final HandlingPriority handlingPriority) {
194 final NetconfOperation netconfOperation = sortedByPriority.get(handlingPriority);
195 final HandlingPriority subsequentHandlingPriority = sortedByPriority.lowerKey(handlingPriority);
197 NetconfOperationChainedExecution subsequentExecution = null;
199 if (subsequentHandlingPriority != null) {
200 subsequentExecution = createExecutionChain(sortedByPriority, subsequentHandlingPriority);
202 subsequentExecution = EXECUTION_TERMINATION_POINT;
205 return new NetconfOperationExecution(netconfOperation, subsequentExecution);
210 public String toString() {
211 return "NetconfOperationRouterImpl{" + "netconfOperationServiceSnapshot=" + netconfOperationServiceSnapshot