Search in sources :

Example 1 with ActionDefinition

use of org.opendaylight.yangtools.yang.model.api.ActionDefinition in project lighty-netconf-simulator by PANTHEONtech.

the class ActionServiceDeviceProcessor method execute.

@Override
protected CompletableFuture<Response> execute(final Element requestXmlElement) {
    final XmlElement fromDomElement = XmlElement.fromDomElement(requestXmlElement);
    final Optional<ActionDefinition> actionDefinition = requireNonNull(findActionInElement(fromDomElement));
    if (actionDefinition.isPresent() && actionDefinition.get().getQName().equals(Start.QNAME)) {
        this.actionProcessor = new StartActionProcessor(new StartAction(), this.adapterContext.currentSerializer());
    } else {
        this.actionProcessor = new ResetActionProcessor(new ResetAction(), this.adapterContext.currentSerializer());
    }
    this.actionProcessor.init(getNetconfDeviceServices());
    return this.actionProcessor.execute(requestXmlElement, actionDefinition.get());
}
Also used : StartAction(io.lighty.netconf.device.action.actions.StartAction) ResetAction(io.lighty.netconf.device.action.actions.ResetAction) XmlElement(org.opendaylight.netconf.api.xml.XmlElement) ActionDefinition(org.opendaylight.yangtools.yang.model.api.ActionDefinition)

Example 2 with ActionDefinition

use of org.opendaylight.yangtools.yang.model.api.ActionDefinition in project netconf by opendaylight.

the class JsonNormalizedNodeBodyWriter method writeNormalizedNode.

private static void writeNormalizedNode(final JsonWriter jsonWriter, final SchemaPath path, final InstanceIdentifierContext<SchemaNode> context, final NormalizedNode data, final DepthParam depth, final List<Set<QName>> fields) throws IOException {
    final RestconfNormalizedNodeWriter nnWriter;
    if (context.getSchemaNode() instanceof RpcDefinition) {
        /*
             *  RpcDefinition is not supported as initial codec in JSONStreamWriter,
             *  so we need to emit initial output declaration..
             */
        final RpcDefinition rpc = (RpcDefinition) context.getSchemaNode();
        final SchemaPath rpcPath = SchemaPath.of(Absolute.of(rpc.getQName(), rpc.getOutput().getQName()));
        nnWriter = createNormalizedNodeWriter(context, rpcPath, jsonWriter, depth, fields);
        final Module module = context.getSchemaContext().findModule(data.getIdentifier().getNodeType().getModule()).get();
        jsonWriter.name(module.getName() + ":output");
        jsonWriter.beginObject();
        writeChildren(nnWriter, (ContainerNode) data);
        jsonWriter.endObject();
    } else if (context.getSchemaNode() instanceof ActionDefinition) {
        /*
             *  ActionDefinition is not supported as initial codec in JSONStreamWriter,
             *  so we need to emit initial output declaration..
             */
        final ActionDefinition actDef = (ActionDefinition) context.getSchemaNode();
        final List<QName> qNames = context.getInstanceIdentifier().getPathArguments().stream().filter(arg -> !(arg instanceof YangInstanceIdentifier.NodeIdentifierWithPredicates)).filter(arg -> !(arg instanceof YangInstanceIdentifier.AugmentationIdentifier)).map(PathArgument::getNodeType).collect(Collectors.toList());
        qNames.add(actDef.getQName());
        qNames.add(actDef.getOutput().getQName());
        final SchemaPath actPath = SchemaPath.of(Absolute.of(qNames));
        nnWriter = createNormalizedNodeWriter(context, actPath, jsonWriter, depth, fields);
        final Module module = context.getSchemaContext().findModule(data.getIdentifier().getNodeType().getModule()).get();
        jsonWriter.name(module.getName() + ":output");
        jsonWriter.beginObject();
        writeChildren(nnWriter, (ContainerNode) data);
        jsonWriter.endObject();
    } else {
        if (SchemaPath.ROOT.equals(path)) {
            nnWriter = createNormalizedNodeWriter(context, path, jsonWriter, depth, fields);
        } else {
            nnWriter = createNormalizedNodeWriter(context, path.getParent(), jsonWriter, depth, fields);
        }
        if (data instanceof MapEntryNode) {
            // Restconf allows returning one list item. We need to wrap it
            // in map node in order to serialize it properly
            nnWriter.write(ImmutableNodes.mapNodeBuilder(data.getIdentifier().getNodeType()).withChild((MapEntryNode) data).build());
        } else {
            nnWriter.write(data);
        }
    }
    nnWriter.flush();
}
Also used : JSONNormalizedNodeStreamWriter(org.opendaylight.yangtools.yang.data.codec.gson.JSONNormalizedNodeStreamWriter) JSONCodecFactorySupplier(org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactorySupplier) SchemaPath(org.opendaylight.yangtools.yang.model.api.SchemaPath) Produces(javax.ws.rs.Produces) Provider(javax.ws.rs.ext.Provider) InstanceIdentifierContext(org.opendaylight.restconf.common.context.InstanceIdentifierContext) RpcDefinition(org.opendaylight.yangtools.yang.model.api.RpcDefinition) NormalizedNodePayload(org.opendaylight.restconf.nb.rfc8040.legacy.NormalizedNodePayload) XMLNamespace(org.opendaylight.yangtools.yang.common.XMLNamespace) JsonWriterFactory(org.opendaylight.yangtools.yang.data.codec.gson.JsonWriterFactory) Absolute(org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute) MediaType(javax.ws.rs.core.MediaType) Map(java.util.Map) OutputStreamWriter(java.io.OutputStreamWriter) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) JsonWriter(com.google.gson.stream.JsonWriter) ContainerNode(org.opendaylight.yangtools.yang.data.api.schema.ContainerNode) OutputStream(java.io.OutputStream) DepthParam(org.opendaylight.restconf.nb.rfc8040.DepthParam) Module(org.opendaylight.yangtools.yang.model.api.Module) SchemaInferenceStack(org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack) Set(java.util.Set) IOException(java.io.IOException) DataContainerChild(org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) QName(org.opendaylight.yangtools.yang.common.QName) MediaTypes(org.opendaylight.restconf.nb.rfc8040.MediaTypes) RestconfNormalizedNodeWriter(org.opendaylight.restconf.nb.rfc8040.jersey.providers.api.RestconfNormalizedNodeWriter) SchemaNode(org.opendaylight.yangtools.yang.model.api.SchemaNode) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) List(java.util.List) PathArgument(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument) Type(java.lang.reflect.Type) NormalizedNodeStreamWriter(org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter) ImmutableNodes(org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes) Annotation(java.lang.annotation.Annotation) JSONCodecFactory(org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactory) WebApplicationException(javax.ws.rs.WebApplicationException) ActionDefinition(org.opendaylight.yangtools.yang.model.api.ActionDefinition) MapEntryNode(org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) RestconfNormalizedNodeWriter(org.opendaylight.restconf.nb.rfc8040.jersey.providers.api.RestconfNormalizedNodeWriter) RpcDefinition(org.opendaylight.yangtools.yang.model.api.RpcDefinition) MapEntryNode(org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) ActionDefinition(org.opendaylight.yangtools.yang.model.api.ActionDefinition) SchemaPath(org.opendaylight.yangtools.yang.model.api.SchemaPath) List(java.util.List) ContainerNode(org.opendaylight.yangtools.yang.data.api.schema.ContainerNode) PathArgument(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument) Module(org.opendaylight.yangtools.yang.model.api.Module)

