Search in sources :

Example 81 with RestconfDocumentedException

use of org.opendaylight.restconf.common.errors.RestconfDocumentedException in project netconf by opendaylight.

the class RestconfDocumentedExceptionMapperTest method testToJsonResponseWithMultipleErrors.

@Test
// FIXME : find why it return "error-type" RPC no expected APPLICATION
@Ignore
public void testToJsonResponseWithMultipleErrors() throws Exception {
    final List<RestconfError> errorList = Arrays.asList(new RestconfError(ErrorType.APPLICATION, ErrorTag.LOCK_DENIED, "mock error1"), new RestconfError(ErrorType.RPC, ErrorTag.ROLLBACK_FAILED, "mock error2"));
    stageMockEx(new RestconfDocumentedException("mock", null, errorList));
    final Response resp = target("/operational/foo").request(MediaType.APPLICATION_JSON).get();
    final InputStream stream = verifyResponse(resp, MediaType.APPLICATION_JSON, Status.CONFLICT);
    final JsonArray arrayElement = parseJsonErrorArrayElement(stream);
    assertEquals("\"error\" Json array element length", 2, arrayElement.size());
    verifyJsonErrorNode(arrayElement.get(0), ErrorType.APPLICATION, ErrorTag.LOCK_DENIED, "mock error1", null, null);
    verifyJsonErrorNode(arrayElement.get(1), ErrorType.RPC, ErrorTag.ROLLBACK_FAILED, "mock error2", null, null);
}
Also used : Response(javax.ws.rs.core.Response) JsonArray(com.google.gson.JsonArray) RestconfDocumentedException(org.opendaylight.restconf.common.errors.RestconfDocumentedException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) RestconfError(org.opendaylight.restconf.common.errors.RestconfError) Ignore(org.junit.Ignore) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Example 82 with RestconfDocumentedException

use of org.opendaylight.restconf.common.errors.RestconfDocumentedException in project netconf by opendaylight.

the class RestconfDocumentedExceptionMapperTest method testToXMLResponseWithExceptionCause.

@Test
public void testToXMLResponseWithExceptionCause() throws Exception {
    final Exception cause = new Exception("mock exception cause");
    testXMLResponse(new RestconfDocumentedException("mock error", cause), Status.INTERNAL_SERVER_ERROR, ErrorType.APPLICATION, ErrorTag.OPERATION_FAILED, "mock error", null, new SimpleErrorInfoVerifier(cause.getMessage()));
}
Also used : RestconfDocumentedException(org.opendaylight.restconf.common.errors.RestconfDocumentedException) SAXException(org.xml.sax.SAXException) IOException(java.io.IOException) RestconfDocumentedException(org.opendaylight.restconf.common.errors.RestconfDocumentedException) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Example 83 with RestconfDocumentedException

use of org.opendaylight.restconf.common.errors.RestconfDocumentedException in project netconf by opendaylight.

the class RestconfDataServiceImpl method invokeAction.

/**
 * Invoke Action operation.
 *
 * @param payload {@link NormalizedNodePayload} - the body of the operation
 * @return {@link NormalizedNodePayload} wrapped in {@link Response}
 */
