Search in sources :

Example 1 with IdentitySchemaNode

use of org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode in project controller by opendaylight.

the class ModuleMXBeanEntryBuilder method getIdentityMap.

private Map<String, IdentitySchemaNode> getIdentityMap() {
    Map<String, IdentitySchemaNode> moduleIdentities = Maps.newHashMap();
    for (IdentitySchemaNode id : currentModule.getIdentities()) {
        if (!id.getBaseIdentities().isEmpty() && ConfigConstants.MODULE_TYPE_Q_NAME.equals(id.getBaseIdentities().iterator().next().getQName())) {
            String identityLocalName = id.getQName().getLocalName();
            if (moduleIdentities.containsKey(identityLocalName)) {
                throw new IllegalStateException("Module name already defined in this currentModule: " + identityLocalName);
            } else {
                moduleIdentities.put(identityLocalName, id);
                LOG.debug("Found identity {}", identityLocalName);
            }
            // validation check on unknown schema nodes
            boolean providedServiceWasSet = false;
            for (UnknownSchemaNode unknownNode : id.getUnknownSchemaNodes()) {
                // TODO: test this
                boolean unknownNodeIsProvidedServiceExtension = ConfigConstants.PROVIDED_SERVICE_EXTENSION_QNAME.equals(unknownNode.getNodeType());
                if (ConfigConstants.JAVA_NAME_PREFIX_EXTENSION_QNAME.equals(unknownNode.getNodeType())) {
                    // 0..1 allowed
                    checkState(providedServiceWasSet == false, format("More than one language extension %s is not allowed here: %s", ConfigConstants.JAVA_NAME_PREFIX_EXTENSION_QNAME, id));
                    providedServiceWasSet = true;
                } else if (unknownNodeIsProvidedServiceExtension == false) {
                    throw new IllegalStateException("Unexpected language extension " + unknownNode.getNodeType());
                }
            }
        }
    }
    return moduleIdentities;
}
Also used : UnknownSchemaNode(org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode) IdentitySchemaNode(org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode)

Example 2 with IdentitySchemaNode

use of org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode 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 3 with IdentitySchemaNode

use of org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode in project controller by opendaylight.

the class ServiceInterfaceEntry method create.

/**
 * @return Map of QNames as keys and ServiceInterfaceEntry instances as
 *         values
 */
public static Map<QName, ServiceInterfaceEntry> create(final Module currentModule, final String packageName, final Map<IdentitySchemaNode, ServiceInterfaceEntry> definedSEItracker) {
    LOG.debug("Generating ServiceInterfaces from {} to package {}", currentModule.getNamespace(), packageName);
    Map<IdentitySchemaNode, ServiceInterfaceEntry> identitiesToSIs = new HashMap<>();
    Set<IdentitySchemaNode> notVisited = new HashSet<>(currentModule.getIdentities());
    int lastSize = notVisited.size() + 1;
    while (!notVisited.isEmpty()) {
        if (notVisited.size() == lastSize) {
            LOG.debug("Following identities will be ignored while generating ServiceInterfaces, as they are not derived from {} : {}", SERVICE_TYPE_Q_NAME, notVisited);
            break;
        }
        lastSize = notVisited.size();
        for (Iterator<IdentitySchemaNode> iterator = notVisited.iterator(); iterator.hasNext(); ) {
            IdentitySchemaNode identity = iterator.next();
            ServiceInterfaceEntry created = null;
            if (identity.getBaseIdentities().isEmpty()) {
                // the identity
                continue;
            } else if (identity.getBaseIdentities().iterator().next().getQName().equals(SERVICE_TYPE_Q_NAME)) {
                // this is a base type
                created = new ServiceInterfaceEntry(identity, packageName, ModuleUtil.getQName(currentModule));
            } else {
                ServiceInterfaceEntry foundBase = definedSEItracker.get(identity.getBaseIdentities().iterator().next());
                // derived type, did we convert the parent?
                if (foundBase != null) {
                    created = new ServiceInterfaceEntry(Optional.of(foundBase), identity, packageName, ModuleUtil.getQName(currentModule));
                }
            }
            if (created != null) {
                created.setYangModuleName(currentModule.getName());
                // TODO how to get local name
                created.setYangModuleLocalname(identity.getQName().getLocalName());
                identitiesToSIs.put(identity, created);
                definedSEItracker.put(identity, created);
                iterator.remove();
            }
        }
    }
    // create result map
    Map<QName, ServiceInterfaceEntry> resultMap = new HashMap<>();
    for (ServiceInterfaceEntry sie : identitiesToSIs.values()) {
        resultMap.put(sie.getQName(), sie);
    }
    LOG.debug("Number of ServiceInterfaces to be generated: {}", resultMap.size());
    return resultMap;
}
Also used : HashMap(java.util.HashMap) QName(org.opendaylight.yangtools.yang.common.QName) IdentitySchemaNode(org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode) HashSet(java.util.HashSet)

Example 4 with IdentitySchemaNode

use of org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode in project controller by opendaylight.

the class ServiceInterfaceEntryTest method testCreateFromIdentities.

