1 package org.openecomp.sdc.validation.util;
4 import org.apache.commons.collections4.CollectionUtils;
5 import org.openecomp.core.utilities.json.JsonUtil;
6 import org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder;
7 import org.openecomp.core.validation.types.GlobalValidationContext;
8 import org.openecomp.sdc.common.errors.Messages;
9 import org.openecomp.sdc.common.errors.SdcRuntimeException;
10 import org.openecomp.sdc.common.utils.SdcCommon;
11 import org.openecomp.sdc.datatypes.error.ErrorLevel;
12 import org.openecomp.sdc.heat.datatypes.manifest.ManifestContent;
13 import org.openecomp.sdc.heat.datatypes.model.Environment;
14 import org.openecomp.sdc.heat.datatypes.model.HeatOrchestrationTemplate;
15 import org.openecomp.sdc.heat.datatypes.model.HeatResourcesTypes;
16 import org.openecomp.sdc.heat.datatypes.model.Resource;
17 import org.openecomp.sdc.heat.datatypes.model.ResourceReferenceFunctions;
18 import org.openecomp.sdc.heat.services.HeatStructureUtil;
19 import org.openecomp.sdc.logging.api.Logger;
20 import org.openecomp.sdc.logging.api.LoggerFactory;
21 import org.openecomp.sdc.logging.context.impl.MdcDataDebugMessage;
22 import org.openecomp.sdc.logging.context.impl.MdcDataErrorMessage;
23 import org.openecomp.sdc.logging.types.LoggerConstants;
24 import org.openecomp.sdc.logging.types.LoggerErrorCode;
25 import org.openecomp.sdc.logging.types.LoggerErrorDescription;
26 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
27 import org.openecomp.sdc.tosca.services.YamlUtil;
29 import java.io.InputStream;
31 import java.util.Objects;
32 import java.util.Optional;
34 import java.util.regex.Pattern;
36 import static java.util.Objects.nonNull;
38 public class ValidationUtil {
40 private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
41 private final static Logger log = (Logger) LoggerFactory.getLogger(ValidationUtil.class.getName());
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 log.debug("",exception);
144 mdcDataDebugMessage.debugExitMessage("file", envFileName);
147 mdcDataDebugMessage.debugExitMessage("file", envFileName);
151 public static boolean validateMapPropertyValue(String fileName,
152 Map.Entry<String, Resource> resourceEntry,
153 GlobalValidationContext globalContext,
154 String propertyName, Object nameValue,
155 String[] regexList) {
157 mdcDataDebugMessage.debugEntryMessage("file", fileName);
159 String propertyValue = getWantedNameFromPropertyValueGetParam(nameValue);
160 if (nonNull(propertyValue)) {
161 if (!evalPattern(propertyValue, regexList)) {
162 globalContext.addMessage(
165 ErrorMessagesFormatBuilder.getErrorWithParameters(globalContext.getMessageCode(),
166 Messages.PARAMETER_NAME_NOT_ALIGNED_WITH_GUIDELINES.getErrorMessage(),
167 getMessagePartAccordingToResourceType(resourceEntry), propertyName, propertyValue,
168 resourceEntry.getKey()),
169 LoggerTragetServiceName.VALIDATE_IMAGE_AND_FLAVOR_NAME,
170 LoggerErrorDescription.NAME_NOT_ALIGNED_WITH_GUIDELINES);
171 mdcDataDebugMessage.debugExitMessage("file", fileName);
176 mdcDataDebugMessage.debugExitMessage("file", fileName);
180 public static ManifestContent validateManifest(GlobalValidationContext globalContext) {
181 Optional<InputStream> manifest = globalContext.getFileContent(SdcCommon.MANIFEST_NAME);
182 if (!manifest.isPresent()) {
183 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_API,
184 LoggerTragetServiceName.VALIDATE_MANIFEST_CONTENT, ErrorLevel.ERROR.name(),
185 LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.MISSING_FILE);
186 throw new RuntimeException("Can't load manifest file for Heat Validator");
188 ManifestContent manifestContent;
190 manifestContent = JsonUtil.json2Object(manifest.get(), ManifestContent.class);
191 } catch (Exception exception) {
192 log.debug("",exception);
193 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_API,
194 LoggerTragetServiceName.VALIDATE_MANIFEST_CONTENT, ErrorLevel.ERROR.name(),
195 LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.INVALID_MANIFEST);
196 throw new SdcRuntimeException("Can't load manifest file for Heat Validator");
199 return manifestContent;
202 public static String getParserExceptionReason(Exception exception) {
205 if (exception.getCause() != null && exception.getCause().getCause() != null) {
206 reason = exception.getCause().getCause().getMessage();
207 } else if (exception.getCause() != null) {
208 reason = exception.getCause().getMessage();
210 reason = Messages.GENERAL_HEAT_PARSER_ERROR.getErrorMessage();
215 public static HeatOrchestrationTemplate checkHeatOrchestrationPreCondition(String fileName,
216 GlobalValidationContext globalContext) {
218 mdcDataDebugMessage.debugEntryMessage("file", fileName);
220 HeatOrchestrationTemplate heatOrchestrationTemplate;
222 Optional<InputStream> fileContent = globalContext.getFileContent(fileName);
223 if (fileContent.isPresent()) {
224 heatOrchestrationTemplate =
225 new YamlUtil().yamlToObject(fileContent.get(), HeatOrchestrationTemplate.class);
227 heatOrchestrationTemplate = null;
229 } catch (Exception exception) {
230 globalContext.addMessage(fileName, ErrorLevel.ERROR, ErrorMessagesFormatBuilder
231 .getErrorWithParameters(globalContext.getMessageCode(),
232 Messages.INVALID_HEAT_FORMAT_REASON.getErrorMessage()
233 , getParserExceptionReason(exception)),
234 LoggerTragetServiceName.VALIDATE_HEAT_FORMAT,
235 LoggerErrorDescription.INVALID_HEAT_FORMAT);
236 mdcDataDebugMessage.debugExitMessage("file", fileName);
239 mdcDataDebugMessage.debugExitMessage("file", fileName);
240 return heatOrchestrationTemplate;