import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
import org.opendaylight.restconf.common.patch.PatchContext;
import org.opendaylight.restconf.nb.rfc8040.legacy.InstanceIdentifierContext;
-import org.opendaylight.restconf.nb.rfc8040.utils.parser.IdentifierCodec;
+import org.opendaylight.restconf.nb.rfc8040.utils.parser.YangInstanceIdentifierSerializer;
import org.opendaylight.restconf.server.api.DataPatchPath;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.patch.rev170222.yang.patch.yang.patch.Edit.Operation;
import org.opendaylight.yangtools.yang.common.ErrorTag;
if (urlPath.isEmpty()) {
targetUrl = target.startsWith("/") ? target.substring(1) : target;
} else {
- targetUrl = IdentifierCodec.serialize(urlPath, databind) + target;
+ targetUrl = new YangInstanceIdentifierSerializer(databind).serializePath(urlPath) + target;
}
try {
import org.opendaylight.restconf.common.patch.PatchStatusContext;
import org.opendaylight.restconf.common.patch.PatchStatusEntity;
import org.opendaylight.restconf.nb.rfc8040.Insert;
-import org.opendaylight.restconf.nb.rfc8040.utils.parser.IdentifierCodec;
+import org.opendaylight.restconf.nb.rfc8040.utils.parser.YangInstanceIdentifierSerializer;
import org.opendaylight.restconf.server.api.DataPostResult.CreateResource;
import org.opendaylight.restconf.server.api.DataPutResult;
import org.opendaylight.restconf.server.api.DatabindContext;
Futures.addCallback(future, new FutureCallback<CommitInfo>() {
@Override
public void onSuccess(final CommitInfo result) {
- ret.set(new CreateResource(IdentifierCodec.serialize(
+ ret.set(new CreateResource(new YangInstanceIdentifierSerializer(databind).serializePath(
data instanceof MapNode mapData && !mapData.isEmpty()
- ? path.node(mapData.body().iterator().next().name()) : path,
- databind)));
+ ? path.node(mapData.body().iterator().next().name()) : path)));
}
@Override
+++ /dev/null
-/*
- * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-package org.opendaylight.restconf.nb.rfc8040.utils.parser;
-
-import org.opendaylight.restconf.server.api.DatabindContext;
-import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
-
-/**
- * Codec for identifier to serialize {@link YangInstanceIdentifier} to {@link String} and deserialize {@link String} to
- * {@link YangInstanceIdentifier}.
- */
-public final class IdentifierCodec {
- private IdentifierCodec() {
- // Hidden on purpose
- }
-
- public static String serialize(final YangInstanceIdentifier data, final DatabindContext databind) {
- return new YangInstanceIdentifierSerializer(databind).serializePath(data);
- }
-}
import org.opendaylight.mdsal.dom.api.DOMMountPointService;
import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
import org.opendaylight.restconf.common.errors.RestconfFuture;
-import org.opendaylight.restconf.nb.rfc8040.utils.parser.IdentifierCodec;
+import org.opendaylight.restconf.nb.rfc8040.utils.parser.YangInstanceIdentifierSerializer;
import org.opendaylight.restconf.server.api.OperationsPostResult;
import org.opendaylight.restconf.server.spi.OperationInput;
import org.opendaylight.restconf.server.spi.RestconfStream;
}
return streamRegistry.createStream(restconfURI, new DeviceNotificationSource(mountPointService, path),
- "All YANG notifications occuring on mount point /" + IdentifierCodec.serialize(path, input.databind()))
+ "All YANG notifications occuring on mount point /"
+ + new YangInstanceIdentifierSerializer(input.databind()).serializePath(path))
.transform(stream -> input.newOperationOutput(Builders.containerBuilder()
.withNodeIdentifier(new NodeIdentifier(SubscribeDeviceNotificationOutput.QNAME))
.withChild(ImmutableNodes.leafNode(DEVICE_NOTIFICATION_STREAM_PATH_NODEID, stream.name()))
import org.opendaylight.mdsal.dom.api.DOMDataTreeChangeService;
import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
import org.opendaylight.restconf.common.errors.RestconfFuture;
-import org.opendaylight.restconf.nb.rfc8040.utils.parser.IdentifierCodec;
+import org.opendaylight.restconf.nb.rfc8040.utils.parser.YangInstanceIdentifierSerializer;
import org.opendaylight.restconf.server.api.OperationsPostResult;
import org.opendaylight.restconf.server.spi.DatabindProvider;
import org.opendaylight.restconf.server.spi.OperationInput;
return streamRegistry.createStream(restconfURI,
new DataTreeChangeSource(databindProvider, changeService, datastore, path),
"Events occuring in " + datastore + " datastore under /"
- + IdentifierCodec.serialize(path, input.databind()))
+ + new YangInstanceIdentifierSerializer(input.databind()).serializePath(path))
.transform(stream -> input.newOperationOutput(Builders.containerBuilder()
.withNodeIdentifier(OUTPUT_NODEID)
.withChild(ImmutableNodes.leafNode(STREAM_NAME_NODEID, stream.name()))
+++ /dev/null
-/*
- * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-package org.opendaylight.restconf.nb.rfc8040.utils.parser;
-
-import static org.junit.jupiter.api.Assertions.assertEquals;
-
-import java.text.ParseException;
-import java.util.Map;
-import org.junit.jupiter.api.Test;
-import org.opendaylight.restconf.api.ApiPath;
-import org.opendaylight.restconf.server.api.DatabindContext;
-import org.opendaylight.restconf.server.spi.ApiPathNormalizer;
-import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.common.Uint32;
-import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
-import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
-import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
-
-/**
- * Unit tests for {@link IdentifierCodec} mostly according to examples from draft-ietf-netconf-restconf-13.
- */
-class IdentifierCodecTest {
- private static final DatabindContext DATABIND = DatabindContext.ofModel(
- YangParserTestUtils.parseYangResourceDirectory("/restconf/parser"));
-
- /**
- * Positive test of deserialization URI <code>String</code> to <code>YangInstanceIdentifier</code> and
- * serialization of <code>YangInstanceIdentifier</code> to <code>String</code> when original <code>String</code>
- * URI contains list identifier and leaf identifier.
- */
- @Test
- void codecListAndLeafTest() {
- final var dataYangII = assertNormalized("list-test:top/list1=%2C%27\"%3A\"%20%2F,,foo/list2=a,b/result");
- final var list1 = QName.create("list:test", "2016-04-29", "list1");
- final var list2 = QName.create("list:test", "2016-04-29", "list2");
- assertEquals(YangInstanceIdentifier.builder()
- .node(QName.create("list:test", "2016-04-29", "top"))
- .node(list1)
- .nodeWithKey(list1, Map.<QName, Object>of(
- QName.create(list1, "key1"), ",'\":\" /",
- QName.create(list1, "key2"), "",
- QName.create(list1, "key3"), "foo"))
- .node(list2)
- .nodeWithKey(list2, Map.<QName, Object>of(
- QName.create(list2, "key4"), "a",
- QName.create(list2, "key5"), "b"))
- .node(QName.create("list:test", "2016-04-29", "result"))
- .build(), dataYangII);
- assertEquals("list-test:top/list1=%2C%27\"%3A\" %2F,,foo/list2=a,b/result",
- IdentifierCodec.serialize(dataYangII, DATABIND));
- }
-
- /**
- * Positive test of deserialization URI <code>String</code> to <code>YangInstanceIdentifier</code> and
- * serialization of <code>YangInstanceIdentifier</code> to <code>String</code> when original <code>String</code>
- * URI contains leaf list identifier.
- */
- @Test
- void codecLeafListTest() {
- final var str = "list-test:top/Y=4";
- final var dataYangII = assertNormalized(str);
- final var y = QName.create("list:test", "2016-04-29", "Y");
- assertEquals(YangInstanceIdentifier.builder()
- .node(QName.create("list:test", "2016-04-29", "top"))
- .node(y)
- .node(new NodeWithValue<>(y, Uint32.valueOf(4)))
- .build(), dataYangII);
- assertEquals(str, IdentifierCodec.serialize(dataYangII, DATABIND));
- }
-
- /**
- * Positive test of serialization of an empty {@link YangInstanceIdentifier}.
- */
- @Test
- void codecDeserializeAndSerializeEmptyTest() {
- assertEquals("", IdentifierCodec.serialize(YangInstanceIdentifier.of(), DATABIND));
- }
-
- private static YangInstanceIdentifier assertNormalized(final String str) {
- try {
- return new ApiPathNormalizer(DATABIND).normalizePath(ApiPath.parse(str)).path;
- } catch (ParseException e) {
- throw new AssertionError(e);
- }
- }
-}
import static org.junit.jupiter.api.Assertions.assertThrows;
import com.google.common.collect.ImmutableMap;
+import java.text.ParseException;
import java.util.Map;
+import org.eclipse.jdt.annotation.NonNull;
import org.junit.jupiter.api.Test;
+import org.opendaylight.restconf.api.ApiPath;
import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
import org.opendaylight.restconf.common.errors.RestconfError;
import org.opendaylight.restconf.server.api.DatabindContext;
+import org.opendaylight.restconf.server.spi.ApiPathNormalizer;
import org.opendaylight.yangtools.yang.common.ErrorTag;
import org.opendaylight.yangtools.yang.common.ErrorType;
import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.common.Uint32;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
* Unit tests for {@link YangInstanceIdentifierSerializer}.
*/
class YangInstanceIdentifierSerializerTest {
- private static final YangInstanceIdentifierSerializer SERIALIZER = new YangInstanceIdentifierSerializer(
- DatabindContext.ofModel(YangParserTestUtils.parseYangResourceDirectory("/restconf/parser/serializer")));
+ private static final @NonNull DatabindContext DATABIND = DatabindContext.ofModel(
+ YangParserTestUtils.parseYangResourceDirectory("/restconf/parser/serializer"));
+ private static final YangInstanceIdentifierSerializer SERIALIZER = new YangInstanceIdentifierSerializer(DATABIND);
/**
* Positive test of serialization of <code>YangInstanceIdentifier</code> containing container node to
assertEquals(ErrorTag.UNKNOWN_ELEMENT, error.getErrorTag());
}
+ /**
+ * Positive test of deserialization URI <code>String</code> to <code>YangInstanceIdentifier</code> and
+ * serialization of <code>YangInstanceIdentifier</code> to <code>String</code> when original <code>String</code>
+ * URI contains list identifier and leaf identifier.
+ */
+ @Test
+ void codecListAndLeafTest() {
+ final var dataYangII = assertNormalized("list-test:top/list1=%2C%27\"%3A\"%20%2F,,foo/list2=a,b/result");
+ final var list1 = QName.create("list:test", "2016-04-29", "list1");
+ final var list2 = QName.create("list:test", "2016-04-29", "list2");
+ assertEquals(YangInstanceIdentifier.builder()
+ .node(QName.create("list:test", "2016-04-29", "top"))
+ .node(list1)
+ .nodeWithKey(list1, Map.<QName, Object>of(
+ QName.create(list1, "key1"), ",'\":\" /",
+ QName.create(list1, "key2"), "",
+ QName.create(list1, "key3"), "foo"))
+ .node(list2)
+ .nodeWithKey(list2, Map.<QName, Object>of(
+ QName.create(list2, "key4"), "a",
+ QName.create(list2, "key5"), "b"))
+ .node(QName.create("list:test", "2016-04-29", "result"))
+ .build(), dataYangII);
+ assertEquals("list-test:top/list1=%2C%27\"%3A\" %2F,,foo/list2=a,b/result",
+ SERIALIZER.serializePath(dataYangII));
+ }
+
+ /**
+ * Positive test of deserialization URI <code>String</code> to <code>YangInstanceIdentifier</code> and
+ * serialization of <code>YangInstanceIdentifier</code> to <code>String</code> when original <code>String</code>
+ * URI contains leaf list identifier.
+ */
+ @Test
+ void codecLeafListTest() {
+ final var str = "list-test:top/Y=4";
+ final var dataYangII = assertNormalized(str);
+ final var y = QName.create("list:test", "2016-04-29", "Y");
+ assertEquals(YangInstanceIdentifier.builder()
+ .node(QName.create("list:test", "2016-04-29", "top"))
+ .node(y)
+ .node(new NodeWithValue<>(y, Uint32.valueOf(4)))
+ .build(), dataYangII);
+ assertEquals(str, SERIALIZER.serializePath(dataYangII));
+ }
+
+ /**
+ * Positive test of serialization of an empty {@link YangInstanceIdentifier}.
+ */
+ @Test
+ void codecDeserializeAndSerializeEmptyTest() {
+ assertEquals("", SERIALIZER.serializePath(YangInstanceIdentifier.of()));
+ }
+
+ private static YangInstanceIdentifier assertNormalized(final String str) {
+ try {
+ return new ApiPathNormalizer(DATABIND).normalizePath(ApiPath.parse(str)).path;
+ } catch (ParseException e) {
+ throw new AssertionError(e);
+ }
+ }
+
private static RestconfError assertError(final YangInstanceIdentifier path) {
final var ex = assertThrows(RestconfDocumentedException.class, () -> SERIALIZER.serializePath(path));
final var errors = ex.getErrors();
assertEquals(1, errors.size());
return errors.get(0);
}
-
}