Search in sources :

Example 11 with YangInstanceIdentifier

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

the class EntityOwnershipShard method removeCandidateFromEntities.

private void removeCandidateFromEntities(final MemberName member) {
    final List<Modification> modifications = new ArrayList<>();
    searchForEntities((entityTypeNode, entityNode) -> {
        if (hasCandidate(entityNode, member)) {
            YangInstanceIdentifier entityId = (YangInstanceIdentifier) entityNode.getIdentifier().getKeyValues().get(ENTITY_ID_QNAME);
            YangInstanceIdentifier candidatePath = candidatePath(entityTypeNode.getIdentifier().getKeyValues().get(ENTITY_TYPE_QNAME).toString(), entityId, member.getName());
            LOG.info("{}: Found entity {}, removing candidate {}, path {}", persistenceId(), entityId, member, candidatePath);
            modifications.add(new DeleteModification(candidatePath));
        }
    });
    commitCoordinator.commitModifications(modifications, this);
}
Also used : Modification(org.opendaylight.controller.cluster.datastore.modification.Modification) DeleteModification(org.opendaylight.controller.cluster.datastore.modification.DeleteModification) MergeModification(org.opendaylight.controller.cluster.datastore.modification.MergeModification) WriteModification(org.opendaylight.controller.cluster.datastore.modification.WriteModification) ArrayList(java.util.ArrayList) DeleteModification(org.opendaylight.controller.cluster.datastore.modification.DeleteModification) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)

Example 12 with YangInstanceIdentifier

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

the class CandidateListChangeListener method onDataTreeChanged.

@Override
public void onDataTreeChanged(Collection<DataTreeCandidate> changes) {
    for (DataTreeCandidate change : changes) {
        DataTreeCandidateNode changeRoot = change.getRootNode();
        ModificationType type = changeRoot.getModificationType();
        LOG.debug("{}: Candidate node changed: {}, {}", logId, type, change.getRootPath());
        NodeIdentifierWithPredicates candidateKey = (NodeIdentifierWithPredicates) change.getRootPath().getLastPathArgument();
        String candidate = candidateKey.getKeyValues().get(CANDIDATE_NAME_QNAME).toString();
        YangInstanceIdentifier entityId = extractEntityPath(change.getRootPath());
        if (type == ModificationType.WRITE || type == ModificationType.APPEARED) {
            LOG.debug("{}: Candidate {} was added for entity {}", logId, candidate, entityId);
            Collection<String> newCandidates = addToCurrentCandidates(entityId, candidate);
            shard.tell(new CandidateAdded(entityId, candidate, new ArrayList<>(newCandidates)), shard);
        } else if (type == ModificationType.DELETE || type == ModificationType.DISAPPEARED) {
            LOG.debug("{}: Candidate {} was removed for entity {}", logId, candidate, entityId);
            Collection<String> newCandidates = removeFromCurrentCandidates(entityId, candidate);
            shard.tell(new CandidateRemoved(entityId, candidate, new ArrayList<>(newCandidates)), shard);
        }
    }
}
Also used : DataTreeCandidate(org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate) ModificationType(org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType) CandidateAdded(org.opendaylight.controller.cluster.datastore.entityownership.messages.CandidateAdded) DataTreeCandidateNode(org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode) ArrayList(java.util.ArrayList) Collection(java.util.Collection) CandidateRemoved(org.opendaylight.controller.cluster.datastore.entityownership.messages.CandidateRemoved) NodeIdentifierWithPredicates(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)

Example 13 with YangInstanceIdentifier

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

the class DataChanged method readExternal.

@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
    // Read the version
    in.readShort();
    NormalizedNodeDataInput streamReader = NormalizedNodeInputOutput.newDataInputWithoutValidation(in);
    // Note: the scope passed to builder is not actually used.
    Builder builder = DOMImmutableDataChangeEvent.builder(DataChangeScope.SUBTREE);
    // Read created data
    int size = in.readInt();
    for (int i = 0; i < size; i++) {
        YangInstanceIdentifier path = streamReader.readYangInstanceIdentifier();
        NormalizedNode<?, ?> node = streamReader.readNormalizedNode();
        builder.addCreated(path, node);
    }
    // Read updated data
    size = in.readInt();
    for (int i = 0; i < size; i++) {
        YangInstanceIdentifier path = streamReader.readYangInstanceIdentifier();
        NormalizedNode<?, ?> before = streamReader.readNormalizedNode();
        NormalizedNode<?, ?> after = streamReader.readNormalizedNode();
        builder.addUpdated(path, before, after);
    }
    // Read removed data
    size = in.readInt();
    for (int i = 0; i < size; i++) {
        YangInstanceIdentifier path = streamReader.readYangInstanceIdentifier();
        NormalizedNode<?, ?> node = streamReader.readNormalizedNode();
        builder.addRemoved(path, node);
    }
    // Read original subtree
    boolean present = in.readBoolean();
    if (present) {
        builder.setBefore(streamReader.readNormalizedNode());
    }
    // Read updated subtree
    present = in.readBoolean();
    if (present) {
        builder.setAfter(streamReader.readNormalizedNode());
    }
    change = builder.build();
}
Also used : Builder(org.opendaylight.controller.md.sal.dom.store.impl.DOMImmutableDataChangeEvent.Builder) NormalizedNodeDataInput(org.opendaylight.controller.cluster.datastore.node.utils.stream.NormalizedNodeDataInput) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)

