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