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