Example 14 with YangInstanceIdentifier

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

the class DataChanged method writeExternal.

@Override
public void writeExternal(ObjectOutput out) throws IOException {
    out.writeShort(DataStoreVersions.CURRENT_VERSION);
    NormalizedNodeDataOutput streamWriter = NormalizedNodeInputOutput.newDataOutput(out);
    // Write created data
    Map<YangInstanceIdentifier, NormalizedNode<?, ?>> createdData = change.getCreatedData();
    out.writeInt(createdData.size());
    for (Map.Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> e : createdData.entrySet()) {
        streamWriter.writeYangInstanceIdentifier(e.getKey());
        streamWriter.writeNormalizedNode(e.getValue());
    }
    // Write updated data
    Map<YangInstanceIdentifier, NormalizedNode<?, ?>> originalData = change.getOriginalData();
    Map<YangInstanceIdentifier, NormalizedNode<?, ?>> updatedData = change.getUpdatedData();
    out.writeInt(updatedData.size());
    for (Map.Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> e : updatedData.entrySet()) {
        streamWriter.writeYangInstanceIdentifier(e.getKey());
        streamWriter.writeNormalizedNode(originalData.get(e.getKey()));
        streamWriter.writeNormalizedNode(e.getValue());
    }
    // Write removed data
    Set<YangInstanceIdentifier> removed = change.getRemovedPaths();
    out.writeInt(removed.size());
    for (YangInstanceIdentifier path : removed) {
        streamWriter.writeYangInstanceIdentifier(path);
        streamWriter.writeNormalizedNode(originalData.get(path));
    }
    // Write original subtree
    NormalizedNode<?, ?> originalSubtree = change.getOriginalSubtree();
    out.writeBoolean(originalSubtree != null);
    if (originalSubtree != null) {
        streamWriter.writeNormalizedNode(originalSubtree);
    }
    // Write original subtree
    NormalizedNode<?, ?> updatedSubtree = change.getUpdatedSubtree();
    out.writeBoolean(updatedSubtree != null);
    if (updatedSubtree != null) {
        streamWriter.writeNormalizedNode(updatedSubtree);
    }
}
Also used : NormalizedNodeDataOutput(org.opendaylight.controller.cluster.datastore.node.utils.stream.NormalizedNodeDataOutput) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) Map(java.util.Map)

Example 15 with YangInstanceIdentifier

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

the class ShardDataTreeTest method testPipelinedTransactionsWithCoordinatedCommits.

