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;
15 import javax.management.ObjectName;
16 import javax.management.openmbean.OpenType;
17 import org.opendaylight.controller.config.util.ConfigRegistryClient;
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.api.xml.XmlNetconfConstants;
25 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.fromxml.AttributeConfigElement;
26 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.mapping.AttributeMappingStrategy;
27 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.mapping.ObjectMapper;
28 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.toxml.ObjectXmlWriter;
29 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.rpc.InstanceRuntimeRpc;
30 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.rpc.ModuleRpcs;
31 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.rpc.Rpcs;
32 import org.opendaylight.controller.netconf.confignetconfconnector.operations.AbstractConfigNetconfOperation;
33 import org.opendaylight.controller.netconf.confignetconfconnector.osgi.EnumResolver;
34 import org.opendaylight.controller.netconf.confignetconfconnector.osgi.YangStoreContext;
35 import org.opendaylight.controller.netconf.mapping.api.HandlingPriority;
36 import org.opendaylight.controller.netconf.util.exception.MissingNameSpaceException;
37 import org.opendaylight.controller.netconf.util.xml.XmlElement;
38 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41 import org.w3c.dom.Document;
42 import org.w3c.dom.Element;
44 public class RuntimeRpc extends AbstractConfigNetconfOperation {
46 private static final Logger LOG = LoggerFactory.getLogger(RuntimeRpc.class);
47 public static final String CONTEXT_INSTANCE = "context-instance";
49 private final YangStoreContext yangStoreSnapshot;
51 public RuntimeRpc(final YangStoreContext yangStoreSnapshot, ConfigRegistryClient configRegistryClient,
52 String netconfSessionIdForReporting) {
53 super(configRegistryClient, netconfSessionIdForReporting);
54 this.yangStoreSnapshot = yangStoreSnapshot;
57 private Element toXml(Document doc, Object result, AttributeIfc returnType, String namespace, String elementName) throws NetconfDocumentedException {
58 AttributeMappingStrategy<?, ? extends OpenType<?>> mappingStrategy = new ObjectMapper().prepareStrategy(returnType);
59 Optional<?> mappedAttributeOpt = mappingStrategy.mapAttribute(result);
60 Preconditions.checkState(mappedAttributeOpt.isPresent(), "Unable to map return value %s as %s", result, returnType.getOpenType());
62 // FIXME: multiple return values defined as leaf-list and list in yang should not be wrapped in output xml element,
63 // they need to be appended directly under rpc-reply element
65 // Either allow List of Elements to be returned from NetconfOperation or
66 // pass reference to parent output xml element for netconf operations to
67 // append result(s) on their own
68 Element tempParent = XmlUtil.createElement(doc, "output", Optional.of(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0));
69 new ObjectXmlWriter().prepareWritingStrategy(elementName, returnType, doc).writeElement(tempParent, namespace, mappedAttributeOpt.get());
71 XmlElement xmlElement = XmlElement.fromDomElement(tempParent);
72 return xmlElement.getChildElements().size() > 1 ? tempParent : xmlElement.getOnlyChildElement().getDomElement();
75 private Object executeOperation(final ConfigRegistryClient configRegistryClient, final ObjectName on,
76 final String name, final Map<String, AttributeConfigElement> attributes) {
77 final Object[] params = new Object[attributes.size()];
78 final String[] signature = new String[attributes.size()];
81 for (final AttributeConfigElement attribute : attributes.values()) {
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;
96 namespace = xml.getNamespace();
97 } catch (MissingNameSpaceException e) {
98 LOG.trace("Can't get namespace from xml element due to ",e);
99 throw NetconfDocumentedException.wrap(e);
101 final XmlElement contextInstanceElement = xml.getOnlyChildElement(CONTEXT_INSTANCE);
102 final String operationName = xml.getName();
104 final RuntimeRpcElementResolved id = RuntimeRpcElementResolved.fromXpath(
105 contextInstanceElement.getTextContent(), operationName, namespace);
107 final Rpcs rpcs = mapRpcs(yangStoreSnapshot.getModuleMXBeanEntryMap(), yangStoreSnapshot.getEnumResolver());
109 final ModuleRpcs rpcMapping = rpcs.getRpcMapping(id);
110 final InstanceRuntimeRpc instanceRuntimeRpc = rpcMapping.getRpc(id.getRuntimeBeanName(), operationName);
112 // TODO move to Rpcs after xpath attribute is redesigned
114 final ObjectName on = id.getObjectName(rpcMapping);
115 Map<String, AttributeConfigElement> attributes = instanceRuntimeRpc.fromXml(xml);
116 attributes = sortAttributes(attributes, xml);
118 return new NetconfOperationExecution(on, instanceRuntimeRpc.getName(), attributes,
119 instanceRuntimeRpc.getReturnType(), namespace);
123 public HandlingPriority canHandle(Document message) throws NetconfDocumentedException {
124 XmlElement requestElement = null;
125 requestElement = getRequestElementWithCheck(message);
127 XmlElement operationElement = requestElement.getOnlyChildElement();
128 final String netconfOperationName = operationElement.getName();
129 final String netconfOperationNamespace;
131 netconfOperationNamespace = operationElement.getNamespace();
132 } catch (MissingNameSpaceException e) {
133 LOG.debug("Cannot retrieve netconf operation namespace from message due to ", e);
134 return HandlingPriority.CANNOT_HANDLE;
137 final Optional<XmlElement> contextInstanceElement = operationElement
138 .getOnlyChildElementOptionally(CONTEXT_INSTANCE);
140 if (!contextInstanceElement.isPresent()){
141 return HandlingPriority.CANNOT_HANDLE;
144 final RuntimeRpcElementResolved id = RuntimeRpcElementResolved.fromXpath(contextInstanceElement.get()
145 .getTextContent(), netconfOperationName, netconfOperationNamespace);
147 // TODO reuse rpcs instance in fromXml method
148 final Rpcs rpcs = mapRpcs(yangStoreSnapshot.getModuleMXBeanEntryMap(), yangStoreSnapshot.getEnumResolver());
152 final ModuleRpcs rpcMapping = rpcs.getRpcMapping(id);
153 final InstanceRuntimeRpc instanceRuntimeRpc = rpcMapping.getRpc(id.getRuntimeBeanName(),
154 netconfOperationName);
155 Preconditions.checkState(instanceRuntimeRpc != null, "No rpc found for %s:%s", netconfOperationNamespace,
156 netconfOperationName);
158 } catch (IllegalStateException e) {
159 LOG.debug("Cannot handle runtime operation {}:{}", netconfOperationNamespace, netconfOperationName, e);
160 return HandlingPriority.CANNOT_HANDLE;
163 return HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY;
167 protected HandlingPriority canHandle(String netconfOperationName, String namespace) {
168 throw new UnsupportedOperationException(
169 "This should not be used since it is not possible to provide check with these attributes");
173 protected String getOperationName() {
174 throw new UnsupportedOperationException("Runtime rpc does not have a stable name");
178 protected Element handleWithNoSubsequentOperations(Document document, XmlElement xml) throws NetconfDocumentedException {
179 // TODO check for namespaces and unknown elements
180 final NetconfOperationExecution execution = fromXml(xml);
182 LOG.debug("Invoking operation {} on {} with arguments {}", execution.operationName, execution.on,
183 execution.attributes);
184 final Object result = executeOperation(getConfigRegistryClient(), execution.on, execution.operationName,
185 execution.attributes);
187 LOG.trace("Operation {} called successfully on {} with arguments {} with result {}", execution.operationName,
188 execution.on, execution.attributes, result);
190 if (execution.isVoid()) {
191 return XmlUtil.createElement(document, XmlNetconfConstants.OK, Optional.<String>absent());
193 return toXml(document, result, execution.returnType, execution.namespace,
194 execution.returnType.getAttributeYangName());
198 private static class NetconfOperationExecution {
200 private final ObjectName on;
201 private final String operationName;
202 private final Map<String, AttributeConfigElement> attributes;
203 private final AttributeIfc returnType;
204 private final String namespace;
206 public NetconfOperationExecution(final ObjectName on, final String name,
207 final Map<String, AttributeConfigElement> attributes, final AttributeIfc returnType, final String namespace) {
209 this.operationName = name;
210 this.attributes = attributes;
211 this.returnType = returnType;
212 this.namespace = namespace;
216 return returnType == VoidAttribute.getInstance();
221 private static Map<String, AttributeConfigElement> sortAttributes(
222 final Map<String, AttributeConfigElement> attributes, final XmlElement xml) {
223 final Map<String, AttributeConfigElement> sorted = Maps.newLinkedHashMap();
225 for (XmlElement xmlElement : xml.getChildElements()) {
226 final String name = xmlElement.getName();
227 if (!CONTEXT_INSTANCE.equals(name)) { // skip context
228 // instance child node
232 final AttributeConfigElement value = attributes.get(name);
234 throw new IllegalArgumentException("Cannot find yang mapping for node " + xmlElement);
236 sorted.put(name, value);
243 private static Rpcs mapRpcs(final Map<String, Map<String, ModuleMXBeanEntry>> mBeanEntries, final EnumResolver enumResolver) {
245 final Map<String, Map<String, ModuleRpcs>> map = Maps.newHashMap();
247 for (final Map.Entry<String, Map<String, ModuleMXBeanEntry>> namespaceToModuleEntry : mBeanEntries.entrySet()) {
249 Map<String, ModuleRpcs> namespaceToModules = map.get(namespaceToModuleEntry.getKey());
250 if (namespaceToModules == null) {
251 namespaceToModules = Maps.newHashMap();
252 map.put(namespaceToModuleEntry.getKey(), namespaceToModules);
255 for (final Map.Entry<String, ModuleMXBeanEntry> moduleEntry : namespaceToModuleEntry.getValue().entrySet()) {
257 ModuleRpcs rpcMapping = namespaceToModules.get(moduleEntry.getKey());
258 if (rpcMapping == null) {
259 rpcMapping = new ModuleRpcs(enumResolver);
260 namespaceToModules.put(moduleEntry.getKey(), rpcMapping);
263 final ModuleMXBeanEntry entry = moduleEntry.getValue();
265 for (final RuntimeBeanEntry runtimeEntry : entry.getRuntimeBeans()) {
266 rpcMapping.addNameMapping(runtimeEntry);
267 for (final Rpc rpc : runtimeEntry.getRpcs()) {
268 rpcMapping.addRpc(runtimeEntry, rpc);
274 return new Rpcs(map);