Search in sources :

Example 1 with JavaAttribute

use of org.opendaylight.controller.config.yangjmxgenerator.attribute.JavaAttribute in project controller by opendaylight.

the class RuntimeBeanEntry method extractSubtree.

/**
 * Get direct descendants of this subtree, together with attributes defined
 * in subtree.
 */
private static AttributesRpcsAndRuntimeBeans extractSubtree(final String packageName, final DataNodeContainer subtree, final TypeProviderWrapper typeProviderWrapper, final Module currentModule, final SchemaContext ctx) {
    Multimap<QName, RpcDefinition> identitiesToRpcs = getIdentitiesToRpcs(ctx);
    List<AttributeIfc> attributes = Lists.newArrayList();
    List<RuntimeBeanEntry> runtimeBeanEntries = new ArrayList<>();
    for (DataSchemaNode child : subtree.getChildNodes()) {
        // runtime beans
        if (child instanceof LeafSchemaNode) {
            // just save the attribute
            LeafSchemaNode leaf = (LeafSchemaNode) child;
            attributes.add(new JavaAttribute(leaf, typeProviderWrapper));
        } else if (child instanceof ContainerSchemaNode) {
            ContainerSchemaNode container = (ContainerSchemaNode) child;
            // this can be either TO or hierarchical RB
            TOAttribute toAttribute = TOAttribute.create(container, typeProviderWrapper, packageName);
            attributes.add(toAttribute);
        } else if (child instanceof ListSchemaNode) {
            if (isInnerStateBean(child)) {
                ListSchemaNode listSchemaNode = (ListSchemaNode) child;
                RuntimeBeanEntry hierarchicalChild = createHierarchical(packageName, listSchemaNode, typeProviderWrapper, currentModule, ctx);
                runtimeBeanEntries.add(hierarchicalChild);
            } else /* ordinary list attribute */
            {
                ListAttribute listAttribute = ListAttribute.create((ListSchemaNode) child, typeProviderWrapper, packageName);
                attributes.add(listAttribute);
            }
        } else if (child instanceof LeafListSchemaNode) {
            ListAttribute listAttribute = ListAttribute.create((LeafListSchemaNode) child, typeProviderWrapper);
            attributes.add(listAttribute);
        } else {
            throw new IllegalStateException("Unexpected running-data node " + child);
        }
    }
    Set<Rpc> rpcs = new HashSet<>();
    SchemaNode subtreeSchemaNode = (SchemaNode) subtree;
    for (UnknownSchemaNode unknownSchemaNode : subtreeSchemaNode.getUnknownSchemaNodes()) {
        if (ConfigConstants.RPC_CONTEXT_INSTANCE_EXTENSION_QNAME.equals(unknownSchemaNode.getNodeType())) {
            String localIdentityName = unknownSchemaNode.getNodeParameter();
            QName identityQName = unknownSchemaNode.isAddedByUses() ? findQNameFromGrouping(subtree, ctx, unknownSchemaNode, localIdentityName) : QName.create(currentModule.getNamespace(), currentModule.getRevision(), localIdentityName);
            // convert RpcDefinition to Rpc
            for (RpcDefinition rpcDefinition : identitiesToRpcs.get(identityQName)) {
                String name = TypeProviderWrapper.findJavaParameter(rpcDefinition);
                AttributeIfc returnType;
                if (rpcDefinition.getOutput() == null || rpcDefinition.getOutput().getChildNodes().isEmpty()) {
                    returnType = VoidAttribute.getInstance();
                } else if (rpcDefinition.getOutput().getChildNodes().size() == 1) {
                    DataSchemaNode returnDSN = rpcDefinition.getOutput().getChildNodes().iterator().next();
                    returnType = getReturnTypeAttribute(returnDSN, typeProviderWrapper, packageName);
                } else {
                    throw new IllegalArgumentException("More than one child node in rpc output is not supported. " + "Error occured in " + rpcDefinition);
                }
                List<JavaAttribute> parameters = new ArrayList<>();
                for (DataSchemaNode childNode : sortAttributes(rpcDefinition.getInput().getChildNodes())) {
                    if (childNode.isAddedByUses() == false) {
                        // skip
                        // refined
                        // context-instance
                        checkArgument(childNode instanceof LeafSchemaNode, "Unexpected type of rpc input type. " + "Currently only leafs and empty output nodes are supported, got " + childNode);
                        JavaAttribute javaAttribute = new JavaAttribute((LeafSchemaNode) childNode, typeProviderWrapper);
                        parameters.add(javaAttribute);
                    }
                }
                Rpc newRpc = new Rpc(returnType, name, rpcDefinition.getQName().getLocalName(), parameters);
                rpcs.add(newRpc);
            }
        }
    }
    return new AttributesRpcsAndRuntimeBeans(runtimeBeanEntries, attributes, rpcs);
}
Also used : RpcDefinition(org.opendaylight.yangtools.yang.model.api.RpcDefinition) LeafListSchemaNode(org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode) ArrayList(java.util.ArrayList) TOAttribute(org.opendaylight.controller.config.yangjmxgenerator.attribute.TOAttribute) ContainerSchemaNode(org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode) UnknownSchemaNode(org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode) JavaAttribute(org.opendaylight.controller.config.yangjmxgenerator.attribute.JavaAttribute) HashSet(java.util.HashSet) DataSchemaNode(org.opendaylight.yangtools.yang.model.api.DataSchemaNode) QName(org.opendaylight.yangtools.yang.common.QName) LeafSchemaNode(org.opendaylight.yangtools.yang.model.api.LeafSchemaNode) LeafListSchemaNode(org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode) UnknownSchemaNode(org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode) ListSchemaNode(org.opendaylight.yangtools.yang.model.api.ListSchemaNode) ContainerSchemaNode(org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode) SchemaNode(org.opendaylight.yangtools.yang.model.api.SchemaNode) DataSchemaNode(org.opendaylight.yangtools.yang.model.api.DataSchemaNode) LeafSchemaNode(org.opendaylight.yangtools.yang.model.api.LeafSchemaNode) LeafListSchemaNode(org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode) ListSchemaNode(org.opendaylight.yangtools.yang.model.api.ListSchemaNode) AttributeIfc(org.opendaylight.controller.config.yangjmxgenerator.attribute.AttributeIfc) ListAttribute(org.opendaylight.controller.config.yangjmxgenerator.attribute.ListAttribute)

