X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fnetconf%2Fconfig-netconf-connector%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fnetconf%2Fconfignetconfconnector%2Foperations%2Fruntimerpc%2FRuntimeRpc.java;h=f838c6f9f5e69160b4c889d799f7e42ed2c7338c;hb=4043d42c401e0ad6369c9ec35f2c926dcc18c80d;hp=7bdfa277a00418d39b5724267947fcf7e4446a81;hpb=a92d9d6a21a0f6ca8d2153795721f500eaf29ee9;p=controller.git diff --git a/opendaylight/netconf/config-netconf-connector/src/main/java/org/opendaylight/controller/netconf/confignetconfconnector/operations/runtimerpc/RuntimeRpc.java b/opendaylight/netconf/config-netconf-connector/src/main/java/org/opendaylight/controller/netconf/confignetconfconnector/operations/runtimerpc/RuntimeRpc.java index 7bdfa277a0..f838c6f9f5 100644 --- a/opendaylight/netconf/config-netconf-connector/src/main/java/org/opendaylight/controller/netconf/confignetconfconnector/operations/runtimerpc/RuntimeRpc.java +++ b/opendaylight/netconf/config-netconf-connector/src/main/java/org/opendaylight/controller/netconf/confignetconfconnector/operations/runtimerpc/RuntimeRpc.java @@ -16,10 +16,13 @@ import org.opendaylight.controller.config.yang.store.api.YangStoreSnapshot; import org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry; import org.opendaylight.controller.config.yangjmxgenerator.RuntimeBeanEntry; import org.opendaylight.controller.config.yangjmxgenerator.RuntimeBeanEntry.Rpc; -import org.opendaylight.controller.config.yangjmxgenerator.attribute.SimpleTypeResolver; +import org.opendaylight.controller.config.yangjmxgenerator.attribute.AttributeIfc; +import org.opendaylight.controller.config.yangjmxgenerator.attribute.VoidAttribute; import org.opendaylight.controller.netconf.api.NetconfDocumentedException; import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.fromxml.AttributeConfigElement; -import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.mapping.SimpleAttributeMappingStrategy; +import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.mapping.AttributeMappingStrategy; +import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.mapping.ObjectMapper; +import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.toxml.ObjectXmlWriter; import org.opendaylight.controller.netconf.confignetconfconnector.mapping.rpc.InstanceRuntimeRpc; import org.opendaylight.controller.netconf.confignetconfconnector.mapping.rpc.ModuleRpcs; import org.opendaylight.controller.netconf.confignetconfconnector.mapping.rpc.Rpcs; @@ -27,14 +30,14 @@ import org.opendaylight.controller.netconf.confignetconfconnector.operations.Abs import org.opendaylight.controller.netconf.confignetconfconnector.operations.Commit; import org.opendaylight.controller.netconf.mapping.api.HandlingPriority; import org.opendaylight.controller.netconf.util.xml.XmlElement; -import org.opendaylight.controller.netconf.util.xml.XmlUtil; +import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import javax.management.ObjectName; -import javax.management.openmbean.SimpleType; +import javax.management.openmbean.OpenType; import java.util.Map; public class RuntimeRpc extends AbstractConfigNetconfOperation { @@ -50,10 +53,22 @@ public class RuntimeRpc extends AbstractConfigNetconfOperation { this.yangStoreSnapshot = yangStoreSnapshot; } - private String getStringRepresentation(final Object result) { - final SimpleType simpleType = SimpleTypeResolver.getSimpleType(result.getClass().getName()); - final Optional mappedAttributeOpt = new SimpleAttributeMappingStrategy(simpleType).mapAttribute(result); - return mappedAttributeOpt.isPresent() ? mappedAttributeOpt.get() : ""; + private Element toXml(Document doc, Object result, AttributeIfc returnType, String namespace, String elementName) { + AttributeMappingStrategy> mappingStrategy = new ObjectMapper(null).prepareStrategy(returnType); + Optional mappedAttributeOpt = mappingStrategy.mapAttribute(result); + Preconditions.checkState(mappedAttributeOpt.isPresent(), "Unable to map return value %s as %s", result, returnType.getOpenType()); + + // FIXME: multiple return values defined as leaf-list and list in yang should not be wrapped in output xml element, + // they need to be appended directly under rpc-reply element + // + // Either allow List of Elements to be returned from NetconfOperation or + // pass reference to parent output xml element for netconf operations to + // append result(s) on their own + Element tempParent = doc.createElementNS(XmlNetconfConstants.RFC4741_TARGET_NAMESPACE, "output"); + new ObjectXmlWriter().prepareWritingStrategy(elementName, returnType, doc).writeElement(tempParent, namespace, mappedAttributeOpt.get()); + + XmlElement xmlElement = XmlElement.fromDomElement(tempParent); + return xmlElement.getChildElements().size() > 1 ? tempParent : xmlElement.getOnlyChildElement().getDomElement(); } private Object executeOperation(final ConfigRegistryClient configRegistryClient, final ObjectName on, @@ -161,12 +176,11 @@ public class RuntimeRpc extends AbstractConfigNetconfOperation { logger.info("Operation {} called successfully on {} with arguments {} with result {}", execution.operationName, execution.on, execution.attributes, result); - if (execution.returnType.equals("void")) { + if (execution.isVoid()) { return document.createElement("ok"); } else { - final Element output = XmlUtil.createTextElement(document, "result", getStringRepresentation(result)); - XmlUtil.addNamespaceAttr(output, execution.namespace); - return output; + return toXml(document, result, execution.returnType, execution.namespace, + execution.returnType.getAttributeYangName()); } } @@ -175,11 +189,11 @@ public class RuntimeRpc extends AbstractConfigNetconfOperation { private final ObjectName on; private final String operationName; private final Map attributes; - private final String returnType; + private final AttributeIfc returnType; private final String namespace; public NetconfOperationExecution(final ObjectName on, final String name, - final Map attributes, final String returnType, final String namespace) { + final Map attributes, final AttributeIfc returnType, final String namespace) { this.on = on; this.operationName = name; this.attributes = attributes; @@ -187,6 +201,10 @@ public class RuntimeRpc extends AbstractConfigNetconfOperation { this.namespace = namespace; } + boolean isVoid() { + return returnType == VoidAttribute.getInstance(); + } + } private static Map sortAttributes(