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