Search in sources :

Example 1 with AttributeIfc

use of org.opendaylight.controller.config.yangjmxgenerator.attribute.AttributeIfc in project controller by opendaylight.

the class ModuleMXBeanEntryBuilder method processCaseSchemaNode.

private <HAS_CHILDREN_AND_QNAME extends DataNodeContainer & SchemaNode> void processCaseSchemaNode(final Map<String, ModuleMXBeanEntry> result, final Map<String, QName> uniqueGeneratedClassesNames, final String configModulePrefix, final Map<String, IdentitySchemaNode> moduleIdentities, final Map<String, IdentitySchemaNode> unaugmentedModuleIdentities, final AugmentationSchemaNode augmentation, final DataSchemaNode when) {
    CaseSchemaNode choiceCaseNode = (CaseSchemaNode) when;
    if (!choiceCaseNode.getWhenCondition().isPresent()) {
        return;
    }
    java.util.Optional<RevisionAwareXPath> xPath = choiceCaseNode.getWhenCondition();
    checkState(xPath.isPresent(), "Choice node %s does not have a when condition", choiceCaseNode);
    Matcher matcher = getWhenConditionMatcher(configModulePrefix, xPath.get());
    if (matcher.matches() == false) {
        return;
    }
    String moduleLocalNameFromXPath = matcher.group(1);
    IdentitySchemaNode moduleIdentity = moduleIdentities.get(moduleLocalNameFromXPath);
    unaugmentedModuleIdentities.remove(moduleLocalNameFromXPath);
    checkState(moduleIdentity != null, "Cannot find identity %s matching augmentation %s", moduleLocalNameFromXPath, augmentation);
    Map<String, QName> providedServices = findProvidedServices(moduleIdentity, currentModule, qNamesToSIEs, schemaContext);
    String javaNamePrefix = TypeProviderWrapper.findJavaNamePrefix(moduleIdentity);
    Map<String, AttributeIfc> yangToAttributes = null;
    // runtime-data
    Collection<RuntimeBeanEntry> runtimeBeans = null;
    HAS_CHILDREN_AND_QNAME dataNodeContainer = getDataNodeContainer(choiceCaseNode);
    if (EXPECTED_CONFIGURATION_AUGMENTATION_SCHEMA_PATH.equals(augmentation.getTargetPath())) {
        LOG.debug("Parsing configuration of {}", moduleLocalNameFromXPath);
        yangToAttributes = fillConfiguration(dataNodeContainer, currentModule, typeProviderWrapper, qNamesToSIEs, schemaContext, packageName);
        checkUniqueAttributesWithGeneratedClass(uniqueGeneratedClassesNames, when.getQName(), yangToAttributes);
    } else if (EXPECTED_STATE_AUGMENTATION_SCHEMA_PATH.equals(augmentation.getTargetPath())) {
        LOG.debug("Parsing state of {}", moduleLocalNameFromXPath);
        try {
            runtimeBeans = fillRuntimeBeans(dataNodeContainer, currentModule, typeProviderWrapper, packageName, moduleLocalNameFromXPath, javaNamePrefix);
        } catch (NameConflictException e) {
            throw new NameConflictException(e.getConflictingName(), when.getQName(), when.getQName());
        }
        checkUniqueRuntimeBeansGeneratedClasses(uniqueGeneratedClassesNames, when, runtimeBeans);
        Set<RuntimeBeanEntry> runtimeBeanEntryValues = Sets.newHashSet(runtimeBeans);
        for (RuntimeBeanEntry entry : runtimeBeanEntryValues) {
            checkUniqueAttributesWithGeneratedClass(uniqueGeneratedClassesNames, when.getQName(), entry.getYangPropertiesToTypesMap());
        }
    } else {
        throw new IllegalArgumentException("Cannot parse augmentation " + augmentation);
    }
    boolean hasDummyContainer = choiceCaseNode.equals(dataNodeContainer) == false;
    String nullableDummyContainerName = hasDummyContainer ? dataNodeContainer.getQName().getLocalName() : null;
    if (result.containsKey(moduleLocalNameFromXPath)) {
        // either fill runtimeBeans or yangToAttributes, merge
        ModuleMXBeanEntry moduleMXBeanEntry = result.get(moduleLocalNameFromXPath);
        if (yangToAttributes != null && moduleMXBeanEntry.getAttributes() == null) {
            moduleMXBeanEntry.setYangToAttributes(yangToAttributes);
        } else if (runtimeBeans != null && moduleMXBeanEntry.getRuntimeBeans() == null) {
            moduleMXBeanEntry.setRuntimeBeans(runtimeBeans);
        }
        checkState(Objects.equals(nullableDummyContainerName, moduleMXBeanEntry.getNullableDummyContainerName()), "Mismatch in module " + moduleMXBeanEntry.toString() + " - dummy container must be present/missing in" + " both state and configuration");
    } else {
        ModuleMXBeanEntry.ModuleMXBeanEntryInitial initial = new ModuleMXBeanEntry.ModuleMXBeanEntryInitialBuilder().setIdSchemaNode(moduleIdentity).setPackageName(packageName).setJavaNamePrefix(javaNamePrefix).setNamespace(currentModule.getNamespace().toString()).setqName(ModuleUtil.getQName(currentModule)).build();
        // construct ModuleMXBeanEntry
        ModuleMXBeanEntry moduleMXBeanEntry = new ModuleMXBeanEntry(initial, yangToAttributes, providedServices, runtimeBeans);
        moduleMXBeanEntry.setYangModuleName(currentModule.getName());
        moduleMXBeanEntry.setYangModuleLocalname(moduleLocalNameFromXPath);
        moduleMXBeanEntry.setNullableDummyContainerName(nullableDummyContainerName);
        result.put(moduleLocalNameFromXPath, moduleMXBeanEntry);
    }
}
Also used : Set(java.util.Set) Matcher(java.util.regex.Matcher) QName(org.opendaylight.yangtools.yang.common.QName) ConfigConstants.createConfigQName(org.opendaylight.controller.config.yangjmxgenerator.ConfigConstants.createConfigQName) NameConflictException(org.opendaylight.controller.config.yangjmxgenerator.plugin.util.NameConflictException) IdentitySchemaNode(org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode) RevisionAwareXPath(org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath) CaseSchemaNode(org.opendaylight.yangtools.yang.model.api.CaseSchemaNode) AttributeIfc(org.opendaylight.controller.config.yangjmxgenerator.attribute.AttributeIfc)

