2 * ============LICENSE_START=======================================================
3 * ONAP : ccsdk features
4 * ================================================================================
5 * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property.
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.ccsdk.features.sdnr.wt.dataprovider.http;
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.List;
27 import org.json.JSONObject;
29 public class DataTreeObject extends HashMap<String, DataTreeChildObject> {
31 private static final long serialVersionUID = 1L;
32 private final String parentKey;
33 private final String childKey;
35 public DataTreeObject createTreeByPath(String[] pathFilter) {
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);
52 private DataTreeChildObject getChildElemByPath(DataTreeChildObject source, String[] pathFilter) {
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));
65 public DataTreeObject(String parentKey, String childKey) {
66 this.parentKey = parentKey;
67 this.childKey = childKey;
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)) {
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());
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());
106 for(String toRemoveKey:toRemove) {
107 this.remove(toRemoveKey);
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)) {
129 public void putIfNotExists(String id, DataTreeChildObject data) {
130 if (!this.containsKey(id)) {
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];