2 * ============LICENSE_START=======================================================
3 * ONAP : ccsdk features
4 * ================================================================================
5 * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.ccsdk.features.sdnr.wt.dataprovider.http;
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.List;
28 import java.util.Map.Entry;
29 import org.json.JSONObject;
31 public class DataTreeChildObject {
33 private final String label;
34 // private final String ownSeverity;
35 // private final String childrenSeveritySummary;
36 private final boolean isMatch;
37 private final Map<String, DataTreeChildObject> children;
38 private final Map<String, Object> properties;
40 public boolean isMatch() {
44 public DataTreeChildObject(String label, boolean isMatch, Map<String, DataTreeChildObject> children,
45 String ownSeverity, String childrenSeveritySummary) {
47 this.isMatch = isMatch;
48 this.children = children;
49 // this.ownSeverity = ownSeverity;
50 // this.childrenSeveritySummary = childrenSeveritySummary;
51 this.properties = new HashMap<>();
54 public DataTreeChildObject setProperty(String key, Object value) {
55 this.properties.put(key, value);
59 public Object getProperty(String key, Object defaultValue) {
60 return this.properties.getOrDefault(key, defaultValue);
67 public DataTreeChildObject(String label, boolean isMatch) {
68 this(label, isMatch, new HashMap<>(), null, null);
79 public boolean putChild(long treeLevel, String id, DataTreeChildObject data, String parentKey, String childKey) {
81 Object itemValueToMatch = data.getProperty(parentKey, null);
82 if (itemValueToMatch == null) {
86 if (this.children != null) {
87 for (DataTreeChildObject child : this.children.values()) {
88 if (child.putChild(treeLevel - 1, id, data, parentKey, childKey)) {
94 // if(this.children!=null) {
95 // for(DataTreeChildObject child:this.children.values()) {
96 // itemValue=(String) child.getProperty(childKey, null);
97 // if(itemValue!=null && itemValue.equals(itemValueToMatch)) {
98 // child.children.put(id, data);
103 itemValue = this.getProperty(childKey, null);
104 if (itemValue != null && itemValue.equals(itemValueToMatch)) {
105 this.children.put(id, data);
120 public boolean putChildIfNotExists(long treeLevel, String id, DataTreeChildObject data, String parentKey,
123 Object itemValueToMatch = data.getProperty(parentKey, null);
124 if (itemValueToMatch == null) {
128 if (this.children != null) {
129 for (DataTreeChildObject child : this.children.values()) {
130 if (child.putChildIfNotExists(treeLevel - 1, id, data, parentKey, childKey)) {
136 itemValue = this.getProperty(childKey, null);
137 if (itemValue != null && itemValue.equals(itemValueToMatch)) {
138 if (!this.children.containsKey(id)) {
139 this.children.put(id, data);
149 public JSONObject toJSONObject() {
150 JSONObject o = new JSONObject();
151 o.put("label", this.label);
152 o.put("isMatch", this.isMatch);
153 JSONObject c = new JSONObject();
154 if (this.children != null) {
155 for (Entry<String, DataTreeChildObject> entry : this.children.entrySet()) {
156 c.put(entry.getKey(), entry.getValue().toJSONObject());
159 o.put("children", c);
160 // o.put("ownSeverity", null);
161 // o.put("childrenSeveritySummary", null);
165 public boolean hasChildMatching() {
166 boolean match = false;
167 for (DataTreeChildObject child : this.children.values()) {
168 match = match || child.hasChildMatching() || this.isMatch;
179 public void removeUnmatchedPaths() {
180 List<String> toRemove = new ArrayList<>();
181 for (Entry<String, DataTreeChildObject> entry : this.children.entrySet()) {
182 if (!(entry.getValue().hasChildMatching() || entry.getValue().isMatch)) {
183 toRemove.add(entry.getKey());
185 entry.getValue().removeUnmatchedPaths();
188 for (String key : toRemove) {
189 this.children.remove(key);
193 public boolean hasChildren() {
194 return this.children!=null && !this.children.isEmpty();