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.ErrorType;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import org.w3c.dom.Document;
39 public class NetconfOperationRouterImpl implements NetconfOperationRouter {
41 private static final Logger LOG = LoggerFactory.getLogger(NetconfOperationRouterImpl.class);
42 private final NetconfOperationService netconfOperationServiceSnapshot;
43 private final Collection<NetconfOperation> allNetconfOperations;
45 public NetconfOperationRouterImpl(final NetconfOperationService netconfOperationServiceSnapshot,
46 final NetconfMonitoringService netconfMonitoringService, final String sessionId) {
47 this.netconfOperationServiceSnapshot = requireNonNull(netconfOperationServiceSnapshot);
49 final Set<NetconfOperation> ops = new HashSet<>();
50 ops.add(new DefaultCloseSession(sessionId, this));
51 ops.add(new DefaultStartExi(sessionId));
52 ops.add(new DefaultStopExi(sessionId));
54 ops.addAll(netconfOperationServiceSnapshot.getNetconfOperations());
56 allNetconfOperations = ImmutableSet.copyOf(ops);
59 @SuppressWarnings("checkstyle:IllegalCatch")
61 public Document onNetconfMessage(final Document message, final NetconfServerSession session) throws
63 requireNonNull(allNetconfOperations, "Operation router was not initialized properly");
65 final NetconfOperationExecution netconfOperationExecution;
67 netconfOperationExecution = getNetconfOperationWithHighestPriority(message, session);
68 } catch (IllegalArgumentException | IllegalStateException e) {
69 final String messageAsString = XmlUtil.toString(message);
70 LOG.warn("Unable to handle rpc {} on session {}", messageAsString, session, e);
72 final DocumentedException.ErrorTag tag;
73 if (e instanceof IllegalArgumentException) {
74 tag = DocumentedException.ErrorTag.OPERATION_NOT_SUPPORTED;
76 tag = DocumentedException.ErrorTag.OPERATION_FAILED;
79 throw new DocumentedException(
80 String.format("Unable to handle rpc %s on session %s", messageAsString, session),
81 e, ErrorType.APPLICATION, tag, ErrorSeverity.ERROR, Map.of(tag.toString(), e.getMessage()));
82 } catch (final RuntimeException e) {
83 throw handleUnexpectedEx("sort", e);
87 return executeOperationWithHighestPriority(message, netconfOperationExecution);
88 } catch (final RuntimeException e) {
89 throw handleUnexpectedEx("execution", e);
95 netconfOperationServiceSnapshot.close();
98 private static DocumentedException handleUnexpectedEx(final String op, final Exception exception) {
99 LOG.error("Unexpected exception during netconf operation {}", op, exception);
100 return new DocumentedException("Unexpected error",
101 ErrorType.APPLICATION, DocumentedException.ErrorTag.OPERATION_FAILED, ErrorSeverity.ERROR,
102 Map.of(ErrorSeverity.ERROR.toString(), exception.toString()));
105 private static Document executeOperationWithHighestPriority(final Document message,
106 final NetconfOperationExecution netconfOperationExecution) throws DocumentedException {
107 if (LOG.isDebugEnabled()) {
108 LOG.debug("Forwarding netconf message {} to {}", XmlUtil.toString(message), netconfOperationExecution
112 return netconfOperationExecution.execute(message);
115 private NetconfOperationExecution getNetconfOperationWithHighestPriority(
116 final Document message, final NetconfServerSession session) throws DocumentedException {
118 final NavigableMap<HandlingPriority, NetconfOperation> sortedByPriority =
119 getSortedNetconfOperationsWithCanHandle(
122 if (sortedByPriority.isEmpty()) {
123 throw new IllegalArgumentException(String.format("No %s available to handle message %s",
124 NetconfOperation.class.getName(), XmlUtil.toString(message)));
127 return NetconfOperationExecution.createExecutionChain(sortedByPriority, sortedByPriority.lastKey());
130 private TreeMap<HandlingPriority, NetconfOperation> getSortedNetconfOperationsWithCanHandle(
131 final Document message, final NetconfServerSession session) throws DocumentedException {
132 final TreeMap<HandlingPriority, NetconfOperation> sortedPriority = new TreeMap<>();
134 for (final NetconfOperation netconfOperation : allNetconfOperations) {
135 final HandlingPriority handlingPriority = netconfOperation.canHandle(message);
136 if (netconfOperation instanceof DefaultNetconfOperation) {
137 ((DefaultNetconfOperation) netconfOperation).setNetconfSession(session);
139 if (netconfOperation instanceof SessionAwareNetconfOperation) {
140 ((SessionAwareNetconfOperation) netconfOperation).setSession(session);
142 if (!handlingPriority.equals(HandlingPriority.CANNOT_HANDLE)) {
144 checkState(!sortedPriority.containsKey(handlingPriority),
145 "Multiple %s available to handle message %s with priority %s, %s and %s",
146 NetconfOperation.class.getName(), message, handlingPriority, netconfOperation, sortedPriority
147 .get(handlingPriority));
148 sortedPriority.put(handlingPriority, netconfOperation);
151 return sortedPriority;
154 private static final class NetconfOperationExecution implements NetconfOperationChainedExecution {
155 private final NetconfOperation netconfOperation;
156 private final NetconfOperationChainedExecution subsequentExecution;
158 private NetconfOperationExecution(final NetconfOperation netconfOperation,
159 final NetconfOperationChainedExecution subsequentExecution) {
160 this.netconfOperation = netconfOperation;
161 this.subsequentExecution = subsequentExecution;
165 public boolean isExecutionTermination() {
170 public Document execute(final Document message) throws DocumentedException {
171 return netconfOperation.handle(message, subsequentExecution);
174 public static NetconfOperationExecution createExecutionChain(
175 final NavigableMap<HandlingPriority, NetconfOperation> sortedByPriority,
176 final HandlingPriority handlingPriority) {
177 final NetconfOperation netconfOperation = sortedByPriority.get(handlingPriority);
178 final HandlingPriority subsequentHandlingPriority = sortedByPriority.lowerKey(handlingPriority);
180 NetconfOperationChainedExecution subsequentExecution = null;
182 if (subsequentHandlingPriority != null) {
183 subsequentExecution = createExecutionChain(sortedByPriority, subsequentHandlingPriority);
185 subsequentExecution = EXECUTION_TERMINATION_POINT;
188 return new NetconfOperationExecution(netconfOperation, subsequentExecution);
193 public String toString() {
194 return "NetconfOperationRouterImpl{" + "netconfOperationServiceSnapshot=" + netconfOperationServiceSnapshot