Add the queries endpoint and files
[aai/schema-service.git] / aai-schema-service / src / main / java / org / onap / aai / schemaservice / query / QueryService.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.schemaservice.query;
21
22 import com.att.eelf.configuration.EELFLogger;
23 import com.att.eelf.configuration.EELFManager;
24 import org.springframework.beans.factory.annotation.Value;
25 import org.springframework.stereotype.Service;
26
27 import javax.annotation.PostConstruct;
28 import java.io.File;
29 import java.io.IOException;
30 import java.nio.charset.StandardCharsets;
31 import java.nio.file.Files;
32 import java.nio.file.Paths;
33 import java.util.stream.Stream;
34
35 @Service
36 public class QueryService {
37
38     private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(QueryService.class);
39
40     private String queryLocation;
41
42     private String storedQueriesContent;
43
44     public QueryService(@Value("${schema.query.location}") String queryLocation){
45         this.queryLocation = queryLocation;
46     }
47
48     @PostConstruct
49     public void initialize() throws IOException {
50
51         String fileName = queryLocation + File.separator + "stored-queries.json";
52         LOGGER.debug("Loading the following stored queries file {}", fileName);
53
54         StringBuilder contentBuilder = new StringBuilder();
55
56         try(Stream<String> stream = Files.lines(Paths.get(fileName), StandardCharsets.UTF_8)){
57             stream.forEach(s -> contentBuilder.append(s));
58         }
59
60         storedQueriesContent = contentBuilder.toString();
61
62         LOGGER.trace("Contents of the stored query file {}", storedQueriesContent);
63     }
64
65     public String getStoredQueries(){
66         return storedQueriesContent;
67     }
68 }