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.controller.netconf.impl.osgi;
10 import com.google.common.base.Preconditions;
11 import com.google.common.collect.Maps;
12 import com.google.common.collect.Sets;
13 import java.util.Collections;
14 import java.util.HashSet;
16 import java.util.NavigableMap;
18 import java.util.TreeMap;
19 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
20 import org.opendaylight.controller.netconf.impl.DefaultCommitNotificationProducer;
21 import org.opendaylight.controller.netconf.impl.NetconfServerSession;
22 import org.opendaylight.controller.netconf.impl.mapping.CapabilityProvider;
23 import org.opendaylight.controller.netconf.impl.mapping.operations.DefaultCloseSession;
24 import org.opendaylight.controller.netconf.impl.mapping.operations.DefaultCommit;
25 import org.opendaylight.controller.netconf.impl.mapping.operations.DefaultGetSchema;
26 import org.opendaylight.controller.netconf.impl.mapping.operations.DefaultNetconfOperation;
27 import org.opendaylight.controller.netconf.impl.mapping.operations.DefaultStartExi;
28 import org.opendaylight.controller.netconf.impl.mapping.operations.DefaultStopExi;
29 import org.opendaylight.controller.netconf.mapping.api.HandlingPriority;
30 import org.opendaylight.controller.netconf.mapping.api.NetconfOperation;
31 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationChainedExecution;
32 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationService;
33 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationServiceSnapshot;
34 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
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);
43 private final NetconfOperationServiceSnapshot netconfOperationServiceSnapshot;
44 private Set<NetconfOperation> allNetconfOperations;
46 private NetconfOperationRouterImpl(final NetconfOperationServiceSnapshot netconfOperationServiceSnapshot) {
47 this.netconfOperationServiceSnapshot = netconfOperationServiceSnapshot;
50 private synchronized void initNetconfOperations(final Set<NetconfOperation> allOperations) {
51 allNetconfOperations = allOperations;
55 * Factory method to produce instance of NetconfOperationRouter
57 public static NetconfOperationRouter createOperationRouter(final NetconfOperationServiceSnapshot netconfOperationServiceSnapshot,
58 final CapabilityProvider capabilityProvider, final DefaultCommitNotificationProducer commitNotifier) {
59 NetconfOperationRouterImpl router = new NetconfOperationRouterImpl(netconfOperationServiceSnapshot);
61 Preconditions.checkNotNull(netconfOperationServiceSnapshot);
62 Preconditions.checkNotNull(capabilityProvider);
64 final String sessionId = netconfOperationServiceSnapshot.getNetconfSessionIdForReporting();
66 final Set<NetconfOperation> defaultNetconfOperations = Sets.newHashSet();
67 defaultNetconfOperations.add(new DefaultGetSchema(capabilityProvider, sessionId));
68 defaultNetconfOperations.add(new DefaultCloseSession(sessionId, router));
69 defaultNetconfOperations.add(new DefaultStartExi(sessionId));
70 defaultNetconfOperations.add(new DefaultStopExi(sessionId));
71 defaultNetconfOperations.add(new DefaultCommit(commitNotifier, capabilityProvider, sessionId, router));
73 router.initNetconfOperations(getAllNetconfOperations(defaultNetconfOperations, netconfOperationServiceSnapshot));
78 private static Set<NetconfOperation> getAllNetconfOperations(final Set<NetconfOperation> defaultNetconfOperations,
79 final NetconfOperationServiceSnapshot netconfOperationServiceSnapshot) {
80 Set<NetconfOperation> result = new HashSet<>();
81 result.addAll(defaultNetconfOperations);
83 for (NetconfOperationService netconfOperationService : netconfOperationServiceSnapshot.getServices()) {
84 final Set<NetconfOperation> netOpsFromService = netconfOperationService.getNetconfOperations();
85 for (NetconfOperation netconfOperation : netOpsFromService) {
86 Preconditions.checkState(!result.contains(netconfOperation),
87 "Netconf operation %s already present", netconfOperation);
88 result.add(netconfOperation);
91 return Collections.unmodifiableSet(result);
95 public synchronized Document onNetconfMessage(final Document message,
96 final NetconfServerSession session) throws NetconfDocumentedException {
97 Preconditions.checkNotNull(allNetconfOperations, "Operation router was not initialized properly");
99 final NetconfOperationExecution netconfOperationExecution;
101 netconfOperationExecution = getNetconfOperationWithHighestPriority(message, session);
102 } catch (IllegalArgumentException | IllegalStateException e) {
103 final String messageAsString = XmlUtil.toString(message);
104 LOG.warn("Unable to handle rpc {} on session {}", messageAsString, session, e);
106 final NetconfDocumentedException.ErrorTag tag;
107 if (e instanceof IllegalArgumentException) {
108 tag = NetconfDocumentedException.ErrorTag.operation_not_supported;
110 tag = NetconfDocumentedException.ErrorTag.operation_failed;
113 throw new NetconfDocumentedException(
114 String.format("Unable to handle rpc %s on session %s", messageAsString, session),
115 e, NetconfDocumentedException.ErrorType.application,
116 tag, NetconfDocumentedException.ErrorSeverity.error,
117 Collections.singletonMap(tag.toString(), e.getMessage()));
118 } catch (RuntimeException e) {
119 throw handleUnexpectedEx("Unexpected exception during netconf operation sort", e);
123 return executeOperationWithHighestPriority(message, netconfOperationExecution);
124 } catch (RuntimeException e) {
125 throw handleUnexpectedEx("Unexpected exception during netconf operation execution", e);
130 public void close() throws Exception {
131 netconfOperationServiceSnapshot.close();
134 private NetconfDocumentedException handleUnexpectedEx(final String s, final Exception e) throws NetconfDocumentedException {
137 Map<String, String> info = Maps.newHashMap();
138 info.put(NetconfDocumentedException.ErrorSeverity.error.toString(), e.toString());
139 return new NetconfDocumentedException("Unexpected error",
140 NetconfDocumentedException.ErrorType.application,
141 NetconfDocumentedException.ErrorTag.operation_failed,
142 NetconfDocumentedException.ErrorSeverity.error, info);
145 private Document executeOperationWithHighestPriority(final Document message,
146 final NetconfOperationExecution netconfOperationExecution)
147 throws NetconfDocumentedException {
148 if (LOG.isDebugEnabled()) {
149 LOG.debug("Forwarding netconf message {} to {}", XmlUtil.toString(message), netconfOperationExecution.netconfOperation);
152 return netconfOperationExecution.execute(message);
155 private NetconfOperationExecution getNetconfOperationWithHighestPriority(
156 final Document message, final NetconfServerSession session) throws NetconfDocumentedException {
158 NavigableMap<HandlingPriority, NetconfOperation> sortedByPriority = getSortedNetconfOperationsWithCanHandle(
161 if (sortedByPriority.isEmpty()) {
162 throw new IllegalArgumentException(String.format("No %s available to handle message %s",
163 NetconfOperation.class.getName(), XmlUtil.toString(message)));
166 return NetconfOperationExecution.createExecutionChain(sortedByPriority, sortedByPriority.lastKey());
169 private TreeMap<HandlingPriority, NetconfOperation> getSortedNetconfOperationsWithCanHandle(final Document message,
170 final NetconfServerSession session) throws NetconfDocumentedException {
171 TreeMap<HandlingPriority, NetconfOperation> sortedPriority = Maps.newTreeMap();
173 for (NetconfOperation netconfOperation : allNetconfOperations) {
174 final HandlingPriority handlingPriority = netconfOperation.canHandle(message);
175 if (netconfOperation instanceof DefaultNetconfOperation) {
176 ((DefaultNetconfOperation) netconfOperation).setNetconfSession(session);
178 if (!handlingPriority.equals(HandlingPriority.CANNOT_HANDLE)) {
180 Preconditions.checkState(!sortedPriority.containsKey(handlingPriority),
181 "Multiple %s available to handle message %s with priority %s",
182 NetconfOperation.class.getName(), message, handlingPriority);
183 sortedPriority.put(handlingPriority, netconfOperation);
186 return sortedPriority;
189 public static final NetconfOperationChainedExecution EXECUTION_TERMINATION_POINT = new NetconfOperationChainedExecution() {
191 public boolean isExecutionTermination() {
196 public Document execute(final Document requestMessage) throws NetconfDocumentedException {
197 throw new NetconfDocumentedException("This execution represents the termination point in operation execution and cannot be executed itself",
198 NetconfDocumentedException.ErrorType.application,
199 NetconfDocumentedException.ErrorTag.operation_failed,
200 NetconfDocumentedException.ErrorSeverity.error);
204 private static class NetconfOperationExecution implements NetconfOperationChainedExecution {
205 private final NetconfOperation netconfOperation;
206 private final NetconfOperationChainedExecution subsequentExecution;
208 private NetconfOperationExecution(final NetconfOperation netconfOperation, final NetconfOperationChainedExecution subsequentExecution) {
209 this.netconfOperation = netconfOperation;
210 this.subsequentExecution = subsequentExecution;
214 public boolean isExecutionTermination() {
219 public Document execute(final Document message) throws NetconfDocumentedException {
220 return netconfOperation.handle(message, subsequentExecution);
223 public static NetconfOperationExecution createExecutionChain(
224 final NavigableMap<HandlingPriority, NetconfOperation> sortedByPriority, final HandlingPriority handlingPriority) {
225 NetconfOperation netconfOperation = sortedByPriority.get(handlingPriority);
226 HandlingPriority subsequentHandlingPriority = sortedByPriority.lowerKey(handlingPriority);
228 NetconfOperationChainedExecution subsequentExecution = null;
230 if (subsequentHandlingPriority != null) {
231 subsequentExecution = createExecutionChain(sortedByPriority, subsequentHandlingPriority);
233 subsequentExecution = EXECUTION_TERMINATION_POINT;
236 return new NetconfOperationExecution(netconfOperation, subsequentExecution);
241 public String toString() {
242 return "NetconfOperationRouterImpl{" + "netconfOperationServiceSnapshot=" + netconfOperationServiceSnapshot