blob: a24772b784daae01fc3cc252c1aca8cd12dc07e9 [file] [log] [blame]
sonu gupta1bb37b82016-11-11 16:51:18 +05301/*
2 * Copyright 2016-present Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onosproject.yms.app.ydt;
18
19import com.google.common.collect.ImmutableList;
20import org.onosproject.yangutils.datamodel.YangList;
21import org.onosproject.yangutils.datamodel.YangSchemaNodeIdentifier;
22import org.onosproject.yms.ydt.YdtContext;
23
24import java.util.ArrayList;
25import java.util.Iterator;
26import java.util.List;
27
28import static org.onosproject.yms.app.ydt.YdtConstants.errorMsg;
29import static org.onosproject.yms.ydt.YdtType.MULTI_INSTANCE_NODE;
30
31
32/**
33 * Represents a multi instance node in YANG data tree.
34 */
35public class YdtMultiInstanceNode extends YdtNode {
36
37 // ydt formatted error string
38 private static final String FMT_MISSING_KEY =
39 "%s is missing some of the keys of %s.";
40
41 /*
42 * Reference for list of key element's ydtContext.
43 */
44 private List<YdtContext> keyNodeList = new ArrayList<>();
45
46 /*
47 * Reference for composite key string for multi Instance Node..
48 */
49 private String compositeKey;
50
51 /**
52 * Creates a YANG multi instance node object.
53 *
54 * @param id node identifier of YDT multi instance node .
55 */
56 protected YdtMultiInstanceNode(YangSchemaNodeIdentifier id) {
57 super(MULTI_INSTANCE_NODE, id);
58 }
59
60 /**
61 * Returns the composite key string for current multi instance node.
62 *
63 * @return composite key string
64 */
65 public String getCompositeKey() {
66 return compositeKey;
67 }
68
69 /**
70 * Returns the list of key element's ydtContext.
71 *
72 * @return list of key element's ydtContext
73 */
74 public List<YdtContext> getKeyNodeList() {
75 return ImmutableList.copyOf(keyNodeList);
76 }
77
78 @Override
79 public void createKeyNodeList() {
80 YangList yangListHolder = (YangList) getYangSchemaNode();
81 List<String> schemaKeyList = yangListHolder.getKeyList();
82
83 /*
84 * If key element not defined in schema or config is false then
85 * return no need to do create key list.
86 */
87 if (schemaKeyList == null || !yangListHolder.isConfig()) {
88 return;
89 }
90
91 StringBuilder ksb = new StringBuilder();
92
93 // Iterator for schema key name list.
94 Iterator<String> sklItr = schemaKeyList.iterator();
95
96 List<YdtContext> nodeList = new ArrayList<>();
97
98 YangSchemaNodeIdentifier id = new YangSchemaNodeIdentifier();
99 id.setNameSpace(getYdtNodeIdentifier().getNameSpace());
100 // This loop should run while schema key list is not finished
101 while (sklItr.hasNext()) {
102 String name = sklItr.next();
103 id.setName(name);
104 List<YdtNode<YdtMultiInstanceNode>> collidingChild =
105 (List<YdtNode<YdtMultiInstanceNode>>) ydtNodeMap.get(id);
106
107 if (collidingChild == null) {
108 errorHandler(errorMsg(FMT_MISSING_KEY,
109 yangListHolder.getParent().getName(),
110 yangListHolder.getName()), this);
111 }
112
113 YdtNode<YdtMultiInstanceNode> ydtNode = collidingChild.get(0);
114 /*
115 * Preparing composite key string by concatenating values of
116 * all the key leaf.
117 */
118 ksb.append(ydtNode.getValue());
119 nodeList.add(ydtNode);
120 }
121 //Setting te key object in List.
122 keyNodeList = nodeList;
123 compositeKey = ksb.toString();
124 }
125}