Example 3 with ActionDefinition

use of org.opendaylight.yangtools.yang.model.api.ActionDefinition in project netconf by opendaylight.

the class NetconfMessageTransformer method toActionResult.

@Override
public DOMActionResult toActionResult(final Absolute action, final NetconfMessage message) {
    final ActionDefinition actionDefinition = actions.get(action);
    Preconditions.checkArgument(actionDefinition != null, "Action does not exist: %s", action);
    final ContainerNode normalizedNode = (ContainerNode) parseResult(message, actionDefinition);
    if (normalizedNode == null) {
        return new SimpleDOMActionResult(Collections.emptyList());
    } else {
        return new SimpleDOMActionResult(normalizedNode, Collections.emptyList());
    }
}
Also used : SimpleDOMActionResult(org.opendaylight.mdsal.dom.spi.SimpleDOMActionResult) ContainerNode(org.opendaylight.yangtools.yang.data.api.schema.ContainerNode) ActionDefinition(org.opendaylight.yangtools.yang.model.api.ActionDefinition)

Example 4 with ActionDefinition

use of org.opendaylight.yangtools.yang.model.api.ActionDefinition in project netconf by opendaylight.

the class NetconfMessageTransformer method toActionRequest.

@Override
public NetconfMessage toActionRequest(final Absolute action, final DOMDataTreeIdentifier domDataTreeIdentifier, final NormalizedNode payload) {
    final ActionDefinition actionDef = actions.get(action);
    Preconditions.checkArgument(actionDef != null, "Action does not exist: %s", action);
    final InputSchemaNode inputDef = actionDef.getInput();
    if (inputDef.getChildNodes().isEmpty()) {
        return new NetconfMessage(NetconfMessageTransformUtil.prepareDomResultForActionRequest(contextTree, domDataTreeIdentifier, counter, actionDef.getQName()).getNode().getOwnerDocument());
    }
    Preconditions.checkNotNull(payload, "Transforming an action with input: %s, payload cannot be null", action);
    Preconditions.checkArgument(payload instanceof ContainerNode, "Transforming an action with input: %s, payload has to be a container, but was: %s", action, payload);
    final DOMResult result = NetconfMessageTransformUtil.prepareDomResultForActionRequest(contextTree, domDataTreeIdentifier, counter, actionDef.getQName());
    try {
        NetconfMessageTransformUtil.writeNormalizedOperationInput((ContainerNode) payload, result, action, mountContext.getEffectiveModelContext());
    } catch (final XMLStreamException | IOException | IllegalStateException e) {
        throw new IllegalStateException("Unable to serialize input of " + action, e);
    }
    return new NetconfMessage(result.getNode().getOwnerDocument());
}
Also used : DOMResult(javax.xml.transform.dom.DOMResult) XMLStreamException(javax.xml.stream.XMLStreamException) NetconfMessage(org.opendaylight.netconf.api.NetconfMessage) InputSchemaNode(org.opendaylight.yangtools.yang.model.api.InputSchemaNode) ContainerNode(org.opendaylight.yangtools.yang.data.api.schema.ContainerNode) IOException(java.io.IOException) ActionDefinition(org.opendaylight.yangtools.yang.model.api.ActionDefinition)