Example 2 with JavaAttribute

use of org.opendaylight.controller.config.yangjmxgenerator.attribute.JavaAttribute in project controller by opendaylight.

the class ModuleMXBeanEntryTemplatesTest method mockMbe.

public static ModuleMXBeanEntry mockMbe(final String packageName) {
    final ModuleMXBeanEntry mbe = mock(ModuleMXBeanEntry.class);
    final Map<String, AttributeIfc> a = Maps.newHashMap();
    final JavaAttribute attr = mockJavaAttr();
    a.put("attr1", attr);
    doReturn(a).when(mbe).getAttributes();
    doReturn(packageName).when(mbe).getPackageName();
    doReturn(Collections.emptyMap()).when(mbe).getProvidedServices();
    doReturn("yang-module").when(mbe).getYangModuleName();
    doReturn("local").when(mbe).getYangModuleLocalname();
    doReturn("AbstractType").when(mbe).getAbstractFactoryName();
    doReturn("Module").when(mbe).getStubModuleName();
    doReturn("fullA").when(mbe).getFullyQualifiedName(anyString());
    doReturn("uniq").when(mbe).getGloballyUniqueName();
    return mbe;
}
Also used : ModuleMXBeanEntry(org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry) AttributeIfc(org.opendaylight.controller.config.yangjmxgenerator.attribute.AttributeIfc) Matchers.anyString(org.mockito.Matchers.anyString) JavaAttribute(org.opendaylight.controller.config.yangjmxgenerator.attribute.JavaAttribute)

Example 3 with JavaAttribute

use of org.opendaylight.controller.config.yangjmxgenerator.attribute.JavaAttribute in project controller by opendaylight.

the class TemplateFactory method tOsFromRbe.

