2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.aai.queries;
24 import java.io.IOException;
25 import java.nio.file.Files;
26 import java.nio.file.Path;
27 import java.nio.file.Paths;
28 import java.util.Date;
29 import java.util.Timer;
30 import java.util.TimerTask;
32 import javax.annotation.PostConstruct;
34 import org.onap.aai.aaf.auth.FileWatcher;
35 import org.onap.aai.logging.LogFormatTools;
36 import org.onap.aai.util.AAIConstants;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import org.springframework.beans.factory.annotation.Value;
41 public class GremlinServerSingleton {
43 private static Logger logger = LoggerFactory.getLogger(GremlinServerSingleton.class);
45 private boolean timerSet;
48 private GetCustomQueryConfig queryConfig;
50 @Value("${schema.queries.location}")
51 private String storedQueriesLocation;
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
67 storedQueriesLocation + AAIConstants.AAI_FILESEP + "stored-queries.json";
68 Path path = Paths.get(filepath);
69 String customQueryConfigJson = new String(Files.readAllBytes(path));
71 queryConfig = new GetCustomQueryConfig(customQueryConfigJson);
72 } catch (IOException e) {
73 logger.error("Error occurred during the processing of query json file: "
74 + LogFormatTools.getStackTop(e));
77 TimerTask task = new FileWatcher(new File(storedQueriesLocation)) {
79 protected void onChange(File file) {
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: "
87 + LogFormatTools.getStackTop(e));
95 timer.schedule(task, new Date(), 10000);
101 * Gets the query using CustomQueryConfig
103 * @param key the config key
104 * @return the stored query
106 public String getStoredQueryFromConfig(String key) {
107 CustomQueryConfig customQueryConfig = queryConfig.getStoredQuery(key);
108 if (customQueryConfig == null) {
111 return customQueryConfig.getQuery();
114 public CustomQueryConfig getCustomQueryConfig(String key) {
115 return queryConfig.getStoredQuery(key);