Merge "Fix bug 153: change the key container-config to ContainerConfig in the contain...
[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         // FIXME update xpath to instance to conform to config-api yang
131         final RuntimeRpcElementResolved id = RuntimeRpcElementResolved.fromXpath(contextInstanceElement.get()
132                 .getTextContent(), netconfOperationName, netconfOperationNamespace);
133
134         // TODO reuse rpcs instance in fromXml method
135         final Rpcs rpcs = mapRpcs(yangStoreSnapshot.getModuleMXBeanEntryMap());
136
137         try {
138
139             final ModuleRpcs rpcMapping = rpcs.getRpcMapping(id);
140             final InstanceRuntimeRpc instanceRuntimeRpc = rpcMapping.getRpc(id.getRuntimeBeanName(),
141                     netconfOperationName);
142             Preconditions.checkState(instanceRuntimeRpc != null, "No rpc found for %s:%s", netconfOperationNamespace,
143                     netconfOperationName);
144
145         } catch (IllegalStateException e) {
146             logger.debug("Cannot handle runtime operation {}:{}", netconfOperationNamespace, netconfOperationName, e);
147             return HandlingPriority.CANNOT_HANDLE;
148         }
149
150         return HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY;
151     }
152
153     @Override
154     protected HandlingPriority canHandle(String netconfOperationName, String namespace) {
155         throw new UnsupportedOperationException(
156                 "This should not be used since it is not possible to provide check with these attributes");
157     }
158
159     @Override
160     protected String getOperationName() {
161         throw new UnsupportedOperationException("Runtime rpc does not have a stable name");
162     }
163
164     @Override
165     protected Element handle(Document document, XmlElement xml) throws NetconfDocumentedException {
166
167         // TODO exception handling
168         // TODO check for namespaces and unknown elements
169
170         final NetconfOperationExecution execution = fromXml(xml);
171
172         logger.debug("Invoking operation {} on {} with arguments {}", execution.operationName, execution.on,
173                 execution.attributes);
174         final Object result = executeOperation(configRegistryClient, execution.on, execution.operationName,
175                 execution.attributes);
176
177         logger.info("Operation {} called successfully on {} with arguments {} with result {}", execution.operationName,
178                 execution.on, execution.attributes, result);
179
180         if (execution.isVoid()) {
181             return document.createElement("ok");
182         } else {
183             return toXml(document, result, execution.returnType, execution.namespace,
184                     execution.returnType.getAttributeYangName());
185         }
186     }
187
188     private static class NetconfOperationExecution {
189
190         private final ObjectName on;
191         private final String operationName;
192         private final Map<String, AttributeConfigElement> attributes;
193         private final AttributeIfc returnType;
194         private final String namespace;
195
196         public NetconfOperationExecution(final ObjectName on, final String name,
197                 final Map<String, AttributeConfigElement> attributes, final AttributeIfc returnType, final String namespace) {
198             this.on = on;
199             this.operationName = name;
200             this.attributes = attributes;
201             this.returnType = returnType;
202             this.namespace = namespace;
203         }
204
205         boolean isVoid() {
206             return returnType == VoidAttribute.getInstance();
207         }
208
209     }
210
211     private static Map<String, AttributeConfigElement> sortAttributes(
212             final Map<String, AttributeConfigElement> attributes, final XmlElement xml) {
213         final Map<String, AttributeConfigElement> sorted = Maps.newLinkedHashMap();
214
215         for (XmlElement xmlElement : xml.getChildElements()) {
216             final String name = xmlElement.getName();
217             if (CONTEXT_INSTANCE.equals(name) == false) { // skip context
218                                                           // instance child node
219                                                           // because it
220                                                           // specifies
221                 // ObjectName
222                 final AttributeConfigElement value = attributes.get(name);
223                 if (value == null) {
224                     throw new IllegalArgumentException("Cannot find yang mapping for node " + xmlElement);
225                 }
226                 sorted.put(name, value);
227             }
228         }
229
230         return sorted;
231     }
232
233     private static Rpcs mapRpcs(final Map<String, Map<String, ModuleMXBeanEntry>> mBeanEntries) {
234
235         final Map<String, Map<String, ModuleRpcs>> map = Maps.newHashMap();
236
237         for (final String namespace : mBeanEntries.keySet()) {
238
239             Map<String, ModuleRpcs> namespaceToModules = map.get(namespace);
240             if (namespaceToModules == null) {
241                 namespaceToModules = Maps.newHashMap();
242                 map.put(namespace, namespaceToModules);
243             }
244
245             for (final String moduleName : mBeanEntries.get(namespace).keySet()) {
246
247                 ModuleRpcs rpcMapping = namespaceToModules.get(moduleName);
248                 if (rpcMapping == null) {
249                     rpcMapping = new ModuleRpcs();
250                     namespaceToModules.put(moduleName, rpcMapping);
251                 }
252
253                 final ModuleMXBeanEntry entry = mBeanEntries.get(namespace).get(moduleName);
254
255                 for (final RuntimeBeanEntry runtimeEntry : entry.getRuntimeBeans()) {
256                     rpcMapping.addNameMapping(runtimeEntry);
257                     for (final Rpc rpc : runtimeEntry.getRpcs()) {
258                         rpcMapping.addRpc(runtimeEntry, rpc);
259                     }
260                 }
261             }
262         }
263
264         return new Rpcs(map);
265     }
266
267 }