2 * Copyright (c) 2015, 2017 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.config.facade.xml;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import java.util.HashMap;
14 import java.util.LinkedHashMap;
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;
39 public class RpcFacade {
41 public static final String CONTEXT_INSTANCE = "context-instance";
42 private final YangStoreService yangStoreService;
43 private final ConfigRegistryClient configRegistryClient;
45 public RpcFacade(final YangStoreService yangStoreService, final ConfigRegistryClient configRegistryClient) {
46 this.yangStoreService = yangStoreService;
47 this.configRegistryClient = configRegistryClient;
50 public Rpcs mapRpcs() {
52 final Map<String, Map<String, ModuleRpcs>> map = new HashMap<>();
54 for (final Map.Entry<String, Map<String, ModuleMXBeanEntry>> namespaceToModuleEntry : yangStoreService
55 .getModuleMXBeanEntryMap().entrySet()) {
57 Map<String, ModuleRpcs> namespaceToModules = map.computeIfAbsent(namespaceToModuleEntry.getKey(),
58 k -> new HashMap<>());
60 for (final Map.Entry<String, ModuleMXBeanEntry> moduleEntry : namespaceToModuleEntry.getValue()
63 ModuleRpcs rpcMapping = namespaceToModules.computeIfAbsent(moduleEntry.getKey(),
64 k -> new ModuleRpcs(yangStoreService.getEnumResolver()));
66 final ModuleMXBeanEntry entry = moduleEntry.getValue();
68 for (final RuntimeBeanEntry runtimeEntry : entry.getRuntimeBeans()) {
69 rpcMapping.addNameMapping(runtimeEntry);
70 for (final RuntimeBeanEntry.Rpc rpc : runtimeEntry.getRpcs()) {
71 rpcMapping.addRpc(runtimeEntry, rpc);
80 public OperationExecution fromXml(final XmlElement xml) throws DocumentedException {
81 final String namespace;
82 namespace = xml.getNamespace();
84 final XmlElement contextInstanceElement = xml.getOnlyChildElement(CONTEXT_INSTANCE);
85 final String operationName = xml.getName();
87 final RuntimeRpcElementResolved id = RuntimeRpcElementResolved
88 .fromXpath(contextInstanceElement.getTextContent(), operationName, namespace);
90 final Rpcs rpcs = mapRpcs();
92 final ModuleRpcs rpcMapping = rpcs.getRpcMapping(id);
93 final InstanceRuntimeRpc instanceRuntimeRpc = rpcMapping.getRpc(id.getRuntimeBeanName(), operationName);
95 // TODO move to Rpcs after xpath attribute is redesigned
97 final ObjectName on = id.getObjectName(rpcMapping);
98 Map<String, AttributeConfigElement> attributes = instanceRuntimeRpc.fromXml(xml);
99 attributes = sortAttributes(attributes, xml);
101 return new OperationExecution(on, instanceRuntimeRpc.getName(), attributes, instanceRuntimeRpc.getReturnType(),
105 private Map<String, AttributeConfigElement> sortAttributes(final Map<String, AttributeConfigElement> attributes,
106 final XmlElement xml) {
107 final Map<String, AttributeConfigElement> sorted = new LinkedHashMap<>();
109 for (XmlElement xmlElement : xml.getChildElements()) {
110 final String name = xmlElement.getName();
111 if (!CONTEXT_INSTANCE.equals(name)) { // skip context
112 // instance child node
116 final AttributeConfigElement value = attributes.get(name);
118 throw new IllegalArgumentException("Cannot find yang mapping for node " + xmlElement);
120 sorted.put(name, value);
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()];
132 for (final AttributeConfigElement attribute : execution.attributes.values()) {
133 final Optional<?> resolvedValueOpt = attribute.getResolvedValue();
135 params[index] = resolvedValueOpt.isPresent() ? resolvedValueOpt.get() : attribute.getResolvedDefaultValue();
136 signature[index] = resolvedValueOpt.isPresent() ? resolvedValueOpt.get().getClass().getName()
137 : attribute.getResolvedDefaultValue().getClass().getName();
141 return configRegistryClient.invokeMethod(execution.on, execution.operationName, params, signature);
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());
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
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());
165 XmlElement xmlElement = XmlElement.fromDomElement(tempParent);
166 return xmlElement.getChildElements().size() > 1 ? tempParent : xmlElement.getOnlyChildElement().getDomElement();
169 public class OperationExecution {
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;
177 public OperationExecution(final ObjectName on, final String name,
178 final Map<String, AttributeConfigElement> attributes, final AttributeIfc returnType,
179 final String namespace) {
181 this.operationName = name;
182 this.attributes = attributes;
183 this.returnType = returnType;
184 this.namespace = namespace;
187 public boolean isVoid() {
188 return returnType == VoidAttribute.getInstance();
191 public ObjectName getOn() {
195 public String getOperationName() {
196 return operationName;
199 public Map<String, AttributeConfigElement> getAttributes() {
203 public AttributeIfc getReturnType() {
207 public String getNamespace() {