2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.aai.schemaservice.edges;
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;
34 import javax.annotation.PostConstruct;
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;
42 import java.util.stream.Collectors;
43 import java.util.stream.Stream;
46 public class EdgeService {
48 private static final Logger LOGGER = LoggerFactory.getLogger(EdgeService.class);
50 private static final String FILESEP = System.getProperty("file.separator");
52 private SchemaVersions schemaVersions;
53 private String edgesLocation;
55 private Map<String, EdgeRules> rulesMap;
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<>();
66 public void initialize() throws IOException {
68 Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES).create();
70 for (SchemaVersion schemaVersion : schemaVersions.getVersions()) {
72 String edgeRuleVersionPath = edgesLocation + FILESEP + schemaVersion.toString();
74 LOGGER.debug("For the version {} looking for edge rules in folder {}", schemaVersion, edgeRuleVersionPath);
76 try (Stream<Path> pathStream = Files.walk(Paths.get(edgeRuleVersionPath))){
78 List<Path> jsonFiles = pathStream
79 .filter((path) -> path.toString().endsWith(".json"))
80 .collect(Collectors.toList());
82 if(jsonFiles.isEmpty()){
83 LOGGER.error("Unable to find any edge rules json files in folder {}", edgeRuleVersionPath);
85 LOGGER.trace("Found the following edge rules {}", jsonFiles);
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());
97 rulesMap.put(schemaVersion.toString(), new EdgeRules(rules));
103 public Optional<EdgeRules> findRules(String version){
104 return Optional.ofNullable(rulesMap.get(version));