- return RestconfInvokeOperationsServiceImpl.checkedGet(Futures.catching(actionService.invokeAction(
- schemaPath, new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL, yangIId.getParent()), data),
- DOMActionException.class,
- cause -> new SimpleDOMActionResult(List.of(RpcResultBuilder.newError(
- ErrorType.RPC, ErrorTag.OPERATION_FAILED, cause.getMessage()))),
- MoreExecutors.directExecutor()));
- }
-
- /**
- * Check the validity of the result.
- *
- * @param response response of Action
- * @return {@link DOMActionResult} result
- */
- private static DOMActionResult checkActionResponse(final DOMActionResult response) {
- if (response == null) {
- return null;
- }
-
- try {
- if (response.getErrors().isEmpty()) {
- return response;
- }
- LOG.debug("InvokeAction Error Message {}", response.getErrors());
- throw new RestconfDocumentedException("InvokeAction Error Message ", null, response.getErrors());
- } catch (final CancellationException e) {
- final String errMsg = "The Action Operation was cancelled while executing.";
- LOG.debug("Cancel Execution: {}", errMsg, e);
- throw new RestconfDocumentedException(errMsg, ErrorType.RPC, ErrorTag.PARTIAL_OPERATION, e);
- }
- }
-
- /**
- * Valid input data based on presence of a schema node.
- *
- * @param haveSchemaNode true if there is an underlying schema node
- * @param payload input data
- */
- @VisibleForTesting
- static void validInputData(final boolean haveSchemaNode, final NormalizedNodePayload payload) {
- final boolean haveData = payload.getData() != null;
- if (haveSchemaNode) {
- if (!haveData) {
- throw new RestconfDocumentedException("Input is required.", ErrorType.PROTOCOL,
- ErrorTag.MALFORMED_MESSAGE);
- }
- } else if (haveData) {
- throw new RestconfDocumentedException("No input expected.", ErrorType.PROTOCOL, ErrorTag.MALFORMED_MESSAGE);
- }
- }
-
- /**
- * Valid top level node name.
- *
- * @param path path of node
- * @param payload data
- */
- @VisibleForTesting
- static void validTopLevelNodeName(final YangInstanceIdentifier path, final NormalizedNodePayload payload) {
- final QName dataNodeType = payload.getData().name().getNodeType();
- if (path.isEmpty()) {
- if (!Data.QNAME.equals(dataNodeType)) {
- throw new RestconfDocumentedException("Instance identifier has to contain at least one path argument",
- ErrorType.PROTOCOL, ErrorTag.MALFORMED_MESSAGE);
- }
- } else {
- final String identifierName = path.getLastPathArgument().getNodeType().getLocalName();
- final String payloadName = dataNodeType.getLocalName();
- if (!payloadName.equals(identifierName)) {
- throw new RestconfDocumentedException(
- "Payload name (" + payloadName + ") is different from identifier name (" + identifierName + ")",
- ErrorType.PROTOCOL, ErrorTag.MALFORMED_MESSAGE);
- }
- }
- }
-
- /**
- * Validates whether keys in {@code payload} are equal to values of keys in
- * {@code iiWithData} for list schema node.
- *
- * @throws RestconfDocumentedException if key values or key count in payload and URI isn't equal
- */
- @VisibleForTesting
- static void validateListKeysEqualityInPayloadAndUri(final NormalizedNodePayload payload) {
- final InstanceIdentifierContext iiWithData = payload.getInstanceIdentifierContext();
- final PathArgument lastPathArgument = iiWithData.getInstanceIdentifier().getLastPathArgument();
- final SchemaNode schemaNode = iiWithData.getSchemaNode();
- final NormalizedNode data = payload.getData();
- if (schemaNode instanceof ListSchemaNode listSchema) {
- final var keyDefinitions = listSchema.getKeyDefinition();
- if (lastPathArgument instanceof NodeIdentifierWithPredicates && data instanceof MapEntryNode) {
- final Map<QName, Object> uriKeyValues = ((NodeIdentifierWithPredicates) lastPathArgument).asMap();
- isEqualUriAndPayloadKeyValues(uriKeyValues, (MapEntryNode) data, keyDefinitions);
- }
- }
- }
-
- private static void isEqualUriAndPayloadKeyValues(final Map<QName, Object> uriKeyValues, final MapEntryNode payload,
- final List<QName> keyDefinitions) {
- final Map<QName, Object> mutableCopyUriKeyValues = new HashMap<>(uriKeyValues);
- for (final QName keyDefinition : keyDefinitions) {
- final Object uriKeyValue = RestconfDocumentedException.throwIfNull(
- mutableCopyUriKeyValues.remove(keyDefinition), ErrorType.PROTOCOL, ErrorTag.DATA_MISSING,
- "Missing key %s in URI.", keyDefinition);
-
- final Object dataKeyValue = payload.name().getValue(keyDefinition);
-
- if (!uriKeyValue.equals(dataKeyValue)) {
- final String errMsg = "The value '" + uriKeyValue + "' for key '" + keyDefinition.getLocalName()
- + "' specified in the URI doesn't match the value '" + dataKeyValue
- + "' specified in the message body. ";
- throw new RestconfDocumentedException(errMsg, ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
- }
- }
+ final var ret = new SettableRestconfFuture<DOMActionResult>();
+
+ Futures.addCallback(actionService.invokeAction(schemaPath,
+ new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL, yangIId.getParent()), data),
+ new FutureCallback<DOMActionResult>() {
+ @Override
+ public void onSuccess(final DOMActionResult result) {
+ final var errors = result.getErrors();
+ LOG.debug("InvokeAction Error Message {}", errors);
+ if (errors.isEmpty()) {
+ ret.set(result);
+ } else {
+ ret.setFailure(new RestconfDocumentedException("InvokeAction Error Message ", null, errors));
+ }
+ }
+
+ @Override
+ public void onFailure(final Throwable cause) {
+ if (cause instanceof DOMActionException) {
+ ret.set(new SimpleDOMActionResult(List.of(RpcResultBuilder.newError(
+ ErrorType.RPC, ErrorTag.OPERATION_FAILED, cause.getMessage()))));
+ } else if (cause instanceof RestconfDocumentedException e) {
+ ret.setFailure(e);
+ } else if (cause instanceof CancellationException) {
+ ret.setFailure(new RestconfDocumentedException("Action cancelled while executing",
+ ErrorType.RPC, ErrorTag.PARTIAL_OPERATION, cause));
+ } else {
+ ret.setFailure(new RestconfDocumentedException("Invocation failed", cause));
+ }
+ }
+ }, MoreExecutors.directExecutor());
+
+ return ret;