4467bd1311a3261f40427dfd55969faa01688c3e
[sdc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2021 Nokia 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.openecomp.sdc.validation.impl.validators;
22
23 import org.openecomp.core.validation.types.GlobalValidationContext;
24 import org.openecomp.sdc.heat.datatypes.manifest.FileData;
25 import org.openecomp.sdc.heat.datatypes.manifest.ManifestContent;
26 import org.openecomp.sdc.heat.services.manifest.ManifestUtil;
27 import org.openecomp.sdc.validation.util.ValidationUtil;
28
29 import java.util.Map;
30 import java.util.Set;
31 import java.util.function.Predicate;
32 import java.util.stream.Collectors;
33
34 class GlobalContextUtil {
35
36     private GlobalContextUtil() {}
37
38     static Set<String> findPmDictionaryFiles(GlobalValidationContext globalContext) {
39         if (isManifestMissing(globalContext)) {
40             return Set.of();
41         }
42
43         Map<String, FileData.Type> filesWithTypes = readAllFilesWithTypes(globalContext);
44         return filterPmDictionaryFiles(filesWithTypes);
45     }
46
47     private static boolean isManifestMissing(GlobalValidationContext globalContext) {
48         return globalContext.getFileContent("MANIFEST.json")
49                 .isEmpty();
50     }
51
52     private static Set<String> filterPmDictionaryFiles(Map<String, FileData.Type> filesWithTypes) {
53         return filesWithTypes.entrySet().stream()
54                 .filter(isPmDictionaryType())
55                 .map(Map.Entry::getKey)
56                 .collect(Collectors.toSet());
57     }
58
59     private static Map<String, FileData.Type> readAllFilesWithTypes(GlobalValidationContext globalContext) {
60         ManifestContent manifestContent = ValidationUtil.validateManifest(globalContext);
61         return ManifestUtil.getFileTypeMap(manifestContent);
62     }
63
64     private static Predicate<Map.Entry<String, FileData.Type>> isPmDictionaryType() {
65         return entry -> entry.getValue()
66                 .equals(FileData.Type.PM_DICTIONARY);
67     }
68 }