Merge "[AAI] Improve test coverage for A&AI component aai-schema-service"
[aai/schema-service.git] / aai-queries / src / main / java / org / onap / aai / queries / GremlinServerSingleton.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.aai.queries;
22
23 import java.io.IOException;
24 import java.nio.file.Files;
25 import java.nio.file.Path;
26 import java.nio.file.Paths;
27 import java.util.Timer;
28 import javax.annotation.PostConstruct;
29
30 import org.onap.aai.logging.LogFormatTools;
31 import org.onap.aai.util.AAIConstants;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.springframework.beans.factory.annotation.Value;
35
36 public class GremlinServerSingleton {
37
38     private static Logger logger = LoggerFactory.getLogger(GremlinServerSingleton.class);
39
40     private boolean timerSet;
41     private Timer timer;
42
43     private GetCustomQueryConfig queryConfig;
44
45     @Value("${schema.queries.location}")
46     private String storedQueriesLocation;
47
48     /**
49      * Initializes the gremlin server singleton
50      * Loads the configuration of the gremlin server and creates a cluster
51      * Loads the gremlin query file into the properties object
52      * Then creates a file watcher to watch the file every ten seconds
53      * and if there is a change in the file, then reloads the file into
54      * the properties object
55      *
56      */
57     @PostConstruct
58     public void init() {
59
60         try {
61             String filepath =
62                 storedQueriesLocation + AAIConstants.AAI_FILESEP + "stored-queries.json";
63             Path path = Paths.get(filepath);
64             String customQueryConfigJson = new String(Files.readAllBytes(path));
65
66             queryConfig = new GetCustomQueryConfig(customQueryConfigJson);
67         } catch (IOException e) {
68             logger.error("Error occurred during the processing of query json file: "
69                 + LogFormatTools.getStackTop(e));
70         }
71
72     }
73
74     /**
75      * Gets the query using CustomQueryConfig
76      *
77      * @param key the config key
78      * @return the stored query
79      */
80     public String getStoredQueryFromConfig(String key) {
81         CustomQueryConfig customQueryConfig = queryConfig.getStoredQuery(key);
82         if (customQueryConfig == null) {
83             return null;
84         }
85         return customQueryConfig.getQuery();
86     }
87
88     public CustomQueryConfig getCustomQueryConfig(String key) {
89         return queryConfig.getStoredQuery(key);
90     }
91
92 }