blob: 23c107d77b6de239719d6e4739cf9c8826965aa8 [file] [log] [blame]
Sithara Punnassery4b091dc2017-03-02 17:22:40 -08001/*
2 * Copyright 2017-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 */
16package org.onosproject.config;
17
18import java.util.Iterator;
Sithara Punnassery44e2a702017-03-06 15:38:10 -080019
20import java.util.List;
21
22import org.onosproject.yang.model.KeyLeaf;
23import org.onosproject.yang.model.LeafListKey;
24import org.onosproject.yang.model.ListKey;
Sithara Punnassery4b091dc2017-03-02 17:22:40 -080025import org.onosproject.yang.model.NodeKey;
26import org.onosproject.yang.model.ResourceId;
27
28/**
Sithara Punnassery44e2a702017-03-06 15:38:10 -080029 * Utilities to work on the ResourceId.
Sithara Punnassery4b091dc2017-03-02 17:22:40 -080030 */
31
32public final class ResourceIdParser {
33
Sithara Punnassery44e2a702017-03-06 15:38:10 -080034 public static final String ROOT = "root";
35 public static final String NM_SEP = "#";
36 public static final String VAL_SEP = "@";
37 public static final String KEY_SEP = "$";
38 public static final String EL_SEP = ".";
39
40
41
Sithara Punnassery4b091dc2017-03-02 17:22:40 -080042 private ResourceIdParser() {
43
44 }
Sithara Punnassery44e2a702017-03-06 15:38:10 -080045
46 public static ResourceId getParent(ResourceId path) {
47 int last = path.nodeKeys().size();
48 path.nodeKeys().remove(last - 1);
49 return path;
50 }
51
52 public static NodeKey getInstanceKey(ResourceId path) {
53 int last = path.nodeKeys().size();
54 NodeKey ret = path.nodeKeys().get(last - 1);
55 if (ret instanceof NodeKey) {
56 return ret;
57 } else {
58 return null;
59 }
60 }
61
62 public static NodeKey getMultiInstanceKey(ResourceId path) {
63 int last = path.nodeKeys().size();
64 NodeKey ret = path.nodeKeys().get(last - 1);
65 if (ret instanceof ListKey) {
66 return ret;
67 } else {
68 return null;
69 }
70 }
71
72 public static String appendMultiInstKey(String path, String leaf) {
73 return (path + leaf.substring(leaf.indexOf(KEY_SEP)));
74 }
75
76 public static String appendKeyLeaf(String path, String key) {
77 return (path + EL_SEP + key);
78 }
79
80 //DONE
81 public static String appendKeyLeaf(String path, KeyLeaf key) {
Sithara Punnassery4b091dc2017-03-02 17:22:40 -080082 StringBuilder bldr = new StringBuilder();
Sithara Punnassery44e2a702017-03-06 15:38:10 -080083 bldr.append(key.leafSchema().name());
84 bldr.append(NM_SEP);
85 bldr.append(key.leafSchema().namespace());
86 bldr.append(NM_SEP);
87 bldr.append(key.leafValue().toString());
88 return (path + EL_SEP + bldr.toString());
89 }
90
91 public static String appendNodeKey(String path, NodeKey key) {
92 return (path + EL_SEP + key.schemaId().name() + NM_SEP + key.schemaId().namespace());
93 }
94
95 public static String appendNodeKey(String path, String name, String nmSpc) {
96 return (path + EL_SEP + name + NM_SEP + nmSpc);
97 }
98
99 public static String appendLeafList(String path, LeafListKey key) {
100 return (path + NM_SEP + key.asString());
101 }
102
103 public static String appendLeafList(String path, String val) {
104 return (path + NM_SEP + val);
105 }
106
107 public static String appendKeyList(String path, ListKey key) {
108 StringBuilder bldr = new StringBuilder();
109 for (KeyLeaf keyLeaf : key.keyLeafs()) {
110 bldr.append(KEY_SEP);
111 bldr.append(keyLeaf.leafSchema().name());
112 bldr.append(NM_SEP);
113 bldr.append(keyLeaf.leafSchema().namespace());
114 bldr.append(NM_SEP);
115 bldr.append(keyLeaf.leafValue().toString());
116 }
117 return (path + bldr.toString());
118 }
119
120 public static String parseNodeKey(NodeKey key) {
121 if (key == null) {
122 return null;
123 }
124 StringBuilder bldr = new StringBuilder();
125 if (key instanceof LeafListKey) {
126 parseLeafList((LeafListKey) key, bldr);
127 } else if (key instanceof ListKey) {
128 parseKeyList((ListKey) key, bldr);
129 } else {
130 parseNodeKey(key, bldr);
131 }
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800132 return bldr.toString();
133 }
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800134
135 public static String parseResId(ResourceId path) {
136 StringBuilder bldr = new StringBuilder();
137 bldr.append(ROOT);
138 if (path == null) {
139 return bldr.toString();
140 }
141 List<NodeKey> nodeKeyList = path.nodeKeys();
142 for (NodeKey key : nodeKeyList) {
143 bldr.append(EL_SEP);
144 if (key instanceof LeafListKey) {
145 parseLeafList((LeafListKey) key, bldr);
146 } else if (key instanceof ListKey) {
147 parseKeyList((ListKey) key, bldr);
148 } else {
149 parseNodeKey(key, bldr);
150 }
151 }
152 return bldr.toString();
153 }
154
155 private static void parseLeafList(LeafListKey key, StringBuilder bldr) {
156 bldr.append(key.schemaId().name());
157 bldr.append(NM_SEP);
158 bldr.append(key.schemaId().namespace());
159 bldr.append(NM_SEP);
160 bldr.append(key.asString());
161 }
162
163 private static void parseKeyList(ListKey key, StringBuilder bldr) {
164 bldr.append(key.schemaId().name());
165 bldr.append(NM_SEP);
166 bldr.append(key.schemaId().namespace());
167 bldr.append(NM_SEP);
168 Iterator<KeyLeaf> iter = key.keyLeafs().iterator();
169 KeyLeaf next;
170 while (iter.hasNext()) {
171 next = iter.next();
172 bldr.append(KEY_SEP);
173 bldr.append(next.leafSchema().name());
174 bldr.append(NM_SEP);
175 bldr.append(next.leafSchema().namespace());
176 bldr.append(NM_SEP);
177 bldr.append(next.leafValue().toString());
178 }
179 }
180
181 private static void parseNodeKey(NodeKey key, StringBuilder bldr) {
182 bldr.append(key.schemaId().name());
183 bldr.append(NM_SEP);
184 bldr.append(key.schemaId().namespace());
185 }
186
187 public static ResourceId getResId(List<String> dpath) {
188 ResourceId.Builder resBldr = new ResourceId.Builder();
189 Iterator<String> itr = dpath.iterator();
190 itr.next();
191 while (itr.hasNext()) {
192 String name = itr.next();
193 if (name.contains(VAL_SEP)) {
194 resBldr.addLeafListBranchPoint(name.substring(0, name.indexOf(NM_SEP)),
195 name.substring(name.indexOf(NM_SEP) + 1, name.indexOf(VAL_SEP)),
196 name.substring(name.indexOf(VAL_SEP) + 1));
197 } else if (name.contains(KEY_SEP)) {
198 resBldr.addBranchPointSchema(name.substring(0, name.indexOf(NM_SEP)),
199 name.substring(name.indexOf(NM_SEP) + 1, name.indexOf(KEY_SEP)));
200 String[] keys = name.split(KEY_SEP);
201 for (int i = 1; i < keys.length; i++) {
202 String key = keys[i];
203 resBldr.addKeyLeaf(key.substring(0, key.indexOf(NM_SEP)),
204 key.substring(key.indexOf(NM_SEP) + 1, key.lastIndexOf(NM_SEP)),
205 key.substring(name.lastIndexOf(NM_SEP) + 1));
206 }
207 } else {
208 resBldr.addBranchPointSchema(name.substring(0, name.indexOf(NM_SEP)),
209 name.substring(name.indexOf(NM_SEP) + 1));
210 }
211 }
212 return resBldr.build();
213 }
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800214}