1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
|
/*-
* ============LICENSE_START=======================================================
* ONAP : APPC
* ================================================================================
* Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Copyright (C) 2017 Amdocs
* =============================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* ECOMP is a trademark and service mark of AT&T Intellectual Property.
* ============LICENSE_END=========================================================
*/
package org.onap.appc.util;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* This class is used to assemble properties that are defined using a structured name into groups, and allow them to be
* processed as sets of definitions.
* <p>
* For example, a structured name uses a dotted-notation, like "provider.name". Further, the nodes of the structured
* name may be serialized using a suffix ordinal number (e.g., "provider1.name"). These structured properties form a
* hierarchical name space where the names are grouped together and can be retrieved as a set.
* </p>
*
*/
public class StructuredPropertyHelper {
/**
* This method scans the properties object for all properties that match the root name and constructs a list of
* structured property node graphs that represents the namespaces of the properties.
* <p>
* For example, assume that there are structured properties of the form "provider1.name", "provider2.name",
* "provider3.name", and so forth. There may also be other subordinate properties as well (e.g., "provider1.type").
* This method would construct a list of graphs of nodes, where each node represents one value of the structured
* name. The roots would be the values "provider1", "provider2", "provider3", and so forth. The values of the
* subordinate nodes would be the second, third, and so forth name nodes of the compound name. The value of the
* property is associated with nodes that are representative of the leaf of the name space.
* </p>
*
* @param properties
* The properties to be processed
* @param prefix
* The prefix of the root structured property name
* @return The node graph of the properties
*/
public static List<Node> getStructuredProperties(Properties properties, String prefix) {
List<Node> roots = new ArrayList<>();
for (String name : properties.stringPropertyNames()) {
if (name.startsWith(prefix)) {
String value = properties.getProperty(name);
processNamespace(roots, name, value);
}
}
return roots;
}
/**
* This method recursively walks the name space of the structured property and constructs the node graph to
* represent the property
*
* @param nodes
* The collection of nodes for the current level of the name space
* @param propertyName
* The name of the node
* @param value
* The value, if any
* @return The node for this level in the namespace
*/
@SuppressWarnings("nls")
private static Node processNamespace(List<Node> nodes, String propertyName, String value) {
String[] tokens = propertyName.split("\\.", 2);
String nodeName = normalizeNodeName(tokens[0]);
Node namespaceNode = null;
for (Node node : nodes) {
if (node.getName().equals(nodeName)) {
namespaceNode = node;
break;
}
}
if (namespaceNode == null) {
namespaceNode = new Node();
namespaceNode.setName(nodeName);
nodes.add(namespaceNode);
}
if (tokens.length == 1 || tokens[1] == null || tokens[1].length() == 0) {
namespaceNode.setValue(value);
} else {
processNamespace(namespaceNode.getChildren(), tokens[1], value);
}
return namespaceNode;
}
/**
* This method normalizes a node name of the structured property name by removing leading and trailing whitespace,
* and by converting any ordinal position to a simple expression without leading zeroes.
*
* @param token
* The token to be normalized
* @return The normalized name, or null if the token was null;
*/
@SuppressWarnings("nls")
private static String normalizeNodeName(String token) {
if (token == null) {
return null;
}
StringBuffer buffer = new StringBuffer(token.trim());
Pattern pattern = Pattern.compile("([^0-9]+)([0-9]*)");
Matcher matcher = pattern.matcher(buffer);
if (matcher.matches()) {
String nameRoot = matcher.group(1);
String ordinal = matcher.group(2);
if (ordinal != null && ordinal.length() > 0) {
int i = Integer.parseInt(ordinal);
buffer.setLength(0);
buffer.append(nameRoot);
buffer.append(Integer.toString(i));
}
}
return buffer.toString();
}
/**
* This class represents a node in the structured property name space
*
*/
public static class Node implements Comparable<Node> {
/**
* The name of the structured property node
*/
private String name;
/**
* If the node is a leaf, then the value of the property
*/
private String value;
/**
* If the node is not a leaf, then the sub-nodes of the property
*/
private List<Node> children;
/**
* @return the value of name
*/
public String getName() {
return name;
}
/**
* @param name
* the value for name
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the value of value
*/
public String getValue() {
return value;
}
/**
* @param value
* the value for value
*/
public void setValue(String value) {
this.value = value;
}
/**
* @return the value of children
*/
public List<Node> getChildren() {
if (children == null) {
children = new ArrayList<>();
}
return children;
}
/**
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
return name.hashCode() + (value != null ? value.hashCode() : children.hashCode());
}
/**
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (obj == null)
return false;
if (this.getClass() != obj.getClass())
return false;
Node other = (Node) obj;
boolean result = name.equals(other.name);
if (value == null) {
result &= other.value == null;
} else {
result &= value.equals(other.value);
}
if (children == null) {
result &= other.children == null;
} else {
result &= children.equals(other.children);
}
return result;
}
/**
* @see java.lang.Object#toString()
*/
@SuppressWarnings("nls")
@Override
public String toString() {
if (value != null) {
return String.format("%s = %s", name, value);
}
return String.format("%s.%s", name, children.toString());
}
@Override
public int compareTo(StructuredPropertyHelper.Node o) {
return name.compareTo(o.name);
}
}
}
|