Search in sources :

Example 61 with NormalizedNode

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

the class EntityOwnershipStatistics method onDataTreeChanged.

@Override
public void onDataTreeChanged(@Nonnull final Collection<DataTreeCandidate> changes) {
    for (DataTreeCandidate change : changes) {
        DataTreeCandidateNode changeRoot = change.getRootNode();
        LeafNode<?> ownerLeaf = (LeafNode<?>) changeRoot.getDataAfter().get();
        String entityType = entityTypeFromEntityPath(change.getRootPath());
        String newOwner = extractOwner(ownerLeaf);
        if (!Strings.isNullOrEmpty(newOwner)) {
            updateStatistics(entityType, newOwner, 1);
        }
        Optional<NormalizedNode<?, ?>> dataBefore = changeRoot.getDataBefore();
        if (dataBefore.isPresent()) {
            String origOwner = extractOwner((LeafNode<?>) changeRoot.getDataBefore().get());
            if (!Strings.isNullOrEmpty(origOwner)) {
                updateStatistics(entityType, origOwner, -1);
            }
        }
    }
}
Also used : DataTreeCandidate(org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate) DataTreeCandidateNode(org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode) LeafNode(org.opendaylight.yangtools.yang.data.api.schema.LeafNode) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)

Example 62 with NormalizedNode

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

the class DistributedEntityOwnershipService method getOwnershipState.

@Override
public Optional<EntityOwnershipState> getOwnershipState(final DOMEntity forEntity) {
    Preconditions.checkNotNull(forEntity, "forEntity cannot be null");
    DataTree dataTree = getLocalEntityOwnershipShardDataTree();
    if (dataTree == null) {
        return Optional.absent();
    }
    java.util.Optional<NormalizedNode<?, ?>> entityNode = dataTree.takeSnapshot().readNode(entityPath(forEntity.getType(), forEntity.getIdentifier()));
    if (!entityNode.isPresent()) {
        return Optional.absent();
    }
    // Check if there are any candidates, if there are none we do not really have ownership state
    final MapEntryNode entity = (MapEntryNode) entityNode.get();
    final java.util.Optional<DataContainerChild<? extends PathArgument, ?>> optionalCandidates = entity.getChild(CANDIDATE_NODE_ID);
    final boolean hasCandidates = optionalCandidates.isPresent() && ((MapNode) optionalCandidates.get()).getValue().size() > 0;
    if (!hasCandidates) {
        return Optional.absent();
    }
    MemberName localMemberName = context.getCurrentMemberName();
    java.util.Optional<DataContainerChild<? extends PathArgument, ?>> ownerLeaf = entity.getChild(ENTITY_OWNER_NODE_ID);
    String owner = ownerLeaf.isPresent() ? ownerLeaf.get().getValue().toString() : null;
    boolean hasOwner = !Strings.isNullOrEmpty(owner);
    boolean isOwner = hasOwner && localMemberName.getName().equals(owner);
    return Optional.of(EntityOwnershipState.from(isOwner, hasOwner));
}
Also used : DataTree(org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree) GetShardDataTree(org.opendaylight.controller.cluster.datastore.messages.GetShardDataTree) DataContainerChild(org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild) PathArgument(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument) MemberName(org.opendaylight.controller.cluster.access.concepts.MemberName) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) MapEntryNode(org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode)

Example 63 with NormalizedNode

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

the class EntityOwnerChangeListener method onDataTreeChanged.

@Override
public void onDataTreeChanged(final Collection<DataTreeCandidate> changes) {
    for (DataTreeCandidate change : changes) {
        DataTreeCandidateNode changeRoot = change.getRootNode();
        LeafNode<?> ownerLeaf = (LeafNode<?>) changeRoot.getDataAfter().get();
        LOG.debug("{}: Entity node changed: {}, {}", logId(), changeRoot.getModificationType(), change.getRootPath());
        String newOwner = extractOwner(ownerLeaf);
        String origOwner = null;
        Optional<NormalizedNode<?, ?>> dataBefore = changeRoot.getDataBefore();
        if (dataBefore.isPresent()) {
            origOwner = extractOwner((LeafNode<?>) changeRoot.getDataBefore().get());
        }
        LOG.debug("{}: New owner: {}, Original owner: {}", logId(), newOwner, origOwner);
        if (!Objects.equals(origOwner, newOwner)) {
            boolean isOwner = localMemberName.equals(newOwner);
            boolean wasOwner = localMemberName.equals(origOwner);
            boolean hasOwner = !Strings.isNullOrEmpty(newOwner);
            DOMEntity entity = createEntity(change.getRootPath());
            LOG.debug("{}: Calling notifyEntityOwnershipListeners: entity: {}, wasOwner: {}, isOwner: {}, hasOwner: {}", logId(), entity, wasOwner, isOwner, hasOwner);
            publisher.notifyEntityOwnershipListeners(entity, wasOwner, isOwner, hasOwner);
        }
    }
}
Also used : DataTreeCandidate(org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate) DataTreeCandidateNode(org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode) LeafNode(org.opendaylight.yangtools.yang.data.api.schema.LeafNode) DOMEntity(org.opendaylight.mdsal.eos.dom.api.DOMEntity) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)

