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 com.google.common.collect.Maps;
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;
38 public class RpcFacade {
40 public static final String CONTEXT_INSTANCE = "context-instance";
41 private YangStoreService yangStoreService;
42 private ConfigRegistryClient configRegistryClient;
44 public RpcFacade(final YangStoreService yangStoreService, final ConfigRegistryClient configRegistryClient) {
45 this.yangStoreService = yangStoreService;
46 this.configRegistryClient = configRegistryClient;
49 public Rpcs mapRpcs() {
51 final Map<String, Map<String, ModuleRpcs>> map = Maps.newHashMap();
53 for (final Map.Entry<String, Map<String, ModuleMXBeanEntry>> namespaceToModuleEntry : yangStoreService.getModuleMXBeanEntryMap().entrySet()) {
55 Map<String, ModuleRpcs> namespaceToModules = map.get(namespaceToModuleEntry.getKey());
56 if (namespaceToModules == null) {
57 namespaceToModules = Maps.newHashMap();
58 map.put(namespaceToModuleEntry.getKey(), namespaceToModules);
61 for (final Map.Entry<String, ModuleMXBeanEntry> moduleEntry : namespaceToModuleEntry.getValue().entrySet()) {
63 ModuleRpcs rpcMapping = namespaceToModules.get(moduleEntry.getKey());
64 if (rpcMapping == null) {
65 rpcMapping = new ModuleRpcs(yangStoreService.getEnumResolver());
66 namespaceToModules.put(moduleEntry.getKey(), rpcMapping);
69 final ModuleMXBeanEntry entry = moduleEntry.getValue();
71 for (final RuntimeBeanEntry runtimeEntry : entry.getRuntimeBeans()) {
72 rpcMapping.addNameMapping(runtimeEntry);
73 for (final RuntimeBeanEntry.Rpc rpc : runtimeEntry.getRpcs()) {
74 rpcMapping.addRpc(runtimeEntry, rpc);
84 public OperationExecution fromXml(final XmlElement xml) throws DocumentedException {
85 final String namespace;
86 namespace = xml.getNamespace();
88 final XmlElement contextInstanceElement = xml.getOnlyChildElement(CONTEXT_INSTANCE);
89 final String operationName = xml.getName();
91 final RuntimeRpcElementResolved id = RuntimeRpcElementResolved.fromXpath(
92 contextInstanceElement.getTextContent(), operationName, namespace);
94 final Rpcs rpcs = mapRpcs();
96 final ModuleRpcs rpcMapping = rpcs.getRpcMapping(id);
97 final InstanceRuntimeRpc instanceRuntimeRpc = rpcMapping.getRpc(id.getRuntimeBeanName(), operationName);
99 // TODO move to Rpcs after xpath attribute is redesigned
101 final ObjectName on = id.getObjectName(rpcMapping);
102 Map<String, AttributeConfigElement> attributes = instanceRuntimeRpc.fromXml(xml);
103 attributes = sortAttributes(attributes, xml);
105 return new OperationExecution(on, instanceRuntimeRpc.getName(), attributes,
106 instanceRuntimeRpc.getReturnType(), namespace);
109 private Map<String, AttributeConfigElement> sortAttributes(
110 final Map<String, AttributeConfigElement> attributes, final XmlElement xml) {
111 final Map<String, AttributeConfigElement> sorted = Maps.newLinkedHashMap();
113 for (XmlElement xmlElement : xml.getChildElements()) {
114 final String name = xmlElement.getName();
115 if (!CONTEXT_INSTANCE.equals(name)) { // skip context
116 // instance child node
120 final AttributeConfigElement value = attributes.get(name);
122 throw new IllegalArgumentException("Cannot find yang mapping for node " + xmlElement);
124 sorted.put(name, value);
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()];
136 for (final AttributeConfigElement attribute : execution.attributes.values()) {
137 final Optional<?> resolvedValueOpt = attribute.getResolvedValue();
139 params[i] = resolvedValueOpt.isPresent() ? resolvedValueOpt.get() : attribute.getResolvedDefaultValue();
140 signature[i] = resolvedValueOpt.isPresent() ? resolvedValueOpt.get().getClass().getName() : attribute
141 .getResolvedDefaultValue().getClass().getName();
145 return configRegistryClient.invokeMethod(execution.on, execution.operationName, params, signature);
148 public Element toXml(final Document doc, final Object result, final 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());
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
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());
163 XmlElement xmlElement = XmlElement.fromDomElement(tempParent);
164 return xmlElement.getChildElements().size() > 1 ? tempParent : xmlElement.getOnlyChildElement().getDomElement();
167 public class OperationExecution {
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;
175 public OperationExecution(final ObjectName on, final String name,
176 final Map<String, AttributeConfigElement> attributes, final AttributeIfc returnType, final String namespace) {
178 this.operationName = name;
179 this.attributes = attributes;
180 this.returnType = returnType;
181 this.namespace = namespace;
184 public boolean isVoid() {
185 return returnType == VoidAttribute.getInstance();
188 public ObjectName getOn() {
192 public String getOperationName() {
193 return operationName;
196 public Map<String, AttributeConfigElement> getAttributes() {
200 public AttributeIfc getReturnType() {
204 public String getNamespace() {