use of org.opendaylight.mdsal.binding.yang.types.TypeProviderImpl in project controller by opendaylight.
the class JMXGenerator method generateSources.
@Override
public Collection<File> generateSources(final SchemaContext context, final File outputBaseDir, final Set<Module> currentModules, final Function<Module, Optional<String>> moduleResourcePathResolver) {
Preconditions.checkArgument(context != null, "Null context received");
Preconditions.checkArgument(outputBaseDir != null, "Null outputBaseDir received");
Preconditions.checkArgument(this.namespaceToPackageMapping != null && !this.namespaceToPackageMapping.isEmpty(), "No namespace to package mapping provided in additionalConfiguration");
final PackageTranslator packageTranslator = new PackageTranslator(this.namespaceToPackageMapping);
if (!outputBaseDir.exists()) {
outputBaseDir.mkdirs();
}
final GeneratedFilesTracker generatedFiles = new GeneratedFilesTracker();
// create SIE structure qNamesToSIEs
final Map<QName, ServiceInterfaceEntry> qNamesToSIEs = new HashMap<>();
final Map<IdentitySchemaNode, ServiceInterfaceEntry> knownSEITracker = new HashMap<>();
for (final Module module : context.getModules()) {
final String packageName = packageTranslator.getPackageName(module);
final Map<QName, ServiceInterfaceEntry> namesToSIEntries = ServiceInterfaceEntry.create(module, packageName, knownSEITracker);
for (final Entry<QName, ServiceInterfaceEntry> sieEntry : namesToSIEntries.entrySet()) {
// merge value into qNamesToSIEs
if (qNamesToSIEs.put(sieEntry.getKey(), sieEntry.getValue()) != null) {
throw new IllegalStateException("Cannot add two SIE with same qname " + sieEntry.getValue());
}
}
if (currentModules.contains(module)) {
// write this sie to disk
for (final ServiceInterfaceEntry sie : namesToSIEntries.values()) {
try {
generatedFiles.addFile(this.codeWriter.writeSie(sie, outputBaseDir));
} catch (final Exception e) {
throw new RuntimeException("Error occurred during SIE source generate phase", e);
}
}
}
}
final File mainBaseDir = concatFolders(this.projectBaseDir, "src", "main", "java");
Preconditions.checkNotNull(this.resourceBaseDir, "resource base dir attribute was null");
final StringBuilder fullyQualifiedNamesOfFactories = new StringBuilder();
// create MBEs
for (final Module module : currentModules) {
final String packageName = packageTranslator.getPackageName(module);
final Map<String, ModuleMXBeanEntry> /* MB identity local name */
namesToMBEs = ModuleMXBeanEntry.create(module, qNamesToSIEs, context, new TypeProviderWrapper(new TypeProviderImpl(context)), packageName);
for (final Entry<String, ModuleMXBeanEntry> mbeEntry : namesToMBEs.entrySet()) {
final ModuleMXBeanEntry mbe = mbeEntry.getValue();
try {
final List<File> files1 = this.codeWriter.writeMbe(mbe, outputBaseDir, mainBaseDir);
generatedFiles.addFile(files1);
} catch (final Exception e) {
throw new RuntimeException("Error occurred during MBE source generate phase", e);
}
fullyQualifiedNamesOfFactories.append(mbe.getFullyQualifiedName(mbe.getStubFactoryName()));
fullyQualifiedNamesOfFactories.append("\n");
}
}
// create ModuleFactory file if needed
if (fullyQualifiedNamesOfFactories.length() > 0 && this.generateModuleFactoryFile) {
final File serviceLoaderFile = JMXGenerator.concatFolders(this.resourceBaseDir, "META-INF", "services", ModuleFactory.class.getName());
// if this file does not exist, create empty file
serviceLoaderFile.getParentFile().mkdirs();
try {
serviceLoaderFile.createNewFile();
Files.asCharSink(serviceLoaderFile, StandardCharsets.UTF_8).write(fullyQualifiedNamesOfFactories.toString());
} catch (final IOException e) {
final String message = "Cannot write to " + serviceLoaderFile;
LOG.error(message, e);
throw new RuntimeException(message, e);
}
}
return generatedFiles.getFiles();
}
use of org.opendaylight.mdsal.binding.yang.types.TypeProviderImpl in project controller by opendaylight.
the class ModuleMXBeanEntryNameConflictTest method testNameConflicts.
@Ignore
@Test
public void testNameConflicts() {
for (final String moduleName : testedModules) {
final Module testedModule = loadYangs(moduleName, moduleName);
try {
LOG.debug("Testing {}", moduleName);
ModuleMXBeanEntry.create(testedModule, new HashMap<>(), this.context, new TypeProviderWrapper(new TypeProviderImpl(this.context)), PACKAGE_NAME);
fail(moduleName + " did not cause a name conflict and should");
} catch (final NameConflictException e) {
assertEquals(this.testedYangModulesToExpectedConflictingName.get(moduleName), e.getConflictingName());
}
}
}
use of org.opendaylight.mdsal.binding.yang.types.TypeProviderImpl in project controller by opendaylight.
the class ModuleMXBeanEntryTest method test_jmxImplModule.
@Test
public void test_jmxImplModule() {
final Map<IdentitySchemaNode, ServiceInterfaceEntry> identitiesToSIs = new HashMap<>();
final Map<QName, ServiceInterfaceEntry> modulesToSIEs = ServiceInterfaceEntry.create(this.threadsModule, PACKAGE_NAME, identitiesToSIs);
modulesToSIEs.putAll(ServiceInterfaceEntry.create(this.jmxModule, PACKAGE_NAME, identitiesToSIs));
final Map<String, ModuleMXBeanEntry> /* identity local name */
namesToMBEs = ModuleMXBeanEntry.create(this.jmxImplModule, modulesToSIEs, this.context, new TypeProviderWrapper(new TypeProviderImpl(this.context)), PACKAGE_NAME);
final Map<String, AttributeIfc> attributes = namesToMBEs.get("impl-netconf").getAttributes();
assertCorrectAttributesSize(namesToMBEs, attributes);
//
final DependencyAttribute threadFactoryAttribute = (DependencyAttribute) attributes.get("thread-factory");
assertNotNull(threadFactoryAttribute);
assertFalse(threadFactoryAttribute.getDependency().isMandatory());
assertThat(threadFactoryAttribute.getDependency().getSie().getTypeName(), is("ThreadFactoryServiceInterface"));
assertThat(threadFactoryAttribute.getAttributeYangName(), is("thread-factory"));
assertThat(threadFactoryAttribute.getLowerCaseCammelCase(), is("threadFactory"));
assertThat(threadFactoryAttribute.getUpperCaseCammelCase(), is("ThreadFactory"));
assertThat(threadFactoryAttribute.getOpenType(), isA(SimpleType.class));
assertNull(threadFactoryAttribute.getNullableDefault());
assertNull(threadFactoryAttribute.getNullableDescription());
assertThat(threadFactoryAttribute.getType().getName(), is("ObjectName"));
}
use of org.opendaylight.mdsal.binding.yang.types.TypeProviderImpl in project controller by opendaylight.
the class RuntimeBeanEntryTest method createRuntimeBean.
@Test
public void createRuntimeBean() {
final CaseSchemaNode caseNode = Mockito.mock(CaseSchemaNode.class);
doReturn(new HashSet<LeafSchemaNode>()).when(caseNode).getChildNodes();
doReturn(new ArrayList<UnknownSchemaNode>()).when(caseNode).getUnknownSchemaNodes();
final Map<String, RuntimeBeanEntry> runtimeBeans = RuntimeBeanEntry.extractClassNameToRuntimeBeanMap(PACKAGE_NAME, caseNode, "test-name", new TypeProviderWrapper(new TypeProviderImpl(this.context)), "test", this.jmxImplModule, this.context);
assertEquals(1, runtimeBeans.size());
final RuntimeBeanEntry runtimeMXBean = runtimeBeans.get("testRuntimeMXBean");
assertTrue(runtimeMXBean.isRoot());
assertEquals("test-name", runtimeMXBean.getYangName());
}
use of org.opendaylight.mdsal.binding.yang.types.TypeProviderImpl in project controller by opendaylight.
the class RuntimeBeanEntryTest method runtimeBeanRPCTest.
@Test
public void runtimeBeanRPCTest() {
// create service interfaces
final Map<QName, ServiceInterfaceEntry> modulesToSIEs = ServiceInterfaceEntry.create(this.threadsModule, "packages.sis", identitiesToSIs);
assertNotNull(modulesToSIEs);
// create MXBeans map
final Map<String, ModuleMXBeanEntry> namesToMBEs = ModuleMXBeanEntry.create(this.threadsJavaModule, modulesToSIEs, this.context, new TypeProviderWrapper(new TypeProviderImpl(this.context)), PACKAGE_NAME);
assertFalse(namesToMBEs.isEmpty());
// get threadfactory-naming bean
final ModuleMXBeanEntry threadfactoryNamingMXBean = namesToMBEs.get(THREADFACTORY_NAMING_MXB_NAME);
assertNotNull(threadfactoryNamingMXBean);
// get runtime beans
final Collection<RuntimeBeanEntry> runtimeBeanEntries = threadfactoryNamingMXBean.getRuntimeBeans();
assertFalse(runtimeBeanEntries.isEmpty());
// get root runtime bean
final RuntimeBeanEntry threadfactoryRuntimeBeanEntry = getRuntimeBeanEntryByJavaName(runtimeBeanEntries, "NamingThreadFactoryRuntimeMXBean");
assertNotNull(threadfactoryRuntimeBeanEntry);
assertTrue(threadfactoryRuntimeBeanEntry.isRoot());
// get thread runtime bean
final RuntimeBeanEntry runtimeBeanEntry = getRuntimeBeanEntryByJavaName(runtimeBeanEntries, THREAD_RUNTIME_BEAN_JAVA_NAME);
assertNotNull(runtimeBeanEntry);
// test thread runtime bean properties
assertEquals(THREAD_RUNTIME_BEAN_JAVA_PREFIX, runtimeBeanEntry.getJavaNamePrefix());
assertEquals(PACKAGE_NAME, runtimeBeanEntry.getPackageName());
assertEquals(PACKAGE_NAME + "." + THREAD_RUNTIME_BEAN_JAVA_NAME, runtimeBeanEntry.getFullyQualifiedName(runtimeBeanEntry.getJavaNameOfRuntimeMXBean()));
assertEquals(THREAD_RUNTIME_BEAN_YANG_NAME, runtimeBeanEntry.getYangName());
// get thread runtime bean rpcs
final List<RuntimeBeanEntry.Rpc> rpcs = new ArrayList<>(runtimeBeanEntry.getRpcs());
assertEquals(2, rpcs.size());
// get sleep rpc and test it
final RuntimeBeanEntry.Rpc rpc = getRpcByName(rpcs, SLEEP_RPC_NAME);
assertNotNull(rpc);
assertEquals(SLEEP_RPC_NAME, rpc.getYangName());
assertTrue(((JavaAttribute) rpc.getReturnType()).getType().getFullyQualifiedName().endsWith(SLEEP_RPC_OUTPUT));
// get sleep rpc input attribute and test it
final List<JavaAttribute> attributes = rpc.getParameters();
assertEquals(1, attributes.size());
final JavaAttribute attribute = attributes.get(0);
assertEquals(SLEEP_RPC_INPUT_NAME, attribute.getAttributeYangName());
assertEquals(SLEEP_RPC_INPUT_TYPE, attribute.getType().getName());
assertEquals(SLEEP_RPC_INPUT_NAME, attribute.getLowerCaseCammelCase());
assertEquals("Millis", attribute.getUpperCaseCammelCase());
assertNull(attribute.getNullableDefault());
assertNull(attribute.getNullableDescription());
assertTrue(attribute.getOpenType() instanceof SimpleType);
}
Aggregations