Example 64 with NormalizedNode

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

the class DOMBrokerPerformanceTest method measureOneTransactionTopContainer.

private void measureOneTransactionTopContainer() throws Exception {
    final DOMDataReadWriteTransaction writeTx = measure("Txs:1 Allocate", () -> domBroker.newReadWriteTransaction());
    measure("Txs:1 Write", (Callable<Void>) () -> {
        writeTx.put(OPERATIONAL, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
        writeTx.put(OPERATIONAL, TestModel.OUTER_LIST_PATH, ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build());
        return null;
    });
    measure("Txs:1 Reads:1", (Callable<Void>) () -> {
        // Reads /test in writeTx
        ListenableFuture<Optional<NormalizedNode<?, ?>>> writeTxContainer = writeTx.read(OPERATIONAL, TestModel.TEST_PATH);
        assertTrue(writeTxContainer.get().isPresent());
        return null;
    });
    measure("Txs:1 Reads:1", (Callable<Void>) () -> {
        // Reads /test in writeTx
        ListenableFuture<Optional<NormalizedNode<?, ?>>> writeTxContainer = writeTx.read(OPERATIONAL, TestModel.TEST_PATH);
        assertTrue(writeTxContainer.get().isPresent());
        return null;
    });
    measure("Txs:1 Submit, Finish", (Callable<Void>) () -> {
        measure("Txs:1 Submit", (Callable<ListenableFuture<?>>) () -> writeTx.submit()).get();
        return null;
    });
}
Also used : DOMDataReadWriteTransaction(org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)

Example 65 with NormalizedNode

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

the class RoutedDOMRpcRoutingTableEntry method invokeRpc.

@Override
protected CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(final NormalizedNode<?, ?> input) {
    final Optional<NormalizedNode<?, ?>> maybeKey = NormalizedNodes.findNode(input, keyId);
    // Routing key is present, attempt to deliver as a routed RPC
    if (maybeKey.isPresent()) {
        final NormalizedNode<?, ?> key = maybeKey.get();
        final Object value = key.getValue();
        if (value instanceof YangInstanceIdentifier) {
            final YangInstanceIdentifier iid = (YangInstanceIdentifier) value;
            // Find a DOMRpcImplementation for a specific iid
            final List<DOMRpcImplementation> specificImpls = getImplementations(iid);
            if (specificImpls != null) {
                return specificImpls.get(0).invokeRpc(DOMRpcIdentifier.create(getSchemaPath(), iid), input);
            }
            LOG.debug("No implementation for context {} found will now look for wildcard id", iid);
            // Find a DOMRpcImplementation for a wild card. Usually remote-rpc-connector would register an
            // implementation this way
            final List<DOMRpcImplementation> mayBeRemoteImpls = getImplementations(YangInstanceIdentifier.EMPTY);
            if (mayBeRemoteImpls != null) {
                return mayBeRemoteImpls.get(0).invokeRpc(DOMRpcIdentifier.create(getSchemaPath(), iid), input);
            }
        } else {
            LOG.warn("Ignoring wrong context value {}", value);
        }
    }
    final List<DOMRpcImplementation> impls = getImplementations(null);
    if (impls != null) {
        return impls.get(0).invokeRpc(globalRpcId, input);
    } else {
        return Futures.<DOMRpcResult, DOMRpcException>immediateFailedCheckedFuture(new DOMRpcImplementationNotAvailableException("No implementation of RPC %s available", getSchemaPath()));
    }
}
Also used : DOMRpcResult(org.opendaylight.controller.md.sal.dom.api.DOMRpcResult) DOMRpcImplementation(org.opendaylight.controller.md.sal.dom.api.DOMRpcImplementation) DOMRpcImplementationNotAvailableException(org.opendaylight.controller.md.sal.dom.api.DOMRpcImplementationNotAvailableException) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) DOMRpcException(org.opendaylight.controller.md.sal.dom.api.DOMRpcException) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)

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