Skip to content
This repository was archived by the owner on Nov 5, 2024. It is now read-only.

Commit 3769a33

Browse files
[ONOS-7882] Ordering Childs for json serialization to avoid splitting array/list nodes and reading just the bottom portion in the final json node.
Change-Id: Ifc0eafcd9f32c87245efec844610aa640234493f
1 parent 78c44f1 commit 3769a33

File tree

3 files changed

+45
-74
lines changed

3 files changed

+45
-74
lines changed

runtime/src/main/java/org/onosproject/yang/runtime/impl/DefaultYangModelRegistry.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ public void registerModel(ModelRegistrationParam param) throws
165165

166166
//update child context
167167
updateChildContext(curNodes);
168-
log.info("ModelId: {} registered!", id);
168+
log.debug("ModelId: {} registered!", id);
169169
}
170170

171171
@Override
@@ -389,7 +389,7 @@ void processApplicationContext(YangSchemaNode appNode, String name) {
389389
appNode);
390390
}
391391

392-
log.info("successfully registered this application {}", name);
392+
log.debug("successfully registered this application {}", name);
393393
}
394394

395395
/**

serializers/json/src/main/java/org/onosproject/yang/serializers/json/EncoderUtils.java

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,13 @@
2323
import org.onosproject.yang.runtime.YangSerializerContext;
2424

2525
import java.util.ArrayList;
26-
import java.util.Collection;
26+
import java.util.Comparator;
2727
import java.util.HashMap;
2828
import java.util.Iterator;
2929
import java.util.List;
3030
import java.util.Map;
31+
import java.util.Map.Entry;
32+
import java.util.stream.Collectors;
3133

3234
import static com.google.common.base.Preconditions.checkNotNull;
3335
import static org.onosproject.yang.model.DataNode.Type.MULTI_INSTANCE_LEAF_VALUE_NODE;
@@ -67,9 +69,8 @@ public static ObjectNode convertDataNodeToJson(DataNode dataNode, YangSerializer
6769
FIRST_INSTANCE : NOT_MULTI_INSTANCE_NODE;
6870
walkDataNodeTree(treeNodeListener, dataNode, siblingType);
6971

70-
jsonBuilder.finalizeJson((dataNode.type() == MULTI_INSTANCE_NODE) ? true : false);
71-
ObjectNode resultData = jsonBuilder.getTreeNode();
72-
return resultData;
72+
jsonBuilder.finalizeJson(dataNode.type() == MULTI_INSTANCE_NODE);
73+
return jsonBuilder.getTreeNode();
7374
}
7475

7576
private static void walkDataNodeTree(DataNodeVisitor dataNodeVisitor,
@@ -109,18 +110,16 @@ private static void walkChildNodeList(DataNodeVisitor dataNodeVisitor,
109110
*/
110111

111112
DataNodeSiblingPositionType prevChildType = UNKNOWN_TYPE;
112-
DataNodeSiblingPositionType currChildType = UNKNOWN_TYPE;
113+
DataNodeSiblingPositionType currChildType;
113114

