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=========================================================
21 package org.onap.aai.schemaservice.edges;
23 import com.google.gson.FieldNamingPolicy;
24 import com.google.gson.Gson;
25 import com.google.gson.GsonBuilder;
26 import com.google.gson.stream.JsonReader;
29 import java.io.FileReader;
30 import java.io.IOException;
31 import java.nio.file.Files;
32 import java.nio.file.Path;
33 import java.nio.file.Paths;
35 import java.util.stream.Collectors;
36 import java.util.stream.Stream;
38 import javax.annotation.PostConstruct;
40 import org.onap.aai.schemaservice.nodeschema.SchemaVersion;
41 import org.onap.aai.schemaservice.nodeschema.SchemaVersions;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44 import org.springframework.beans.factory.annotation.Autowired;
45 import org.springframework.beans.factory.annotation.Value;
46 import org.springframework.stereotype.Service;
49 public class EdgeService {
51 private static final Logger LOGGER = LoggerFactory.getLogger(EdgeService.class);
53 private static final String FILESEP = System.getProperty("file.separator");
55 private SchemaVersions schemaVersions;
56 private String edgesLocation;
58 private Map<String, EdgeRules> rulesMap;
61 public EdgeService(SchemaVersions schemaVersions,
62 @Value("${schema.edges.location}") String edgesLocation) {
63 this.schemaVersions = schemaVersions;
64 this.edgesLocation = edgesLocation;
65 this.rulesMap = new HashMap<>();
69 public void initialize() throws IOException {
71 Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES)
74 for (SchemaVersion schemaVersion : schemaVersions.getVersions()) {
76 String edgeRuleVersionPath = edgesLocation + FILESEP + schemaVersion.toString();
78 LOGGER.debug("For the version {} looking for edge rules in folder {}", schemaVersion,
81 try (Stream<Path> pathStream = Files.walk(Paths.get(edgeRuleVersionPath))) {
83 List<Path> jsonFiles =
84 pathStream.filter((path) -> path.toString().endsWith(".json"))
85 .collect(Collectors.toList());
87 if (jsonFiles.isEmpty()) {
88 LOGGER.error("Unable to find any edge rules json files in folder {}",
91 LOGGER.trace("Found the following edge rules {}", jsonFiles);
94 List<EdgeRule> rules = new ArrayList<>();
95 for (Path path : jsonFiles) {
96 File edgeRuleFile = path.toFile();
97 try (JsonReader jsonReader = new JsonReader(new FileReader(edgeRuleFile))) {
98 EdgeRules edgeRules = gson.fromJson(jsonReader, EdgeRules.class);
99 rules.addAll(edgeRules.getRules());
103 rulesMap.put(schemaVersion.toString(), new EdgeRules(rules));
109 public Optional<EdgeRules> findRules(String version) {
110 return Optional.ofNullable(rulesMap.get(version));