Search in sources :

Example 16 with DOMRpcResult

use of org.opendaylight.mdsal.dom.api.DOMRpcResult in project netconf by opendaylight.

the class RestconfImpl method invokeRpc.

@Override
public NormalizedNodeContext invokeRpc(final String identifier, final NormalizedNodeContext payload, final UriInfo uriInfo) {
    if (payload == null) {
        // no payload specified, reroute this to no payload invokeRpc implementation
        return invokeRpc(identifier, uriInfo);
    }
    final SchemaNode schema = payload.getInstanceIdentifierContext().getSchemaNode();
    final ListenableFuture<? extends DOMRpcResult> response;
    final DOMMountPoint mountPoint = payload.getInstanceIdentifierContext().getMountPoint();
    final NormalizedNode input = nonnullInput(schema, payload.getData());
    final EffectiveModelContext schemaContext;
    if (mountPoint != null) {
        final Optional<DOMRpcService> mountRpcServices = mountPoint.getService(DOMRpcService.class);
        if (mountRpcServices.isEmpty()) {
            LOG.debug("Error: Rpc service is missing.");
            throw new RestconfDocumentedException("Rpc service is missing.");
        }
        schemaContext = modelContext(mountPoint);
        response = mountRpcServices.get().invokeRpc(schema.getQName(), input);
    } else {
        final XMLNamespace namespace = schema.getQName().getNamespace();
        if (namespace.toString().equals(SAL_REMOTE_NAMESPACE)) {
            if (identifier.contains(CREATE_DATA_SUBSCR)) {
                response = invokeSalRemoteRpcSubscribeRPC(payload);
            } else if (identifier.contains(CREATE_NOTIFICATION_STREAM)) {
                response = invokeSalRemoteRpcNotifiStrRPC(payload);
            } else {
                final String msg = "Not supported operation";
                LOG.warn(msg);
                throw new RestconfDocumentedException(msg, ErrorType.RPC, ErrorTag.OPERATION_NOT_SUPPORTED);
            }
        } else {
            response = broker.invokeRpc(schema.getQName(), input);
        }
        schemaContext = controllerContext.getGlobalSchema();
    }
    final DOMRpcResult result = checkRpcResponse(response);
    RpcDefinition resultNodeSchema = null;
    final NormalizedNode resultData;
    if (result != null && result.getResult() != null) {
        resultData = result.getResult();
        resultNodeSchema = (RpcDefinition) payload.getInstanceIdentifierContext().getSchemaNode();
    } else {
        resultData = null;
    }
    if (resultData != null && ((ContainerNode) resultData).isEmpty()) {
        throw new WebApplicationException(Response.Status.NO_CONTENT);
    } else {
        return new NormalizedNodeContext(new InstanceIdentifierContext<>(null, resultNodeSchema, mountPoint, schemaContext), resultData, QueryParametersParser.parseWriterParameters(uriInfo));
    }
}
Also used : RestconfDocumentedException(org.opendaylight.restconf.common.errors.RestconfDocumentedException) DOMRpcService(org.opendaylight.mdsal.dom.api.DOMRpcService) DefaultDOMRpcResult(org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult) DOMRpcResult(org.opendaylight.mdsal.dom.api.DOMRpcResult) RpcDefinition(org.opendaylight.yangtools.yang.model.api.RpcDefinition) WebApplicationException(javax.ws.rs.WebApplicationException) DOMMountPoint(org.opendaylight.mdsal.dom.api.DOMMountPoint) NormalizedNodeContext(org.opendaylight.netconf.sal.rest.impl.NormalizedNodeContext) ContainerSchemaNode(org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode) LeafListSchemaNode(org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode) ListSchemaNode(org.opendaylight.yangtools.yang.model.api.ListSchemaNode) SchemaNode(org.opendaylight.yangtools.yang.model.api.SchemaNode) DataSchemaNode(org.opendaylight.yangtools.yang.model.api.DataSchemaNode) LeafSchemaNode(org.opendaylight.yangtools.yang.model.api.LeafSchemaNode) XMLNamespace(org.opendaylight.yangtools.yang.common.XMLNamespace) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) EffectiveModelContext(org.opendaylight.yangtools.yang.model.api.EffectiveModelContext)

