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.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.netconf.api.NetconfDocumentedException;
20 import org.opendaylight.controller.netconf.impl.CommitNotifier;
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.mapping.api.SessionAwareNetconfOperation;
35 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.w3c.dom.Document;
40 public class NetconfOperationRouterImpl implements NetconfOperationRouter {
42 private static final Logger LOG = LoggerFactory.getLogger(NetconfOperationRouterImpl.class);
43 private final NetconfOperationServiceSnapshot netconfOperationServiceSnapshot;
44 private final Collection<NetconfOperation> allNetconfOperations;
46 public NetconfOperationRouterImpl(final NetconfOperationServiceSnapshot netconfOperationServiceSnapshot, final CapabilityProvider capabilityProvider,
47 final CommitNotifier commitNotifier) {
48 this.netconfOperationServiceSnapshot = Preconditions.checkNotNull(netconfOperationServiceSnapshot);
50 final String sessionId = netconfOperationServiceSnapshot.getNetconfSessionIdForReporting();
52 final Set<NetconfOperation> ops = new HashSet<>();
53 ops.add(new DefaultGetSchema(capabilityProvider, sessionId));
54 ops.add(new DefaultCloseSession(sessionId, this));
55 ops.add(new DefaultStartExi(sessionId));
56 ops.add(new DefaultStopExi(sessionId));
57 ops.add(new DefaultCommit(commitNotifier, capabilityProvider, sessionId, this));
59 for (NetconfOperationService netconfOperationService : netconfOperationServiceSnapshot.getServices()) {
60 for (NetconfOperation netconfOperation : netconfOperationService.getNetconfOperations()) {
61 Preconditions.checkState(!ops.contains(netconfOperation),
62 "Netconf operation %s already present", netconfOperation);
63 ops.add(netconfOperation);
67 allNetconfOperations = ImmutableSet.copyOf(ops);
71 public Document onNetconfMessage(final Document message, final NetconfServerSession session) throws NetconfDocumentedException {
72 Preconditions.checkNotNull(allNetconfOperations, "Operation router was not initialized properly");
74 final NetconfOperationExecution netconfOperationExecution;
76 netconfOperationExecution = getNetconfOperationWithHighestPriority(message, session);
77 } catch (IllegalArgumentException | IllegalStateException e) {
78 final String messageAsString = XmlUtil.toString(message);
79 LOG.warn("Unable to handle rpc {} on session {}", messageAsString, session, e);
81 final NetconfDocumentedException.ErrorTag tag;
82 if (e instanceof IllegalArgumentException) {
83 tag = NetconfDocumentedException.ErrorTag.operation_not_supported;
85 tag = NetconfDocumentedException.ErrorTag.operation_failed;
88 throw new NetconfDocumentedException(
89 String.format("Unable to handle rpc %s on session %s", messageAsString, session),
90 e, NetconfDocumentedException.ErrorType.application,
91 tag, NetconfDocumentedException.ErrorSeverity.error,
92 Collections.singletonMap(tag.toString(), e.getMessage()));
93 } catch (RuntimeException e) {
94 throw handleUnexpectedEx("Unexpected exception during netconf operation sort", e);
98 return executeOperationWithHighestPriority(message, netconfOperationExecution);
99 } catch (RuntimeException e) {
100 throw handleUnexpectedEx("Unexpected exception during netconf operation execution", e);
105 public void close() throws Exception {
106 netconfOperationServiceSnapshot.close();
109 private static NetconfDocumentedException handleUnexpectedEx(final String s, final Exception e) throws NetconfDocumentedException {
110 LOG.error("{}", s, e);
111 return new NetconfDocumentedException("Unexpected error",
112 NetconfDocumentedException.ErrorType.application,
113 NetconfDocumentedException.ErrorTag.operation_failed,
114 NetconfDocumentedException.ErrorSeverity.error,
115 Collections.singletonMap(NetconfDocumentedException.ErrorSeverity.error.toString(), e.toString()));
118 private Document executeOperationWithHighestPriority(final Document message,
119 final NetconfOperationExecution netconfOperationExecution)
120 throws NetconfDocumentedException {
121 if (LOG.isDebugEnabled()) {
122 LOG.debug("Forwarding netconf message {} to {}", XmlUtil.toString(message), netconfOperationExecution.netconfOperation);
125 return netconfOperationExecution.execute(message);
128 private NetconfOperationExecution getNetconfOperationWithHighestPriority(
129 final Document message, final NetconfServerSession session) throws NetconfDocumentedException {
131 NavigableMap<HandlingPriority, NetconfOperation> sortedByPriority = getSortedNetconfOperationsWithCanHandle(
134 if (sortedByPriority.isEmpty()) {
135 throw new IllegalArgumentException(String.format("No %s available to handle message %s",
136 NetconfOperation.class.getName(), XmlUtil.toString(message)));
139 return NetconfOperationExecution.createExecutionChain(sortedByPriority, sortedByPriority.lastKey());
142 private TreeMap<HandlingPriority, NetconfOperation> getSortedNetconfOperationsWithCanHandle(final Document message,
143 final NetconfServerSession session) throws NetconfDocumentedException {
144 TreeMap<HandlingPriority, NetconfOperation> sortedPriority = Maps.newTreeMap();
146 for (NetconfOperation netconfOperation : allNetconfOperations) {
147 final HandlingPriority handlingPriority = netconfOperation.canHandle(message);
148 if (netconfOperation instanceof DefaultNetconfOperation) {
149 ((DefaultNetconfOperation) netconfOperation).setNetconfSession(session);
151 if(netconfOperation instanceof SessionAwareNetconfOperation) {
152 ((SessionAwareNetconfOperation) netconfOperation).setSession(session);
154 if (!handlingPriority.equals(HandlingPriority.CANNOT_HANDLE)) {
156 Preconditions.checkState(!sortedPriority.containsKey(handlingPriority),
157 "Multiple %s available to handle message %s with priority %s",
158 NetconfOperation.class.getName(), message, handlingPriority);
159 sortedPriority.put(handlingPriority, netconfOperation);
162 return sortedPriority;
165 public static final NetconfOperationChainedExecution EXECUTION_TERMINATION_POINT = new NetconfOperationChainedExecution() {
167 public boolean isExecutionTermination() {
172 public Document execute(final Document requestMessage) throws NetconfDocumentedException {
173 throw new NetconfDocumentedException("This execution represents the termination point in operation execution and cannot be executed itself",
174 NetconfDocumentedException.ErrorType.application,
175 NetconfDocumentedException.ErrorTag.operation_failed,
176 NetconfDocumentedException.ErrorSeverity.error);
180 private static class NetconfOperationExecution implements NetconfOperationChainedExecution {
181 private final NetconfOperation netconfOperation;
182 private final NetconfOperationChainedExecution subsequentExecution;
184 private NetconfOperationExecution(final NetconfOperation netconfOperation, final NetconfOperationChainedExecution subsequentExecution) {
185 this.netconfOperation = netconfOperation;
186 this.subsequentExecution = subsequentExecution;
190 public boolean isExecutionTermination() {
195 public Document execute(final Document message) throws NetconfDocumentedException {
196 return netconfOperation.handle(message, subsequentExecution);
199 public static NetconfOperationExecution createExecutionChain(
200 final NavigableMap<HandlingPriority, NetconfOperation> sortedByPriority, final HandlingPriority handlingPriority) {
201 NetconfOperation netconfOperation = sortedByPriority.get(handlingPriority);
202 HandlingPriority subsequentHandlingPriority = sortedByPriority.lowerKey(handlingPriority);
204 NetconfOperationChainedExecution subsequentExecution = null;
206 if (subsequentHandlingPriority != null) {
207 subsequentExecution = createExecutionChain(sortedByPriority, subsequentHandlingPriority);
209 subsequentExecution = EXECUTION_TERMINATION_POINT;
212 return new NetconfOperationExecution(netconfOperation, subsequentExecution);
217 public String toString() {
218 return "NetconfOperationRouterImpl{" + "netconfOperationServiceSnapshot=" + netconfOperationServiceSnapshot