41897f4563e38e22cc64a594be692cd54df947e9
[sdc.git] /
1 /*
2  * Copyright © 2016-2017 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.openecomp.sdc.validation.impl.validators.heatresource;
18
19 import org.apache.commons.collections4.CollectionUtils;
20 import org.openecomp.core.validation.ErrorMessageCode;
21 import org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder;
22 import org.openecomp.core.validation.types.GlobalValidationContext;
23 import org.openecomp.sdc.common.errors.Messages;
24 import org.openecomp.sdc.datatypes.error.ErrorLevel;
25 import org.openecomp.sdc.heat.datatypes.model.Resource;
26 import org.openecomp.sdc.logging.context.impl.MdcDataDebugMessage;
27 import org.openecomp.sdc.logging.types.LoggerErrorDescription;
28 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
29 import org.openecomp.sdc.validation.ResourceValidator;
30 import org.openecomp.sdc.validation.ValidationContext;
31 import org.openecomp.sdc.validation.impl.util.HeatValidationService;
32
33 import java.util.ArrayList;
34 import java.util.Collections;
35 import java.util.List;
36 import java.util.Map;
37 import java.util.Optional;
38 import java.util.Set;
39
40 /**
41  * Created by TALIO on 2/22/2017.
42  */
43 public class NestedResourceValidator implements ResourceValidator {
44   private static final MdcDataDebugMessage MDC_DATA_DEBUG_MESSAGE = new MdcDataDebugMessage();
45   private static final ErrorMessageCode ERROR_CODE_HNR1 = new ErrorMessageCode("HNR1");
46   private static final ErrorMessageCode ERROR_CODE_HNR2 = new ErrorMessageCode("HNR2");
47   private static final ErrorMessageCode ERROR_CODE_HNR3 = new ErrorMessageCode("HNR3");
48   private static final ErrorMessageCode ERROR_CODE_HNR4 = new ErrorMessageCode("HNR4");
49
50   @Override
51   public void validate(String fileName, Map.Entry<String, Resource> resourceEntry,
52                        GlobalValidationContext globalContext, ValidationContext validationContext) {
53
54     handleNestedResourceType(fileName, resourceEntry.getKey(), resourceEntry.getValue(),
55             Optional.empty(), globalContext);
56   }
57
58   private static void handleNestedResourceType(String fileName, String resourceName,
59                                                Resource resource, Optional<String> indexVarValue,
60                                                GlobalValidationContext globalContext) {
61
62     MDC_DATA_DEBUG_MESSAGE.debugEntryMessage("file", fileName);
63
64     validateAllPropertiesMatchNestedParameters(fileName, resourceName, resource, indexVarValue,
65             globalContext);
66     validateLoopsOfNestingFromFile(fileName, resource.getType(), globalContext);
67
68     MDC_DATA_DEBUG_MESSAGE.debugExitMessage("file", fileName);
69   }
70
71   public static void validateAllPropertiesMatchNestedParameters(String fileName,
72                                                                 String resourceName,
73                                                                 Resource resource,
74                                                                 Optional<String> indexVarValue,
75                                                                 GlobalValidationContext
76                                                                         globalContext) {
77
78     MDC_DATA_DEBUG_MESSAGE.debugEntryMessage("file", fileName);
79
80     String resourceType = resource.getType();
81     if (globalContext.getFileContextMap().containsKey(resourceType)) {
82       Set<String> propertiesNames =
83               resource.getProperties() == null ? null : resource.getProperties().keySet();
84       if (CollectionUtils.isNotEmpty(propertiesNames)) {
85         globalContext.setMessageCode(ERROR_CODE_HNR3);
86         HeatValidationService
87                 .checkNestedParametersNoMissingParameterInNested(fileName, resourceType, resourceName,
88                          propertiesNames,
89                          globalContext);
90         globalContext.setMessageCode(ERROR_CODE_HNR4);
91         HeatValidationService
92                 .checkNestedInputValuesAlignWithType(fileName, resourceType, resourceName, resource,
93                         indexVarValue, globalContext);
94       }
95     } else {
96       globalContext.addMessage(resourceType, ErrorLevel.ERROR, ErrorMessagesFormatBuilder
97                       .getErrorWithParameters(ERROR_CODE_HNR1,
98                               Messages.MISSING_NESTED_FILE.getErrorMessage(),
99                               resourceType),
100               LoggerTragetServiceName.VALIDATE_PROPERTIES_MATCH_NESTED_PARAMETERS,
101               LoggerErrorDescription.MISSING_FILE);
102     }
103
104     MDC_DATA_DEBUG_MESSAGE.debugExitMessage("file", fileName);
105   }
106
107   public static void validateLoopsOfNestingFromFile(String fileName, String resourceType,
108                                                     GlobalValidationContext globalContext) {
109
110     MDC_DATA_DEBUG_MESSAGE.debugEntryMessage("file", fileName);
111
112     List<String> filesInLoop = new ArrayList<>(Collections.singletonList(fileName));
113     if (HeatValidationService
114             .isNestedLoopExistInFile(fileName, resourceType, filesInLoop, globalContext)) {
115       globalContext.addMessage(fileName, ErrorLevel.ERROR, ErrorMessagesFormatBuilder
116                       .getErrorWithParameters(ERROR_CODE_HNR2, Messages.NESTED_LOOP.getErrorMessage(),
117                               HeatValidationService.drawFilesLoop(filesInLoop)),
118               LoggerTragetServiceName.VALIDATE_NESTING_LOOPS, LoggerErrorDescription.NESTED_LOOP);
119     }
120
121     MDC_DATA_DEBUG_MESSAGE.debugExitMessage("file", fileName);
122   }
123 }