Search in sources :

Example 11 with DOMMountPoint

use of org.opendaylight.mdsal.dom.api.DOMMountPoint in project netconf by opendaylight.

the class RestconfImpl method invokeRpc.

@SuppressFBWarnings(value = "NP_LOAD_OF_KNOWN_NULL_VALUE", justification = "Looks like a false positive, see below FIXME")
private NormalizedNodeContext invokeRpc(final String identifier, final UriInfo uriInfo) {
    final DOMMountPoint mountPoint;
    final String identifierEncoded;
    final EffectiveModelContext schemaContext;
    if (identifier.contains(ControllerContext.MOUNT)) {
        // mounted RPC call - look up mount instance.
        final InstanceIdentifierContext<?> mountPointId = controllerContext.toMountPointIdentifier(identifier);
        mountPoint = mountPointId.getMountPoint();
        schemaContext = modelContext(mountPoint);
        final int startOfRemoteRpcName = identifier.lastIndexOf(ControllerContext.MOUNT) + ControllerContext.MOUNT.length() + 1;
        final String remoteRpcName = identifier.substring(startOfRemoteRpcName);
        identifierEncoded = remoteRpcName;
    } else if (identifier.indexOf('/') == CHAR_NOT_FOUND) {
        identifierEncoded = identifier;
        mountPoint = null;
        schemaContext = controllerContext.getGlobalSchema();
    } else {
        LOG.debug("Identifier {} cannot contain slash character (/).", identifier);
        throw new RestconfDocumentedException(String.format("Identifier %n%s%ncan\'t contain slash character (/).%n" + "If slash is part of identifier name then use %%2F placeholder.", identifier), ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
    }
    final String identifierDecoded = ControllerContext.urlPathArgDecode(identifierEncoded);
    final RpcDefinition rpc;
    if (mountPoint == null) {
        rpc = controllerContext.getRpcDefinition(identifierDecoded);
    } else {
        rpc = findRpc(modelContext(mountPoint), identifierDecoded);
    }
    if (rpc == null) {
        LOG.debug("RPC {} does not exist.", identifierDecoded);
        throw new RestconfDocumentedException("RPC does not exist.", ErrorType.RPC, ErrorTag.UNKNOWN_ELEMENT);
    }
    if (!rpc.getInput().getChildNodes().isEmpty()) {
        LOG.debug("No input specified for RPC {} with an input section", rpc);
        throw new RestconfDocumentedException("No input specified for RPC " + rpc + " with an input section defined", ErrorType.RPC, ErrorTag.MISSING_ELEMENT);
    }
    final ContainerNode input = defaultInput(rpc.getQName());
    final ListenableFuture<? extends DOMRpcResult> response;
    if (mountPoint != null) {
        final Optional<DOMRpcService> mountRpcServices = mountPoint.getService(DOMRpcService.class);
        if (mountRpcServices.isEmpty()) {
            throw new RestconfDocumentedException("Rpc service is missing.");
        }
        response = mountRpcServices.get().invokeRpc(rpc.getQName(), input);
    } else {
        response = broker.invokeRpc(rpc.getQName(), input);
    }
    final NormalizedNode result = checkRpcResponse(response).getResult();
    if (result != null && ((ContainerNode) result).isEmpty()) {
        throw new WebApplicationException(Response.Status.NO_CONTENT);
    }
    // doing that work.
    return new NormalizedNodeContext(new InstanceIdentifierContext<>(null, rpc, mountPoint, schemaContext), result, QueryParametersParser.parseWriterParameters(uriInfo));
}
Also used : RestconfDocumentedException(org.opendaylight.restconf.common.errors.RestconfDocumentedException) DOMRpcService(org.opendaylight.mdsal.dom.api.DOMRpcService) RpcDefinition(org.opendaylight.yangtools.yang.model.api.RpcDefinition) WebApplicationException(javax.ws.rs.WebApplicationException) DOMMountPoint(org.opendaylight.mdsal.dom.api.DOMMountPoint) DOMMountPoint(org.opendaylight.mdsal.dom.api.DOMMountPoint) NormalizedNodeContext(org.opendaylight.netconf.sal.rest.impl.NormalizedNodeContext) ContainerNode(org.opendaylight.yangtools.yang.data.api.schema.ContainerNode) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) EffectiveModelContext(org.opendaylight.yangtools.yang.model.api.EffectiveModelContext) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 12 with DOMMountPoint

