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.yangjmxgenerator.ModuleMXBeanEntry;
18 import org.opendaylight.controller.config.yangjmxgenerator.RuntimeBeanEntry;
19 import org.opendaylight.controller.config.yangjmxgenerator.RuntimeBeanEntry.Rpc;
20 import org.opendaylight.controller.config.yangjmxgenerator.attribute.AttributeIfc;
21 import org.opendaylight.controller.config.yangjmxgenerator.attribute.VoidAttribute;
22 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
23 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.fromxml.AttributeConfigElement;
24 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.mapping.AttributeMappingStrategy;
25 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.mapping.ObjectMapper;
26 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.toxml.ObjectXmlWriter;
27 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.rpc.InstanceRuntimeRpc;
28 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.rpc.ModuleRpcs;
29 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.rpc.Rpcs;
30 import org.opendaylight.controller.netconf.confignetconfconnector.operations.AbstractConfigNetconfOperation;
31 import org.opendaylight.controller.netconf.confignetconfconnector.operations.Commit;
32 import org.opendaylight.controller.netconf.confignetconfconnector.osgi.YangStoreSnapshot;
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.opendaylight.controller.netconf.util.xml.XmlUtil;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import org.w3c.dom.Document;
40 import org.w3c.dom.Element;
42 import com.google.common.base.Optional;
43 import com.google.common.base.Preconditions;
44 import com.google.common.collect.Maps;
46 public class RuntimeRpc extends AbstractConfigNetconfOperation {
48 private static final Logger logger = LoggerFactory.getLogger(Commit.class);
49 public static final String CONTEXT_INSTANCE = "context-instance";
51 private final YangStoreSnapshot yangStoreSnapshot;
53 public RuntimeRpc(final YangStoreSnapshot yangStoreSnapshot, ConfigRegistryClient configRegistryClient,
54 String netconfSessionIdForReporting) {
55 super(configRegistryClient, netconfSessionIdForReporting);
56 this.yangStoreSnapshot = yangStoreSnapshot;
59 private Element toXml(Document doc, Object result, AttributeIfc returnType, String namespace, String elementName) {
60 AttributeMappingStrategy<?, ? extends OpenType<?>> mappingStrategy = new ObjectMapper(null).prepareStrategy(returnType);
61 Optional<?> mappedAttributeOpt = mappingStrategy.mapAttribute(result);
62 Preconditions.checkState(mappedAttributeOpt.isPresent(), "Unable to map return value %s as %s", result, returnType.getOpenType());
64 // FIXME: multiple return values defined as leaf-list and list in yang should not be wrapped in output xml element,
65 // they need to be appended directly under rpc-reply element
67 // Either allow List of Elements to be returned from NetconfOperation or
68 // pass reference to parent output xml element for netconf operations to
69 // append result(s) on their own
70 Element tempParent = XmlUtil.createElement(doc, "output", Optional.of(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0));
71 new ObjectXmlWriter().prepareWritingStrategy(elementName, returnType, doc).writeElement(tempParent, namespace, mappedAttributeOpt.get());
73 XmlElement xmlElement = XmlElement.fromDomElement(tempParent);
74 return xmlElement.getChildElements().size() > 1 ? tempParent : xmlElement.getOnlyChildElement().getDomElement();
77 private Object executeOperation(final ConfigRegistryClient configRegistryClient, final ObjectName on,
78 final String name, final Map<String, AttributeConfigElement> attributes) {
79 final Object[] params = new Object[attributes.size()];
80 final String[] signature = new String[attributes.size()];
83 for (final String attrName : attributes.keySet()) {
84 final AttributeConfigElement attribute = attributes.get(attrName);
85 final Optional<?> resolvedValueOpt = attribute.getResolvedValue();
87 params[i] = resolvedValueOpt.isPresent() ? resolvedValueOpt.get() : attribute.getResolvedDefaultValue();
88 signature[i] = resolvedValueOpt.isPresent() ? resolvedValueOpt.get().getClass().getName() : attribute
89 .getResolvedDefaultValue().getClass().getName();
93 return configRegistryClient.invokeMethod(on, name, params, signature);
96 public NetconfOperationExecution fromXml(final XmlElement xml) throws NetconfDocumentedException {
97 final String namespace = xml.getNamespace();
98 final XmlElement contextInstanceElement = xml.getOnlyChildElement(CONTEXT_INSTANCE);
99 final String operationName = xml.getName();
101 final RuntimeRpcElementResolved id = RuntimeRpcElementResolved.fromXpath(
102 contextInstanceElement.getTextContent(), operationName, namespace);
104 final Rpcs rpcs = mapRpcs(yangStoreSnapshot.getModuleMXBeanEntryMap());
106 final ModuleRpcs rpcMapping = rpcs.getRpcMapping(id);
107 final InstanceRuntimeRpc instanceRuntimeRpc = rpcMapping.getRpc(id.getRuntimeBeanName(), operationName);
109 // TODO move to Rpcs after xpath attribute is redesigned
111 final ObjectName on = id.getObjectName(rpcMapping);
112 Map<String, AttributeConfigElement> attributes = instanceRuntimeRpc.fromXml(xml);
113 attributes = sortAttributes(attributes, xml);
115 return new NetconfOperationExecution(on, instanceRuntimeRpc.getName(), attributes,
116 instanceRuntimeRpc.getReturnType(), namespace);
120 public HandlingPriority canHandle(Document message) {
121 XmlElement requestElement = getRequestElementWithCheck(message);
123 XmlElement operationElement = requestElement.getOnlyChildElement();
124 final String netconfOperationName = operationElement.getName();
125 final String netconfOperationNamespace = operationElement.getNamespace();
127 final Optional<XmlElement> contextInstanceElement = operationElement
128 .getOnlyChildElementOptionally(CONTEXT_INSTANCE);
130 if (contextInstanceElement.isPresent() == false)
131 return HandlingPriority.CANNOT_HANDLE;
133 final RuntimeRpcElementResolved id = RuntimeRpcElementResolved.fromXpath(contextInstanceElement.get()
134 .getTextContent(), netconfOperationName, netconfOperationNamespace);
136 // TODO reuse rpcs instance in fromXml method
137 final Rpcs rpcs = mapRpcs(yangStoreSnapshot.getModuleMXBeanEntryMap());
141 final ModuleRpcs rpcMapping = rpcs.getRpcMapping(id);
142 final InstanceRuntimeRpc instanceRuntimeRpc = rpcMapping.getRpc(id.getRuntimeBeanName(),
143 netconfOperationName);
144 Preconditions.checkState(instanceRuntimeRpc != null, "No rpc found for %s:%s", netconfOperationNamespace,
145 netconfOperationName);
147 } catch (IllegalStateException e) {
148 logger.debug("Cannot handle runtime operation {}:{}", netconfOperationNamespace, netconfOperationName, e);
149 return HandlingPriority.CANNOT_HANDLE;
152 return HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY;
156 protected HandlingPriority canHandle(String netconfOperationName, String namespace) {
157 throw new UnsupportedOperationException(
158 "This should not be used since it is not possible to provide check with these attributes");
162 protected String getOperationName() {
163 throw new UnsupportedOperationException("Runtime rpc does not have a stable name");
167 protected Element handleWithNoSubsequentOperations(Document document, XmlElement xml) throws NetconfDocumentedException {
169 // TODO exception handling
170 // TODO check for namespaces and unknown elements
172 final NetconfOperationExecution execution = fromXml(xml);
174 logger.debug("Invoking operation {} on {} with arguments {}", execution.operationName, execution.on,
175 execution.attributes);
176 final Object result = executeOperation(configRegistryClient, execution.on, execution.operationName,
177 execution.attributes);
179 logger.trace("Operation {} called successfully on {} with arguments {} with result {}", execution.operationName,
180 execution.on, execution.attributes, result);
182 if (execution.isVoid()) {
183 return XmlUtil.createElement(document, XmlNetconfConstants.OK, Optional.<String>absent());
185 return toXml(document, result, execution.returnType, execution.namespace,
186 execution.returnType.getAttributeYangName());
190 private static class NetconfOperationExecution {
192 private final ObjectName on;
193 private final String operationName;
194 private final Map<String, AttributeConfigElement> attributes;
195 private final AttributeIfc returnType;
196 private final String namespace;
198 public NetconfOperationExecution(final ObjectName on, final String name,
199 final Map<String, AttributeConfigElement> attributes, final AttributeIfc returnType, final String namespace) {
201 this.operationName = name;
202 this.attributes = attributes;
203 this.returnType = returnType;
204 this.namespace = namespace;
208 return returnType == VoidAttribute.getInstance();
213 private static Map<String, AttributeConfigElement> sortAttributes(
214 final Map<String, AttributeConfigElement> attributes, final XmlElement xml) {
215 final Map<String, AttributeConfigElement> sorted = Maps.newLinkedHashMap();
217 for (XmlElement xmlElement : xml.getChildElements()) {
218 final String name = xmlElement.getName();
219 if (CONTEXT_INSTANCE.equals(name) == false) { // skip context
220 // instance child node
224 final AttributeConfigElement value = attributes.get(name);
226 throw new IllegalArgumentException("Cannot find yang mapping for node " + xmlElement);
228 sorted.put(name, value);
235 private static Rpcs mapRpcs(final Map<String, Map<String, ModuleMXBeanEntry>> mBeanEntries) {
237 final Map<String, Map<String, ModuleRpcs>> map = Maps.newHashMap();
239 for (final String namespace : mBeanEntries.keySet()) {
241 Map<String, ModuleRpcs> namespaceToModules = map.get(namespace);
242 if (namespaceToModules == null) {
243 namespaceToModules = Maps.newHashMap();
244 map.put(namespace, namespaceToModules);
247 for (final String moduleName : mBeanEntries.get(namespace).keySet()) {
249 ModuleRpcs rpcMapping = namespaceToModules.get(moduleName);
250 if (rpcMapping == null) {
251 rpcMapping = new ModuleRpcs();
252 namespaceToModules.put(moduleName, rpcMapping);
255 final ModuleMXBeanEntry entry = mBeanEntries.get(namespace).get(moduleName);
257 for (final RuntimeBeanEntry runtimeEntry : entry.getRuntimeBeans()) {
258 rpcMapping.addNameMapping(runtimeEntry);
259 for (final Rpc rpc : runtimeEntry.getRpcs()) {
260 rpcMapping.addRpc(runtimeEntry, rpc);
266 return new Rpcs(map);