Search in sources :

Example 1 with EffectiveModelContext

use of org.opendaylight.yangtools.yang.model.api.EffectiveModelContext in project controller by opendaylight.

the class DataStoreAppConfigDefaultXMLReader method createDefaultInstance.

@SuppressWarnings("unchecked")
public T createDefaultInstance(final FallbackConfigProvider fallback) throws ConfigXMLReaderException, URISyntaxException, ParserConfigurationException, XMLStreamException, SAXException, IOException {
    YangInstanceIdentifier yangPath = bindingSerializer.toYangInstanceIdentifier(bindingContext.appConfigPath);
    LOG.debug("{}: Creating app config instance from path {}, Qname: {}", logName, yangPath, bindingContext.bindingQName);
    checkNotNull(schemaService, "%s: Could not obtain the SchemaService OSGi service", logName);
    EffectiveModelContext schemaContext = schemaService.getGlobalContext();
    Module module = schemaContext.findModule(bindingContext.bindingQName.getModule()).orElse(null);
    checkNotNull(module, "%s: Could not obtain the module schema for namespace %s, revision %s", logName, bindingContext.bindingQName.getNamespace(), bindingContext.bindingQName.getRevision());
    final SchemaInferenceStack schemaStack = SchemaInferenceStack.of(schemaContext);
    final SchemaTreeEffectiveStatement<?> dataSchema;
    try {
        dataSchema = schemaStack.enterSchemaTree(bindingContext.bindingQName);
    } catch (IllegalArgumentException e) {
        throw new ConfigXMLReaderException(logName + ": Could not obtain the schema for " + bindingContext.bindingQName, e);
    }
    checkCondition(bindingContext.schemaType.isInstance(dataSchema), "%s: Expected schema type %s for %s but actual type is %s", logName, bindingContext.schemaType, bindingContext.bindingQName, dataSchema.getClass());
    NormalizedNode dataNode = parsePossibleDefaultAppConfigXMLFile(schemaStack);
    if (dataNode == null) {
        dataNode = fallback.get(schemaStack.toSchemaTreeInference());
    }
    DataObject appConfig = bindingSerializer.fromNormalizedNode(yangPath, dataNode).getValue();
    // This shouldn't happen but need to handle it in case...
    checkNotNull(appConfig, "%s: Could not create instance for app config binding %s", logName, bindingContext.appConfigBindingClass);
    return (T) appConfig;
}
Also used : SchemaInferenceStack(org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack) DataObject(org.opendaylight.yangtools.yang.binding.DataObject) Module(org.opendaylight.yangtools.yang.model.api.Module) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) EffectiveModelContext(org.opendaylight.yangtools.yang.model.api.EffectiveModelContext)

Example 2 with EffectiveModelContext

use of org.opendaylight.yangtools.yang.model.api.EffectiveModelContext in project controller by opendaylight.

the class TransactionProxyTest method testReadRoot.

