91d469700a17d8e5939004421c2091f7457a9330
[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             //                  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);
99             //                                          return true;
100             //                                  }
101             //                          }
102             //                  }
103             itemValue = this.getProperty(childKey, null);
104             if (itemValue != null && itemValue.equals(itemValueToMatch)) {
105                 this.children.put(id, data);
106                 return true;
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
193     public boolean hasChildren() {
194         return this.children!=null && !this.children.isEmpty();
195     }
196 }