Search in sources :

Example 6 with RestconfDocumentedException

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

the class RestGetOperationTest method getDataWithInvalidDepthParameterTest.

private void getDataWithInvalidDepthParameterTest(final UriInfo uriInfo) {
    try {
        final QName qNameDepth1Cont = QName.create("urn:nested:module", "2014-06-3", "depth1-cont");
        final YangInstanceIdentifier ii = YangInstanceIdentifier.builder().node(qNameDepth1Cont).build();
        final NormalizedNode value = Builders.containerBuilder().withNodeIdentifier(new NodeIdentifier(qNameDepth1Cont)).build();
        when(brokerFacade.readConfigurationData(eq(ii))).thenReturn(value);
        restconfImpl.readConfigurationData("nested-module:depth1-cont", uriInfo);
        fail("Expected RestconfDocumentedException");
    } catch (final RestconfDocumentedException e) {
        assertTrue("Unexpected error message: " + e.getErrors().get(0).getErrorMessage(), e.getErrors().get(0).getErrorMessage().contains("depth"));
    }
}
Also used : RestconfDocumentedException(org.opendaylight.restconf.common.errors.RestconfDocumentedException) QName(org.opendaylight.yangtools.yang.common.QName) NodeIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)

Example 7 with RestconfDocumentedException

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

the class QueryParams method newNotificationQueryParams.

@NonNull
public static NotificationQueryParams newNotificationQueryParams(final UriInfo uriInfo) {
    StartTimeParam startTime = null;
    StopTimeParam stopTime = null;
    FilterParam filter = null;
    LeafNodesOnlyParam leafNodesOnly = null;
    SkipNotificationDataParam skipNotificationData = null;
    for (Entry<String, List<String>> entry : uriInfo.getQueryParameters().entrySet()) {
        final String paramName = entry.getKey();
        final List<String> paramValues = entry.getValue();
        try {
            switch(paramName) {
                case FilterParam.uriName:
                    filter = optionalParam(FilterParam::forUriValue, paramName, paramValues);
                    break;
                case StartTimeParam.uriName:
                    startTime = optionalParam(StartTimeParam::forUriValue, paramName, paramValues);
                    break;
                case StopTimeParam.uriName:
                    stopTime = optionalParam(StopTimeParam::forUriValue, paramName, paramValues);
                    break;
                case LeafNodesOnlyParam.uriName:
                    leafNodesOnly = optionalParam(LeafNodesOnlyParam::forUriValue, paramName, paramValues);
                    break;
                case SkipNotificationDataParam.uriName:
                    skipNotificationData = optionalParam(SkipNotificationDataParam::forUriValue, paramName, paramValues);
                    break;
                default:
                    throw unhandledParam("notification", paramName);
            }
        } catch (IllegalArgumentException e) {
            throw new RestconfDocumentedException("Invalid " + paramName + " value: " + e.getMessage(), ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE, e);
        }
    }
    try {
        return NotificationQueryParams.of(startTime, stopTime, filter, leafNodesOnly, skipNotificationData);
    } catch (IllegalArgumentException e) {
        throw new RestconfDocumentedException("Invalid query parameters: " + e.getMessage(), e);
    }
}
Also used : RestconfDocumentedException(org.opendaylight.restconf.common.errors.RestconfDocumentedException) StopTimeParam(org.opendaylight.restconf.nb.rfc8040.StopTimeParam) StartTimeParam(org.opendaylight.restconf.nb.rfc8040.StartTimeParam) FilterParam(org.opendaylight.restconf.nb.rfc8040.FilterParam) SkipNotificationDataParam(org.opendaylight.restconf.nb.rfc8040.SkipNotificationDataParam) List(java.util.List) LeafNodesOnlyParam(org.opendaylight.restconf.nb.rfc8040.LeafNodesOnlyParam) Objects.requireNonNull(java.util.Objects.requireNonNull) NonNull(org.eclipse.jdt.annotation.NonNull)

Example 8 with RestconfDocumentedException

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

the class QueryParams method newWriteDataParams.

@NonNull
public static WriteDataParams newWriteDataParams(final UriInfo uriInfo) {
    InsertParam insert = null;
    PointParam point = null;
    for (final Entry<String, List<String>> entry : uriInfo.getQueryParameters().entrySet()) {
        final String paramName = entry.getKey();
        final List<String> paramValues = entry.getValue();
        try {
            switch(paramName) {
                case InsertParam.uriName:
                    insert = optionalParam(InsertParam::forUriValue, paramName, paramValues);
                    break;
                case PointParam.uriName:
                    point = optionalParam(PointParam::forUriValue, paramName, paramValues);
                    break;
                default:
                    throw unhandledParam("write", paramName);
            }
        } catch (IllegalArgumentException e) {
            throw new RestconfDocumentedException("Invalid " + paramName + " value: " + e.getMessage(), ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE, e);
        }
    }
    try {
        return WriteDataParams.of(insert, point);
    } catch (IllegalArgumentException e) {
        throw new RestconfDocumentedException("Invalid query parameters: " + e.getMessage(), e);
    }
}
Also used : PointParam(org.opendaylight.restconf.nb.rfc8040.PointParam) RestconfDocumentedException(org.opendaylight.restconf.common.errors.RestconfDocumentedException) InsertParam(org.opendaylight.restconf.nb.rfc8040.InsertParam) List(java.util.List) Objects.requireNonNull(java.util.Objects.requireNonNull) NonNull(org.eclipse.jdt.annotation.NonNull)

Example 9 with RestconfDocumentedException

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

the class SchemaContextHandler method putData.