Example 2 with AttributeIfc

use of org.opendaylight.controller.config.yangjmxgenerator.attribute.AttributeIfc in project controller by opendaylight.

the class ModuleMXBeanEntryBuilder method fillConfiguration.

private static Map<String, AttributeIfc> fillConfiguration(final DataNodeContainer dataNodeContainer, final Module currentModule, final TypeProviderWrapper typeProviderWrapper, final Map<QName, ServiceInterfaceEntry> qNamesToSIEs, final SchemaContext schemaContext, final String packageName) {
    Map<String, AttributeIfc> yangToAttributes = new HashMap<>();
    for (DataSchemaNode attrNode : dataNodeContainer.getChildNodes()) {
        AttributeIfc attributeValue = getAttributeValue(attrNode, currentModule, qNamesToSIEs, typeProviderWrapper, schemaContext, packageName);
        yangToAttributes.put(attributeValue.getAttributeYangName(), attributeValue);
    }
    return yangToAttributes;
}
Also used : HashMap(java.util.HashMap) DataSchemaNode(org.opendaylight.yangtools.yang.model.api.DataSchemaNode) AttributeIfc(org.opendaylight.controller.config.yangjmxgenerator.attribute.AttributeIfc)

Example 3 with AttributeIfc

use of org.opendaylight.controller.config.yangjmxgenerator.attribute.AttributeIfc in project controller by opendaylight.

