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