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 {
32 public static AggregationResult[] parseAggregationResults(JSONObject aggregations)
33 throws JsonProcessingException {
35 // Obtain the set of aggregation names
36 Set keySet = aggregations.keySet();
37 AggregationResult[] aggResults = new AggregationResult[keySet.size()];
40 for (Iterator it = keySet.iterator(); it.hasNext(); ) {
41 String key = (String) it.next();
42 AggregationResult aggResult = new AggregationResult();
43 aggResult.setName(key);
45 JSONObject bucketsOrNested = (JSONObject) aggregations.get(key);
46 Object buckets = bucketsOrNested.get("buckets");
47 if (buckets == null) {
49 Number count = (Number) bucketsOrNested.remove("doc_count");
50 aggResult.setCount(count);
51 AggregationResult[] nestedResults = parseAggregationResults(bucketsOrNested);
52 aggResult.setNestedAggregations(nestedResults);
54 AggregationBucket[] aggBuckets = parseAggregationBuckets((JSONArray) buckets);
55 aggResult.setBuckets(aggBuckets);
58 aggResults[index] = aggResult;
66 private static AggregationBucket[] parseAggregationBuckets(JSONArray buckets)
67 throws JsonProcessingException {
68 AggregationBucket[] aggBuckets = new AggregationBucket[buckets.size()];
69 for (int i = 0; i < buckets.size(); i++) {
70 AggregationBucket aggBucket = new AggregationBucket();
71 JSONObject bucketContent = (JSONObject) buckets.get(i);
72 Object key = bucketContent.remove("key");
73 aggBucket.setKey(key);
74 Object formatted = bucketContent.remove("key_as_string");
75 if (formatted != null) {
76 aggBucket.setFormattedKey((String) formatted);
78 Object count = bucketContent.remove("doc_count");
80 aggBucket.setCount((Number) count);
82 bucketContent.remove("from");
83 bucketContent.remove("from_as_string");
84 bucketContent.remove("to");
85 bucketContent.remove("to_as_string");
88 if (!bucketContent.entrySet().isEmpty()) {
89 // we have results from sub-aggregation
90 AggregationResult[] subResult = parseAggregationResults(bucketContent);
91 if (subResult != null) {
92 aggBucket.setSubAggregationResult(subResult);
95 aggBuckets[i] = aggBucket;