Search in sources :

Example 36 with NormalizedNode

use of org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode in project controller by opendaylight.

the class WriteModificationTest method testApply.

@Test
public void testApply() throws Exception {
    // Write something into the datastore
    DOMStoreReadWriteTransaction writeTransaction = store.newReadWriteTransaction();
    WriteModification writeModification = new WriteModification(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
    writeModification.apply(writeTransaction);
    commitTransaction(writeTransaction);
    // Check if it's in the datastore
    Optional<NormalizedNode<?, ?>> data = readData(TestModel.TEST_PATH);
    Assert.assertTrue(data.isPresent());
}
Also used : DOMStoreReadWriteTransaction(org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) Test(org.junit.Test)

Example 37 with NormalizedNode

use of org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode in project controller by opendaylight.

the class ShardDataTreeSnapshotTest method testShardDataTreeSnapshotWithMetadata.

@Test
public void testShardDataTreeSnapshotWithMetadata() throws Exception {
    NormalizedNode<?, ?> expectedNode = ImmutableContainerNodeBuilder.create().withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME)).withChild(ImmutableNodes.leafNode(TestModel.DESC_QNAME, "foo")).build();
    Map<Class<? extends ShardDataTreeSnapshotMetadata<?>>, ShardDataTreeSnapshotMetadata<?>> expMetadata = ImmutableMap.of(TestShardDataTreeSnapshotMetadata.class, new TestShardDataTreeSnapshotMetadata("test"));
    MetadataShardDataTreeSnapshot snapshot = new MetadataShardDataTreeSnapshot(expectedNode, expMetadata);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    try (ObjectOutputStream out = new ObjectOutputStream(bos)) {
        snapshot.serialize(out);
    }
    ShardDataTreeSnapshot deserialized;
    try (ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()))) {
        deserialized = ShardDataTreeSnapshot.deserialize(in);
    }
    Optional<NormalizedNode<?, ?>> actualNode = deserialized.getRootNode();
    assertEquals("rootNode present", true, actualNode.isPresent());
    assertEquals("rootNode", expectedNode, actualNode.get());
    assertEquals("Deserialized type", MetadataShardDataTreeSnapshot.class, deserialized.getClass());
    assertEquals("Metadata", expMetadata, ((MetadataShardDataTreeSnapshot) deserialized).getMetadata());
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) ByteArrayInputStream(java.io.ByteArrayInputStream) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Example 38 with NormalizedNode

use of org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode in project controller by opendaylight.

the class DataChangeListener method dataChanged.

@SuppressWarnings("checkstyle:IllegalCatch")
private void dataChanged(Object message) {
    // Do nothing if notifications are not enabled
    if (!notificationsEnabled) {
        LOG.debug("Notifications not enabled for listener {} - dropping change notification", listener);
        return;
    }
    DataChanged reply = (DataChanged) message;
    AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>> change = reply.getChange();
    LOG.debug("Sending change notification {} to listener {}", change, listener);
    notificationCount++;
    try {
        this.listener.onDataChanged(change);
    } catch (RuntimeException e) {
        LOG.error(String.format("Error notifying listener %s", this.listener), e);
    }
    if (isValidSender(getSender())) {
        getSender().tell(DataChangedReply.INSTANCE, getSelf());
    }
}
Also used : DataChanged(org.opendaylight.controller.cluster.datastore.messages.DataChanged) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)

Example 39 with NormalizedNode

use of org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode in project controller by opendaylight.

the class CarEntryDataTreeCommitCohort method canCommit.

@Override
public CheckedFuture<PostCanCommitStep, DataValidationFailedException> canCommit(final Object txId, final DOMDataTreeCandidate candidate, final SchemaContext ctx) {
    // Simple data validation - verify the year, if present, is >= 1990
    final DataTreeCandidateNode rootNode = candidate.getRootNode();
    final Optional<NormalizedNode<?, ?>> dataAfter = rootNode.getDataAfter();
    LOG.info("In canCommit: modificationType: {}, dataBefore: {}, dataAfter: {}", rootNode.getModificationType(), rootNode.getDataBefore(), dataAfter);
    // MapEntryNode but we verify anyway.
    if (dataAfter.isPresent()) {
        final NormalizedNode<?, ?> normalizedNode = dataAfter.get();
        Verify.verify(normalizedNode instanceof DataContainerNode, "Expected type DataContainerNode, actual was %s", normalizedNode.getClass());
        DataContainerNode<?> entryNode = (DataContainerNode<?>) normalizedNode;
        final Optional<DataContainerChild<? extends PathArgument, ?>> possibleYear = entryNode.getChild(YEAR_NODE_ID);
        if (possibleYear.isPresent()) {
            final Number year = (Number) possibleYear.get().getValue();
            LOG.info("year is {}", year);
            if (!(year.longValue() >= 1990)) {
                return Futures.immediateFailedCheckedFuture(new DataValidationFailedException(DOMDataTreeIdentifier.class, candidate.getRootPath(), String.format("Invalid year %d - year must be >= 1990", year)));
            }
        }
    }
    // remaining 3PC stages (pre-commit and commit).
    return PostCanCommitStep.NOOP_SUCCESS_FUTURE;
}
Also used : DataValidationFailedException(org.opendaylight.mdsal.common.api.DataValidationFailedException) DataContainerChild(org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild) DOMDataTreeIdentifier(org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier) DataTreeCandidateNode(org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode) DataContainerNode(org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode) PathArgument(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)

Example 40 with NormalizedNode

use of org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode in project controller by opendaylight.

the class InMemoryDataStoreTest method testTransactionAbort.

@Test
public void testTransactionAbort() throws InterruptedException, ExecutionException {
    DOMStoreReadWriteTransaction writeTx = domStore.newReadWriteTransaction();
    assertNotNull(writeTx);
    assertTestContainerWrite(writeTx);
    DOMStoreThreePhaseCommitCohort cohort = writeTx.ready();
    assertTrue(cohort.canCommit().get().booleanValue());
    cohort.preCommit().get();
    cohort.abort().get();
    Optional<NormalizedNode<?, ?>> afterCommitRead = domStore.newReadOnlyTransaction().read(TestModel.TEST_PATH).get();
    assertFalse(afterCommitRead.isPresent());
}
Also used : DOMStoreReadWriteTransaction(org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) DOMStoreThreePhaseCommitCohort(org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort) Test(org.junit.Test)

Aggregations

NormalizedNode (org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)94 Test (org.junit.Test)55 YangInstanceIdentifier (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)39 ContainerNode (org.opendaylight.yangtools.yang.data.api.schema.ContainerNode)18 Optional (com.google.common.base.Optional)15 MapEntryNode (org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode)11 DOMStoreReadTransaction (org.opendaylight.mdsal.dom.spi.store.DOMStoreReadTransaction)10 ReadFailedException (org.opendaylight.controller.md.sal.common.api.data.ReadFailedException)9 DOMStoreWriteTransaction (org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction)9 NodeIdentifier (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier)9 ActorRef (akka.actor.ActorRef)8 InOrder (org.mockito.InOrder)8 DOMStoreReadWriteTransaction (org.opendaylight.mdsal.dom.spi.store.DOMStoreReadWriteTransaction)8 DOMStoreThreePhaseCommitCohort (org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort)8 ExecutionException (java.util.concurrent.ExecutionException)7 ReadFailedException (org.opendaylight.mdsal.common.api.ReadFailedException)7 DataTreeCandidate (org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate)7 Map (java.util.Map)6 CountDownLatch (java.util.concurrent.CountDownLatch)6 AtomicReference (java.util.concurrent.atomic.AtomicReference)6