f12ff3debde34d04edf944bcd1f91a67b5c6164e
[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 org.json.JSONObject;
28
29 public class DataTreeObject extends HashMap<String, DataTreeChildObject> {
30
31     private static final long serialVersionUID = 1L;
32     private final String parentKey;
33     private final String childKey;
34
35     public DataTreeObject createTreeByPath(String[] pathFilter) {
36
37         if (pathFilter != null && pathFilter.length > 0) {
38             for (String key : this.keySet()) {
39                 if (key.equals(pathFilter[0])) {
40                     DataTreeChildObject o = this.getChildElemByPath(this.get(key), slice(pathFilter, 1));
41                     DataTreeObject r = new DataTreeObject(this.parentKey, this.childKey);
42                     r.put(key, o);
43                     return r;
44                 }
45             }
46         }
47
48         return null;
49
50     }
51
52     private DataTreeChildObject getChildElemByPath(DataTreeChildObject source, String[] pathFilter) {
53
54         if (pathFilter != null && pathFilter.length > 0) {
55             //                  for(String key:source..keySet()) {
56             //                          if(key.equals(pathFilter[0])){
57             //                                  DataTreeChildObject o= this.getChildElemByPath(this.get(key),slice(pathFilter,1));
58             //                          }
59             //                  }
60         }
61
62         return null;
63     }
64
65     public DataTreeObject(String parentKey, String childKey) {
66         this.parentKey = parentKey;
67         this.childKey = childKey;
68     }
69
70     /**
71      * @param treeLevel
72      * @param id
73      * @param data
74      */
75     public void put(long treeLevel, String id, DataTreeChildObject data) {
76         for (DataTreeChildObject entry : this.values()) {
77             if (entry.putChild(treeLevel, id, data, this.parentKey, this.childKey)) {
78                 break;
79             }
80         }
81
82     }
83
84     /**
85      *
86      */
87     public String toJSON() {
88         JSONObject o = new JSONObject();
89         for (Entry<String, DataTreeChildObject> entry : this.entrySet()) {
90             o.put(entry.getKey(), entry.getValue().toJSONObject());
91         }
92         return o.toString();
93     }
94
95     /**
96      *
97      */
98     public void removeUnmatchedPaths() {
99         List<String> toRemove = new ArrayList<>();
100          for (Entry<String,DataTreeChildObject> entry : this.entrySet()) {
101             entry.getValue().removeUnmatchedPaths();
102             if(!entry.getValue().isMatch() && !entry.getValue().hasChildren()) {
103                 toRemove.add(entry.getKey());
104             }
105         }
106         for(String toRemoveKey:toRemove) {
107             this.remove(toRemoveKey);
108         }
109
110     }
111
112     /**
113      * @param treeLevel
114      * @param id
115      * @param setProperty
116      */
117     public void putIfNotExists(long treeLevel, String id, DataTreeChildObject data) {
118         for (DataTreeChildObject entry : this.values()) {
119             if (entry.putChildIfNotExists(treeLevel, id, data, this.parentKey, this.childKey)) {
120                 break;
121             }
122         }
123     }
124
125     /**
126      * @param id
127      * @param data
128      */
129     public void putIfNotExists(String id, DataTreeChildObject data) {
130         if (!this.containsKey(id)) {
131             this.put(id, data);
132         }
133     }
134
135     /**
136      * @param source
137      * @param i
138      * @return
139      */
140     public static String[] slice(String[] source, int start) {
141         String[] r = new String[source.length - start];
142         for (int i = 0; i < r.length; i++) {
143             r[i] = source[i + start];
144         }
145         return r;
146
147     }
148 }