Sync logging context changes
[aai/traversal.git] / aai-traversal / src / main / java / org / onap / aai / rest / search / GetCustomQueryConfig.java
1 package org.onap.aai.rest.search;
2
3 /*-
4  * ============LICENSE_START=======================================================
5  * org.onap.aai
6  * ================================================================================
7  * Copyright (C) 2017 AT&T Intellectual Property. 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
23 import java.io.IOException;
24 import java.lang.reflect.Type;
25 import java.nio.file.Files;
26 import java.nio.file.Paths;
27 import java.util.ArrayList;
28 import java.util.List;
29
30 import org.onap.aai.util.AAIConstants;
31
32 import com.google.gson.Gson;
33 import com.google.gson.JsonArray;
34 import com.google.gson.JsonElement;
35 import com.google.gson.JsonObject;
36 import com.google.gson.JsonParser;
37 import com.google.gson.reflect.TypeToken;
38
39 public class GetCustomQueryConfig {
40
41         private JsonArray storedQueries = null;
42         private CustomQueryConfig customQueryConfig;
43         
44         
45         private final static String QUERY_CONFIG = "query";
46         private final static String REQUIRED_CONFIG = "required-properties";
47         private final static String OPTIONAL_CONFIG = "optional-properties";
48         private final static String STORED_QUERIES_CONFIG = "stored-queries";
49         private final static String STORED_QUERY_CONFIG = "stored-query";
50         
51         public static final String AAI_HOME_ETC_QUERY_JSON = AAIConstants.AAI_HOME_ETC + "query" + AAIConstants.AAI_FILESEP + "stored-queries.json";
52         
53         public GetCustomQueryConfig(String customQueryJson ) {
54                 init(customQueryJson);
55         }
56         
57         private void init( String customQueryJson) {
58                 JsonParser parser = new JsonParser();
59                 JsonObject queriesObject = parser.parse(customQueryJson).getAsJsonObject();
60                 if (queriesObject.has(STORED_QUERIES_CONFIG)) {
61                         
62                         storedQueries = queriesObject.getAsJsonArray(STORED_QUERIES_CONFIG);
63                 }
64         }
65
66         private List<String> toStringList(JsonArray array) {
67            Gson converter = new Gson(); 
68            Type listType = new TypeToken<List<String>>() {}.getType();
69            return converter.fromJson(array, listType);
70         }
71         
72         private List<String> getPropertyList(JsonObject configObject, String config ) {
73                 JsonElement subqueryConfig;
74                 JsonArray props;
75                 
76                 if ( configObject.has(config)) {
77                         subqueryConfig = configObject.get(config);
78                         if ( subqueryConfig != null && !subqueryConfig.isJsonNull() ) {
79                                 props = subqueryConfig.getAsJsonArray();
80                                 if ( props != null ) {
81                                         return toStringList(props);
82                                 }
83                         }
84                 }
85                 return toStringList(null);
86         }
87         
88         private String getPropertyString(JsonObject configObject, String config) {
89                 JsonElement subqueryConfig;
90                 
91                 if ( configObject.has(config)) {
92                         subqueryConfig = configObject.get(config);
93                         if ( subqueryConfig != null && !subqueryConfig.isJsonNull() ) {
94                                 return subqueryConfig.getAsString();
95                         }
96                 }
97                 return null;
98         }
99         
100         private void getStoredQueryBlock( JsonObject configObject, String config ) {
101                 if ( !configObject.has(config)) {
102                         customQueryConfig.setQueryRequiredProperties( new ArrayList<String>() );
103                         customQueryConfig.setQueryOptionalProperties( new ArrayList<String>()  );
104                         return;
105                 }
106                 
107                 JsonElement queryConfig;
108                 JsonObject subObject;
109                 String multipleStartNodes;
110                 List<String> propertyList;
111
112                 queryConfig = configObject.get(config);
113                 subObject = queryConfig.getAsJsonObject();
114                 propertyList = getPropertyList(subObject, REQUIRED_CONFIG);
115                 if ( propertyList == null ) {
116                         propertyList = new ArrayList<String>();
117                 }
118                 customQueryConfig.setQueryRequiredProperties( propertyList );
119                 propertyList = getPropertyList(subObject, OPTIONAL_CONFIG);
120                 if ( propertyList == null ) {
121                         propertyList = new ArrayList<String>();
122                 }
123                 customQueryConfig.setQueryOptionalProperties( propertyList );
124                         
125         }
126         
127         
128         public CustomQueryConfig getStoredQuery(String queryName ) {
129         
130                 customQueryConfig = null;
131                 JsonObject configObject;
132                 JsonElement query;
133                 JsonElement queryConfig;
134                 String queryString;
135
136                 for (JsonElement storedQuery : storedQueries) {
137                         if (storedQuery.isJsonObject()) {
138                                 JsonObject queryObject = storedQuery.getAsJsonObject();
139                                 query = queryObject.get(queryName);
140                                 if ( query != null ) {
141                                         customQueryConfig = new CustomQueryConfig();
142                                         configObject = query.getAsJsonObject();
143                                         getStoredQueryBlock(configObject, QUERY_CONFIG);
144                                         if ( configObject.has(STORED_QUERY_CONFIG)) {
145                                                 queryConfig = configObject.get(STORED_QUERY_CONFIG);
146                                                 customQueryConfig.setQuery(queryConfig.getAsString());
147                                         }
148                                         break;
149                                 } 
150                         }
151                 }
152
153                 return customQueryConfig;
154                 
155         }
156
157
158
159 }