Search in sources :

Example 51 with DefaultDOMRpcResult

use of org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult in project netconf by opendaylight.

the class ProxyDOMRpcService method invokeRpc.

@Override
public FluentFuture<DOMRpcResult> invokeRpc(final QName type, final NormalizedNode input) {
    LOG.trace("{}: Rpc operation invoked with schema type: {} and node: {}.", id, type, input);
    final NormalizedNodeMessage normalizedNodeMessage = input != null ? new NormalizedNodeMessage(YangInstanceIdentifier.empty(), input) : null;
    final Future<Object> scalaFuture = Patterns.ask(masterActorRef, new InvokeRpcMessage(new SchemaPathMessage(type), normalizedNodeMessage), actorResponseWaitTime);
    final SettableFuture<DOMRpcResult> settableFuture = SettableFuture.create();
    scalaFuture.onComplete(new OnComplete<>() {

        @Override
        public void onComplete(final Throwable failure, final Object response) {
            if (failure != null) {
                if (failure instanceof ClusteringRpcException) {
                    settableFuture.setException(failure);
                } else {
                    settableFuture.setException(new ClusteringRpcException(id + ": Exception during remote rpc invocation.", failure));
                }
                return;
            }
            if (response instanceof EmptyResultResponse) {
                settableFuture.set(null);
                return;
            }
            final Collection<? extends RpcError> errors = ((InvokeRpcMessageReply) response).getRpcErrors();
            final NormalizedNodeMessage normalizedNodeMessageResult = ((InvokeRpcMessageReply) response).getNormalizedNodeMessage();
            final DOMRpcResult result;
            if (normalizedNodeMessageResult == null) {
                result = new DefaultDOMRpcResult(ImmutableList.copyOf(errors));
            } else {
                result = new DefaultDOMRpcResult(normalizedNodeMessageResult.getNode(), errors);
            }
            settableFuture.set(result);
        }
    }, actorSystem.dispatcher());
    return FluentFuture.from(settableFuture);
}
Also used : DefaultDOMRpcResult(org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult) NormalizedNodeMessage(org.opendaylight.netconf.topology.singleton.messages.NormalizedNodeMessage) DefaultDOMRpcResult(org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult) DOMRpcResult(org.opendaylight.mdsal.dom.api.DOMRpcResult) SchemaPathMessage(org.opendaylight.netconf.topology.singleton.messages.SchemaPathMessage) RpcError(org.opendaylight.yangtools.yang.common.RpcError) EmptyResultResponse(org.opendaylight.netconf.topology.singleton.messages.transactions.EmptyResultResponse) InvokeRpcMessage(org.opendaylight.netconf.topology.singleton.messages.rpc.InvokeRpcMessage) ClusteringRpcException(org.opendaylight.netconf.topology.singleton.impl.utils.ClusteringRpcException) Collection(java.util.Collection)

Example 52 with DefaultDOMRpcResult

use of org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult in project netconf by opendaylight.

the class RestconfImpl method invokeSalRemoteRpcSubscribeRPC.

