re base code
[sdc.git] / openecomp-be / lib / openecomp-sdc-validation-lib / openecomp-sdc-validation-impl / src / main / java / org / openecomp / sdc / validation / impl / validators / ForbiddenResourceGuideLineValidator.java
1 /*
2  * Copyright © 2018 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15 */
16
17 package org.openecomp.sdc.validation.impl.validators;
18
19 import org.apache.commons.collections4.MapUtils;
20 import org.openecomp.core.validation.ErrorMessageCode;
21 import org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder;
22 import org.openecomp.core.validation.types.GlobalValidationContext;
23 import org.openecomp.sdc.common.errors.Messages;
24 import org.openecomp.sdc.datatypes.error.ErrorLevel;
25 import org.openecomp.sdc.heat.datatypes.manifest.FileData;
26 import org.openecomp.sdc.heat.datatypes.manifest.ManifestContent;
27 import org.openecomp.sdc.heat.datatypes.model.HeatOrchestrationTemplate;
28 import org.openecomp.sdc.heat.datatypes.model.Resource;
29 import org.openecomp.sdc.heat.services.manifest.ManifestUtil;
30 import org.openecomp.sdc.logging.api.Logger;
31 import org.openecomp.sdc.logging.api.LoggerFactory;
32 import org.openecomp.sdc.validation.Validator;
33 import org.openecomp.sdc.validation.util.ValidationUtil;
34
35 import java.util.HashSet;
36 import java.util.Map;
37 import java.util.Objects;
38 import java.util.Set;
39
40 public class ForbiddenResourceGuideLineValidator implements Validator {
41   private static Set<String> forbiddenResources = new HashSet<>();
42   private static final ErrorMessageCode ERROR_CODE_FRG_1 = new ErrorMessageCode("FRG1");
43   private static final ErrorMessageCode ERROR_CODE_FRG_2 = new ErrorMessageCode("FRG2");
44   private static final ErrorMessageCode ERROR_CODE_FRG_3 = new ErrorMessageCode("FRG3");
45
46   private static final Logger LOGGER =  LoggerFactory
47           .getLogger(ForbiddenResourceGuideLineValidator.class);
48
49   @Override
50   public void init(Map<String, Object> properties) {
51     Map<String, Map<String, Object>> forbiddenResourcesMap =
52         (Map<String, Map<String, Object>>) properties.get("forbiddenResourceTypes");
53
54     forbiddenResourcesMap.entrySet().stream()
55         .filter(entry -> isResourceEnabled(entry.getValue().get("enable")))
56         .forEach(entry -> forbiddenResources.add(entry.getKey()));
57   }
58
59   private boolean isResourceEnabled(Object enableValue) {
60     if (Objects.isNull(enableValue)) {
61       return true;
62     }
63
64     if (enableValue instanceof Boolean) {
65       return (Boolean)enableValue;
66     }
67
68     return Boolean.valueOf((String) enableValue);
69   }
70
71
72   @Override
73   public void validate(GlobalValidationContext globalContext) {
74     ManifestContent manifestContent;
75     try {
76       manifestContent = ValidationUtil.validateManifest(globalContext);
77     } catch (Exception exception) {
78       LOGGER.error("Failed to validate manifest file", exception);
79       return;
80     }
81
82     Map<String, FileData.Type> fileTypeMap = ManifestUtil.getFileTypeMap(manifestContent);
83
84     globalContext.getFiles().stream()
85         .filter(fileName -> FileData
86             .isHeatFile(fileTypeMap.get(fileName)))
87         .forEach(fileName -> validate(fileName, globalContext));
88   }
89
90   private void validate(String fileName, GlobalValidationContext globalContext) {
91     globalContext.setMessageCode(ERROR_CODE_FRG_3);
92     HeatOrchestrationTemplate
93         heatOrchestrationTemplate = ValidationUtil.checkHeatOrchestrationPreCondition(
94         fileName, globalContext);
95     if (heatOrchestrationTemplate == null) {
96       return;
97     }
98
99     validateResourceTypeIsForbidden(fileName, heatOrchestrationTemplate, globalContext);
100   }
101
102   private void validateResourceTypeIsForbidden(String fileName,
103                                                HeatOrchestrationTemplate heatOrchestrationTemplate,
104                                                GlobalValidationContext globalContext) {
105     Map<String, Resource> resourcesMap = heatOrchestrationTemplate.getResources();
106     if (MapUtils.isEmpty(resourcesMap)) {
107       return;
108     }
109
110     for (Map.Entry<String, Resource> resourceEntry : resourcesMap.entrySet()) {
111       String resourceType = resourceEntry.getValue().getType();
112       if (Objects.isNull(resourceType)) {
113         globalContext.addMessage(fileName, ErrorLevel.WARNING, ErrorMessagesFormatBuilder
114                 .getErrorWithParameters(ERROR_CODE_FRG_1,
115                         Messages.INVALID_RESOURCE_TYPE.getErrorMessage(),"null",
116                     resourceEntry.getKey()));
117       } else {
118         if (isResourceForbidden(resourceType)) {
119            globalContext.addMessage(
120               fileName,
121               ErrorLevel.WARNING,
122               ErrorMessagesFormatBuilder
123                   .getErrorWithParameters(ERROR_CODE_FRG_2, Messages.FORBIDDEN_RESOURCE_IN_USE
124                           .getErrorMessage(),
125                       resourceType, resourceEntry.getKey()));
126         }
127       }
128     }
129   }
130
131   private boolean isResourceForbidden(String resourceType) {
132     return forbiddenResources.contains(resourceType);
133   }
134 }