@Test
public void testReadRoot() throws InterruptedException, ExecutionException, java.util.concurrent.TimeoutException {
    EffectiveModelContext schemaContext = SchemaContextHelper.full();
    Configuration configuration = mock(Configuration.class);
    doReturn(configuration).when(mockActorContext).getConfiguration();
    doReturn(schemaContext).when(mockActorContext).getSchemaContext();
    doReturn(Sets.newHashSet("test", "cars")).when(configuration).getAllShardNames();
    NormalizedNode expectedNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
    NormalizedNode expectedNode2 = ImmutableNodes.containerNode(CarsModel.CARS_QNAME);
    setUpReadData("test", NormalizedNodeAggregatorTest.getRootNode(expectedNode1, schemaContext));
    setUpReadData("cars", NormalizedNodeAggregatorTest.getRootNode(expectedNode2, schemaContext));
    doReturn(MemberName.forName(memberName)).when(mockActorContext).getCurrentMemberName();
    doReturn(getSystem().dispatchers().defaultGlobalDispatcher()).when(mockActorContext).getClientDispatcher();
    TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
    Optional<NormalizedNode> readOptional = transactionProxy.read(YangInstanceIdentifier.empty()).get(5, TimeUnit.SECONDS);
    assertTrue("NormalizedNode isPresent", readOptional.isPresent());
    NormalizedNode normalizedNode = readOptional.get();
    assertTrue("Expect value to be a Collection", normalizedNode.body() instanceof Collection);
    @SuppressWarnings("unchecked") Collection<NormalizedNode> collection = (Collection<NormalizedNode>) normalizedNode.body();
    for (NormalizedNode node : collection) {
        assertTrue("Expected " + node + " to be a ContainerNode", node instanceof ContainerNode);
    }
    assertTrue("Child with QName = " + TestModel.TEST_QNAME + " not found", NormalizedNodeAggregatorTest.findChildWithQName(collection, TestModel.TEST_QNAME) != null);
    assertEquals(expectedNode1, NormalizedNodeAggregatorTest.findChildWithQName(collection, TestModel.TEST_QNAME));
    assertTrue("Child with QName = " + CarsModel.BASE_QNAME + " not found", NormalizedNodeAggregatorTest.findChildWithQName(collection, CarsModel.BASE_QNAME) != null);
    assertEquals(expectedNode2, NormalizedNodeAggregatorTest.findChildWithQName(collection, CarsModel.BASE_QNAME));
}
Also used : Configuration(org.opendaylight.controller.cluster.datastore.config.Configuration) Collection(java.util.Collection) ContainerNode(org.opendaylight.yangtools.yang.data.api.schema.ContainerNode) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) EffectiveModelContext(org.opendaylight.yangtools.yang.model.api.EffectiveModelContext) NormalizedNodeAggregatorTest(org.opendaylight.controller.cluster.datastore.utils.NormalizedNodeAggregatorTest) Test(org.junit.Test)

Example 3 with EffectiveModelContext

use of org.opendaylight.yangtools.yang.model.api.EffectiveModelContext in project controller by opendaylight.

the class NormalizedNodeAggregatorTest method testAggregate.

@Test
public void testAggregate() throws InterruptedException, ExecutionException, DataValidationFailedException {
    EffectiveModelContext schemaContext = SchemaContextHelper.full();
    NormalizedNode expectedNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
    NormalizedNode expectedNode2 = ImmutableNodes.containerNode(CarsModel.CARS_QNAME);
    Optional<NormalizedNode> optional = NormalizedNodeAggregator.aggregate(YangInstanceIdentifier.empty(), ImmutableList.of(Optional.<NormalizedNode>of(getRootNode(expectedNode1, schemaContext)), Optional.<NormalizedNode>of(getRootNode(expectedNode2, schemaContext))), schemaContext, LogicalDatastoreType.CONFIGURATION);
    NormalizedNode normalizedNode = optional.get();
    assertTrue("Expect value to be a Collection", normalizedNode.body() instanceof Collection);
    @SuppressWarnings("unchecked") Collection<NormalizedNode> collection = (Collection<NormalizedNode>) normalizedNode.body();
    for (NormalizedNode node : collection) {
        assertTrue("Expected " + node + " to be a ContainerNode", node instanceof ContainerNode);
    }
    assertTrue("Child with QName = " + TestModel.TEST_QNAME + " not found", findChildWithQName(collection, TestModel.TEST_QNAME) != null);
    assertEquals(expectedNode1, findChildWithQName(collection, TestModel.TEST_QNAME));
    assertTrue("Child with QName = " + CarsModel.BASE_QNAME + " not found", findChildWithQName(collection, CarsModel.BASE_QNAME) != null);
    assertEquals(expectedNode2, findChildWithQName(collection, CarsModel.BASE_QNAME));
}
Also used : Collection(java.util.Collection) ContainerNode(org.opendaylight.yangtools.yang.data.api.schema.ContainerNode) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) EffectiveModelContext(org.opendaylight.yangtools.yang.model.api.EffectiveModelContext) Test(org.junit.Test)

Example 4 with EffectiveModelContext

