[SDC-29] Amdocs OnBoard 1707 initial commit.
[sdc.git] / openecomp-be / lib / openecomp-sdc-validation-lib / openecomp-sdc-validation-impl / src / main / java / org / openecomp / sdc / validation / impl / validators / heatresource / ResourceGroupResourceValidator.java
1 package org.openecomp.sdc.validation.impl.validators.heatresource;
2
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;
17
18 import java.util.ArrayList;
19 import java.util.Collections;
20 import java.util.List;
21 import java.util.Map;
22 import java.util.Objects;
23 import java.util.Optional;
24 import java.util.Set;
25
26 /**
27  * Created by TALIO on 2/22/2017.
28  */
29 public class ResourceGroupResourceValidator implements ResourceValidator {
30   private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
31
32   @Override
33   public void validate(String fileName, Map.Entry<String, Resource> resourceEntry,
34                        GlobalValidationContext globalContext, ValidationContext validationContext) {
35     validateResourceGroupType(fileName, resourceEntry, globalContext);
36   }
37
38   private static void validateResourceGroupType(String fileName,
39                                                 Map.Entry<String, Resource> resourceEntry,
40                                                 GlobalValidationContext globalContext) {
41
42     mdcDataDebugMessage.debugEntryMessage("file", fileName);
43
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,
52             globalContext);
53       }
54     }
55
56     mdcDataDebugMessage.debugExitMessage("file", fileName);
57   }
58
59   private static Optional<String> getResourceGroupIndexVarValue(
60       Map.Entry<String, Resource> resourceEntry, String fileName,
61       GlobalValidationContext globalContext) {
62     Object indexVar =
63         resourceEntry.getValue().getProperties().get(HeatConstants.INDEX_PROPERTY_NAME);
64     if (indexVar == null) {
65       return Optional.of(HeatConstants.RESOURCE_GROUP_INDEX_VAR_DEFAULT_VALUE);
66     }
67
68     if (indexVar instanceof String) {
69       return Optional.of((String) indexVar);
70     } else {
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();
76     }
77   }
78
79   private static void handleNestedResourceType(String fileName, String resourceName,
80                                                Resource resource, Optional<String> indexVarValue,
81                                                GlobalValidationContext globalContext) {
82
83     mdcDataDebugMessage.debugEntryMessage("file", fileName);
84
85     validateAllPropertiesMatchNestedParameters(fileName, resourceName, resource, indexVarValue,
86         globalContext);
87     validateLoopsOfNestingFromFile(fileName, resource.getType(), globalContext);
88
89     mdcDataDebugMessage.debugExitMessage("file", fileName);
90   }
91
92   private static void validateAllPropertiesMatchNestedParameters(String fileName,
93                                                                  String resourceName,
94                                                                  Resource resource,
95                                                                  Optional<String> indexVarValue,
96                                                                  GlobalValidationContext
97                                                                      globalContext) {
98
99     mdcDataDebugMessage.debugEntryMessage("file", fileName);
100
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);
109       }
110     } else {
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);
115     }
116
117     mdcDataDebugMessage.debugExitMessage("file", fileName);
118   }
119
120   private static void validateLoopsOfNestingFromFile(String fileName, String resourceType,
121                                                      GlobalValidationContext globalContext) {
122
123     mdcDataDebugMessage.debugEntryMessage("file", fileName);
124
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);
132     }
133
134     mdcDataDebugMessage.debugExitMessage("file", fileName);
135   }
136 }