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;
}
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);
}
}
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;
}
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));
}
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;
}
Aggregations