Add netconf-impl unit tests
[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 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.netconf.api.monitoring.NetconfMonitoringService;
22 import org.opendaylight.netconf.impl.NetconfServerSession;
23 import org.opendaylight.netconf.impl.mapping.operations.DefaultCloseSession;
24 import org.opendaylight.netconf.impl.mapping.operations.DefaultNetconfOperation;
25 import org.opendaylight.netconf.impl.mapping.operations.DefaultStartExi;
26 import org.opendaylight.netconf.impl.mapping.operations.DefaultStopExi;
27 import org.opendaylight.netconf.mapping.api.HandlingPriority;
28 import org.opendaylight.netconf.mapping.api.NetconfOperation;
29 import org.opendaylight.netconf.mapping.api.NetconfOperationChainedExecution;
30 import org.opendaylight.netconf.mapping.api.NetconfOperationService;
31 import org.opendaylight.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 (final RuntimeException e) {
80             throw handleUnexpectedEx("Unexpected exception during netconf operation sort", e);
81         }
82
83         try {
84             return executeOperationWithHighestPriority(message, netconfOperationExecution);
85         } catch (final 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         final 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         final TreeMap<HandlingPriority, NetconfOperation> sortedPriority = Maps.newTreeMap();
131
132         for (final 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     private static class NetconfOperationExecution implements NetconfOperationChainedExecution {
152         private final NetconfOperation netconfOperation;
153         private final NetconfOperationChainedExecution subsequentExecution;
154
155         private NetconfOperationExecution(final NetconfOperation netconfOperation, final NetconfOperationChainedExecution subsequentExecution) {
156             this.netconfOperation = netconfOperation;
157             this.subsequentExecution = subsequentExecution;
158         }
159
160         @Override
161         public boolean isExecutionTermination() {
162             return false;
163         }
164
165         @Override
166         public Document execute(final Document message) throws DocumentedException {
167             return netconfOperation.handle(message, subsequentExecution);
168         }
169
170         public static NetconfOperationExecution createExecutionChain(
171                 final NavigableMap<HandlingPriority, NetconfOperation> sortedByPriority, final HandlingPriority handlingPriority) {
172             final NetconfOperation netconfOperation = sortedByPriority.get(handlingPriority);
173             final HandlingPriority subsequentHandlingPriority = sortedByPriority.lowerKey(handlingPriority);
174
175             NetconfOperationChainedExecution subsequentExecution = null;
176
177             if (subsequentHandlingPriority != null) {
178                 subsequentExecution = createExecutionChain(sortedByPriority, subsequentHandlingPriority);
179             } else {
180                 subsequentExecution = EXECUTION_TERMINATION_POINT;
181             }
182
183             return new NetconfOperationExecution(netconfOperation, subsequentExecution);
184         }
185     }
186
187     @Override
188     public String toString() {
189         return "NetconfOperationRouterImpl{" + "netconfOperationServiceSnapshot=" + netconfOperationServiceSnapshot
190                 + '}';
191     }
192 }