b1d68bcb891e4a401cebefe6c6ff82143945b023
[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.logging.LogFormatTools;
25 import org.onap.aai.util.AAIConstants;
26 import org.onap.aai.util.FileWatcher;
27 import com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
29 import org.apache.tinkerpop.gremlin.driver.Cluster;
30
31 import java.io.File;
32 import java.io.FileInputStream;
33 import java.io.FileNotFoundException;
34 import java.io.IOException;
35 import java.nio.file.Files;
36 import java.nio.file.Path;
37 import java.nio.file.Paths;
38 import java.util.Date;
39 import java.util.Properties;
40 import java.util.Timer;
41 import java.util.TimerTask;
42
43 public class GremlinServerSingleton {
44
45     private static EELFLogger logger = EELFManager.getInstance().getLogger(GremlinServerSingleton.class);
46
47     private Cluster cluster;
48     private boolean timerSet;
49     private Timer timer;
50
51     
52     private GetCustomQueryConfig queryConfig;
53
54
55     private static class Helper {
56         private static final GremlinServerSingleton INSTANCE = new GremlinServerSingleton();
57     }
58
59     public static GremlinServerSingleton getInstance() {
60         return Helper.INSTANCE;
61     }
62
63     private GremlinServerSingleton(){
64         init();
65     }
66
67     /**
68      * Initializes the gremlin server singleton
69      * Loads the configuration of the gremlin server and creates a cluster
70      * Loads the gremlin query file into the properties object
71      * Then creates a file watcher to watch the file every ten seconds
72      * and if there is a change in the file, then reloads the file into
73      * the properties object
74      *
75      */
76     private void init() {
77
78         try {
79             cluster = Cluster.build(new File(AAIConstants.AAI_HOME_ETC_APP_PROPERTIES + "gremlin-server-config.yaml"))
80                     .maxContentLength(6537920)
81                     .create();
82         } catch (FileNotFoundException e) {
83             logger.error("Unable to find the file: " + LogFormatTools.getStackTop(e));
84         }
85
86                 try {
87                         String filepath = GetCustomQueryConfig.AAI_HOME_ETC_QUERY_JSON;
88                         Path path = Paths.get(filepath);
89                         String customQueryConfigJson = new String(Files.readAllBytes(path));
90                         
91
92                         queryConfig = new GetCustomQueryConfig(customQueryConfigJson);
93                 } catch (IOException e) {
94                         logger.error("Error occurred during the processing of query json file: " + LogFormatTools.getStackTop(e));
95                 }
96
97
98         TimerTask task = new FileWatcher(new File(GetCustomQueryConfig.AAI_HOME_ETC_QUERY_JSON)) {
99             @Override
100             protected void onChange(File file) {
101                         try {
102                                 String filepath = GetCustomQueryConfig.AAI_HOME_ETC_QUERY_JSON;
103                                 Path path = Paths.get(filepath);
104                                 String customQueryConfigJson = new String(Files.readAllBytes(path));
105                                 queryConfig = new GetCustomQueryConfig(customQueryConfigJson);
106                         } catch (IOException e) {
107                                 logger.error("Error occurred during the processing of query json file: " + LogFormatTools.getStackTop(e));
108                         }
109             }
110         };
111
112         if (!timerSet) {
113             timerSet = true;
114             timer = new Timer();
115             timer.schedule( task , new Date(), 10000 );
116         }
117
118     }
119
120     public Cluster getCluster(){
121         return cluster;
122     }
123     
124     /**
125      * Gets the query using CustomQueryConfig
126      * @param key
127      * @return
128      */
129     public String getStoredQueryFromConfig(String key){
130         CustomQueryConfig customQueryConfig = queryConfig.getStoredQuery(key);
131         if ( customQueryConfig == null ) {
132                 return null;
133         }
134         return customQueryConfig.getQuery();
135     }
136     
137     public CustomQueryConfig getCustomQueryConfig(String key) {
138         return queryConfig.getStoredQuery(key);
139     }
140
141
142 }