9be5f61f25b8ca1ed8c6a0fbe2c01aa6ee560269
[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;
18
19 import org.apache.commons.collections4.CollectionUtils;
20 import org.openecomp.core.utilities.CommonMethods;
21 import org.openecomp.core.validation.ErrorMessageCode;
22 import org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder;
23 import org.openecomp.core.validation.types.GlobalValidationContext;
24 import org.openecomp.sdc.common.errors.Messages;
25 import org.openecomp.sdc.common.utils.SdcCommon;
26 import org.openecomp.sdc.datatypes.error.ErrorLevel;
27 import org.openecomp.sdc.heat.datatypes.manifest.FileData;
28 import org.openecomp.sdc.heat.datatypes.manifest.ManifestContent;
29 import org.openecomp.sdc.heat.datatypes.model.HeatOrchestrationTemplate;
30 import org.openecomp.sdc.heat.datatypes.model.HeatResourcesTypes;
31 import org.openecomp.sdc.heat.datatypes.model.ResourceReferenceFunctions;
32 import org.openecomp.sdc.heat.services.HeatStructureUtil;
33 import org.openecomp.sdc.heat.services.manifest.ManifestUtil;
34 import org.openecomp.sdc.logging.api.Logger;
35 import org.openecomp.sdc.logging.api.LoggerFactory;
36 import org.openecomp.sdc.validation.Validator;
37 import org.openecomp.sdc.validation.util.ValidationUtil;
38
39 import java.util.HashSet;
40 import java.util.Map;
41 import java.util.Set;
42
43 public class SharedResourceGuideLineValidator implements Validator {
44   private static final Logger LOGGER = LoggerFactory.getLogger(
45           SharedResourceGuideLineValidator.class);
46   private static final ErrorMessageCode ERROR_CODE_SRG_1 = new ErrorMessageCode("SRG1");
47   private static final ErrorMessageCode ERROR_CODE_SRG_2 = new ErrorMessageCode("SRG2");
48   private static final ErrorMessageCode ERROR_CODE_SRG_3 = new ErrorMessageCode("SRG3");
49   private static final ErrorMessageCode ERROR_CODE_SRG_4 = new ErrorMessageCode("SRG4");
50   private static final ErrorMessageCode ERROR_CODE_SRG_5 = new ErrorMessageCode("SRG5");
51   private static final ErrorMessageCode ERROR_CODE_SRG_6 = new ErrorMessageCode("SRG6");
52
53   @Override
54   public void validate(GlobalValidationContext globalContext) {
55     ManifestContent manifestContent;
56     try {
57       manifestContent = ValidationUtil.validateManifest(globalContext);
58     } catch (Exception exception) {
59       LOGGER.error("Fialed to check  Validation PreCondition ",exception);
60       return;
61     }
62
63     Set<String> baseFiles = validateManifest(manifestContent, globalContext);
64
65     Map<String, FileData.Type> fileTypeMap = ManifestUtil.getFileTypeMap(manifestContent);
66     globalContext.getFiles().stream()
67         .filter(fileName -> FileData
68             .isHeatFile(fileTypeMap.get(fileName)))
69         .forEach(fileName -> validate(fileName,
70             fileTypeMap, baseFiles, globalContext));
71
72
73   }
74
75   private Set<String> validateManifest(ManifestContent manifestContent,
76                                               GlobalValidationContext globalContext) {
77     Set<String> baseFiles = ManifestUtil.getBaseFiles(manifestContent);
78     if (baseFiles == null || baseFiles.isEmpty()) {
79       globalContext.addMessage(
80           SdcCommon.MANIFEST_NAME,
81           ErrorLevel.WARNING,
82           ErrorMessagesFormatBuilder
83               .getErrorWithParameters(ERROR_CODE_SRG_3,Messages
84                   .MISSIN_BASE_HEAT_FILE.getErrorMessage()));
85     } else if (baseFiles.size() > 1) {
86       String baseFileList = getElementListAsString(baseFiles);
87       globalContext.addMessage(
88           SdcCommon.MANIFEST_NAME,
89           ErrorLevel.WARNING,
90           ErrorMessagesFormatBuilder
91               .getErrorWithParameters(ERROR_CODE_SRG_4,Messages
92                       .MULTI_BASE_HEAT_FILE.getErrorMessage(),
93                   baseFileList));
94     }
95     return baseFiles;
96   }
97
98   private static String getElementListAsString(Set<String> elementCollection) {
99     return "["
100         + CommonMethods.collectionToCommaSeparatedString(elementCollection)
101         +  "]";
102   }
103
104   private void validate(String fileName, Map<String, FileData.Type> fileTypeMap,
105                         Set<String> baseFiles, GlobalValidationContext globalContext) {
106     globalContext.setMessageCode(ERROR_CODE_SRG_5);
107     HeatOrchestrationTemplate
108         heatOrchestrationTemplate = ValidationUtil
109         .checkHeatOrchestrationPreCondition(fileName, globalContext);
110     if (heatOrchestrationTemplate == null) {
111       return;
112     }
113
114     validateBaseFile(fileName, baseFiles, heatOrchestrationTemplate, globalContext);
115     validateHeatVolumeFile(fileName, fileTypeMap, heatOrchestrationTemplate, globalContext);
116   }
117
118
119   private void validateBaseFile(String fileName, Set<String> baseFiles,
120                                 HeatOrchestrationTemplate heatOrchestrationTemplate,
121                                 GlobalValidationContext globalContext) {
122     //if not base return
123     if (baseFiles == null || !baseFiles.contains(fileName)) {
124       return;
125     }
126
127     //if no resources exist return
128     if (heatOrchestrationTemplate.getResources() == null
129         || heatOrchestrationTemplate.getResources().size() == 0) {
130       return;
131     }
132
133     Set<String> expectedExposedResources = new HashSet<>();
134     heatOrchestrationTemplate.getResources()
135         .entrySet()
136         .stream()
137         .filter(entry -> ValidationUtil.isExpectedToBeExposed(entry.getValue().getType()))
138         .forEach(entry -> expectedExposedResources.add(entry.getKey()));
139     Set<String> actualExposedResources = new HashSet<>();
140
141     if (heatOrchestrationTemplate.getOutputs() != null) {
142       globalContext.setMessageCode(ERROR_CODE_SRG_6);
143       heatOrchestrationTemplate.getOutputs().entrySet()
144           .stream()
145           .filter(entry -> isPropertyValueGetResource(fileName, entry.getValue().getValue(),
146               globalContext))
147           .forEach(entry -> actualExposedResources.add(
148               getResourceIdFromPropertyValue(fileName, entry.getValue().getValue(),
149                   globalContext)));
150     }
151     ValidationUtil.removeExposedResourcesCalledByGetResource(fileName, actualExposedResources,
152         heatOrchestrationTemplate, globalContext);
153
154     actualExposedResources.forEach(expectedExposedResources::remove);
155
156     if (CollectionUtils.isNotEmpty(expectedExposedResources)) {
157       expectedExposedResources
158           .stream()
159           .forEach(name -> globalContext.addMessage(
160               fileName,
161               ErrorLevel.WARNING, ErrorMessagesFormatBuilder
162                   .getErrorWithParameters(ERROR_CODE_SRG_1,
163                           Messages.RESOURCE_NOT_DEFINED_IN_OUTPUT.getErrorMessage(),
164                           name)));
165     }
166   }
167
168   private void validateHeatVolumeFile(String fileName, Map<String, FileData.Type> fileTypeMap,
169                                       HeatOrchestrationTemplate heatOrchestrationTemplate,
170                                       GlobalValidationContext globalContext) {
171     //if not heat volume return
172     if (!fileTypeMap.get(fileName).equals(FileData.Type.HEAT_VOL)) {
173       return;
174     }
175
176     //if no resources exist return
177     if (heatOrchestrationTemplate.getResources() == null
178         || heatOrchestrationTemplate.getResources().size() == 0) {
179       return;
180     }
181
182     Set<String> expectedExposedResources = new HashSet<>();
183     Set<String> actualExposedResources = new HashSet<>();
184     heatOrchestrationTemplate.getResources()
185         .entrySet()
186         .stream()
187         .filter(entry -> entry.getValue().getType()
188             .equals(HeatResourcesTypes.CINDER_VOLUME_RESOURCE_TYPE.getHeatResource()))
189         .forEach(entry -> expectedExposedResources.add(entry.getKey()));
190
191     if (heatOrchestrationTemplate.getOutputs() != null) {
192       globalContext.setMessageCode(ERROR_CODE_SRG_6);
193       heatOrchestrationTemplate.getOutputs().entrySet()
194           .stream()
195           .filter(entry -> isPropertyValueGetResource(fileName, entry.getValue().getValue(),
196               globalContext))
197           .forEach(entry -> actualExposedResources.add(
198               getResourceIdFromPropertyValue(fileName, entry.getValue().getValue(),
199                   globalContext)));
200     }
201
202     actualExposedResources.forEach(expectedExposedResources::remove);
203
204     if (CollectionUtils.isNotEmpty(expectedExposedResources)) {
205       expectedExposedResources
206           .stream()
207           .forEach(name -> globalContext.addMessage(
208               fileName,
209               ErrorLevel.WARNING, ErrorMessagesFormatBuilder
210                   .getErrorWithParameters(ERROR_CODE_SRG_2,
211                           Messages.VOLUME_HEAT_NOT_EXPOSED.getErrorMessage(), name)));
212     }
213   }
214
215
216   private boolean isPropertyValueGetResource(String filename, Object value,
217                                              GlobalValidationContext globalContext) {
218     Set<String> referenceValues = HeatStructureUtil.getReferencedValuesByFunctionName(filename,
219         ResourceReferenceFunctions.GET_RESOURCE.getFunction(), value, globalContext);
220     return referenceValues != null && (CollectionUtils.isNotEmpty(referenceValues));
221   }
222
223   private String getResourceIdFromPropertyValue(String filename, Object value,
224                                                 GlobalValidationContext globalContext) {
225     Set<String> referenceValues = HeatStructureUtil.getReferencedValuesByFunctionName(filename,
226         ResourceReferenceFunctions.GET_RESOURCE.getFunction(), value, globalContext);
227     if (referenceValues != null && CollectionUtils.isNotEmpty(referenceValues)) {
228       return (String) referenceValues.toArray()[0];
229     }
230     return null;
231   }
232
233 }