private ListenableFuture<DOMRpcResult> invokeSalRemoteRpcSubscribeRPC(final NormalizedNodeContext payload) {
    final ContainerNode value = (ContainerNode) payload.getData();
    final QName rpcQName = payload.getInstanceIdentifierContext().getSchemaNode().getQName();
    final Optional<DataContainerChild> path = value.findChildByArg(new NodeIdentifier(QName.create(rpcQName, "path")));
    final Object pathValue = path.isPresent() ? path.get().body() : null;
    if (!(pathValue instanceof YangInstanceIdentifier)) {
        LOG.debug("Instance identifier {} was not normalized correctly", rpcQName);
        throw new RestconfDocumentedException("Instance identifier was not normalized correctly", ErrorType.APPLICATION, ErrorTag.OPERATION_FAILED);
    }
    final YangInstanceIdentifier pathIdentifier = (YangInstanceIdentifier) pathValue;
    String streamName = (String) CREATE_DATA_SUBSCR;
    NotificationOutputType outputType = null;
    if (!pathIdentifier.isEmpty()) {
        final String fullRestconfIdentifier = DATA_SUBSCR + controllerContext.toFullRestconfIdentifier(pathIdentifier, null);
        LogicalDatastoreType datastore = parseEnumTypeParameter(value, LogicalDatastoreType.class, DATASTORE_PARAM_NAME);
        datastore = datastore == null ? DEFAULT_DATASTORE : datastore;
        Scope scope = parseEnumTypeParameter(value, Scope.class, SCOPE_PARAM_NAME);
        scope = scope == null ? Scope.BASE : scope;
        outputType = parseEnumTypeParameter(value, NotificationOutputType.class, OUTPUT_TYPE_PARAM_NAME);
        outputType = outputType == null ? NotificationOutputType.XML : outputType;
        streamName = Notificator.createStreamNameFromUri(fullRestconfIdentifier + "/datastore=" + datastore + "/scope=" + scope);
    }
    if (Strings.isNullOrEmpty(streamName)) {
        LOG.debug("Path is empty or contains value node which is not Container or List built-in type at {}", pathIdentifier);
        throw new RestconfDocumentedException("Path is empty or contains value node which is not Container or List " + "built-in type.", ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
    }
    final QName outputQname = QName.create(rpcQName, "output");
    final QName streamNameQname = QName.create(rpcQName, "stream-name");
    final ContainerNode output = ImmutableContainerNodeBuilder.create().withNodeIdentifier(new NodeIdentifier(outputQname)).withChild(ImmutableNodes.leafNode(streamNameQname, streamName)).build();
    if (!Notificator.existListenerFor(streamName)) {
        Notificator.createListener(pathIdentifier, streamName, outputType, controllerContext);
    }
    return Futures.immediateFuture(new DefaultDOMRpcResult(output));
}
Also used : DefaultDOMRpcResult(org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult) RestconfDocumentedException(org.opendaylight.restconf.common.errors.RestconfDocumentedException) QName(org.opendaylight.yangtools.yang.common.QName) NotificationOutputType(org.opendaylight.yang.gen.v1.urn.sal.restconf.event.subscription.rev140708.NotificationOutputTypeGrouping.NotificationOutputType) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) Scope(org.opendaylight.yang.gen.v1.urn.sal.restconf.event.subscription.rev140708.CreateDataChangeEventSubscriptionInput1.Scope) DataContainerChild(org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild) NodeIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier) ContainerNode(org.opendaylight.yangtools.yang.data.api.schema.ContainerNode) LogicalDatastoreType(org.opendaylight.mdsal.common.api.LogicalDatastoreType)

Example 53 with DefaultDOMRpcResult

use of org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult in project netconf by opendaylight.

the class CreateStreamUtil method createDataChangeNotifiStream.

/**
 * Create data-change-event or notification stream with POST operation via RPC.
 *
 * @param payload      Input of RPC - example in JSON (data-change-event stream):
 *                     <pre>
 *                     {@code
 *                         {
 *                             "input": {
 *                                 "path": "/toaster:toaster/toaster:toasterStatus",
 *                                 "sal-remote-augment:datastore": "OPERATIONAL",
 *                                 "sal-remote-augment:scope": "ONE"
 *                             }
 *                         }
 *                     }
 *                     </pre>
 * @param refSchemaCtx Reference to {@link EffectiveModelContext}.
 * @return {@link DOMRpcResult} - Output of RPC - example in JSON:
 *     <pre>
 *     {@code
 *         {
 *             "output": {
 *                 "stream-name": "toaster:toaster/toaster:toasterStatus/datastore=OPERATIONAL/scope=ONE"
 *             }
 *         }
 *     }
 *     </pre>
 */
static DOMRpcResult createDataChangeNotifiStream(final NormalizedNodePayload payload, final EffectiveModelContext refSchemaCtx) {
    // parsing out of container with settings and path
    final ContainerNode data = (ContainerNode) requireNonNull(payload).getData();
    final QName qname = payload.getInstanceIdentifierContext().getSchemaNode().getQName();
    final YangInstanceIdentifier path = preparePath(data, qname);
    // building of stream name
    final StringBuilder streamNameBuilder = new StringBuilder(prepareDataChangeNotifiStreamName(path, requireNonNull(refSchemaCtx), data));
    final NotificationOutputType outputType = prepareOutputType(data);
    if (outputType.equals(NotificationOutputType.JSON)) {
        streamNameBuilder.append('/').append(outputType.getName());
    }
    final String streamName = streamNameBuilder.toString();
    // registration of the listener
    ListenersBroker.getInstance().registerDataChangeListener(path, streamName, outputType);
    // building of output
    final QName outputQname = QName.create(qname, "output");
    final QName streamNameQname = QName.create(qname, "stream-name");
    final ContainerNode output = ImmutableContainerNodeBuilder.create().withNodeIdentifier(new NodeIdentifier(outputQname)).withChild(ImmutableNodes.leafNode(streamNameQname, streamName)).build();
    return new DefaultDOMRpcResult(output);
}
Also used : DefaultDOMRpcResult(org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult) QName(org.opendaylight.yangtools.yang.common.QName) NodeIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier) ContainerNode(org.opendaylight.yangtools.yang.data.api.schema.ContainerNode) NotificationOutputType(org.opendaylight.yang.gen.v1.urn.sal.restconf.event.subscription.rev140708.NotificationOutputTypeGrouping.NotificationOutputType) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)