public Response invokeAction(final NormalizedNodePayload payload) {
    final InstanceIdentifierContext<?> context = payload.getInstanceIdentifierContext();
    final DOMMountPoint mountPoint = context.getMountPoint();
    final YangInstanceIdentifier yangIIdContext = context.getInstanceIdentifier();
    final NormalizedNode data = payload.getData();
    if (yangIIdContext.isEmpty() && !NETCONF_BASE_QNAME.equals(data.getIdentifier().getNodeType())) {
        throw new RestconfDocumentedException("Instance identifier need to contain at least one path argument", ErrorType.PROTOCOL, ErrorTag.MALFORMED_MESSAGE);
    }
    final List<QName> qNames = yangIIdContext.getPathArguments().stream().filter(arg -> !(arg instanceof YangInstanceIdentifier.NodeIdentifierWithPredicates)).filter(arg -> !(arg instanceof YangInstanceIdentifier.AugmentationIdentifier)).map(PathArgument::getNodeType).collect(Collectors.toList());
    qNames.add(context.getSchemaNode().getQName());
    final Absolute schemaPath = Absolute.of(qNames);
    final DOMActionResult response;
    final EffectiveModelContext schemaContextRef;
    if (mountPoint != null) {
        response = invokeAction((ContainerNode) data, schemaPath, yangIIdContext, mountPoint);
        schemaContextRef = modelContext(mountPoint);
    } else {
        response = invokeAction((ContainerNode) data, schemaPath, yangIIdContext, actionService);
        schemaContextRef = schemaContextHandler.get();
    }
    final DOMActionResult result = checkActionResponse(response);
    ActionDefinition resultNodeSchema = null;
    ContainerNode resultData = null;
    if (result != null) {
        final Optional<ContainerNode> optOutput = result.getOutput();
        if (optOutput.isPresent()) {
            resultData = optOutput.get();
            resultNodeSchema = (ActionDefinition) context.getSchemaNode();
        }
    }
    if (resultData != null && resultData.isEmpty()) {
        return Response.status(Status.NO_CONTENT).build();
    }
    return Response.status(Status.OK).entity(NormalizedNodePayload.ofNullable(new InstanceIdentifierContext<>(yangIIdContext, resultNodeSchema, mountPoint, schemaContextRef), resultData)).build();
}
Also used : DOMDataTreeWriteTransaction(org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction) DOMActionResult(org.opendaylight.mdsal.dom.api.DOMActionResult) WriteDataParams(org.opendaylight.restconf.nb.rfc8040.WriteDataParams) PlainPatchDataTransactionUtil(org.opendaylight.restconf.nb.rfc8040.rests.utils.PlainPatchDataTransactionUtil) SchemaContext(org.opendaylight.yangtools.yang.model.api.SchemaContext) Path(javax.ws.rs.Path) LoggerFactory(org.slf4j.LoggerFactory) DOMActionException(org.opendaylight.mdsal.dom.api.DOMActionException) Absolute(org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute) STREAM_ACCESS_PATH_PART(org.opendaylight.restconf.nb.rfc8040.rests.utils.RestconfStreamsConstants.STREAM_ACCESS_PATH_PART) PatchContext(org.opendaylight.restconf.common.patch.PatchContext) Map(java.util.Map) QueryParams(org.opendaylight.restconf.nb.rfc8040.databind.jaxrs.QueryParams) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) URI(java.net.URI) ContainerNode(org.opendaylight.yangtools.yang.data.api.schema.ContainerNode) DOMDataTreeIdentifier(org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier) DOMActionService(org.opendaylight.mdsal.dom.api.DOMActionService) ListSchemaNode(org.opendaylight.yangtools.yang.model.api.ListSchemaNode) CancellationException(java.util.concurrent.CancellationException) STREAM_LOCATION_PATH_PART(org.opendaylight.restconf.nb.rfc8040.rests.utils.RestconfStreamsConstants.STREAM_LOCATION_PATH_PART) NotificationOutputType(org.opendaylight.yang.gen.v1.urn.sal.restconf.event.subscription.rev140708.NotificationOutputTypeGrouping.NotificationOutputType) Collectors(java.util.stream.Collectors) STREAM_PATH_PART(org.opendaylight.restconf.nb.rfc8040.rests.utils.RestconfStreamsConstants.STREAM_PATH_PART) ParserIdentifier(org.opendaylight.restconf.nb.rfc8040.utils.parser.ParserIdentifier) List(java.util.List) NOTIFICATION_STREAM(org.opendaylight.restconf.nb.rfc8040.rests.utils.RestconfStreamsConstants.NOTIFICATION_STREAM) Revision(org.opendaylight.yangtools.yang.common.Revision) Response(javax.ws.rs.core.Response) DOMDataBroker(org.opendaylight.mdsal.dom.api.DOMDataBroker) PathArgument(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument) RestconfDataService(org.opendaylight.restconf.nb.rfc8040.rests.services.api.RestconfDataService) Optional(java.util.Optional) UriInfo(javax.ws.rs.core.UriInfo) DOMSchemaService(org.opendaylight.mdsal.dom.api.DOMSchemaService) MapEntryNode(org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode) SimpleDOMActionResult(org.opendaylight.mdsal.dom.spi.SimpleDOMActionResult) NotificationDefinition(org.opendaylight.yangtools.yang.model.api.NotificationDefinition) MoreExecutors(com.google.common.util.concurrent.MoreExecutors) ReadDataTransactionUtil(org.opendaylight.restconf.nb.rfc8040.rests.utils.ReadDataTransactionUtil) ReadDataParams(org.opendaylight.restconf.nb.rfc8040.ReadDataParams) PutDataTransactionUtil(org.opendaylight.restconf.nb.rfc8040.rests.utils.PutDataTransactionUtil) InstanceIdentifierContext(org.opendaylight.restconf.common.context.InstanceIdentifierContext) PostDataTransactionUtil(org.opendaylight.restconf.nb.rfc8040.rests.utils.PostDataTransactionUtil) LocalDateTime(java.time.LocalDateTime) HashMap(java.util.HashMap) DOMMountPointService(org.opendaylight.mdsal.dom.api.DOMMountPointService) NormalizedNodePayload(org.opendaylight.restconf.nb.rfc8040.legacy.NormalizedNodePayload) STREAMS_PATH(org.opendaylight.restconf.nb.rfc8040.rests.utils.RestconfStreamsConstants.STREAMS_PATH) NotificationListenerAdapter(org.opendaylight.restconf.nb.rfc8040.streams.listeners.NotificationListenerAdapter) DeleteDataTransactionUtil(org.opendaylight.restconf.nb.rfc8040.rests.utils.DeleteDataTransactionUtil) PatchStatusContext(org.opendaylight.restconf.common.patch.PatchStatusContext) ErrorType(org.opendaylight.yangtools.yang.common.ErrorType) ImmutableList(com.google.common.collect.ImmutableList) Objects.requireNonNull(java.util.Objects.requireNonNull) SchemaContextHandler(org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler) Status(javax.ws.rs.core.Response.Status) PatchDataTransactionUtil(org.opendaylight.restconf.nb.rfc8040.rests.utils.PatchDataTransactionUtil) Logger(org.slf4j.Logger) DOMDataTreeWriteOperations(org.opendaylight.mdsal.dom.api.DOMDataTreeWriteOperations) ErrorTag(org.opendaylight.yangtools.yang.common.ErrorTag) NodeIdentifierWithPredicates(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates) EffectiveModelContext(org.opendaylight.yangtools.yang.model.api.EffectiveModelContext) QueryParameters(org.opendaylight.restconf.nb.rfc8040.legacy.QueryParameters) Configuration(org.opendaylight.restconf.nb.rfc8040.streams.Configuration) QName(org.opendaylight.yangtools.yang.common.QName) RestconfStreamsSubscriptionService(org.opendaylight.restconf.nb.rfc8040.rests.services.api.RestconfStreamsSubscriptionService) SchemaNode(org.opendaylight.yangtools.yang.model.api.SchemaNode) ExecutionException(java.util.concurrent.ExecutionException) STREAM_PATH(org.opendaylight.restconf.nb.rfc8040.rests.utils.RestconfStreamsConstants.STREAM_PATH) DOMMountPoint(org.opendaylight.mdsal.dom.api.DOMMountPoint) Futures(com.google.common.util.concurrent.Futures) RestconfMappingNodeUtil(org.opendaylight.restconf.nb.rfc8040.utils.mapping.RestconfMappingNodeUtil) RestconfStrategy(org.opendaylight.restconf.nb.rfc8040.rests.transactions.RestconfStrategy) RpcResultBuilder(org.opendaylight.yangtools.yang.common.RpcResultBuilder) DateTimeFormatter(java.time.format.DateTimeFormatter) LogicalDatastoreType(org.opendaylight.mdsal.common.api.LogicalDatastoreType) Clock(java.time.Clock) MdsalRestconfStrategy(org.opendaylight.restconf.nb.rfc8040.rests.transactions.MdsalRestconfStrategy) VisibleForTesting(com.google.common.annotations.VisibleForTesting) ActionDefinition(org.opendaylight.yangtools.yang.model.api.ActionDefinition) RestconfDocumentedException(org.opendaylight.restconf.common.errors.RestconfDocumentedException) Rfc8040(org.opendaylight.restconf.nb.rfc8040.Rfc8040) RpcError(org.opendaylight.yangtools.yang.common.RpcError) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) RestconfDocumentedException(org.opendaylight.restconf.common.errors.RestconfDocumentedException) QName(org.opendaylight.yangtools.yang.common.QName) DOMMountPoint(org.opendaylight.mdsal.dom.api.DOMMountPoint) NodeIdentifierWithPredicates(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates) Absolute(org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) ActionDefinition(org.opendaylight.yangtools.yang.model.api.ActionDefinition) ContainerNode(org.opendaylight.yangtools.yang.data.api.schema.ContainerNode) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) DOMActionResult(org.opendaylight.mdsal.dom.api.DOMActionResult) SimpleDOMActionResult(org.opendaylight.mdsal.dom.spi.SimpleDOMActionResult) EffectiveModelContext(org.opendaylight.yangtools.yang.model.api.EffectiveModelContext)

