Merge "Bug 460 - Fix warning throughout netconf subsystem"
[controller.git] / opendaylight / netconf / netconf-impl / src / main / java / org / opendaylight / controller / 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.controller.netconf.impl.osgi;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.collect.Maps;
12 import com.google.common.collect.Sets;
13 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
14 import org.opendaylight.controller.netconf.impl.DefaultCommitNotificationProducer;
15 import org.opendaylight.controller.netconf.impl.NetconfServerSession;
16 import org.opendaylight.controller.netconf.impl.mapping.CapabilityProvider;
17 import org.opendaylight.controller.netconf.impl.mapping.operations.DefaultCloseSession;
18 import org.opendaylight.controller.netconf.impl.mapping.operations.DefaultCommit;
19 import org.opendaylight.controller.netconf.impl.mapping.operations.DefaultGetSchema;
20 import org.opendaylight.controller.netconf.impl.mapping.operations.DefaultNetconfOperation;
21 import org.opendaylight.controller.netconf.impl.mapping.operations.DefaultStartExi;
22 import org.opendaylight.controller.netconf.impl.mapping.operations.DefaultStopExi;
23 import org.opendaylight.controller.netconf.mapping.api.HandlingPriority;
24 import org.opendaylight.controller.netconf.mapping.api.NetconfOperation;
25 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationChainedExecution;
26 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationService;
27 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationServiceSnapshot;
28 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.w3c.dom.Document;
32
33 import java.util.Collections;
34 import java.util.HashSet;
35 import java.util.Map;
36 import java.util.NavigableMap;
37 import java.util.Set;
38 import java.util.TreeMap;
39
40 public class NetconfOperationRouterImpl implements NetconfOperationRouter {
41
42     private static final Logger logger = LoggerFactory.getLogger(NetconfOperationRouterImpl.class);
43
44     private final NetconfOperationServiceSnapshot netconfOperationServiceSnapshot;
45     private Set<NetconfOperation> allNetconfOperations;
46
47     private NetconfOperationRouterImpl(NetconfOperationServiceSnapshot netconfOperationServiceSnapshot) {
48         this.netconfOperationServiceSnapshot = netconfOperationServiceSnapshot;
49     }
50
51     private void initNetconfOperations(Set<NetconfOperation> allOperations) {
52         allNetconfOperations = allOperations;
53     }
54
55     /**
56      * Factory method to produce instance of NetconfOperationRouter
57      */
58     public static NetconfOperationRouter createOperationRouter(NetconfOperationServiceSnapshot netconfOperationServiceSnapshot,
59                                                                CapabilityProvider capabilityProvider, DefaultCommitNotificationProducer commitNotifier) {
60         NetconfOperationRouterImpl router = new NetconfOperationRouterImpl(netconfOperationServiceSnapshot);
61
62         Preconditions.checkNotNull(netconfOperationServiceSnapshot);
63         Preconditions.checkNotNull(capabilityProvider);
64
65         final String sessionId = netconfOperationServiceSnapshot.getNetconfSessionIdForReporting();
66
67         final Set<NetconfOperation> defaultNetconfOperations = Sets.newHashSet();
68         defaultNetconfOperations.add(new DefaultGetSchema(capabilityProvider, sessionId));
69         defaultNetconfOperations.add(new DefaultCloseSession(sessionId, router));
70         defaultNetconfOperations.add(new DefaultStartExi(sessionId));
71         defaultNetconfOperations.add(new DefaultStopExi(sessionId));
72         defaultNetconfOperations.add(new DefaultCommit(commitNotifier, capabilityProvider, sessionId, router));
73
74         router.initNetconfOperations(getAllNetconfOperations(defaultNetconfOperations, netconfOperationServiceSnapshot));
75
76         return router;
77     }
78
79     private static Set<NetconfOperation> getAllNetconfOperations(Set<NetconfOperation> defaultNetconfOperations,
80             NetconfOperationServiceSnapshot netconfOperationServiceSnapshot) {
81         Set<NetconfOperation> result = new HashSet<>();
82         result.addAll(defaultNetconfOperations);
83
84         for (NetconfOperationService netconfOperationService : netconfOperationServiceSnapshot.getServices()) {
85             final Set<NetconfOperation> netOpsFromService = netconfOperationService.getNetconfOperations();
86             for (NetconfOperation netconfOperation : netOpsFromService) {
87                 Preconditions.checkState(!result.contains(netconfOperation),
88                         "Netconf operation %s already present", netconfOperation);
89                 result.add(netconfOperation);
90             }
91         }
92         return Collections.unmodifiableSet(result);
93     }
94
95     @Override
96     public synchronized Document onNetconfMessage(Document message,
97             NetconfServerSession session) throws NetconfDocumentedException {
98         Preconditions.checkNotNull(allNetconfOperations, "Operation router was not initialized properly");
99
100         NetconfOperationExecution netconfOperationExecution;
101
102         String messageAsString = "";
103         try {
104             messageAsString = XmlUtil.toString(message);
105             netconfOperationExecution = getNetconfOperationWithHighestPriority(message, session);
106         } catch (IllegalArgumentException | IllegalStateException e) {
107             logger.warn("Unable to handle rpc {} on session {}", messageAsString, session, e);
108
109             String errorMessage = String.format("Unable to handle rpc %s on session %s", messageAsString, session);
110             Map<String, String> errorInfo = Maps.newHashMap();
111
112             NetconfDocumentedException.ErrorTag tag;
113             if (e instanceof IllegalArgumentException) {
114                 errorInfo.put(NetconfDocumentedException.ErrorTag.operation_not_supported.toString(), e.getMessage());
115                 tag = NetconfDocumentedException.ErrorTag.operation_not_supported;
116             } else {
117                 errorInfo.put(NetconfDocumentedException.ErrorTag.operation_failed.toString(), e.getMessage());
118                 tag = NetconfDocumentedException.ErrorTag.operation_failed;
119             }
120
121             throw new NetconfDocumentedException(errorMessage, e, NetconfDocumentedException.ErrorType.application,
122                     tag, NetconfDocumentedException.ErrorSeverity.error, errorInfo);
123         } catch (RuntimeException e) {
124             throw handleUnexpectedEx("Unexpected exception during netconf operation sort", e);
125         }
126
127         try {
128             return executeOperationWithHighestPriority(message, netconfOperationExecution, messageAsString);
129         } catch (RuntimeException e) {
130             throw handleUnexpectedEx("Unexpected exception during netconf operation execution", e);
131         }
132     }
133
134     @Override
135     public void close() throws Exception {
136         netconfOperationServiceSnapshot.close();
137     }
138
139     private NetconfDocumentedException handleUnexpectedEx(String s, Exception e) throws NetconfDocumentedException {
140         logger.error(s, e);
141
142         Map<String, String> info = Maps.newHashMap();
143         info.put(NetconfDocumentedException.ErrorSeverity.error.toString(), e.toString());
144         return new NetconfDocumentedException("Unexpected error",
145                 NetconfDocumentedException.ErrorType.application,
146                 NetconfDocumentedException.ErrorTag.operation_failed,
147                 NetconfDocumentedException.ErrorSeverity.error, info);
148     }
149
150     private Document executeOperationWithHighestPriority(Document message,
151             NetconfOperationExecution netconfOperationExecution, String messageAsString)
152             throws NetconfDocumentedException {
153         logger.debug("Forwarding netconf message {} to {}", messageAsString, netconfOperationExecution.netconfOperation);
154         return netconfOperationExecution.execute(message);
155     }
156
157     private NetconfOperationExecution getNetconfOperationWithHighestPriority(
158             Document message, NetconfServerSession session) throws NetconfDocumentedException {
159
160         NavigableMap<HandlingPriority, NetconfOperation> sortedByPriority = getSortedNetconfOperationsWithCanHandle(
161                 message, session);
162
163         Preconditions.checkArgument(sortedByPriority.isEmpty() == false,
164                 "No %s available to handle message %s", NetconfOperation.class.getName(),
165                 XmlUtil.toString(message));
166
167         return NetconfOperationExecution.createExecutionChain(sortedByPriority, sortedByPriority.lastKey());
168     }
169
170     private TreeMap<HandlingPriority, NetconfOperation> getSortedNetconfOperationsWithCanHandle(Document message,
171             NetconfServerSession session) throws NetconfDocumentedException {
172         TreeMap<HandlingPriority, NetconfOperation> sortedPriority = Maps.newTreeMap();
173
174         for (NetconfOperation netconfOperation : allNetconfOperations) {
175             final HandlingPriority handlingPriority = netconfOperation.canHandle(message);
176             if (netconfOperation instanceof DefaultNetconfOperation) {
177                 ((DefaultNetconfOperation) netconfOperation).setNetconfSession(session);
178             }
179             if (!handlingPriority.equals(HandlingPriority.CANNOT_HANDLE)) {
180
181                 Preconditions.checkState(!sortedPriority.containsKey(handlingPriority),
182                         "Multiple %s available to handle message %s with priority %s",
183                         NetconfOperation.class.getName(), message, handlingPriority);
184                 sortedPriority.put(handlingPriority, netconfOperation);
185             }
186         }
187         return sortedPriority;
188     }
189
190     public static final NetconfOperationChainedExecution EXECUTION_TERMINATION_POINT = new NetconfOperationChainedExecution() {
191         @Override
192         public boolean isExecutionTermination() {
193             return true;
194         }
195
196         @Override
197         public Document execute(Document requestMessage) throws NetconfDocumentedException {
198             throw new NetconfDocumentedException("This execution represents the termination point in operation execution and cannot be executed itself",
199                     NetconfDocumentedException.ErrorType.application,
200                     NetconfDocumentedException.ErrorTag.operation_failed,
201                     NetconfDocumentedException.ErrorSeverity.error);
202         }
203     };
204
205     private static class NetconfOperationExecution implements NetconfOperationChainedExecution {
206         private final NetconfOperation netconfOperation;
207         private NetconfOperationChainedExecution subsequentExecution;
208
209         private NetconfOperationExecution(NetconfOperation netconfOperation, NetconfOperationChainedExecution subsequentExecution) {
210             this.netconfOperation = netconfOperation;
211             this.subsequentExecution = subsequentExecution;
212         }
213
214         @Override
215         public boolean isExecutionTermination() {
216             return false;
217         }
218
219         @Override
220         public Document execute(Document message) throws NetconfDocumentedException {
221             return netconfOperation.handle(message, subsequentExecution);
222         }
223
224         public static NetconfOperationExecution createExecutionChain(
225                 NavigableMap<HandlingPriority, NetconfOperation> sortedByPriority, HandlingPriority handlingPriority) {
226             NetconfOperation netconfOperation = sortedByPriority.get(handlingPriority);
227             HandlingPriority subsequentHandlingPriority = sortedByPriority.lowerKey(handlingPriority);
228
229             NetconfOperationChainedExecution subsequentExecution = null;
230
231             if (subsequentHandlingPriority != null) {
232                 subsequentExecution = createExecutionChain(sortedByPriority, subsequentHandlingPriority);
233             } else {
234                 subsequentExecution = EXECUTION_TERMINATION_POINT;
235             }
236
237             return new NetconfOperationExecution(netconfOperation, subsequentExecution);
238         }
239     }
240
241     @Override
242     public String toString() {
243         return "NetconfOperationRouterImpl{" + "netconfOperationServiceSnapshot=" + netconfOperationServiceSnapshot
244                 + '}';
245     }
246 }