76cf227bd14668fd1f044022de9c7c406c806fb8
[aai/search-data-service.git] / src / main / java / org / onap / aai / sa / searchdbabstraction / util / AggregationParsingUtil.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 Amdocs
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 package org.onap.aai.sa.searchdbabstraction.util;
22
23 import com.fasterxml.jackson.core.JsonProcessingException;
24 import org.json.simple.JSONArray;
25 import org.json.simple.JSONObject;
26 import org.onap.aai.sa.searchdbabstraction.entity.AggregationBucket;
27 import org.onap.aai.sa.searchdbabstraction.entity.AggregationResult;
28
29 import java.util.Iterator;
30 import java.util.Set;
31
32 public class AggregationParsingUtil {
33   public static AggregationResult[] parseAggregationResults(JSONObject aggregations)
34     throws JsonProcessingException {
35
36     // Obtain the set of aggregation names
37     Set keySet = aggregations.keySet();
38     AggregationResult[] aggResults = new AggregationResult[keySet.size()];
39
40     int index = 0;
41     for (Iterator it = keySet.iterator(); it.hasNext(); ) {
42       String key = (String) it.next();
43       AggregationResult aggResult = new AggregationResult();
44       aggResult.setName(key);
45
46       JSONObject bucketsOrNested = (JSONObject) aggregations.get(key);
47       Object buckets = bucketsOrNested.get("buckets");
48       if (buckets == null) {
49         // we have a nested
50         Number count = (Number) bucketsOrNested.remove("doc_count");
51         aggResult.setCount(count);
52         AggregationResult[] nestedResults = parseAggregationResults(bucketsOrNested);
53         aggResult.setNestedAggregations(nestedResults);
54       } else {
55         AggregationBucket[] aggBuckets = parseAggregationBuckets((JSONArray) buckets);
56         aggResult.setBuckets(aggBuckets);
57       }
58
59       aggResults[index] = aggResult;
60       index++;
61     }
62
63     return aggResults;
64
65   }
66
67   private static AggregationBucket[] parseAggregationBuckets(JSONArray buckets)
68     throws JsonProcessingException {
69     AggregationBucket[] aggBuckets = new AggregationBucket[buckets.size()];
70     for (int i = 0; i < buckets.size(); i++) {
71       AggregationBucket aggBucket = new AggregationBucket();
72       JSONObject bucketContent = (JSONObject) buckets.get(i);
73       Object key = bucketContent.remove("key");
74       aggBucket.setKey(key);
75       Object formatted = bucketContent.remove("key_as_string");
76       if (formatted != null) {
77         aggBucket.setFormattedKey((String) formatted);
78       }
79       Object count = bucketContent.remove("doc_count");
80       if (count != null) {
81         aggBucket.setCount((Number) count);
82       }
83       bucketContent.remove("from");
84       bucketContent.remove("from_as_string");
85       bucketContent.remove("to");
86       bucketContent.remove("to_as_string");
87
88
89       if (!bucketContent.entrySet().isEmpty()) {
90         // we have results from sub-aggregation
91         AggregationResult[] subResult = parseAggregationResults(bucketContent);
92         if (subResult != null) {
93           aggBucket.setSubAggregationResult(subResult);
94         }
95       }
96       aggBuckets[i] = aggBucket;
97     }
98
99     return aggBuckets;
100   }
101
102 }