Search in sources :

Example 11 with RestconfDocumentedException

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

the class RestconfDataServiceImpl method isEqualUriAndPayloadKeyValues.

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.getIdentifier().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);
        }
    }
}
Also used : RestconfDocumentedException(org.opendaylight.restconf.common.errors.RestconfDocumentedException) HashMap(java.util.HashMap) QName(org.opendaylight.yangtools.yang.common.QName)

Example 12 with RestconfDocumentedException

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

the class RestconfDataServiceImpl method readData.

@Override
public Response readData(final String identifier, final UriInfo uriInfo) {
    final ReadDataParams readParams = QueryParams.newReadDataParams(uriInfo);
    final EffectiveModelContext schemaContextRef = schemaContextHandler.get();
    final InstanceIdentifierContext<?> instanceIdentifier = ParserIdentifier.toInstanceIdentifier(identifier, schemaContextRef, Optional.of(mountPointService));
    final DOMMountPoint mountPoint = instanceIdentifier.getMountPoint();
    // FIXME: this looks quite crazy, why do we even have it?
    if (mountPoint == null && identifier != null && identifier.contains(STREAMS_PATH) && !identifier.contains(STREAM_PATH_PART)) {
        createAllYangNotificationStreams(schemaContextRef, uriInfo);
    }
    final QueryParameters queryParams = QueryParams.newQueryParameters(readParams, instanceIdentifier);
    final List<YangInstanceIdentifier> fieldPaths = queryParams.fieldPaths();
    final RestconfStrategy strategy = getRestconfStrategy(mountPoint);
    final NormalizedNode node;
    if (fieldPaths != null && !fieldPaths.isEmpty()) {
        node = ReadDataTransactionUtil.readData(readParams.content(), instanceIdentifier.getInstanceIdentifier(), strategy, readParams.withDefaults(), schemaContextRef, fieldPaths);
    } else {
        node = ReadDataTransactionUtil.readData(readParams.content(), instanceIdentifier.getInstanceIdentifier(), strategy, readParams.withDefaults(), schemaContextRef);
    }
    // FIXME: this is utter craziness, refactor it properly!
    if (identifier != null && identifier.contains(STREAM_PATH) && identifier.contains(STREAM_ACCESS_PATH_PART) && identifier.contains(STREAM_LOCATION_PATH_PART)) {
        final String value = (String) node.body();
        final String streamName = value.substring(value.indexOf(NOTIFICATION_STREAM + '/'));
        delegRestconfSubscrService.subscribeToStream(streamName, uriInfo);
    }
    if (node == null) {
        throw new RestconfDocumentedException("Request could not be completed because the relevant data model content does not exist", ErrorType.PROTOCOL, ErrorTag.DATA_MISSING);
    }
    switch(readParams.content()) {
        case ALL:
        case CONFIG:
            final QName type = node.getIdentifier().getNodeType();
            return Response.status(Status.OK).entity(NormalizedNodePayload.ofReadData(instanceIdentifier, node, queryParams)).header("ETag", '"' + type.getModule().getRevision().map(Revision::toString).orElse(null) + "-" + type.getLocalName() + '"').header("Last-Modified", FORMATTER.format(LocalDateTime.now(Clock.systemUTC()))).build();
        default:
            return Response.status(Status.OK).entity(NormalizedNodePayload.ofReadData(instanceIdentifier, node, queryParams)).build();
    }
}
Also used : ReadDataParams(org.opendaylight.restconf.nb.rfc8040.ReadDataParams) RestconfDocumentedException(org.opendaylight.restconf.common.errors.RestconfDocumentedException) QName(org.opendaylight.yangtools.yang.common.QName) DOMMountPoint(org.opendaylight.mdsal.dom.api.DOMMountPoint) RestconfStrategy(org.opendaylight.restconf.nb.rfc8040.rests.transactions.RestconfStrategy) MdsalRestconfStrategy(org.opendaylight.restconf.nb.rfc8040.rests.transactions.MdsalRestconfStrategy) QueryParameters(org.opendaylight.restconf.nb.rfc8040.legacy.QueryParameters) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) Revision(org.opendaylight.yangtools.yang.common.Revision) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) EffectiveModelContext(org.opendaylight.yangtools.yang.model.api.EffectiveModelContext)

Example 13 with RestconfDocumentedException

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

the class RestconfDataServiceImpl method validTopLevelNodeName.

/**
 * Valid top level node name.
 *
 * @param path    path of node
 * @param payload data
 */
@VisibleForTesting
public static void validTopLevelNodeName(final YangInstanceIdentifier path, final NormalizedNodePayload payload) {
    final QName dataNodeType = payload.getData().getIdentifier().getNodeType();
    if (path.isEmpty()) {
        if (!NETCONF_BASE_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);
        }
    }
}
Also used : RestconfDocumentedException(org.opendaylight.restconf.common.errors.RestconfDocumentedException) QName(org.opendaylight.yangtools.yang.common.QName) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 14 with RestconfDocumentedException

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

the class XmlPatchBodyReader method readValueNodes.

/**
 * Read value nodes.
 *
 * @param element Element of current edit operation
 * @param operation Name of current operation
 * @return List of value elements
 */
private static List<Element> readValueNodes(@NonNull final Element element, @NonNull final PatchEditOperation operation) {
    final Node valueNode = element.getElementsByTagName("value").item(0);
    if (operation.isWithValue() && valueNode == null) {
        throw new RestconfDocumentedException("Error parsing input", ErrorType.PROTOCOL, ErrorTag.MALFORMED_MESSAGE);
    }
    if (!operation.isWithValue() && valueNode != null) {
        throw new RestconfDocumentedException("Error parsing input", ErrorType.PROTOCOL, ErrorTag.MALFORMED_MESSAGE);
    }
    if (valueNode == null) {
        return null;
    }
    final List<Element> result = new ArrayList<>();
    final NodeList childNodes = valueNode.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        if (childNodes.item(i) instanceof Element) {
            result.add((Element) childNodes.item(i));
        }
    }
    return result;
}
Also used : RestconfDocumentedException(org.opendaylight.restconf.common.errors.RestconfDocumentedException) ListSchemaNode(org.opendaylight.yangtools.yang.model.api.ListSchemaNode) ContainerSchemaNode(org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode) Node(org.w3c.dom.Node) SchemaNode(org.opendaylight.yangtools.yang.model.api.SchemaNode) DataSchemaNode(org.opendaylight.yangtools.yang.model.api.DataSchemaNode) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) ArrayList(java.util.ArrayList)

Example 15 with RestconfDocumentedException

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

the class DeleteDataTransactionUtil method deleteData.

/**
 * Delete data from DS via transaction.
 *
 * @param strategy object that perform the actual DS operations
 * @return {@link Response}
 */
public static Response deleteData(final RestconfStrategy strategy, final YangInstanceIdentifier path) {
    final RestconfTransaction transaction = strategy.prepareWriteExecution();
    try {
        transaction.delete(path);
    } catch (RestconfDocumentedException e) {
        // close transaction if any and pass exception further
        transaction.cancel();
        throw e;
    }
    final FluentFuture<? extends CommitInfo> future = transaction.commit();
    final ResponseFactory response = new ResponseFactory(Status.NO_CONTENT);
    // This method will close transactionChain if any
    FutureCallbackTx.addCallback(future, DELETE_TX_TYPE, response, path);
    return response.build();
}
Also used : RestconfDocumentedException(org.opendaylight.restconf.common.errors.RestconfDocumentedException) RestconfTransaction(org.opendaylight.restconf.nb.rfc8040.rests.transactions.RestconfTransaction)

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