114115
/*
115-
* Dynamic Config preserves the order of child nodes.
116-
* So, we no longer need to sort the children.
116+
* Dynamic Config does not preserve the order of child nodes.
117+
* For cases where an array of objects gets fragmented by an internal key
117118
*/
118-
//List<DataNode> sortedChildList = sortChildrenList(childrenList);
119-
//checkNotNull(sortedChildList, "sorted children list cannot be null");
120-
Collection<DataNode> dataNodeList = childrenList.values();
121-
Iterator<DataNode> it = dataNodeList.iterator();
119+
List<DataNode> sortedChildList = sortChildrenList(childrenList);
120+
Iterator<DataNode> it = sortedChildList.iterator();
122121
DataNode currChild = it.next();
123-
DataNode nextChild = null;
122+
DataNode nextChild;
124123
boolean lastChildNotProcessed = true;
125124
while (lastChildNotProcessed) {
126125
/*
@@ -197,35 +196,23 @@ private static DataNodeSiblingPositionType getCurrentChildSiblingType(DataNode c
197196
return curChildSiblingType;
198197
}
199198

200-
private static List<DataNode> sortChildrenList(
201-
Map<NodeKey, DataNode> childrenList) {
202-
if (childrenList == null || childrenList.isEmpty()) {
203-
// the children list is either not yet created or empty.
204-
return null;
205-
}
199+
private static List<DataNode> sortChildrenList(Map<NodeKey, DataNode> childrenList) {
206200

207201
List<DataNode> sortedList = new ArrayList<>();
208202
Map<String, List<DataNode>> groupedBucket = new HashMap<>();
209203

210-
Iterator it = childrenList.entrySet().iterator();
211-
212-
while (it.hasNext()) {
213-
DataNode dataNode = ((Map.Entry<NodeKey, DataNode>) it.next()).getValue();
204+
//Sort by name
205+
for (Entry<NodeKey, DataNode> nodeKeyDataNodeEntry : childrenList.entrySet()) {
206+
DataNode dataNode = nodeKeyDataNodeEntry.getValue();
214207
String nodeName = dataNode.key().schemaId().name();
215-
List<DataNode> group = groupedBucket.get(nodeName);
216-
if (group == null) {
217-
group = new ArrayList<>();
218-
groupedBucket.put(nodeName, group);
219-
}
220-
208+
List<DataNode> group = groupedBucket.computeIfAbsent(nodeName, k -> new ArrayList<>());
221209
group.add(dataNode);
222-
223210
}
224-
225-
for (Map.Entry<String, List<DataNode>> entry : groupedBucket.entrySet()) {
211+
for (Entry<String, List<DataNode>> entry : groupedBucket.entrySet()) {
226212
sortedList.addAll(entry.getValue());
227213
}
228214

229-
return sortedList;
215+
return sortedList.stream().sorted(Comparator.comparing(object -> object.key().schemaId().name()))
216+
.collect(Collectors.toList());
230217
}
231218
}

serializers/json/src/test/java/org/onosproject/yang/serializers/json/JsonSerializerTest.java

Lines changed: 24 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import static org.hamcrest.MatcherAssert.assertThat;
5454
import static org.hamcrest.core.Is.is;
5555
import static org.junit.Assert.assertEquals;
56+
import static org.junit.Assert.assertNotNull;
5657
import static org.onosproject.yang.serializers.utils.SerializersUtil.convertRidToUri;
5758
import static org.slf4j.LoggerFactory.getLogger;
5859

@@ -66,21 +67,20 @@ public class JsonSerializerTest {
6667
private static YangSerializerContext context;
6768
private static YangSerializer jsonSerializer;
6869

69-
private static String outputIdTestJson = "{\"identity-test:con1\":{" +
70-
"\"interface\":\"identity-types:physical\",\"interfaces\":{" +
71-
"\"int-list\":[{\"iden\":\"identity-types-second:virtual\"," +
72-
"\"available\":{\"ll\":[\"identity-types:Loopback\"," +
73-
"\"identity-test:Giga\",\"identity-types-second:Ethernet\"]}}," +
74-
"{\"iden\":\"optical\",\"available\":{\"ll\":[\"Giga\"]}}]}}}";
75-
76-
private static String outputIdTestJson1 = "{\"jsonlist:c2\":{\"leaflist1" +
77-
"\":[\"a\",\"b\",\"c\"],\"leaf1\":1,\"leaf2\":2,\"leaf3\":3," +
78-
"\"leaf4\":4,\"leaf5\":5,\"leaf6\":6,\"leaf7\":\"7\",\"leaf8\"" +
79-
":\"8\",\"leaf9\":true,\"leaf10\":\"-922337203685477580.8\"" +
80-
",\"ll1\":[1,10],\"ll2\":[2,20],\"ll3\":[3,30],\"ll4\":[4,40],\"" +
81-
"ll5\":[5,50],\"ll6\":[6,60],\"ll7\":[\"7\",\"70\"],\"ll8\":[\"" +
82-
"8\",\"80\"],\"ll9\":[true,false],\"ll10\":[" +
83-
"\"-922337203685477580.8\",\"-922337203685477480.8\"]}}";
70+
private static String outputIdTestJson = "{\"identity-test:con1\":" +
71+
"{\"interface\":\"identity-types:physical\",\"interfaces\":" +
72+
"{\"int-list\":[{\"available\":{\"ll\":[\"identity-types:Loopback\"," +
73+
"\"identity-test:Giga\",\"identity-types-second:Ethernet\"]}," +
74+
"\"iden\":\"identity-types-second:virtual\"},{\"available\":{" +
75+
"\"ll\":[\"Giga\"]},\"iden\":\"optical\"}]}}}";
76+
77+
private static String outputIdTestJson1 = "{\"jsonlist:c2\":{\"leaf1\":1,\"" +
78+
"leaf10\":\"-922337203685477580.8\",\"leaf2\":2,\"leaf3\":3," +
79+
"\"leaf4\":4,\"leaf5\":5,\"leaf6\":6,\"leaf7\":\"7\"," +
80+
"\"leaf8\":\"8\",\"leaf9\":true,\"leaflist1\":[\"a\",\"b\",\"c\"]," +
81+
"\"ll1\":[1,10],\"ll10\":[\"-922337203685477580.8\",\"-922337203685477480.8\"]," +
82+
"\"ll2\":[2,20],\"ll3\":[3,30],\"ll4\":[4,40],\"ll5\":[5,50],\"ll6\":[6,60]," +
83+
"\"ll7\":[\"7\",\"70\"],\"ll8\":[\"8\",\"80\"],\"ll9\":[true,false]}}";
8484
@Rule
8585
public ExpectedException thrown = ExpectedException.none();
8686

@@ -112,12 +112,8 @@ public void demo1Test() throws IOException {
112112
InputStream inputStreamOutput = compositeStreamOutPut.resourceData();
113113
ObjectNode rootNodeOutput;
114114
ObjectMapper mapper = new ObjectMapper();
115-
try {
116-
rootNodeOutput = (ObjectNode) mapper.readTree(inputStreamOutput);
117-
assertEquals(true, rootNodeOutput != null);
118-
} catch (IOException e) {
119-
throw e;
120-
}
115+
rootNodeOutput = (ObjectNode) mapper.readTree(inputStreamOutput);
116+
assertNotNull(rootNodeOutput);
121117
}
122118

123119
@Test
@@ -148,12 +144,8 @@ public void colonTest() throws IOException {
148144
InputStream inputStreamOutput = compositeStreamOutPut.resourceData();
149145
ObjectNode rootNodeOutput;
150146
ObjectMapper mapper = new ObjectMapper();
151-
try {
152-
rootNodeOutput = (ObjectNode) mapper.readTree(inputStreamOutput);
153-
assertEquals(true, rootNodeOutput != null);
154-
} catch (IOException e) {
155-
throw e;
156-
}
147+
rootNodeOutput = (ObjectNode) mapper.readTree(inputStreamOutput);
148+
assertNotNull(rootNodeOutput);
157149
}
158150

159151
@Test
@@ -184,12 +176,8 @@ public void jsonListTest() throws IOException {
184176
InputStream inputStreamOutput = compositeStreamOutPut.resourceData();
185177
ObjectNode rootNodeOutput;
186178
ObjectMapper mapper = new ObjectMapper();
187-
try {
188-
rootNodeOutput = (ObjectNode) mapper.readTree(inputStreamOutput);
189-
assertEquals(rootNodeOutput.toString(), outputIdTestJson1);
190-
} catch (IOException e) {
191-
throw e;
192-
}
179+
rootNodeOutput = (ObjectNode) mapper.readTree(inputStreamOutput);
180+
assertEquals(rootNodeOutput.toString(), outputIdTestJson1);
193181
}
194182

195183
@Test
@@ -220,16 +208,12 @@ public void identityValueNsTest() throws IOException {
220208
InputStream inputStreamOutput = compositeStreamOutPut.resourceData();
221209
ObjectNode rootNodeOutput;
222210
ObjectMapper mapper = new ObjectMapper();
223-
try {
224-
rootNodeOutput = (ObjectNode) mapper.readTree(inputStreamOutput);
225-
assertEquals(rootNodeOutput.toString(), outputIdTestJson);
226-
} catch (IOException e) {
227-
throw e;
228-
}
211+
rootNodeOutput = (ObjectNode) mapper.readTree(inputStreamOutput);
212+
assertEquals(rootNodeOutput.toString(), outputIdTestJson);
229213
}
230214

231215
@Test
232-
public void identityValueNsErrorTest() throws IOException {
216+
public void identityValueNsErrorTest() {
233217
thrown.expect(IllegalArgumentException.class);
234218
thrown.expectMessage("Invalid input for value namespace");
235219
String path = "src/test/resources/id-test2.json";

0 commit comments

Comments
 (0)