private void putData(final ContainerNode normNode) {
    final DOMDataTreeWriteTransaction wTx = domDataBroker.newWriteOnlyTransaction();
    wTx.put(LogicalDatastoreType.OPERATIONAL, YangInstanceIdentifier.create(NodeIdentifier.create(normNode.getIdentifier().getNodeType())), normNode);
    try {
        wTx.commit().get();
    } catch (InterruptedException e) {
        throw new RestconfDocumentedException("Problem occurred while putting data to DS.", e);
    } catch (ExecutionException e) {
        final TransactionCommitFailedException failure = Throwables.getCauseAs(e, TransactionCommitFailedException.class);
        if (failure.getCause() instanceof ConflictingModificationAppliedException) {
            /*
                 * Ignore error when another cluster node is already putting the same data to DS.
                 * We expect that cluster is homogeneous and that node was going to write the same data
                 * (that means no retry is needed). Transaction chain reset must be invoked to be able
                 * to continue writing data with another transaction after failed transaction.
                 * This is workaround for bug https://bugs.opendaylight.org/show_bug.cgi?id=7728
                 */
            LOG.warn("Ignoring that another cluster node is already putting the same data to DS.", e);
        } else {
            throw new RestconfDocumentedException("Problem occurred while putting data to DS.", failure);
        }
    }
}
Also used : TransactionCommitFailedException(org.opendaylight.mdsal.common.api.TransactionCommitFailedException) RestconfDocumentedException(org.opendaylight.restconf.common.errors.RestconfDocumentedException) DOMDataTreeWriteTransaction(org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction) ConflictingModificationAppliedException(org.opendaylight.yangtools.yang.data.api.schema.tree.ConflictingModificationAppliedException) ExecutionException(java.util.concurrent.ExecutionException)

Example 10 with RestconfDocumentedException

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

the class RestconfDocumentedExceptionMapper method getResponseStatusCode.

/**
 * Deriving of the status code from the thrown exception. At the first step, status code is tried to be read using
 * {@link RestconfDocumentedException#getStatus()}. If it is {@code null}, status code will be derived from status
 * codes appended to error entries (the first that will be found). If there are not any error entries,
 * {@link RestconfDocumentedExceptionMapper#DEFAULT_STATUS_CODE} will be used.
 *
 * @param exception Thrown exception.
 * @return Derived status code.
 */
private static Status getResponseStatusCode(final RestconfDocumentedException exception) {
    final Status status = exception.getStatus();
    if (status != null) {
        // status code that is specified directly as field in exception has the precedence over error entries
        return status;
    }
    final List<RestconfError> errors = exception.getErrors();
    if (errors.isEmpty()) {
        // server error
        return DEFAULT_STATUS_CODE;
    }
    final Set<Status> allStatusCodesOfErrorEntries = errors.stream().map(restconfError -> ErrorTags.statusOf(restconfError.getErrorTag())).collect(Collectors.toCollection(LinkedHashSet::new));
    // entries, we should create WARN message
    if (allStatusCodesOfErrorEntries.size() > 1) {
        LOG.warn("An unexpected error occurred during translation of exception {} to response: " + "Different status codes have been found in appended error entries: {}. The first error " + "entry status code is chosen for response.", exception, allStatusCodesOfErrorEntries);
    }
    return allStatusCodesOfErrorEntries.iterator().next();
}
Also used : Status(javax.ws.rs.core.Response.Status) SchemaPath(org.opendaylight.yangtools.yang.model.api.SchemaPath) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Provider(javax.ws.rs.ext.Provider) NodeIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier) ExceptionMapper(javax.ws.rs.ext.ExceptionMapper) LoggerFactory(org.slf4j.LoggerFactory) XMLNamespace(org.opendaylight.yangtools.yang.common.XMLNamespace) MediaType(javax.ws.rs.core.MediaType) NormalizedNodeWriter(org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter) SchemaContextHandler(org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler) OutputStreamWriter(java.io.OutputStreamWriter) ImmutableContainerNodeBuilder(org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder) Status(javax.ws.rs.core.Response.Status) ContainerNode(org.opendaylight.yangtools.yang.data.api.schema.ContainerNode) LinkedHashSet(java.util.LinkedHashSet) Context(javax.ws.rs.core.Context) Logger(org.slf4j.Logger) DataContainerNodeBuilder(org.opendaylight.yangtools.yang.data.api.schema.builder.DataContainerNodeBuilder) Error(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.restconf.rev170126.errors.errors.Error) ImmutableUnkeyedListNodeBuilder(org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUnkeyedListNodeBuilder) Set(java.util.Set) UnkeyedListEntryNode(org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode) IOException(java.io.IOException) ImmutableUnkeyedListEntryNodeBuilder(org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUnkeyedListEntryNodeBuilder) Collectors(java.util.stream.Collectors) RestconfError(org.opendaylight.restconf.common.errors.RestconfError) StandardCharsets(java.nio.charset.StandardCharsets) Errors(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.restconf.rev170126.errors.Errors) QName(org.opendaylight.yangtools.yang.common.QName) Preconditions.checkState(com.google.common.base.Preconditions.checkState) ErrorTags(org.opendaylight.restconf.common.ErrorTags) MediaTypes(org.opendaylight.restconf.nb.rfc8040.MediaTypes) org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.restconf.rev170126.$YangModuleInfoImpl.qnameOf(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.restconf.rev170126.$YangModuleInfoImpl.qnameOf) List(java.util.List) HttpHeaders(javax.ws.rs.core.HttpHeaders) Response(javax.ws.rs.core.Response) ImmutableNodes(org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes) Optional(java.util.Optional) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Collections(java.util.Collections) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) RestconfDocumentedException(org.opendaylight.restconf.common.errors.RestconfDocumentedException) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) RestconfError(org.opendaylight.restconf.common.errors.RestconfError)

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