Search in sources :

Example 6 with NormalizedNodeContext

use of org.opendaylight.netconf.sal.rest.impl.NormalizedNodeContext in project netconf by opendaylight.

the class InvokeRpcMethodTest method testInvokeRpcMethodWithInput.

@Test
@Ignore
public void testInvokeRpcMethodWithInput() {
    final DOMRpcResult expResult = mock(DOMRpcResult.class);
    final QName path = QName.create("(http://netconfcentral.org/ns/toaster?revision=2009-11-20)make-toast");
    final Module rpcModule = schemaContext.findModules("toaster").iterator().next();
    assertNotNull(rpcModule);
    final QName rpcQName = QName.create(rpcModule.getQNameModule(), "make-toast");
    RpcDefinition rpcDef = null;
    ContainerLike rpcInputSchemaNode = null;
    for (final RpcDefinition rpc : rpcModule.getRpcs()) {
        if (rpcQName.isEqualWithoutRevision(rpc.getQName())) {
            rpcInputSchemaNode = rpc.getInput();
            rpcDef = rpc;
            break;
        }
    }
    assertNotNull(rpcDef);
    assertNotNull(rpcInputSchemaNode);
    final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> containerBuilder = SchemaAwareBuilders.containerBuilder(rpcInputSchemaNode);
    final NormalizedNodeContext payload = new NormalizedNodeContext(new InstanceIdentifierContext<>(null, rpcInputSchemaNode, null, schemaContext), containerBuilder.build());
    doReturn(immediateFluentFuture(expResult)).when(brokerFacade).invokeRpc(eq(path), any(NormalizedNode.class));
    final NormalizedNodeContext output = this.restconfImpl.invokeRpc("toaster:make-toast", payload, uriInfo);
    assertNotNull(output);
    assertEquals(null, output.getData());
// additional validation in the fact that the restconfImpl does not
// throw an exception.
}
Also used : ContainerLike(org.opendaylight.yangtools.yang.model.api.ContainerLike) DefaultDOMRpcResult(org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult) DOMRpcResult(org.opendaylight.mdsal.dom.api.DOMRpcResult) RpcDefinition(org.opendaylight.yangtools.yang.model.api.RpcDefinition) QName(org.opendaylight.yangtools.yang.common.QName) NodeIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier) ContainerNode(org.opendaylight.yangtools.yang.data.api.schema.ContainerNode) Module(org.opendaylight.yangtools.yang.model.api.Module) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) NormalizedNodeContext(org.opendaylight.netconf.sal.rest.impl.NormalizedNodeContext) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 7 with NormalizedNodeContext

use of org.opendaylight.netconf.sal.rest.impl.NormalizedNodeContext 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 8 with NormalizedNodeContext

use of org.opendaylight.netconf.sal.rest.impl.NormalizedNodeContext in project netconf by opendaylight.

the class RestconfImpl method getAvailableStreams.

@Override
@Deprecated
public NormalizedNodeContext getAvailableStreams(final UriInfo uriInfo) {
    final EffectiveModelContext schemaContext = controllerContext.getGlobalSchema();
    final Set<String> availableStreams = Notificator.getStreamNames();
    final Module restconfModule = getRestconfModule();
    final DataSchemaNode streamSchemaNode = controllerContext.getRestconfModuleRestConfSchemaNode(restconfModule, RestConfModule.STREAM_LIST_SCHEMA_NODE);
    checkState(streamSchemaNode instanceof ListSchemaNode);
    final CollectionNodeBuilder<MapEntryNode, SystemMapNode> listStreamsBuilder = SchemaAwareBuilders.mapBuilder((ListSchemaNode) streamSchemaNode);
    for (final String streamName : availableStreams) {
        listStreamsBuilder.withChild(toStreamEntryNode(streamName, streamSchemaNode));
    }
    final DataSchemaNode streamsContainerSchemaNode = controllerContext.getRestconfModuleRestConfSchemaNode(restconfModule, RestConfModule.STREAMS_CONTAINER_SCHEMA_NODE);
    checkState(streamsContainerSchemaNode instanceof ContainerSchemaNode);
    final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> streamsContainerBuilder = SchemaAwareBuilders.containerBuilder((ContainerSchemaNode) streamsContainerSchemaNode);
    streamsContainerBuilder.withChild(listStreamsBuilder.build());
    return new NormalizedNodeContext(new InstanceIdentifierContext<>(STREAMS, streamsContainerSchemaNode, null, schemaContext), streamsContainerBuilder.build(), QueryParametersParser.parseWriterParameters(uriInfo));
}
Also used : SystemMapNode(org.opendaylight.yangtools.yang.data.api.schema.SystemMapNode) DataSchemaNode(org.opendaylight.yangtools.yang.model.api.DataSchemaNode) MapEntryNode(org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode) 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) 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) EffectiveModelContext(org.opendaylight.yangtools.yang.model.api.EffectiveModelContext)

Example 9 with NormalizedNodeContext

use of org.opendaylight.netconf.sal.rest.impl.NormalizedNodeContext 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 10 with NormalizedNodeContext

use of org.opendaylight.netconf.sal.rest.impl.NormalizedNodeContext in project netconf by opendaylight.

the class RestconfImpl method getModules.

@Override
@Deprecated
public NormalizedNodeContext getModules(final UriInfo uriInfo) {
    final MapNode allModuleMap = makeModuleMapNode(controllerContext.getAllModules());
    final EffectiveModelContext schemaContext = controllerContext.getGlobalSchema();
    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(allModuleMap);
    return new NormalizedNodeContext(new InstanceIdentifierContext<>(MODULES, modulesSchemaNode, null, schemaContext), moduleContainerBuilder.build(), QueryParametersParser.parseWriterParameters(uriInfo));
}
Also used : ContainerSchemaNode(org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode) DataSchemaNode(org.opendaylight.yangtools.yang.model.api.DataSchemaNode) NodeIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier) ContainerNode(org.opendaylight.yangtools.yang.data.api.schema.ContainerNode) MapNode(org.opendaylight.yangtools.yang.data.api.schema.MapNode) SystemMapNode(org.opendaylight.yangtools.yang.data.api.schema.SystemMapNode) 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) NormalizedNodeContext(org.opendaylight.netconf.sal.rest.impl.NormalizedNodeContext)

Aggregations

NormalizedNodeContext (org.opendaylight.netconf.sal.rest.impl.NormalizedNodeContext)105 Test (org.junit.Test)70 InputStream (java.io.InputStream)36 AbstractBodyReaderTest (org.opendaylight.controller.sal.rest.impl.test.providers.AbstractBodyReaderTest)34 DataSchemaNode (org.opendaylight.yangtools.yang.model.api.DataSchemaNode)34 QName (org.opendaylight.yangtools.yang.common.QName)25 YangInstanceIdentifier (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)22 ContainerNode (org.opendaylight.yangtools.yang.data.api.schema.ContainerNode)21 NodeIdentifier (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier)18 RestconfDocumentedException (org.opendaylight.restconf.common.errors.RestconfDocumentedException)13 LeafSchemaNode (org.opendaylight.yangtools.yang.model.api.LeafSchemaNode)13 Module (org.opendaylight.yangtools.yang.model.api.Module)12 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 OutputStream (java.io.OutputStream)9 NormalizedNode (org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)9 ContainerSchemaNode (org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode)9 UriInfo (javax.ws.rs.core.UriInfo)8 DOMMountPoint (org.opendaylight.mdsal.dom.api.DOMMountPoint)8 QNameModule (org.opendaylight.yangtools.yang.common.QNameModule)8 IOException (java.io.IOException)7