Search in sources :

Example 1 with RuntimeBeanEntry

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;
}
Also used : ModuleField(org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.ModuleField) IdentityRefModuleField(org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.IdentityRefModuleField) RuntimeBeanEntry(org.opendaylight.controller.config.yangjmxgenerator.RuntimeBeanEntry) RuntimeBeanRegistratorAwareModule(org.opendaylight.controller.config.api.RuntimeBeanRegistratorAwareModule)

Example 2 with RuntimeBeanEntry

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;
}
Also used : OpenType(javax.management.openmbean.OpenType) RuntimeBeanEntry(org.opendaylight.controller.config.yangjmxgenerator.RuntimeBeanEntry) AttributeMappingStrategy(org.opendaylight.controller.config.facade.xml.mapping.attributes.mapping.AttributeMappingStrategy) AttributeIfc(org.opendaylight.controller.config.yangjmxgenerator.attribute.AttributeIfc) ObjectMapper(org.opendaylight.controller.config.facade.xml.mapping.attributes.mapping.ObjectMapper)

Example 3 with RuntimeBeanEntry

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;
}
Also used : InstanceRuntime(org.opendaylight.controller.config.facade.xml.runtime.InstanceRuntime) HashMap(java.util.HashMap) RuntimeBeanEntry(org.opendaylight.controller.config.yangjmxgenerator.RuntimeBeanEntry) InstanceConfig(org.opendaylight.controller.config.facade.xml.mapping.config.InstanceConfig) ModuleMXBeanEntry(org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry) ModuleRuntime(org.opendaylight.controller.config.facade.xml.runtime.ModuleRuntime) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with RuntimeBeanEntry

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);
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) RuntimeBeanEntry(org.opendaylight.controller.config.yangjmxgenerator.RuntimeBeanEntry) Rpcs(org.opendaylight.controller.config.facade.xml.rpc.Rpcs) ModuleRpcs(org.opendaylight.controller.config.facade.xml.rpc.ModuleRpcs) ModuleRpcs(org.opendaylight.controller.config.facade.xml.rpc.ModuleRpcs) ModuleMXBeanEntry(org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 5 with RuntimeBeanEntry

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;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) RuntimeBeanEntry(org.opendaylight.controller.config.yangjmxgenerator.RuntimeBeanEntry) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Field(org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.Field) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MethodDefinition(org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.MethodDefinition) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Aggregations

RuntimeBeanEntry (org.opendaylight.controller.config.yangjmxgenerator.RuntimeBeanEntry)8 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Test (org.junit.Test)3 ModuleMXBeanEntry (org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry)3 FtlTemplate (org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.FtlTemplate)3 LinkedHashMap (java.util.LinkedHashMap)2 RuntimeRegistratorTest (org.opendaylight.controller.config.yangjmxgenerator.RuntimeRegistratorTest)2 AttributeIfc (org.opendaylight.controller.config.yangjmxgenerator.attribute.AttributeIfc)2 RuntimeRegistratorFtlTemplate (org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.RuntimeRegistratorFtlTemplate)2 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 OpenType (javax.management.openmbean.OpenType)1 RuntimeBeanRegistratorAwareModule (org.opendaylight.controller.config.api.RuntimeBeanRegistratorAwareModule)1 AttributeMappingStrategy (org.opendaylight.controller.config.facade.xml.mapping.attributes.mapping.AttributeMappingStrategy)1 ObjectMapper (org.opendaylight.controller.config.facade.xml.mapping.attributes.mapping.ObjectMapper)1 InstanceConfig (org.opendaylight.controller.config.facade.xml.mapping.config.InstanceConfig)1 ModuleRpcs (org.opendaylight.controller.config.facade.xml.rpc.ModuleRpcs)1