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