84899d19679b3c2d36f73daaf5eb592616cef1c9
[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.types.LoggerErrorDescription;
27 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
28 import org.openecomp.sdc.validation.ResourceValidator;
29 import org.openecomp.sdc.validation.ValidationContext;
30 import org.openecomp.sdc.validation.impl.util.HeatValidationService;
31
32 import java.util.ArrayList;
33 import java.util.Collections;
34 import java.util.List;
35 import java.util.Map;
36 import java.util.Optional;
37 import java.util.Set;
38
39 /**
40  * Created by TALIO on 2/22/2017.
41  */
42 public class NestedResourceValidator implements ResourceValidator {
43   private static final ErrorMessageCode ERROR_CODE_HNR1 = new ErrorMessageCode("HNR1");
44   private static final ErrorMessageCode ERROR_CODE_HNR2 = new ErrorMessageCode("HNR2");
45   private static final ErrorMessageCode ERROR_CODE_HNR3 = new ErrorMessageCode("HNR3");
46   private static final ErrorMessageCode ERROR_CODE_HNR4 = new ErrorMessageCode("HNR4");
47
48   @Override
49   public void validate(String fileName, Map.Entry<String, Resource> resourceEntry,
50                        GlobalValidationContext globalContext, ValidationContext validationContext) {
51
52     handleNestedResourceType(fileName, resourceEntry.getKey(), resourceEntry.getValue(),
53             Optional.empty(), globalContext);
54   }
55
56   private static void handleNestedResourceType(String fileName, String resourceName,
57                                                Resource resource, Optional<String> indexVarValue,
58                                                GlobalValidationContext globalContext) {
59     validateAllPropertiesMatchNestedParameters(fileName, resourceName, resource, indexVarValue,
60             globalContext);
61     validateLoopsOfNestingFromFile(fileName, resource.getType(), globalContext);
62   }
63
64   public static void validateAllPropertiesMatchNestedParameters(String fileName,
65                                                                 String resourceName,
66                                                                 Resource resource,
67                                                                 Optional<String> indexVarValue,
68                                                                 GlobalValidationContext
69                                                                         globalContext) {
70     String resourceType = resource.getType();
71     if (globalContext.getFileContextMap().containsKey(resourceType)) {
72       Set<String> propertiesNames =
73               resource.getProperties() == null ? null : resource.getProperties().keySet();
74       if (CollectionUtils.isNotEmpty(propertiesNames)) {
75         globalContext.setMessageCode(ERROR_CODE_HNR3);
76         HeatValidationService
77                 .checkNestedParametersNoMissingParameterInNested(fileName, resourceType, resourceName,
78                          propertiesNames,
79                          globalContext);
80         globalContext.setMessageCode(ERROR_CODE_HNR4);
81         HeatValidationService
82                 .checkNestedInputValuesAlignWithType(fileName, resourceType, resourceName, resource,
83                         indexVarValue, globalContext);
84       }
85     } else {
86       globalContext.addMessage(resourceType, ErrorLevel.ERROR, ErrorMessagesFormatBuilder
87                       .getErrorWithParameters(ERROR_CODE_HNR1,
88                               Messages.MISSING_NESTED_FILE.getErrorMessage(),
89                               resourceType),
90               LoggerTragetServiceName.VALIDATE_PROPERTIES_MATCH_NESTED_PARAMETERS,
91               LoggerErrorDescription.MISSING_FILE);
92     }
93   }
94
95   public static void validateLoopsOfNestingFromFile(String fileName, String resourceType,
96                                                     GlobalValidationContext globalContext) {
97     List<String> filesInLoop = new ArrayList<>(Collections.singletonList(fileName));
98     if (HeatValidationService
99             .isNestedLoopExistInFile(fileName, resourceType, filesInLoop, globalContext)) {
100       globalContext.addMessage(fileName, ErrorLevel.ERROR, ErrorMessagesFormatBuilder
101                       .getErrorWithParameters(ERROR_CODE_HNR2, Messages.NESTED_LOOP.getErrorMessage(),
102                               HeatValidationService.drawFilesLoop(filesInLoop)),
103               LoggerTragetServiceName.VALIDATE_NESTING_LOOPS, LoggerErrorDescription.NESTED_LOOP);
104     }
105   }
106 }