Example 84 with RestconfDocumentedException

use of org.opendaylight.restconf.common.errors.RestconfDocumentedException in project netconf by opendaylight.

the class RestconfDataServiceImpl method createAllYangNotificationStreams.

private void createAllYangNotificationStreams(final EffectiveModelContext schemaContext, final UriInfo uriInfo) {
    final DOMDataTreeWriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
    for (final NotificationDefinition notificationDefinition : schemaContext.getNotifications()) {
        writeNotificationStreamToDatastore(schemaContext, uriInfo, transaction, CreateStreamUtil.createYangNotifiStream(notificationDefinition, schemaContext, NotificationOutputType.XML));
        writeNotificationStreamToDatastore(schemaContext, uriInfo, transaction, CreateStreamUtil.createYangNotifiStream(notificationDefinition, schemaContext, NotificationOutputType.JSON));
    }
    try {
        transaction.commit().get();
    } catch (final InterruptedException | ExecutionException e) {
        throw new RestconfDocumentedException("Problem while putting data to DS.", e);
    }
}
Also used : RestconfDocumentedException(org.opendaylight.restconf.common.errors.RestconfDocumentedException) DOMDataTreeWriteTransaction(org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction) NotificationDefinition(org.opendaylight.yangtools.yang.model.api.NotificationDefinition) ExecutionException(java.util.concurrent.ExecutionException)

