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 / config / AAIConfigTranslator.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.config;
22
23 import java.io.File;
24 import java.util.Arrays;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.TreeMap;
28 import java.util.function.Supplier;
29 import java.util.stream.Collectors;
30 import java.util.stream.Stream;
31
32 import org.onap.aai.schemaservice.nodeschema.SchemaVersion;
33 import org.onap.aai.schemaservice.nodeschema.SchemaVersions;
34
35 /**
36  * <b>AAIConfigTranslator</b> is responsible for looking at the
37  * schema files and edge files based on the available versions
38  * Also has the ability to exclude them based on the node.exclusion.pattern
39  */
40 public class AAIConfigTranslator extends ConfigTranslator {
41
42     private static final String FILESEP =
43         (System.getProperty("file.separator") == null) ? "/" : System.getProperty("file.separator");
44
45     public AAIConfigTranslator(SchemaLocationsBean bean, SchemaVersions schemaVersions) {
46         super(bean, schemaVersions);
47     }
48
49     /*
50      * (non-Javadoc)
51      * 
52      * @see org.onap.aai.setup.ConfigTranslator#getNodeFiles()
53      */
54     @Override
55     public Map<SchemaVersion, List<String>> getNodeFiles() {
56
57         Map<SchemaVersion, List<String>> files = new TreeMap<>();
58         for (SchemaVersion v : schemaVersions.getVersions()) {
59             List<String> container = getVersionNodeFiles(v);
60             files.put(v, container);
61         }
62
63         return files;
64     }
65
66     private List<String> getVersionNodeFiles(SchemaVersion v) {
67
68         return getVersionFiles(bean.getNodeDirectory(), v,
69             () -> bean.getNodesInclusionPattern().stream(),
70             () -> bean.getNodesExclusionPattern().stream());
71     }
72
73     /*
74      * (non-Javadoc)
75      * 
76      * @see org.onap.aai.setup.ConfigTranslator#getEdgeFiles()
77      */
78     @Override
79     public Map<SchemaVersion, List<String>> getEdgeFiles() {
80
81         Map<SchemaVersion, List<String>> files = new TreeMap<>();
82         for (SchemaVersion v : schemaVersions.getVersions()) {
83             List<String> container = getVersionEdgeFiles(v);
84             files.put(v, container);
85         }
86
87         return files;
88     }
89
90     private List<String> getVersionEdgeFiles(SchemaVersion v) {
91
92         return getVersionFiles(bean.getEdgeDirectory(), v,
93             () -> bean.getEdgesInclusionPattern().stream(),
94             () -> bean.getEdgesExclusionPattern().stream());
95     }
96
97     private List<String> getVersionFiles(String startDirectory, SchemaVersion schemaVersion,
98         Supplier<Stream<String>> inclusionPattern, Supplier<Stream<String>> exclusionPattern) {
99
100         List<String> container;
101         final String directoryName = startDirectory + FILESEP + schemaVersion.toString() + FILESEP;
102
103         container = Arrays.stream(new File(directoryName).listFiles()).map(File::getName)
104             .filter(name -> inclusionPattern.get().anyMatch(name::matches))
105             .map(name -> directoryName + name)
106             .filter(name -> exclusionPattern.get().noneMatch(name::matches))
107             .collect(Collectors.toList());
108
109         return container;
110     }
111 }