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;
}
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);
}
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();
}
}
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);
}
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));
}
Aggregations