2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 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=========================================================
20 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 package org.onap.aai.rest.search;
24 import org.onap.aai.util.AAIConstants;
25 import org.onap.aai.util.FileWatcher;
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28 import org.apache.tinkerpop.gremlin.driver.Cluster;
31 import java.io.FileInputStream;
32 import java.io.FileNotFoundException;
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.Properties;
39 import java.util.Timer;
40 import java.util.TimerTask;
42 public class GremlinServerSingleton {
44 private static EELFLogger logger = EELFManager.getInstance().getLogger(GremlinServerSingleton.class);
46 private Cluster cluster;
47 private boolean timerSet;
51 private GetCustomQueryConfig queryConfig;
54 private static class Helper {
55 private static final GremlinServerSingleton INSTANCE = new GremlinServerSingleton();
58 public static GremlinServerSingleton getInstance() {
59 return Helper.INSTANCE;
62 private GremlinServerSingleton(){
67 * Initializes the gremlin server singleton
68 * Loads the configuration of the gremlin server and creates a cluster
69 * Loads the gremlin query file into the properties object
70 * Then creates a file watcher to watch the file every ten seconds
71 * and if there is a change in the file, then reloads the file into
72 * the properties object
78 cluster = Cluster.build(new File(AAIConstants.AAI_HOME_ETC_APP_PROPERTIES + "gremlin-server-config.yaml"))
79 .maxContentLength(6537920)
81 } catch (FileNotFoundException e) {
82 logger.error("Unable to find the file: " + e);
86 String filepath = GetCustomQueryConfig.AAI_HOME_ETC_QUERY_JSON;
87 Path path = Paths.get(filepath);
88 String customQueryConfigJson = new String(Files.readAllBytes(path));
91 queryConfig = new GetCustomQueryConfig(customQueryConfigJson);
92 } catch (IOException e) {
93 logger.error("Error occurred during the processing of query json file: " + e);
97 TimerTask task = new FileWatcher(new File(GetCustomQueryConfig.AAI_HOME_ETC_QUERY_JSON)) {
99 protected void onChange(File file) {
101 String filepath = GetCustomQueryConfig.AAI_HOME_ETC_QUERY_JSON;
102 Path path = Paths.get(filepath);
103 String customQueryConfigJson = new String(Files.readAllBytes(path));
104 queryConfig = new GetCustomQueryConfig(customQueryConfigJson);
105 } catch (IOException e) {
106 logger.error("Error occurred during the processing of query json file: " + e);
114 timer.schedule( task , new Date(), 10000 );
119 public Cluster getCluster(){
124 * Gets the query using CustomQueryConfig
128 public String getStoredQueryFromConfig(String key){
129 CustomQueryConfig customQueryConfig = queryConfig.getStoredQuery(key);
130 return customQueryConfig.getQuery();
133 public CustomQueryConfig getCustomQueryConfig(String key) {
134 return queryConfig.getStoredQuery(key);