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;
13 import javax.management.ObjectName;
14 import javax.management.openmbean.OpenType;
16 import org.opendaylight.controller.config.util.ConfigRegistryClient;
17 import org.opendaylight.controller.config.yang.store.api.YangStoreSnapshot;
18 import org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry;
19 import org.opendaylight.controller.config.yangjmxgenerator.RuntimeBeanEntry;
20 import org.opendaylight.controller.config.yangjmxgenerator.RuntimeBeanEntry.Rpc;
21 import org.opendaylight.controller.config.yangjmxgenerator.attribute.AttributeIfc;
22 import org.opendaylight.controller.config.yangjmxgenerator.attribute.VoidAttribute;
23 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
24 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.fromxml.AttributeConfigElement;
25 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.mapping.AttributeMappingStrategy;
26 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.mapping.ObjectMapper;
27 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.toxml.ObjectXmlWriter;
28 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.rpc.InstanceRuntimeRpc;
29 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.rpc.ModuleRpcs;
30 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.rpc.Rpcs;
31 import org.opendaylight.controller.netconf.confignetconfconnector.operations.AbstractConfigNetconfOperation;
32 import org.opendaylight.controller.netconf.confignetconfconnector.operations.Commit;
33 import org.opendaylight.controller.netconf.mapping.api.HandlingPriority;
34 import org.opendaylight.controller.netconf.util.xml.XmlElement;
35 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.w3c.dom.Document;
39 import org.w3c.dom.Element;
41 import com.google.common.base.Optional;
42 import com.google.common.base.Preconditions;
43 import com.google.common.collect.Maps;
45 public class RuntimeRpc extends AbstractConfigNetconfOperation {
47 private static final Logger logger = LoggerFactory.getLogger(Commit.class);
48 public static final String CONTEXT_INSTANCE = "context-instance";
50 private final YangStoreSnapshot yangStoreSnapshot;
52 public RuntimeRpc(final YangStoreSnapshot yangStoreSnapshot, ConfigRegistryClient configRegistryClient,
53 String netconfSessionIdForReporting) {
54 super(configRegistryClient, netconfSessionIdForReporting);
55 this.yangStoreSnapshot = yangStoreSnapshot;
58 private Element toXml(Document doc, Object result, AttributeIfc returnType, String namespace, String elementName) {
59 AttributeMappingStrategy<?, ? extends OpenType<?>> mappingStrategy = new ObjectMapper(null).prepareStrategy(returnType);
60 Optional<?> mappedAttributeOpt = mappingStrategy.mapAttribute(result);
61 Preconditions.checkState(mappedAttributeOpt.isPresent(), "Unable to map return value %s as %s", result, returnType.getOpenType());
63 // FIXME: multiple return values defined as leaf-list and list in yang should not be wrapped in output xml element,
64 // they need to be appended directly under rpc-reply element
66 // Either allow List of Elements to be returned from NetconfOperation or
67 // pass reference to parent output xml element for netconf operations to
68 // append result(s) on their own
69 Element tempParent = doc.createElementNS(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0, "output");
70 new ObjectXmlWriter().prepareWritingStrategy(elementName, returnType, doc).writeElement(tempParent, namespace, mappedAttributeOpt.get());
72 XmlElement xmlElement = XmlElement.fromDomElement(tempParent);
73 return xmlElement.getChildElements().size() > 1 ? tempParent : xmlElement.getOnlyChildElement().getDomElement();
76 private Object executeOperation(final ConfigRegistryClient configRegistryClient, final ObjectName on,
77 final String name, final Map<String, AttributeConfigElement> attributes) {
78 final Object[] params = new Object[attributes.size()];
79 final String[] signature = new String[attributes.size()];
82 for (final String attrName : attributes.keySet()) {
83 final AttributeConfigElement attribute = attributes.get(attrName);
84 final Optional<?> resolvedValueOpt = attribute.getResolvedValue();
86 params[i] = resolvedValueOpt.isPresent() ? resolvedValueOpt.get() : attribute.getResolvedDefaultValue();
87 signature[i] = resolvedValueOpt.isPresent() ? resolvedValueOpt.get().getClass().getName() : attribute
88 .getResolvedDefaultValue().getClass().getName();
92 return configRegistryClient.invokeMethod(on, name, params, signature);
95 public NetconfOperationExecution fromXml(final XmlElement xml) throws NetconfDocumentedException {
96 final String namespace = xml.getNamespace();
97 final XmlElement contextInstanceElement = xml.getOnlyChildElement(CONTEXT_INSTANCE);
98 final String operationName = xml.getName();
100 final RuntimeRpcElementResolved id = RuntimeRpcElementResolved.fromXpath(
101 contextInstanceElement.getTextContent(), operationName, namespace);
103 final Rpcs rpcs = mapRpcs(yangStoreSnapshot.getModuleMXBeanEntryMap());
105 final ModuleRpcs rpcMapping = rpcs.getRpcMapping(id);
106 final InstanceRuntimeRpc instanceRuntimeRpc = rpcMapping.getRpc(id.getRuntimeBeanName(), operationName);
108 // TODO move to Rpcs after xpath attribute is redesigned
110 final ObjectName on = id.getObjectName(rpcMapping);
111 Map<String, AttributeConfigElement> attributes = instanceRuntimeRpc.fromXml(xml);
112 attributes = sortAttributes(attributes, xml);
114 return new NetconfOperationExecution(on, instanceRuntimeRpc.getName(), attributes,
115 instanceRuntimeRpc.getReturnType(), namespace);
119 public HandlingPriority canHandle(Document message) {
120 XmlElement requestElement = getRequestElementWithCheck(message);
122 XmlElement operationElement = requestElement.getOnlyChildElement();
123 final String netconfOperationName = operationElement.getName();
124 final String netconfOperationNamespace = operationElement.getNamespace();
126 final Optional<XmlElement> contextInstanceElement = operationElement
127 .getOnlyChildElementOptionally(CONTEXT_INSTANCE);
129 if (contextInstanceElement.isPresent() == false)
130 return HandlingPriority.CANNOT_HANDLE;
132 final RuntimeRpcElementResolved id = RuntimeRpcElementResolved.fromXpath(contextInstanceElement.get()
133 .getTextContent(), netconfOperationName, netconfOperationNamespace);
135 // TODO reuse rpcs instance in fromXml method
136 final Rpcs rpcs = mapRpcs(yangStoreSnapshot.getModuleMXBeanEntryMap());
140 final ModuleRpcs rpcMapping = rpcs.getRpcMapping(id);
141 final InstanceRuntimeRpc instanceRuntimeRpc = rpcMapping.getRpc(id.getRuntimeBeanName(),
142 netconfOperationName);
143 Preconditions.checkState(instanceRuntimeRpc != null, "No rpc found for %s:%s", netconfOperationNamespace,
144 netconfOperationName);
146 } catch (IllegalStateException e) {
147 logger.debug("Cannot handle runtime operation {}:{}", netconfOperationNamespace, netconfOperationName, e);
148 return HandlingPriority.CANNOT_HANDLE;
151 return HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY;
155 protected HandlingPriority canHandle(String netconfOperationName, String namespace) {
156 throw new UnsupportedOperationException(
157 "This should not be used since it is not possible to provide check with these attributes");
161 protected String getOperationName() {
162 throw new UnsupportedOperationException("Runtime rpc does not have a stable name");
166 protected Element handleWithNoSubsequentOperations(Document document, XmlElement xml) throws NetconfDocumentedException {
168 // TODO exception handling
169 // TODO check for namespaces and unknown elements
171 final NetconfOperationExecution execution = fromXml(xml);
173 logger.debug("Invoking operation {} on {} with arguments {}", execution.operationName, execution.on,
174 execution.attributes);
175 final Object result = executeOperation(configRegistryClient, execution.on, execution.operationName,
176 execution.attributes);
178 logger.trace("Operation {} called successfully on {} with arguments {} with result {}", execution.operationName,
179 execution.on, execution.attributes, result);
181 if (execution.isVoid()) {
182 return document.createElement("ok");
184 return toXml(document, result, execution.returnType, execution.namespace,
185 execution.returnType.getAttributeYangName());
189 private static class NetconfOperationExecution {
191 private final ObjectName on;
192 private final String operationName;
193 private final Map<String, AttributeConfigElement> attributes;
194 private final AttributeIfc returnType;
195 private final String namespace;
197 public NetconfOperationExecution(final ObjectName on, final String name,
198 final Map<String, AttributeConfigElement> attributes, final AttributeIfc returnType, final String namespace) {
200 this.operationName = name;
201 this.attributes = attributes;
202 this.returnType = returnType;
203 this.namespace = namespace;
207 return returnType == VoidAttribute.getInstance();
212 private static Map<String, AttributeConfigElement> sortAttributes(
213 final Map<String, AttributeConfigElement> attributes, final XmlElement xml) {
214 final Map<String, AttributeConfigElement> sorted = Maps.newLinkedHashMap();
216 for (XmlElement xmlElement : xml.getChildElements()) {
217 final String name = xmlElement.getName();
218 if (CONTEXT_INSTANCE.equals(name) == false) { // skip context
219 // instance child node
223 final AttributeConfigElement value = attributes.get(name);
225 throw new IllegalArgumentException("Cannot find yang mapping for node " + xmlElement);
227 sorted.put(name, value);
234 private static Rpcs mapRpcs(final Map<String, Map<String, ModuleMXBeanEntry>> mBeanEntries) {
236 final Map<String, Map<String, ModuleRpcs>> map = Maps.newHashMap();
238 for (final String namespace : mBeanEntries.keySet()) {
240 Map<String, ModuleRpcs> namespaceToModules = map.get(namespace);
241 if (namespaceToModules == null) {
242 namespaceToModules = Maps.newHashMap();
243 map.put(namespace, namespaceToModules);
246 for (final String moduleName : mBeanEntries.get(namespace).keySet()) {
248 ModuleRpcs rpcMapping = namespaceToModules.get(moduleName);
249 if (rpcMapping == null) {
250 rpcMapping = new ModuleRpcs();
251 namespaceToModules.put(moduleName, rpcMapping);
254 final ModuleMXBeanEntry entry = mBeanEntries.get(namespace).get(moduleName);
256 for (final RuntimeBeanEntry runtimeEntry : entry.getRuntimeBeans()) {
257 rpcMapping.addNameMapping(runtimeEntry);
258 for (final Rpc rpc : runtimeEntry.getRpcs()) {
259 rpcMapping.addRpc(runtimeEntry, rpc);
265 return new Rpcs(map);