4615f7be4507377a325923e2b1183a4c85ddf51c
[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.MapUtils;
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.manifest.FileData;
26 import org.openecomp.sdc.heat.datatypes.manifest.ManifestContent;
27 import org.openecomp.sdc.heat.datatypes.model.HeatOrchestrationTemplate;
28 import org.openecomp.sdc.heat.datatypes.model.Resource;
29 import org.openecomp.sdc.heat.services.HeatConstants;
30 import org.openecomp.sdc.heat.services.manifest.ManifestUtil;
31 import org.openecomp.sdc.logging.api.Logger;
32 import org.openecomp.sdc.logging.api.LoggerFactory;
33 import org.openecomp.sdc.logging.types.LoggerErrorDescription;
34 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
35 import org.openecomp.sdc.tosca.services.YamlUtil;
36 import org.openecomp.sdc.validation.Validator;
37 import org.openecomp.sdc.validation.tos.ContrailResourcesMappingTo;
38 import org.openecomp.sdc.validation.util.ValidationUtil;
39
40 import java.io.InputStream;
41 import java.util.Map;
42 import java.util.Objects;
43 import java.util.Optional;
44
45
46 public class ContrailValidator implements Validator {
47   private static final Logger LOGGER = LoggerFactory.getLogger(ContrailValidator.class);
48   private static final ErrorMessageCode ERROR_CODE_CTL_1 = new ErrorMessageCode("CTL1");
49   private static final ErrorMessageCode ERROR_CODE_CTL_2 = new ErrorMessageCode("CTL2");
50   private static final ErrorMessageCode ERROR_CODE_CTL_3 = new ErrorMessageCode("CTL3");
51   private static final ErrorMessageCode ERROR_CODE_CTL_4 = new ErrorMessageCode("CTL4");
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.debug("",exception);
60       return;
61     }
62     Map<String, FileData.Type> fileTypeMap = ManifestUtil.getFileTypeMap(manifestContent);
63     ContrailResourcesMappingTo contrailResourcesMappingTo = new ContrailResourcesMappingTo();
64
65     globalContext.getFiles().stream()
66         .filter(fileName -> FileData.isHeatFile(fileTypeMap.get(fileName)))
67         .forEach(fileName -> validate(fileName,
68             contrailResourcesMappingTo, globalContext));
69   }
70
71
72   private void validate(String fileName,
73                         ContrailResourcesMappingTo contrailResourcesMappingTo,
74                         GlobalValidationContext globalContext) {
75     handleContrailV1AndContrailV2ResourceMerging(fileName, contrailResourcesMappingTo,
76         globalContext);
77     validateNoContrailResourceTypeIsInUse(fileName, globalContext);
78   }
79
80
81   private void handleContrailV1AndContrailV2ResourceMerging(String fileName,
82                ContrailResourcesMappingTo contrailResourcesMappingTo,
83                GlobalValidationContext globalContext) {
84     Optional<ContrailResourcesMappingTo> fileContrailResourcesMappingTo =
85         collectHeatFileContrailResources(globalContext, fileName);
86     fileContrailResourcesMappingTo.ifPresent(contrailResourcesMappingTo::addAll);
87     addContrailMergeValidationMessageToGlobalContext(globalContext, contrailResourcesMappingTo);
88   }
89
90   private void addContrailMergeValidationMessageToGlobalContext(
91       GlobalValidationContext globalContext,
92       ContrailResourcesMappingTo contrailResourcesMappingTo) {
93     if (!MapUtils.isEmpty(contrailResourcesMappingTo.getContrailV1Resources())
94         && !MapUtils.isEmpty(contrailResourcesMappingTo.getContrailV2Resources())) {
95       globalContext.addMessage(
96           contrailResourcesMappingTo.getContrailV1Resources().keySet().iterator().next(),
97           ErrorLevel.WARNING, ErrorMessagesFormatBuilder.getErrorWithParameters(
98                   ERROR_CODE_CTL_2, Messages.MERGE_OF_CONTRAIL2_AND_CONTRAIL3_RESOURCES.getErrorMessage(),
99               contrailResourcesMappingTo.fetchContrailV1Resources(),
100               contrailResourcesMappingTo.fetchContrailV2Resources()),
101           LoggerTragetServiceName.MERGE_OF_CONTRAIL_2_AND_3,
102           LoggerErrorDescription.MERGE_CONTRAIL_2_AND_3);
103     }
104   }
105
106   private Optional<ContrailResourcesMappingTo> collectHeatFileContrailResources(
107       GlobalValidationContext globalContext, String fileName) {
108     Optional<InputStream> fileContent = globalContext.getFileContent(fileName);
109     if (!fileContent.isPresent()) {
110       globalContext.addMessage(fileName, ErrorLevel.ERROR, ErrorMessagesFormatBuilder
111           .getErrorWithParameters(ERROR_CODE_CTL_1, Messages.INVALID_HEAT_FORMAT_REASON
112                   .getErrorMessage(),
113           "The file '" + fileName + "' has no content"),
114           LoggerTragetServiceName.VALIDATE_HEAT_FORMAT, LoggerErrorDescription.INVALID_HEAT_FORMAT);
115       return Optional.empty();
116     }
117     return fetchContrailResourcesMapping(fileName, fileContent.get());
118   }
119
120   private Optional<ContrailResourcesMappingTo> fetchContrailResourcesMapping(String fileName,
121           InputStream fileContent ) {
122     ContrailResourcesMappingTo contrailResourcesMappingTo = new ContrailResourcesMappingTo();
123     HeatOrchestrationTemplate heatOrchestrationTemplate;
124     try {
125       heatOrchestrationTemplate =
126           new YamlUtil().yamlToObject(fileContent, HeatOrchestrationTemplate.class);
127     } catch (Exception ignored) {
128       LOGGER.debug("",ignored);
129       // the HeatValidator should handle file that is failing to parse
130       return Optional.empty();
131     }
132     if( !MapUtils.isEmpty(heatOrchestrationTemplate.getResources())) {
133       heatOrchestrationTemplate.getResources().entrySet()
134           .forEach(entry -> {
135             if (entry.getValue().getType().startsWith(HeatConstants.CONTRAIL_RESOURCE_PREFIX)) {
136               contrailResourcesMappingTo.addContrailV1Resource(fileName, entry.getKey());
137             } else if (entry.getValue().getType()
138                 .startsWith(HeatConstants.CONTRAIL_V2_RESOURCE_PREFIX)) {
139               contrailResourcesMappingTo.addContrailV2Resource(fileName, entry.getKey());
140             }
141           });
142     }
143     return Optional.of(contrailResourcesMappingTo);
144   }
145
146
147   private void validateNoContrailResourceTypeIsInUse(String fileName,
148                                                      GlobalValidationContext globalContext) {
149     globalContext.setMessageCode(ERROR_CODE_CTL_4);
150     HeatOrchestrationTemplate heatOrchestrationTemplate =
151         ValidationUtil.checkHeatOrchestrationPreCondition(fileName, globalContext);
152
153     if (heatOrchestrationTemplate == null) {
154       return;
155     }
156     validateResourcePrefix(fileName, globalContext, heatOrchestrationTemplate);
157   }
158
159   private void validateResourcePrefix(String fileName, GlobalValidationContext globalContext,
160                                       HeatOrchestrationTemplate heatOrchestrationTemplate) {
161     Map<String, Resource> resourcesMap = heatOrchestrationTemplate.getResources();
162     if(!MapUtils.isEmpty(resourcesMap)) {
163       for (Map.Entry<String, Resource> resourceEntry : resourcesMap.entrySet()) {
164         String type = resourceEntry.getValue().getType();
165         if (Objects.nonNull(type) && type.startsWith(HeatConstants.CONTRAIL_RESOURCE_PREFIX)) {
166           globalContext.addMessage(fileName, ErrorLevel.WARNING, ErrorMessagesFormatBuilder
167               .getErrorWithParameters(ERROR_CODE_CTL_3, Messages.CONTRAIL_2_IN_USE.getErrorMessage(),
168               resourceEntry.getKey()), LoggerTragetServiceName.CONTRAIL_2_IN_USE,
169               LoggerErrorDescription.CONTRAIL_2_IN_USE);
170         }
171       }
172     }
173   }
174
175 }