Example 85 with RestconfDocumentedException

use of org.opendaylight.restconf.common.errors.RestconfDocumentedException in project netconf by opendaylight.

the class RestconfDataStreamServiceImpl method getSSE.

@Override
public void getSSE(final String identifier, final UriInfo uriInfo, final SseEventSink sink, final Sse sse) {
    final String streamName = ListenersBroker.createStreamNameFromUri(identifier);
    final BaseListenerInterface listener = listenersBroker.getListenerFor(streamName).orElseThrow(() -> {
        LOG.debug("Listener for stream with name {} was not found.", streamName);
        throw new RestconfDocumentedException("Data missing", ErrorType.APPLICATION, ErrorTag.DATA_MISSING);
    });
    LOG.debug("Listener for stream with name {} has been found, SSE session handler will be created.", streamName);
    // FIXME: invert control here: we should call 'listener.addSession()', which in turn should call
    // handler.init()/handler.close()
    final SSESessionHandler handler = new SSESessionHandler(executorService, sink, sse, listener, maximumFragmentLength, heartbeatInterval);
    handler.init();
}
Also used : RestconfDocumentedException(org.opendaylight.restconf.common.errors.RestconfDocumentedException) BaseListenerInterface(org.opendaylight.restconf.nb.rfc8040.streams.listeners.BaseListenerInterface) SSESessionHandler(org.opendaylight.restconf.nb.rfc8040.streams.SSESessionHandler)

Aggregations

RestconfDocumentedException (org.opendaylight.restconf.common.errors.RestconfDocumentedException)136 Test (org.junit.Test)65 InputStream (java.io.InputStream)30 DOMMountPoint (org.opendaylight.mdsal.dom.api.DOMMountPoint)20 RestconfError (org.opendaylight.restconf.common.errors.RestconfError)20 YangInstanceIdentifier (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)19 QName (org.opendaylight.yangtools.yang.common.QName)17 NormalizedNode (org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)17 DataSchemaNode (org.opendaylight.yangtools.yang.model.api.DataSchemaNode)16 ListSchemaNode (org.opendaylight.yangtools.yang.model.api.ListSchemaNode)14 List (java.util.List)13 EffectiveModelContext (org.opendaylight.yangtools.yang.model.api.EffectiveModelContext)13 ArrayList (java.util.ArrayList)12 ContainerNode (org.opendaylight.yangtools.yang.data.api.schema.ContainerNode)11 NodeIdentifier (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier)9 LeafListSchemaNode (org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode)9 IOException (java.io.IOException)8 ExecutionException (java.util.concurrent.ExecutionException)8 NodeIdentifierWithPredicates (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates)8 MapEntryNode (org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode)8