use of org.opendaylight.yangtools.yang.model.api.EffectiveModelContext in project mdsal by opendaylight.

the class OSGiDOMSchemaService method bindSnapshot.

@Reference(policy = ReferencePolicy.DYNAMIC, policyOption = ReferencePolicyOption.GREEDY)
void bindSnapshot(final OSGiModuleInfoSnapshot newContext) {
    LOG.info("Updating context to generation {}", newContext.getGeneration());
    final ModuleInfoSnapshot snapshot = newContext.getService();
    final EffectiveModelContext ctx = snapshot.getEffectiveModelContext();
    final ModuleInfoSnapshot previous = currentSnapshot.getAndSet(snapshot);
    LOG.debug("Snapshot updated from {} to {}", previous, snapshot);
    listeners.forEach(listener -> notifyListener(ctx, listener));
}
Also used : OSGiModuleInfoSnapshot(org.opendaylight.mdsal.dom.schema.osgi.OSGiModuleInfoSnapshot) ModuleInfoSnapshot(org.opendaylight.mdsal.binding.runtime.api.ModuleInfoSnapshot) EffectiveModelContext(org.opendaylight.yangtools.yang.model.api.EffectiveModelContext) AtomicReference(java.util.concurrent.atomic.AtomicReference) Reference(org.osgi.service.component.annotations.Reference)

Example 5 with EffectiveModelContext

use of org.opendaylight.yangtools.yang.model.api.EffectiveModelContext in project mdsal by opendaylight.

the class ModuleInfoSnapshotResolver method updateSnapshot.

