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.logging.context.impl.MdcDataDebugMessage;
10 import org.openecomp.sdc.logging.types.LoggerErrorDescription;
11 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
12 import org.openecomp.sdc.validation.ResourceValidator;
13 import org.openecomp.sdc.validation.ValidationContext;
14 import org.openecomp.sdc.validation.impl.util.HeatValidationService;
16 import java.util.ArrayList;
17 import java.util.Collections;
18 import java.util.List;
20 import java.util.Optional;
24 * Created by TALIO on 2/22/2017.
26 public class NestedResourceValidator implements ResourceValidator {
27 private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
30 public void validate(String fileName, Map.Entry<String, Resource> resourceEntry,
31 GlobalValidationContext globalContext, ValidationContext validationContext){
33 handleNestedResourceType(fileName, resourceEntry.getKey(), resourceEntry.getValue(),
34 Optional.empty(), globalContext);
37 private static void handleNestedResourceType(String fileName, String resourceName,
38 Resource resource, Optional<String> indexVarValue,
39 GlobalValidationContext globalContext) {
41 mdcDataDebugMessage.debugEntryMessage("file", fileName);
43 validateAllPropertiesMatchNestedParameters(fileName, resourceName, resource, indexVarValue,
45 validateLoopsOfNestingFromFile(fileName, resource.getType(), globalContext);
47 mdcDataDebugMessage.debugExitMessage("file", fileName);
50 public static void validateAllPropertiesMatchNestedParameters(String fileName,
53 Optional<String> indexVarValue,
54 GlobalValidationContext
57 mdcDataDebugMessage.debugEntryMessage("file", fileName);
59 String resourceType = resource.getType();
60 if (globalContext.getFileContextMap().containsKey(resourceType)) {
61 Set<String> propertiesNames =
62 resource.getProperties() == null ? null : resource.getProperties().keySet();
63 if (CollectionUtils.isNotEmpty(propertiesNames)) {
65 .checkNestedParameters(fileName, resourceType, resourceName, resource, propertiesNames,
66 indexVarValue, globalContext);
69 globalContext.addMessage(resourceType, ErrorLevel.ERROR, ErrorMessagesFormatBuilder
70 .getErrorWithParameters(Messages.MISSING_NESTED_FILE.getErrorMessage(), resourceType),
71 LoggerTragetServiceName.VALIDATE_PROPERTIES_MATCH_NESTED_PARAMETERS,
72 LoggerErrorDescription.MISSING_FILE);
75 mdcDataDebugMessage.debugExitMessage("file", fileName);
78 public static void validateLoopsOfNestingFromFile(String fileName, String resourceType,
79 GlobalValidationContext globalContext) {
81 mdcDataDebugMessage.debugEntryMessage("file", fileName);
83 List<String> filesInLoop = new ArrayList<>(Collections.singletonList(fileName));
84 if (HeatValidationService
85 .isNestedLoopExistInFile(fileName, resourceType, filesInLoop, globalContext)) {
86 globalContext.addMessage(fileName, ErrorLevel.ERROR, ErrorMessagesFormatBuilder
87 .getErrorWithParameters(Messages.NESTED_LOOP.getErrorMessage(),
88 HeatValidationService.drawFilesLoop(filesInLoop)),
89 LoggerTragetServiceName.VALIDATE_NESTING_LOOPS, LoggerErrorDescription.NESTED_LOOP);
92 mdcDataDebugMessage.debugExitMessage("file", fileName);