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