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