a4eba45538c115ba2fc69d9f1e1dbdc928d76bfa
[aai/schema-service.git] / aai-schema-service / src / main / java / org / onap / aai / schemaservice / edges / EdgeService.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.edges;
21
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24 import com.google.gson.FieldNamingPolicy;
25 import com.google.gson.Gson;
26 import com.google.gson.GsonBuilder;
27 import com.google.gson.stream.JsonReader;
28 import org.onap.aai.schemaservice.nodeschema.SchemaVersion;
29 import org.onap.aai.schemaservice.nodeschema.SchemaVersions;
30 import org.springframework.beans.factory.annotation.Autowired;
31 import org.springframework.beans.factory.annotation.Value;
32 import org.springframework.stereotype.Service;
33
34 import javax.annotation.PostConstruct;
35 import java.io.File;
36 import java.io.FileReader;
37 import java.io.IOException;
38 import java.nio.file.Files;
39 import java.nio.file.Path;
40 import java.nio.file.Paths;
41 import java.util.*;
42 import java.util.stream.Collectors;
43 import java.util.stream.Stream;
44
45 @Service
46 public class EdgeService {
47
48     private static final Logger LOGGER = LoggerFactory.getLogger(EdgeService.class);
49
50     private static final String FILESEP = System.getProperty("file.separator");
51
52     private SchemaVersions schemaVersions;
53     private String edgesLocation;
54
55     private Map<String, EdgeRules> rulesMap;
56
57     @Autowired
58     public EdgeService(SchemaVersions schemaVersions,
59                        @Value("${schema.edges.location}") String edgesLocation){
60         this.schemaVersions = schemaVersions;
61         this.edgesLocation  = edgesLocation;
62         this.rulesMap       = new HashMap<>();
63     }
64
65     @PostConstruct
66     public void initialize() throws IOException {
67
68         Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES).create();
69
70         for (SchemaVersion schemaVersion : schemaVersions.getVersions()) {
71
72             String edgeRuleVersionPath = edgesLocation + FILESEP + schemaVersion.toString();
73
74             LOGGER.debug("For the version {} looking for edge rules in folder {}", schemaVersion, edgeRuleVersionPath);
75
76             try (Stream<Path> pathStream = Files.walk(Paths.get(edgeRuleVersionPath))){
77
78                 List<Path> jsonFiles = pathStream
79                     .filter((path) -> path.toString().endsWith(".json"))
80                     .collect(Collectors.toList());
81
82                 if(jsonFiles.isEmpty()){
83                     LOGGER.error("Unable to find any edge rules json files in folder {}", edgeRuleVersionPath);
84                 } else {
85                     LOGGER.trace("Found the following edge rules {}", jsonFiles);
86                 }
87
88                 List<EdgeRule> rules = new ArrayList<>();
89                 for(Path path : jsonFiles){
90                     File edgeRuleFile = path.toFile();
91                     try (JsonReader jsonReader = new JsonReader(new FileReader(edgeRuleFile))){
92                         EdgeRules edgeRules = gson.fromJson(jsonReader, EdgeRules.class);
93                         rules.addAll(edgeRules.getRules());
94                     }
95                 }
96
97                 rulesMap.put(schemaVersion.toString(), new EdgeRules(rules));
98             }
99         }
100
101     }
102
103     public Optional<EdgeRules> findRules(String version){
104         return Optional.ofNullable(rulesMap.get(version));
105     }
106 }