2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 * ECOMP and OpenECOMP are trademarks
23 * and service marks of AT&T Intellectual Property.
25 package org.openecomp.sa.searchdbabstraction.util;
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;
33 import java.util.Iterator;
36 public class AggregationParsingUtil {
37 public static AggregationResult[] parseAggregationResults(JSONObject aggregations)
38 throws JsonProcessingException {
40 // Obtain the set of aggregation names
41 Set keySet = aggregations.keySet();
42 AggregationResult[] aggResults = new AggregationResult[keySet.size()];
45 for (Iterator it = keySet.iterator(); it.hasNext(); ) {
46 String key = (String) it.next();
47 AggregationResult aggResult = new AggregationResult();
48 aggResult.setName(key);
50 JSONObject bucketsOrNested = (JSONObject) aggregations.get(key);
51 Object buckets = bucketsOrNested.get("buckets");
52 if (buckets == null) {
54 Number count = (Number) bucketsOrNested.remove("doc_count");
55 aggResult.setCount(count);
56 AggregationResult[] nestedResults = parseAggregationResults(bucketsOrNested);
57 aggResult.setNestedAggregations(nestedResults);
59 AggregationBucket[] aggBuckets = parseAggregationBuckets((JSONArray) buckets);
60 aggResult.setBuckets(aggBuckets);
63 aggResults[index] = aggResult;
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);
83 Object count = bucketContent.remove("doc_count");
85 aggBucket.setCount((Number) count);
87 bucketContent.remove("from");
88 bucketContent.remove("from_as_string");
89 bucketContent.remove("to");
90 bucketContent.remove("to_as_string");
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);
100 aggBuckets[i] = aggBucket;