Only load SchemaService related beans in aai-common when schema.translator.list=schem...
[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  * Modifications Copyright © 2024 DEUTSCHE TELEKOM AG.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *    http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.aai.setup;
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.Objects;
30 import java.util.TreeMap;
31 import java.util.function.Supplier;
32 import java.util.stream.Collectors;
33 import java.util.stream.Stream;
34
35 import org.springframework.stereotype.Component;
36
37 /**
38  * <b>AAIConfigTranslator</b> is responsible for looking at the
39  * schema files and edge files based on the available versions
40  * Also has the ability to exclude them based on the node.exclusion.pattern
41  */
42 @Component
43 public class AAIConfigTranslator extends ConfigTranslator {
44
45     public AAIConfigTranslator(SchemaLocationsBean schemaLocationsBean, SchemaConfigVersions schemaConfigVersions) {
46         super(schemaLocationsBean, schemaConfigVersions);
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         return getVersionFiles(schemaLocationsBean.getNodeDirectory(), v, () -> schemaLocationsBean.getNodesInclusionPattern().stream(),
68                 () -> schemaLocationsBean.getNodesExclusionPattern().stream());
69     }
70
71     /*
72      * (non-Javadoc)
73      * 
74      * @see org.onap.aai.setup.ConfigTranslator#getEdgeFiles()
75      */
76     @Override
77     public Map<SchemaVersion, List<String>> getEdgeFiles() {
78
79         Map<SchemaVersion, List<String>> files = new TreeMap<>();
80         for (SchemaVersion v : schemaVersions.getVersions()) {
81             List<String> container = getVersionEdgeFiles(v);
82             files.put(v, container);
83         }
84
85         return files;
86     }
87
88     private List<String> getVersionEdgeFiles(SchemaVersion v) {
89
90         return getVersionFiles(schemaLocationsBean.getEdgeDirectory(), v, () -> schemaLocationsBean.getEdgesInclusionPattern().stream(),
91                 () -> schemaLocationsBean.getEdgesExclusionPattern().stream());
92     }
93
94     private List<String> getVersionFiles(String startDirectory, SchemaVersion schemaVersion,
95            Supplier<Stream<String>> inclusionPattern, Supplier<Stream<String>> exclusionPattern) {
96
97        final File versionDirectory = new File(startDirectory + "/" + schemaVersion.toString());
98        final List<String> container = Arrays.stream(versionDirectory.listFiles())
99            .filter(Objects::nonNull)    
100            .map(File::getName)
101            .filter(versionFileName -> inclusionPattern
102                .get()
103                .anyMatch(versionFileName::matches))
104            .map(versionFileName -> versionDirectory.getAbsolutePath() + "/" + versionFileName)
105            .filter(versionFilePath -> exclusionPattern
106                .get()
107                .noneMatch(versionFilePath::matches))
108            .collect(Collectors.toList());
109
110         return container;
111     }
112 }