use of org.opendaylight.mdsal.dom.api.DOMMountPoint in project netconf by opendaylight.

the class RestconfImpl method getModule.

@Override
@Deprecated
public NormalizedNodeContext getModule(final String identifier, final UriInfo uriInfo) {
    final Entry<String, Revision> nameRev = getModuleNameAndRevision(requireNonNull(identifier));
    Module module = null;
    DOMMountPoint mountPoint = null;
    final EffectiveModelContext schemaContext;
    if (identifier.contains(ControllerContext.MOUNT)) {
        final InstanceIdentifierContext<?> mountPointIdentifier = controllerContext.toMountPointIdentifier(identifier);
        mountPoint = mountPointIdentifier.getMountPoint();
        module = controllerContext.findModuleByNameAndRevision(mountPoint, nameRev.getKey(), nameRev.getValue());
        schemaContext = modelContext(mountPoint);
    } else {
        module = controllerContext.findModuleByNameAndRevision(nameRev.getKey(), nameRev.getValue());
        schemaContext = controllerContext.getGlobalSchema();
    }
    if (module == null) {
        LOG.debug("Module with name '{}' and revision '{}' was not found.", nameRev.getKey(), nameRev.getValue());
        throw new RestconfDocumentedException("Module with name '" + nameRev.getKey() + "' and revision '" + nameRev.getValue() + "' was not found.", ErrorType.PROTOCOL, ErrorTag.UNKNOWN_ELEMENT);
    }
    final Module restconfModule = getRestconfModule();
    final Set<Module> modules = Collections.singleton(module);
    final MapNode moduleMap = makeModuleMapNode(modules);
    final DataSchemaNode moduleSchemaNode = controllerContext.getRestconfModuleRestConfSchemaNode(restconfModule, RestConfModule.MODULE_LIST_SCHEMA_NODE);
    checkState(moduleSchemaNode instanceof ListSchemaNode);
    return new NormalizedNodeContext(new InstanceIdentifierContext<>(MODULE, moduleSchemaNode, mountPoint, schemaContext), moduleMap, QueryParametersParser.parseWriterParameters(uriInfo));
}
Also used : RestconfDocumentedException(org.opendaylight.restconf.common.errors.RestconfDocumentedException) DataSchemaNode(org.opendaylight.yangtools.yang.model.api.DataSchemaNode) DOMMountPoint(org.opendaylight.mdsal.dom.api.DOMMountPoint) MapNode(org.opendaylight.yangtools.yang.data.api.schema.MapNode) SystemMapNode(org.opendaylight.yangtools.yang.data.api.schema.SystemMapNode) NormalizedNodeContext(org.opendaylight.netconf.sal.rest.impl.NormalizedNodeContext) Revision(org.opendaylight.yangtools.yang.common.Revision) LeafListSchemaNode(org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode) ListSchemaNode(org.opendaylight.yangtools.yang.model.api.ListSchemaNode) QNameModule(org.opendaylight.yangtools.yang.common.QNameModule) Module(org.opendaylight.yangtools.yang.model.api.Module) RestConfModule(org.opendaylight.netconf.sal.rest.api.Draft02.RestConfModule) EffectiveModelContext(org.opendaylight.yangtools.yang.model.api.EffectiveModelContext)

Example 13 with DOMMountPoint

use of org.opendaylight.mdsal.dom.api.DOMMountPoint in project netconf by opendaylight.

the class RestconfImpl method invokeRpc.

