be0d1e2f8894ef82b2b6a72d73fe27cd29ab9882
[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 java.util.ArrayList;
20 import java.util.Collections;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.Objects;
24 import java.util.Optional;
25 import java.util.Set;
26
27 import org.apache.commons.collections4.CollectionUtils;
28 import org.openecomp.core.validation.ErrorMessageCode;
29 import org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder;
30 import org.openecomp.core.validation.types.GlobalValidationContext;
31 import org.openecomp.sdc.common.errors.Messages;
32 import org.openecomp.sdc.datatypes.error.ErrorLevel;
33 import org.openecomp.sdc.heat.datatypes.model.Resource;
34 import org.openecomp.sdc.heat.services.HeatConstants;
35 import org.openecomp.sdc.heat.services.tree.HeatTreeManagerUtil;
36 import org.openecomp.sdc.validation.ResourceValidator;
37 import org.openecomp.sdc.validation.ValidationContext;
38 import org.openecomp.sdc.validation.impl.util.HeatValidationService;
39
40 /**
41  * Created by TALIO on 2/22/2017.
42  */
43 public class ResourceGroupResourceValidator implements ResourceValidator {
44   private static final ErrorMessageCode ERROR_CODE_HRR1 = new ErrorMessageCode("HRR1");
45   private static final ErrorMessageCode ERROR_CODE_HRR2 = new ErrorMessageCode("HRR2");
46   private static final ErrorMessageCode ERROR_CODE_HRR3 = new ErrorMessageCode("HRR3");
47   private static final ErrorMessageCode ERROR_CODE_HRR4 = new ErrorMessageCode("HRR4");
48   private static final ErrorMessageCode ERROR_CODE_HRR5 = new ErrorMessageCode("HRR5");
49   private static final ErrorMessageCode ERROR_CODE_HRR6 = new ErrorMessageCode("HRR6");
50   private static final ErrorMessageCode ERROR_CODE_HRR7 = new ErrorMessageCode("HRR7");
51   private static final ErrorMessageCode ERROR_CODE_HRR8 = new ErrorMessageCode("HRR8");
52
53   @Override
54   public void validate(String fileName, Map.Entry<String, Resource> resourceEntry,
55                        GlobalValidationContext globalContext, ValidationContext validationContext) {
56     validateResourceGroupType(fileName, resourceEntry, globalContext);
57   }
58
59   private static void validateResourceGroupType(String fileName,
60                                                 Map.Entry<String, Resource> resourceEntry,
61                                                 GlobalValidationContext globalContext) {
62     globalContext.setMessageCode(ERROR_CODE_HRR6);
63     HeatTreeManagerUtil
64             .checkResourceTypeValid(fileName, resourceEntry.getKey(), resourceEntry.getValue(),
65                     globalContext);
66     globalContext.setMessageCode(ERROR_CODE_HRR7);
67     HeatTreeManagerUtil
68             .checkResourceGroupTypeValid(fileName, resourceEntry.getKey(), resourceEntry.getValue(),
69                     globalContext);
70     globalContext.setMessageCode(ERROR_CODE_HRR8);
71     HeatTreeManagerUtil.checkIfResourceGroupTypeIsNested(fileName, resourceEntry.getKey(),
72             resourceEntry.getValue(), globalContext);
73     Resource resourceDef = HeatTreeManagerUtil
74             .getResourceDef( resourceEntry.getValue());
75
76       if (resourceDef != null  && Objects.nonNull(resourceDef.getType())
77               && HeatValidationService.isNestedResource(resourceDef.getType())) {
78         Optional<String> indexVarValue =
79                 getResourceGroupIndexVarValue(resourceEntry, fileName, globalContext);
80         handleNestedResourceType(fileName, resourceEntry.getKey(), resourceDef, indexVarValue,
81                 globalContext);
82
83     }
84   }
85
86   private static Optional<String> getResourceGroupIndexVarValue(
87           Map.Entry<String, Resource> resourceEntry, String fileName,
88           GlobalValidationContext globalContext) {
89     Object indexVar =
90             resourceEntry.getValue().getProperties().get(HeatConstants.INDEX_PROPERTY_NAME);
91     if (indexVar == null) {
92       return Optional.of(HeatConstants.RESOURCE_GROUP_INDEX_VAR_DEFAULT_VALUE);
93     }
94
95     if (indexVar instanceof String) {
96       return Optional.of((String) indexVar);
97     } else {
98       globalContext.addMessage(fileName, ErrorLevel.ERROR, ErrorMessagesFormatBuilder
99                       .getErrorWithParameters(
100                               ERROR_CODE_HRR1, Messages.RESOURCE_GROUP_INVALID_INDEX_VAR.getErrorMessage(),
101                               resourceEntry.getKey()));
102       return Optional.empty();
103     }
104   }
105
106   private static void handleNestedResourceType(String fileName, String resourceName,
107                                                Resource resource, Optional<String> indexVarValue,
108                                                GlobalValidationContext globalContext) {
109     validateAllPropertiesMatchNestedParameters(fileName, resourceName, resource, indexVarValue,
110             globalContext);
111     validateLoopsOfNestingFromFile(fileName, resource.getType(), globalContext);
112   }
113
114   private static void validateAllPropertiesMatchNestedParameters(String fileName,
115                                                                  String resourceName,
116                                                                  Resource resource,
117                                                                  Optional<String> indexVarValue,
118                                                                  GlobalValidationContext
119                                                                          globalContext) {
120     String resourceType = resource.getType();
121     if (globalContext.getFileContextMap().containsKey(resourceType)) {
122       Set<String> propertiesNames =
123               resource.getProperties() == null ? null : resource.getProperties().keySet();
124       if (CollectionUtils.isNotEmpty(propertiesNames)) {
125         globalContext.setMessageCode(ERROR_CODE_HRR4);
126         HeatValidationService
127                 .checkNestedParametersNoMissingParameterInNested(fileName, resourceType, resourceName,
128                          propertiesNames,
129                          globalContext);
130         globalContext.setMessageCode(ERROR_CODE_HRR5);
131         HeatValidationService
132                 .checkNestedInputValuesAlignWithType(fileName, resourceType, resourceName, resource,
133                         indexVarValue, globalContext);
134       }
135     } else {
136       globalContext.addMessage(resourceType, ErrorLevel.ERROR, ErrorMessagesFormatBuilder
137                       .getErrorWithParameters(
138                               ERROR_CODE_HRR2, Messages.MISSING_NESTED_FILE.getErrorMessage(),
139                               resourceType));
140     }
141   }
142
143   private static void validateLoopsOfNestingFromFile(String fileName, String resourceType,
144                                                      GlobalValidationContext globalContext) {
145     List<String> filesInLoop = new ArrayList<>(Collections.singletonList(fileName));
146     if (HeatValidationService
147             .isNestedLoopExistInFile(fileName, resourceType, filesInLoop, globalContext)) {
148       globalContext.addMessage(fileName, ErrorLevel.ERROR, ErrorMessagesFormatBuilder
149                       .getErrorWithParameters(
150                               ERROR_CODE_HRR3, Messages.NESTED_LOOP.getErrorMessage(),
151                               HeatValidationService.drawFilesLoop(filesInLoop)));
152     }
153   }
154 }