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