Search in sources :

Example 6 with DocumentedException

use of org.opendaylight.controller.config.util.xml.DocumentedException in project controller by opendaylight.

the class Config method fromXmlModulesMap.

/**
 * return a map containing namespace -> moduleName -> instanceName map.
 * Attribute parsing is omitted.
 */
public Map<String, Multimap<String, ModuleElementDefinition>> fromXmlModulesMap(final XmlElement xml, final EditStrategyType defaultEditStrategyType, final ServiceRegistryWrapper serviceTracker) throws DocumentedException {
    Optional<XmlElement> modulesElement = getModulesElement(xml);
    List<XmlElement> moduleElements = getModulesElementList(modulesElement);
    Map<String, Multimap<String, ModuleElementDefinition>> retVal = Maps.newHashMap();
    ResolvingStrategy<ModuleElementDefinition> resolvingStrategy = (moduleMapping, moduleElement, serviceTracker1, instanceName, moduleNamespace, defaultStrategy) -> {
        // TODO: add check for conflicts between global and local edit
        // strategy
        String perInstanceEditStrategy = moduleElement.getAttribute(XmlMappingConstants.OPERATION_ATTR_KEY, XmlMappingConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
        return new ModuleElementDefinition(instanceName, perInstanceEditStrategy, defaultStrategy);
    };
    for (XmlElement moduleElement : moduleElements) {
        resolveModule(retVal, serviceTracker, moduleElement, defaultEditStrategyType, resolvingStrategy);
    }
    return retVal;
}
Also used : XmlUtil(org.opendaylight.controller.config.util.xml.XmlUtil) Multimap(com.google.common.collect.Multimap) EditStrategyType(org.opendaylight.controller.config.facade.xml.strategy.EditStrategyType) HashMultimap(com.google.common.collect.HashMultimap) Lists(com.google.common.collect.Lists) Optional(com.google.common.base.Optional) Document(org.w3c.dom.Document) Map(java.util.Map) EnumResolver(org.opendaylight.controller.config.facade.xml.osgi.EnumResolver) DocumentedException(org.opendaylight.controller.config.util.xml.DocumentedException) IdentityMapping(org.opendaylight.controller.config.facade.xml.mapping.IdentityMapping) Collection(java.util.Collection) Set(java.util.Set) ObjectName(javax.management.ObjectName) Maps(com.google.common.collect.Maps) Preconditions.checkState(com.google.common.base.Preconditions.checkState) List(java.util.List) Revision(org.opendaylight.yangtools.yang.common.Revision) Element(org.w3c.dom.Element) Entry(java.util.Map.Entry) Preconditions(com.google.common.base.Preconditions) XmlElement(org.opendaylight.controller.config.util.xml.XmlElement) ObjectNameUtil(org.opendaylight.controller.config.api.jmx.ObjectNameUtil) Collections(java.util.Collections) XmlMappingConstants(org.opendaylight.controller.config.util.xml.XmlMappingConstants) Multimap(com.google.common.collect.Multimap) HashMultimap(com.google.common.collect.HashMultimap) XmlElement(org.opendaylight.controller.config.util.xml.XmlElement)

Example 7 with DocumentedException

use of org.opendaylight.controller.config.util.xml.DocumentedException in project controller by opendaylight.

the class ConfigSubsystemFacade method setServicesOnTransaction.

private static void setServicesOnTransaction(final ConfigTransactionClient ta, final ConfigExecution execution) throws DocumentedException {
    Services services = execution.getServices();
    Map<String, Map<String, Map<String, Services.ServiceInstance>>> namespaceToServiceNameToRefNameToInstance = services.getNamespaceToServiceNameToRefNameToInstance();
    for (Map.Entry<String, Map<String, Map<String, Services.ServiceInstance>>> namespaceToServiceToRefEntry : namespaceToServiceNameToRefNameToInstance.entrySet()) {
        for (Map.Entry<String, Map<String, Services.ServiceInstance>> serviceToRefEntry : namespaceToServiceToRefEntry.getValue().entrySet()) {
            String qnameOfService = getQname(ta, namespaceToServiceToRefEntry.getKey(), serviceToRefEntry.getKey());
            Map<String, Services.ServiceInstance> refNameToInstance = serviceToRefEntry.getValue();
            for (Map.Entry<String, Services.ServiceInstance> refNameToServiceEntry : refNameToInstance.entrySet()) {
                ObjectName on = refNameToServiceEntry.getValue().getObjectName(ta.getTransactionName());
                try {
                    if (Services.ServiceInstance.EMPTY_SERVICE_INSTANCE == refNameToServiceEntry.getValue()) {
                        ta.removeServiceReference(qnameOfService, refNameToServiceEntry.getKey());
                        LOG.debug("Removing service {} with name {}", qnameOfService, refNameToServiceEntry.getKey());
                    } else {
                        ObjectName saved = ta.saveServiceReference(qnameOfService, refNameToServiceEntry.getKey(), on);
                        LOG.debug("Saving service {} with on {} under name {} with service on {}", qnameOfService, on, refNameToServiceEntry.getKey(), saved);
                    }
                } catch (final InstanceNotFoundException e) {
                    throw new DocumentedException(String.format("Unable to edit ref name " + refNameToServiceEntry.getKey() + " for instance " + on, e), ErrorType.APPLICATION, ErrorTag.OPERATION_FAILED, ErrorSeverity.ERROR);
                }
            }
        }
    }
}
Also used : Services(org.opendaylight.controller.config.facade.xml.mapping.config.Services) DocumentedException(org.opendaylight.controller.config.util.xml.DocumentedException) InstanceNotFoundException(javax.management.InstanceNotFoundException) HashMap(java.util.HashMap) Map(java.util.Map) ObjectName(javax.management.ObjectName)

Example 8 with DocumentedException

use of org.opendaylight.controller.config.util.xml.DocumentedException in project controller by opendaylight.

the class InstanceConfig method fromXml.

public InstanceConfigElementResolved fromXml(XmlElement moduleElement, final ServiceRegistryWrapper services, final String moduleNamespace, final EditStrategyType defaultStrategy, final Map<String, Map<Optional<Revision>, IdentityMapping>> identityMap, final EnumResolver enumResolver) throws DocumentedException {
    Map<String, AttributeConfigElement> retVal = Maps.newHashMap();
    Map<String, AttributeReadingStrategy> strats = new ObjectXmlReader().prepareReading(yangToAttrConfig, identityMap);
    List<XmlElement> recognisedChildren = Lists.newArrayList();
    XmlElement typeElement = moduleElement.getOnlyChildElementWithSameNamespace(XmlMappingConstants.TYPE_KEY);
    XmlElement nameElement = moduleElement.getOnlyChildElementWithSameNamespace(XmlMappingConstants.NAME_KEY);
    List<XmlElement> typeAndNameElements = Lists.newArrayList(typeElement, nameElement);
    // if dummy container was defined in yang, set moduleElement to its content
    if (nullableDummyContainerName != null) {
        int size = moduleElement.getChildElements().size();
        int expectedChildNodes = 1 + typeAndNameElements.size();
        if (size > expectedChildNodes) {
            throw new DocumentedException("Error reading module " + typeElement.getTextContent() + " : " + nameElement.getTextContent() + " - Expected " + expectedChildNodes + " child nodes, " + "one of them with name " + nullableDummyContainerName + ", got " + size + " elements.");
        }
        if (size == expectedChildNodes) {
            try {
                moduleElement = moduleElement.getOnlyChildElement(nullableDummyContainerName, moduleNamespace);
            } catch (final DocumentedException e) {
                throw new DocumentedException("Error reading module " + typeElement.getTextContent() + " : " + nameElement.getTextContent() + " - Expected child node with name " + nullableDummyContainerName + "." + e.getMessage(), e);
            }
        }
    // else 2 elements, no need to descend
    }
    for (Entry<String, AttributeReadingStrategy> readStratEntry : strats.entrySet()) {
        List<XmlElement> configNodes = getConfigNodes(moduleElement, moduleNamespace, readStratEntry.getKey(), recognisedChildren, typeAndNameElements);
        AttributeConfigElement readElement = readStratEntry.getValue().readElement(configNodes);
        retVal.put(readStratEntry.getKey(), readElement);
    }
    recognisedChildren.addAll(typeAndNameElements);
    try {
        moduleElement.checkUnrecognisedElements(recognisedChildren);
    } catch (final DocumentedException e) {
        throw new DocumentedException("Error reading module " + typeElement.getTextContent() + " : " + nameElement.getTextContent() + " - " + e.getMessage(), e, e.getErrorType(), e.getErrorTag(), e.getErrorSeverity(), e.getErrorInfo());
    }
    // TODO: add check for conflicts between global and local edit strategy
    String perInstanceEditStrategy = moduleElement.getAttribute(XmlMappingConstants.OPERATION_ATTR_KEY, XmlMappingConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
    InstanceConfigElementResolved instanceConfigElementResolved = perInstanceEditStrategy.equals("") ? new InstanceConfigElementResolved(retVal, defaultStrategy) : new InstanceConfigElementResolved(perInstanceEditStrategy, retVal, defaultStrategy);
    resolveConfiguration(instanceConfigElementResolved, services, enumResolver);
    return instanceConfigElementResolved;
}
Also used : AttributeReadingStrategy(org.opendaylight.controller.config.facade.xml.mapping.attributes.fromxml.AttributeReadingStrategy) AttributeConfigElement(org.opendaylight.controller.config.facade.xml.mapping.attributes.fromxml.AttributeConfigElement) ObjectXmlReader(org.opendaylight.controller.config.facade.xml.mapping.attributes.fromxml.ObjectXmlReader) DocumentedException(org.opendaylight.controller.config.util.xml.DocumentedException) XmlElement(org.opendaylight.controller.config.util.xml.XmlElement)

Example 9 with DocumentedException

use of org.opendaylight.controller.config.util.xml.DocumentedException in project controller by opendaylight.

the class InstanceConfig method resolveConfiguration.

private void resolveConfiguration(final InstanceConfigElementResolved mappedConfig, final ServiceRegistryWrapper depTracker, final EnumResolver enumResolver) {
    // TODO make field, resolvingStrategies can be instantiated only once
    Map<String, AttributeResolvingStrategy<?, ? extends OpenType<?>>> resolvingStrategies = new ObjectResolver(depTracker).prepareResolving(yangToAttrConfig, enumResolver);
    for (Entry<String, AttributeConfigElement> configDefEntry : mappedConfig.getConfiguration().entrySet()) {
        AttributeConfigElement value = configDefEntry.getValue();
        String attributeName = configDefEntry.getKey();
        try {
            AttributeResolvingStrategy<?, ? extends OpenType<?>> attributeResolvingStrategy = resolvingStrategies.get(attributeName);
            LOG.trace("Trying to set value {} of attribute {} with {}", value, attributeName, attributeResolvingStrategy);
            value.resolveValue(attributeResolvingStrategy, attributeName);
            value.setJmxName(yangToAttrConfig.get(attributeName).getUpperCaseCammelCase());
        } catch (final DocumentedException e) {
            throw new IllegalStateException("Unable to resolve value " + value + " to attribute " + attributeName, e);
        }
    }
}
Also used : OpenType(javax.management.openmbean.OpenType) AttributeResolvingStrategy(org.opendaylight.controller.config.facade.xml.mapping.attributes.resolving.AttributeResolvingStrategy) AttributeConfigElement(org.opendaylight.controller.config.facade.xml.mapping.attributes.fromxml.AttributeConfigElement) DocumentedException(org.opendaylight.controller.config.util.xml.DocumentedException) ObjectResolver(org.opendaylight.controller.config.facade.xml.mapping.attributes.resolving.ObjectResolver)

Aggregations

DocumentedException (org.opendaylight.controller.config.util.xml.DocumentedException)9 Map (java.util.Map)3 ObjectName (javax.management.ObjectName)3 ValidationException (org.opendaylight.controller.config.api.ValidationException)3 AttributeConfigElement (org.opendaylight.controller.config.facade.xml.mapping.attributes.fromxml.AttributeConfigElement)3 XmlElement (org.opendaylight.controller.config.util.xml.XmlElement)3 Element (org.w3c.dom.Element)3 Optional (com.google.common.base.Optional)2 Preconditions (com.google.common.base.Preconditions)2 Preconditions.checkState (com.google.common.base.Preconditions.checkState)2 HashMultimap (com.google.common.collect.HashMultimap)2 Lists (com.google.common.collect.Lists)2 Maps (com.google.common.collect.Maps)2 Multimap (com.google.common.collect.Multimap)2 IOException (java.io.IOException)2 Collection (java.util.Collection)2 Collections (java.util.Collections)2 List (java.util.List)2 Entry (java.util.Map.Entry)2 Set (java.util.Set)2