Search in sources :

Example 11 with NormalizedNodeContext

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

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

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

Example 14 with NormalizedNodeContext

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

the class RestconfImpl method subscribeToStream.

/**
 * Subscribes to some path in schema context (stream) to listen on changes
 * on this stream.
 *
 * <p>
 * Additional parameters for subscribing to stream are loaded via rpc input
 * parameters:
 * <ul>
 * <li>datastore - default CONFIGURATION (other values of
 * {@link LogicalDatastoreType} enum type)</li>
 * <li>scope - default BASE (other values of {@link Scope})</li>
 * </ul>
 */
@Override
public NormalizedNodeContext subscribeToStream(final String identifier, final UriInfo uriInfo) {
    boolean startTimeUsed = false;
    boolean stopTimeUsed = false;
    Instant start = Instant.now();
    Instant stop = null;
    boolean filterUsed = false;
    String filter = null;
    boolean leafNodesOnlyUsed = false;
    boolean leafNodesOnly = false;
    boolean skipNotificationDataUsed = false;
    boolean skipNotificationData = false;
    for (final Entry<String, List<String>> entry : uriInfo.getQueryParameters().entrySet()) {
        switch(entry.getKey()) {
            case "start-time":
                if (!startTimeUsed) {
                    startTimeUsed = true;
                    start = parseDateFromQueryParam(entry);
                } else {
                    throw new RestconfDocumentedException("Start-time parameter can be used only once.");
                }
                break;
            case "stop-time":
                if (!stopTimeUsed) {
                    stopTimeUsed = true;
                    stop = parseDateFromQueryParam(entry);
                } else {
                    throw new RestconfDocumentedException("Stop-time parameter can be used only once.");
                }
                break;
            case "filter":
                if (!filterUsed) {
                    filterUsed = true;
                    filter = entry.getValue().iterator().next();
                } else {
                    throw new RestconfDocumentedException("Filter parameter can be used only once.");
                }
                break;
            case "odl-leaf-nodes-only":
                if (!leafNodesOnlyUsed) {
                    leafNodesOnlyUsed = true;
                    leafNodesOnly = Boolean.parseBoolean(entry.getValue().iterator().next());
                } else {
                    throw new RestconfDocumentedException("Odl-leaf-nodes-only parameter can be used only once.");
                }
                break;
            case "odl-skip-notification-data":
                if (!skipNotificationDataUsed) {
                    skipNotificationDataUsed = true;
                    skipNotificationData = Boolean.parseBoolean(entry.getValue().iterator().next());
                } else {
                    throw new RestconfDocumentedException("Odl-skip-notification-data parameter can be used only once.");
                }
                break;
            default:
                throw new RestconfDocumentedException("Bad parameter used with notifications: " + entry.getKey());
        }
    }
    if (!startTimeUsed && stopTimeUsed) {
        throw new RestconfDocumentedException("Stop-time parameter has to be used with start-time parameter.");
    }
    URI response = null;
    if (identifier.contains(DATA_SUBSCR)) {
        response = dataSubs(identifier, uriInfo, start, stop, filter, leafNodesOnly, skipNotificationData);
    } else if (identifier.contains(NOTIFICATION_STREAM)) {
        response = notifStream(identifier, uriInfo, start, stop, filter);
    }
    if (response != null) {
        // prepare node with value of location
        final InstanceIdentifierContext<?> iid = prepareIIDSubsStreamOutput();
        final NormalizedNodeBuilder<NodeIdentifier, Object, LeafNode<Object>> builder = ImmutableLeafNodeBuilder.create().withValue(response.toString());
        builder.withNodeIdentifier(NodeIdentifier.create(QName.create("subscribe:to:notification", "2016-10-28", "location")));
        // prepare new header with location
        return new NormalizedNodeContext(iid, builder.build(), ImmutableMap.of("Location", response));
    }
    final String msg = "Bad type of notification of sal-remote";
    LOG.warn(msg);
    throw new RestconfDocumentedException(msg);
}
Also used : RestconfDocumentedException(org.opendaylight.restconf.common.errors.RestconfDocumentedException) Instant(java.time.Instant) URI(java.net.URI) NormalizedNodeContext(org.opendaylight.netconf.sal.rest.impl.NormalizedNodeContext) NodeIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier) LeafNode(org.opendaylight.yangtools.yang.data.api.schema.LeafNode) ArrayList(java.util.ArrayList) List(java.util.List)

Example 15 with NormalizedNodeContext

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

the class StatisticsRestconfServiceWrapper method readConfigurationData.

@SuppressWarnings("checkstyle:IllegalCatch")
@Override
public NormalizedNodeContext readConfigurationData(final String identifier, final UriInfo uriInfo) {
    this.configGet.incrementAndGet();
    NormalizedNodeContext normalizedNodeContext = null;
    try {
        normalizedNodeContext = this.delegate.readConfigurationData(identifier, uriInfo);
        if (normalizedNodeContext.getData() != null) {
            this.successGetConfig.incrementAndGet();
        } else {
            this.failureGetConfig.incrementAndGet();
        }
    } catch (final Exception e) {
        this.failureGetConfig.incrementAndGet();
        throw e;
    }
    return normalizedNodeContext;
}
Also used : 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