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