Merge "(Bug 2035) - Increasing default Akka config for auto-down of unreachable nodes...
[controller.git] / opendaylight / netconf / config-netconf-connector / src / main / java / org / opendaylight / controller / netconf / confignetconfconnector / operations / runtimerpc / RuntimeRpc.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
9 package org.opendaylight.controller.netconf.confignetconfconnector.operations.runtimerpc;
10
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import com.google.common.collect.Maps;
14 import java.util.Map;
15 import javax.management.ObjectName;
16 import javax.management.openmbean.OpenType;
17 import org.opendaylight.controller.config.util.ConfigRegistryClient;
18 import org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry;
19 import org.opendaylight.controller.config.yangjmxgenerator.RuntimeBeanEntry;
20 import org.opendaylight.controller.config.yangjmxgenerator.RuntimeBeanEntry.Rpc;
21 import org.opendaylight.controller.config.yangjmxgenerator.attribute.AttributeIfc;
22 import org.opendaylight.controller.config.yangjmxgenerator.attribute.VoidAttribute;
23 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
24 import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
25 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.fromxml.AttributeConfigElement;
26 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.mapping.AttributeMappingStrategy;
27 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.mapping.ObjectMapper;
28 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.toxml.ObjectXmlWriter;
29 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.rpc.InstanceRuntimeRpc;
30 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.rpc.ModuleRpcs;
31 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.rpc.Rpcs;
32 import org.opendaylight.controller.netconf.confignetconfconnector.operations.AbstractConfigNetconfOperation;
33 import org.opendaylight.controller.netconf.confignetconfconnector.osgi.YangStoreContext;
34 import org.opendaylight.controller.netconf.mapping.api.HandlingPriority;
35 import org.opendaylight.controller.netconf.util.exception.MissingNameSpaceException;
36 import org.opendaylight.controller.netconf.util.xml.XmlElement;
37 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.w3c.dom.Document;
41 import org.w3c.dom.Element;
42
43 public class RuntimeRpc extends AbstractConfigNetconfOperation {
44
45     private static final Logger LOG = LoggerFactory.getLogger(RuntimeRpc.class);
46     public static final String CONTEXT_INSTANCE = "context-instance";
47
48     private final YangStoreContext yangStoreSnapshot;
49
50     public RuntimeRpc(final YangStoreContext yangStoreSnapshot, ConfigRegistryClient configRegistryClient,
51             String netconfSessionIdForReporting) {
52         super(configRegistryClient, netconfSessionIdForReporting);
53         this.yangStoreSnapshot = yangStoreSnapshot;
54     }
55
56     private Element toXml(Document doc, Object result, AttributeIfc returnType, String namespace, String elementName) throws NetconfDocumentedException {
57         AttributeMappingStrategy<?, ? extends OpenType<?>> mappingStrategy = new ObjectMapper().prepareStrategy(returnType);
58         Optional<?> mappedAttributeOpt = mappingStrategy.mapAttribute(result);
59         Preconditions.checkState(mappedAttributeOpt.isPresent(), "Unable to map return value %s as %s", result, returnType.getOpenType());
60
61         // FIXME: multiple return values defined as leaf-list and list in yang should not be wrapped in output xml element,
62         // they need to be appended directly under rpc-reply element
63         //
64         // Either allow List of Elements to be returned from NetconfOperation or
65         // pass reference to parent output xml element for netconf operations to
66         // append result(s) on their own
67         Element tempParent = XmlUtil.createElement(doc, "output", Optional.of(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0));
68         new ObjectXmlWriter().prepareWritingStrategy(elementName, returnType, doc).writeElement(tempParent, namespace, mappedAttributeOpt.get());
69
70         XmlElement xmlElement = XmlElement.fromDomElement(tempParent);
71         return xmlElement.getChildElements().size() > 1 ? tempParent : xmlElement.getOnlyChildElement().getDomElement();
72     }
73
74     private Object executeOperation(final ConfigRegistryClient configRegistryClient, final ObjectName on,
75             final String name, final Map<String, AttributeConfigElement> attributes) {
76         final Object[] params = new Object[attributes.size()];
77         final String[] signature = new String[attributes.size()];
78
79         int i = 0;
80         for (final AttributeConfigElement attribute : attributes.values()) {
81             final Optional<?> resolvedValueOpt = attribute.getResolvedValue();
82
83             params[i] = resolvedValueOpt.isPresent() ? resolvedValueOpt.get() : attribute.getResolvedDefaultValue();
84             signature[i] = resolvedValueOpt.isPresent() ? resolvedValueOpt.get().getClass().getName() : attribute
85                     .getResolvedDefaultValue().getClass().getName();
86             i++;
87         }
88
89         return configRegistryClient.invokeMethod(on, name, params, signature);
90     }
91
92     public NetconfOperationExecution fromXml(final XmlElement xml) throws NetconfDocumentedException {
93         final String namespace;
94         try {
95             namespace = xml.getNamespace();
96         } catch (MissingNameSpaceException e) {
97             LOG.trace("Can't get namespace from xml element due to ",e);
98             throw NetconfDocumentedException.wrap(e);
99         }
100         final XmlElement contextInstanceElement = xml.getOnlyChildElement(CONTEXT_INSTANCE);
101         final String operationName = xml.getName();
102
103         final RuntimeRpcElementResolved id = RuntimeRpcElementResolved.fromXpath(
104                 contextInstanceElement.getTextContent(), operationName, namespace);
105
106         final Rpcs rpcs = mapRpcs(yangStoreSnapshot.getModuleMXBeanEntryMap());
107
108         final ModuleRpcs rpcMapping = rpcs.getRpcMapping(id);
109         final InstanceRuntimeRpc instanceRuntimeRpc = rpcMapping.getRpc(id.getRuntimeBeanName(), operationName);
110
111         // TODO move to Rpcs after xpath attribute is redesigned
112
113         final ObjectName on = id.getObjectName(rpcMapping);
114         Map<String, AttributeConfigElement> attributes = instanceRuntimeRpc.fromXml(xml);
115         attributes = sortAttributes(attributes, xml);
116
117         return new NetconfOperationExecution(on, instanceRuntimeRpc.getName(), attributes,
118                 instanceRuntimeRpc.getReturnType(), namespace);
119     }
120
121     @Override
122     public HandlingPriority canHandle(Document message) throws NetconfDocumentedException {
123         XmlElement requestElement = null;
124         requestElement = getRequestElementWithCheck(message);
125
126         XmlElement operationElement = requestElement.getOnlyChildElement();
127         final String netconfOperationName = operationElement.getName();
128         final String netconfOperationNamespace;
129         try {
130             netconfOperationNamespace = operationElement.getNamespace();
131         } catch (MissingNameSpaceException e) {
132             LOG.debug("Cannot retrieve netconf operation namespace from message due to ", e);
133             return HandlingPriority.CANNOT_HANDLE;
134         }
135
136         final Optional<XmlElement> contextInstanceElement = operationElement
137                 .getOnlyChildElementOptionally(CONTEXT_INSTANCE);
138
139         if (!contextInstanceElement.isPresent()){
140             return HandlingPriority.CANNOT_HANDLE;
141         }
142
143         final RuntimeRpcElementResolved id = RuntimeRpcElementResolved.fromXpath(contextInstanceElement.get()
144                 .getTextContent(), netconfOperationName, netconfOperationNamespace);
145
146         // TODO reuse rpcs instance in fromXml method
147         final Rpcs rpcs = mapRpcs(yangStoreSnapshot.getModuleMXBeanEntryMap());
148
149         try {
150
151             final ModuleRpcs rpcMapping = rpcs.getRpcMapping(id);
152             final InstanceRuntimeRpc instanceRuntimeRpc = rpcMapping.getRpc(id.getRuntimeBeanName(),
153                     netconfOperationName);
154             Preconditions.checkState(instanceRuntimeRpc != null, "No rpc found for %s:%s", netconfOperationNamespace,
155                     netconfOperationName);
156
157         } catch (IllegalStateException e) {
158             LOG.debug("Cannot handle runtime operation {}:{}", netconfOperationNamespace, netconfOperationName, e);
159             return HandlingPriority.CANNOT_HANDLE;
160         }
161
162         return HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY;
163     }
164
165     @Override
166     protected HandlingPriority canHandle(String netconfOperationName, String namespace) {
167         throw new UnsupportedOperationException(
168                 "This should not be used since it is not possible to provide check with these attributes");
169     }
170
171     @Override
172     protected String getOperationName() {
173         throw new UnsupportedOperationException("Runtime rpc does not have a stable name");
174     }
175
176     @Override
177     protected Element handleWithNoSubsequentOperations(Document document, XmlElement xml) throws NetconfDocumentedException {
178         // TODO check for namespaces and unknown elements
179         final NetconfOperationExecution execution = fromXml(xml);
180
181         LOG.debug("Invoking operation {} on {} with arguments {}", execution.operationName, execution.on,
182                 execution.attributes);
183         final Object result = executeOperation(getConfigRegistryClient(), execution.on, execution.operationName,
184                 execution.attributes);
185
186         LOG.trace("Operation {} called successfully on {} with arguments {} with result {}", execution.operationName,
187                 execution.on, execution.attributes, result);
188
189         if (execution.isVoid()) {
190             return XmlUtil.createElement(document, XmlNetconfConstants.OK, Optional.<String>absent());
191         } else {
192             return toXml(document, result, execution.returnType, execution.namespace,
193                     execution.returnType.getAttributeYangName());
194         }
195     }
196
197     private static class NetconfOperationExecution {
198
199         private final ObjectName on;
200         private final String operationName;
201         private final Map<String, AttributeConfigElement> attributes;
202         private final AttributeIfc returnType;
203         private final String namespace;
204
205         public NetconfOperationExecution(final ObjectName on, final String name,
206                 final Map<String, AttributeConfigElement> attributes, final AttributeIfc returnType, final String namespace) {
207             this.on = on;
208             this.operationName = name;
209             this.attributes = attributes;
210             this.returnType = returnType;
211             this.namespace = namespace;
212         }
213
214         boolean isVoid() {
215             return returnType == VoidAttribute.getInstance();
216         }
217
218     }
219
220     private static Map<String, AttributeConfigElement> sortAttributes(
221             final Map<String, AttributeConfigElement> attributes, final XmlElement xml) {
222         final Map<String, AttributeConfigElement> sorted = Maps.newLinkedHashMap();
223
224         for (XmlElement xmlElement : xml.getChildElements()) {
225             final String name = xmlElement.getName();
226             if (!CONTEXT_INSTANCE.equals(name)) { // skip context
227                                                           // instance child node
228                                                           // because it
229                                                           // specifies
230                 // ObjectName
231                 final AttributeConfigElement value = attributes.get(name);
232                 if (value == null) {
233                     throw new IllegalArgumentException("Cannot find yang mapping for node " + xmlElement);
234                 }
235                 sorted.put(name, value);
236             }
237         }
238
239         return sorted;
240     }
241
242     private static Rpcs mapRpcs(final Map<String, Map<String, ModuleMXBeanEntry>> mBeanEntries) {
243
244         final Map<String, Map<String, ModuleRpcs>> map = Maps.newHashMap();
245
246         for (final Map.Entry<String, Map<String, ModuleMXBeanEntry>> namespaceToModuleEntry : mBeanEntries.entrySet()) {
247
248             Map<String, ModuleRpcs> namespaceToModules = map.get(namespaceToModuleEntry.getKey());
249             if (namespaceToModules == null) {
250                 namespaceToModules = Maps.newHashMap();
251                 map.put(namespaceToModuleEntry.getKey(), namespaceToModules);
252             }
253
254             for (final Map.Entry<String, ModuleMXBeanEntry> moduleEntry : namespaceToModuleEntry.getValue().entrySet()) {
255
256                 ModuleRpcs rpcMapping = namespaceToModules.get(moduleEntry.getKey());
257                 if (rpcMapping == null) {
258                     rpcMapping = new ModuleRpcs();
259                     namespaceToModules.put(moduleEntry.getKey(), rpcMapping);
260                 }
261
262                 final ModuleMXBeanEntry entry = moduleEntry.getValue();
263
264                 for (final RuntimeBeanEntry runtimeEntry : entry.getRuntimeBeans()) {
265                     rpcMapping.addNameMapping(runtimeEntry);
266                     for (final Rpc rpc : runtimeEntry.getRpcs()) {
267                         rpcMapping.addRpc(runtimeEntry, rpc);
268                     }
269                 }
270             }
271         }
272
273         return new Rpcs(map);
274     }
275
276 }