2 * Copyright © 2016-2017 European Support Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.openecomp.sdc.validation.impl.validators;
19 import org.apache.commons.collections4.CollectionUtils;
20 import org.openecomp.core.utilities.CommonMethods;
21 import org.openecomp.core.validation.ErrorMessageCode;
22 import org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder;
23 import org.openecomp.core.validation.types.GlobalValidationContext;
24 import org.openecomp.sdc.common.errors.Messages;
25 import org.openecomp.sdc.common.utils.SdcCommon;
26 import org.openecomp.sdc.datatypes.error.ErrorLevel;
27 import org.openecomp.sdc.heat.datatypes.manifest.FileData;
28 import org.openecomp.sdc.heat.datatypes.manifest.ManifestContent;
29 import org.openecomp.sdc.heat.datatypes.model.HeatOrchestrationTemplate;
30 import org.openecomp.sdc.heat.datatypes.model.HeatResourcesTypes;
31 import org.openecomp.sdc.heat.datatypes.model.ResourceReferenceFunctions;
32 import org.openecomp.sdc.heat.services.HeatStructureUtil;
33 import org.openecomp.sdc.heat.services.manifest.ManifestUtil;
34 import org.openecomp.sdc.logging.api.Logger;
35 import org.openecomp.sdc.logging.api.LoggerFactory;
36 import org.openecomp.sdc.validation.Validator;
37 import org.openecomp.sdc.validation.util.ValidationUtil;
39 import java.util.HashSet;
43 public class SharedResourceGuideLineValidator implements Validator {
44 private static final Logger LOGGER = LoggerFactory.getLogger(
45 SharedResourceGuideLineValidator.class);
46 private static final ErrorMessageCode ERROR_CODE_SRG_1 = new ErrorMessageCode("SRG1");
47 private static final ErrorMessageCode ERROR_CODE_SRG_2 = new ErrorMessageCode("SRG2");
48 private static final ErrorMessageCode ERROR_CODE_SRG_3 = new ErrorMessageCode("SRG3");
49 private static final ErrorMessageCode ERROR_CODE_SRG_4 = new ErrorMessageCode("SRG4");
50 private static final ErrorMessageCode ERROR_CODE_SRG_5 = new ErrorMessageCode("SRG5");
51 private static final ErrorMessageCode ERROR_CODE_SRG_6 = new ErrorMessageCode("SRG6");
54 public void validate(GlobalValidationContext globalContext) {
55 ManifestContent manifestContent;
57 manifestContent = ValidationUtil.validateManifest(globalContext);
58 } catch (Exception exception) {
59 LOGGER.error("Fialed to check Validation PreCondition ",exception);
63 Set<String> baseFiles = validateManifest(manifestContent, globalContext);
65 Map<String, FileData.Type> fileTypeMap = ManifestUtil.getFileTypeMap(manifestContent);
66 globalContext.getFiles().stream()
67 .filter(fileName -> FileData
68 .isHeatFile(fileTypeMap.get(fileName)))
69 .forEach(fileName -> validate(fileName,
70 fileTypeMap, baseFiles, globalContext));
75 private Set<String> validateManifest(ManifestContent manifestContent,
76 GlobalValidationContext globalContext) {
77 Set<String> baseFiles = ManifestUtil.getBaseFiles(manifestContent);
78 if (baseFiles == null || baseFiles.isEmpty()) {
79 globalContext.addMessage(
80 SdcCommon.MANIFEST_NAME,
82 ErrorMessagesFormatBuilder
83 .getErrorWithParameters(ERROR_CODE_SRG_3,Messages
84 .MISSIN_BASE_HEAT_FILE.getErrorMessage()));
85 } else if (baseFiles.size() > 1) {
86 String baseFileList = getElementListAsString(baseFiles);
87 globalContext.addMessage(
88 SdcCommon.MANIFEST_NAME,
90 ErrorMessagesFormatBuilder
91 .getErrorWithParameters(ERROR_CODE_SRG_4,Messages
92 .MULTI_BASE_HEAT_FILE.getErrorMessage(),
98 private static String getElementListAsString(Set<String> elementCollection) {
100 + CommonMethods.collectionToCommaSeparatedString(elementCollection)
104 private void validate(String fileName, Map<String, FileData.Type> fileTypeMap,
105 Set<String> baseFiles, GlobalValidationContext globalContext) {
106 globalContext.setMessageCode(ERROR_CODE_SRG_5);
107 HeatOrchestrationTemplate
108 heatOrchestrationTemplate = ValidationUtil
109 .checkHeatOrchestrationPreCondition(fileName, globalContext);
110 if (heatOrchestrationTemplate == null) {
114 validateBaseFile(fileName, baseFiles, heatOrchestrationTemplate, globalContext);
115 validateHeatVolumeFile(fileName, fileTypeMap, heatOrchestrationTemplate, globalContext);
119 private void validateBaseFile(String fileName, Set<String> baseFiles,
120 HeatOrchestrationTemplate heatOrchestrationTemplate,
121 GlobalValidationContext globalContext) {
123 if (baseFiles == null || !baseFiles.contains(fileName)) {
127 //if no resources exist return
128 if (heatOrchestrationTemplate.getResources() == null
129 || heatOrchestrationTemplate.getResources().size() == 0) {
133 Set<String> expectedExposedResources = new HashSet<>();
134 heatOrchestrationTemplate.getResources()
137 .filter(entry -> ValidationUtil.isExpectedToBeExposed(entry.getValue().getType()))
138 .forEach(entry -> expectedExposedResources.add(entry.getKey()));
139 Set<String> actualExposedResources = new HashSet<>();
141 if (heatOrchestrationTemplate.getOutputs() != null) {
142 globalContext.setMessageCode(ERROR_CODE_SRG_6);
143 heatOrchestrationTemplate.getOutputs().entrySet()
145 .filter(entry -> isPropertyValueGetResource(fileName, entry.getValue().getValue(),
147 .forEach(entry -> actualExposedResources.add(
148 getResourceIdFromPropertyValue(fileName, entry.getValue().getValue(),
151 ValidationUtil.removeExposedResourcesCalledByGetResource(fileName, actualExposedResources,
152 heatOrchestrationTemplate, globalContext);
154 actualExposedResources.forEach(expectedExposedResources::remove);
156 if (CollectionUtils.isNotEmpty(expectedExposedResources)) {
157 expectedExposedResources
159 .forEach(name -> globalContext.addMessage(
161 ErrorLevel.WARNING, ErrorMessagesFormatBuilder
162 .getErrorWithParameters(ERROR_CODE_SRG_1,
163 Messages.RESOURCE_NOT_DEFINED_IN_OUTPUT.getErrorMessage(),
168 private void validateHeatVolumeFile(String fileName, Map<String, FileData.Type> fileTypeMap,
169 HeatOrchestrationTemplate heatOrchestrationTemplate,
170 GlobalValidationContext globalContext) {
171 //if not heat volume return
172 if (!fileTypeMap.get(fileName).equals(FileData.Type.HEAT_VOL)) {
176 //if no resources exist return
177 if (heatOrchestrationTemplate.getResources() == null
178 || heatOrchestrationTemplate.getResources().size() == 0) {
182 Set<String> expectedExposedResources = new HashSet<>();
183 Set<String> actualExposedResources = new HashSet<>();
184 heatOrchestrationTemplate.getResources()
187 .filter(entry -> entry.getValue().getType()
188 .equals(HeatResourcesTypes.CINDER_VOLUME_RESOURCE_TYPE.getHeatResource()))
189 .forEach(entry -> expectedExposedResources.add(entry.getKey()));
191 if (heatOrchestrationTemplate.getOutputs() != null) {
192 globalContext.setMessageCode(ERROR_CODE_SRG_6);
193 heatOrchestrationTemplate.getOutputs().entrySet()
195 .filter(entry -> isPropertyValueGetResource(fileName, entry.getValue().getValue(),
197 .forEach(entry -> actualExposedResources.add(
198 getResourceIdFromPropertyValue(fileName, entry.getValue().getValue(),
202 actualExposedResources.forEach(expectedExposedResources::remove);
204 if (CollectionUtils.isNotEmpty(expectedExposedResources)) {
205 expectedExposedResources
207 .forEach(name -> globalContext.addMessage(
209 ErrorLevel.WARNING, ErrorMessagesFormatBuilder
210 .getErrorWithParameters(ERROR_CODE_SRG_2,
211 Messages.VOLUME_HEAT_NOT_EXPOSED.getErrorMessage(), name)));
216 private boolean isPropertyValueGetResource(String filename, Object value,
217 GlobalValidationContext globalContext) {
218 Set<String> referenceValues = HeatStructureUtil.getReferencedValuesByFunctionName(filename,
219 ResourceReferenceFunctions.GET_RESOURCE.getFunction(), value, globalContext);
220 return referenceValues != null && (CollectionUtils.isNotEmpty(referenceValues));
223 private String getResourceIdFromPropertyValue(String filename, Object value,
224 GlobalValidationContext globalContext) {
225 Set<String> referenceValues = HeatStructureUtil.getReferencedValuesByFunctionName(filename,
226 ResourceReferenceFunctions.GET_RESOURCE.getFunction(), value, globalContext);
227 if (referenceValues != null && CollectionUtils.isNotEmpty(referenceValues)) {
228 return (String) referenceValues.toArray()[0];