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