use of org.opendaylight.controller.config.yangjmxgenerator.RuntimeBeanEntry in project controller by opendaylight.
the class TemplateFactory method abstractModuleTemplateFromMbe.
public static AbstractModuleTemplate abstractModuleTemplateFromMbe(final ModuleMXBeanEntry mbe) {
final AbstractModuleAttributesProcessor attrProcessor = new AbstractModuleAttributesProcessor(mbe.getAttributes());
final List<ModuleField> moduleFields = attrProcessor.getModuleFields();
final List<String> implementedIfcs = Lists.newArrayList(mbe.getFullyQualifiedName(mbe.getMXBeanInterfaceName()));
for (final String implementedService : mbe.getProvidedServices().keySet()) {
implementedIfcs.add(implementedService);
}
boolean generateRuntime = false;
String registratorFullyQualifiedName = null;
if (mbe.getRuntimeBeans() != null && !mbe.getRuntimeBeans().isEmpty()) {
generateRuntime = true;
final RuntimeBeanEntry rootEntry = RuntimeRegistratorFtlTemplate.findRoot(mbe.getRuntimeBeans());
registratorFullyQualifiedName = rootEntry.getPackageName().concat(".").concat(RuntimeRegistratorFtlTemplate.getJavaNameOfRuntimeRegistrator(rootEntry));
implementedIfcs.add(RuntimeBeanRegistratorAwareModule.class.getCanonicalName());
}
final List<String> extendedClasses = Collections.singletonList(AbstractModule.class.getCanonicalName() + "<" + mbe.getAbstractModuleName() + ">");
final AbstractModuleTemplate abstractModuleTemplate = new AbstractModuleTemplate(getHeaderFromEntry(mbe), mbe.getPackageName(), mbe.getAbstractModuleName(), extendedClasses, implementedIfcs, moduleFields, attrProcessor.getMethods(), generateRuntime, registratorFullyQualifiedName);
if (mbe.getNullableDescription() != null) {
abstractModuleTemplate.getAnnotations().add(Annotation.createDescriptionAnnotation(mbe.getNullableDescription()));
}
return abstractModuleTemplate;
}
use of org.opendaylight.controller.config.yangjmxgenerator.RuntimeBeanEntry in project controller by opendaylight.
the class InstanceConfig method getMappedConfiguration.
@SuppressWarnings("IllegalCatch")
private Map<String, Object> getMappedConfiguration(final ObjectName on, final EnumResolver enumResolver) {
// TODO make field, mappingStrategies can be instantiated only once
Map<String, AttributeMappingStrategy<?, ? extends OpenType<?>>> mappingStrategies = new ObjectMapper().prepareMapping(jmxToAttrConfig, enumResolver);
Map<String, Object> toXml = Maps.newHashMap();
for (Entry<String, AttributeIfc> configDefEntry : jmxToAttrConfig.entrySet()) {
// Skip children runtime beans as they are mapped by InstanceRuntime
if (configDefEntry.getValue() instanceof RuntimeBeanEntry) {
continue;
}
Object value = configRegistryClient.getAttributeCurrentValue(on, configDefEntry.getKey());
try {
AttributeMappingStrategy<?, ? extends OpenType<?>> attributeMappingStrategy = mappingStrategies.get(configDefEntry.getKey());
Optional<?> attribute = attributeMappingStrategy.mapAttribute(value);
if (!attribute.isPresent()) {
continue;
}
toXml.put(configDefEntry.getValue().getAttributeYangName(), attribute.get());
} catch (final RuntimeException e) {
throw new IllegalStateException("Unable to map value " + value + " to attribute " + configDefEntry.getKey(), e);
}
}
return toXml;
}
use of org.opendaylight.controller.config.yangjmxgenerator.RuntimeBeanEntry in project controller by opendaylight.
the class ConfigSubsystemFacade method createModuleRuntimes.
private Map<String, Map<String, ModuleRuntime>> createModuleRuntimes(final ConfigRegistryClient client, final Map<String, Map<String, ModuleMXBeanEntry>> mbeanentries) {
Map<String, Map<String, ModuleRuntime>> retVal = new HashMap<>();
for (Map.Entry<String, Map<String, ModuleMXBeanEntry>> namespaceToModuleEntry : mbeanentries.entrySet()) {
Map<String, ModuleRuntime> innerMap = new HashMap<>();
Map<String, ModuleMXBeanEntry> entriesFromNamespace = namespaceToModuleEntry.getValue();
for (Map.Entry<String, ModuleMXBeanEntry> moduleToMXEntry : entriesFromNamespace.entrySet()) {
ModuleMXBeanEntry mbe = moduleToMXEntry.getValue();
Map<RuntimeBeanEntry, InstanceConfig> cache = new HashMap<>();
RuntimeBeanEntry root = null;
for (RuntimeBeanEntry rbe : mbe.getRuntimeBeans()) {
cache.put(rbe, new InstanceConfig(client, rbe.getYangPropertiesToTypesMap(), mbe.getNullableDummyContainerName()));
if (rbe.isRoot()) {
root = rbe;
}
}
if (root == null) {
continue;
}
InstanceRuntime rootInstanceRuntime = createInstanceRuntime(root, cache);
ModuleRuntime moduleRuntime = new ModuleRuntime(rootInstanceRuntime);
innerMap.put(moduleToMXEntry.getKey(), moduleRuntime);
}
retVal.put(namespaceToModuleEntry.getKey(), innerMap);
}
return retVal;
}
use of org.opendaylight.controller.config.yangjmxgenerator.RuntimeBeanEntry in project controller by opendaylight.
the class RpcFacade method mapRpcs.
public Rpcs mapRpcs() {
final Map<String, Map<String, ModuleRpcs>> map = new HashMap<>();
for (final Map.Entry<String, Map<String, ModuleMXBeanEntry>> namespaceToModuleEntry : yangStoreService.getModuleMXBeanEntryMap().entrySet()) {
Map<String, ModuleRpcs> namespaceToModules = map.computeIfAbsent(namespaceToModuleEntry.getKey(), k -> new HashMap<>());
for (final Map.Entry<String, ModuleMXBeanEntry> moduleEntry : namespaceToModuleEntry.getValue().entrySet()) {
ModuleRpcs rpcMapping = namespaceToModules.computeIfAbsent(moduleEntry.getKey(), k -> new ModuleRpcs(yangStoreService.getEnumResolver()));
final ModuleMXBeanEntry entry = moduleEntry.getValue();
for (final RuntimeBeanEntry runtimeEntry : entry.getRuntimeBeans()) {
rpcMapping.addNameMapping(runtimeEntry);
for (final RuntimeBeanEntry.Rpc rpc : runtimeEntry.getRpcs()) {
rpcMapping.addRpc(runtimeEntry, rpc);
}
}
}
}
return new Rpcs(map);
}
use of org.opendaylight.controller.config.yangjmxgenerator.RuntimeBeanEntry in project controller by opendaylight.
the class RuntimeRegistratorFtlTemplate method createRegistrationHierarchy.
// TODO move to factory + RuntimeBeanEntry
/**
* Create ftls representing registrations. First registration is represents
* parent.
*
* @return map containing java class name as key, instance representing the
* java file as value.
*/
private static LinkedHashMap<String, RuntimeRegistratorFtlTemplate> createRegistrationHierarchy(RuntimeBeanEntry parent, Set<String> occupiedKeys) {
LinkedHashMap<String, RuntimeRegistratorFtlTemplate> unorderedResult = new LinkedHashMap<>();
List<MethodDefinition> methods = new ArrayList<>();
// leaf list>: key or counter
if (occupiedKeys.contains(parent.getJavaNamePrefix())) {
throw new IllegalArgumentException("Name conflict in runtime bean hierarchy - java name found more than " + "once. Consider using java-name extension. Conflicting name: " + parent.getJavaNamePrefix());
}
Set<String> currentOccupiedKeys = new HashSet<>(occupiedKeys);
currentOccupiedKeys.add(parent.getJavaNamePrefix());
Field registratorsMapField = new Field(Collections.singletonList(Modifier.FINAL), TypeHelper.getGenericType(Map.class, String.class, AtomicInteger.class), "unkeyedMap", "new " + TypeHelper.getGenericType(HashMap.class, String.class, AtomicInteger.class) + "()");
// create register methods for children
for (RuntimeBeanEntry child : parent.getChildren()) {
checkArgument(parent.getPackageName().equals(child.getPackageName()), "Invalid package name");
// call itself recursively to generate child
// registrators/registrations
LinkedHashMap<String, RuntimeRegistratorFtlTemplate> childRegistratorMap = createRegistrationHierarchy(child, currentOccupiedKeys);
for (Entry<String, RuntimeRegistratorFtlTemplate> entry : childRegistratorMap.entrySet()) {
if (unorderedResult.containsKey(entry.getKey())) {
throw new IllegalStateException("Conflicting name found while generating runtime registration:" + entry.getKey());
}
unorderedResult.put(entry.getKey(), entry.getValue());
}
if (!childRegistratorMap.isEmpty()) {
// first entry is the direct descendant according to the create
// contract
RuntimeRegistratorFtlTemplate childRegistrator = childRegistratorMap.values().iterator().next();
StringBuilder body = new StringBuilder();
String key, value;
key = child.getJavaNamePrefix();
body.append(format("String key = \"%s\"; //TODO: check for conflicts\n", key));
Optional<String> childKeyJavaName = child.getKeyJavaName();
if (childKeyJavaName.isPresent()) {
value = "bean.get" + childKeyJavaName.get() + "()";
value = "String.valueOf(" + value + ")";
} else {
body.append("java.util.concurrent.atomic.AtomicInteger counter = unkeyedMap.get(key);\n" + "if (counter==null){\n" + "counter = new java.util.concurrent.atomic.AtomicInteger();\n" + "unkeyedMap.put(key, counter);\n" + "}\n");
value = "String.valueOf(counter.incrementAndGet())";
}
body.append(format("String value = %s;\n", value));
body.append(format("%s r = %s.register(key, value, bean);\n", HierarchicalRuntimeBeanRegistration.class.getCanonicalName(), hierachicalRegistration.getName()));
body.append(format("return new %s(r);", childRegistrator.getFullyQualifiedName()));
Field param = new Field(Collections.singletonList(Modifier.FINAL), child.getJavaNameOfRuntimeMXBean(), "bean");
MethodDefinition register = new MethodDefinition(Collections.singletonList(Modifier.SYNCHRONIZED), childRegistrator.getFullyQualifiedName(), "register", Collections.singletonList(param), Collections.emptyList(), Collections.emptyList(), body.toString());
methods.add(register);
}
}
// create parent registration
String createdName = getJavaNameOfRuntimeRegistration(parent.getJavaNamePrefix());
List<Field> constructorParameters = Collections.singletonList(hierachicalRegistration);
String constructorBody = constructConstructorBody(constructorParameters);
MethodDefinition constructor = MethodDefinition.createConstructor(createdName, constructorParameters, constructorBody);
MethodDefinition closeRegistrator = createCloseMethodToCloseField(hierachicalRegistration);
methods.add(closeRegistrator);
methods.add(constructor);
List<Field> privateFields = Lists.newArrayList(registratorsMapField);
privateFields.addAll(constructorParameters);
RuntimeRegistratorFtlTemplate created = new RuntimeRegistratorFtlTemplate(parent, createdName, privateFields, methods);
LinkedHashMap<String, RuntimeRegistratorFtlTemplate> result = new LinkedHashMap<>();
result.put(created.getTypeDeclaration().getName(), created);
checkState(!unorderedResult.containsKey(created.getTypeDeclaration().getName()), "Naming conflict: " + created.getTypeDeclaration().getName());
result.putAll(unorderedResult);
return result;
}
Aggregations