05e6d2f13c14fb73c711d13652899a84a33b0b9b
[ccsdk/features.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : ccsdk features
4  * ================================================================================
5  * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property.
6  * All rights reserved.
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
11  *
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  *
21  */
22 package org.onap.ccsdk.features.sdnr.wt.dataprovider.http;
23
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Map.Entry;
29 import org.json.JSONObject;
30
31 public class DataTreeChildObject {
32
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;
39
40     public boolean isMatch() {
41         return this.isMatch;
42     }
43
44     public DataTreeChildObject(String label, boolean isMatch, Map<String, DataTreeChildObject> children,
45             String ownSeverity, String childrenSeveritySummary) {
46         this.label = label;
47         this.isMatch = isMatch;
48         this.children = children;
49 //        this.ownSeverity = ownSeverity;
50 //        this.childrenSeveritySummary = childrenSeveritySummary;
51         this.properties = new HashMap<>();
52     }
53
54     public DataTreeChildObject setProperty(String key, Object value) {
55         this.properties.put(key, value);
56         return this;
57     }
58
59     public Object getProperty(String key, Object defaultValue) {
60         return this.properties.getOrDefault(key, defaultValue);
61     }
62
63     /**
64      * @param string
65      * @param b
66      */
67     public DataTreeChildObject(String label, boolean isMatch) {
68         this(label, isMatch, new HashMap<>(), null, null);
69     }
70
71     /**
72      * @param treeLevel
73      * @param id
74      * @param data
75      * @param childKey
76      * @param parentKey
77      * @return
78      */
79     public boolean putChild(long treeLevel, String id, DataTreeChildObject data, String parentKey, String childKey) {
80         Object itemValue;
81         Object itemValueToMatch = data.getProperty(parentKey, null);
82         if (itemValueToMatch == null) {
83             return false;
84         }
85         if (treeLevel > 0) {
86             if (this.children != null) {
87                 for (DataTreeChildObject child : this.children.values()) {
88                     if (child.putChild(treeLevel - 1, id, data, parentKey, childKey)) {
89                         return true;
90                     }
91                 }
92             }
93         } else {
94             itemValue = this.getProperty(childKey, null);
95             if (itemValue != null && itemValue.equals(itemValueToMatch)) {
96                 this.children.put(id, data);
97                 return true;
98             }
99         }
100         return false;
101     }
102
103     /**
104      * @param treeLevel
105      * @param id
106      * @param data
107      * @param parentKey
108      * @param childKey
109      * @return
110      */
111     public boolean putChildIfNotExists(long treeLevel, String id, DataTreeChildObject data, String parentKey,
112             String childKey) {
113         Object itemValue;
114         Object itemValueToMatch = data.getProperty(parentKey, null);
115         if (itemValueToMatch == null) {
116             return false;
117         }
118         if (treeLevel > 0) {
119             if (this.children != null) {
120                 for (DataTreeChildObject child : this.children.values()) {
121                     if (child.putChildIfNotExists(treeLevel - 1, id, data, parentKey, childKey)) {
122                         return true;
123                     }
124                 }
125             }
126         } else {
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);
131                 }
132             }
133         }
134         return false;
135     }
136
137     /**
138      * @return
139      */
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());
148             }
149         }
150         o.put("children", c);
151         //o.put("ownSeverity", null);
152         //o.put("childrenSeveritySummary", null);
153         return o;
154     }
155
156     public boolean hasChildMatching() {
157         boolean match = false;
158         for (DataTreeChildObject child : this.children.values()) {
159             match = match || child.hasChildMatching() || this.isMatch;
160             if (match) {
161                 break;
162             }
163         }
164         return match;
165     }
166
167     /**
168      *
169      */
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());
175             } else {
176                 entry.getValue().removeUnmatchedPaths();
177             }
178         }
179         for (String key : toRemove) {
180             this.children.remove(key);
181         }
182     }
183
184     public boolean hasChildren() {
185         return this.children!=null && !this.children.isEmpty();
186     }
187 }