@Test
public void testCreateFromIdentities() {
    // each identity has to have a base that leads to service-type
    Map<IdentitySchemaNode, ServiceInterfaceEntry> definedIdentities = new HashMap<>();
    Map<QName, ServiceInterfaceEntry> namesToSIEntries = ServiceInterfaceEntry.create(threadsModule, PACKAGE_NAME, definedIdentities);
    // expected eventbus, threadfactory, threadpool,
    // scheduled-threadpool,thread-rpc-context
    assertThat(namesToSIEntries.size(), is(expectedSIEFileNames.size()));
    Set<QName> withNoBaseType = Sets.newHashSet(EVENTBUS_QNAME, THREADFACTORY_QNAME, THREADPOOL_QNAME, SCHEDULED_EXECUTOR_SERVICE_QNAME);
    HashSet<QName> withBaseType = new HashSet<>();
    for (Entry<QName, ServiceInterfaceEntry> entry : namesToSIEntries.entrySet()) {
        QName qName = entry.getKey();
        if (withNoBaseType.contains(qName)) {
            ServiceInterfaceEntry sie = namesToSIEntries.get(qName);
            assertNotNull(qName + " not found", sie);
            assertThat(qName + " should have empty base type", sie.getBase().isPresent(), is(false));
            assertThat(sie.getQName(), is(qName));
        } else {
            withBaseType.add(qName);
        }
    }
    // scheduled-threadpool has super type threadpool
    assertThat(withBaseType, is(Sets.newHashSet(SCHEDULED_THREADPOOL_QNAME)));
    assertThat(withBaseType.contains(SCHEDULED_THREADPOOL_QNAME), is(true));
    ServiceInterfaceEntry scheduled = namesToSIEntries.get(SCHEDULED_THREADPOOL_QNAME);
    assertNotNull(scheduled);
    assertThat(scheduled.getQName(), is(SCHEDULED_THREADPOOL_QNAME));
    ServiceInterfaceEntry threadPool = namesToSIEntries.get(THREADPOOL_QNAME);
    assertNotNull(threadPool);
    assertThat("scheduled-threadpool should extend threadpool", scheduled.getBase().get(), is(threadPool));
    assertThat(scheduled.getExportedOsgiClassName(), is(PackageTranslatorTest.EXPECTED_PACKAGE_PREFIX + ".threadpool.ScheduledThreadPool"));
    assertThat(threadPool.getExportedOsgiClassName(), is(PackageTranslatorTest.EXPECTED_PACKAGE_PREFIX + ".threadpool.ThreadPool"));
    String expectedDescription = "An extension of the simple pool of threads able to schedule\n" + "work to be executed at some point in time.";
    assertThat(trimInnerSpacesOrNull(scheduled.getNullableDescription()), is(expectedDescription));
    assertThat(scheduled.getPackageName(), is(PACKAGE_NAME));
    assertThat(scheduled.getTypeName(), is(SCHEDULED_THREADPOOL_INTERFACE_NAME));
    assertThat(scheduled.getFullyQualifiedName(), is(PACKAGE_NAME + "." + SCHEDULED_THREADPOOL_INTERFACE_NAME));
}
Also used : HashMap(java.util.HashMap) QName(org.opendaylight.yangtools.yang.common.QName) IdentitySchemaNode(org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 5 with IdentitySchemaNode

use of org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode in project controller by opendaylight.

the class AbstractYangTest method mapIdentitiesByQNames.

public Map<QName, IdentitySchemaNode> mapIdentitiesByQNames(final Module module) {
    final Map<QName, IdentitySchemaNode> result = new HashMap<>();
    for (final IdentitySchemaNode identitySchemaNode : module.getIdentities()) {
        final QName qName = identitySchemaNode.getQName();
        Preconditions.checkArgument(result.containsKey(qName) == false, "Two identities of %s contain same qname %s", module, qName);
        result.put(qName, identitySchemaNode);
    }
    return result;
}
Also used : HashMap(java.util.HashMap) QName(org.opendaylight.yangtools.yang.common.QName) IdentitySchemaNode(org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode)

Aggregations

IdentitySchemaNode (org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode)12 QName (org.opendaylight.yangtools.yang.common.QName)9 HashMap (java.util.HashMap)8 Optional (com.google.common.base.Optional)3 Test (org.junit.Test)3 HashSet (java.util.HashSet)2 ConfigConstants.createConfigQName (org.opendaylight.controller.config.yangjmxgenerator.ConfigConstants.createConfigQName)2 AttributeIfc (org.opendaylight.controller.config.yangjmxgenerator.attribute.AttributeIfc)2 TypeProviderImpl (org.opendaylight.mdsal.binding.yang.types.TypeProviderImpl)2 CaseSchemaNode (org.opendaylight.yangtools.yang.model.api.CaseSchemaNode)2 Module (org.opendaylight.yangtools.yang.model.api.Module)2 UnknownSchemaNode (org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode)2 File (java.io.File)1 IOException (java.io.IOException)1 Map (java.util.Map)1 Set (java.util.Set)1 Matcher (java.util.regex.Matcher)1 SimpleType (javax.management.openmbean.SimpleType)1 IdentityMapping (org.opendaylight.controller.config.facade.xml.mapping.IdentityMapping)1 ModuleFactory (org.opendaylight.controller.config.spi.ModuleFactory)1