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