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