Add instructions to invoke the linter and code formatter plugins to the README and...
[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
21 package org.onap.aai.schemaservice.edges;
22
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;
27
28 import java.io.File;
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;
34 import java.util.*;
35 import java.util.stream.Collectors;
36 import java.util.stream.Stream;
37
38 import javax.annotation.PostConstruct;
39
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;
47
48 @Service
49 public class EdgeService {
50
51     private static final Logger LOGGER = LoggerFactory.getLogger(EdgeService.class);
52
53     private static final String FILESEP = System.getProperty("file.separator");
54
55     private SchemaVersions schemaVersions;
56     private String edgesLocation;
57
58     private Map<String, EdgeRules> rulesMap;
59
60     @Autowired
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<>();
66     }
67
68     @PostConstruct
69     public void initialize() throws IOException {
70
71         Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES)
72             .create();
73
74         for (SchemaVersion schemaVersion : schemaVersions.getVersions()) {
75
76             String edgeRuleVersionPath = edgesLocation + FILESEP + schemaVersion.toString();
77
78             LOGGER.debug("For the version {} looking for edge rules in folder {}", schemaVersion,
79                 edgeRuleVersionPath);
80
81             try (Stream<Path> pathStream = Files.walk(Paths.get(edgeRuleVersionPath))) {
82
83                 List<Path> jsonFiles =
84                     pathStream.filter((path) -> path.toString().endsWith(".json"))
85                         .collect(Collectors.toList());
86
87                 if (jsonFiles.isEmpty()) {
88                     LOGGER.error("Unable to find any edge rules json files in folder {}",
89                         edgeRuleVersionPath);
90                 } else {
91                     LOGGER.trace("Found the following edge rules {}", jsonFiles);
92                 }
93
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());
100                     }
101                 }
102
103                 rulesMap.put(schemaVersion.toString(), new EdgeRules(rules));
104             }
105         }
106
107     }
108
109     public Optional<EdgeRules> findRules(String version) {
110         return Optional.ofNullable(rulesMap.get(version));
111     }
112 }