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=========================================================
20 package org.onap.aai.rest.search;
22 import org.onap.aai.logging.LogFormatTools;
23 import org.onap.aai.util.AAIConstants;
24 import org.onap.aai.util.FileWatcher;
25 import com.att.eelf.configuration.EELFLogger;
26 import com.att.eelf.configuration.EELFManager;
27 import org.apache.tinkerpop.gremlin.driver.Cluster;
30 import java.io.FileInputStream;
31 import java.io.FileNotFoundException;
32 import java.io.IOException;
33 import java.nio.file.Files;
34 import java.nio.file.Path;
35 import java.nio.file.Paths;
36 import java.util.Date;
37 import java.util.Properties;
38 import java.util.Timer;
39 import java.util.TimerTask;
41 public class GremlinServerSingleton {
43 private static EELFLogger logger = EELFManager.getInstance().getLogger(GremlinServerSingleton.class);
45 private Cluster cluster;
46 private boolean timerSet;
50 private GetCustomQueryConfig queryConfig;
53 private static class Helper {
54 private static final GremlinServerSingleton INSTANCE = new GremlinServerSingleton();
57 public static GremlinServerSingleton getInstance() {
58 return Helper.INSTANCE;
61 private GremlinServerSingleton(){
66 * Initializes the gremlin server singleton
67 * Loads the configuration of the gremlin server and creates a cluster
68 * Loads the gremlin query file into the properties object
69 * Then creates a file watcher to watch the file every ten seconds
70 * and if there is a change in the file, then reloads the file into
71 * the properties object
77 cluster = Cluster.build(new File(AAIConstants.AAI_HOME_ETC_APP_PROPERTIES + "gremlin-server-config.yaml"))
78 .maxContentLength(6537920)
80 } catch (FileNotFoundException e) {
81 logger.error("Unable to find the file: " + LogFormatTools.getStackTop(e));
85 String filepath = GetCustomQueryConfig.AAI_HOME_ETC_QUERY_JSON;
86 Path path = Paths.get(filepath);
87 String customQueryConfigJson = new String(Files.readAllBytes(path));
90 queryConfig = new GetCustomQueryConfig(customQueryConfigJson);
91 } catch (IOException e) {
92 logger.error("Error occurred during the processing of query json file: " + LogFormatTools.getStackTop(e));
96 TimerTask task = new FileWatcher(new File(GetCustomQueryConfig.AAI_HOME_ETC_QUERY_JSON)) {
98 protected void onChange(File file) {
100 String filepath = GetCustomQueryConfig.AAI_HOME_ETC_QUERY_JSON;
101 Path path = Paths.get(filepath);
102 String customQueryConfigJson = new String(Files.readAllBytes(path));
103 queryConfig = new GetCustomQueryConfig(customQueryConfigJson);
104 } catch (IOException e) {
105 logger.error("Error occurred during the processing of query json file: " + LogFormatTools.getStackTop(e));
113 timer.schedule( task , new Date(), 10000 );
118 public Cluster getCluster(){
123 * Gets the query using CustomQueryConfig
127 public String getStoredQueryFromConfig(String key){
128 CustomQueryConfig customQueryConfig = queryConfig.getStoredQuery(key);
129 if ( customQueryConfig == null ) {
132 return customQueryConfig.getQuery();
135 public CustomQueryConfig getCustomQueryConfig(String key) {
136 return queryConfig.getStoredQuery(key);