Search in sources :

Example 56 with MapEntryNode

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

the class DistributedDataStoreRemotingIntegrationTest method testReadyLocalTransactionForwardedToLeader.

@SuppressWarnings("unchecked")
@Test
public void testReadyLocalTransactionForwardedToLeader() throws Exception {
    initDatastoresWithCars("testReadyLocalTransactionForwardedToLeader");
    followerTestKit.waitUntilLeader(followerDistributedDataStore.getActorContext(), "cars");
    final Optional<ActorRef> carsFollowerShard = followerDistributedDataStore.getActorContext().findLocalShard("cars");
    assertEquals("Cars follower shard found", true, carsFollowerShard.isPresent());
    final DataTree dataTree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_OPERATIONAL, SchemaContextHelper.full());
    // Send a tx with immediate commit.
    DataTreeModification modification = dataTree.takeSnapshot().newModification();
    new WriteModification(CarsModel.BASE_PATH, CarsModel.emptyContainer()).apply(modification);
    new MergeModification(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode()).apply(modification);
    final MapEntryNode car1 = CarsModel.newCarEntry("optima", BigInteger.valueOf(20000));
    new WriteModification(CarsModel.newCarPath("optima"), car1).apply(modification);
    modification.ready();
    ReadyLocalTransaction readyLocal = new ReadyLocalTransaction(tx1, modification, true);
    carsFollowerShard.get().tell(readyLocal, followerTestKit.getRef());
    Object resp = followerTestKit.expectMsgClass(Object.class);
    if (resp instanceof akka.actor.Status.Failure) {
        throw new AssertionError("Unexpected failure response", ((akka.actor.Status.Failure) resp).cause());
    }
    assertEquals("Response type", CommitTransactionReply.class, resp.getClass());
    verifyCars(leaderDistributedDataStore.newReadOnlyTransaction(), car1);
    // Send another tx without immediate commit.
    modification = dataTree.takeSnapshot().newModification();
    MapEntryNode car2 = CarsModel.newCarEntry("sportage", BigInteger.valueOf(30000));
    new WriteModification(CarsModel.newCarPath("sportage"), car2).apply(modification);
    modification.ready();
    readyLocal = new ReadyLocalTransaction(tx2, modification, false);
    carsFollowerShard.get().tell(readyLocal, followerTestKit.getRef());
    resp = followerTestKit.expectMsgClass(Object.class);
    if (resp instanceof akka.actor.Status.Failure) {
        throw new AssertionError("Unexpected failure response", ((akka.actor.Status.Failure) resp).cause());
    }
    assertEquals("Response type", ReadyTransactionReply.class, resp.getClass());
    final ActorSelection txActor = leaderDistributedDataStore.getActorContext().actorSelection(((ReadyTransactionReply) resp).getCohortPath());
    final Supplier<Short> versionSupplier = Mockito.mock(Supplier.class);
    Mockito.doReturn(DataStoreVersions.CURRENT_VERSION).when(versionSupplier).get();
    ThreePhaseCommitCohortProxy cohort = new ThreePhaseCommitCohortProxy(leaderDistributedDataStore.getActorContext(), Arrays.asList(new ThreePhaseCommitCohortProxy.CohortInfo(Futures.successful(txActor), versionSupplier)), tx2);
    cohort.canCommit().get(5, TimeUnit.SECONDS);
    cohort.preCommit().get(5, TimeUnit.SECONDS);
    cohort.commit().get(5, TimeUnit.SECONDS);
    verifyCars(leaderDistributedDataStore.newReadOnlyTransaction(), car1, car2);
}
Also used : DataTreeModification(org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification) WriteModification(org.opendaylight.controller.cluster.datastore.modification.WriteModification) ActorRef(akka.actor.ActorRef) MapEntryNode(org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode) InMemoryDataTreeFactory(org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory) MergeModification(org.opendaylight.controller.cluster.datastore.modification.MergeModification) ReadyLocalTransaction(org.opendaylight.controller.cluster.datastore.messages.ReadyLocalTransaction) ActorSelection(akka.actor.ActorSelection) GetShardDataTree(org.opendaylight.controller.cluster.datastore.messages.GetShardDataTree) DataTree(org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree) Test(org.junit.Test)

