Search in sources :

Example 26 with RestconfDocumentedException

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

the class ControllerContext method toIdentifier.

private InstanceIdentifierContext<?> toIdentifier(final String restconfInstance, final boolean toMountPointIdentifier) {
    checkPreconditions();
    if (restconfInstance == null) {
        return new InstanceIdentifierContext<>(YangInstanceIdentifier.empty(), globalSchema, null, globalSchema);
    }
    final List<String> pathArgs = urlPathArgsDecode(SLASH_SPLITTER.split(restconfInstance));
    omitFirstAndLastEmptyString(pathArgs);
    if (pathArgs.isEmpty()) {
        return null;
    }
    final String first = pathArgs.iterator().next();
    final String startModule = toModuleName(first);
    if (startModule == null) {
        throw new RestconfDocumentedException("First node in URI has to be in format \"moduleName:nodeName\"", ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
    }
    final InstanceIdentifierBuilder builder = YangInstanceIdentifier.builder();
    final Collection<? extends Module> latestModule = globalSchema.findModules(startModule);
    if (latestModule.isEmpty()) {
        throw new RestconfDocumentedException("The module named '" + startModule + "' does not exist.", ErrorType.PROTOCOL, ErrorTag.UNKNOWN_ELEMENT);
    }
    final InstanceIdentifierContext<?> iiWithSchemaNode = collectPathArguments(builder, pathArgs, latestModule.iterator().next(), null, toMountPointIdentifier);
    if (iiWithSchemaNode == null) {
        throw new RestconfDocumentedException("URI has bad format", ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
    }
    return iiWithSchemaNode;
}
Also used : RestconfDocumentedException(org.opendaylight.restconf.common.errors.RestconfDocumentedException) InstanceIdentifierBuilder(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.InstanceIdentifierBuilder) InstanceIdentifierContext(org.opendaylight.restconf.common.context.InstanceIdentifierContext)

Example 27 with RestconfDocumentedException

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

the class ControllerContext method addKeyValue.

private void addKeyValue(final HashMap<QName, Object> map, final DataSchemaNode node, final String uriValue, final DOMMountPoint mountPoint) {
    checkArgument(node instanceof LeafSchemaNode);
    final EffectiveModelContext schemaContext = mountPoint == null ? globalSchema : getModelContext(mountPoint);
    final String urlDecoded = urlPathArgDecode(requireNonNull(uriValue));
    TypeDefinition<?> typedef = ((LeafSchemaNode) node).getType();
    final TypeDefinition<?> baseType = RestUtil.resolveBaseTypeFrom(typedef);
    if (baseType instanceof LeafrefTypeDefinition) {
        typedef = SchemaInferenceStack.ofInstantiatedPath(schemaContext, node.getPath()).resolveLeafref((LeafrefTypeDefinition) baseType);
    }
    final IllegalArgumentCodec<Object, Object> codec = RestCodec.from(typedef, mountPoint, this);
    Object decoded = codec.deserialize(urlDecoded);
    String additionalInfo = "";
    if (decoded == null) {
        if (typedef instanceof IdentityrefTypeDefinition) {
            decoded = toQName(schemaContext, urlDecoded);
            additionalInfo = "For key which is of type identityref it should be in format module_name:identity_name.";
        }
    }
    if (decoded == null) {
        throw new RestconfDocumentedException(uriValue + " from URI can't be resolved. " + additionalInfo, ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
    }
    map.put(node.getQName(), decoded);
}
Also used : RestconfDocumentedException(org.opendaylight.restconf.common.errors.RestconfDocumentedException) LeafrefTypeDefinition(org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition) IdentityrefTypeDefinition(org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition) LeafSchemaNode(org.opendaylight.yangtools.yang.model.api.LeafSchemaNode) EffectiveModelContext(org.opendaylight.yangtools.yang.model.api.EffectiveModelContext)

Example 28 with RestconfDocumentedException

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

the class RestconfImpl method isEqualUriAndPayloadKeyValues.

@VisibleForTesting
public 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(// should be caught during parsing URI to InstanceIdentifier
        mutableCopyUriKeyValues.remove(keyDefinition), ErrorType.PROTOCOL, ErrorTag.DATA_MISSING, "Missing key %s in URI.", keyDefinition);
        final Object dataKeyValue = payload.getIdentifier().getValue(keyDefinition);
        if (!Objects.deepEquals(uriKeyValue, 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) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 29 with RestconfDocumentedException

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

the class RestconfImpl method deleteConfigurationData.

@Override
public Response deleteConfigurationData(final String identifier) {
    final InstanceIdentifierContext<?> iiWithData = controllerContext.toInstanceIdentifier(identifier);
    final DOMMountPoint mountPoint = iiWithData.getMountPoint();
    final YangInstanceIdentifier normalizedII = iiWithData.getInstanceIdentifier();
    final FluentFuture<? extends CommitInfo> future;
    if (mountPoint != null) {
        future = broker.commitConfigurationDataDelete(mountPoint, normalizedII);
    } else {
        future = broker.commitConfigurationDataDelete(normalizedII);
    }
    try {
        future.get();
    } catch (final InterruptedException e) {
        throw new RestconfDocumentedException(e.getMessage(), e);
    } catch (final ExecutionException e) {
        final Optional<Throwable> searchedException = Iterables.tryFind(Throwables.getCausalChain(e), Predicates.instanceOf(ModifiedNodeDoesNotExistException.class)).toJavaUtil();
        if (searchedException.isPresent()) {
            throw new RestconfDocumentedException("Data specified for delete doesn't exist.", ErrorType.APPLICATION, ErrorTag.DATA_MISSING, e);
        }
        throw RestconfDocumentedException.decodeAndThrow(e.getMessage(), Throwables.getCauseAs(e, TransactionCommitFailedException.class));
    }
    return Response.status(Status.OK).build();
}
Also used : ModifiedNodeDoesNotExistException(org.opendaylight.yangtools.yang.data.api.schema.tree.ModifiedNodeDoesNotExistException) RestconfDocumentedException(org.opendaylight.restconf.common.errors.RestconfDocumentedException) Optional(java.util.Optional) DOMMountPoint(org.opendaylight.mdsal.dom.api.DOMMountPoint) ExecutionException(java.util.concurrent.ExecutionException) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)

Example 30 with RestconfDocumentedException

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

the class RestconfImpl method getOperations.

@Override
@Deprecated
public NormalizedNodeContext getOperations(final String identifier, final UriInfo uriInfo) {
    if (!identifier.contains(ControllerContext.MOUNT)) {
        final String errMsg = "URI has bad format. If operations behind mount point should be showed, URI has to " + " end with " + ControllerContext.MOUNT;
        LOG.debug("{} for {}", errMsg, identifier);
        throw new RestconfDocumentedException(errMsg, ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
    }
    final InstanceIdentifierContext<?> mountPointIdentifier = controllerContext.toMountPointIdentifier(identifier);
    final DOMMountPoint mountPoint = mountPointIdentifier.getMountPoint();
    final var entry = OperationsResourceUtils.contextForModelContext(modelContext(mountPoint), mountPoint);
    return new NormalizedNodeContext(entry.getKey(), entry.getValue());
}
Also used : RestconfDocumentedException(org.opendaylight.restconf.common.errors.RestconfDocumentedException) DOMMountPoint(org.opendaylight.mdsal.dom.api.DOMMountPoint) NormalizedNodeContext(org.opendaylight.netconf.sal.rest.impl.NormalizedNodeContext)

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