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));
}
}
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);
}
}
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()));
}
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());
}
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);
}
Aggregations