Remove yang-test
[controller.git] / opendaylight / config / config-manager-facade-xml / src / main / java / org / opendaylight / controller / config / facade / xml / RpcFacade.java
1 /*
2  * Copyright (c) 2015, 2017 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 final YangStoreService yangStoreService;
43     private final 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
55                 .getModuleMXBeanEntryMap().entrySet()) {
56
57             Map<String, ModuleRpcs> namespaceToModules = map.computeIfAbsent(namespaceToModuleEntry.getKey(),
58                 k -> new HashMap<>());
59
60             for (final Map.Entry<String, ModuleMXBeanEntry> moduleEntry : namespaceToModuleEntry.getValue()
61                     .entrySet()) {
62
63                 ModuleRpcs rpcMapping = namespaceToModules.computeIfAbsent(moduleEntry.getKey(),
64                     k -> new ModuleRpcs(yangStoreService.getEnumResolver()));
65
66                 final ModuleMXBeanEntry entry = moduleEntry.getValue();
67
68                 for (final RuntimeBeanEntry runtimeEntry : entry.getRuntimeBeans()) {
69                     rpcMapping.addNameMapping(runtimeEntry);
70                     for (final RuntimeBeanEntry.Rpc rpc : runtimeEntry.getRpcs()) {
71                         rpcMapping.addRpc(runtimeEntry, rpc);
72                     }
73                 }
74             }
75         }
76
77         return new Rpcs(map);
78     }
79
80     public OperationExecution fromXml(final XmlElement xml) throws DocumentedException {
81         final String namespace;
82         namespace = xml.getNamespace();
83
84         final XmlElement contextInstanceElement = xml.getOnlyChildElement(CONTEXT_INSTANCE);
85         final String operationName = xml.getName();
86
87         final RuntimeRpcElementResolved id = RuntimeRpcElementResolved
88                 .fromXpath(contextInstanceElement.getTextContent(), operationName, namespace);
89
90         final Rpcs rpcs = mapRpcs();
91
92         final ModuleRpcs rpcMapping = rpcs.getRpcMapping(id);
93         final InstanceRuntimeRpc instanceRuntimeRpc = rpcMapping.getRpc(id.getRuntimeBeanName(), operationName);
94
95         // TODO move to Rpcs after xpath attribute is redesigned
96
97         final ObjectName on = id.getObjectName(rpcMapping);
98         Map<String, AttributeConfigElement> attributes = instanceRuntimeRpc.fromXml(xml);
99         attributes = sortAttributes(attributes, xml);
100
101         return new OperationExecution(on, instanceRuntimeRpc.getName(), attributes, instanceRuntimeRpc.getReturnType(),
102                 namespace);
103     }
104
105     private Map<String, AttributeConfigElement> sortAttributes(final Map<String, AttributeConfigElement> attributes,
106             final XmlElement xml) {
107         final Map<String, AttributeConfigElement> sorted = new LinkedHashMap<>();
108
109         for (XmlElement xmlElement : xml.getChildElements()) {
110             final String name = xmlElement.getName();
111             if (!CONTEXT_INSTANCE.equals(name)) { // skip context
112                 // instance child node
113                 // because it
114                 // specifies
115                 // ObjectName
116                 final AttributeConfigElement value = attributes.get(name);
117                 if (value == null) {
118                     throw new IllegalArgumentException("Cannot find yang mapping for node " + xmlElement);
119                 }
120                 sorted.put(name, value);
121             }
122         }
123
124         return sorted;
125     }
126
127     public Object executeOperation(final OperationExecution execution) {
128         final Object[] params = new Object[execution.attributes.size()];
129         final String[] signature = new String[execution.attributes.size()];
130
131         int index = 0;
132         for (final AttributeConfigElement attribute : execution.attributes.values()) {
133             final Optional<?> resolvedValueOpt = attribute.getResolvedValue();
134
135             params[index] = resolvedValueOpt.isPresent() ? resolvedValueOpt.get() : attribute.getResolvedDefaultValue();
136             signature[index] = resolvedValueOpt.isPresent() ? resolvedValueOpt.get().getClass().getName()
137                     : attribute.getResolvedDefaultValue().getClass().getName();
138             index++;
139         }
140
141         return configRegistryClient.invokeMethod(execution.on, execution.operationName, params, signature);
142     }
143
144     public Element toXml(final Document doc, final Object result, final OperationExecution execution)
145             throws DocumentedException {
146         AttributeMappingStrategy<?, ? extends OpenType<?>> mappingStrategy = new ObjectMapper()
147                 .prepareStrategy(execution.getReturnType());
148         Optional<?> mappedAttributeOpt = mappingStrategy.mapAttribute(result);
149         Preconditions.checkState(mappedAttributeOpt.isPresent(), "Unable to map return value %s as %s", result,
150                 execution.getReturnType().getOpenType());
151
152         // FIXME: multiple return values defined as leaf-list and list in yang should
153         // 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",
160                 Optional.of(XmlMappingConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0));
161         new ObjectXmlWriter().prepareWritingStrategy(execution.getReturnType().getAttributeYangName(),
162                 execution.getReturnType(), doc)
163                 .writeElement(tempParent, execution.getNamespace(), mappedAttributeOpt.get());
164
165         XmlElement xmlElement = XmlElement.fromDomElement(tempParent);
166         return xmlElement.getChildElements().size() > 1 ? tempParent : xmlElement.getOnlyChildElement().getDomElement();
167     }
168
169     public class OperationExecution {
170
171         private final ObjectName on;
172         private final String operationName;
173         private final Map<String, AttributeConfigElement> attributes;
174         private final AttributeIfc returnType;
175         private final String namespace;
176
177         public OperationExecution(final ObjectName on, final String name,
178                 final Map<String, AttributeConfigElement> attributes, final AttributeIfc returnType,
179                 final String namespace) {
180             this.on = on;
181             this.operationName = name;
182             this.attributes = attributes;
183             this.returnType = returnType;
184             this.namespace = namespace;
185         }
186
187         public boolean isVoid() {
188             return returnType == VoidAttribute.getInstance();
189         }
190
191         public ObjectName getOn() {
192             return on;
193         }
194
195         public String getOperationName() {
196             return operationName;
197         }
198
199         public Map<String, AttributeConfigElement> getAttributes() {
200             return attributes;
201         }
202
203         public AttributeIfc getReturnType() {
204             return returnType;
205         }
206
207         public String getNamespace() {
208             return namespace;
209         }
210     }
211 }