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             itemValue = this.getProperty(childKey, null);
 
  95             if (itemValue != null && itemValue.equals(itemValueToMatch)) {
 
  96                 this.children.put(id, data);
 
 111     public boolean putChildIfNotExists(long treeLevel, String id, DataTreeChildObject data, String parentKey,
 
 114         Object itemValueToMatch = data.getProperty(parentKey, null);
 
 115         if (itemValueToMatch == null) {
 
 119             if (this.children != null) {
 
 120                 for (DataTreeChildObject child : this.children.values()) {
 
 121                     if (child.putChildIfNotExists(treeLevel - 1, id, data, parentKey, childKey)) {
 
 127             itemValue = this.getProperty(childKey, null);
 
 128             if (itemValue != null && itemValue.equals(itemValueToMatch)) {
 
 129                 if (!this.children.containsKey(id)) {
 
 130                     this.children.put(id, data);
 
 140     public JSONObject toJSONObject() {
 
 141         JSONObject o = new JSONObject();
 
 142         o.put("label", this.label);
 
 143         o.put("isMatch", this.isMatch);
 
 144         JSONObject c = new JSONObject();
 
 145         if (this.children != null) {
 
 146             for (Entry<String, DataTreeChildObject> entry : this.children.entrySet()) {
 
 147                 c.put(entry.getKey(), entry.getValue().toJSONObject());
 
 150         o.put("children", c);
 
 151         //o.put("ownSeverity", null);
 
 152         //o.put("childrenSeveritySummary", null);
 
 156     public boolean hasChildMatching() {
 
 157         boolean match = false;
 
 158         for (DataTreeChildObject child : this.children.values()) {
 
 159             match = match || child.hasChildMatching() || this.isMatch;
 
 170     public void removeUnmatchedPaths() {
 
 171         List<String> toRemove = new ArrayList<>();
 
 172         for (Entry<String, DataTreeChildObject> entry : this.children.entrySet()) {
 
 173             if (!(entry.getValue().hasChildMatching() || entry.getValue().isMatch)) {
 
 174                 toRemove.add(entry.getKey());
 
 176                 entry.getValue().removeUnmatchedPaths();
 
 179         for (String key : toRemove) {
 
 180             this.children.remove(key);
 
 184     public boolean hasChildren() {
 
 185         return this.children!=null && !this.children.isEmpty();