Remove netconf from commons/opendaylight pom
[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.config.util.xml.DocumentedException;
20 import org.opendaylight.controller.config.util.xml.XmlUtil;
21 import org.opendaylight.controller.netconf.api.monitoring.NetconfMonitoringService;
22 import org.opendaylight.controller.netconf.impl.NetconfServerSession;
23 import org.opendaylight.controller.netconf.impl.mapping.operations.DefaultCloseSession;
24 import org.opendaylight.controller.netconf.impl.mapping.operations.DefaultNetconfOperation;
25 import org.opendaylight.controller.netconf.impl.mapping.operations.DefaultStartExi;
26 import org.opendaylight.controller.netconf.impl.mapping.operations.DefaultStopExi;
27 import org.opendaylight.controller.netconf.mapping.api.HandlingPriority;
28 import org.opendaylight.controller.netconf.mapping.api.NetconfOperation;
29 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationChainedExecution;
30 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationService;
31 import org.opendaylight.controller.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     @Override
57     public Document onNetconfMessage(final Document message, final NetconfServerSession session) throws DocumentedException {
58         Preconditions.checkNotNull(allNetconfOperations, "Operation router was not initialized properly");
59
60         final NetconfOperationExecution netconfOperationExecution;
61         try {
62             netconfOperationExecution = getNetconfOperationWithHighestPriority(message, session);
63         } catch (IllegalArgumentException | IllegalStateException e) {
64             final String messageAsString = XmlUtil.toString(message);
65             LOG.warn("Unable to handle rpc {} on session {}", messageAsString, session, e);
66
67             final DocumentedException.ErrorTag tag;
68             if (e instanceof IllegalArgumentException) {
69                 tag = DocumentedException.ErrorTag.operation_not_supported;
70             } else {
71                 tag = DocumentedException.ErrorTag.operation_failed;
72             }
73
74             throw new DocumentedException(
75                 String.format("Unable to handle rpc %s on session %s", messageAsString, session),
76                 e, DocumentedException.ErrorType.application,
77                 tag, DocumentedException.ErrorSeverity.error,
78                 Collections.singletonMap(tag.toString(), e.getMessage()));
79         } catch (RuntimeException e) {
80             throw handleUnexpectedEx("Unexpected exception during netconf operation sort", e);
81         }
82
83         try {
84             return executeOperationWithHighestPriority(message, netconfOperationExecution);
85         } catch (RuntimeException e) {
86             throw handleUnexpectedEx("Unexpected exception during netconf operation execution", e);
87         }
88     }
89
90     @Override
91     public void close() throws Exception {
92         netconfOperationServiceSnapshot.close();
93     }
94
95     private static DocumentedException handleUnexpectedEx(final String s, final Exception e) throws DocumentedException {
96         LOG.error("{}", s, e);
97         return new DocumentedException("Unexpected error",
98                 DocumentedException.ErrorType.application,
99                 DocumentedException.ErrorTag.operation_failed,
100                 DocumentedException.ErrorSeverity.error,
101                 Collections.singletonMap(DocumentedException.ErrorSeverity.error.toString(), e.toString()));
102     }
103
104     private Document executeOperationWithHighestPriority(final Document message,
105             final NetconfOperationExecution netconfOperationExecution)
106             throws DocumentedException {
107         if (LOG.isDebugEnabled()) {
108             LOG.debug("Forwarding netconf message {} to {}", XmlUtil.toString(message), netconfOperationExecution.netconfOperation);
109         }
110
111         return netconfOperationExecution.execute(message);
112     }
113
114     private NetconfOperationExecution getNetconfOperationWithHighestPriority(
115             final Document message, final NetconfServerSession session) throws DocumentedException {
116
117         NavigableMap<HandlingPriority, NetconfOperation> sortedByPriority = getSortedNetconfOperationsWithCanHandle(
118                 message, session);
119
120         if (sortedByPriority.isEmpty()) {
121             throw new IllegalArgumentException(String.format("No %s available to handle message %s",
122                 NetconfOperation.class.getName(), XmlUtil.toString(message)));
123         }
124
125         return NetconfOperationExecution.createExecutionChain(sortedByPriority, sortedByPriority.lastKey());
126     }
127
128     private TreeMap<HandlingPriority, NetconfOperation> getSortedNetconfOperationsWithCanHandle(final Document message,
129             final NetconfServerSession session) throws DocumentedException {
130         TreeMap<HandlingPriority, NetconfOperation> sortedPriority = Maps.newTreeMap();
131
132         for (NetconfOperation netconfOperation : allNetconfOperations) {
133             final HandlingPriority handlingPriority = netconfOperation.canHandle(message);
134             if (netconfOperation instanceof DefaultNetconfOperation) {
135                 ((DefaultNetconfOperation) netconfOperation).setNetconfSession(session);
136             }
137             if(netconfOperation instanceof SessionAwareNetconfOperation) {
138                 ((SessionAwareNetconfOperation) netconfOperation).setSession(session);
139             }
140             if (!handlingPriority.equals(HandlingPriority.CANNOT_HANDLE)) {
141
142                 Preconditions.checkState(!sortedPriority.containsKey(handlingPriority),
143                         "Multiple %s available to handle message %s with priority %s, %s and %s",
144                         NetconfOperation.class.getName(), message, handlingPriority, netconfOperation, sortedPriority.get(handlingPriority));
145                 sortedPriority.put(handlingPriority, netconfOperation);
146             }
147         }
148         return sortedPriority;
149     }
150
151     public static final NetconfOperationChainedExecution EXECUTION_TERMINATION_POINT = new NetconfOperationChainedExecution() {
152         @Override
153         public boolean isExecutionTermination() {
154             return true;
155         }
156
157         @Override
158         public Document execute(final Document requestMessage) throws DocumentedException {
159             throw new DocumentedException("This execution represents the termination point in operation execution and cannot be executed itself",
160                     DocumentedException.ErrorType.application,
161                     DocumentedException.ErrorTag.operation_failed,
162                     DocumentedException.ErrorSeverity.error);
163         }
164     };
165
166     private static class NetconfOperationExecution implements NetconfOperationChainedExecution {
167         private final NetconfOperation netconfOperation;
168         private final NetconfOperationChainedExecution subsequentExecution;
169
170         private NetconfOperationExecution(final NetconfOperation netconfOperation, final NetconfOperationChainedExecution subsequentExecution) {
171             this.netconfOperation = netconfOperation;
172             this.subsequentExecution = subsequentExecution;
173         }
174
175         @Override
176         public boolean isExecutionTermination() {
177             return false;
178         }
179
180         @Override
181         public Document execute(final Document message) throws DocumentedException {
182             return netconfOperation.handle(message, subsequentExecution);
183         }
184
185         public static NetconfOperationExecution createExecutionChain(
186                 final NavigableMap<HandlingPriority, NetconfOperation> sortedByPriority, final HandlingPriority handlingPriority) {
187             NetconfOperation netconfOperation = sortedByPriority.get(handlingPriority);
188             HandlingPriority subsequentHandlingPriority = sortedByPriority.lowerKey(handlingPriority);
189
190             NetconfOperationChainedExecution subsequentExecution = null;
191
192             if (subsequentHandlingPriority != null) {
193                 subsequentExecution = createExecutionChain(sortedByPriority, subsequentHandlingPriority);
194             } else {
195                 subsequentExecution = EXECUTION_TERMINATION_POINT;
196             }
197
198             return new NetconfOperationExecution(netconfOperation, subsequentExecution);
199         }
200     }
201
202     @Override
203     public String toString() {
204         return "NetconfOperationRouterImpl{" + "netconfOperationServiceSnapshot=" + netconfOperationServiceSnapshot
205                 + '}';
206     }
207 }