Example 5 with ActionDefinition

use of org.opendaylight.yangtools.yang.model.api.ActionDefinition in project netconf by opendaylight.

the class ParserIdentifier method getPathSchema.

private static SchemaNode getPathSchema(final EffectiveModelContext schemaContext, final YangInstanceIdentifier urlPath) {
    // First things first: an empty path means data invocation on SchemaContext
    if (urlPath.isEmpty()) {
        return schemaContext;
    }
    // Peel the last component and locate the parent data node, empty path resolves to SchemaContext
    final DataSchemaContextNode<?> parent = DataSchemaContextTree.from(schemaContext).findChild(verifyNotNull(urlPath.getParent())).orElseThrow(// Parent data node is not present, this is not a valid location.
    () -> new RestconfDocumentedException("Parent of " + urlPath + " not found", ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE));
    // Now try to resolve the last component as a data item...
    final DataSchemaContextNode<?> data = parent.getChild(urlPath.getLastPathArgument());
    if (data != null) {
        return data.getDataSchemaNode();
    }
    // ... otherwise this has to be an operation invocation. RPCs cannot be defined anywhere but schema root,
    // actions can reside everywhere else (and SchemaContext reports them empty)
    final QName qname = urlPath.getLastPathArgument().getNodeType();
    final DataSchemaNode parentSchema = parent.getDataSchemaNode();
    if (parentSchema instanceof SchemaContext) {
        for (final RpcDefinition rpc : ((SchemaContext) parentSchema).getOperations()) {
            if (qname.equals(rpc.getQName())) {
                return rpc;
            }
        }
    }
    if (parentSchema instanceof ActionNodeContainer) {
        for (final ActionDefinition action : ((ActionNodeContainer) parentSchema).getActions()) {
            if (qname.equals(action.getQName())) {
                return action;
            }
        }
    }
    // is deemed invalid
    throw new RestconfDocumentedException("Context for " + urlPath + " not found", ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
}
Also used : RestconfDocumentedException(org.opendaylight.restconf.common.errors.RestconfDocumentedException) RpcDefinition(org.opendaylight.yangtools.yang.model.api.RpcDefinition) DataSchemaNode(org.opendaylight.yangtools.yang.model.api.DataSchemaNode) QName(org.opendaylight.yangtools.yang.common.QName) ActionNodeContainer(org.opendaylight.yangtools.yang.model.api.ActionNodeContainer) SchemaContext(org.opendaylight.yangtools.yang.model.api.SchemaContext) ActionDefinition(org.opendaylight.yangtools.yang.model.api.ActionDefinition)

Aggregations

ActionDefinition (org.opendaylight.yangtools.yang.model.api.ActionDefinition)11 ContainerNode (org.opendaylight.yangtools.yang.data.api.schema.ContainerNode)5 QName (org.opendaylight.yangtools.yang.common.QName)4 IOException (java.io.IOException)3 List (java.util.List)3 Map (java.util.Map)3 Collectors (java.util.stream.Collectors)3 InstanceIdentifierContext (org.opendaylight.restconf.common.context.InstanceIdentifierContext)3 NormalizedNodePayload (org.opendaylight.restconf.nb.rfc8040.legacy.NormalizedNodePayload)3 YangInstanceIdentifier (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)3 PathArgument (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument)3 MapEntryNode (org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode)3 NormalizedNode (org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)3 Absolute (org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute)3 OutputStream (java.io.OutputStream)2 Annotation (java.lang.annotation.Annotation)2 Type (java.lang.reflect.Type)2 StandardCharsets (java.nio.charset.StandardCharsets)2 Set (java.util.Set)2 Test (org.junit.Test)2