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