1 package org.opendaylight.controller.config.facade.xml;
3 import com.google.common.base.Optional;
4 import com.google.common.base.Preconditions;
5 import com.google.common.collect.Maps;
7 import javax.management.ObjectName;
8 import javax.management.openmbean.OpenType;
9 import org.opendaylight.controller.config.facade.xml.mapping.attributes.fromxml.AttributeConfigElement;
10 import org.opendaylight.controller.config.facade.xml.mapping.attributes.mapping.AttributeMappingStrategy;
11 import org.opendaylight.controller.config.facade.xml.mapping.attributes.mapping.ObjectMapper;
12 import org.opendaylight.controller.config.facade.xml.mapping.attributes.toxml.ObjectXmlWriter;
13 import org.opendaylight.controller.config.facade.xml.osgi.YangStoreService;
14 import org.opendaylight.controller.config.facade.xml.rpc.InstanceRuntimeRpc;
15 import org.opendaylight.controller.config.facade.xml.rpc.ModuleRpcs;
16 import org.opendaylight.controller.config.facade.xml.rpc.Rpcs;
17 import org.opendaylight.controller.config.facade.xml.rpc.RuntimeRpcElementResolved;
18 import org.opendaylight.controller.config.util.ConfigRegistryClient;
19 import org.opendaylight.controller.config.util.xml.DocumentedException;
20 import org.opendaylight.controller.config.util.xml.XmlElement;
21 import org.opendaylight.controller.config.util.xml.XmlMappingConstants;
22 import org.opendaylight.controller.config.util.xml.XmlUtil;
23 import org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry;
24 import org.opendaylight.controller.config.yangjmxgenerator.RuntimeBeanEntry;
25 import org.opendaylight.controller.config.yangjmxgenerator.attribute.AttributeIfc;
26 import org.opendaylight.controller.config.yangjmxgenerator.attribute.VoidAttribute;
27 import org.w3c.dom.Document;
28 import org.w3c.dom.Element;
30 public class RpcFacade {
32 public static final String CONTEXT_INSTANCE = "context-instance";
33 private YangStoreService yangStoreService;
34 private ConfigRegistryClient configRegistryClient;
36 public RpcFacade(final YangStoreService yangStoreService, final ConfigRegistryClient configRegistryClient) {
37 this.yangStoreService = yangStoreService;
38 this.configRegistryClient = configRegistryClient;
41 public Rpcs mapRpcs() {
43 final Map<String, Map<String, ModuleRpcs>> map = Maps.newHashMap();
45 for (final Map.Entry<String, Map<String, ModuleMXBeanEntry>> namespaceToModuleEntry : yangStoreService.getModuleMXBeanEntryMap().entrySet()) {
47 Map<String, ModuleRpcs> namespaceToModules = map.get(namespaceToModuleEntry.getKey());
48 if (namespaceToModules == null) {
49 namespaceToModules = Maps.newHashMap();
50 map.put(namespaceToModuleEntry.getKey(), namespaceToModules);
53 for (final Map.Entry<String, ModuleMXBeanEntry> moduleEntry : namespaceToModuleEntry.getValue().entrySet()) {
55 ModuleRpcs rpcMapping = namespaceToModules.get(moduleEntry.getKey());
56 if (rpcMapping == null) {
57 rpcMapping = new ModuleRpcs(yangStoreService.getEnumResolver());
58 namespaceToModules.put(moduleEntry.getKey(), rpcMapping);
61 final ModuleMXBeanEntry entry = moduleEntry.getValue();
63 for (final RuntimeBeanEntry runtimeEntry : entry.getRuntimeBeans()) {
64 rpcMapping.addNameMapping(runtimeEntry);
65 for (final RuntimeBeanEntry.Rpc rpc : runtimeEntry.getRpcs()) {
66 rpcMapping.addRpc(runtimeEntry, rpc);
76 public OperationExecution fromXml(final XmlElement xml) throws DocumentedException {
77 final String namespace;
78 namespace = xml.getNamespace();
80 final XmlElement contextInstanceElement = xml.getOnlyChildElement(CONTEXT_INSTANCE);
81 final String operationName = xml.getName();
83 final RuntimeRpcElementResolved id = RuntimeRpcElementResolved.fromXpath(
84 contextInstanceElement.getTextContent(), operationName, namespace);
86 final Rpcs rpcs = mapRpcs();
88 final ModuleRpcs rpcMapping = rpcs.getRpcMapping(id);
89 final InstanceRuntimeRpc instanceRuntimeRpc = rpcMapping.getRpc(id.getRuntimeBeanName(), operationName);
91 // TODO move to Rpcs after xpath attribute is redesigned
93 final ObjectName on = id.getObjectName(rpcMapping);
94 Map<String, AttributeConfigElement> attributes = instanceRuntimeRpc.fromXml(xml);
95 attributes = sortAttributes(attributes, xml);
97 return new OperationExecution(on, instanceRuntimeRpc.getName(), attributes,
98 instanceRuntimeRpc.getReturnType(), namespace);
101 private Map<String, AttributeConfigElement> sortAttributes(
102 final Map<String, AttributeConfigElement> attributes, final XmlElement xml) {
103 final Map<String, AttributeConfigElement> sorted = Maps.newLinkedHashMap();
105 for (XmlElement xmlElement : xml.getChildElements()) {
106 final String name = xmlElement.getName();
107 if (!CONTEXT_INSTANCE.equals(name)) { // skip context
108 // instance child node
112 final AttributeConfigElement value = attributes.get(name);
114 throw new IllegalArgumentException("Cannot find yang mapping for node " + xmlElement);
116 sorted.put(name, value);
123 public Object executeOperation(final OperationExecution execution) {
124 final Object[] params = new Object[execution.attributes.size()];
125 final String[] signature = new String[execution.attributes.size()];
128 for (final AttributeConfigElement attribute : execution.attributes.values()) {
129 final Optional<?> resolvedValueOpt = attribute.getResolvedValue();
131 params[i] = resolvedValueOpt.isPresent() ? resolvedValueOpt.get() : attribute.getResolvedDefaultValue();
132 signature[i] = resolvedValueOpt.isPresent() ? resolvedValueOpt.get().getClass().getName() : attribute
133 .getResolvedDefaultValue().getClass().getName();
137 return configRegistryClient.invokeMethod(execution.on, execution.operationName, params, signature);
140 public Element toXml(Document doc, Object result, OperationExecution execution) throws DocumentedException {
141 AttributeMappingStrategy<?, ? extends OpenType<?>> mappingStrategy = new ObjectMapper().prepareStrategy(execution.getReturnType());
142 Optional<?> mappedAttributeOpt = mappingStrategy.mapAttribute(result);
143 Preconditions.checkState(mappedAttributeOpt.isPresent(), "Unable to map return value %s as %s", result, execution.getReturnType().getOpenType());
145 // FIXME: multiple return values defined as leaf-list and list in yang should not be wrapped in output xml element,
146 // they need to be appended directly under rpc-reply element
148 // Either allow List of Elements to be returned from NetconfOperation or
149 // pass reference to parent output xml element for netconf operations to
150 // append result(s) on their own
151 Element tempParent = XmlUtil.createElement(doc, "output", Optional.of(XmlMappingConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0));
152 new ObjectXmlWriter().prepareWritingStrategy(execution.getReturnType().getAttributeYangName(),
153 execution.getReturnType(), doc).writeElement(tempParent, execution.getNamespace(), mappedAttributeOpt.get());
155 XmlElement xmlElement = XmlElement.fromDomElement(tempParent);
156 return xmlElement.getChildElements().size() > 1 ? tempParent : xmlElement.getOnlyChildElement().getDomElement();
159 public class OperationExecution {
161 private final ObjectName on;
162 private final String operationName;
163 private final Map<String, AttributeConfigElement> attributes;
164 private final AttributeIfc returnType;
165 private final String namespace;
167 public OperationExecution(final ObjectName on, final String name,
168 final Map<String, AttributeConfigElement> attributes, final AttributeIfc returnType, final String namespace) {
170 this.operationName = name;
171 this.attributes = attributes;
172 this.returnType = returnType;
173 this.namespace = namespace;
176 public boolean isVoid() {
177 return returnType == VoidAttribute.getInstance();
180 public ObjectName getOn() {
184 public String getOperationName() {
185 return operationName;
188 public Map<String, AttributeConfigElement> getAttributes() {
192 public AttributeIfc getReturnType() {
196 public String getNamespace() {