1 package org.openecomp.sdc.validation.impl.validators.heatresource;
3 import org.apache.commons.collections4.CollectionUtils;
4 import org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder;
5 import org.openecomp.core.validation.types.GlobalValidationContext;
6 import org.openecomp.sdc.common.errors.Messages;
7 import org.openecomp.sdc.datatypes.error.ErrorLevel;
8 import org.openecomp.sdc.heat.datatypes.model.Resource;
9 import org.openecomp.sdc.heat.services.HeatConstants;
10 import org.openecomp.sdc.heat.services.tree.HeatTreeManagerUtil;
11 import org.openecomp.sdc.logging.context.impl.MdcDataDebugMessage;
12 import org.openecomp.sdc.logging.types.LoggerErrorDescription;
13 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
14 import org.openecomp.sdc.validation.ResourceValidator;
15 import org.openecomp.sdc.validation.ValidationContext;
16 import org.openecomp.sdc.validation.impl.util.HeatValidationService;
18 import java.util.ArrayList;
19 import java.util.Collections;
20 import java.util.List;
22 import java.util.Objects;
23 import java.util.Optional;
27 * Created by TALIO on 2/22/2017.
29 public class ResourceGroupResourceValidator implements ResourceValidator {
30 private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
33 public void validate(String fileName, Map.Entry<String, Resource> resourceEntry,
34 GlobalValidationContext globalContext, ValidationContext validationContext) {
35 validateResourceGroupType(fileName, resourceEntry, globalContext);
38 private static void validateResourceGroupType(String fileName,
39 Map.Entry<String, Resource> resourceEntry,
40 GlobalValidationContext globalContext) {
42 mdcDataDebugMessage.debugEntryMessage("file", fileName);
44 Resource resourceDef = HeatTreeManagerUtil
45 .getResourceDef(fileName, resourceEntry.getKey(), resourceEntry.getValue(), globalContext);
46 if (resourceDef != null) {
47 if (Objects.nonNull(resourceDef.getType())
48 && HeatValidationService.isNestedResource(resourceDef.getType())) {
49 Optional<String> indexVarValue =
50 getResourceGroupIndexVarValue(resourceEntry, fileName, globalContext);
51 handleNestedResourceType(fileName, resourceEntry.getKey(), resourceDef, indexVarValue,
56 mdcDataDebugMessage.debugExitMessage("file", fileName);
59 private static Optional<String> getResourceGroupIndexVarValue(
60 Map.Entry<String, Resource> resourceEntry, String fileName,
61 GlobalValidationContext globalContext) {
63 resourceEntry.getValue().getProperties().get(HeatConstants.INDEX_PROPERTY_NAME);
64 if (indexVar == null) {
65 return Optional.of(HeatConstants.RESOURCE_GROUP_INDEX_VAR_DEFAULT_VALUE);
68 if (indexVar instanceof String) {
69 return Optional.of((String) indexVar);
71 globalContext.addMessage(fileName, ErrorLevel.ERROR, ErrorMessagesFormatBuilder
72 .getErrorWithParameters(Messages.RESOURCE_GROUP_INVALID_INDEX_VAR.getErrorMessage(),
73 resourceEntry.getKey()), LoggerTragetServiceName.VALIDATE_RESOURCE_GROUP_TYPE,
74 LoggerErrorDescription.INVALID_INDEX_VAR);
75 return Optional.empty();
79 private static void handleNestedResourceType(String fileName, String resourceName,
80 Resource resource, Optional<String> indexVarValue,
81 GlobalValidationContext globalContext) {
83 mdcDataDebugMessage.debugEntryMessage("file", fileName);
85 validateAllPropertiesMatchNestedParameters(fileName, resourceName, resource, indexVarValue,
87 validateLoopsOfNestingFromFile(fileName, resource.getType(), globalContext);
89 mdcDataDebugMessage.debugExitMessage("file", fileName);
92 private static void validateAllPropertiesMatchNestedParameters(String fileName,
95 Optional<String> indexVarValue,
96 GlobalValidationContext
99 mdcDataDebugMessage.debugEntryMessage("file", fileName);
101 String resourceType = resource.getType();
102 if (globalContext.getFileContextMap().containsKey(resourceType)) {
103 Set<String> propertiesNames =
104 resource.getProperties() == null ? null : resource.getProperties().keySet();
105 if (CollectionUtils.isNotEmpty(propertiesNames)) {
106 HeatValidationService
107 .checkNestedParameters(fileName, resourceType, resourceName, resource, propertiesNames,
108 indexVarValue, globalContext);
111 globalContext.addMessage(resourceType, ErrorLevel.ERROR, ErrorMessagesFormatBuilder
112 .getErrorWithParameters(Messages.MISSING_NESTED_FILE.getErrorMessage(), resourceType),
113 LoggerTragetServiceName.VALIDATE_PROPERTIES_MATCH_NESTED_PARAMETERS,
114 LoggerErrorDescription.MISSING_FILE);
117 mdcDataDebugMessage.debugExitMessage("file", fileName);
120 private static void validateLoopsOfNestingFromFile(String fileName, String resourceType,
121 GlobalValidationContext globalContext) {
123 mdcDataDebugMessage.debugEntryMessage("file", fileName);
125 List<String> filesInLoop = new ArrayList<>(Collections.singletonList(fileName));
126 if (HeatValidationService
127 .isNestedLoopExistInFile(fileName, resourceType, filesInLoop, globalContext)) {
128 globalContext.addMessage(fileName, ErrorLevel.ERROR, ErrorMessagesFormatBuilder
129 .getErrorWithParameters(Messages.NESTED_LOOP.getErrorMessage(),
130 HeatValidationService.drawFilesLoop(filesInLoop)),
131 LoggerTragetServiceName.VALIDATE_NESTING_LOOPS, LoggerErrorDescription.NESTED_LOOP);
134 mdcDataDebugMessage.debugExitMessage("file", fileName);