60c0e72e26e36080e8bc33816f6d8c111b4497c6
[controller.git] / opendaylight / config / config-manager-facade-xml / src / main / java / org / opendaylight / controller / config / facade / xml / RpcFacade.java
1 /*
2  * Copyright (c) 2015 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.config.facade.xml;
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.facade.xml.mapping.attributes.fromxml.AttributeConfigElement;
18 import org.opendaylight.controller.config.facade.xml.mapping.attributes.mapping.AttributeMappingStrategy;
19 import org.opendaylight.controller.config.facade.xml.mapping.attributes.mapping.ObjectMapper;
20 import org.opendaylight.controller.config.facade.xml.mapping.attributes.toxml.ObjectXmlWriter;
21 import org.opendaylight.controller.config.facade.xml.osgi.YangStoreService;
22 import org.opendaylight.controller.config.facade.xml.rpc.InstanceRuntimeRpc;
23 import org.opendaylight.controller.config.facade.xml.rpc.ModuleRpcs;
24 import org.opendaylight.controller.config.facade.xml.rpc.Rpcs;
25 import org.opendaylight.controller.config.facade.xml.rpc.RuntimeRpcElementResolved;
26 import org.opendaylight.controller.config.util.ConfigRegistryClient;
27 import org.opendaylight.controller.config.util.xml.DocumentedException;
28 import org.opendaylight.controller.config.util.xml.XmlElement;
29 import org.opendaylight.controller.config.util.xml.XmlMappingConstants;
30 import org.opendaylight.controller.config.util.xml.XmlUtil;
31 import org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry;
32 import org.opendaylight.controller.config.yangjmxgenerator.RuntimeBeanEntry;
33 import org.opendaylight.controller.config.yangjmxgenerator.attribute.AttributeIfc;
34 import org.opendaylight.controller.config.yangjmxgenerator.attribute.VoidAttribute;
35 import org.w3c.dom.Document;
36 import org.w3c.dom.Element;
37
38 public class RpcFacade {
39
40     public static final String CONTEXT_INSTANCE = "context-instance";
41     private YangStoreService yangStoreService;
42     private ConfigRegistryClient configRegistryClient;
43
44     public RpcFacade(final YangStoreService yangStoreService, final ConfigRegistryClient configRegistryClient) {
45         this.yangStoreService = yangStoreService;
46         this.configRegistryClient = configRegistryClient;
47     }
48
49     public Rpcs mapRpcs() {
50
51         final Map<String, Map<String, ModuleRpcs>> map = Maps.newHashMap();
52
53         for (final Map.Entry<String, Map<String, ModuleMXBeanEntry>> namespaceToModuleEntry : yangStoreService.getModuleMXBeanEntryMap().entrySet()) {
54
55             Map<String, ModuleRpcs> namespaceToModules = map.get(namespaceToModuleEntry.getKey());
56             if (namespaceToModules == null) {
57                 namespaceToModules = Maps.newHashMap();
58                 map.put(namespaceToModuleEntry.getKey(), namespaceToModules);
59             }
60
61             for (final Map.Entry<String, ModuleMXBeanEntry> moduleEntry : namespaceToModuleEntry.getValue().entrySet()) {
62
63                 ModuleRpcs rpcMapping = namespaceToModules.get(moduleEntry.getKey());
64                 if (rpcMapping == null) {
65                     rpcMapping = new ModuleRpcs(yangStoreService.getEnumResolver());
66                     namespaceToModules.put(moduleEntry.getKey(), rpcMapping);
67                 }
68
69                 final ModuleMXBeanEntry entry = moduleEntry.getValue();
70
71                 for (final RuntimeBeanEntry runtimeEntry : entry.getRuntimeBeans()) {
72                     rpcMapping.addNameMapping(runtimeEntry);
73                     for (final RuntimeBeanEntry.Rpc rpc : runtimeEntry.getRpcs()) {
74                         rpcMapping.addRpc(runtimeEntry, rpc);
75                     }
76                 }
77             }
78         }
79
80         return new Rpcs(map);
81     }
82
83
84     public OperationExecution fromXml(final XmlElement xml) throws DocumentedException {
85         final String namespace;
86         namespace = xml.getNamespace();
87
88         final XmlElement contextInstanceElement = xml.getOnlyChildElement(CONTEXT_INSTANCE);
89         final String operationName = xml.getName();
90
91         final RuntimeRpcElementResolved id = RuntimeRpcElementResolved.fromXpath(
92                 contextInstanceElement.getTextContent(), operationName, namespace);
93
94         final Rpcs rpcs = mapRpcs();
95
96         final ModuleRpcs rpcMapping = rpcs.getRpcMapping(id);
97         final InstanceRuntimeRpc instanceRuntimeRpc = rpcMapping.getRpc(id.getRuntimeBeanName(), operationName);
98
99         // TODO move to Rpcs after xpath attribute is redesigned
100
101         final ObjectName on = id.getObjectName(rpcMapping);
102         Map<String, AttributeConfigElement> attributes = instanceRuntimeRpc.fromXml(xml);
103         attributes = sortAttributes(attributes, xml);
104
105         return new OperationExecution(on, instanceRuntimeRpc.getName(), attributes,
106                 instanceRuntimeRpc.getReturnType(), namespace);
107     }
108
109     private Map<String, AttributeConfigElement> sortAttributes(
110             final Map<String, AttributeConfigElement> attributes, final XmlElement xml) {
111         final Map<String, AttributeConfigElement> sorted = Maps.newLinkedHashMap();
112
113         for (XmlElement xmlElement : xml.getChildElements()) {
114             final String name = xmlElement.getName();
115             if (!CONTEXT_INSTANCE.equals(name)) { // skip context
116                 // instance child node
117                 // because it
118                 // specifies
119                 // ObjectName
120                 final AttributeConfigElement value = attributes.get(name);
121                 if (value == null) {
122                     throw new IllegalArgumentException("Cannot find yang mapping for node " + xmlElement);
123                 }
124                 sorted.put(name, value);
125             }
126         }
127
128         return sorted;
129     }
130
131     public Object executeOperation(final OperationExecution execution) {
132         final Object[] params = new Object[execution.attributes.size()];
133         final String[] signature = new String[execution.attributes.size()];
134
135         int i = 0;
136         for (final AttributeConfigElement attribute : execution.attributes.values()) {
137             final Optional<?> resolvedValueOpt = attribute.getResolvedValue();
138
139             params[i] = resolvedValueOpt.isPresent() ? resolvedValueOpt.get() : attribute.getResolvedDefaultValue();
140             signature[i] = resolvedValueOpt.isPresent() ? resolvedValueOpt.get().getClass().getName() : attribute
141                     .getResolvedDefaultValue().getClass().getName();
142             i++;
143         }
144
145         return configRegistryClient.invokeMethod(execution.on, execution.operationName, params, signature);
146     }
147
148     public Element toXml(Document doc, Object result, OperationExecution execution) throws DocumentedException {
149         AttributeMappingStrategy<?, ? extends OpenType<?>> mappingStrategy = new ObjectMapper().prepareStrategy(execution.getReturnType());
150         Optional<?> mappedAttributeOpt = mappingStrategy.mapAttribute(result);
151         Preconditions.checkState(mappedAttributeOpt.isPresent(), "Unable to map return value %s as %s", result, execution.getReturnType().getOpenType());
152
153         // FIXME: multiple return values defined as leaf-list and list in yang should not be wrapped in output xml element,
154         // they need to be appended directly under rpc-reply element
155         //
156         // Either allow List of Elements to be returned from NetconfOperation or
157         // pass reference to parent output xml element for netconf operations to
158         // append result(s) on their own
159         Element tempParent = XmlUtil.createElement(doc, "output", Optional.of(XmlMappingConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0));
160         new ObjectXmlWriter().prepareWritingStrategy(execution.getReturnType().getAttributeYangName(),
161                 execution.getReturnType(), doc).writeElement(tempParent, execution.getNamespace(), mappedAttributeOpt.get());
162
163         XmlElement xmlElement = XmlElement.fromDomElement(tempParent);
164         return xmlElement.getChildElements().size() > 1 ? tempParent : xmlElement.getOnlyChildElement().getDomElement();
165     }
166
167     public class OperationExecution {
168
169         private final ObjectName on;
170         private final String operationName;
171         private final Map<String, AttributeConfigElement> attributes;
172         private final AttributeIfc returnType;
173         private final String namespace;
174
175         public OperationExecution(final ObjectName on, final String name,
176             final Map<String, AttributeConfigElement> attributes, final AttributeIfc returnType, final String namespace) {
177             this.on = on;
178             this.operationName = name;
179             this.attributes = attributes;
180             this.returnType = returnType;
181             this.namespace = namespace;
182         }
183
184         public boolean isVoid() {
185             return returnType == VoidAttribute.getInstance();
186         }
187
188         public ObjectName getOn() {
189             return on;
190         }
191
192         public String getOperationName() {
193             return operationName;
194         }
195
196         public Map<String, AttributeConfigElement> getAttributes() {
197             return attributes;
198         }
199
200         public AttributeIfc getReturnType() {
201             return returnType;
202         }
203
204         public String getNamespace() {
205             return namespace;
206         }
207     }
208
209 }