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