Replace all tab characters in java files with two spaces to remove linter warning
[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 package org.onap.aai.queries;
21
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 import org.onap.aai.aaf.auth.FileWatcher;
26 import org.onap.aai.logging.LogFormatTools;
27 import org.onap.aai.util.AAIConstants;
28
29 import org.springframework.beans.factory.annotation.Value;
30 import javax.annotation.PostConstruct;
31
32 import java.io.File;
33 import java.io.IOException;
34 import java.nio.file.Files;
35 import java.nio.file.Path;
36 import java.nio.file.Paths;
37 import java.util.Date;
38 import java.util.Timer;
39 import java.util.TimerTask;
40
41 public class GremlinServerSingleton {
42
43     private static Logger logger = LoggerFactory.getLogger(GremlinServerSingleton.class);
44
45     private boolean timerSet;
46     private Timer timer;
47
48     private GetCustomQueryConfig queryConfig;
49
50     @Value("${schema.queries.location}")
51     private String storedQueriesLocation;
52
53     /**
54      * Initializes the gremlin server singleton
55      * Loads the configuration of the gremlin server and creates a cluster
56      * Loads the gremlin query file into the properties object
57      * Then creates a file watcher to watch the file every ten seconds
58      * and if there is a change in the file, then reloads the file into
59      * the properties object
60      *
61      */
62     @PostConstruct
63     public void init() {
64
65     try {
66       String filepath = storedQueriesLocation + AAIConstants.AAI_FILESEP + "stored-queries.json";
67       Path path = Paths.get(filepath);
68       String customQueryConfigJson = new String(Files.readAllBytes(path));
69
70
71       queryConfig = new GetCustomQueryConfig(customQueryConfigJson);
72     } catch (IOException e) {
73       logger.error("Error occurred during the processing of query json file: " + LogFormatTools.getStackTop(e));
74     }
75
76
77         TimerTask task = new FileWatcher(new File(storedQueriesLocation)) {
78             @Override
79             protected void onChange(File file) {
80             try {
81               String filepath = storedQueriesLocation;
82               Path path = Paths.get(filepath);
83               String customQueryConfigJson = new String(Files.readAllBytes(path));
84               queryConfig = new GetCustomQueryConfig(customQueryConfigJson);
85             } catch (IOException e) {
86               logger.error("Error occurred during the processing of query json file: " + LogFormatTools.getStackTop(e));
87             }
88             }
89         };
90
91         if (!timerSet) {
92             timerSet = true;
93             timer = new Timer();
94             timer.schedule( task , new Date(), 10000 );
95         }
96
97     }
98
99     /**
100      * Gets the query using CustomQueryConfig
101      * @param key
102      * @return
103      */
104     public String getStoredQueryFromConfig(String key){
105       CustomQueryConfig customQueryConfig = queryConfig.getStoredQuery(key);
106       if ( customQueryConfig == null ) {
107         return null;
108       }
109       return customQueryConfig.getQuery();
110     }
111
112     public CustomQueryConfig getCustomQueryConfig(String key) {
113       return queryConfig.getStoredQuery(key);
114     }
115
116
117 }