the class RuntimeBeanEntry method extractSubtree.

/**
 * Get direct descendants of this subtree, together with attributes defined
 * in subtree.
 */
private static AttributesRpcsAndRuntimeBeans extractSubtree(final String packageName, final DataNodeContainer subtree, final TypeProviderWrapper typeProviderWrapper, final Module currentModule, final SchemaContext ctx) {
    Multimap<QName, RpcDefinition> identitiesToRpcs = getIdentitiesToRpcs(ctx);
    List<AttributeIfc> attributes = Lists.newArrayList();
    List<RuntimeBeanEntry> runtimeBeanEntries = new ArrayList<>();
    for (DataSchemaNode child : subtree.getChildNodes()) {
        // runtime beans
        if (child instanceof LeafSchemaNode) {
            // just save the attribute
            LeafSchemaNode leaf = (LeafSchemaNode) child;
            attributes.add(new JavaAttribute(leaf, typeProviderWrapper));
        } else if (child instanceof ContainerSchemaNode) {
            ContainerSchemaNode container = (ContainerSchemaNode) child;
            // this can be either TO or hierarchical RB
            TOAttribute toAttribute = TOAttribute.create(container, typeProviderWrapper, packageName);
            attributes.add(toAttribute);
        } else if (child instanceof ListSchemaNode) {
            if (isInnerStateBean(child)) {
                ListSchemaNode listSchemaNode = (ListSchemaNode) child;
                RuntimeBeanEntry hierarchicalChild = createHierarchical(packageName, listSchemaNode, typeProviderWrapper, currentModule, ctx);
                runtimeBeanEntries.add(hierarchicalChild);
            } else /* ordinary list attribute */
            {
                ListAttribute listAttribute = ListAttribute.create((ListSchemaNode) child, typeProviderWrapper, packageName);
                attributes.add(listAttribute);
            }
        } else if (child instanceof LeafListSchemaNode) {
            ListAttribute listAttribute = ListAttribute.create((LeafListSchemaNode) child, typeProviderWrapper);
            attributes.add(listAttribute);
        } else {
            throw new IllegalStateException("Unexpected running-data node " + child);
        }
    }
    Set<Rpc> rpcs = new HashSet<>();
    SchemaNode subtreeSchemaNode = (SchemaNode) subtree;
    for (UnknownSchemaNode unknownSchemaNode : subtreeSchemaNode.getUnknownSchemaNodes()) {
        if (ConfigConstants.RPC_CONTEXT_INSTANCE_EXTENSION_QNAME.equals(unknownSchemaNode.getNodeType())) {
            String localIdentityName = unknownSchemaNode.getNodeParameter();
            QName identityQName = unknownSchemaNode.isAddedByUses() ? findQNameFromGrouping(subtree, ctx, unknownSchemaNode, localIdentityName) : QName.create(currentModule.getNamespace(), currentModule.getRevision(), localIdentityName);
            // convert RpcDefinition to Rpc
            for (RpcDefinition rpcDefinition : identitiesToRpcs.get(identityQName)) {
                String name = TypeProviderWrapper.findJavaParameter(rpcDefinition);
                AttributeIfc returnType;
                if (rpcDefinition.getOutput() == null || rpcDefinition.getOutput().getChildNodes().isEmpty()) {
                    returnType = VoidAttribute.getInstance();
                } else if (rpcDefinition.getOutput().getChildNodes().size() == 1) {
                    DataSchemaNode returnDSN = rpcDefinition.getOutput().getChildNodes().iterator().next();
                    returnType = getReturnTypeAttribute(returnDSN, typeProviderWrapper, packageName);
                } else {
                    throw new IllegalArgumentException("More than one child node in rpc output is not supported. " + "Error occured in " + rpcDefinition);
                }
                List<JavaAttribute> parameters = new ArrayList<>();
                for (DataSchemaNode childNode : sortAttributes(rpcDefinition.getInput().getChildNodes())) {
                    if (childNode.isAddedByUses() == false) {
                        // skip
                        // refined
                        // context-instance
                        checkArgument(childNode instanceof LeafSchemaNode, "Unexpected type of rpc input type. " + "Currently only leafs and empty output nodes are supported, got " + childNode);
                        JavaAttribute javaAttribute = new JavaAttribute((LeafSchemaNode) childNode, typeProviderWrapper);
                        parameters.add(javaAttribute);
                    }
                }
                Rpc newRpc = new Rpc(returnType, name, rpcDefinition.getQName().getLocalName(), parameters);
                rpcs.add(newRpc);
            }
        }
    }
    return new AttributesRpcsAndRuntimeBeans(runtimeBeanEntries, attributes, rpcs);
}
Also used : RpcDefinition(org.opendaylight.yangtools.yang.model.api.RpcDefinition) LeafListSchemaNode(org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode) ArrayList(java.util.ArrayList) TOAttribute(org.opendaylight.controller.config.yangjmxgenerator.attribute.TOAttribute) ContainerSchemaNode(org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode) UnknownSchemaNode(org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode) JavaAttribute(org.opendaylight.controller.config.yangjmxgenerator.attribute.JavaAttribute) HashSet(java.util.HashSet) DataSchemaNode(org.opendaylight.yangtools.yang.model.api.DataSchemaNode) QName(org.opendaylight.yangtools.yang.common.QName) LeafSchemaNode(org.opendaylight.yangtools.yang.model.api.LeafSchemaNode) LeafListSchemaNode(org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode) UnknownSchemaNode(org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode) ListSchemaNode(org.opendaylight.yangtools.yang.model.api.ListSchemaNode) ContainerSchemaNode(org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode) SchemaNode(org.opendaylight.yangtools.yang.model.api.SchemaNode) DataSchemaNode(org.opendaylight.yangtools.yang.model.api.DataSchemaNode) LeafSchemaNode(org.opendaylight.yangtools.yang.model.api.LeafSchemaNode) LeafListSchemaNode(org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode) ListSchemaNode(org.opendaylight.yangtools.yang.model.api.ListSchemaNode) AttributeIfc(org.opendaylight.controller.config.yangjmxgenerator.attribute.AttributeIfc) ListAttribute(org.opendaylight.controller.config.yangjmxgenerator.attribute.ListAttribute)

