/* * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.yangtools.yang.stmt; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import java.net.URI; import java.util.List; import java.util.Set; import org.junit.Test; import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.common.QNameModule; import org.opendaylight.yangtools.yang.common.Revision; import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode; import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode; import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode; import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode; import org.opendaylight.yangtools.yang.model.api.DataNodeContainer; import org.opendaylight.yangtools.yang.model.api.DataSchemaNode; import org.opendaylight.yangtools.yang.model.api.ListSchemaNode; import org.opendaylight.yangtools.yang.model.api.Module; import org.opendaylight.yangtools.yang.model.api.SchemaContext; import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode; import org.opendaylight.yangtools.yang.model.api.UsesNode; public class ControllerStmtParserTest { @Test public void test() throws Exception { final SchemaContext context = StmtTestUtils.parseYangSources("/sal-broker-impl"); assertNotNull(context); salDomBrokerImplModuleTest(context); configModuleTest(context); } private static void salDomBrokerImplModuleTest(final SchemaContext context) { final Module module = context.findModule("opendaylight-sal-dom-broker-impl", Revision.of("2013-10-28")).get(); final Set augmentations = module.getAugmentations(); boolean checked = false; for (final AugmentationSchemaNode augmentationSchema : augmentations) { final DataSchemaNode dataNode = augmentationSchema .getDataChildByName(QName.create(module.getQNameModule(), "dom-broker-impl")); if (dataNode instanceof ChoiceCaseNode) { final ChoiceCaseNode caseNode = (ChoiceCaseNode) dataNode; final DataSchemaNode dataNode2 = caseNode .getDataChildByName(QName.create(module.getQNameModule(), "async-data-broker")); if (dataNode2 instanceof ContainerSchemaNode) { final ContainerSchemaNode containerNode = (ContainerSchemaNode) dataNode2; final DataSchemaNode leaf = containerNode .getDataChildByName(QName.create(module.getQNameModule(), "type")); final List unknownSchemaNodes = leaf.getUnknownSchemaNodes(); assertEquals(1, unknownSchemaNodes.size()); final UnknownSchemaNode unknownSchemaNode = unknownSchemaNodes.get(0); assertEquals("dom-async-data-broker", unknownSchemaNode.getQName().getLocalName()); assertEquals(unknownSchemaNode.getQName(), unknownSchemaNode.getPath().getLastComponent()); checked = true; } } } assertTrue(checked); } private static void configModuleTest(final SchemaContext context) { final Module configModule = context.findModule("config", Revision.of("2013-04-05")).get(); final Module module = context.findModule("opendaylight-sal-dom-broker-impl", Revision.of("2013-10-28")).get(); final DataSchemaNode dataNode = configModule.getDataChildByName(QName.create(configModule.getQNameModule(), "modules")); assertTrue(dataNode instanceof ContainerSchemaNode); final ContainerSchemaNode moduleContainer = (ContainerSchemaNode) dataNode; final DataSchemaNode dataChildList = moduleContainer .getDataChildByName(QName.create(configModule.getQNameModule(), "module")); assertTrue(dataChildList instanceof ListSchemaNode); final ListSchemaNode listModule = (ListSchemaNode) dataChildList; final DataSchemaNode dataChildChoice = listModule .getDataChildByName(QName.create(configModule.getQNameModule(), "configuration")); assertTrue(dataChildChoice instanceof ChoiceSchemaNode); final ChoiceSchemaNode confChoice = (ChoiceSchemaNode) dataChildChoice; final ChoiceCaseNode caseNodeByName = confChoice.findCaseNodes("dom-broker-impl").iterator().next(); assertNotNull(caseNodeByName); final DataSchemaNode dataNode2 = caseNodeByName .getDataChildByName(QName.create(module.getQNameModule(), "async-data-broker")); assertTrue(dataNode2 instanceof ContainerSchemaNode); final ContainerSchemaNode containerNode = (ContainerSchemaNode) dataNode2; final DataSchemaNode leaf = containerNode.getDataChildByName(QName.create(module.getQNameModule(), "type")); final List unknownSchemaNodes = leaf.getUnknownSchemaNodes(); assertEquals(1, unknownSchemaNodes.size()); final UnknownSchemaNode unknownSchemaNode = unknownSchemaNodes.get(0); assertEquals(unknownSchemaNode.getQName(), unknownSchemaNode.getPath().getLastComponent()); assertEquals("dom-async-data-broker", unknownSchemaNode.getQName().getLocalName()); final ChoiceCaseNode domInmemoryDataBroker = confChoice.findCaseNodes("dom-inmemory-data-broker").iterator() .next(); assertNotNull(domInmemoryDataBroker); final DataSchemaNode schemaService = domInmemoryDataBroker .getDataChildByName(QName.create(module.getQNameModule(), "schema-service")); assertTrue(schemaService instanceof ContainerSchemaNode); final ContainerSchemaNode schemaServiceContainer = (ContainerSchemaNode) schemaService; assertEquals(1, schemaServiceContainer.getUses().size()); final UsesNode uses = schemaServiceContainer.getUses().iterator().next(); final QName groupingQName = QName.create("urn:opendaylight:params:xml:ns:yang:controller:config", "2013-04-05", "service-ref"); final QName usesGroupingPathLastComponent = uses.getGroupingPath().getLastComponent(); assertEquals(groupingQName, usesGroupingPathLastComponent); assertEquals(0, getChildNodeSizeWithoutUses(schemaServiceContainer)); final DataSchemaNode type = schemaServiceContainer.getDataChildByName(QName.create(module.getQNameModule(), "type")); final List typeUnknownSchemaNodes = type.getUnknownSchemaNodes(); assertEquals(1, typeUnknownSchemaNodes.size()); final UnknownSchemaNode typeUnknownSchemaNode = typeUnknownSchemaNodes.get(0); final QNameModule qNameModule = QNameModule.create( URI.create("urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom"), Revision.of("2013-10-28")); final QName qName = QName.create(qNameModule, "schema-service"); assertEquals(qName, typeUnknownSchemaNode.getQName()); assertEquals(typeUnknownSchemaNode.getQName(), typeUnknownSchemaNode .getPath().getLastComponent()); } private static int getChildNodeSizeWithoutUses(final DataNodeContainer csn) { int result = 0; for (final DataSchemaNode dsn : csn.getChildNodes()) { if (!dsn.isAddedByUses()) { result++; } } return result; } }