2 * Copyright (c) 2015 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 YangStoreService yangStoreService;
43 private 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.getModuleMXBeanEntryMap().entrySet()) {
56 Map<String, ModuleRpcs> namespaceToModules =
57 map.computeIfAbsent(namespaceToModuleEntry.getKey(), k -> new HashMap<>());
59 for (final Map.Entry<String, ModuleMXBeanEntry> moduleEntry : namespaceToModuleEntry.getValue().entrySet()) {
61 ModuleRpcs rpcMapping = namespaceToModules.computeIfAbsent(moduleEntry.getKey(),
62 k -> new ModuleRpcs(yangStoreService.getEnumResolver()));
64 final ModuleMXBeanEntry entry = moduleEntry.getValue();
66 for (final RuntimeBeanEntry runtimeEntry : entry.getRuntimeBeans()) {
67 rpcMapping.addNameMapping(runtimeEntry);
68 for (final RuntimeBeanEntry.Rpc rpc : runtimeEntry.getRpcs()) {
69 rpcMapping.addRpc(runtimeEntry, rpc);
79 public OperationExecution fromXml(final XmlElement xml) throws DocumentedException {
80 final String namespace;
81 namespace = xml.getNamespace();
83 final XmlElement contextInstanceElement = xml.getOnlyChildElement(CONTEXT_INSTANCE);
84 final String operationName = xml.getName();
86 final RuntimeRpcElementResolved id = RuntimeRpcElementResolved.fromXpath(
87 contextInstanceElement.getTextContent(), operationName, namespace);
89 final Rpcs rpcs = mapRpcs();
91 final ModuleRpcs rpcMapping = rpcs.getRpcMapping(id);
92 final InstanceRuntimeRpc instanceRuntimeRpc = rpcMapping.getRpc(id.getRuntimeBeanName(), operationName);
94 // TODO move to Rpcs after xpath attribute is redesigned
96 final ObjectName on = id.getObjectName(rpcMapping);
97 Map<String, AttributeConfigElement> attributes = instanceRuntimeRpc.fromXml(xml);
98 attributes = sortAttributes(attributes, xml);
100 return new OperationExecution(on, instanceRuntimeRpc.getName(), attributes,
101 instanceRuntimeRpc.getReturnType(), namespace);
104 private Map<String, AttributeConfigElement> sortAttributes(
105 final Map<String, AttributeConfigElement> attributes, final XmlElement xml) {
106 final Map<String, AttributeConfigElement> sorted = new LinkedHashMap<>();
108 for (XmlElement xmlElement : xml.getChildElements()) {
109 final String name = xmlElement.getName();
110 if (!CONTEXT_INSTANCE.equals(name)) { // skip context
111 // instance child node
115 final AttributeConfigElement value = attributes.get(name);
117 throw new IllegalArgumentException("Cannot find yang mapping for node " + xmlElement);
119 sorted.put(name, value);
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()];
131 for (final AttributeConfigElement attribute : execution.attributes.values()) {
132 final Optional<?> resolvedValueOpt = attribute.getResolvedValue();
134 params[i] = resolvedValueOpt.isPresent() ? resolvedValueOpt.get() : attribute.getResolvedDefaultValue();
135 signature[i] = resolvedValueOpt.isPresent() ? resolvedValueOpt.get().getClass().getName() : attribute
136 .getResolvedDefaultValue().getClass().getName();
140 return configRegistryClient.invokeMethod(execution.on, execution.operationName, params, signature);
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());
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
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());
158 XmlElement xmlElement = XmlElement.fromDomElement(tempParent);
159 return xmlElement.getChildElements().size() > 1 ? tempParent : xmlElement.getOnlyChildElement().getDomElement();
162 public class OperationExecution {
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;
170 public OperationExecution(final ObjectName on, final String name,
171 final Map<String, AttributeConfigElement> attributes, final AttributeIfc returnType, final String namespace) {
173 this.operationName = name;
174 this.attributes = attributes;
175 this.returnType = returnType;
176 this.namespace = namespace;
179 public boolean isVoid() {
180 return returnType == VoidAttribute.getInstance();
183 public ObjectName getOn() {
187 public String getOperationName() {
188 return operationName;
191 public Map<String, AttributeConfigElement> getAttributes() {
195 public AttributeIfc getReturnType() {
199 public String getNamespace() {