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 NetconfOperationExecution netconfOperationExecution;
101 String messageAsString = "";
103 messageAsString = XmlUtil.toString(message);
104 netconfOperationExecution = getNetconfOperationWithHighestPriority(message, session);
105 } catch (IllegalArgumentException | IllegalStateException e) {
106 LOG.warn("Unable to handle rpc {} on session {}", messageAsString, session, e);
108 String errorMessage = String.format("Unable to handle rpc %s on session %s", messageAsString, session);
109 Map<String, String> errorInfo = Maps.newHashMap();
111 NetconfDocumentedException.ErrorTag tag;
112 if (e instanceof IllegalArgumentException) {
113 errorInfo.put(NetconfDocumentedException.ErrorTag.operation_not_supported.toString(), e.getMessage());
114 tag = NetconfDocumentedException.ErrorTag.operation_not_supported;
116 errorInfo.put(NetconfDocumentedException.ErrorTag.operation_failed.toString(), e.getMessage());
117 tag = NetconfDocumentedException.ErrorTag.operation_failed;
120 throw new NetconfDocumentedException(errorMessage, e, NetconfDocumentedException.ErrorType.application,
121 tag, NetconfDocumentedException.ErrorSeverity.error, errorInfo);
122 } catch (RuntimeException e) {
123 throw handleUnexpectedEx("Unexpected exception during netconf operation sort", e);
127 return executeOperationWithHighestPriority(message, netconfOperationExecution, messageAsString);
128 } catch (RuntimeException e) {
129 throw handleUnexpectedEx("Unexpected exception during netconf operation execution", e);
134 public void close() throws Exception {
135 netconfOperationServiceSnapshot.close();
138 private NetconfDocumentedException handleUnexpectedEx(final String s, final Exception e) throws NetconfDocumentedException {
141 Map<String, String> info = Maps.newHashMap();
142 info.put(NetconfDocumentedException.ErrorSeverity.error.toString(), e.toString());
143 return new NetconfDocumentedException("Unexpected error",
144 NetconfDocumentedException.ErrorType.application,
145 NetconfDocumentedException.ErrorTag.operation_failed,
146 NetconfDocumentedException.ErrorSeverity.error, info);
149 private Document executeOperationWithHighestPriority(final Document message,
150 final NetconfOperationExecution netconfOperationExecution, final String messageAsString)
151 throws NetconfDocumentedException {
152 LOG.debug("Forwarding netconf message {} to {}", messageAsString, netconfOperationExecution.netconfOperation);
153 return netconfOperationExecution.execute(message);
156 private NetconfOperationExecution getNetconfOperationWithHighestPriority(
157 final Document message, final NetconfServerSession session) throws NetconfDocumentedException {
159 NavigableMap<HandlingPriority, NetconfOperation> sortedByPriority = getSortedNetconfOperationsWithCanHandle(
162 if (sortedByPriority.isEmpty()) {
163 throw new IllegalArgumentException(String.format("No %s available to handle message %s",
164 NetconfOperation.class.getName(), XmlUtil.toString(message)));
167 return NetconfOperationExecution.createExecutionChain(sortedByPriority, sortedByPriority.lastKey());
170 private TreeMap<HandlingPriority, NetconfOperation> getSortedNetconfOperationsWithCanHandle(final Document message,
171 final NetconfServerSession session) throws NetconfDocumentedException {
172 TreeMap<HandlingPriority, NetconfOperation> sortedPriority = Maps.newTreeMap();
174 for (NetconfOperation netconfOperation : allNetconfOperations) {
175 final HandlingPriority handlingPriority = netconfOperation.canHandle(message);
176 if (netconfOperation instanceof DefaultNetconfOperation) {
177 ((DefaultNetconfOperation) netconfOperation).setNetconfSession(session);
179 if (!handlingPriority.equals(HandlingPriority.CANNOT_HANDLE)) {
181 Preconditions.checkState(!sortedPriority.containsKey(handlingPriority),
182 "Multiple %s available to handle message %s with priority %s",
183 NetconfOperation.class.getName(), message, handlingPriority);
184 sortedPriority.put(handlingPriority, netconfOperation);
187 return sortedPriority;
190 public static final NetconfOperationChainedExecution EXECUTION_TERMINATION_POINT = new NetconfOperationChainedExecution() {
192 public boolean isExecutionTermination() {
197 public Document execute(final Document requestMessage) throws NetconfDocumentedException {
198 throw new NetconfDocumentedException("This execution represents the termination point in operation execution and cannot be executed itself",
199 NetconfDocumentedException.ErrorType.application,
200 NetconfDocumentedException.ErrorTag.operation_failed,
201 NetconfDocumentedException.ErrorSeverity.error);
205 private static class NetconfOperationExecution implements NetconfOperationChainedExecution {
206 private final NetconfOperation netconfOperation;
207 private final NetconfOperationChainedExecution subsequentExecution;
209 private NetconfOperationExecution(final NetconfOperation netconfOperation, final NetconfOperationChainedExecution subsequentExecution) {
210 this.netconfOperation = netconfOperation;
211 this.subsequentExecution = subsequentExecution;
215 public boolean isExecutionTermination() {
220 public Document execute(final Document message) throws NetconfDocumentedException {
221 return netconfOperation.handle(message, subsequentExecution);
224 public static NetconfOperationExecution createExecutionChain(
225 final NavigableMap<HandlingPriority, NetconfOperation> sortedByPriority, final HandlingPriority handlingPriority) {
226 NetconfOperation netconfOperation = sortedByPriority.get(handlingPriority);
227 HandlingPriority subsequentHandlingPriority = sortedByPriority.lowerKey(handlingPriority);
229 NetconfOperationChainedExecution subsequentExecution = null;
231 if (subsequentHandlingPriority != null) {
232 subsequentExecution = createExecutionChain(sortedByPriority, subsequentHandlingPriority);
234 subsequentExecution = EXECUTION_TERMINATION_POINT;
237 return new NetconfOperationExecution(netconfOperation, subsequentExecution);
242 public String toString() {
243 return "NetconfOperationRouterImpl{" + "netconfOperationServiceSnapshot=" + netconfOperationServiceSnapshot