[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 / NestedResourceValidator.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.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;
15
16 import java.util.ArrayList;
17 import java.util.Collections;
18 import java.util.List;
19 import java.util.Map;
20 import java.util.Optional;
21 import java.util.Set;
22
23 /**
24  * Created by TALIO on 2/22/2017.
25  */
26 public class NestedResourceValidator implements ResourceValidator {
27   private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
28
29   @Override
30   public void validate(String fileName, Map.Entry<String, Resource> resourceEntry,
31                        GlobalValidationContext globalContext, ValidationContext validationContext){
32
33     handleNestedResourceType(fileName, resourceEntry.getKey(), resourceEntry.getValue(),
34         Optional.empty(), globalContext);
35   }
36
37   private static void handleNestedResourceType(String fileName, String resourceName,
38                                                Resource resource, Optional<String> indexVarValue,
39                                                GlobalValidationContext globalContext) {
40
41     mdcDataDebugMessage.debugEntryMessage("file", fileName);
42
43     validateAllPropertiesMatchNestedParameters(fileName, resourceName, resource, indexVarValue,
44         globalContext);
45     validateLoopsOfNestingFromFile(fileName, resource.getType(), globalContext);
46
47     mdcDataDebugMessage.debugExitMessage("file", fileName);
48   }
49
50   public static void validateAllPropertiesMatchNestedParameters(String fileName,
51                                                                  String resourceName,
52                                                                  Resource resource,
53                                                                  Optional<String> indexVarValue,
54                                                                  GlobalValidationContext
55                                                                      globalContext) {
56
57     mdcDataDebugMessage.debugEntryMessage("file", fileName);
58
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)) {
64         HeatValidationService
65             .checkNestedParameters(fileName, resourceType, resourceName, resource, propertiesNames,
66                 indexVarValue, globalContext);
67       }
68     } else {
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);
73     }
74
75     mdcDataDebugMessage.debugExitMessage("file", fileName);
76   }
77
78   public static void validateLoopsOfNestingFromFile(String fileName, String resourceType,
79                                                      GlobalValidationContext globalContext) {
80
81     mdcDataDebugMessage.debugEntryMessage("file", fileName);
82
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);
90     }
91
92     mdcDataDebugMessage.debugExitMessage("file", fileName);
93   }
94 }