Merge "Added required false for root data type"
[sdc.git] / openecomp-be / lib / openecomp-sdc-validation-lib / openecomp-sdc-validation-impl / src / main / java / org / openecomp / sdc / validation / impl / validators / ContrailValidator.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.validation.impl.validators;
22
23 import org.apache.commons.collections4.MapUtils;
24 import org.openecomp.sdc.tosca.services.YamlUtil;
25 import org.openecomp.sdc.validation.Validator;
26 import org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder;
27 import org.openecomp.core.validation.types.GlobalValidationContext;
28 import org.openecomp.sdc.common.errors.Messages;
29 import org.openecomp.sdc.datatypes.error.ErrorLevel;
30 import org.openecomp.sdc.heat.datatypes.manifest.FileData;
31 import org.openecomp.sdc.heat.datatypes.manifest.ManifestContent;
32 import org.openecomp.sdc.heat.datatypes.model.HeatOrchestrationTemplate;
33 import org.openecomp.sdc.heat.datatypes.model.Resource;
34 import org.openecomp.sdc.heat.services.HeatConstants;
35 import org.openecomp.sdc.heat.services.manifest.ManifestUtil;
36 import org.openecomp.sdc.logging.api.Logger;
37 import org.openecomp.sdc.logging.api.LoggerFactory;
38 import org.openecomp.sdc.logging.context.impl.MdcDataDebugMessage;
39 import org.openecomp.sdc.logging.types.LoggerErrorDescription;
40 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
41 import org.openecomp.sdc.validation.tos.ContrailResourcesMappingTo;
42 import org.openecomp.sdc.validation.util.ValidationUtil;
43
44 import java.io.InputStream;
45 import java.util.Map;
46 import java.util.Objects;
47 import java.util.Optional;
48
49
50 public class ContrailValidator implements Validator {
51   public static final MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
52   protected static Logger logger = (Logger) LoggerFactory.getLogger(ContrailValidator.class);
53
54   @Override
55   public void validate(GlobalValidationContext globalContext) {
56     mdcDataDebugMessage.debugEntryMessage(null, null);
57
58     ManifestContent manifestContent;
59     try {
60       manifestContent = ValidationUtil.checkValidationPreCondition(globalContext);
61     } catch (Exception exception) {
62       logger.debug("",exception);
63       return;
64     }
65     Map<String, FileData.Type> fileTypeMap = ManifestUtil.getFileTypeMap(manifestContent);
66     ContrailResourcesMappingTo contrailResourcesMappingTo = new ContrailResourcesMappingTo();
67
68     globalContext.getFiles().stream()
69         .filter(fileName -> FileData.isHeatFile(fileTypeMap.get(fileName)))
70         .forEach(fileName -> validate(fileName, fileTypeMap,
71             contrailResourcesMappingTo, globalContext));
72
73     mdcDataDebugMessage.debugExitMessage(null, null);
74   }
75
76
77   private void validate(String fileName, Map<String, FileData.Type> fileTypeMap,
78                         ContrailResourcesMappingTo contrailResourcesMappingTo,
79                         GlobalValidationContext globalContext) {
80     handleContrailV1AndContrailV2ResourceMerging(fileName, fileTypeMap, contrailResourcesMappingTo,
81         globalContext);
82     validateNoContrailResourceTypeIsInUse(fileName, globalContext);
83   }
84
85
86   private void handleContrailV1AndContrailV2ResourceMerging(String fileName,
87                Map<String, FileData.Type> fileTypeMap,
88                ContrailResourcesMappingTo contrailResourcesMappingTo,
89                GlobalValidationContext globalContext) {
90
91
92     mdcDataDebugMessage.debugEntryMessage("file", fileName);
93
94     Optional<ContrailResourcesMappingTo> fileContrailResourcesMappingTo =
95         collectHeatFileContrailResources(globalContext, fileName);
96     if (fileContrailResourcesMappingTo.isPresent()) {
97       contrailResourcesMappingTo.addAll(fileContrailResourcesMappingTo.get());
98     }
99     addContrailMergeValidationMessageToGlobalContext(globalContext, contrailResourcesMappingTo);
100
101     mdcDataDebugMessage.debugExitMessage("file", fileName);
102   }
103
104   private void addContrailMergeValidationMessageToGlobalContext(
105       GlobalValidationContext globalContext,
106       ContrailResourcesMappingTo contrailResourcesMappingTo) {
107
108
109     mdcDataDebugMessage.debugEntryMessage(null, null);
110
111     if (!MapUtils.isEmpty(contrailResourcesMappingTo.getContrailV1Resources())
112         && !MapUtils.isEmpty(contrailResourcesMappingTo.getContrailV2Resources())) {
113       globalContext.addMessage(
114           contrailResourcesMappingTo.getContrailV1Resources().keySet().iterator().next(),
115           ErrorLevel.WARNING, ErrorMessagesFormatBuilder.getErrorWithParameters(
116               Messages.MERGE_OF_CONTRAIL2_AND_CONTRAIL3_RESOURCES.getErrorMessage(),
117               contrailResourcesMappingTo.fetchContrailV1Resources(),
118               contrailResourcesMappingTo.fetchContrailV2Resources()),
119           LoggerTragetServiceName.MERGE_OF_CONTRAIL_2_AND_3,
120           LoggerErrorDescription.MERGE_CONTRAIL_2_AND_3);
121     }
122
123     mdcDataDebugMessage.debugExitMessage(null, null);
124   }
125
126   private Optional<ContrailResourcesMappingTo> collectHeatFileContrailResources(
127       GlobalValidationContext globalContext, String fileName) {
128     Optional<InputStream> fileContent = globalContext.getFileContent(fileName);
129     if (!fileContent.isPresent()) {
130       globalContext.addMessage(fileName, ErrorLevel.ERROR, ErrorMessagesFormatBuilder
131               .getErrorWithParameters(Messages.INVALID_HEAT_FORMAT_REASON.getErrorMessage(),
132                   "The file '" + fileName + "' has no content"),
133           LoggerTragetServiceName.VALIDATE_HEAT_FORMAT, LoggerErrorDescription.INVALID_HEAT_FORMAT);
134       return Optional.empty();
135     }
136     return fetchContrailResourcesMapping(fileName, fileContent.get(), globalContext);
137   }
138
139   private Optional<ContrailResourcesMappingTo> fetchContrailResourcesMapping(String fileName,
140           InputStream fileContent,
141           GlobalValidationContext globalContext) {
142
143
144     mdcDataDebugMessage.debugEntryMessage("file", fileName);
145
146     ContrailResourcesMappingTo contrailResourcesMappingTo = new ContrailResourcesMappingTo();
147     HeatOrchestrationTemplate heatOrchestrationTemplate;
148     try {
149       heatOrchestrationTemplate =
150           new YamlUtil().yamlToObject(fileContent, HeatOrchestrationTemplate.class);
151     } catch (Exception ignored) {
152       logger.debug("",ignored);
153       // the HeatValidator should handle file that is failing to parse
154       mdcDataDebugMessage.debugExitMessage("file", fileName);
155       return Optional.empty();
156     }
157     if( !MapUtils.isEmpty(heatOrchestrationTemplate.getResources())) {
158       heatOrchestrationTemplate.getResources().entrySet()
159           .forEach(entry -> {
160             if (entry.getValue().getType().startsWith(HeatConstants.CONTRAIL_RESOURCE_PREFIX)) {
161               contrailResourcesMappingTo.addContrailV1Resource(fileName, entry.getKey());
162             } else if (entry.getValue().getType()
163                 .startsWith(HeatConstants.CONTRAIL_V2_RESOURCE_PREFIX)) {
164               contrailResourcesMappingTo.addContrailV2Resource(fileName, entry.getKey());
165             }
166           });
167     }
168
169     mdcDataDebugMessage.debugExitMessage("file", fileName);
170     return Optional.of(contrailResourcesMappingTo);
171   }
172
173
174   private void validateNoContrailResourceTypeIsInUse(String fileName,
175                                                      GlobalValidationContext globalContext) {
176
177     mdcDataDebugMessage.debugEntryMessage("file", fileName);
178
179     HeatOrchestrationTemplate heatOrchestrationTemplate =
180         ValidationUtil.checkHeatOrchestrationPreCondition(fileName, globalContext);
181
182     if (heatOrchestrationTemplate == null) {
183       return;
184     }
185
186     Map<String, Resource> resourcesMap = heatOrchestrationTemplate.getResources();
187
188     if( ! MapUtils.isEmpty(resourcesMap)) {
189       for (Map.Entry<String, Resource> resourceEntry : resourcesMap.entrySet()) {
190         String type = resourceEntry.getValue().getType();
191         if (Objects.nonNull(type) && type.startsWith(HeatConstants.CONTRAIL_RESOURCE_PREFIX)) {
192           globalContext.addMessage(fileName, ErrorLevel.WARNING, ErrorMessagesFormatBuilder
193                   .getErrorWithParameters(Messages.CONTRAIL_2_IN_USE.getErrorMessage(),
194                       resourceEntry.getKey()), LoggerTragetServiceName.CONTRAIL_2_IN_USE,
195               LoggerErrorDescription.CONTRAIL_2_IN_USE);
196         }
197       }
198     }
199
200     mdcDataDebugMessage.debugExitMessage("file", fileName);
201   }
202
203 }