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.controller.config.util.xml.DocumentedException;
20 import org.opendaylight.controller.config.util.xml.XmlUtil;
21 import org.opendaylight.netconf.mapping.api.HandlingPriority;
22 import org.opendaylight.netconf.mapping.api.NetconfOperation;
23 import org.opendaylight.netconf.mapping.api.NetconfOperationChainedExecution;
24 import org.opendaylight.netconf.mapping.api.NetconfOperationService;
25 import org.opendaylight.netconf.mapping.api.SessionAwareNetconfOperation;
26 import org.opendaylight.netconf.api.monitoring.NetconfMonitoringService;
27 import org.opendaylight.netconf.impl.NetconfServerSession;
28 import org.opendaylight.netconf.impl.mapping.operations.DefaultCloseSession;
29 import org.opendaylight.netconf.impl.mapping.operations.DefaultNetconfOperation;
30 import org.opendaylight.netconf.impl.mapping.operations.DefaultStartExi;
31 import org.opendaylight.netconf.impl.mapping.operations.DefaultStopExi;
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);
57 public Document onNetconfMessage(final Document message, final NetconfServerSession session) throws DocumentedException {
58 Preconditions.checkNotNull(allNetconfOperations, "Operation router was not initialized properly");
60 final NetconfOperationExecution netconfOperationExecution;
62 netconfOperationExecution = getNetconfOperationWithHighestPriority(message, session);
63 } catch (IllegalArgumentException | IllegalStateException e) {
64 final String messageAsString = XmlUtil.toString(message);
65 LOG.warn("Unable to handle rpc {} on session {}", messageAsString, session, e);
67 final DocumentedException.ErrorTag tag;
68 if (e instanceof IllegalArgumentException) {
69 tag = DocumentedException.ErrorTag.operation_not_supported;
71 tag = DocumentedException.ErrorTag.operation_failed;
74 throw new DocumentedException(
75 String.format("Unable to handle rpc %s on session %s", messageAsString, session),
76 e, DocumentedException.ErrorType.application,
77 tag, DocumentedException.ErrorSeverity.error,
78 Collections.singletonMap(tag.toString(), e.getMessage()));
79 } catch (RuntimeException e) {
80 throw handleUnexpectedEx("Unexpected exception during netconf operation sort", e);
84 return executeOperationWithHighestPriority(message, netconfOperationExecution);
85 } catch (RuntimeException e) {
86 throw handleUnexpectedEx("Unexpected exception during netconf operation execution", e);
91 public void close() throws Exception {
92 netconfOperationServiceSnapshot.close();
95 private static DocumentedException handleUnexpectedEx(final String s, final Exception e) throws DocumentedException {
96 LOG.error("{}", s, e);
97 return new DocumentedException("Unexpected error",
98 DocumentedException.ErrorType.application,
99 DocumentedException.ErrorTag.operation_failed,
100 DocumentedException.ErrorSeverity.error,
101 Collections.singletonMap(DocumentedException.ErrorSeverity.error.toString(), e.toString()));
104 private Document executeOperationWithHighestPriority(final Document message,
105 final NetconfOperationExecution netconfOperationExecution)
106 throws DocumentedException {
107 if (LOG.isDebugEnabled()) {
108 LOG.debug("Forwarding netconf message {} to {}", XmlUtil.toString(message), netconfOperationExecution.netconfOperation);
111 return netconfOperationExecution.execute(message);
114 private NetconfOperationExecution getNetconfOperationWithHighestPriority(
115 final Document message, final NetconfServerSession session) throws DocumentedException {
117 NavigableMap<HandlingPriority, NetconfOperation> sortedByPriority = getSortedNetconfOperationsWithCanHandle(
120 if (sortedByPriority.isEmpty()) {
121 throw new IllegalArgumentException(String.format("No %s available to handle message %s",
122 NetconfOperation.class.getName(), XmlUtil.toString(message)));
125 return NetconfOperationExecution.createExecutionChain(sortedByPriority, sortedByPriority.lastKey());
128 private TreeMap<HandlingPriority, NetconfOperation> getSortedNetconfOperationsWithCanHandle(final Document message,
129 final NetconfServerSession session) throws DocumentedException {
130 TreeMap<HandlingPriority, NetconfOperation> sortedPriority = Maps.newTreeMap();
132 for (NetconfOperation netconfOperation : allNetconfOperations) {
133 final HandlingPriority handlingPriority = netconfOperation.canHandle(message);
134 if (netconfOperation instanceof DefaultNetconfOperation) {
135 ((DefaultNetconfOperation) netconfOperation).setNetconfSession(session);
137 if(netconfOperation instanceof SessionAwareNetconfOperation) {
138 ((SessionAwareNetconfOperation) netconfOperation).setSession(session);
140 if (!handlingPriority.equals(HandlingPriority.CANNOT_HANDLE)) {
142 Preconditions.checkState(!sortedPriority.containsKey(handlingPriority),
143 "Multiple %s available to handle message %s with priority %s, %s and %s",
144 NetconfOperation.class.getName(), message, handlingPriority, netconfOperation, sortedPriority.get(handlingPriority));
145 sortedPriority.put(handlingPriority, netconfOperation);
148 return sortedPriority;
151 public static final NetconfOperationChainedExecution EXECUTION_TERMINATION_POINT = new NetconfOperationChainedExecution() {
153 public boolean isExecutionTermination() {
158 public Document execute(final Document requestMessage) throws DocumentedException {
159 throw new DocumentedException("This execution represents the termination point in operation execution and cannot be executed itself",
160 DocumentedException.ErrorType.application,
161 DocumentedException.ErrorTag.operation_failed,
162 DocumentedException.ErrorSeverity.error);
166 private static class NetconfOperationExecution implements NetconfOperationChainedExecution {
167 private final NetconfOperation netconfOperation;
168 private final NetconfOperationChainedExecution subsequentExecution;
170 private NetconfOperationExecution(final NetconfOperation netconfOperation, final NetconfOperationChainedExecution subsequentExecution) {
171 this.netconfOperation = netconfOperation;
172 this.subsequentExecution = subsequentExecution;
176 public boolean isExecutionTermination() {
181 public Document execute(final Document message) throws DocumentedException {
182 return netconfOperation.handle(message, subsequentExecution);
185 public static NetconfOperationExecution createExecutionChain(
186 final NavigableMap<HandlingPriority, NetconfOperation> sortedByPriority, final HandlingPriority handlingPriority) {
187 NetconfOperation netconfOperation = sortedByPriority.get(handlingPriority);
188 HandlingPriority subsequentHandlingPriority = sortedByPriority.lowerKey(handlingPriority);
190 NetconfOperationChainedExecution subsequentExecution = null;
192 if (subsequentHandlingPriority != null) {
193 subsequentExecution = createExecutionChain(sortedByPriority, subsequentHandlingPriority);
195 subsequentExecution = EXECUTION_TERMINATION_POINT;
198 return new NetconfOperationExecution(netconfOperation, subsequentExecution);
203 public String toString() {
204 return "NetconfOperationRouterImpl{" + "netconfOperationServiceSnapshot=" + netconfOperationServiceSnapshot