Example 4 with AttributeIfc

use of org.opendaylight.controller.config.yangjmxgenerator.attribute.AttributeIfc in project controller by opendaylight.

the class ModuleMXBeanEntryTemplatesTest method mockMbe.

public static ModuleMXBeanEntry mockMbe(final String packageName) {
    final ModuleMXBeanEntry mbe = mock(ModuleMXBeanEntry.class);
    final Map<String, AttributeIfc> a = Maps.newHashMap();
    final JavaAttribute attr = mockJavaAttr();
    a.put("attr1", attr);
    doReturn(a).when(mbe).getAttributes();
    doReturn(packageName).when(mbe).getPackageName();
    doReturn(Collections.emptyMap()).when(mbe).getProvidedServices();
    doReturn("yang-module").when(mbe).getYangModuleName();
    doReturn("local").when(mbe).getYangModuleLocalname();
    doReturn("AbstractType").when(mbe).getAbstractFactoryName();
    doReturn("Module").when(mbe).getStubModuleName();
    doReturn("fullA").when(mbe).getFullyQualifiedName(anyString());
    doReturn("uniq").when(mbe).getGloballyUniqueName();
    return mbe;
}
Also used : ModuleMXBeanEntry(org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry) AttributeIfc(org.opendaylight.controller.config.yangjmxgenerator.attribute.AttributeIfc) Matchers.anyString(org.mockito.Matchers.anyString) JavaAttribute(org.opendaylight.controller.config.yangjmxgenerator.attribute.JavaAttribute)

