2 * ============LICENSE_START=======================================================
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
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=========================================================
21 package org.onap.aai.sa.searchdbabstraction.util;
23 import com.fasterxml.jackson.core.JsonProcessingException;
24 import java.util.Iterator;
26 import org.json.simple.JSONArray;
27 import org.json.simple.JSONObject;
28 import org.onap.aai.sa.searchdbabstraction.entity.AggregationBucket;
29 import org.onap.aai.sa.searchdbabstraction.entity.AggregationResult;
31 public class AggregationParsingUtil {
33 private AggregationParsingUtil() { // Do not instantiate
36 public static AggregationResult[] parseAggregationResults(JSONObject aggregations) throws JsonProcessingException {
38 // Obtain the set of aggregation names
39 Set<?> keySet = aggregations.keySet();
40 AggregationResult[] aggResults = new AggregationResult[keySet.size()];
43 for (Iterator<?> it = keySet.iterator(); it.hasNext();) {
44 String key = (String) it.next();
45 AggregationResult aggResult = new AggregationResult();
46 aggResult.setName(key);
48 JSONObject bucketsOrNested = (JSONObject) aggregations.get(key);
49 Object buckets = bucketsOrNested.get("buckets");
50 if (buckets == null) {
52 Number count = (Number) bucketsOrNested.remove("doc_count");
53 aggResult.setCount(count);
54 AggregationResult[] nestedResults = parseAggregationResults(bucketsOrNested);
55 aggResult.setNestedAggregations(nestedResults);
57 AggregationBucket[] aggBuckets = parseAggregationBuckets((JSONArray) buckets);
58 aggResult.setBuckets(aggBuckets);
61 aggResults[index] = aggResult;
69 private static AggregationBucket[] parseAggregationBuckets(JSONArray buckets) throws JsonProcessingException {
70 AggregationBucket[] aggBuckets = new AggregationBucket[buckets.size()];
71 for (int i = 0; i < buckets.size(); i++) {
72 AggregationBucket aggBucket = new AggregationBucket();
73 JSONObject bucketContent = (JSONObject) buckets.get(i);
74 Object key = bucketContent.remove("key");
75 aggBucket.setKey(key);
76 Object formatted = bucketContent.remove("key_as_string");
77 if (formatted != null) {
78 aggBucket.setFormattedKey((String) formatted);
80 Object count = bucketContent.remove("doc_count");
82 aggBucket.setCount((Number) count);
84 bucketContent.remove("from");
85 bucketContent.remove("from_as_string");
86 bucketContent.remove("to");
87 bucketContent.remove("to_as_string");
90 if (!bucketContent.entrySet().isEmpty()) {
91 // we have results from sub-aggregation
92 AggregationResult[] subResult = parseAggregationResults(bucketContent);
93 if (subResult != null) {
94 aggBucket.setSubAggregationResult(subResult);
97 aggBuckets[i] = aggBucket;