public static Map<String, GeneralClassTemplate> tOsFromRbe(final RuntimeBeanEntry rbe) {
    final Map<String, GeneralClassTemplate> retVal = Maps.newHashMap();
    final TOAttributesProcessor processor = new TOAttributesProcessor();
    final Map<String, AttributeIfc> yangPropertiesToTypesMap = Maps.newHashMap(rbe.getYangPropertiesToTypesMap());
    // Add TOs from output parameters
    for (final Rpc rpc : rbe.getRpcs()) {
        final AttributeIfc returnType = rpc.getReturnType();
        if (returnType == VoidAttribute.getInstance()) {
            continue;
        }
        if (returnType instanceof JavaAttribute) {
            continue;
        }
        if (returnType instanceof ListAttribute && returnType.getOpenType() instanceof SimpleType) {
            continue;
        }
        Preconditions.checkState(!yangPropertiesToTypesMap.containsKey(returnType.getAttributeYangName()), "Duplicate TO %s for %s", returnType.getAttributeYangName(), rbe);
        yangPropertiesToTypesMap.put(returnType.getAttributeYangName(), returnType);
    }
    processor.processAttributes(yangPropertiesToTypesMap);
    for (final org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.TemplateFactory.TOAttributesProcessor.TOInternal to : processor.getTOs()) {
        final List<Constructor> constructors = Lists.newArrayList();
        constructors.add(new Constructor(to.getName(), "super();"));
        // TODO header
        retVal.put(to.getType(), new GeneralClassTemplate(null, rbe.getPackageName(), to.getName(), Collections.<String>emptyList(), Collections.<String>emptyList(), to.getFields(), to.getMethods(), false, false, constructors));
    }
    return retVal;
}
Also used : Rpc(org.opendaylight.controller.config.yangjmxgenerator.RuntimeBeanEntry.Rpc) Constructor(org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.Constructor) SimpleType(javax.management.openmbean.SimpleType) AttributeIfc(org.opendaylight.controller.config.yangjmxgenerator.attribute.AttributeIfc) JavaAttribute(org.opendaylight.controller.config.yangjmxgenerator.attribute.JavaAttribute) ListAttribute(org.opendaylight.controller.config.yangjmxgenerator.attribute.ListAttribute)

Example 4 with JavaAttribute

use of org.opendaylight.controller.config.yangjmxgenerator.attribute.JavaAttribute in project controller by opendaylight.

the class TemplateFactory method getTOAndMXInterfaceFtlFiles.

/**
 * Get map of file name as key, FtlFile instance representing runtime mx
 * bean as value that should be persisted from this instance.
 */
public static Map<String, FtlTemplate> getTOAndMXInterfaceFtlFiles(final RuntimeBeanEntry entry) {
    final Map<String, FtlTemplate> result = new HashMap<>();
    {
        // create GeneralInterfaceFtlFile for runtime MXBean. Attributes will
        // be transformed to getter methods
        final String mxBeanTypeName = entry.getJavaNameOfRuntimeMXBean();
        final List<String> extendedInterfaces = Collections.singletonList(RuntimeBean.class.getCanonicalName());
        final List<MethodDeclaration> methods = new ArrayList<>();
        // convert attributes to getters
        for (final AttributeIfc attributeIfc : entry.getAttributes()) {
            String returnType;
            returnType = getReturnType(attributeIfc);
            final String getterName = "get" + attributeIfc.getUpperCaseCammelCase();
            final MethodDeclaration getter = new MethodDeclaration(returnType, getterName, Collections.<Field>emptyList());
            methods.add(getter);
        }
        // add rpc methods
        for (final Rpc rpc : entry.getRpcs()) {
            // convert JavaAttribute parameters into fields
            final List<Field> fields = new ArrayList<>();
            for (final JavaAttribute ja : rpc.getParameters()) {
                final Field field = new Field(Collections.emptyList(), ja.getType().getFullyQualifiedName(), ja.getLowerCaseCammelCase(), ja.getNullableDefaultWrappedForCode());
                fields.add(field);
            }
            final MethodDeclaration operation = new MethodDeclaration(getReturnType(rpc.getReturnType()), rpc.getName(), fields);
            methods.add(operation);
        }
        // FIXME header
        final GeneralInterfaceTemplate runtimeMxBeanIfc = new GeneralInterfaceTemplate(null, entry.getPackageName(), mxBeanTypeName, extendedInterfaces, methods);
        result.put(runtimeMxBeanIfc.getTypeDeclaration().getName() + ".java", runtimeMxBeanIfc);
    }
    result.putAll(TemplateFactory.tOsFromRbe(entry));
    return result;
}
Also used : ModuleField(org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.ModuleField) Field(org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.Field) IdentityRefModuleField(org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.IdentityRefModuleField) Rpc(org.opendaylight.controller.config.yangjmxgenerator.RuntimeBeanEntry.Rpc) HashMap(java.util.HashMap) MethodDeclaration(org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.MethodDeclaration) AttributeIfc(org.opendaylight.controller.config.yangjmxgenerator.attribute.AttributeIfc) ArrayList(java.util.ArrayList) List(java.util.List) JavaAttribute(org.opendaylight.controller.config.yangjmxgenerator.attribute.JavaAttribute)

