Search in sources :

Example 6 with InstanceIdentifierContext

use of org.opendaylight.restconf.common.context.InstanceIdentifierContext 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 7 with InstanceIdentifierContext

use of org.opendaylight.restconf.common.context.InstanceIdentifierContext in project netconf by opendaylight.

the class JsonNormalizedNodeBodyReader method readFrom.

private static NormalizedNodeContext readFrom(final InstanceIdentifierContext<?> path, final InputStream entityStream, final boolean isPost) throws IOException {
    final Optional<InputStream> nonEmptyInputStreamOptional = RestUtil.isInputStreamEmpty(entityStream);
    if (nonEmptyInputStreamOptional.isEmpty()) {
        return new NormalizedNodeContext(path, null);
    }
    final NormalizedNodeResult resultHolder = new NormalizedNodeResult();
    final NormalizedNodeStreamWriter writer = ImmutableNormalizedNodeStreamWriter.from(resultHolder);
    final SchemaInferenceStack stack;
    if (path.getSchemaNode() instanceof RpcEffectiveStatement) {
        stack = SchemaInferenceStack.of(path.getSchemaContext(), Absolute.of(path.getSchemaNode().getQName()));
    } else {
        stack = SchemaInferenceStack.of(path.getSchemaContext());
        path.getInstanceIdentifier().getPathArguments().stream().filter(arg -> !(arg instanceof YangInstanceIdentifier.NodeIdentifierWithPredicates)).filter(arg -> !(arg instanceof YangInstanceIdentifier.AugmentationIdentifier)).forEach(p -> stack.enterSchemaTree(p.getNodeType()));
    }
    if (!isPost) {
        stack.exit();
    }
    final JsonParserStream jsonParser = JsonParserStream.create(writer, JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02.getShared(path.getSchemaContext()), stack.toInference());
    final JsonReader reader = new JsonReader(new InputStreamReader(nonEmptyInputStreamOptional.get(), StandardCharsets.UTF_8));
    jsonParser.parse(reader);
    NormalizedNode result = resultHolder.getResult();
    final List<YangInstanceIdentifier.PathArgument> iiToDataList = new ArrayList<>();
    InstanceIdentifierContext<? extends SchemaNode> newIIContext;
    while (result instanceof AugmentationNode || result instanceof ChoiceNode) {
        final Object childNode = ((DataContainerNode) result).body().iterator().next();
        if (isPost) {
            iiToDataList.add(result.getIdentifier());
        }
        result = (NormalizedNode) childNode;
    }
    if (isPost) {
        if (result instanceof MapEntryNode) {
            iiToDataList.add(new YangInstanceIdentifier.NodeIdentifier(result.getIdentifier().getNodeType()));
            iiToDataList.add(result.getIdentifier());
        } else {
            iiToDataList.add(result.getIdentifier());
        }
    } else {
        if (result instanceof MapNode) {
            result = Iterables.getOnlyElement(((MapNode) result).body());
        }
    }
    final YangInstanceIdentifier fullIIToData = YangInstanceIdentifier.create(Iterables.concat(path.getInstanceIdentifier().getPathArguments(), iiToDataList));
    newIIContext = new InstanceIdentifierContext<>(fullIIToData, path.getSchemaNode(), path.getMountPoint(), path.getSchemaContext());
    return new NormalizedNodeContext(newIIContext, result);
}
Also used : SchemaInferenceStack(org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack) JSONCodecFactorySupplier(org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactorySupplier) Iterables(com.google.common.collect.Iterables) Provider(javax.ws.rs.ext.Provider) ImmutableNormalizedNodeStreamWriter(org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter) InstanceIdentifierContext(org.opendaylight.restconf.common.context.InstanceIdentifierContext) Draft02(org.opendaylight.netconf.sal.rest.api.Draft02) LoggerFactory(org.slf4j.LoggerFactory) JsonReader(com.google.gson.stream.JsonReader) ArrayList(java.util.ArrayList) ErrorType(org.opendaylight.yangtools.yang.common.ErrorType) Absolute(org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute) MediaType(javax.ws.rs.core.MediaType) ResultAlreadySetException(org.opendaylight.yangtools.yang.data.impl.schema.ResultAlreadySetException) Consumes(javax.ws.rs.Consumes) NormalizedNodeResult(org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult) MapNode(org.opendaylight.yangtools.yang.data.api.schema.MapNode) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) RestconfService(org.opendaylight.netconf.sal.rest.api.RestconfService) RestUtil(org.opendaylight.restconf.common.util.RestUtil) DataContainerNode(org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode) Logger(org.slf4j.Logger) SchemaInferenceStack(org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack) ErrorTag(org.opendaylight.yangtools.yang.common.ErrorTag) Throwables(com.google.common.base.Throwables) IOException(java.io.IOException) RpcEffectiveStatement(org.opendaylight.yangtools.yang.model.api.stmt.RpcEffectiveStatement) InputStreamReader(java.io.InputStreamReader) StandardCharsets(java.nio.charset.StandardCharsets) SchemaNode(org.opendaylight.yangtools.yang.model.api.SchemaNode) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) List(java.util.List) Type(java.lang.reflect.Type) AugmentationNode(org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode) NormalizedNodeStreamWriter(org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter) ControllerContext(org.opendaylight.netconf.sal.restconf.impl.ControllerContext) JsonParserStream(org.opendaylight.yangtools.yang.data.codec.gson.JsonParserStream) Annotation(java.lang.annotation.Annotation) Optional(java.util.Optional) WebApplicationException(javax.ws.rs.WebApplicationException) MapEntryNode(org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode) RestconfDocumentedException(org.opendaylight.restconf.common.errors.RestconfDocumentedException) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) InputStream(java.io.InputStream) MessageBodyReader(javax.ws.rs.ext.MessageBodyReader) ChoiceNode(org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode) JsonParserStream(org.opendaylight.yangtools.yang.data.codec.gson.JsonParserStream) RpcEffectiveStatement(org.opendaylight.yangtools.yang.model.api.stmt.RpcEffectiveStatement) ArrayList(java.util.ArrayList) DataContainerNode(org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode) MapNode(org.opendaylight.yangtools.yang.data.api.schema.MapNode) NormalizedNodeResult(org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult) JsonReader(com.google.gson.stream.JsonReader) ChoiceNode(org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) ImmutableNormalizedNodeStreamWriter(org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter) NormalizedNodeStreamWriter(org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter) MapEntryNode(org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) AugmentationNode(org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode)