@Test
public void testPipelinedTransactionsWithCoordinatedCommits() throws Exception {
    final ShardDataTreeCohort cohort1 = newShardDataTreeCohort(snapshot -> snapshot.write(CarsModel.BASE_PATH, CarsModel.emptyContainer()));
    final ShardDataTreeCohort cohort2 = newShardDataTreeCohort(snapshot -> snapshot.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode()));
    NormalizedNode<?, ?> peopleNode = PeopleModel.create();
    final ShardDataTreeCohort cohort3 = newShardDataTreeCohort(snapshot -> snapshot.write(PeopleModel.BASE_PATH, peopleNode));
    YangInstanceIdentifier carPath = CarsModel.newCarPath("optima");
    MapEntryNode carNode = CarsModel.newCarEntry("optima", new BigInteger("100"));
    final ShardDataTreeCohort cohort4 = newShardDataTreeCohort(snapshot -> snapshot.write(carPath, carNode));
    immediateCanCommit(cohort1);
    final FutureCallback<Void> canCommitCallback2 = coordinatedCanCommit(cohort2);
    final FutureCallback<Void> canCommitCallback3 = coordinatedCanCommit(cohort3);
    final FutureCallback<Void> canCommitCallback4 = coordinatedCanCommit(cohort4);
    final FutureCallback<DataTreeCandidate> preCommitCallback1 = coordinatedPreCommit(cohort1);
    verify(preCommitCallback1).onSuccess(cohort1.getCandidate());
    verify(canCommitCallback2).onSuccess(null);
    final FutureCallback<DataTreeCandidate> preCommitCallback2 = coordinatedPreCommit(cohort2);
    verify(preCommitCallback2).onSuccess(cohort2.getCandidate());
    verify(canCommitCallback3).onSuccess(null);
    final FutureCallback<DataTreeCandidate> preCommitCallback3 = coordinatedPreCommit(cohort3);
    verify(preCommitCallback3).onSuccess(cohort3.getCandidate());
    verify(canCommitCallback4).onSuccess(null);
    final FutureCallback<DataTreeCandidate> preCommitCallback4 = coordinatedPreCommit(cohort4);
    verify(preCommitCallback4).onSuccess(cohort4.getCandidate());
    final FutureCallback<UnsignedLong> commitCallback2 = coordinatedCommit(cohort2);
    verify(mockShard, never()).persistPayload(eq(cohort1.getIdentifier()), any(CommitTransactionPayload.class), anyBoolean());
    verifyNoMoreInteractions(commitCallback2);
    final FutureCallback<UnsignedLong> commitCallback4 = coordinatedCommit(cohort4);
    verify(mockShard, never()).persistPayload(eq(cohort4.getIdentifier()), any(CommitTransactionPayload.class), anyBoolean());
    verifyNoMoreInteractions(commitCallback4);
    final FutureCallback<UnsignedLong> commitCallback1 = coordinatedCommit(cohort1);
    InOrder inOrder = inOrder(mockShard);
    inOrder.verify(mockShard).persistPayload(eq(cohort1.getIdentifier()), any(CommitTransactionPayload.class), eq(true));
    inOrder.verify(mockShard).persistPayload(eq(cohort2.getIdentifier()), any(CommitTransactionPayload.class), eq(false));
    verifyNoMoreInteractions(commitCallback1);
    verifyNoMoreInteractions(commitCallback2);
    final FutureCallback<UnsignedLong> commitCallback3 = coordinatedCommit(cohort3);
    inOrder = inOrder(mockShard);
    inOrder.verify(mockShard).persistPayload(eq(cohort3.getIdentifier()), any(CommitTransactionPayload.class), eq(true));
    inOrder.verify(mockShard).persistPayload(eq(cohort4.getIdentifier()), any(CommitTransactionPayload.class), eq(false));
    verifyNoMoreInteractions(commitCallback3);
    verifyNoMoreInteractions(commitCallback4);
    final ShardDataTreeCohort cohort5 = newShardDataTreeCohort(snapshot -> snapshot.merge(CarsModel.BASE_PATH, CarsModel.emptyContainer()));
    final FutureCallback<Void> canCommitCallback5 = coordinatedCanCommit(cohort5);
    // The payload instance doesn't matter - it just needs to be of type CommitTransactionPayload.
    CommitTransactionPayload mockPayload = CommitTransactionPayload.create(nextTransactionId(), cohort1.getCandidate());
    shardDataTree.applyReplicatedPayload(cohort1.getIdentifier(), mockPayload);
    shardDataTree.applyReplicatedPayload(cohort2.getIdentifier(), mockPayload);
    shardDataTree.applyReplicatedPayload(cohort3.getIdentifier(), mockPayload);
    shardDataTree.applyReplicatedPayload(cohort4.getIdentifier(), mockPayload);
    inOrder = inOrder(commitCallback1, commitCallback2, commitCallback3, commitCallback4);
    inOrder.verify(commitCallback1).onSuccess(any(UnsignedLong.class));
    inOrder.verify(commitCallback2).onSuccess(any(UnsignedLong.class));
    inOrder.verify(commitCallback3).onSuccess(any(UnsignedLong.class));
    inOrder.verify(commitCallback4).onSuccess(any(UnsignedLong.class));
    verify(canCommitCallback5).onSuccess(null);
    final DataTreeSnapshot snapshot = shardDataTree.newReadOnlyTransaction(nextTransactionId()).getSnapshot();
    Optional<NormalizedNode<?, ?>> optional = snapshot.readNode(carPath);
    assertEquals("Car node present", true, optional.isPresent());
    assertEquals("Car node", carNode, optional.get());
    optional = snapshot.readNode(PeopleModel.BASE_PATH);
    assertEquals("People node present", true, optional.isPresent());
    assertEquals("People node", peopleNode, optional.get());
}
Also used : InOrder(org.mockito.InOrder) UnsignedLong(com.google.common.primitives.UnsignedLong) MapEntryNode(org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) DataTreeCandidate(org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate) BigInteger(java.math.BigInteger) CommitTransactionPayload(org.opendaylight.controller.cluster.datastore.persisted.CommitTransactionPayload) DataTreeSnapshot(org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) Test(org.junit.Test)

Aggregations

YangInstanceIdentifier (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)187 Test (org.junit.Test)111 NormalizedNode (org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)34 MapEntryNode (org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode)21 ActorRef (akka.actor.ActorRef)19 NodeIdentifierWithPredicates (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates)19 ContainerNode (org.opendaylight.yangtools.yang.data.api.schema.ContainerNode)15 TestActorRef (akka.testkit.TestActorRef)14 TestKit (akka.testkit.javadsl.TestKit)13 WriteModification (org.opendaylight.controller.cluster.datastore.modification.WriteModification)12 ArrayList (java.util.ArrayList)11 RegisterDataTreeNotificationListenerReply (org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeNotificationListenerReply)10 DeleteModification (org.opendaylight.controller.cluster.datastore.modification.DeleteModification)10 ActorContext (org.opendaylight.controller.cluster.datastore.utils.ActorContext)10 MapNode (org.opendaylight.yangtools.yang.data.api.schema.MapNode)10 AbstractTest (org.opendaylight.controller.cluster.datastore.AbstractTest)9 RegisterChangeListener (org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListener)9 DOMDataWriteTransaction (org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction)9 DOMStoreWriteTransaction (org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction)9 NodeIdentifier (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier)9