Example 5 with JavaAttribute

use of org.opendaylight.controller.config.yangjmxgenerator.attribute.JavaAttribute in project controller by opendaylight.

the class ModuleMXBeanEntryPluginTest method testThreadsJavaPlugin.

@Test
public void testThreadsJavaPlugin() {
    Map<String, ModuleMXBeanEntry> /* identity local name */
    namesToMBEs = loadThreadsJava();
    {
        // check threadfactory-naming
        ModuleMXBeanEntry threadFactoryNaming = namesToMBEs.get(THREADFACTORY_NAMING_MXB_NAME);
        Collection<RuntimeBeanEntry> runtimeBeans = threadFactoryNaming.getRuntimeBeans();
        assertThat(runtimeBeans.size(), is(4));
        // first one should be root
        {
            RuntimeBeanEntry rootRB = findFirstByYangName(runtimeBeans, THREADFACTORY_NAMING_MXB_NAME);
            assertThat(rootRB.isRoot(), is(true));
            assertThat(rootRB.getAttributes().size(), is(1));
            JavaAttribute attribute = (JavaAttribute) rootRB.getAttributes().iterator().next();
            assertThat(attribute.getAttributeYangName(), is("created-sessions"));
            assertThat(rootRB.getYangName(), is(THREADFACTORY_NAMING_MXB_NAME));
            Map<String, FtlTemplate> ftlMap = TemplateFactory.getTOAndMXInterfaceFtlFiles(rootRB);
            assertThat(ftlMap.size(), is(1));
            GeneralInterfaceTemplate rootGeneratorInterface = (GeneralInterfaceTemplate) ftlMap.get("NamingThreadFactoryRuntimeMXBean.java");
            assertNotNull(rootGeneratorInterface);
            assertThat(rootGeneratorInterface.getPackageName(), is(PACKAGE_NAME));
            assertThat(rootGeneratorInterface.getFullyQualifiedName(), is(PACKAGE_NAME + ".NamingThreadFactoryRuntimeMXBean"));
            assertThat(rootGeneratorInterface.getTypeDeclaration().getExtended(), is(Arrays.asList("org.opendaylight.controller.config.api.runtime.RuntimeBean")));
            assertThat(rootGeneratorInterface.getMethods().size(), is(1));
            Method getCreatedSessions = findFirstMethodByName(rootGeneratorInterface.getMethods(), "getCreatedSessions");
            assertThat(getCreatedSessions.getName(), is("getCreatedSessions"));
            assertThat(getCreatedSessions.getParameters().isEmpty(), is(true));
            assertThat(getCreatedSessions.getReturnType(), is(Long.class.getName()));
        }
    }
    {
        ModuleMXBeanEntry threadFactoryNaming = namesToMBEs.get(THREADFACTORY_NAMING_MXB_NAME);
        Collection<RuntimeBeanEntry> runtimeBeans = threadFactoryNaming.getRuntimeBeans();
        assertThat(runtimeBeans.size(), is(4));
        {
            RuntimeBeanEntry streamRB = findFirstByNamePrefix(runtimeBeans, "ThreadStream");
            assertNotNull(streamRB);
            assertFalse(streamRB.getKeyYangName().isPresent());
            assertFalse(streamRB.getKeyJavaName().isPresent());
            Map<String, AttributeIfc> attributeMap = streamRB.getYangPropertiesToTypesMap();
            assertEquals(4, attributeMap.size());
            TOAttribute toAttr = (TOAttribute) attributeMap.get("peer");
            assertNotNull(toAttr);
            JavaAttribute timestampAttr = (JavaAttribute) attributeMap.get("timestamp");
            assertNotNull(timestampAttr);
            JavaAttribute stateAttr = (JavaAttribute) attributeMap.get("state");
            assertNotNull(stateAttr);
            ListAttribute innerStreamList = (ListAttribute) attributeMap.get("inner-stream-list");
            assertNotNull(innerStreamList);
            Map<String, FtlTemplate> ftlMap = TemplateFactory.getTOAndMXInterfaceFtlFiles(streamRB);
            assertThat(ftlMap.size(), is(3));
            GeneralInterfaceTemplate streamGeneralInterface = (GeneralInterfaceTemplate) ftlMap.get("ThreadStreamRuntimeMXBean.java");
            assertThat(streamGeneralInterface.getMethods().size(), is(4));
            Method getPeer = findFirstMethodByName(streamGeneralInterface.getMethods(), "getPeer");
            assertNotNull(getPeer);
            assertThat(getPeer.getReturnType(), is(PACKAGE_NAME + ".Peer"));
            // test TO
            GeneralClassTemplate peerTO = (GeneralClassTemplate) ftlMap.get("pack2.Peer");
            assertThat(peerTO.getPackageName(), is(PACKAGE_NAME));
            assertThat(peerTO.getTypeDeclaration().getExtended().isEmpty(), is(true));
            assertThat(peerTO.getFullyQualifiedName(), is(PACKAGE_NAME + ".Peer"));
            assertThat(peerTO.getMethods().size(), is(5 + 2));
            Method getPort = findFirstMethodByName(peerTO.getMethods(), "getPort");
            assertNotNull(getPort);
            Method setPort = findFirstMethodByName(peerTO.getMethods(), "setPort");
            assertNotNull(setPort);
            Method getCoreSize = findFirstMethodByName(peerTO.getMethods(), "getCoreSize");
            Method setCoreSize = findFirstMethodByName(peerTO.getMethods(), "setCoreSize");
            assertNotNull(setCoreSize);
            assertNotNull(getCoreSize);
        }
    }
}
Also used : GeneralInterfaceTemplate(org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.GeneralInterfaceTemplate) RuntimeBeanEntry(org.opendaylight.controller.config.yangjmxgenerator.RuntimeBeanEntry) GeneralClassTemplate(org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.GeneralClassTemplate) Method(org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.Method) TOAttribute(org.opendaylight.controller.config.yangjmxgenerator.attribute.TOAttribute) ModuleMXBeanEntry(org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry) Collection(java.util.Collection) AttributeIfc(org.opendaylight.controller.config.yangjmxgenerator.attribute.AttributeIfc) FtlTemplate(org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.FtlTemplate) JavaAttribute(org.opendaylight.controller.config.yangjmxgenerator.attribute.JavaAttribute) ListAttribute(org.opendaylight.controller.config.yangjmxgenerator.attribute.ListAttribute) Test(org.junit.Test) ModuleMXBeanEntryTest(org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntryTest)

Aggregations

JavaAttribute (org.opendaylight.controller.config.yangjmxgenerator.attribute.JavaAttribute)8 AttributeIfc (org.opendaylight.controller.config.yangjmxgenerator.attribute.AttributeIfc)6 ListAttribute (org.opendaylight.controller.config.yangjmxgenerator.attribute.ListAttribute)4 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)3 TOAttribute (org.opendaylight.controller.config.yangjmxgenerator.attribute.TOAttribute)3 QName (org.opendaylight.yangtools.yang.common.QName)3 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 SimpleType (javax.management.openmbean.SimpleType)2 ModuleMXBeanEntry (org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry)2 Rpc (org.opendaylight.controller.config.yangjmxgenerator.RuntimeBeanEntry.Rpc)2 DataSchemaNode (org.opendaylight.yangtools.yang.model.api.DataSchemaNode)2 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 ArrayType (javax.management.openmbean.ArrayType)1 CompositeType (javax.management.openmbean.CompositeType)1 Matchers.anyString (org.mockito.Matchers.anyString)1