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