Example 5 with AttributeIfc

use of org.opendaylight.controller.config.yangjmxgenerator.attribute.AttributeIfc in project controller by opendaylight.

the class TemplateFactory method tOsFromRbe.

public static Map<String, GeneralClassTemplate> tOsFromRbe(final RuntimeBeanEntry rbe) {
    final Map<String, GeneralClassTemplate> retVal = Maps.newHashMap();
    final TOAttributesProcessor processor = new TOAttributesProcessor();
    final Map<String, AttributeIfc> yangPropertiesToTypesMap = Maps.newHashMap(rbe.getYangPropertiesToTypesMap());
    // Add TOs from output parameters
    for (final Rpc rpc : rbe.getRpcs()) {
        final AttributeIfc returnType = rpc.getReturnType();
        if (returnType == VoidAttribute.getInstance()) {
            continue;
        }
        if (returnType instanceof JavaAttribute) {
            continue;
        }
        if (returnType instanceof ListAttribute && returnType.getOpenType() instanceof SimpleType) {
            continue;
        }
        Preconditions.checkState(!yangPropertiesToTypesMap.containsKey(returnType.getAttributeYangName()), "Duplicate TO %s for %s", returnType.getAttributeYangName(), rbe);
        yangPropertiesToTypesMap.put(returnType.getAttributeYangName(), returnType);
    }
    processor.processAttributes(yangPropertiesToTypesMap);
    for (final org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.TemplateFactory.TOAttributesProcessor.TOInternal to : processor.getTOs()) {
        final List<Constructor> constructors = Lists.newArrayList();
        constructors.add(new Constructor(to.getName(), "super();"));
        // TODO header
        retVal.put(to.getType(), new GeneralClassTemplate(null, rbe.getPackageName(), to.getName(), Collections.<String>emptyList(), Collections.<String>emptyList(), to.getFields(), to.getMethods(), false, false, constructors));
    }
    return retVal;
}
Also used : Rpc(org.opendaylight.controller.config.yangjmxgenerator.RuntimeBeanEntry.Rpc) Constructor(org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.Constructor) SimpleType(javax.management.openmbean.SimpleType) AttributeIfc(org.opendaylight.controller.config.yangjmxgenerator.attribute.AttributeIfc) JavaAttribute(org.opendaylight.controller.config.yangjmxgenerator.attribute.JavaAttribute) ListAttribute(org.opendaylight.controller.config.yangjmxgenerator.attribute.ListAttribute)

Aggregations

AttributeIfc (org.opendaylight.controller.config.yangjmxgenerator.attribute.AttributeIfc)18 ListAttribute (org.opendaylight.controller.config.yangjmxgenerator.attribute.ListAttribute)7 JavaAttribute (org.opendaylight.controller.config.yangjmxgenerator.attribute.JavaAttribute)6 TOAttribute (org.opendaylight.controller.config.yangjmxgenerator.attribute.TOAttribute)6 HashMap (java.util.HashMap)4 QName (org.opendaylight.yangtools.yang.common.QName)4 OpenType (javax.management.openmbean.OpenType)3 Test (org.junit.Test)3 DataSchemaNode (org.opendaylight.yangtools.yang.model.api.DataSchemaNode)3 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2 SimpleType (javax.management.openmbean.SimpleType)2 ModuleMXBeanEntry (org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry)2 RuntimeBeanEntry (org.opendaylight.controller.config.yangjmxgenerator.RuntimeBeanEntry)2 Rpc (org.opendaylight.controller.config.yangjmxgenerator.RuntimeBeanEntry.Rpc)2 DependencyAttribute (org.opendaylight.controller.config.yangjmxgenerator.attribute.DependencyAttribute)2 ListDependenciesAttribute (org.opendaylight.controller.config.yangjmxgenerator.attribute.ListDependenciesAttribute)2 IdentitySchemaNode (org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode)2