@Holding("this")
@NonNull
private ModuleInfoSnapshot updateSnapshot(final EffectiveModelContext effectiveModel) {
    // Alright, now let's find out which sources got captured
    final Set<SourceIdentifier> sources = new HashSet<>();
    for (Entry<QNameModule, ModuleEffectiveStatement> entry : effectiveModel.getModuleStatements().entrySet()) {
        final Optional<Revision> revision = entry.getKey().getRevision();
        final ModuleEffectiveStatement module = entry.getValue();
        sources.add(RevisionSourceIdentifier.create(module.argument().getLocalName(), revision));
        module.streamEffectiveSubstatements(SubmoduleEffectiveStatement.class).map(submodule -> RevisionSourceIdentifier.create(submodule.argument().getLocalName(), revision)).forEach(sources::add);
    }
    final Map<SourceIdentifier, YangModuleInfo> moduleInfos = new HashMap<>();
    final Map<String, ClassLoader> classLoaders = new HashMap<>();
    for (SourceIdentifier source : sources) {
        final List<RegisteredModuleInfo> regs = sourceToInfoReg.get(source);
        checkState(!regs.isEmpty(), "No registration for %s", source);
        final RegisteredModuleInfo reg = regs.get(0);
        final YangModuleInfo info = reg.info;
        moduleInfos.put(source, info);
        final Class<?> infoClass = info.getClass();
        classLoaders.put(BindingReflections.getModelRootPackageName(infoClass.getPackage()), infoClass.getClassLoader());
    }
    final ModuleInfoSnapshot next = new DefaultModuleInfoSnapshot(effectiveModel, moduleInfos, classLoaders);
    currentSnapshot = next;
    return next;
}
Also used : YangTextSchemaSource(org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource) ListMultimap(com.google.common.collect.ListMultimap) YangSyntaxErrorException(org.opendaylight.yangtools.yang.parser.api.YangSyntaxErrorException) MultimapBuilder(com.google.common.collect.MultimapBuilder) Mutable(org.opendaylight.yangtools.concepts.Mutable) BindingReflections(org.opendaylight.mdsal.binding.spec.reflect.BindingReflections) LoggerFactory(org.slf4j.LoggerFactory) YangParserFactory(org.opendaylight.yangtools.yang.parser.api.YangParserFactory) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ModuleEffectiveStatement(org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement) HashSet(java.util.HashSet) QNameModule(org.opendaylight.yangtools.yang.common.QNameModule) ImmutableList(com.google.common.collect.ImmutableList) Nullable(org.eclipse.jdt.annotation.Nullable) ObjectRegistration(org.opendaylight.yangtools.concepts.ObjectRegistration) Objects.requireNonNull(java.util.Objects.requireNonNull) Map(java.util.Map) YangTextSchemaContextResolver(org.opendaylight.yangtools.yang.parser.repo.YangTextSchemaContextResolver) LinkedHashSet(java.util.LinkedHashSet) ModuleInfoSnapshot(org.opendaylight.mdsal.binding.runtime.api.ModuleInfoSnapshot) Logger(org.slf4j.Logger) SourceIdentifier(org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier) MoreObjects(com.google.common.base.MoreObjects) Set(java.util.Set) IOException(java.io.IOException) EffectiveModelContext(org.opendaylight.yangtools.yang.model.api.EffectiveModelContext) SubmoduleEffectiveStatement(org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleEffectiveStatement) QName(org.opendaylight.yangtools.yang.common.QName) Preconditions.checkState(com.google.common.base.Preconditions.checkState) Beta(com.google.common.annotations.Beta) YangModuleInfo(org.opendaylight.yangtools.yang.binding.YangModuleInfo) List(java.util.List) Holding(org.checkerframework.checker.lock.qual.Holding) AbstractObjectRegistration(org.opendaylight.yangtools.concepts.AbstractObjectRegistration) Revision(org.opendaylight.yangtools.yang.common.Revision) GuardedBy(org.checkerframework.checker.lock.qual.GuardedBy) RevisionSourceIdentifier(org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier) Entry(java.util.Map.Entry) Optional(java.util.Optional) SchemaSourceException(org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException) Registration(org.opendaylight.yangtools.concepts.Registration) Builder(com.google.common.collect.ImmutableList.Builder) NonNull(org.eclipse.jdt.annotation.NonNull) ModuleInfoSnapshot(org.opendaylight.mdsal.binding.runtime.api.ModuleInfoSnapshot) HashMap(java.util.HashMap) SourceIdentifier(org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier) RevisionSourceIdentifier(org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier) ModuleEffectiveStatement(org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement) YangModuleInfo(org.opendaylight.yangtools.yang.binding.YangModuleInfo) Revision(org.opendaylight.yangtools.yang.common.Revision) QNameModule(org.opendaylight.yangtools.yang.common.QNameModule) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Objects.requireNonNull(java.util.Objects.requireNonNull) NonNull(org.eclipse.jdt.annotation.NonNull) Holding(org.checkerframework.checker.lock.qual.Holding)

Aggregations

EffectiveModelContext (org.opendaylight.yangtools.yang.model.api.EffectiveModelContext)182 Test (org.junit.Test)99 Module (org.opendaylight.yangtools.yang.model.api.Module)37 QName (org.opendaylight.yangtools.yang.common.QName)29 ContainerNode (org.opendaylight.yangtools.yang.data.api.schema.ContainerNode)28 DataSchemaNode (org.opendaylight.yangtools.yang.model.api.DataSchemaNode)26 NormalizedNode (org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)24 LeafSchemaNode (org.opendaylight.yangtools.yang.model.api.LeafSchemaNode)24 ContainerSchemaNode (org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode)21 YangInstanceIdentifier (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)18 QNameModule (org.opendaylight.yangtools.yang.common.QNameModule)16 NodeIdentifier (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier)16 NormalizedNodeStreamWriter (org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter)16 IRSchemaSource (org.opendaylight.yangtools.yang.parser.rfc7950.ir.IRSchemaSource)14 DOMMountPoint (org.opendaylight.mdsal.dom.api.DOMMountPoint)13 RestconfDocumentedException (org.opendaylight.restconf.common.errors.RestconfDocumentedException)13 RpcDefinition (org.opendaylight.yangtools.yang.model.api.RpcDefinition)13 MapEntryNode (org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode)11 IOException (java.io.IOException)9 Collection (java.util.Collection)9