@Override
public NormalizedNodeContext invokeRpc(final String identifier, final NormalizedNodeContext payload, final UriInfo uriInfo) {
    if (payload == null) {
        // no payload specified, reroute this to no payload invokeRpc implementation
        return invokeRpc(identifier, uriInfo);
    }
    final SchemaNode schema = payload.getInstanceIdentifierContext().getSchemaNode();
    final ListenableFuture<? extends DOMRpcResult> response;
    final DOMMountPoint mountPoint = payload.getInstanceIdentifierContext().getMountPoint();
    final NormalizedNode input = nonnullInput(schema, payload.getData());
    final EffectiveModelContext schemaContext;
    if (mountPoint != null) {
        final Optional<DOMRpcService> mountRpcServices = mountPoint.getService(DOMRpcService.class);
        if (mountRpcServices.isEmpty()) {
            LOG.debug("Error: Rpc service is missing.");
            throw new RestconfDocumentedException("Rpc service is missing.");
        }
        schemaContext = modelContext(mountPoint);
        response = mountRpcServices.get().invokeRpc(schema.getQName(), input);
    } else {
        final XMLNamespace namespace = schema.getQName().getNamespace();
        if (namespace.toString().equals(SAL_REMOTE_NAMESPACE)) {
            if (identifier.contains(CREATE_DATA_SUBSCR)) {
                response = invokeSalRemoteRpcSubscribeRPC(payload);
            } else if (identifier.contains(CREATE_NOTIFICATION_STREAM)) {
                response = invokeSalRemoteRpcNotifiStrRPC(payload);
            } else {
                final String msg = "Not supported operation";
                LOG.warn(msg);
                throw new RestconfDocumentedException(msg, ErrorType.RPC, ErrorTag.OPERATION_NOT_SUPPORTED);
            }
        } else {
            response = broker.invokeRpc(schema.getQName(), input);
        }
        schemaContext = controllerContext.getGlobalSchema();
    }
    final DOMRpcResult result = checkRpcResponse(response);
    RpcDefinition resultNodeSchema = null;
    final NormalizedNode resultData;
    if (result != null && result.getResult() != null) {
        resultData = result.getResult();
        resultNodeSchema = (RpcDefinition) payload.getInstanceIdentifierContext().getSchemaNode();
    } else {
        resultData = null;
    }
    if (resultData != null && ((ContainerNode) resultData).isEmpty()) {
        throw new WebApplicationException(Response.Status.NO_CONTENT);
    } else {
        return new NormalizedNodeContext(new InstanceIdentifierContext<>(null, resultNodeSchema, mountPoint, schemaContext), resultData, QueryParametersParser.parseWriterParameters(uriInfo));
    }
}
Also used : RestconfDocumentedException(org.opendaylight.restconf.common.errors.RestconfDocumentedException) DOMRpcService(org.opendaylight.mdsal.dom.api.DOMRpcService) DefaultDOMRpcResult(org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult) DOMRpcResult(org.opendaylight.mdsal.dom.api.DOMRpcResult) RpcDefinition(org.opendaylight.yangtools.yang.model.api.RpcDefinition) WebApplicationException(javax.ws.rs.WebApplicationException) DOMMountPoint(org.opendaylight.mdsal.dom.api.DOMMountPoint) NormalizedNodeContext(org.opendaylight.netconf.sal.rest.impl.NormalizedNodeContext) ContainerSchemaNode(org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode) LeafListSchemaNode(org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode) ListSchemaNode(org.opendaylight.yangtools.yang.model.api.ListSchemaNode) SchemaNode(org.opendaylight.yangtools.yang.model.api.SchemaNode) DataSchemaNode(org.opendaylight.yangtools.yang.model.api.DataSchemaNode) LeafSchemaNode(org.opendaylight.yangtools.yang.model.api.LeafSchemaNode) XMLNamespace(org.opendaylight.yangtools.yang.common.XMLNamespace) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) EffectiveModelContext(org.opendaylight.yangtools.yang.model.api.EffectiveModelContext)

Example 14 with DOMMountPoint

use of org.opendaylight.mdsal.dom.api.DOMMountPoint in project netconf by opendaylight.

