a405a2e1ea939c0d64572913b6faaaaa06f454af
[integration.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * PNF-REGISTRATION-HANDLER
4  * ================================================================================
5  * Copyright (C) 2018 Nokia. 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
21 package org.onap.pnfsimulator.simulator;
22
23 import com.google.gson.Gson;
24 import com.google.gson.JsonObject;
25 import org.springframework.beans.factory.annotation.Autowired;
26 import org.springframework.beans.factory.annotation.Value;
27
28 import java.io.IOException;
29 import java.nio.file.Files;
30 import java.nio.file.Path;
31 import java.nio.file.Paths;
32 import java.util.stream.Collectors;
33 import java.util.stream.Stream;
34
35 class FilesystemTemplateReader implements TemplateReader {
36
37     private final Path templatesDir;
38     private final Gson gson;
39
40     @Autowired
41     FilesystemTemplateReader(@Value("${templates.dir}") String templatesDir, Gson gson) {
42         this.templatesDir = Paths.get(templatesDir);
43         this.gson = gson;
44     }
45
46     public JsonObject readTemplate(String templateFileName) throws IOException {
47         Path absTemplateFilePath = templatesDir.resolve(templateFileName);
48         try (Stream<String> lines = Files.lines(absTemplateFilePath)) {
49             String content = lines.collect(Collectors.joining("\n"));
50             return gson.fromJson(content, JsonObject.class);
51         }
52     }
53 }
54