3aa1c7107243a89c3a1a92ff3e15131eb2ec3a09
[netconf.git] / netconf / netconf-impl / src / main / java / org / opendaylight / netconf / impl / osgi / NetconfOperationRouterImpl.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.netconf.impl.osgi;
9
10 import static com.google.common.base.Preconditions.checkState;
11 import static java.util.Objects.requireNonNull;
12
13 import com.google.common.collect.ImmutableSet;
14 import java.util.Collection;
15 import java.util.HashSet;
16 import java.util.Map;
17 import java.util.NavigableMap;
18 import java.util.Set;
19 import java.util.TreeMap;
20 import org.opendaylight.netconf.api.DocumentedException;
21 import org.opendaylight.netconf.api.monitoring.NetconfMonitoringService;
22 import org.opendaylight.netconf.api.xml.XmlUtil;
23 import org.opendaylight.netconf.impl.NetconfServerSession;
24 import org.opendaylight.netconf.impl.mapping.operations.DefaultCloseSession;
25 import org.opendaylight.netconf.impl.mapping.operations.DefaultNetconfOperation;
26 import org.opendaylight.netconf.impl.mapping.operations.DefaultStartExi;
27 import org.opendaylight.netconf.impl.mapping.operations.DefaultStopExi;
28 import org.opendaylight.netconf.mapping.api.HandlingPriority;
29 import org.opendaylight.netconf.mapping.api.NetconfOperation;
30 import org.opendaylight.netconf.mapping.api.NetconfOperationChainedExecution;
31 import org.opendaylight.netconf.mapping.api.NetconfOperationService;
32 import org.opendaylight.netconf.mapping.api.SessionAwareNetconfOperation;
33 import org.opendaylight.yangtools.yang.common.ErrorSeverity;
34 import org.opendaylight.yangtools.yang.common.ErrorTag;
35 import org.opendaylight.yangtools.yang.common.ErrorType;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.w3c.dom.Document;
39
40 public class NetconfOperationRouterImpl implements NetconfOperationRouter {
41
42     private static final Logger LOG = LoggerFactory.getLogger(NetconfOperationRouterImpl.class);
43     private final NetconfOperationService netconfOperationServiceSnapshot;
44     private final Collection<NetconfOperation> allNetconfOperations;
45
46     public NetconfOperationRouterImpl(final NetconfOperationService netconfOperationServiceSnapshot,
47                                       final NetconfMonitoringService netconfMonitoringService, final String sessionId) {
48         this.netconfOperationServiceSnapshot = requireNonNull(netconfOperationServiceSnapshot);
49
50         final Set<NetconfOperation> ops = new HashSet<>();
51         ops.add(new DefaultCloseSession(sessionId, this));
52         ops.add(new DefaultStartExi(sessionId));
53         ops.add(new DefaultStopExi(sessionId));
54
55         ops.addAll(netconfOperationServiceSnapshot.getNetconfOperations());
56
57         allNetconfOperations = ImmutableSet.copyOf(ops);
58     }
59
60     @SuppressWarnings("checkstyle:IllegalCatch")
61     @Override
62     public Document onNetconfMessage(final Document message, final NetconfServerSession session) throws
63             DocumentedException {
64         requireNonNull(allNetconfOperations, "Operation router was not initialized properly");
65
66         final NetconfOperationExecution netconfOperationExecution;
67         try {
68             netconfOperationExecution = getNetconfOperationWithHighestPriority(message, session);
69         } catch (IllegalArgumentException | IllegalStateException e) {
70             final String messageAsString = XmlUtil.toString(message);
71             LOG.warn("Unable to handle rpc {} on session {}", messageAsString, session, e);
72
73             final ErrorTag tag = e instanceof IllegalArgumentException ? ErrorTag.OPERATION_NOT_SUPPORTED
74                 : ErrorTag.OPERATION_FAILED;
75
76             throw new DocumentedException(
77                     String.format("Unable to handle rpc %s on session %s", messageAsString, session), e,
78                     ErrorType.APPLICATION, tag, ErrorSeverity.ERROR,
79                     // FIXME: i.e. in what namespace are we providing these tags? why is this not just:
80                     //
81                     // <java-throwable xmlns="org.opendaylight.something">
82                     //   <message>e.getMessage()</message>
83                     // </java-throwable>
84                     //
85                     // for each place where we are mapping Exception.getMessage() ? We probably do not want to propagate
86                     // stack traces out, but suppressed exceptions and causal list might be interesting:
87                     //
88                     // <java-throwable xmlns="org.opendaylight.something">
89                     //   <message>reported exception</message>
90                     // </java-throwable>
91                     // <java-throwable xmlns="org.opendaylight.something">
92                     //   <message>cause of reported exception</message>
93                     // </java-throwable>
94                     // <java-throwable xmlns="org.opendaylight.something">
95                     //   <message>cause of cause of reported exception</message>
96                     // </java-throwable>
97                     Map.of(tag.elementBody(), e.getMessage()));
98         } catch (final RuntimeException e) {
99             throw handleUnexpectedEx("sort", e);
100         }
101
102         try {
103             return executeOperationWithHighestPriority(message, netconfOperationExecution);
104         } catch (final RuntimeException e) {
105             throw handleUnexpectedEx("execution", e);
106         }
107     }
108
109     @Override
110     public void close() {
111         netconfOperationServiceSnapshot.close();
112     }
113
114     private static DocumentedException handleUnexpectedEx(final String op, final Exception exception) {
115         LOG.error("Unexpected exception during netconf operation {}", op, exception);
116         return new DocumentedException("Unexpected error",
117                 ErrorType.APPLICATION, ErrorTag.OPERATION_FAILED, ErrorSeverity.ERROR,
118                 // FIXME: i.e. <error>exception.toString()</error>? That looks wrong on a few levels.
119                 Map.of(ErrorSeverity.ERROR.elementBody(), exception.toString()));
120     }
121
122     private static Document executeOperationWithHighestPriority(final Document message,
123             final NetconfOperationExecution netconfOperationExecution) throws DocumentedException {
124         if (LOG.isDebugEnabled()) {
125             LOG.debug("Forwarding netconf message {} to {}", XmlUtil.toString(message), netconfOperationExecution
126                     .netconfOperation);
127         }
128
129         return netconfOperationExecution.execute(message);
130     }
131
132     private NetconfOperationExecution getNetconfOperationWithHighestPriority(
133             final Document message, final NetconfServerSession session) throws DocumentedException {
134
135         final NavigableMap<HandlingPriority, NetconfOperation> sortedByPriority =
136                 getSortedNetconfOperationsWithCanHandle(
137                 message, session);
138
139         if (sortedByPriority.isEmpty()) {
140             throw new IllegalArgumentException(String.format("No %s available to handle message %s",
141                     NetconfOperation.class.getName(), XmlUtil.toString(message)));
142         }
143
144         return NetconfOperationExecution.createExecutionChain(sortedByPriority, sortedByPriority.lastKey());
145     }
146
147     private TreeMap<HandlingPriority, NetconfOperation> getSortedNetconfOperationsWithCanHandle(
148             final Document message, final NetconfServerSession session) throws DocumentedException {
149         final TreeMap<HandlingPriority, NetconfOperation> sortedPriority = new TreeMap<>();
150
151         for (final NetconfOperation netconfOperation : allNetconfOperations) {
152             final HandlingPriority handlingPriority = netconfOperation.canHandle(message);
153             if (netconfOperation instanceof DefaultNetconfOperation) {
154                 ((DefaultNetconfOperation) netconfOperation).setNetconfSession(session);
155             }
156             if (netconfOperation instanceof SessionAwareNetconfOperation) {
157                 ((SessionAwareNetconfOperation) netconfOperation).setSession(session);
158             }
159             if (!handlingPriority.equals(HandlingPriority.CANNOT_HANDLE)) {
160
161                 checkState(!sortedPriority.containsKey(handlingPriority),
162                         "Multiple %s available to handle message %s with priority %s, %s and %s",
163                         NetconfOperation.class.getName(), message, handlingPriority, netconfOperation, sortedPriority
164                                 .get(handlingPriority));
165                 sortedPriority.put(handlingPriority, netconfOperation);
166             }
167         }
168         return sortedPriority;
169     }
170
171     private static final class NetconfOperationExecution implements NetconfOperationChainedExecution {
172         private final NetconfOperation netconfOperation;
173         private final NetconfOperationChainedExecution subsequentExecution;
174
175         private NetconfOperationExecution(final NetconfOperation netconfOperation,
176                                           final NetconfOperationChainedExecution subsequentExecution) {
177             this.netconfOperation = netconfOperation;
178             this.subsequentExecution = subsequentExecution;
179         }
180
181         @Override
182         public boolean isExecutionTermination() {
183             return false;
184         }
185
186         @Override
187         public Document execute(final Document message) throws DocumentedException {
188             return netconfOperation.handle(message, subsequentExecution);
189         }
190
191         public static NetconfOperationExecution createExecutionChain(
192                 final NavigableMap<HandlingPriority, NetconfOperation> sortedByPriority,
193                 final HandlingPriority handlingPriority) {
194             final NetconfOperation netconfOperation = sortedByPriority.get(handlingPriority);
195             final HandlingPriority subsequentHandlingPriority = sortedByPriority.lowerKey(handlingPriority);
196
197             NetconfOperationChainedExecution subsequentExecution = null;
198
199             if (subsequentHandlingPriority != null) {
200                 subsequentExecution = createExecutionChain(sortedByPriority, subsequentHandlingPriority);
201             } else {
202                 subsequentExecution = EXECUTION_TERMINATION_POINT;
203             }
204
205             return new NetconfOperationExecution(netconfOperation, subsequentExecution);
206         }
207     }
208
209     @Override
210     public String toString() {
211         return "NetconfOperationRouterImpl{" + "netconfOperationServiceSnapshot=" + netconfOperationServiceSnapshot
212                 + '}';
213     }
214 }