1 package org.openecomp.sdc.validation.util;
4 import org.apache.commons.collections4.CollectionUtils;
5 import org.openecomp.core.utilities.CommonMethods;
6 import org.openecomp.core.utilities.json.JsonUtil;
7 import org.openecomp.core.utilities.yaml.YamlUtil;
8 import org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder;
9 import org.openecomp.core.validation.types.GlobalValidationContext;
10 import org.openecomp.sdc.common.errors.Messages;
11 import org.openecomp.sdc.common.utils.SdcCommon;
12 import org.openecomp.sdc.datatypes.configuration.ImplementationConfiguration;
13 import org.openecomp.sdc.datatypes.error.ErrorLevel;
14 import org.openecomp.sdc.heat.datatypes.manifest.ManifestContent;
15 import org.openecomp.sdc.heat.datatypes.model.Environment;
16 import org.openecomp.sdc.heat.datatypes.model.HeatOrchestrationTemplate;
17 import org.openecomp.sdc.heat.datatypes.model.HeatResourcesTypes;
18 import org.openecomp.sdc.heat.datatypes.model.Resource;
19 import org.openecomp.sdc.heat.datatypes.model.ResourceReferenceFunctions;
20 import org.openecomp.sdc.heat.services.HeatStructureUtil;
21 import org.openecomp.sdc.heat.services.manifest.ManifestUtil;
22 import org.openecomp.sdc.logging.context.impl.MdcDataDebugMessage;
23 import org.openecomp.sdc.logging.context.impl.MdcDataErrorMessage;
24 import org.openecomp.sdc.logging.types.LoggerConstants;
25 import org.openecomp.sdc.logging.types.LoggerErrorCode;
26 import org.openecomp.sdc.logging.types.LoggerErrorDescription;
27 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
28 import org.openecomp.sdc.validation.ResourceValidator;
30 import java.io.InputStream;
32 import java.util.Objects;
33 import java.util.Optional;
35 import java.util.regex.Pattern;
37 import static java.util.Objects.nonNull;
39 public class ValidationUtil {
41 private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
43 public static void removeExposedResourcesCalledByGetResource(String fileName,
44 Set<String> actualExposedResources,
45 HeatOrchestrationTemplate
46 heatOrchestrationTemplate,
47 GlobalValidationContext globalContext) {
48 Map<String, Resource> resourcesMap = heatOrchestrationTemplate.getResources();
50 for (Map.Entry<String, Resource> resourceEntry : resourcesMap.entrySet()) {
51 Set<String> referencedResources =
52 HeatStructureUtil.getReferencedValuesByFunctionName(fileName, ResourceReferenceFunctions
54 .getFunction(), resourceEntry.getValue().getProperties(), globalContext);
56 removeExposedResourcesCalledByGetResource(referencedResources, actualExposedResources,
61 private static void removeExposedResourcesCalledByGetResource(Set<String> referencedResources,
63 actualExposedResources,
64 Map<String, Resource> resourcesMap) {
65 for (String referencedResourceName : referencedResources) {
66 Resource currResource = resourcesMap.get(referencedResourceName);
67 if (Objects.nonNull(currResource)) {
68 if (isExpectedToBeExposed(currResource.getType())) {
69 actualExposedResources.add(referencedResourceName);
75 public static boolean isExpectedToBeExposed(String type) {
76 return HeatResourcesTypes.isResourceExpectedToBeExposed(type);
79 public static String getWantedNameFromPropertyValueGetParam(Object value) {
80 Set<String> paramName = HeatStructureUtil
81 .getReferencedValuesByFunctionName(null, ResourceReferenceFunctions.GET_PARAM.getFunction(),
83 if (paramName != null && CollectionUtils.isNotEmpty(paramName)) {
84 return (String) paramName.toArray()[0];
89 public static boolean evalPattern(Object paramVal, String[] regexList) {
91 if (paramVal instanceof String) {
92 value = ((String) paramVal);
94 if (paramVal instanceof Integer) {
95 value = paramVal.toString();
97 return evalPattern(value, regexList);
100 private static boolean evalPattern(String paramVal, String[] regexList) {
102 for (String regex : regexList) {
103 if (Pattern.matches(regex, paramVal)) {
111 public static String getMessagePartAccordingToResourceType(Map.Entry<String, Resource>
113 HeatResourcesTypes resourcesType =
114 HeatResourcesTypes.findByHeatResource(resourceEntry.getValue().getType());
115 if (resourcesType == HeatResourcesTypes.NOVA_SERVER_RESOURCE_TYPE) {
117 } else if (resourcesType == HeatResourcesTypes.CONTRAIL_SERVICE_TEMPLATE) {
118 return "Service Template";
119 } else if (resourcesType == HeatResourcesTypes.CONTRAIL_SERVICE_INSTANCE) {
120 return "Service Instance";
126 public static Environment validateEnvContent(String envFileName,
127 GlobalValidationContext globalContext) {
129 mdcDataDebugMessage.debugEntryMessage("file", envFileName);
131 Environment envContent;
133 Optional<InputStream> fileContent = globalContext.getFileContent(envFileName);
134 if (fileContent.isPresent()) {
135 envContent = new YamlUtil().yamlToObject(fileContent.get(), Environment.class);
137 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_API,
138 LoggerTragetServiceName.VALIDATE_ENV_FILE, ErrorLevel.ERROR.name(),
139 LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.EMPTY_FILE);
140 throw new Exception("The file '" + envFileName + "' has no content");
142 } catch (Exception exception) {
143 mdcDataDebugMessage.debugExitMessage("file", envFileName);
146 mdcDataDebugMessage.debugExitMessage("file", envFileName);
150 public static boolean validateMapPropertyValue(String fileName,
151 Map.Entry<String, Resource> resourceEntry,
152 GlobalValidationContext globalContext,
153 String propertyName, Object nameValue,
154 String[] regexList) {
156 mdcDataDebugMessage.debugEntryMessage("file", fileName);
158 String propertyValue = getWantedNameFromPropertyValueGetParam(nameValue);
159 if (nonNull(propertyValue)) {
160 if (!evalPattern(propertyValue, regexList)) {
161 globalContext.addMessage(
164 ErrorMessagesFormatBuilder.getErrorWithParameters(
165 Messages.PARAMETER_NAME_NOT_ALIGNED_WITH_GUIDELINES.getErrorMessage(),
166 getMessagePartAccordingToResourceType(resourceEntry), propertyName, propertyValue,
167 resourceEntry.getKey()),
168 LoggerTragetServiceName.VALIDATE_IMAGE_AND_FLAVOR_NAME,
169 LoggerErrorDescription.NAME_NOT_ALIGNED_WITH_GUIDELINES);
170 mdcDataDebugMessage.debugExitMessage("file", fileName);
175 mdcDataDebugMessage.debugExitMessage("file", fileName);
179 public static ManifestContent checkValidationPreCondition(GlobalValidationContext globalContext) {
180 Optional<InputStream> manifest = globalContext.getFileContent(SdcCommon.MANIFEST_NAME);
181 if (!manifest.isPresent()) {
182 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_API,
183 LoggerTragetServiceName.VALIDATE_MANIFEST_CONTENT, ErrorLevel.ERROR.name(),
184 LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.MISSING_FILE);
185 throw new RuntimeException("Can't load manifest file for Heat Validator");
187 ManifestContent manifestContent;
189 manifestContent = JsonUtil.json2Object(manifest.get(), ManifestContent.class);
190 } catch (Exception exception) {
191 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_API,
192 LoggerTragetServiceName.VALIDATE_MANIFEST_CONTENT, ErrorLevel.ERROR.name(),
193 LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.INVALID_MANIFEST);
194 throw new RuntimeException("Can't load manifest file for Heat Validator");
197 return manifestContent;
200 public static String getParserExceptionReason(Exception exception) {
203 if (exception.getCause() != null && exception.getCause().getCause() != null) {
204 reason = exception.getCause().getCause().getMessage();
205 } else if (exception.getCause() != null) {
206 reason = exception.getCause().getMessage();
208 reason = Messages.GENERAL_HEAT_PARSER_ERROR.getErrorMessage();
213 public static HeatOrchestrationTemplate checkHeatOrchestrationPreCondition(String fileName,
214 GlobalValidationContext globalContext) {
216 mdcDataDebugMessage.debugEntryMessage("file", fileName);
218 HeatOrchestrationTemplate heatOrchestrationTemplate;
220 Optional<InputStream> fileContent = globalContext.getFileContent(fileName);
221 if (fileContent.isPresent()) {
222 heatOrchestrationTemplate =
223 new YamlUtil().yamlToObject(fileContent.get(), HeatOrchestrationTemplate.class);
225 heatOrchestrationTemplate = null;
227 } catch (Exception exception) {
228 globalContext.addMessage(fileName, ErrorLevel.ERROR, ErrorMessagesFormatBuilder
229 .getErrorWithParameters(Messages.INVALID_HEAT_FORMAT_REASON.getErrorMessage(),
230 getParserExceptionReason(exception)),
231 LoggerTragetServiceName.VALIDATE_HEAT_FORMAT,
232 LoggerErrorDescription.INVALID_HEAT_FORMAT);
233 mdcDataDebugMessage.debugExitMessage("file", fileName);
236 mdcDataDebugMessage.debugExitMessage("file", fileName);
237 return heatOrchestrationTemplate;
240 public static Set<String> validateManifest(ManifestContent manifestContent,
241 GlobalValidationContext globalContext) {
243 mdcDataDebugMessage.debugEntryMessage("file", SdcCommon.MANIFEST_NAME);
245 Set<String> baseFiles = ManifestUtil.getBaseFiles(manifestContent);
246 if (baseFiles == null || baseFiles.size() == 0) {
247 globalContext.addMessage(
248 SdcCommon.MANIFEST_NAME,
250 ErrorMessagesFormatBuilder
251 .getErrorWithParameters(Messages.MISSIN_BASE_HEAT_FILE.getErrorMessage()),
252 LoggerTragetServiceName.VALIDATE_BASE_FILE,
253 LoggerErrorDescription.MISSING_BASE_HEAT);
254 } else if (baseFiles.size() > 1) {
255 String baseFileList = getElementListAsString(baseFiles);
256 globalContext.addMessage(
257 SdcCommon.MANIFEST_NAME,
259 ErrorMessagesFormatBuilder
260 .getErrorWithParameters(Messages.MULTI_BASE_HEAT_FILE.getErrorMessage(),
262 LoggerTragetServiceName.VALIDATE_BASE_FILE,
263 LoggerErrorDescription.MULTI_BASE_HEAT);
266 mdcDataDebugMessage.debugExitMessage("file", SdcCommon.MANIFEST_NAME);
270 private static String getElementListAsString(Set<String> elementCollection) {
273 + CommonMethods.collectionToCommaSeparatedString(elementCollection)