Update license and poms
[aai/sparky-be.git] / sparkybe-onap-service / src / main / java / org / onap / aai / sparky / inventory / EntityHistoryQueryBuilder.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
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 package org.onap.aai.sparky.inventory;
22
23 import javax.json.Json;
24 import javax.json.JsonArray;
25 import javax.json.JsonArrayBuilder;
26 import javax.json.JsonObject;
27 import javax.json.JsonObjectBuilder;
28
29 /**
30  * The Class EntityHistoryQueryBuilder.
31  */
32 public class EntityHistoryQueryBuilder {
33
34   private static final String TABLE = "table";
35   private static final String GRAPH = "graph";
36
37   /**
38    * Gets the query.
39    *
40    * @param type the type
41    * @return the query
42    */
43   public static JsonObject getQuery(String type) {
44     if (type.equalsIgnoreCase(TABLE)) {
45       return createTableQuery();
46     } else if (type.equalsIgnoreCase(GRAPH)) {
47       return createGraphQuery();
48     } else {
49       return null;
50     }
51   }
52
53   /**
54    * Creates the graph query.
55    *
56    * @return the json object
57    */
58   public static JsonObject createGraphQuery() {
59     JsonObjectBuilder jsonBuilder = Json.createObjectBuilder();
60
61     jsonBuilder.add("aggs",
62         Json.createObjectBuilder().add("group_by_entityType",
63             Json.createObjectBuilder()
64                 .add("terms", Json.createObjectBuilder().add("field", "entityType").add("size", 0))
65                 .add("aggs", Json.createObjectBuilder().add("group_by_date",
66                     Json.createObjectBuilder().add("date_histogram", createDateHistogram())
67                         .add("aggs", Json.createObjectBuilder().add("sort_by_date",
68                             Json.createObjectBuilder().add("top_hits", createTopHitsBlob())))))));
69     jsonBuilder.add("size", 0);
70
71     return jsonBuilder.build();
72   }
73
74   /**
75    * Creates the table query.
76    *
77    * @return the json object
78    */
79   public static JsonObject createTableQuery() {
80     JsonObjectBuilder jsonBuilder = Json.createObjectBuilder();
81
82     jsonBuilder.add("aggs",
83         Json.createObjectBuilder().add("group_by_entityType",
84             Json.createObjectBuilder()
85                 .add("terms", Json.createObjectBuilder().add("field", "entityType").add("size", 0))
86                 .add("aggs", Json.createObjectBuilder().add("sort_by_date",
87                     Json.createObjectBuilder().add("top_hits", createTopHitsBlob())))));
88     jsonBuilder.add("size", 0);
89
90     return jsonBuilder.build();
91   }
92
93   /**
94    * Creates the date histogram.
95    *
96    * @return the json object
97    */
98   private static JsonObject createDateHistogram() {
99     JsonObjectBuilder jsonBuilder = Json.createObjectBuilder();
100
101     jsonBuilder.add("field", "timestamp");
102     jsonBuilder.add("min_doc_count", 1);
103     jsonBuilder.add("interval", "day");
104     jsonBuilder.add("format", "epoch_millis");
105
106     return jsonBuilder.build();
107   }
108
109   /**
110    * Creates the top hits blob.
111    *
112    * @return the json object
113    */
114   private static JsonObject createTopHitsBlob() {
115     JsonObjectBuilder builder = Json.createObjectBuilder();
116     builder.add("size", 1);
117     builder.add("sort", getSortCriteria());
118     return builder.build();
119   }
120
121   public static JsonArray getSortCriteria() {
122     JsonArrayBuilder jsonBuilder = Json.createArrayBuilder();
123     jsonBuilder.add(Json.createObjectBuilder().add("timestamp",
124         Json.createObjectBuilder().add("order", "desc")));
125
126     return jsonBuilder.build();
127   }
128
129   /**
130    * The main method.
131    *
132    * @param args the arguments
133    */
134   public static void main(String[] args) {
135     System.out.println("TABLE-QUERY: " + createTableQuery().toString());
136     System.out.println("GRAPH_QUERY: " + createGraphQuery().toString());
137   }
138
139 }