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.tosca.services.YamlUtil;
23 import java.io.InputStream;
25 import java.util.Objects;
26 import java.util.Optional;
28 import java.util.regex.Pattern;
30 import static java.util.Objects.nonNull;
32 public class ValidationUtil {
33 private static final Logger LOG = LoggerFactory.getLogger(ValidationUtil.class.getName());
35 private ValidationUtil(){}
37 public static void removeExposedResourcesCalledByGetResource(String fileName,
38 Set<String> actualExposedResources,
39 HeatOrchestrationTemplate
40 heatOrchestrationTemplate,
41 GlobalValidationContext globalContext) {
42 Map<String, Resource> resourcesMap = heatOrchestrationTemplate.getResources();
44 for (Map.Entry<String, Resource> resourceEntry : resourcesMap.entrySet()) {
45 Set<String> referencedResources =
46 HeatStructureUtil.getReferencedValuesByFunctionName(fileName, ResourceReferenceFunctions
48 .getFunction(), resourceEntry.getValue().getProperties(), globalContext);
50 removeExposedResourcesCalledByGetResource(referencedResources, actualExposedResources,
55 private static void removeExposedResourcesCalledByGetResource(Set<String> referencedResources,
57 actualExposedResources,
58 Map<String, Resource> resourcesMap) {
59 for (String referencedResourceName : referencedResources) {
60 Resource currResource = resourcesMap.get(referencedResourceName);
61 if (Objects.nonNull(currResource) && isExpectedToBeExposed(currResource.getType())) {
62 actualExposedResources.add(referencedResourceName);
67 public static boolean isExpectedToBeExposed(String type) {
68 return HeatResourcesTypes.isResourceExpectedToBeExposed(type);
71 public static String getWantedNameFromPropertyValueGetParam(Object value) {
72 Set<String> paramName = HeatStructureUtil
73 .getReferencedValuesByFunctionName(null, ResourceReferenceFunctions.GET_PARAM.getFunction(),
75 if (paramName != null && CollectionUtils.isNotEmpty(paramName)) {
76 return (String) paramName.toArray()[0];
81 public static boolean evalPattern(Object paramVal, String[] regexList) {
83 if (paramVal instanceof String) {
84 value = (String) paramVal;
86 if (paramVal instanceof Integer) {
87 value = paramVal.toString();
89 return evalPattern(value, regexList);
92 private static boolean evalPattern(String paramVal, String[] regexList) {
94 for (String regex : regexList) {
95 if (Pattern.matches(regex, paramVal)) {
103 public static String getMessagePartAccordingToResourceType(Map.Entry<String, Resource>
105 HeatResourcesTypes resourcesType =
106 HeatResourcesTypes.findByHeatResource(resourceEntry.getValue().getType());
107 if (resourcesType == HeatResourcesTypes.NOVA_SERVER_RESOURCE_TYPE) {
109 } else if (resourcesType == HeatResourcesTypes.CONTRAIL_SERVICE_TEMPLATE) {
110 return "Service Template";
111 } else if (resourcesType == HeatResourcesTypes.CONTRAIL_SERVICE_INSTANCE) {
112 return "Service Instance";
118 public static Environment validateEnvContent(String envFileName,
119 GlobalValidationContext globalContext) {
120 Environment envContent;
122 Optional<InputStream> fileContent = globalContext.getFileContent(envFileName);
123 if (fileContent.isPresent()) {
124 envContent = new YamlUtil().yamlToObject(fileContent.get(), Environment.class);
126 throw new Exception("The file '" + envFileName + "' has no content");
128 } catch (Exception exception) {
129 LOG.debug("",exception);
135 public static boolean validateMapPropertyValue(String fileName,
136 Map.Entry<String, Resource> resourceEntry,
137 GlobalValidationContext globalContext,
138 String propertyName, Object nameValue,
139 String[] regexList) {
140 String propertyValue = getWantedNameFromPropertyValueGetParam(nameValue);
141 if (nonNull(propertyValue) && !evalPattern(propertyValue, regexList)) {
142 globalContext.addMessage(
145 ErrorMessagesFormatBuilder.getErrorWithParameters(globalContext.getMessageCode(),
146 Messages.PARAMETER_NAME_NOT_ALIGNED_WITH_GUIDELINES.getErrorMessage(),
147 getMessagePartAccordingToResourceType(resourceEntry), propertyName, propertyValue,
148 resourceEntry.getKey()));
154 public static ManifestContent validateManifest(GlobalValidationContext globalContext) {
155 Optional<InputStream> manifest = globalContext.getFileContent(SdcCommon.MANIFEST_NAME);
156 if (!manifest.isPresent()) {
157 throw new RuntimeException("Can't load manifest file for Heat Validator");
159 ManifestContent manifestContent;
161 manifestContent = JsonUtil.json2Object(manifest.get(), ManifestContent.class);
162 } catch (Exception exception) {
163 LOG.debug("",exception);
164 throw new SdcRuntimeException("Can't load manifest file for Heat Validator");
167 return manifestContent;
170 public static String getParserExceptionReason(Exception exception) {
173 if (exception.getCause() != null && exception.getCause().getCause() != null) {
174 reason = exception.getCause().getCause().getMessage();
175 } else if (exception.getCause() != null) {
176 reason = exception.getCause().getMessage();
178 reason = Messages.GENERAL_HEAT_PARSER_ERROR.getErrorMessage();
183 public static HeatOrchestrationTemplate checkHeatOrchestrationPreCondition(String fileName,
184 GlobalValidationContext globalContext) {
185 HeatOrchestrationTemplate heatOrchestrationTemplate;
187 Optional<InputStream> fileContent = globalContext.getFileContent(fileName);
188 if (fileContent.isPresent()) {
189 heatOrchestrationTemplate =
190 new YamlUtil().yamlToObject(fileContent.get(), HeatOrchestrationTemplate.class);
192 heatOrchestrationTemplate = null;
194 } catch (Exception exception) {
195 globalContext.addMessage(fileName, ErrorLevel.ERROR, ErrorMessagesFormatBuilder
196 .getErrorWithParameters(globalContext.getMessageCode(),
197 Messages.INVALID_HEAT_FORMAT_REASON.getErrorMessage()
198 , getParserExceptionReason(exception)));
201 return heatOrchestrationTemplate;