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