the class RestconfImpl method getModules.

/**
 * Valid only for mount point.
 */
@Override
@Deprecated
public NormalizedNodeContext getModules(final String identifier, final UriInfo uriInfo) {
    if (!identifier.contains(ControllerContext.MOUNT)) {
        final String errMsg = "URI has bad format. If modules 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 MapNode mountPointModulesMap = makeModuleMapNode(controllerContext.getAllModules(mountPoint));
    final Module restconfModule = getRestconfModule();
    final DataSchemaNode modulesSchemaNode = controllerContext.getRestconfModuleRestConfSchemaNode(restconfModule, RestConfModule.MODULES_CONTAINER_SCHEMA_NODE);
    checkState(modulesSchemaNode instanceof ContainerSchemaNode);
    final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> moduleContainerBuilder = SchemaAwareBuilders.containerBuilder((ContainerSchemaNode) modulesSchemaNode);
    moduleContainerBuilder.withChild(mountPointModulesMap);
    return new NormalizedNodeContext(new InstanceIdentifierContext<>(MODULES, modulesSchemaNode, mountPoint, controllerContext.getGlobalSchema()), moduleContainerBuilder.build(), QueryParametersParser.parseWriterParameters(uriInfo));
}
Also used : RestconfDocumentedException(org.opendaylight.restconf.common.errors.RestconfDocumentedException) DataSchemaNode(org.opendaylight.yangtools.yang.model.api.DataSchemaNode) DOMMountPoint(org.opendaylight.mdsal.dom.api.DOMMountPoint) MapNode(org.opendaylight.yangtools.yang.data.api.schema.MapNode) SystemMapNode(org.opendaylight.yangtools.yang.data.api.schema.SystemMapNode) NormalizedNodeContext(org.opendaylight.netconf.sal.rest.impl.NormalizedNodeContext) ContainerSchemaNode(org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode) NodeIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier) ContainerNode(org.opendaylight.yangtools.yang.data.api.schema.ContainerNode) QNameModule(org.opendaylight.yangtools.yang.common.QNameModule) Module(org.opendaylight.yangtools.yang.model.api.Module) RestConfModule(org.opendaylight.netconf.sal.rest.api.Draft02.RestConfModule)

Example 15 with DOMMountPoint

use of org.opendaylight.mdsal.dom.api.DOMMountPoint 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)

Aggregations

DOMMountPoint (org.opendaylight.mdsal.dom.api.DOMMountPoint)38 RestconfDocumentedException (org.opendaylight.restconf.common.errors.RestconfDocumentedException)16 YangInstanceIdentifier (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)15 EffectiveModelContext (org.opendaylight.yangtools.yang.model.api.EffectiveModelContext)13 NormalizedNode (org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)9 NormalizedNodeContext (org.opendaylight.netconf.sal.rest.impl.NormalizedNodeContext)8 DataSchemaNode (org.opendaylight.yangtools.yang.model.api.DataSchemaNode)8 MdsalRestconfStrategy (org.opendaylight.restconf.nb.rfc8040.rests.transactions.MdsalRestconfStrategy)7 RestconfStrategy (org.opendaylight.restconf.nb.rfc8040.rests.transactions.RestconfStrategy)7 ContainerNode (org.opendaylight.yangtools.yang.data.api.schema.ContainerNode)6 Test (org.junit.Test)5 DOMRpcService (org.opendaylight.mdsal.dom.api.DOMRpcService)5 QName (org.opendaylight.yangtools.yang.common.QName)5 ListSchemaNode (org.opendaylight.yangtools.yang.model.api.ListSchemaNode)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 ExecutionException (java.util.concurrent.ExecutionException)4 DOMDataTreeReadWriteTransaction (org.opendaylight.mdsal.dom.api.DOMDataTreeReadWriteTransaction)4 RpcDefinition (org.opendaylight.yangtools.yang.model.api.RpcDefinition)4 SchemaNode (org.opendaylight.yangtools.yang.model.api.SchemaNode)4