1 package org.openecomp.sdc.validation.impl.validators.namingconvention;
3 import org.apache.commons.collections4.CollectionUtils;
4 import org.apache.commons.collections4.MapUtils;
5 import org.apache.commons.lang3.tuple.ImmutablePair;
6 import org.apache.commons.lang3.tuple.Pair;
7 import org.openecomp.core.validation.ErrorMessageCode;
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.datatypes.error.ErrorLevel;
12 import org.openecomp.sdc.heat.datatypes.model.Resource;
13 import org.openecomp.sdc.logging.context.impl.MdcDataDebugMessage;
14 import org.openecomp.sdc.logging.types.LoggerErrorDescription;
15 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
16 import org.openecomp.sdc.validation.ResourceValidator;
17 import org.openecomp.sdc.validation.ValidationContext;
18 import org.openecomp.sdc.validation.util.ValidationUtil;
20 import java.util.Arrays;
21 import java.util.List;
23 import java.util.Objects;
24 import java.util.Optional;
25 import java.util.regex.Pattern;
27 import static java.util.Objects.nonNull;
30 * Created by TALIO on 2/24/2017.
32 public class ContrailServiceTemplateNamingConventionValidator implements ResourceValidator {
33 private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
34 private static final ErrorMessageCode ERROR_CODE_NST1 = new ErrorMessageCode("NST1");
35 private static final ErrorMessageCode ERROR_CODE_NST2 = new ErrorMessageCode("NST2");
36 private static final ErrorMessageCode ERROR_CODE_NST3 = new ErrorMessageCode("NST3");
39 public void validate(String fileName, Map.Entry<String, Resource> resourceEntry,
40 GlobalValidationContext globalContext, ValidationContext validationContext) {
41 validateServiceTemplateImageAndFlavor(fileName, resourceEntry, globalContext);
44 private void validateServiceTemplateImageAndFlavor(String fileName,
45 Map.Entry<String, Resource> entry,
46 GlobalValidationContext globalContext) {
48 mdcDataDebugMessage.debugEntryMessage("file", fileName);
50 if (MapUtils.isEmpty(entry.getValue().getProperties())) {
54 Pair<String, String> imagePair = new ImmutablePair<>("image_name", ".*_image_name");
55 Pair<String, String> flavorPair = new ImmutablePair<>("flavor", ".*_flavor_name");
56 List<Pair<String, String>> imageFlavorPairs = Arrays.asList(imagePair, flavorPair);
58 Map<String, Object> propertiesMap = entry.getValue().getProperties();
60 boolean errorExistValidatingImageOrFlavor = false;
61 for (Pair<String, String> imageOrFlavor : imageFlavorPairs) {
62 boolean errorExistWhenValidatingImageOrFlavorNames =
63 isErrorExistWhenValidatingImageOrFlavorNames(fileName, imageOrFlavor, entry,
64 propertiesMap, globalContext);
65 errorExistValidatingImageOrFlavor =
66 errorExistValidatingImageOrFlavor || errorExistWhenValidatingImageOrFlavorNames;
69 if (!errorExistValidatingImageOrFlavor) {
70 validateServiceTemplatePropertiesValuesVmtypesAreIdentical(fileName, entry, globalContext,
74 mdcDataDebugMessage.debugExitMessage("file", fileName);
77 private void validateServiceTemplatePropertiesValuesVmtypesAreIdentical(String fileName,
78 Map.Entry<String, Resource> entry,
79 GlobalValidationContext globalContext,
80 Map<String, Object> propertiesMap) {
82 mdcDataDebugMessage.debugEntryMessage("file", fileName);
84 Pair<String, String> vmTypeImagePair = new ImmutablePair<>("image_name", "\\_image\\_name");
85 Pair<String, String> vmTypeFlavorPair = new ImmutablePair<>("flavor", "\\_flavor\\_name");
86 validatePropertiesValuesVmtypesAreIdentical(Arrays.asList(vmTypeImagePair, vmTypeFlavorPair),
87 fileName, entry, propertiesMap, globalContext);
89 mdcDataDebugMessage.debugExitMessage("file", fileName);
92 private void validatePropertiesValuesVmtypesAreIdentical(List<Pair> propertiesToMatch,
94 Map.Entry<String, Resource> resourceEntry,
95 Map<String, Object> propertiesMap,
96 GlobalValidationContext globalContext) {
99 mdcDataDebugMessage.debugEntryMessage("file", fileName);
101 if (CollectionUtils.isEmpty(propertiesToMatch)) {
105 String previousPropertyValueValue = null;
106 for (Pair propertyToMatch : propertiesToMatch) {
107 Optional<String> propertyVmType =
108 extractVmTypeFromProperty(fileName, resourceEntry, propertiesMap, globalContext,
110 if (propertyVmType.isPresent()) {
111 String currentPropVmType = propertyVmType.get();
112 previousPropertyValueValue =
113 handleFirstIteration(previousPropertyValueValue, currentPropVmType);
114 if (addWarningIfCurrentVmTypeIsDifferentFromPrevious(fileName, resourceEntry, globalContext,
115 previousPropertyValueValue, currentPropVmType)) {
116 mdcDataDebugMessage.debugExitMessage("file", fileName);
122 mdcDataDebugMessage.debugExitMessage("file", fileName);
125 private boolean addWarningIfCurrentVmTypeIsDifferentFromPrevious(String fileName,
126 Map.Entry<String, Resource> resourceEntry,
127 GlobalValidationContext globalContext,
128 String previousPropertyValueValue,
129 String currentPropVmType) {
130 if (!Objects.equals(previousPropertyValueValue, currentPropVmType)) {
131 globalContext.addMessage(fileName, ErrorLevel.WARNING, ErrorMessagesFormatBuilder
132 .getErrorWithParameters(
133 ERROR_CODE_NST1, Messages.CONTRAIL_VM_TYPE_NAME_NOT_ALIGNED_WITH_NAMING_CONVENSION
135 resourceEntry.getKey()),
136 LoggerTragetServiceName.VALIDATE_CONTRAIL_VM_NAME,
137 LoggerErrorDescription.NAME_NOT_ALIGNED_WITH_GUIDELINES);
144 private boolean isErrorExistWhenValidatingImageOrFlavorNames(String fileName,
145 Pair<String, String> propertyNameAndRegex,
146 Map.Entry<String, Resource> resourceEntry,
147 Map<String, Object> propertiesMap,
148 GlobalValidationContext globalContext) {
149 String propertyName = propertyNameAndRegex.getKey();
151 propertiesMap.get(propertyName) == null ? null : propertiesMap.get(propertyName);
152 String[] regexList = new String[]{propertyNameAndRegex.getValue()};
153 if (nonNull(nameValue)) {
154 if (nameValue instanceof Map) {
155 globalContext.setMessageCode(ERROR_CODE_NST3);
156 if (ValidationUtil.validateMapPropertyValue(fileName, resourceEntry, globalContext,
158 nameValue, regexList)) {
162 globalContext.addMessage(
164 ErrorLevel.WARNING, ErrorMessagesFormatBuilder
165 .getErrorWithParameters(
166 ERROR_CODE_NST2, Messages.MISSING_GET_PARAM.getErrorMessage(),
168 resourceEntry.getKey()),
169 LoggerTragetServiceName.VALIDATE_IMAGE_AND_FLAVOR_NAME,
170 LoggerErrorDescription.MISSING_GET_PARAM);
180 private Optional<String> extractVmTypeFromProperty(String fileName,
181 Map.Entry<String, Resource> resourceEntry,
182 Map<String, Object> propertiesMap,
183 GlobalValidationContext globalContext,
184 Pair propertyKeyRegex) {
185 String propertyName = (String) propertyKeyRegex.getKey();
186 Object propertyVal = propertiesMap.get(propertyName);
187 if (nonNull(propertyVal)) {
188 if (propertyVal instanceof Map) {
189 String propertyValFromGetParam = ValidationUtil.getWantedNameFromPropertyValueGetParam
191 if (nonNull(propertyValFromGetParam)) {
192 Pattern pattern = Pattern.compile("" + propertyKeyRegex.getValue());
193 return Optional.ofNullable(pattern.split(propertyValFromGetParam)[0]);
196 globalContext.addMessage(
198 ErrorLevel.WARNING, ErrorMessagesFormatBuilder
199 .getErrorWithParameters(
200 ERROR_CODE_NST2, Messages.MISSING_GET_PARAM.getErrorMessage(),
202 resourceEntry.getKey()),
203 LoggerTragetServiceName.VALIDATE_VM_SYNC_IN_IMAGE_FLAVOR,
204 LoggerErrorDescription.MISSING_GET_PARAM);
205 return Optional.empty();
208 return Optional.empty();
211 private String handleFirstIteration(String previousPropertyValueValue, String currentPropVmType) {
212 if (Objects.isNull(previousPropertyValueValue)) {
213 previousPropertyValueValue = currentPropVmType;
216 return previousPropertyValueValue;