Initial search service commit
[aai/search-data-service.git] / src / main / java / org / openecomp / sa / searchdbabstraction / util / AggregationParsingUtil.java
1 /**
2  * ============LICENSE_START=======================================================
3  * Search Data Service
4  * ================================================================================
5  * Copyright © 2017 AT&T Intellectual Property.
6  * Copyright © 2017 Amdocs
7  * All rights reserved.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License ati
12  *
13  *    http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  *
22  * ECOMP and OpenECOMP are trademarks
23  * and service marks of AT&T Intellectual Property.
24  */
25 package org.openecomp.sa.searchdbabstraction.util;
26
27 import com.fasterxml.jackson.core.JsonProcessingException;
28 import org.json.simple.JSONArray;
29 import org.json.simple.JSONObject;
30 import org.openecomp.sa.searchdbabstraction.entity.AggregationBucket;
31 import org.openecomp.sa.searchdbabstraction.entity.AggregationResult;
32
33 import java.util.Iterator;
34 import java.util.Set;
35
36 public class AggregationParsingUtil {
37   public static AggregationResult[] parseAggregationResults(JSONObject aggregations)
38       throws JsonProcessingException {
39
40     // Obtain the set of aggregation names
41     Set keySet = aggregations.keySet();
42     AggregationResult[] aggResults = new AggregationResult[keySet.size()];
43
44     int index = 0;
45     for (Iterator it = keySet.iterator(); it.hasNext(); ) {
46       String key = (String) it.next();
47       AggregationResult aggResult = new AggregationResult();
48       aggResult.setName(key);
49
50       JSONObject bucketsOrNested = (JSONObject) aggregations.get(key);
51       Object buckets = bucketsOrNested.get("buckets");
52       if (buckets == null) {
53         // we have a nested
54         Number count = (Number) bucketsOrNested.remove("doc_count");
55         aggResult.setCount(count);
56         AggregationResult[] nestedResults = parseAggregationResults(bucketsOrNested);
57         aggResult.setNestedAggregations(nestedResults);
58       } else {
59         AggregationBucket[] aggBuckets = parseAggregationBuckets((JSONArray) buckets);
60         aggResult.setBuckets(aggBuckets);
61       }
62
63       aggResults[index] = aggResult;
64       index++;
65     }
66
67     return aggResults;
68
69   }
70
71   private static AggregationBucket[] parseAggregationBuckets(JSONArray buckets)
72       throws JsonProcessingException {
73     AggregationBucket[] aggBuckets = new AggregationBucket[buckets.size()];
74     for (int i = 0; i < buckets.size(); i++) {
75       AggregationBucket aggBucket = new AggregationBucket();
76       JSONObject bucketContent = (JSONObject) buckets.get(i);
77       Object key = bucketContent.remove("key");
78       aggBucket.setKey(key);
79       Object formatted = bucketContent.remove("key_as_string");
80       if (formatted != null) {
81         aggBucket.setFormattedKey((String) formatted);
82       }
83       Object count = bucketContent.remove("doc_count");
84       if (count != null) {
85         aggBucket.setCount((Number) count);
86       }
87       bucketContent.remove("from");
88       bucketContent.remove("from_as_string");
89       bucketContent.remove("to");
90       bucketContent.remove("to_as_string");
91
92
93       if (!bucketContent.entrySet().isEmpty()) {
94         // we have results from sub-aggregation
95         AggregationResult[] subResult = parseAggregationResults(bucketContent);
96         if (subResult != null) {
97           aggBucket.setSubAggregationResult(subResult);
98         }
99       }
100       aggBuckets[i] = aggBucket;
101     }
102
103     return aggBuckets;
104   }
105
106 }