Search in sources :

Example 1 with ModificationType

use of org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType 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 2 with ModificationType

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

the class ShardDataTreeTest method testListenerNotifiedOnApplySnapshot.

@Test
public void testListenerNotifiedOnApplySnapshot() throws Exception {
    immediatePayloadReplication(shardDataTree, mockShard);
    DOMDataTreeChangeListener listener = mock(DOMDataTreeChangeListener.class);
    shardDataTree.registerTreeChangeListener(CarsModel.CAR_LIST_PATH.node(CarsModel.CAR_QNAME), listener, com.google.common.base.Optional.absent(), noop -> {
    });
    addCar(shardDataTree, "optima");
    verifyOnDataTreeChanged(listener, dtc -> {
        assertEquals("getModificationType", ModificationType.WRITE, dtc.getRootNode().getModificationType());
        assertEquals("getRootPath", CarsModel.newCarPath("optima"), dtc.getRootPath());
    });
    addCar(shardDataTree, "sportage");
    verifyOnDataTreeChanged(listener, dtc -> {
        assertEquals("getModificationType", ModificationType.WRITE, dtc.getRootNode().getModificationType());
        assertEquals("getRootPath", CarsModel.newCarPath("sportage"), dtc.getRootPath());
    });
    ShardDataTree newDataTree = new ShardDataTree(mockShard, fullSchema, TreeType.OPERATIONAL);
    immediatePayloadReplication(newDataTree, mockShard);
    addCar(newDataTree, "optima");
    addCar(newDataTree, "murano");
    shardDataTree.applySnapshot(newDataTree.takeStateSnapshot());
    Map<YangInstanceIdentifier, ModificationType> expChanges = Maps.newHashMap();
    expChanges.put(CarsModel.newCarPath("optima"), ModificationType.WRITE);
    expChanges.put(CarsModel.newCarPath("murano"), ModificationType.WRITE);
    expChanges.put(CarsModel.newCarPath("sportage"), ModificationType.DELETE);
    verifyOnDataTreeChanged(listener, dtc -> {
        ModificationType expType = expChanges.remove(dtc.getRootPath());
        assertNotNull("Got unexpected change for " + dtc.getRootPath(), expType);
        assertEquals("getModificationType", expType, dtc.getRootNode().getModificationType());
    });
    if (!expChanges.isEmpty()) {
        fail("Missing change notifications: " + expChanges);
    }
}
Also used : ModificationType(org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType) DOMDataTreeChangeListener(org.opendaylight.mdsal.dom.api.DOMDataTreeChangeListener) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) Test(org.junit.Test)

Example 3 with ModificationType

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

the class ResolveDataChangeEventsTask method resolveAnyChangeEvent.

/**
 * Resolves data change event for supplied node.
 *
 * @param path
 *            Path to current node in tree
 * @param listeners
 *            Collection of Listener registration nodes interested in
 *            subtree
 * @param modification
 *            Modification of current node
 * @param before
 *            - Original (before) state of current node
 * @param after
 *            - After state of current node
 * @return True if the subtree changed, false otherwise
 */
private boolean resolveAnyChangeEvent(final ResolveDataChangeState state, final DataTreeCandidateNode node) {
    final Optional<NormalizedNode<?, ?>> maybeBefore = node.getDataBefore();
    final Optional<NormalizedNode<?, ?>> maybeAfter = node.getDataAfter();
    final ModificationType type = node.getModificationType();
    if (type != ModificationType.UNMODIFIED && !maybeAfter.isPresent() && !maybeBefore.isPresent()) {
        LOG.debug("Modification at {} has type {}, but no before- and after-data. Assuming unchanged.", state.getPath(), type);
        return false;
    }
    switch(type) {
        case SUBTREE_MODIFIED:
            return resolveSubtreeChangeEvent(state, node);
        case APPEARED:
        case WRITE:
            Preconditions.checkArgument(maybeAfter.isPresent(), "Modification at {} has type {} but no after-data", state.getPath(), type);
            if (!maybeBefore.isPresent()) {
                @SuppressWarnings({ "unchecked", "rawtypes" }) final NormalizedNode<PathArgument, ?> afterNode = (NormalizedNode) maybeAfter.get();
                resolveSameEventRecursivelly(state, afterNode, DOMImmutableDataChangeEvent.getCreateEventFactory());
                return true;
            }
            return resolveReplacedEvent(state, maybeBefore.get(), maybeAfter.get());
        case DISAPPEARED:
        case DELETE:
            Preconditions.checkArgument(maybeBefore.isPresent(), "Modification at {} has type {} but no before-data", state.getPath(), type);
            @SuppressWarnings({ "unchecked", "rawtypes" }) final NormalizedNode<PathArgument, ?> beforeNode = (NormalizedNode) maybeBefore.get();
            resolveSameEventRecursivelly(state, beforeNode, DOMImmutableDataChangeEvent.getRemoveEventFactory());
            return true;
        case UNMODIFIED:
            return false;
        default:
            break;
    }
    throw new IllegalStateException(String.format("Unhandled node state %s at %s", type, state.getPath()));
}
Also used : ModificationType(org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType) PathArgument(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)

Aggregations

ModificationType (org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType)3 YangInstanceIdentifier (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)2 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Test (org.junit.Test)1 CandidateAdded (org.opendaylight.controller.cluster.datastore.entityownership.messages.CandidateAdded)1 CandidateRemoved (org.opendaylight.controller.cluster.datastore.entityownership.messages.CandidateRemoved)1 DOMDataTreeChangeListener (org.opendaylight.mdsal.dom.api.DOMDataTreeChangeListener)1 NodeIdentifierWithPredicates (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates)1 PathArgument (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument)1 NormalizedNode (org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)1 DataTreeCandidate (org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate)1 DataTreeCandidateNode (org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode)1