2 * Copyright (c) 2013 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.netconf.confignetconfconnector.operations.runtimerpc;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import com.google.common.collect.Maps;
14 import org.opendaylight.controller.config.util.ConfigRegistryClient;
15 import org.opendaylight.controller.config.yang.store.api.YangStoreSnapshot;
16 import org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry;
17 import org.opendaylight.controller.config.yangjmxgenerator.RuntimeBeanEntry;
18 import org.opendaylight.controller.config.yangjmxgenerator.RuntimeBeanEntry.Rpc;
19 import org.opendaylight.controller.config.yangjmxgenerator.attribute.AttributeIfc;
20 import org.opendaylight.controller.config.yangjmxgenerator.attribute.VoidAttribute;
21 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
22 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.fromxml.AttributeConfigElement;
23 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.mapping.AttributeMappingStrategy;
24 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.mapping.ObjectMapper;
25 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.toxml.ObjectXmlWriter;
26 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.rpc.InstanceRuntimeRpc;
27 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.rpc.ModuleRpcs;
28 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.rpc.Rpcs;
29 import org.opendaylight.controller.netconf.confignetconfconnector.operations.AbstractConfigNetconfOperation;
30 import org.opendaylight.controller.netconf.confignetconfconnector.operations.Commit;
31 import org.opendaylight.controller.netconf.mapping.api.HandlingPriority;
32 import org.opendaylight.controller.netconf.util.xml.XmlElement;
33 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.w3c.dom.Document;
37 import org.w3c.dom.Element;
39 import javax.management.ObjectName;
40 import javax.management.openmbean.OpenType;
43 public class RuntimeRpc extends AbstractConfigNetconfOperation {
45 private static final Logger logger = LoggerFactory.getLogger(Commit.class);
46 public static final String CONTEXT_INSTANCE = "context-instance";
48 private final YangStoreSnapshot yangStoreSnapshot;
50 public RuntimeRpc(final YangStoreSnapshot yangStoreSnapshot, ConfigRegistryClient configRegistryClient,
51 String netconfSessionIdForReporting) {
52 super(configRegistryClient, netconfSessionIdForReporting);
53 this.yangStoreSnapshot = yangStoreSnapshot;
56 private Element toXml(Document doc, Object result, AttributeIfc returnType, String namespace, String elementName) {
57 AttributeMappingStrategy<?, ? extends OpenType<?>> mappingStrategy = new ObjectMapper(null).prepareStrategy(returnType);
58 Optional<?> mappedAttributeOpt = mappingStrategy.mapAttribute(result);
59 Preconditions.checkState(mappedAttributeOpt.isPresent(), "Unable to map return value %s as %s", result, returnType.getOpenType());
61 // FIXME: multiple return values defined as leaf-list and list in yang should not be wrapped in output xml element,
62 // they need to be appended directly under rpc-reply element
64 // Either allow List of Elements to be returned from NetconfOperation or
65 // pass reference to parent output xml element for netconf operations to
66 // append result(s) on their own
67 Element tempParent = doc.createElementNS(XmlNetconfConstants.RFC4741_TARGET_NAMESPACE, "output");
68 new ObjectXmlWriter().prepareWritingStrategy(elementName, returnType, doc).writeElement(tempParent, namespace, mappedAttributeOpt.get());
70 XmlElement xmlElement = XmlElement.fromDomElement(tempParent);
71 return xmlElement.getChildElements().size() > 1 ? tempParent : xmlElement.getOnlyChildElement().getDomElement();
74 private Object executeOperation(final ConfigRegistryClient configRegistryClient, final ObjectName on,
75 final String name, final Map<String, AttributeConfigElement> attributes) {
76 final Object[] params = new Object[attributes.size()];
77 final String[] signature = new String[attributes.size()];
80 for (final String attrName : attributes.keySet()) {
81 final AttributeConfigElement attribute = attributes.get(attrName);
82 final Optional<?> resolvedValueOpt = attribute.getResolvedValue();
84 params[i] = resolvedValueOpt.isPresent() ? resolvedValueOpt.get() : attribute.getResolvedDefaultValue();
85 signature[i] = resolvedValueOpt.isPresent() ? resolvedValueOpt.get().getClass().getName() : attribute
86 .getResolvedDefaultValue().getClass().getName();
90 return configRegistryClient.invokeMethod(on, name, params, signature);
93 public NetconfOperationExecution fromXml(final XmlElement xml) throws NetconfDocumentedException {
94 final String namespace = xml.getNamespace();
95 final XmlElement contextInstanceElement = xml.getOnlyChildElement(CONTEXT_INSTANCE);
96 final String operationName = xml.getName();
98 final RuntimeRpcElementResolved id = RuntimeRpcElementResolved.fromXpath(
99 contextInstanceElement.getTextContent(), operationName, namespace);
101 final Rpcs rpcs = mapRpcs(yangStoreSnapshot.getModuleMXBeanEntryMap());
103 final ModuleRpcs rpcMapping = rpcs.getRpcMapping(id);
104 final InstanceRuntimeRpc instanceRuntimeRpc = rpcMapping.getRpc(id.getRuntimeBeanName(), operationName);
106 // TODO move to Rpcs after xpath attribute is redesigned
108 final ObjectName on = id.getObjectName(rpcMapping);
109 Map<String, AttributeConfigElement> attributes = instanceRuntimeRpc.fromXml(xml);
110 attributes = sortAttributes(attributes, xml);
112 return new NetconfOperationExecution(on, instanceRuntimeRpc.getName(), attributes,
113 instanceRuntimeRpc.getReturnType(), namespace);
117 public HandlingPriority canHandle(Document message) {
118 XmlElement requestElement = getRequestElementWithCheck(message);
120 XmlElement operationElement = requestElement.getOnlyChildElement();
121 final String netconfOperationName = operationElement.getName();
122 final String netconfOperationNamespace = operationElement.getNamespace();
124 final Optional<XmlElement> contextInstanceElement = operationElement
125 .getOnlyChildElementOptionally(CONTEXT_INSTANCE);
127 if (contextInstanceElement.isPresent() == false)
128 return HandlingPriority.CANNOT_HANDLE;
130 // FIXME update xpath to instance to conform to config-api yang
131 final RuntimeRpcElementResolved id = RuntimeRpcElementResolved.fromXpath(contextInstanceElement.get()
132 .getTextContent(), netconfOperationName, netconfOperationNamespace);
134 // TODO reuse rpcs instance in fromXml method
135 final Rpcs rpcs = mapRpcs(yangStoreSnapshot.getModuleMXBeanEntryMap());
139 final ModuleRpcs rpcMapping = rpcs.getRpcMapping(id);
140 final InstanceRuntimeRpc instanceRuntimeRpc = rpcMapping.getRpc(id.getRuntimeBeanName(),
141 netconfOperationName);
142 Preconditions.checkState(instanceRuntimeRpc != null, "No rpc found for %s:%s", netconfOperationNamespace,
143 netconfOperationName);
145 } catch (IllegalStateException e) {
146 logger.debug("Cannot handle runtime operation {}:{}", netconfOperationNamespace, netconfOperationName, e);
147 return HandlingPriority.CANNOT_HANDLE;
150 return HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY;
154 protected HandlingPriority canHandle(String netconfOperationName, String namespace) {
155 throw new UnsupportedOperationException(
156 "This should not be used since it is not possible to provide check with these attributes");
160 protected String getOperationName() {
161 throw new UnsupportedOperationException("Runtime rpc does not have a stable name");
165 protected Element handle(Document document, XmlElement xml) throws NetconfDocumentedException {
167 // TODO exception handling
168 // TODO check for namespaces and unknown elements
170 final NetconfOperationExecution execution = fromXml(xml);
172 logger.debug("Invoking operation {} on {} with arguments {}", execution.operationName, execution.on,
173 execution.attributes);
174 final Object result = executeOperation(configRegistryClient, execution.on, execution.operationName,
175 execution.attributes);
177 logger.info("Operation {} called successfully on {} with arguments {} with result {}", execution.operationName,
178 execution.on, execution.attributes, result);
180 if (execution.isVoid()) {
181 return document.createElement("ok");
183 return toXml(document, result, execution.returnType, execution.namespace,
184 execution.returnType.getAttributeYangName());
188 private static class NetconfOperationExecution {
190 private final ObjectName on;
191 private final String operationName;
192 private final Map<String, AttributeConfigElement> attributes;
193 private final AttributeIfc returnType;
194 private final String namespace;
196 public NetconfOperationExecution(final ObjectName on, final String name,
197 final Map<String, AttributeConfigElement> attributes, final AttributeIfc returnType, final String namespace) {
199 this.operationName = name;
200 this.attributes = attributes;
201 this.returnType = returnType;
202 this.namespace = namespace;
206 return returnType == VoidAttribute.getInstance();
211 private static Map<String, AttributeConfigElement> sortAttributes(
212 final Map<String, AttributeConfigElement> attributes, final XmlElement xml) {
213 final Map<String, AttributeConfigElement> sorted = Maps.newLinkedHashMap();
215 for (XmlElement xmlElement : xml.getChildElements()) {
216 final String name = xmlElement.getName();
217 if (CONTEXT_INSTANCE.equals(name) == false) { // skip context
218 // instance child node
222 final AttributeConfigElement value = attributes.get(name);
224 throw new IllegalArgumentException("Cannot find yang mapping for node " + xmlElement);
226 sorted.put(name, value);
233 private static Rpcs mapRpcs(final Map<String, Map<String, ModuleMXBeanEntry>> mBeanEntries) {
235 final Map<String, Map<String, ModuleRpcs>> map = Maps.newHashMap();
237 for (final String namespace : mBeanEntries.keySet()) {
239 Map<String, ModuleRpcs> namespaceToModules = map.get(namespace);
240 if (namespaceToModules == null) {
241 namespaceToModules = Maps.newHashMap();
242 map.put(namespace, namespaceToModules);
245 for (final String moduleName : mBeanEntries.get(namespace).keySet()) {
247 ModuleRpcs rpcMapping = namespaceToModules.get(moduleName);
248 if (rpcMapping == null) {
249 rpcMapping = new ModuleRpcs();
250 namespaceToModules.put(moduleName, rpcMapping);
253 final ModuleMXBeanEntry entry = mBeanEntries.get(namespace).get(moduleName);
255 for (final RuntimeBeanEntry runtimeEntry : entry.getRuntimeBeans()) {
256 rpcMapping.addNameMapping(runtimeEntry);
257 for (final Rpc rpc : runtimeEntry.getRpcs()) {
258 rpcMapping.addRpc(runtimeEntry, rpc);
264 return new Rpcs(map);