Added support for list, leaf-list and container in output under rpc node in yang...
[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 org.opendaylight.controller.config.util.ConfigRegistryClient;
15 import org.opendaylight.controller.config.yang.store.api.YangStoreSnapshot;
16 import org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry;
17 import org.opendaylight.controller.config.yangjmxgenerator.RuntimeBeanEntry;
18 import org.opendaylight.controller.config.yangjmxgenerator.RuntimeBeanEntry.Rpc;
19 import org.opendaylight.controller.config.yangjmxgenerator.attribute.AttributeIfc;
20 import org.opendaylight.controller.config.yangjmxgenerator.attribute.VoidAttribute;
21 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
22 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.fromxml.AttributeConfigElement;
23 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.mapping.AttributeMappingStrategy;
24 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.mapping.ObjectMapper;
25 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.toxml.ObjectXmlWriter;
26 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.rpc.InstanceRuntimeRpc;
27 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.rpc.ModuleRpcs;
28 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.rpc.Rpcs;
29 import org.opendaylight.controller.netconf.confignetconfconnector.operations.AbstractConfigNetconfOperation;
30 import org.opendaylight.controller.netconf.confignetconfconnector.operations.Commit;
31 import org.opendaylight.controller.netconf.mapping.api.HandlingPriority;
32 import org.opendaylight.controller.netconf.util.xml.XmlElement;
33 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.w3c.dom.Document;
37 import org.w3c.dom.Element;
38
39 import javax.management.ObjectName;
40 import javax.management.openmbean.OpenType;
41 import java.util.Map;
42
43 public class RuntimeRpc extends AbstractConfigNetconfOperation {
44
45     private static final Logger logger = LoggerFactory.getLogger(Commit.class);
46     public static final String CONTEXT_INSTANCE = "context-instance";
47
48     private final YangStoreSnapshot yangStoreSnapshot;
49
50     public RuntimeRpc(final YangStoreSnapshot 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) {
57         AttributeMappingStrategy<?, ? extends OpenType<?>> mappingStrategy = new ObjectMapper(null).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 = doc.createElementNS(XmlNetconfConstants.RFC4741_TARGET_NAMESPACE, "output");
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 String attrName : attributes.keySet()) {
81             final AttributeConfigElement attribute = attributes.get(attrName);
82             final Optional<?> resolvedValueOpt = attribute.getResolvedValue();
83
84             params[i] = resolvedValueOpt.isPresent() ? resolvedValueOpt.get() : attribute.getResolvedDefaultValue();
85             signature[i] = resolvedValueOpt.isPresent() ? resolvedValueOpt.get().getClass().getName() : attribute
86                     .getResolvedDefaultValue().getClass().getName();
87             i++;
88         }
89
90         return configRegistryClient.invokeMethod(on, name, params, signature);
91     }
92
93     public NetconfOperationExecution fromXml(final XmlElement xml) throws NetconfDocumentedException {
94         final String namespace = xml.getNamespace();
95         final XmlElement contextInstanceElement = xml.getOnlyChildElement(CONTEXT_INSTANCE);
96         final String operationName = xml.getName();
97
98         final RuntimeRpcElementResolved id = RuntimeRpcElementResolved.fromXpath(
99                 contextInstanceElement.getTextContent(), operationName, namespace);
100
101         final Rpcs rpcs = mapRpcs(yangStoreSnapshot.getModuleMXBeanEntryMap());
102
103         final ModuleRpcs rpcMapping = rpcs.getRpcMapping(id);
104         final InstanceRuntimeRpc instanceRuntimeRpc = rpcMapping.getRpc(id.getRuntimeBeanName(), operationName);
105
106         // TODO move to Rpcs after xpath attribute is redesigned
107
108         final ObjectName on = id.getObjectName(rpcMapping);
109         Map<String, AttributeConfigElement> attributes = instanceRuntimeRpc.fromXml(xml);
110         attributes = sortAttributes(attributes, xml);
111
112         return new NetconfOperationExecution(on, instanceRuntimeRpc.getName(), attributes,
113                 instanceRuntimeRpc.getReturnType(), namespace);
114     }
115
116     @Override
117     public HandlingPriority canHandle(Document message) {
118         XmlElement requestElement = getRequestElementWithCheck(message);
119
120         XmlElement operationElement = requestElement.getOnlyChildElement();
121         final String netconfOperationName = operationElement.getName();
122         final String netconfOperationNamespace = operationElement.getNamespace();
123
124         final Optional<XmlElement> contextInstanceElement = operationElement
125                 .getOnlyChildElementOptionally(CONTEXT_INSTANCE);
126
127         if (contextInstanceElement.isPresent() == false)
128             return HandlingPriority.CANNOT_HANDLE;
129
130         final RuntimeRpcElementResolved id = RuntimeRpcElementResolved.fromXpath(contextInstanceElement.get()
131                 .getTextContent(), netconfOperationName, netconfOperationNamespace);
132
133         // TODO reuse rpcs instance in fromXml method
134         final Rpcs rpcs = mapRpcs(yangStoreSnapshot.getModuleMXBeanEntryMap());
135
136         try {
137
138             final ModuleRpcs rpcMapping = rpcs.getRpcMapping(id);
139             final InstanceRuntimeRpc instanceRuntimeRpc = rpcMapping.getRpc(id.getRuntimeBeanName(),
140                     netconfOperationName);
141             Preconditions.checkState(instanceRuntimeRpc != null, "No rpc found for %s:%s", netconfOperationNamespace,
142                     netconfOperationName);
143
144         } catch (IllegalStateException e) {
145             logger.debug("Cannot handle runtime operation {}:{}", netconfOperationNamespace, netconfOperationName, e);
146             return HandlingPriority.CANNOT_HANDLE;
147         }
148
149         return HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY;
150     }
151
152     @Override
153     protected HandlingPriority canHandle(String netconfOperationName, String namespace) {
154         throw new UnsupportedOperationException(
155                 "This should not be used since it is not possible to provide check with these attributes");
156     }
157
158     @Override
159     protected String getOperationName() {
160         throw new UnsupportedOperationException("Runtime rpc does not have a stable name");
161     }
162
163     @Override
164     protected Element handle(Document document, XmlElement xml) throws NetconfDocumentedException {
165
166         // TODO exception handling
167         // TODO check for namespaces and unknown elements
168
169         final NetconfOperationExecution execution = fromXml(xml);
170
171         logger.debug("Invoking operation {} on {} with arguments {}", execution.operationName, execution.on,
172                 execution.attributes);
173         final Object result = executeOperation(configRegistryClient, execution.on, execution.operationName,
174                 execution.attributes);
175
176         logger.info("Operation {} called successfully on {} with arguments {} with result {}", execution.operationName,
177                 execution.on, execution.attributes, result);
178
179         if (execution.isVoid()) {
180             return document.createElement("ok");
181         } else {
182             return toXml(document, result, execution.returnType, execution.namespace,
183                     execution.returnType.getAttributeYangName());
184         }
185     }
186
187     private static class NetconfOperationExecution {
188
189         private final ObjectName on;
190         private final String operationName;
191         private final Map<String, AttributeConfigElement> attributes;
192         private final AttributeIfc returnType;
193         private final String namespace;
194
195         public NetconfOperationExecution(final ObjectName on, final String name,
196                 final Map<String, AttributeConfigElement> attributes, final AttributeIfc returnType, final String namespace) {
197             this.on = on;
198             this.operationName = name;
199             this.attributes = attributes;
200             this.returnType = returnType;
201             this.namespace = namespace;
202         }
203
204         boolean isVoid() {
205             return returnType == VoidAttribute.getInstance();
206         }
207
208     }
209
210     private static Map<String, AttributeConfigElement> sortAttributes(
211             final Map<String, AttributeConfigElement> attributes, final XmlElement xml) {
212         final Map<String, AttributeConfigElement> sorted = Maps.newLinkedHashMap();
213
214         for (XmlElement xmlElement : xml.getChildElements()) {
215             final String name = xmlElement.getName();
216             if (CONTEXT_INSTANCE.equals(name) == false) { // skip context
217                                                           // instance child node
218                                                           // because it
219                                                           // specifies
220                 // ObjectName
221                 final AttributeConfigElement value = attributes.get(name);
222                 if (value == null) {
223                     throw new IllegalArgumentException("Cannot find yang mapping for node " + xmlElement);
224                 }
225                 sorted.put(name, value);
226             }
227         }
228
229         return sorted;
230     }
231
232     private static Rpcs mapRpcs(final Map<String, Map<String, ModuleMXBeanEntry>> mBeanEntries) {
233
234         final Map<String, Map<String, ModuleRpcs>> map = Maps.newHashMap();
235
236         for (final String namespace : mBeanEntries.keySet()) {
237
238             Map<String, ModuleRpcs> namespaceToModules = map.get(namespace);
239             if (namespaceToModules == null) {
240                 namespaceToModules = Maps.newHashMap();
241                 map.put(namespace, namespaceToModules);
242             }
243
244             for (final String moduleName : mBeanEntries.get(namespace).keySet()) {
245
246                 ModuleRpcs rpcMapping = namespaceToModules.get(moduleName);
247                 if (rpcMapping == null) {
248                     rpcMapping = new ModuleRpcs();
249                     namespaceToModules.put(moduleName, rpcMapping);
250                 }
251
252                 final ModuleMXBeanEntry entry = mBeanEntries.get(namespace).get(moduleName);
253
254                 for (final RuntimeBeanEntry runtimeEntry : entry.getRuntimeBeans()) {
255                     rpcMapping.addNameMapping(runtimeEntry);
256                     for (final Rpc rpc : runtimeEntry.getRpcs()) {
257                         rpcMapping.addRpc(runtimeEntry, rpc);
258                     }
259                 }
260             }
261         }
262
263         return new Rpcs(map);
264     }
265
266 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.