2 * Copyright © 2018 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.util;
19 import static java.util.Objects.nonNull;
21 import java.io.InputStream;
23 import java.util.Objects;
24 import java.util.Optional;
26 import java.util.regex.Pattern;
27 import org.apache.commons.collections4.CollectionUtils;
29 import org.onap.sdc.tosca.services.YamlUtil;
30 import org.openecomp.core.utilities.json.JsonUtil;
31 import org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder;
32 import org.openecomp.core.validation.types.GlobalValidationContext;
33 import org.openecomp.sdc.common.errors.Messages;
34 import org.openecomp.sdc.common.errors.SdcRuntimeException;
35 import org.openecomp.sdc.common.utils.SdcCommon;
36 import org.openecomp.sdc.datatypes.error.ErrorLevel;
37 import org.openecomp.sdc.heat.datatypes.manifest.ManifestContent;
38 import org.openecomp.sdc.heat.datatypes.model.Environment;
39 import org.openecomp.sdc.heat.datatypes.model.HeatOrchestrationTemplate;
40 import org.openecomp.sdc.heat.datatypes.model.HeatResourcesTypes;
41 import org.openecomp.sdc.heat.datatypes.model.Resource;
42 import org.openecomp.sdc.heat.datatypes.model.ResourceReferenceFunctions;
43 import org.openecomp.sdc.heat.services.HeatStructureUtil;
44 import org.openecomp.sdc.logging.api.Logger;
45 import org.openecomp.sdc.logging.api.LoggerFactory;
47 public class ValidationUtil {
48 private static final Logger LOG = LoggerFactory.getLogger(ValidationUtil.class.getName());
50 private ValidationUtil(){}
52 public static void removeExposedResourcesCalledByGetResource(String fileName,
53 Set<String> actualExposedResources,
54 HeatOrchestrationTemplate
55 heatOrchestrationTemplate,
56 GlobalValidationContext globalContext) {
57 Map<String, Resource> resourcesMap = heatOrchestrationTemplate.getResources();
59 for (Map.Entry<String, Resource> resourceEntry : resourcesMap.entrySet()) {
60 Set<String> referencedResources =
61 HeatStructureUtil.getReferencedValuesByFunctionName(fileName, ResourceReferenceFunctions
63 .getFunction(), resourceEntry.getValue().getProperties(), globalContext);
65 removeExposedResourcesCalledByGetResource(referencedResources, actualExposedResources,
70 private static void removeExposedResourcesCalledByGetResource(Set<String> referencedResources,
72 actualExposedResources,
73 Map<String, Resource> resourcesMap) {
74 for (String referencedResourceName : referencedResources) {
75 Resource currResource = resourcesMap.get(referencedResourceName);
76 if (Objects.nonNull(currResource) && isExpectedToBeExposed(currResource.getType())) {
77 actualExposedResources.add(referencedResourceName);
82 public static boolean isExpectedToBeExposed(String type) {
83 return HeatResourcesTypes.isResourceExpectedToBeExposed(type);
86 public static String getWantedNameFromPropertyValueGetParam(Object value) {
87 Set<String> paramName = HeatStructureUtil
88 .getReferencedValuesByFunctionName(null, ResourceReferenceFunctions.GET_PARAM.getFunction(),
90 if (paramName != null && CollectionUtils.isNotEmpty(paramName)) {
91 return (String) paramName.toArray()[0];
96 public static boolean evalPattern(Object paramVal, String[] regexList) {
98 if (paramVal instanceof String) {
99 value = (String) paramVal;
101 if (paramVal instanceof Integer) {
102 value = paramVal.toString();
104 return evalPattern(value, regexList);
107 private static boolean evalPattern(String paramVal, String[] regexList) {
109 for (String regex : regexList) {
110 if (Pattern.matches(regex, paramVal)) {
118 public static String getMessagePartAccordingToResourceType(Map.Entry<String, Resource>
120 HeatResourcesTypes resourcesType =
121 HeatResourcesTypes.findByHeatResource(resourceEntry.getValue().getType());
122 if (resourcesType == HeatResourcesTypes.NOVA_SERVER_RESOURCE_TYPE) {
124 } else if (resourcesType == HeatResourcesTypes.CONTRAIL_SERVICE_TEMPLATE) {
125 return "Service Template";
126 } else if (resourcesType == HeatResourcesTypes.CONTRAIL_SERVICE_INSTANCE) {
127 return "Service Instance";
133 public static Environment validateEnvContent(String envFileName,
134 GlobalValidationContext globalContext) {
135 Environment envContent;
137 Optional<InputStream> fileContent = globalContext.getFileContent(envFileName);
138 if (fileContent.isPresent()) {
139 envContent = new YamlUtil().yamlToObject(fileContent.get(), Environment.class);
141 throw new Exception("The file '" + envFileName + "' has no content");
143 } catch (Exception exception) {
144 LOG.error("Invalid envFile name : " + envFileName, exception);
150 public static boolean validateMapPropertyValue(String fileName,
151 Map.Entry<String, Resource> resourceEntry,
152 GlobalValidationContext globalContext,
153 String propertyName, Object nameValue,
154 String[] regexList) {
155 String propertyValue = getWantedNameFromPropertyValueGetParam(nameValue);
156 if (nonNull(propertyValue) && !evalPattern(propertyValue, regexList)) {
157 globalContext.addMessage(
160 ErrorMessagesFormatBuilder.getErrorWithParameters(globalContext.getMessageCode(),
161 Messages.PARAMETER_NAME_NOT_ALIGNED_WITH_GUIDELINES.getErrorMessage(),
162 getMessagePartAccordingToResourceType(resourceEntry), propertyName, propertyValue,
163 resourceEntry.getKey()));
169 public static ManifestContent validateManifest(GlobalValidationContext globalContext) {
170 Optional<InputStream> manifest = globalContext.getFileContent(SdcCommon.MANIFEST_NAME);
171 if (!manifest.isPresent()) {
172 throw new RuntimeException("Can't load manifest file for Heat Validator");
174 ManifestContent manifestContent;
176 manifestContent = JsonUtil.json2Object(manifest.get(), ManifestContent.class);
177 } catch (Exception exception) {
178 throw new SdcRuntimeException("Can't load manifest file for Heat Validator", exception);
181 return manifestContent;
184 public static String getParserExceptionReason(Exception exception) {
187 if (exception.getCause() != null && exception.getCause().getCause() != null) {
188 reason = exception.getCause().getCause().getMessage();
189 } else if (exception.getCause() != null) {
190 reason = exception.getCause().getMessage();
192 reason = Messages.GENERAL_HEAT_PARSER_ERROR.getErrorMessage();
197 public static HeatOrchestrationTemplate checkHeatOrchestrationPreCondition(String fileName,
198 GlobalValidationContext globalContext) {
199 HeatOrchestrationTemplate heatOrchestrationTemplate;
201 Optional<InputStream> fileContent = globalContext.getFileContent(fileName);
202 if (fileContent.isPresent()) {
203 heatOrchestrationTemplate =
204 new YamlUtil().yamlToObject(fileContent.get(), HeatOrchestrationTemplate.class);
206 heatOrchestrationTemplate = null;
208 } catch (Exception exception) {
209 globalContext.addMessage(fileName, ErrorLevel.ERROR, ErrorMessagesFormatBuilder
210 .getErrorWithParameters(globalContext.getMessageCode(),
211 Messages.INVALID_HEAT_FORMAT_REASON.getErrorMessage()
212 , getParserExceptionReason(exception)));
215 return heatOrchestrationTemplate;