Example 8 with InstanceIdentifierContext

use of org.opendaylight.restconf.common.context.InstanceIdentifierContext in project netconf by opendaylight.

the class NormalizedNodeJsonBodyWriter method writeTo.

@Override
public void writeTo(final NormalizedNodeContext context, final Class<?> type, final Type genericType, final Annotation[] annotations, final MediaType mediaType, final MultivaluedMap<String, Object> httpHeaders, final OutputStream entityStream) throws IOException, WebApplicationException {
    if (httpHeaders != null) {
        for (final Entry<String, Object> entry : context.getNewHeaders().entrySet()) {
            httpHeaders.add(entry.getKey(), entry.getValue());
        }
    }
    NormalizedNode data = context.getData();
    if (data == null) {
        return;
    }
    @SuppressWarnings("unchecked") final InstanceIdentifierContext<SchemaNode> identifierCtx = (InstanceIdentifierContext<SchemaNode>) context.getInstanceIdentifierContext();
    try (JsonWriter jsonWriter = createJsonWriter(entityStream, context.getWriterParameters().isPrettyPrint())) {
        jsonWriter.beginObject();
        writeNormalizedNode(jsonWriter, identifierCtx, data, context.getWriterParameters().getDepth());
        jsonWriter.endObject();
        jsonWriter.flush();
    }
}
Also used : SchemaNode(org.opendaylight.yangtools.yang.model.api.SchemaNode) DataSchemaNode(org.opendaylight.yangtools.yang.model.api.DataSchemaNode) InstanceIdentifierContext(org.opendaylight.restconf.common.context.InstanceIdentifierContext) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) JsonWriter(com.google.gson.stream.JsonWriter)

Example 9 with InstanceIdentifierContext

use of org.opendaylight.restconf.common.context.InstanceIdentifierContext in project netconf by opendaylight.

the class PatchDataTransactionUtilTest method testPatchDataCreateAndDelete.

@Test
public void testPatchDataCreateAndDelete() {
    doReturn(immediateFalseFluentFuture()).when(this.rwTransaction).exists(LogicalDatastoreType.CONFIGURATION, this.instanceIdContainer);
    doReturn(immediateTrueFluentFuture()).when(this.rwTransaction).exists(LogicalDatastoreType.CONFIGURATION, this.targetNodeForCreateAndDelete);
    doReturn(Futures.immediateFuture(new DefaultDOMRpcResult())).when(this.netconfService).create(LogicalDatastoreType.CONFIGURATION, this.instanceIdContainer, this.buildBaseContainerForTests, Optional.empty());
    doReturn(Futures.immediateFuture(new DefaultDOMRpcResult())).when(this.netconfService).delete(LogicalDatastoreType.CONFIGURATION, this.targetNodeForCreateAndDelete);
    final PatchEntity entityCreate = new PatchEntity("edit1", CREATE, this.instanceIdContainer, this.buildBaseContainerForTests);
    final PatchEntity entityDelete = new PatchEntity("edit2", DELETE, this.targetNodeForCreateAndDelete);
    final List<PatchEntity> entities = new ArrayList<>();
    entities.add(entityCreate);
    entities.add(entityDelete);
    final InstanceIdentifierContext<? extends SchemaNode> iidContext = new InstanceIdentifierContext<>(this.instanceIdCreateAndDelete, null, null, this.refSchemaCtx);
    final PatchContext patchContext = new PatchContext(iidContext, entities, "patchCD");
    patch(patchContext, new MdsalRestconfStrategy(mockDataBroker), true);
    patch(patchContext, new NetconfRestconfStrategy(netconfService), true);
}
Also used : DefaultDOMRpcResult(org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult) PatchContext(org.opendaylight.restconf.common.patch.PatchContext) MdsalRestconfStrategy(org.opendaylight.restconf.nb.rfc8040.rests.transactions.MdsalRestconfStrategy) PatchEntity(org.opendaylight.restconf.common.patch.PatchEntity) ArrayList(java.util.ArrayList) InstanceIdentifierContext(org.opendaylight.restconf.common.context.InstanceIdentifierContext) NetconfRestconfStrategy(org.opendaylight.restconf.nb.rfc8040.rests.transactions.NetconfRestconfStrategy) Test(org.junit.Test)

