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