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;
30 import org.json.JSONObject;
32 public class DataTreeChildObject {
34 private final String label;
35 private final String ownSeverity;
36 private final String childrenSeveritySummary;
37 private final boolean isMatch;
38 private final Map<String, DataTreeChildObject> children;
39 private final Map<String, Object> properties;
41 public boolean isMatch() {
45 public DataTreeChildObject(String label, boolean isMatch, Map<String, DataTreeChildObject> children,
46 String ownSeverity, String childrenSeveritySummary) {
48 this.isMatch = isMatch;
49 this.children = children;
50 this.ownSeverity = ownSeverity;
51 this.childrenSeveritySummary = childrenSeveritySummary;
52 this.properties = new HashMap<>();
55 public DataTreeChildObject setProperty(String key, Object value) {
56 this.properties.put(key, value);
60 public Object getProperty(String key, Object defaultValue) {
61 return this.properties.getOrDefault(key, defaultValue);
68 public DataTreeChildObject(String label, boolean isMatch) {
69 this(label, isMatch, new HashMap<>(), null, null);
80 public boolean putChild(long treeLevel, String id, DataTreeChildObject data, String parentKey, String childKey) {
82 Object itemValueToMatch = data.getProperty(parentKey, null);
83 if (itemValueToMatch == null) {
87 if (this.children != null) {
88 for (DataTreeChildObject child : this.children.values()) {
89 if (child.putChild(treeLevel - 1, id, data, parentKey, childKey)) {
95 // if(this.children!=null) {
96 // for(DataTreeChildObject child:this.children.values()) {
97 // itemValue=(String) child.getProperty(childKey, null);
98 // if(itemValue!=null && itemValue.equals(itemValueToMatch)) {
99 // child.children.put(id, data);
104 itemValue = this.getProperty(childKey, null);
105 if (itemValue != null && itemValue.equals(itemValueToMatch)) {
106 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);