fix ServiceHandler SpotBugs false positives
[transportpce.git] / tests / honeynode / 2.2.1 / 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 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;
17 import java.util.Set;
18 import java.util.TreeMap;
19 import org.opendaylight.netconf.api.DocumentedException;
20 import org.opendaylight.netconf.api.monitoring.NetconfMonitoringService;
21 import org.opendaylight.netconf.api.xml.XmlUtil;
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;
35
36 public class NetconfOperationRouterImpl implements NetconfOperationRouter {
37
38     private static final Logger LOG = LoggerFactory.getLogger(NetconfOperationRouterImpl.class);
39     private final NetconfOperationService netconfOperationServiceSnapshot;
40     private final Collection<NetconfOperation> allNetconfOperations;
41
42     public NetconfOperationRouterImpl(final NetconfOperationService netconfOperationServiceSnapshot,
43                                       final NetconfMonitoringService netconfMonitoringService, final String sessionId) {
44         this.netconfOperationServiceSnapshot = Preconditions.checkNotNull(netconfOperationServiceSnapshot);
45
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));
50
51         ops.addAll(netconfOperationServiceSnapshot.getNetconfOperations());
52
53         allNetconfOperations = ImmutableSet.copyOf(ops);
54     }
55
56     @SuppressWarnings("checkstyle:IllegalCatch")
57     @Override
58     public Document onNetconfMessage(final Document message, final NetconfServerSession session) throws
59             DocumentedException {
60         Preconditions.checkNotNull(allNetconfOperations, "Operation router was not initialized properly");
61
62         final NetconfOperationExecution netconfOperationExecution;
63         try {
64             netconfOperationExecution = getNetconfOperationWithHighestPriority(message, session);
65         } catch (IllegalArgumentException | IllegalStateException e) {
66             final String messageAsString = XmlUtil.toString(message);
67             LOG.warn("Unable to handle rpc {} on session {}", messageAsString, session, e);
68
69             final DocumentedException.ErrorTag tag;
70             if (e instanceof IllegalArgumentException) {
71                 tag = DocumentedException.ErrorTag.OPERATION_NOT_SUPPORTED;
72             } else {
73                 tag = DocumentedException.ErrorTag.OPERATION_FAILED;
74             }
75
76             throw new DocumentedException(
77                     String.format("Unable to handle rpc %s on session %s", messageAsString, session),
78                     e, DocumentedException.ErrorType.APPLICATION,
79                     tag, DocumentedException.ErrorSeverity.ERROR,
80                     Collections.singletonMap(tag.toString(), e.getMessage()));
81         } catch (final RuntimeException e) {
82             throw handleUnexpectedEx("Unexpected exception during netconf operation sort", e);
83         }
84
85         try {
86             return executeOperationWithHighestPriority(message, netconfOperationExecution);
87         } catch (final RuntimeException e) {
88             throw handleUnexpectedEx("Unexpected exception during netconf operation execution", e);
89         }
90     }
91
92     @Override
93     public void close() {
94         netconfOperationServiceSnapshot.close();
95     }
96
97     private static DocumentedException handleUnexpectedEx(final String message, final Exception exception) {
98         LOG.error("{}", message, exception);
99         return new DocumentedException("Unexpected error",
100                 DocumentedException.ErrorType.APPLICATION,
101                 DocumentedException.ErrorTag.OPERATION_FAILED,
102                 DocumentedException.ErrorSeverity.ERROR,
103                 Collections.singletonMap(DocumentedException.ErrorSeverity.ERROR.toString(), exception.toString()));
104     }
105
106     private static Document executeOperationWithHighestPriority(final Document message,
107             final NetconfOperationExecution netconfOperationExecution) throws DocumentedException {
108         if (LOG.isDebugEnabled()) {
109             LOG.debug("Forwarding netconf message {} to {}", XmlUtil.toString(message), netconfOperationExecution
110                     .netconfOperation);
111         }
112
113         return netconfOperationExecution.execute(message);
114     }
115
116     private NetconfOperationExecution getNetconfOperationWithHighestPriority(
117             final Document message, final NetconfServerSession session) throws DocumentedException {
118
119         final NavigableMap<HandlingPriority, NetconfOperation> sortedByPriority =
120                 getSortedNetconfOperationsWithCanHandle(
121                 message, session);
122
123         if (sortedByPriority.isEmpty()) {
124             throw new IllegalArgumentException(String.format("No %s available to handle message %s",
125                     NetconfOperation.class.getName(), XmlUtil.toString(message)));
126         }
127
128         return NetconfOperationExecution.createExecutionChain(sortedByPriority, sortedByPriority.lastKey());
129     }
130
131     private TreeMap<HandlingPriority, NetconfOperation> getSortedNetconfOperationsWithCanHandle(
132             final Document message, final NetconfServerSession session) throws DocumentedException {
133         final TreeMap<HandlingPriority, NetconfOperation> sortedPriority = Maps.newTreeMap();
134
135         for (final NetconfOperation netconfOperation : allNetconfOperations) {
136             final HandlingPriority handlingPriority = netconfOperation.canHandle(message);
137             if (netconfOperation instanceof DefaultNetconfOperation) {
138                 ((DefaultNetconfOperation) netconfOperation).setNetconfSession(session);
139             }
140             if (netconfOperation instanceof SessionAwareNetconfOperation) {
141                 ((SessionAwareNetconfOperation) netconfOperation).setSession(session);
142             }
143             if (!handlingPriority.equals(HandlingPriority.CANNOT_HANDLE)) {
144
145                 Preconditions.checkState(!sortedPriority.containsKey(handlingPriority),
146                         "Multiple %s available to handle message %s with priority %s, %s and %s",
147                         NetconfOperation.class.getName(), message, handlingPriority, netconfOperation, sortedPriority
148                                 .get(handlingPriority));
149                 sortedPriority.put(handlingPriority, netconfOperation);
150             }
151         }
152         return sortedPriority;
153     }
154
155     private static final class NetconfOperationExecution implements NetconfOperationChainedExecution {
156         private final NetconfOperation netconfOperation;
157         private final NetconfOperationChainedExecution subsequentExecution;
158
159         private NetconfOperationExecution(final NetconfOperation netconfOperation,
160                                           final NetconfOperationChainedExecution subsequentExecution) {
161             this.netconfOperation = netconfOperation;
162             this.subsequentExecution = subsequentExecution;
163         }
164
165         @Override
166         public boolean isExecutionTermination() {
167             return false;
168         }
169
170         @Override
171         public Document execute(final Document message) throws DocumentedException {
172             return netconfOperation.handle(message, subsequentExecution);
173         }
174
175         public static NetconfOperationExecution createExecutionChain(
176                 final NavigableMap<HandlingPriority, NetconfOperation> sortedByPriority,
177                 final HandlingPriority handlingPriority) {
178             final NetconfOperation netconfOperation = sortedByPriority.get(handlingPriority);
179             final HandlingPriority subsequentHandlingPriority = sortedByPriority.lowerKey(handlingPriority);
180
181             NetconfOperationChainedExecution subsequentExecution = null;
182
183             if (subsequentHandlingPriority != null) {
184                 subsequentExecution = createExecutionChain(sortedByPriority, subsequentHandlingPriority);
185             } else {
186                 subsequentExecution = EXECUTION_TERMINATION_POINT;
187             }
188
189             return new NetconfOperationExecution(netconfOperation, subsequentExecution);
190         }
191     }
192
193     @Override
194     public String toString() {
195         return "NetconfOperationRouterImpl{" + "netconfOperationServiceSnapshot=" + netconfOperationServiceSnapshot
196                 + '}';
197     }
198 }