Example 57 with MapEntryNode

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

the class DistributedDataStoreRemotingIntegrationTest method testReadWriteTransactionWithSingleShard.

@Test
public void testReadWriteTransactionWithSingleShard() throws Exception {
    initDatastoresWithCars("testReadWriteTransactionWithSingleShard");
    final DOMStoreReadWriteTransaction rwTx = followerDistributedDataStore.newReadWriteTransaction();
    assertNotNull("newReadWriteTransaction returned null", rwTx);
    rwTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
    rwTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
    final MapEntryNode car1 = CarsModel.newCarEntry("optima", BigInteger.valueOf(20000));
    rwTx.merge(CarsModel.newCarPath("optima"), car1);
    verifyCars(rwTx, car1);
    final MapEntryNode car2 = CarsModel.newCarEntry("sportage", BigInteger.valueOf(25000));
    final YangInstanceIdentifier car2Path = CarsModel.newCarPath("sportage");
    rwTx.merge(car2Path, car2);
    verifyExists(rwTx, car2Path);
    followerTestKit.doCommit(rwTx.ready());
    verifyCars(followerDistributedDataStore.newReadOnlyTransaction(), car1, car2);
}
Also used : DOMStoreReadWriteTransaction(org.opendaylight.mdsal.dom.spi.store.DOMStoreReadWriteTransaction) MapEntryNode(org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) Test(org.junit.Test)

Example 58 with MapEntryNode

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

the class AbstractEntityOwnershipTest method getMapEntryNodeChild.

protected MapEntryNode getMapEntryNodeChild(final DataContainerNode<? extends PathArgument> parent, final QName childMap, final QName child, final Object key, final boolean expectPresent) {
    Optional<DataContainerChild<? extends PathArgument, ?>> childNode = parent.getChild(new NodeIdentifier(childMap));
    assertEquals("Missing " + childMap.toString(), true, childNode.isPresent());
    MapNode entityTypeMapNode = (MapNode) childNode.get();
    Optional<MapEntryNode> entityTypeEntry = entityTypeMapNode.getChild(new NodeIdentifierWithPredicates(childMap, child, key));
    if (expectPresent && !entityTypeEntry.isPresent()) {
        fail("Missing " + childMap.toString() + " entry for " + key + ". Actual: " + entityTypeMapNode.getValue());
    } else if (!expectPresent && entityTypeEntry.isPresent()) {
        fail("Found unexpected " + childMap.toString() + " entry for " + key);
    }
    return entityTypeEntry.isPresent() ? entityTypeEntry.get() : null;
}
Also used : DataContainerChild(org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild) NodeIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier) PathArgument(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument) MapNode(org.opendaylight.yangtools.yang.data.api.schema.MapNode) MapEntryNode(org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode) NodeIdentifierWithPredicates(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates)

Example 59 with MapEntryNode

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

the class DistributedEntityOwnershipIntegrationTest method verifyCandidates.

private static void verifyCandidates(final AbstractDataStore dataStore, final DOMEntity entity, final String... expCandidates) throws Exception {
    AssertionError lastError = null;
    Stopwatch sw = Stopwatch.createStarted();
    while (sw.elapsed(TimeUnit.MILLISECONDS) <= 10000) {
        Optional<NormalizedNode<?, ?>> possible = dataStore.newReadOnlyTransaction().read(entityPath(entity.getType(), entity.getIdentifier()).node(Candidate.QNAME)).get(5, TimeUnit.SECONDS);
        try {
            assertEquals("Candidates not found for " + entity, true, possible.isPresent());
            Collection<String> actual = new ArrayList<>();
            for (MapEntryNode candidate : ((MapNode) possible.get()).getValue()) {
                actual.add(candidate.getChild(CANDIDATE_NAME_NODE_ID).get().getValue().toString());
            }
            assertEquals("Candidates for " + entity, Arrays.asList(expCandidates), actual);
            return;
        } catch (AssertionError e) {
            lastError = e;
            Uninterruptibles.sleepUninterruptibly(300, TimeUnit.MILLISECONDS);
        }
    }
    throw lastError;
}
Also used : Stopwatch(com.google.common.base.Stopwatch) ArrayList(java.util.ArrayList) MapNode(org.opendaylight.yangtools.yang.data.api.schema.MapNode) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) MapEntryNode(org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode)

