544fe7a687fb24027147423ce5d4d0c1e7108e31
[controller.git] / opendaylight / config / config-manager-facade-xml / src / main / java / org / opendaylight / controller / config / facade / xml / RpcFacade.java
1 package org.opendaylight.controller.config.facade.xml;
2
3 import com.google.common.base.Optional;
4 import com.google.common.base.Preconditions;
5 import com.google.common.collect.Maps;
6 import java.util.Map;
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;
29
30 public class RpcFacade {
31
32     public static final String CONTEXT_INSTANCE = "context-instance";
33     private YangStoreService yangStoreService;
34     private ConfigRegistryClient configRegistryClient;
35
36     public RpcFacade(final YangStoreService yangStoreService, final ConfigRegistryClient configRegistryClient) {
37         this.yangStoreService = yangStoreService;
38         this.configRegistryClient = configRegistryClient;
39     }
40
41     public Rpcs mapRpcs() {
42
43         final Map<String, Map<String, ModuleRpcs>> map = Maps.newHashMap();
44
45         for (final Map.Entry<String, Map<String, ModuleMXBeanEntry>> namespaceToModuleEntry : yangStoreService.getModuleMXBeanEntryMap().entrySet()) {
46
47             Map<String, ModuleRpcs> namespaceToModules = map.get(namespaceToModuleEntry.getKey());
48             if (namespaceToModules == null) {
49                 namespaceToModules = Maps.newHashMap();
50                 map.put(namespaceToModuleEntry.getKey(), namespaceToModules);
51             }
52
53             for (final Map.Entry<String, ModuleMXBeanEntry> moduleEntry : namespaceToModuleEntry.getValue().entrySet()) {
54
55                 ModuleRpcs rpcMapping = namespaceToModules.get(moduleEntry.getKey());
56                 if (rpcMapping == null) {
57                     rpcMapping = new ModuleRpcs(yangStoreService.getEnumResolver());
58                     namespaceToModules.put(moduleEntry.getKey(), rpcMapping);
59                 }
60
61                 final ModuleMXBeanEntry entry = moduleEntry.getValue();
62
63                 for (final RuntimeBeanEntry runtimeEntry : entry.getRuntimeBeans()) {
64                     rpcMapping.addNameMapping(runtimeEntry);
65                     for (final RuntimeBeanEntry.Rpc rpc : runtimeEntry.getRpcs()) {
66                         rpcMapping.addRpc(runtimeEntry, rpc);
67                     }
68                 }
69             }
70         }
71
72         return new Rpcs(map);
73     }
74
75
76     public OperationExecution fromXml(final XmlElement xml) throws DocumentedException {
77         final String namespace;
78         namespace = xml.getNamespace();
79
80         final XmlElement contextInstanceElement = xml.getOnlyChildElement(CONTEXT_INSTANCE);
81         final String operationName = xml.getName();
82
83         final RuntimeRpcElementResolved id = RuntimeRpcElementResolved.fromXpath(
84                 contextInstanceElement.getTextContent(), operationName, namespace);
85
86         final Rpcs rpcs = mapRpcs();
87
88         final ModuleRpcs rpcMapping = rpcs.getRpcMapping(id);
89         final InstanceRuntimeRpc instanceRuntimeRpc = rpcMapping.getRpc(id.getRuntimeBeanName(), operationName);
90
91         // TODO move to Rpcs after xpath attribute is redesigned
92
93         final ObjectName on = id.getObjectName(rpcMapping);
94         Map<String, AttributeConfigElement> attributes = instanceRuntimeRpc.fromXml(xml);
95         attributes = sortAttributes(attributes, xml);
96
97         return new OperationExecution(on, instanceRuntimeRpc.getName(), attributes,
98                 instanceRuntimeRpc.getReturnType(), namespace);
99     }
100
101     private Map<String, AttributeConfigElement> sortAttributes(
102             final Map<String, AttributeConfigElement> attributes, final XmlElement xml) {
103         final Map<String, AttributeConfigElement> sorted = Maps.newLinkedHashMap();
104
105         for (XmlElement xmlElement : xml.getChildElements()) {
106             final String name = xmlElement.getName();
107             if (!CONTEXT_INSTANCE.equals(name)) { // skip context
108                 // instance child node
109                 // because it
110                 // specifies
111                 // ObjectName
112                 final AttributeConfigElement value = attributes.get(name);
113                 if (value == null) {
114                     throw new IllegalArgumentException("Cannot find yang mapping for node " + xmlElement);
115                 }
116                 sorted.put(name, value);
117             }
118         }
119
120         return sorted;
121     }
122
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()];
126
127         int i = 0;
128         for (final AttributeConfigElement attribute : execution.attributes.values()) {
129             final Optional<?> resolvedValueOpt = attribute.getResolvedValue();
130
131             params[i] = resolvedValueOpt.isPresent() ? resolvedValueOpt.get() : attribute.getResolvedDefaultValue();
132             signature[i] = resolvedValueOpt.isPresent() ? resolvedValueOpt.get().getClass().getName() : attribute
133                     .getResolvedDefaultValue().getClass().getName();
134             i++;
135         }
136
137         return configRegistryClient.invokeMethod(execution.on, execution.operationName, params, signature);
138     }
139
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());
144
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
147         //
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());
154
155         XmlElement xmlElement = XmlElement.fromDomElement(tempParent);
156         return xmlElement.getChildElements().size() > 1 ? tempParent : xmlElement.getOnlyChildElement().getDomElement();
157     }
158
159     public class OperationExecution {
160
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;
166
167         public OperationExecution(final ObjectName on, final String name,
168             final Map<String, AttributeConfigElement> attributes, final AttributeIfc returnType, final String namespace) {
169             this.on = on;
170             this.operationName = name;
171             this.attributes = attributes;
172             this.returnType = returnType;
173             this.namespace = namespace;
174         }
175
176         public boolean isVoid() {
177             return returnType == VoidAttribute.getInstance();
178         }
179
180         public ObjectName getOn() {
181             return on;
182         }
183
184         public String getOperationName() {
185             return operationName;
186         }
187
188         public Map<String, AttributeConfigElement> getAttributes() {
189             return attributes;
190         }
191
192         public AttributeIfc getReturnType() {
193             return returnType;
194         }
195
196         public String getNamespace() {
197             return namespace;
198         }
199     }
200
201 }