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.api.monitoring.NetconfMonitoringService;
22 import org.opendaylight.netconf.impl.NetconfServerSession;
23 import org.opendaylight.netconf.impl.mapping.operations.DefaultCloseSession;
24 import org.opendaylight.netconf.impl.mapping.operations.DefaultNetconfOperation;
25 import org.opendaylight.netconf.impl.mapping.operations.DefaultStartExi;
26 import org.opendaylight.netconf.impl.mapping.operations.DefaultStopExi;
27 import org.opendaylight.netconf.mapping.api.HandlingPriority;
28 import org.opendaylight.netconf.mapping.api.NetconfOperation;
29 import org.opendaylight.netconf.mapping.api.NetconfOperationChainedExecution;
30 import org.opendaylight.netconf.mapping.api.NetconfOperationService;
31 import org.opendaylight.netconf.mapping.api.SessionAwareNetconfOperation;
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 (final RuntimeException e) {
80 throw handleUnexpectedEx("Unexpected exception during netconf operation sort", e);
84 return executeOperationWithHighestPriority(message, netconfOperationExecution);
85 } catch (final 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 final 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 final TreeMap<HandlingPriority, NetconfOperation> sortedPriority = Maps.newTreeMap();
132 for (final 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 private static class NetconfOperationExecution implements NetconfOperationChainedExecution {
152 private final NetconfOperation netconfOperation;
153 private final NetconfOperationChainedExecution subsequentExecution;
155 private NetconfOperationExecution(final NetconfOperation netconfOperation, final NetconfOperationChainedExecution subsequentExecution) {
156 this.netconfOperation = netconfOperation;
157 this.subsequentExecution = subsequentExecution;
161 public boolean isExecutionTermination() {
166 public Document execute(final Document message) throws DocumentedException {
167 return netconfOperation.handle(message, subsequentExecution);
170 public static NetconfOperationExecution createExecutionChain(
171 final NavigableMap<HandlingPriority, NetconfOperation> sortedByPriority, final HandlingPriority handlingPriority) {
172 final NetconfOperation netconfOperation = sortedByPriority.get(handlingPriority);
173 final HandlingPriority subsequentHandlingPriority = sortedByPriority.lowerKey(handlingPriority);
175 NetconfOperationChainedExecution subsequentExecution = null;
177 if (subsequentHandlingPriority != null) {
178 subsequentExecution = createExecutionChain(sortedByPriority, subsequentHandlingPriority);
180 subsequentExecution = EXECUTION_TERMINATION_POINT;
183 return new NetconfOperationExecution(netconfOperation, subsequentExecution);
188 public String toString() {
189 return "NetconfOperationRouterImpl{" + "netconfOperationServiceSnapshot=" + netconfOperationServiceSnapshot