Example 17 with DOMRpcResult

use of org.opendaylight.mdsal.dom.api.DOMRpcResult in project netconf by opendaylight.

the class RestconfImpl method checkRpcResponse.

@SuppressWarnings("checkstyle:avoidHidingCauseException")
private static DOMRpcResult checkRpcResponse(final ListenableFuture<? extends DOMRpcResult> response) {
    if (response == null) {
        return null;
    }
    try {
        final DOMRpcResult retValue = response.get();
        if (retValue.getErrors().isEmpty()) {
            return retValue;
        }
        LOG.debug("RpcError message {}", retValue.getErrors());
        throw new RestconfDocumentedException("RpcError message", null, retValue.getErrors());
    } catch (final InterruptedException e) {
        final String errMsg = "The operation was interrupted while executing and did not complete.";
        LOG.debug("Rpc Interrupt - {}", errMsg, e);
        throw new RestconfDocumentedException(errMsg, ErrorType.RPC, ErrorTag.PARTIAL_OPERATION, e);
    } catch (final ExecutionException e) {
        LOG.debug("Execution RpcError: ", e);
        Throwable cause = e.getCause();
        if (cause == null) {
            throw new RestconfDocumentedException("The operation encountered an unexpected error while executing.", e);
        }
        while (cause.getCause() != null) {
            cause = cause.getCause();
        }
        if (cause instanceof IllegalArgumentException) {
            throw new RestconfDocumentedException(cause.getMessage(), ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
        } else if (cause instanceof DOMRpcImplementationNotAvailableException) {
            throw new RestconfDocumentedException(cause.getMessage(), ErrorType.APPLICATION, ErrorTag.OPERATION_NOT_SUPPORTED);
        }
        throw new RestconfDocumentedException("The operation encountered an unexpected error while executing.", cause);
    } catch (final CancellationException e) {
        final String errMsg = "The operation was cancelled while executing.";
        LOG.debug("Cancel RpcExecution: {}", errMsg, e);
        throw new RestconfDocumentedException(errMsg, ErrorType.RPC, ErrorTag.PARTIAL_OPERATION);
    }
}
Also used : RestconfDocumentedException(org.opendaylight.restconf.common.errors.RestconfDocumentedException) DefaultDOMRpcResult(org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult) DOMRpcResult(org.opendaylight.mdsal.dom.api.DOMRpcResult) CancellationException(java.util.concurrent.CancellationException) DOMRpcImplementationNotAvailableException(org.opendaylight.mdsal.dom.api.DOMRpcImplementationNotAvailableException) ExecutionException(java.util.concurrent.ExecutionException)

Example 18 with DOMRpcResult

use of org.opendaylight.mdsal.dom.api.DOMRpcResult in project netconf by opendaylight.

the class RuntimeRpc method handleWithNoSubsequentOperations.

@Override
protected Element handleWithNoSubsequentOperations(final Document document, final XmlElement operationElement) throws DocumentedException {
    final String netconfOperationName = operationElement.getName();
    final String netconfOperationNamespace;
    try {
        netconfOperationNamespace = operationElement.getNamespace();
    } catch (final DocumentedException e) {
        LOG.debug("Cannot retrieve netconf operation namespace from message due to ", e);
        throw new DocumentedException("Cannot retrieve netconf operation namespace from message", e, ErrorType.PROTOCOL, ErrorTag.UNKNOWN_NAMESPACE, ErrorSeverity.ERROR);
    }
    final XMLNamespace namespaceURI = createNsUri(netconfOperationNamespace);
    final Optional<? extends Module> moduleOptional = getModule(namespaceURI);
    if (moduleOptional.isEmpty()) {
        throw new DocumentedException("Unable to find module in Schema Context with namespace and name : " + namespaceURI + " " + netconfOperationName + schemaContext.getCurrentContext(), ErrorType.APPLICATION, ErrorTag.BAD_ELEMENT, ErrorSeverity.ERROR);
    }
    final Optional<RpcDefinition> rpcDefinitionOptional = getRpcDefinitionFromModule(moduleOptional.get(), namespaceURI, netconfOperationName);
    if (rpcDefinitionOptional.isEmpty()) {
        throw new DocumentedException("Unable to find RpcDefinition with namespace and name : " + namespaceURI + " " + netconfOperationName, ErrorType.APPLICATION, ErrorTag.BAD_ELEMENT, ErrorSeverity.ERROR);
    }
    final RpcDefinition rpcDefinition = rpcDefinitionOptional.get();
    final ContainerNode inputNode = rpcToNNode(operationElement, rpcDefinition);
    final DOMRpcResult result;
    try {
        result = rpcService.invokeRpc(rpcDefinition.getQName(), inputNode).get();
    } catch (final InterruptedException | ExecutionException e) {
        throw DocumentedException.wrap(e);
    }
    if (result.getResult() == null) {
        return XmlUtil.createElement(document, XmlNetconfConstants.OK, Optional.of(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0));
    }
    return transformNormalizedNode(document, result.getResult(), Absolute.of(rpcDefinition.getQName(), rpcDefinition.getOutput().getQName()));
}
Also used : RpcDefinition(org.opendaylight.yangtools.yang.model.api.RpcDefinition) DOMRpcResult(org.opendaylight.mdsal.dom.api.DOMRpcResult) NetconfDocumentedException(org.opendaylight.netconf.api.NetconfDocumentedException) DocumentedException(org.opendaylight.netconf.api.DocumentedException) XMLNamespace(org.opendaylight.yangtools.yang.common.XMLNamespace) ContainerNode(org.opendaylight.yangtools.yang.data.api.schema.ContainerNode) ExecutionException(java.util.concurrent.ExecutionException)

Example 19 with DOMRpcResult

use of org.opendaylight.mdsal.dom.api.DOMRpcResult in project netconf by opendaylight.

the class MountedDeviceListener method trackNotificationsPerformance.

private void trackNotificationsPerformance(final YangInstanceIdentifier path) {
    // 1. get nodeId from the path
    final String nodeId = TestUtils.getNodeId(path).get();
    // 2. extract needed services from the mount point
    final DOMMountPoint mountPoint = mountPointService.getMountPoint(path).orElseThrow(() -> new RuntimeException("Unable to get mountpoint"));
    final DOMRpcService rpcService = mountPoint.getService(DOMRpcService.class).orElseThrow(() -> new RuntimeException("Unable to get RPC Service from the mountpoint"));
    final DOMNotificationService notificationService = mountPoint.getService(DOMNotificationService.class).orElseThrow(() -> new RuntimeException("Unable to get NotificationService from the mountpoint"));
    // 3. create a listener for the notifications
    listeners.put(path, notificationService.registerNotificationListener(new NotificationsCounter(nodeId, serializer), Absolute.of(VrfRouteNotification.QNAME)));
    // 4. send 'create-subscription' request to the device
    final StreamNameType streamNameType = new StreamNameType(STREAM_DEFAULT_NAME);
    final CreateSubscriptionInputBuilder subscriptionInputBuilder = new CreateSubscriptionInputBuilder();
    subscriptionInputBuilder.setStream(streamNameType);
    final CreateSubscriptionInput input = subscriptionInputBuilder.build();
    final ContainerNode inputNode = serializer.toNormalizedNodeRpcData(input);
    final ListenableFuture<? extends DOMRpcResult> resultFuture = rpcService.invokeRpc(CREATE_SUBSCRIPTION_QNAME, inputNode);
    Futures.addCallback(resultFuture, new FutureCallback<DOMRpcResult>() {

        @Override
        public void onSuccess(@Nullable final DOMRpcResult rpcResult) {
            LOG.info("Notification stream subscription succesfully completed");
        }

        @Override
        public void onFailure(final Throwable throwable) {
            LOG.error("Notification stream subscription failed");
        }
    }, MoreExecutors.directExecutor());
}
Also used : StreamNameType(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.StreamNameType) DOMRpcService(org.opendaylight.mdsal.dom.api.DOMRpcService) DOMRpcResult(org.opendaylight.mdsal.dom.api.DOMRpcResult) DOMMountPoint(org.opendaylight.mdsal.dom.api.DOMMountPoint) DOMNotificationService(org.opendaylight.mdsal.dom.api.DOMNotificationService) NotificationsCounter(org.opendaylight.netconf.test.perf.notifications.NotificationsCounter) CreateSubscriptionInput(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.CreateSubscriptionInput) ContainerNode(org.opendaylight.yangtools.yang.data.api.schema.ContainerNode) CreateSubscriptionInputBuilder(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.CreateSubscriptionInputBuilder)

Example 20 with DOMRpcResult

use of org.opendaylight.mdsal.dom.api.DOMRpcResult in project netconf by opendaylight.

the class NetconfMessageTransformerTest method testRpcEmptyBodyWithOutputDefinedSchemaResult.

@Test
public void testRpcEmptyBodyWithOutputDefinedSchemaResult() throws Exception {
    final String result = "<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><ok/></rpc-reply>";
    DOMRpcResult domRpcResult = actionNetconfMessageTransformer.toRpcResult(new NetconfMessage(XmlUtil.readXmlToDocument(result)), RPC_WITH_OUTPUT_QNAME);
    assertNotNull(domRpcResult);
}
Also used : DOMRpcResult(org.opendaylight.mdsal.dom.api.DOMRpcResult) NetconfMessage(org.opendaylight.netconf.api.NetconfMessage) AbstractBaseSchemasTest(org.opendaylight.netconf.sal.connect.netconf.AbstractBaseSchemasTest) Test(org.junit.Test)

Aggregations

DOMRpcResult (org.opendaylight.mdsal.dom.api.DOMRpcResult)61 DefaultDOMRpcResult (org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult)39 Test (org.junit.Test)38 ContainerNode (org.opendaylight.yangtools.yang.data.api.schema.ContainerNode)21 QName (org.opendaylight.yangtools.yang.common.QName)18 NormalizedNode (org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)16 NetconfMessage (org.opendaylight.netconf.api.NetconfMessage)10 RpcError (org.opendaylight.yangtools.yang.common.RpcError)8 ExecutionException (java.util.concurrent.ExecutionException)7 AbstractBaseSchemasTest (org.opendaylight.netconf.sal.connect.netconf.AbstractBaseSchemasTest)7 RpcDefinition (org.opendaylight.yangtools.yang.model.api.RpcDefinition)7 ClusteringRpcException (org.opendaylight.netconf.topology.singleton.impl.utils.ClusteringRpcException)6 DOMRpcService (org.opendaylight.mdsal.dom.api.DOMRpcService)5 InvokeRpcMessageReply (org.opendaylight.netconf.topology.singleton.messages.rpc.InvokeRpcMessageReply)5 DOMRpcImplementationNotAvailableException (org.opendaylight.mdsal.dom.api.DOMRpcImplementationNotAvailableException)4 NormalizedNodeContext (org.opendaylight.netconf.sal.rest.impl.NormalizedNodeContext)4 YangInstanceIdentifier (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)4 WebApplicationException (javax.ws.rs.WebApplicationException)3 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)3 ArgumentCaptor (org.mockito.ArgumentCaptor)3