Example 60 with MapEntryNode

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

the class AbstractShardTest method testRecovery.

protected void testRecovery(final Set<Integer> listEntryKeys) throws Exception {
    // Create the actor and wait for recovery complete.
    final int nListEntries = listEntryKeys.size();
    final CountDownLatch recoveryComplete = new CountDownLatch(1);
    final Creator<Shard> creator = () -> new Shard(newShardBuilder()) {

        @Override
        protected void onRecoveryComplete() {
            try {
                super.onRecoveryComplete();
            } finally {
                recoveryComplete.countDown();
            }
        }
    };
    final TestActorRef<Shard> shard = TestActorRef.create(getSystem(), Props.create(new DelegatingShardCreator(creator)).withDispatcher(Dispatchers.DefaultDispatcherId()), "testRecovery");
    assertEquals("Recovery complete", true, recoveryComplete.await(5, TimeUnit.SECONDS));
    // Verify data in the data store.
    final NormalizedNode<?, ?> outerList = readStore(shard, TestModel.OUTER_LIST_PATH);
    assertNotNull(TestModel.OUTER_LIST_QNAME.getLocalName() + " not found", outerList);
    assertTrue(TestModel.OUTER_LIST_QNAME.getLocalName() + " value is not Iterable", outerList.getValue() instanceof Iterable);
    for (final Object entry : (Iterable<?>) outerList.getValue()) {
        assertTrue(TestModel.OUTER_LIST_QNAME.getLocalName() + " entry is not MapEntryNode", entry instanceof MapEntryNode);
        final MapEntryNode mapEntry = (MapEntryNode) entry;
        final Optional<DataContainerChild<? extends PathArgument, ?>> idLeaf = mapEntry.getChild(new YangInstanceIdentifier.NodeIdentifier(TestModel.ID_QNAME));
        assertTrue("Missing leaf " + TestModel.ID_QNAME.getLocalName(), idLeaf.isPresent());
        final Object value = idLeaf.get().getValue();
        assertTrue("Unexpected value for leaf " + TestModel.ID_QNAME.getLocalName() + ": " + value, listEntryKeys.remove(value));
    }
    if (!listEntryKeys.isEmpty()) {
        fail("Missing " + TestModel.OUTER_LIST_QNAME.getLocalName() + " entries with keys: " + listEntryKeys);
    }
    assertEquals("Last log index", nListEntries, shard.underlyingActor().getShardMBean().getLastLogIndex());
    assertEquals("Commit index", nListEntries, shard.underlyingActor().getShardMBean().getCommitIndex());
    assertEquals("Last applied", nListEntries, shard.underlyingActor().getShardMBean().getLastApplied());
    shard.tell(PoisonPill.getInstance(), ActorRef.noSender());
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) MapEntryNode(org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) DataContainerChild(org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild) PathArgument(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument)

Aggregations

MapEntryNode (org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode)95 Test (org.junit.Test)55 NodeIdentifierWithPredicates (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates)50 ArrayList (java.util.ArrayList)39 Flowspec (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev171207.flowspec.destination.Flowspec)34 FlowspecBuilder (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev171207.flowspec.destination.FlowspecBuilder)34 YangInstanceIdentifier (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)29 NumericOperand (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev171207.NumericOperand)20 MapNode (org.opendaylight.yangtools.yang.data.api.schema.MapNode)17 DestinationFlowspecBuilder (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev171207.flowspec.destination.ipv4.DestinationFlowspecBuilder)12 ContainerNode (org.opendaylight.yangtools.yang.data.api.schema.ContainerNode)11 NormalizedNode (org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)10 DOMStoreWriteTransaction (org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction)9 NodeIdentifier (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier)9 DOMDataWriteTransaction (org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction)7 DOMStoreReadWriteTransaction (org.opendaylight.mdsal.dom.spi.store.DOMStoreReadWriteTransaction)6 Ipv4Prefix (org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix)6 BitmaskOperand (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev171207.BitmaskOperand)6 BigInteger (java.math.BigInteger)5 TransactionCommitFailedException (org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException)5