Search in sources :

Example 1 with ObjectMapper

use of org.opendaylight.controller.config.facade.xml.mapping.attributes.mapping.ObjectMapper in project controller by opendaylight.

the class RpcFacade method toXml.

public Element toXml(final Document doc, final Object result, final OperationExecution execution) throws DocumentedException {
    AttributeMappingStrategy<?, ? extends OpenType<?>> mappingStrategy = new ObjectMapper().prepareStrategy(execution.getReturnType());
    Optional<?> mappedAttributeOpt = mappingStrategy.mapAttribute(result);
    Preconditions.checkState(mappedAttributeOpt.isPresent(), "Unable to map return value %s as %s", result, execution.getReturnType().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 = XmlUtil.createElement(doc, "output", Optional.of(XmlMappingConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0));
    new ObjectXmlWriter().prepareWritingStrategy(execution.getReturnType().getAttributeYangName(), execution.getReturnType(), doc).writeElement(tempParent, execution.getNamespace(), mappedAttributeOpt.get());
    XmlElement xmlElement = XmlElement.fromDomElement(tempParent);
    return xmlElement.getChildElements().size() > 1 ? tempParent : xmlElement.getOnlyChildElement().getDomElement();
}
Also used : AttributeConfigElement(org.opendaylight.controller.config.facade.xml.mapping.attributes.fromxml.AttributeConfigElement) Element(org.w3c.dom.Element) XmlElement(org.opendaylight.controller.config.util.xml.XmlElement) XmlElement(org.opendaylight.controller.config.util.xml.XmlElement) ObjectXmlWriter(org.opendaylight.controller.config.facade.xml.mapping.attributes.toxml.ObjectXmlWriter) ObjectMapper(org.opendaylight.controller.config.facade.xml.mapping.attributes.mapping.ObjectMapper)

Example 2 with ObjectMapper

use of org.opendaylight.controller.config.facade.xml.mapping.attributes.mapping.ObjectMapper in project controller by opendaylight.

the class InstanceConfig method getMappedConfiguration.

@SuppressWarnings("IllegalCatch")
private Map<String, Object> getMappedConfiguration(final ObjectName on, final EnumResolver enumResolver) {
    // TODO make field, mappingStrategies can be instantiated only once
    Map<String, AttributeMappingStrategy<?, ? extends OpenType<?>>> mappingStrategies = new ObjectMapper().prepareMapping(jmxToAttrConfig, enumResolver);
    Map<String, Object> toXml = Maps.newHashMap();
    for (Entry<String, AttributeIfc> configDefEntry : jmxToAttrConfig.entrySet()) {
        // Skip children runtime beans as they are mapped by InstanceRuntime
        if (configDefEntry.getValue() instanceof RuntimeBeanEntry) {
            continue;
        }
        Object value = configRegistryClient.getAttributeCurrentValue(on, configDefEntry.getKey());
        try {
            AttributeMappingStrategy<?, ? extends OpenType<?>> attributeMappingStrategy = mappingStrategies.get(configDefEntry.getKey());
            Optional<?> attribute = attributeMappingStrategy.mapAttribute(value);
            if (!attribute.isPresent()) {
                continue;
            }
            toXml.put(configDefEntry.getValue().getAttributeYangName(), attribute.get());
        } catch (final RuntimeException e) {
            throw new IllegalStateException("Unable to map value " + value + " to attribute " + configDefEntry.getKey(), e);
        }
    }
    return toXml;
}
Also used : OpenType(javax.management.openmbean.OpenType) RuntimeBeanEntry(org.opendaylight.controller.config.yangjmxgenerator.RuntimeBeanEntry) AttributeMappingStrategy(org.opendaylight.controller.config.facade.xml.mapping.attributes.mapping.AttributeMappingStrategy) AttributeIfc(org.opendaylight.controller.config.yangjmxgenerator.attribute.AttributeIfc) ObjectMapper(org.opendaylight.controller.config.facade.xml.mapping.attributes.mapping.ObjectMapper)

Aggregations

ObjectMapper (org.opendaylight.controller.config.facade.xml.mapping.attributes.mapping.ObjectMapper)2 OpenType (javax.management.openmbean.OpenType)1 AttributeConfigElement (org.opendaylight.controller.config.facade.xml.mapping.attributes.fromxml.AttributeConfigElement)1 AttributeMappingStrategy (org.opendaylight.controller.config.facade.xml.mapping.attributes.mapping.AttributeMappingStrategy)1 ObjectXmlWriter (org.opendaylight.controller.config.facade.xml.mapping.attributes.toxml.ObjectXmlWriter)1 XmlElement (org.opendaylight.controller.config.util.xml.XmlElement)1 RuntimeBeanEntry (org.opendaylight.controller.config.yangjmxgenerator.RuntimeBeanEntry)1 AttributeIfc (org.opendaylight.controller.config.yangjmxgenerator.attribute.AttributeIfc)1 Element (org.w3c.dom.Element)1