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