Example 54 with DefaultDOMRpcResult

use of org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult in project netconf by opendaylight.

the class RestconfInvokeOperationsServiceImplTest method invokeRpcViaMountPointTest.

@Test
public void invokeRpcViaMountPointTest() throws InterruptedException, ExecutionException {
    doReturn(Optional.ofNullable(rpcService)).when(mountPoint).getService(DOMRpcService.class);
    final DOMRpcResult mockResult = new DefaultDOMRpcResult(OUTPUT, List.of());
    doReturn(immediateFluentFuture(mockResult)).when(rpcService).invokeRpc(RPC, INPUT);
    final DOMRpcResult rpcResult = RestconfInvokeOperationsServiceImpl.invokeRpc(INPUT, RPC, mountPoint).get();
    assertTrue(rpcResult.getErrors().isEmpty());
    assertEquals(OUTPUT, rpcResult.getResult());
}
Also used : DefaultDOMRpcResult(org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult) DefaultDOMRpcResult(org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult) DOMRpcResult(org.opendaylight.mdsal.dom.api.DOMRpcResult) Test(org.junit.Test)

Example 55 with DefaultDOMRpcResult

use of org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult in project netconf by opendaylight.

the class RestconfInvokeOperationsServiceImplTest method checkResponseTest.

@Test
public void checkResponseTest() throws InterruptedException, ExecutionException {
    doReturn(immediateFluentFuture(new DefaultDOMRpcResult(OUTPUT, List.of()))).when(rpcService).invokeRpc(RPC, INPUT);
    final DOMRpcResult rpcResult = RestconfInvokeOperationsServiceImpl.invokeRpc(INPUT, RPC, rpcService).get();
    assertTrue(rpcResult.getErrors().isEmpty());
    assertEquals(OUTPUT, rpcResult.getResult());
}
Also used : DefaultDOMRpcResult(org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult) DefaultDOMRpcResult(org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult) DOMRpcResult(org.opendaylight.mdsal.dom.api.DOMRpcResult) Test(org.junit.Test)

Aggregations

DefaultDOMRpcResult (org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult)72 Test (org.junit.Test)51 QName (org.opendaylight.yangtools.yang.common.QName)27 DOMRpcResult (org.opendaylight.mdsal.dom.api.DOMRpcResult)25 ContainerNode (org.opendaylight.yangtools.yang.data.api.schema.ContainerNode)25 NormalizedNode (org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)16 InstanceIdentifierContext (org.opendaylight.restconf.common.context.InstanceIdentifierContext)15 NetconfRestconfStrategy (org.opendaylight.restconf.nb.rfc8040.rests.transactions.NetconfRestconfStrategy)15 NormalizedNodePayload (org.opendaylight.restconf.nb.rfc8040.legacy.NormalizedNodePayload)12 Before (org.junit.Before)10 NodeIdentifier (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier)10 DataSchemaNode (org.opendaylight.yangtools.yang.model.api.DataSchemaNode)10 NetconfBaseOps (org.opendaylight.netconf.sal.connect.netconf.util.NetconfBaseOps)9 MdsalRestconfStrategy (org.opendaylight.restconf.nb.rfc8040.rests.transactions.MdsalRestconfStrategy)9 YangInstanceIdentifier (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)9 AbstractBaseSchemasTest (org.opendaylight.netconf.sal.connect.netconf.AbstractBaseSchemasTest)5 RemoteDeviceId (org.opendaylight.netconf.sal.connect.util.RemoteDeviceId)5 RpcError (org.opendaylight.yangtools.yang.common.RpcError)5 ArrayList (java.util.ArrayList)4 NormalizedNodeMessage (org.opendaylight.netconf.topology.singleton.messages.NormalizedNodeMessage)4