Example 10 with InstanceIdentifierContext

use of org.opendaylight.restconf.common.context.InstanceIdentifierContext in project netconf by opendaylight.

the class PatchDataTransactionUtilTest method deleteNonexistentDataTest.

@Test
public void deleteNonexistentDataTest() {
    doReturn(immediateFalseFluentFuture()).when(this.rwTransaction).exists(LogicalDatastoreType.CONFIGURATION, this.targetNodeForCreateAndDelete);
    final NetconfDocumentedException exception = new NetconfDocumentedException("id", ErrorType.RPC, ErrorTag.DATA_MISSING, ErrorSeverity.ERROR);
    final SettableFuture<? extends DOMRpcResult> ret = SettableFuture.create();
    ret.setException(new TransactionCommitFailedException(String.format("Commit of transaction %s failed", this), exception));
    doReturn(ret).when(this.netconfService).commit();
    doReturn(Futures.immediateFuture(new DefaultDOMRpcResult())).when(this.netconfService).delete(LogicalDatastoreType.CONFIGURATION, this.targetNodeForCreateAndDelete);
    final PatchEntity entityDelete = new PatchEntity("edit", DELETE, this.targetNodeForCreateAndDelete);
    final List<PatchEntity> entities = new ArrayList<>();
    entities.add(entityDelete);
    final InstanceIdentifierContext<? extends SchemaNode> iidContext = new InstanceIdentifierContext<>(this.instanceIdCreateAndDelete, null, null, this.refSchemaCtx);
    final PatchContext patchContext = new PatchContext(iidContext, entities, "patchD");
    deleteMdsal(patchContext, new MdsalRestconfStrategy(mockDataBroker));
    deleteNetconf(patchContext, new NetconfRestconfStrategy(netconfService));
}
Also used : TransactionCommitFailedException(org.opendaylight.mdsal.common.api.TransactionCommitFailedException) DefaultDOMRpcResult(org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult) PatchContext(org.opendaylight.restconf.common.patch.PatchContext) MdsalRestconfStrategy(org.opendaylight.restconf.nb.rfc8040.rests.transactions.MdsalRestconfStrategy) PatchEntity(org.opendaylight.restconf.common.patch.PatchEntity) NetconfDocumentedException(org.opendaylight.netconf.api.NetconfDocumentedException) ArrayList(java.util.ArrayList) InstanceIdentifierContext(org.opendaylight.restconf.common.context.InstanceIdentifierContext) NetconfRestconfStrategy(org.opendaylight.restconf.nb.rfc8040.rests.transactions.NetconfRestconfStrategy) Test(org.junit.Test)

Aggregations

InstanceIdentifierContext (org.opendaylight.restconf.common.context.InstanceIdentifierContext)50 Test (org.junit.Test)29 DataSchemaNode (org.opendaylight.yangtools.yang.model.api.DataSchemaNode)28 NormalizedNodePayload (org.opendaylight.restconf.nb.rfc8040.legacy.NormalizedNodePayload)26 YangInstanceIdentifier (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)24 NormalizedNode (org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)20 ArrayList (java.util.ArrayList)18 NetconfRestconfStrategy (org.opendaylight.restconf.nb.rfc8040.rests.transactions.NetconfRestconfStrategy)16 DefaultDOMRpcResult (org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult)15 List (java.util.List)14 MdsalRestconfStrategy (org.opendaylight.restconf.nb.rfc8040.rests.transactions.MdsalRestconfStrategy)14 QName (org.opendaylight.yangtools.yang.common.QName)14 NormalizedNodeStreamWriter (org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter)14 Provider (javax.ws.rs.ext.Provider)13 IOException (java.io.IOException)12 RestconfDocumentedException (org.opendaylight.restconf.common.errors.RestconfDocumentedException)12 PatchContext (org.opendaylight.restconf.common.patch.PatchContext)12 MapEntryNode (org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode)12 StandardCharsets (java.nio.charset.StandardCharsets)11 WebApplicationException (javax.ws.rs.WebApplicationException)11