Fix for colon handling in value
Change-Id: Id2e78bdf13f07bc12d3faa95dc23d0dbd6044382
diff --git a/runtime/src/main/java/org/onosproject/yang/runtime/SerializerHelper.java b/runtime/src/main/java/org/onosproject/yang/runtime/SerializerHelper.java
index 2a48d90..bc45189 100644
--- a/runtime/src/main/java/org/onosproject/yang/runtime/SerializerHelper.java
+++ b/runtime/src/main/java/org/onosproject/yang/runtime/SerializerHelper.java
@@ -361,20 +361,30 @@
throw new IllegalArgumentException(E_RESID);
}
schema = (LeafSchemaContext) childSchema;
+
+ lType = schema.getLeafType(value);
+ if (!lType.equals(LeafType.IDENTITYREF) && valNamespace != null) {
+ value = valNamespace + ":" + value;
+ valNamespace = null;
+ }
+
valObject = getLeaf(value, schema);
valNamespace = getValidValNamespace(value, schema,
valNamespace);
- lType = schema.getLeafType(value);
builder = LeafNode.builder(name, namespace)
.type(nodeType).value(valObject)
.valueNamespace(valNamespace).leafType(lType);
break;
case MULTI_INSTANCE_LEAF_VALUE_NODE:
schema = (LeafSchemaContext) childSchema;
+ lType = schema.getLeafType(value);
+ if (!lType.equals(LeafType.IDENTITYREF) && valNamespace != null) {
+ value = valNamespace + ":" + value;
+ valNamespace = null;
+ }
valObject = getLeafList(value, schema);
valNamespace = getValidValNamespace(value, schema,
valNamespace);
- lType = schema.getLeafType(value);
builder = LeafNode.builder(name, namespace)
.type(nodeType).value(valObject)
.valueNamespace(valNamespace).leafType(lType);
@@ -393,10 +403,14 @@
switch (nodeType) {
case SINGLE_INSTANCE_LEAF_VALUE_NODE:
schema = (LeafSchemaContext) childSchema;
+ lType = schema.getLeafType(value);
+ if (!lType.equals(LeafType.IDENTITYREF) && valNamespace != null) {
+ value = valNamespace + ":" + value;
+ valNamespace = null;
+ }
valObject = getLeaf(value, schema);
valNamespace = getValidValNamespace(value, schema,
valNamespace);
- lType = schema.getLeafType(value);
if (((YangLeaf) childSchema).isKeyLeaf()) {
builder = builder.addKeyLeaf(
name, namespace, valObject);
@@ -407,10 +421,14 @@
break;
case MULTI_INSTANCE_LEAF_VALUE_NODE:
schema = (LeafSchemaContext) childSchema;
+ lType = schema.getLeafType(value);
+ if (!lType.equals(LeafType.IDENTITYREF) && valNamespace != null) {
+ value = valNamespace + ":" + value;
+ valNamespace = null;
+ }
valObject = getLeafList(value, schema);
valNamespace = getValidValNamespace(value, schema,
valNamespace);
- lType = schema.getLeafType(value);
builder = builder.createChildBuilder(
name, namespace, valObject, valNamespace)
.type(nodeType).leafType(lType);
diff --git a/serializers/json/src/test/java/org/onosproject/yang/serializers/json/JsonSerializerTest.java b/serializers/json/src/test/java/org/onosproject/yang/serializers/json/JsonSerializerTest.java
index 2c43192..1667674 100644
--- a/serializers/json/src/test/java/org/onosproject/yang/serializers/json/JsonSerializerTest.java
+++ b/serializers/json/src/test/java/org/onosproject/yang/serializers/json/JsonSerializerTest.java
@@ -117,6 +117,42 @@
}
@Test
+ public void colonTest() throws IOException {
+ String path = "src/test/resources/colontest.json";
+ // decode
+ DefaultCompositeStream external =
+ new DefaultCompositeStream("demo1:device", parseInput(path));
+ CompositeData compositeData = jsonSerializer.decode(external, context);
+ ResourceData resourceData = compositeData.resourceData();
+ ResourceId rid = resourceData.resourceId();
+ DataNode rootNode = resourceData.dataNodes().get(0);
+
+ // encode
+ RuntimeContext.Builder runtimeContextBuilder = DefaultRuntimeContext.builder();
+ runtimeContextBuilder.setDataFormat("JSON");
+ DefaultResourceData.Builder resourceDataBuilder = DefaultResourceData.builder();
+ resourceDataBuilder.addDataNode(rootNode);
+ resourceDataBuilder.resourceId(rid);
+
+ ResourceData resourceDataOutput = resourceDataBuilder.build();
+ DefaultCompositeData.Builder compositeDataBuilder = DefaultCompositeData.builder();
+ compositeDataBuilder.resourceData(resourceDataOutput);
+ CompositeData compositeData1 = compositeDataBuilder.build();
+ // CompositeData --- YangRuntimeService ---> CompositeStream.
+ CompositeStream compositeStreamOutPut = jsonSerializer.encode(compositeData1,
+ context);
+ InputStream inputStreamOutput = compositeStreamOutPut.resourceData();
+ ObjectNode rootNodeOutput;
+ ObjectMapper mapper = new ObjectMapper();
+ try {
+ rootNodeOutput = (ObjectNode) mapper.readTree(inputStreamOutput);
+ assertEquals(true, rootNodeOutput != null);
+ } catch (IOException e) {
+ throw e;
+ }
+ }
+
+ @Test
public void jsonListTest() throws IOException {
String path = "src/test/resources/testinput1.json";
// decode
diff --git a/serializers/json/src/test/resources/colontest.json b/serializers/json/src/test/resources/colontest.json
new file mode 100644
index 0000000..10d715f
--- /dev/null
+++ b/serializers/json/src/test/resources/colontest.json
@@ -0,0 +1,3 @@
+{
+ "req-start-date-time":"2018-09-15T15:53:00+05:30"
+}
diff --git a/serializers/json/src/test/resources/demo1.yang b/serializers/json/src/test/resources/demo1.yang
index edf17eb..1dd1ea2 100644
--- a/serializers/json/src/test/resources/demo1.yang
+++ b/serializers/json/src/test/resources/demo1.yang
@@ -4,6 +4,9 @@
prefix "demo1";
revision "2013-07-15";
container device {
+
+ uses start-end-date-time;
+
list device {
key deviceid;
leaf deviceid {
@@ -79,6 +82,24 @@
}
}
+ grouping start-end-date-time
+ {
+ leaf req-start-date-time
+ {
+ type string {
+ pattern '\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?(Z|[\+\-]\d{2}:\d{2})';
+ }
+ }
+ leaf req-end-date-time
+ {
+ type string {
+ pattern '\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?'
+ + '(Z|[\+\-]\d{2}:\d{2})';
+ }
+ }
+ description "";
+ }
+
list list1 {
key leaf1;
leaf leaf1 {