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