e34988b4b0c88cc85792a6c408cf2e315491f508
[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
30 import org.json.JSONObject;
31
32 public class DataTreeChildObject {
33
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;
40
41     public boolean isMatch() {
42         return this.isMatch;
43     }
44
45     public DataTreeChildObject(String label, boolean isMatch, Map<String, DataTreeChildObject> children,
46             String ownSeverity, String childrenSeveritySummary) {
47         this.label = label;
48         this.isMatch = isMatch;
49         this.children = children;
50         this.ownSeverity = ownSeverity;
51         this.childrenSeveritySummary = childrenSeveritySummary;
52         this.properties = new HashMap<>();
53     }
54
55     public DataTreeChildObject setProperty(String key, Object value) {
56         this.properties.put(key, value);
57         return this;
58     }
59
60     public Object getProperty(String key, Object defaultValue) {
61         return this.properties.getOrDefault(key, defaultValue);
62     }
63
64     /**
65      * @param string
66      * @param b
67      */
68     public DataTreeChildObject(String label, boolean isMatch) {
69         this(label, isMatch, new HashMap<>(), null, null);
70     }
71
72     /**
73      * @param treeLevel
74      * @param id
75      * @param data
76      * @param childKey
77      * @param parentKey
78      * @return
79      */
80     public boolean putChild(long treeLevel, String id, DataTreeChildObject data, String parentKey, String childKey) {
81         Object itemValue;
82         Object itemValueToMatch = data.getProperty(parentKey, null);
83         if (itemValueToMatch == null) {
84             return false;
85         }
86         if (treeLevel > 0) {
87             if (this.children != null) {
88                 for (DataTreeChildObject child : this.children.values()) {
89                     if (child.putChild(treeLevel - 1, id, data, parentKey, childKey)) {
90                         return true;
91                     }
92                 }
93             }
94         } else {
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);
100             //                                          return true;
101             //                                  }
102             //                          }
103             //                  }
104             itemValue = this.getProperty(childKey, null);
105             if (itemValue != null && itemValue.equals(itemValueToMatch)) {
106                 this.children.put(id, data);
107             }
108         }
109         return false;
110     }
111
112     /**
113      * @param treeLevel
114      * @param id
115      * @param data
116      * @param parentKey
117      * @param childKey
118      * @return
119      */
120     public boolean putChildIfNotExists(long treeLevel, String id, DataTreeChildObject data, String parentKey,
121             String childKey) {
122         Object itemValue;
123         Object itemValueToMatch = data.getProperty(parentKey, null);
124         if (itemValueToMatch == null) {
125             return false;
126         }
127         if (treeLevel > 0) {
128             if (this.children != null) {
129                 for (DataTreeChildObject child : this.children.values()) {
130                     if (child.putChildIfNotExists(treeLevel - 1, id, data, parentKey, childKey)) {
131                         return true;
132                     }
133                 }
134             }
135         } else {
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);
140                 }
141             }
142         }
143         return false;
144     }
145
146     /**
147      * @return
148      */
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());
157             }
158         }
159         o.put("children", c);
160         //              o.put("ownSeverity", null);
161         //              o.put("childrenSeveritySummary", null);
162         return o;
163     }
164
165     public boolean hasChildMatching() {
166         boolean match = false;
167         for (DataTreeChildObject child : this.children.values()) {
168             match = match || child.hasChildMatching() || this.isMatch;
169             if (match) {
170                 break;
171             }
172         }
173         return match;
174     }
175
176     /**
177      *
178      */
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());
184             } else {
185                 entry.getValue().removeUnmatchedPaths();
186             }
187         }
188         for (String key : toRemove) {
189             this.children.remove(key);
190         }
191     }
192 }