Update the license for 2017-2018 license
[aai/traversal.git] / aai-traversal / src / main / java / org / onap / aai / rest / search / 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.rest.search;
21
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;
28
29 import java.io.File;
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;
40
41 public class GremlinServerSingleton {
42
43     private static EELFLogger logger = EELFManager.getInstance().getLogger(GremlinServerSingleton.class);
44
45     private Cluster cluster;
46     private boolean timerSet;
47     private Timer timer;
48
49     
50     private GetCustomQueryConfig queryConfig;
51
52
53     private static class Helper {
54         private static final GremlinServerSingleton INSTANCE = new GremlinServerSingleton();
55     }
56
57     public static GremlinServerSingleton getInstance() {
58         return Helper.INSTANCE;
59     }
60
61     private GremlinServerSingleton(){
62         init();
63     }
64
65     /**
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
72      *
73      */
74     private void init() {
75
76         try {
77             cluster = Cluster.build(new File(AAIConstants.AAI_HOME_ETC_APP_PROPERTIES + "gremlin-server-config.yaml"))
78                     .maxContentLength(6537920)
79                     .create();
80         } catch (FileNotFoundException e) {
81             logger.error("Unable to find the file: " + LogFormatTools.getStackTop(e));
82         }
83
84                 try {
85                         String filepath = GetCustomQueryConfig.AAI_HOME_ETC_QUERY_JSON;
86                         Path path = Paths.get(filepath);
87                         String customQueryConfigJson = new String(Files.readAllBytes(path));
88                         
89
90                         queryConfig = new GetCustomQueryConfig(customQueryConfigJson);
91                 } catch (IOException e) {
92                         logger.error("Error occurred during the processing of query json file: " + LogFormatTools.getStackTop(e));
93                 }
94
95
96         TimerTask task = new FileWatcher(new File(GetCustomQueryConfig.AAI_HOME_ETC_QUERY_JSON)) {
97             @Override
98             protected void onChange(File file) {
99                         try {
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));
106                         }
107             }
108         };
109
110         if (!timerSet) {
111             timerSet = true;
112             timer = new Timer();
113             timer.schedule( task , new Date(), 10000 );
114         }
115
116     }
117
118     public Cluster getCluster(){
119         return cluster;
120     }
121     
122     /**
123      * Gets the query using CustomQueryConfig
124      * @param key
125      * @return
126      */
127     public String getStoredQueryFromConfig(String key){
128         CustomQueryConfig customQueryConfig = queryConfig.getStoredQuery(key);
129         if ( customQueryConfig == null ) {
130                 return null;
131         }
132         return customQueryConfig.getQuery();
133     }
134     
135     public CustomQueryConfig getCustomQueryConfig(String key) {
136         return queryConfig.getStoredQuery(key);
137     }
138
139
140 }