e38c1b3b371ce8142bea3c952ade99fb7e41da90
[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.vendorsoftwareproduct.impl;
18
19 import org.apache.commons.collections4.CollectionUtils;
20 import org.apache.commons.collections4.MapUtils;
21 import org.openecomp.core.enrichment.api.EnrichmentManager;
22 import org.openecomp.core.enrichment.factory.EnrichmentManagerFactory;
23 import org.openecomp.core.model.dao.EnrichedServiceModelDao;
24 import org.openecomp.core.model.dao.ServiceModelDao;
25 import org.openecomp.core.model.types.ServiceElement;
26 import org.openecomp.core.util.UniqueValueUtil;
27 import org.openecomp.core.utilities.file.FileContentHandler;
28 import org.openecomp.core.utilities.json.JsonSchemaDataGenerator;
29 import org.openecomp.core.utilities.json.JsonUtil;
30 import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
31 import org.openecomp.core.validation.api.ValidationManager;
32 import org.openecomp.core.validation.util.MessageContainerUtil;
33 import org.openecomp.sdc.common.errors.CoreException;
34 import org.openecomp.sdc.common.errors.ErrorCode;
35 import org.openecomp.sdc.common.errors.ValidationErrorBuilder;
36 import org.openecomp.sdc.common.utils.CommonUtil;
37 import org.openecomp.sdc.datatypes.error.ErrorLevel;
38 import org.openecomp.sdc.datatypes.error.ErrorMessage;
39 import org.openecomp.sdc.logging.api.Logger;
40 import org.openecomp.sdc.logging.api.LoggerFactory;
41 import org.openecomp.sdc.logging.context.impl.MdcDataDebugMessage;
42 import org.openecomp.sdc.logging.context.impl.MdcDataErrorMessage;
43 import org.openecomp.sdc.logging.messages.AuditMessages;
44 import org.openecomp.sdc.logging.types.LoggerConstants;
45 import org.openecomp.sdc.logging.types.LoggerErrorCode;
46 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
47 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
48 import org.openecomp.sdc.tosca.services.impl.ToscaFileOutputServiceCsarImpl;
49 import org.openecomp.sdc.validation.util.ValidationManagerUtil;
50 import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacade;
51 import org.openecomp.sdc.vendorlicense.licenseartifacts.VendorLicenseArtifactsService;
52 import org.openecomp.sdc.vendorsoftwareproduct.ManualVspToscaManager;
53 import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductConstants;
54 import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductManager;
55 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDao;
56 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDependencyModelDao;
57 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComputeDao;
58 import org.openecomp.sdc.vendorsoftwareproduct.dao.DeploymentFlavorDao;
59 import org.openecomp.sdc.vendorsoftwareproduct.dao.ImageDao;
60 import org.openecomp.sdc.vendorsoftwareproduct.dao.NicDao;
61 import org.openecomp.sdc.vendorsoftwareproduct.dao.OrchestrationTemplateDao;
62 import org.openecomp.sdc.vendorsoftwareproduct.dao.PackageInfoDao;
63 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductInfoDao;
64 import org.openecomp.sdc.vendorsoftwareproduct.dao.errors.VendorSoftwareProductNotFoundErrorBuilder;
65 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentDependencyModelEntity;
66 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity;
67 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComputeEntity;
68 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.DeploymentFlavorEntity;
69 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ImageEntity;
70 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.NicEntity;
71 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OnboardingMethod;
72 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OrchestrationTemplateEntity;
73 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.PackageInfo;
74 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
75 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspQuestionnaireEntity;
76 import org.openecomp.sdc.vendorsoftwareproduct.errors.ComponentDependencyModelErrorBuilder;
77 import org.openecomp.sdc.vendorsoftwareproduct.errors.ComponentErrorBuilder;
78 import org.openecomp.sdc.vendorsoftwareproduct.errors.DeploymentFlavorErrorBuilder;
79 import org.openecomp.sdc.vendorsoftwareproduct.errors.FileCreationErrorBuilder;
80 import org.openecomp.sdc.vendorsoftwareproduct.errors.InformationArtifactCreationErrorBuilder;
81 import org.openecomp.sdc.vendorsoftwareproduct.errors.NicInternalNetworkErrorBuilder;
82 import org.openecomp.sdc.vendorsoftwareproduct.errors.PackageInvalidErrorBuilder;
83 import org.openecomp.sdc.vendorsoftwareproduct.errors.PackageNotFoundErrorBuilder;
84 import org.openecomp.sdc.vendorsoftwareproduct.errors.TranslationFileCreationErrorBuilder;
85 import org.openecomp.sdc.vendorsoftwareproduct.errors.VendorSoftwareProductInvalidErrorBuilder;
86 import org.openecomp.sdc.vendorsoftwareproduct.factory.CompositionEntityDataManagerFactory;
87 import org.openecomp.sdc.vendorsoftwareproduct.informationArtifact.InformationArtifactGenerator;
88 import org.openecomp.sdc.vendorsoftwareproduct.services.composition.CompositionEntityDataManager;
89 import org.openecomp.sdc.vendorsoftwareproduct.services.schemagenerator.SchemaGenerator;
90 import org.openecomp.sdc.vendorsoftwareproduct.types.QuestionnaireResponse;
91 import org.openecomp.sdc.vendorsoftwareproduct.types.QuestionnaireValidationResult;
92 import org.openecomp.sdc.vendorsoftwareproduct.types.ValidationResponse;
93 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.ComponentComputeAssociation;
94 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityId;
95 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityType;
96 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityValidationData;
97 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.DeploymentFlavor;
98 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.NetworkType;
99 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.Nic;
100 import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.ComponentQuestionnaireSchemaInput;
101 import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.SchemaTemplateContext;
102 import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.SchemaTemplateInput;
103 import org.openecomp.sdc.vendorsoftwareproduct.utils.ComponentDependencyTracker;
104 import org.openecomp.sdc.versioning.VersioningUtil;
105 import org.openecomp.sdc.versioning.dao.types.Version;
106
107 import java.io.BufferedOutputStream;
108 import java.io.ByteArrayInputStream;
109 import java.io.ByteArrayOutputStream;
110 import java.io.File;
111 import java.io.FileOutputStream;
112 import java.io.IOException;
113 import java.io.OutputStream;
114 import java.nio.ByteBuffer;
115 import java.util.*;
116 import java.util.zip.ZipInputStream;
117 import java.util.zip.ZipOutputStream;
118
119 public class VendorSoftwareProductManagerImpl implements VendorSoftwareProductManager {
120   private static final MdcDataDebugMessage MDC_DATA_DEBUG_MESSAGE = new MdcDataDebugMessage();
121   private static final Logger LOGGER =
122       LoggerFactory.getLogger(VendorSoftwareProductManagerImpl.class);
123
124   private final OrchestrationTemplateDao orchestrationTemplateDao;
125   private final VendorSoftwareProductInfoDao vspInfoDao;
126   private final VendorLicenseFacade vendorLicenseFacade;
127   private final ServiceModelDao<ToscaServiceModel, ServiceElement> serviceModelDao;
128   private final EnrichedServiceModelDao<ToscaServiceModel, ServiceElement> enrichedServiceModelDao;
129   private final VendorLicenseArtifactsService licenseArtifactsService;
130   private final InformationArtifactGenerator informationArtifactGenerator;
131   private final PackageInfoDao packageInfoDao;
132   private final DeploymentFlavorDao deploymentFlavorDao;
133   private final ComponentDao componentDao;
134   private final ComponentDependencyModelDao componentDependencyModelDao;
135   private final NicDao nicDao;
136   private final ComputeDao computeDao;
137   private final ImageDao imageDao;
138   private final ManualVspToscaManager manualVspToscaManager;
139   private static final String VSP_ID = "VSP id";
140   private static final String PACKAGE_NOT_FOUND = "Package not found";
141
142   public VendorSoftwareProductManagerImpl(
143           OrchestrationTemplateDao orchestrationTemplateDataDao,
144           VendorSoftwareProductInfoDao vspInfoDao,
145           VendorLicenseFacade vendorLicenseFacade,
146           ServiceModelDao<ToscaServiceModel, ServiceElement> serviceModelDao,
147           EnrichedServiceModelDao<ToscaServiceModel, ServiceElement> enrichedServiceModelDao,
148           VendorLicenseArtifactsService licenseArtifactsService,
149           InformationArtifactGenerator informationArtifactGenerator,
150           PackageInfoDao packageInfoDao,
151           DeploymentFlavorDao deploymentFlavorDao,
152           ComponentDao componentDao,
153           ComponentDependencyModelDao componentDependencyModelDao,
154           NicDao nicDao,
155           ComputeDao computeDao,
156           ImageDao imageDao,
157           ManualVspToscaManager manualVspToscaManager) {
158     this.orchestrationTemplateDao = orchestrationTemplateDataDao;
159     this.vspInfoDao = vspInfoDao;
160     this.vendorLicenseFacade = vendorLicenseFacade;
161     this.serviceModelDao = serviceModelDao;
162     this.enrichedServiceModelDao = enrichedServiceModelDao;
163     this.licenseArtifactsService = licenseArtifactsService;
164     this.informationArtifactGenerator = informationArtifactGenerator;
165     this.packageInfoDao = packageInfoDao;
166     this.deploymentFlavorDao = deploymentFlavorDao;
167     this.componentDao = componentDao;
168     this.componentDependencyModelDao = componentDependencyModelDao;
169     this.nicDao = nicDao;
170     this.computeDao = computeDao;
171     this.imageDao = imageDao;
172     this.manualVspToscaManager = manualVspToscaManager;
173
174     registerToVersioning();
175   }
176   private void registerToVersioning() {
177     serviceModelDao.registerVersioning(
178         VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE);
179     enrichedServiceModelDao.registerVersioning(
180         VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE);
181   }
182
183
184   @Override
185   public ValidationResponse validate(String vspId, Version version) throws IOException {
186     MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(VSP_ID, vspId);
187
188     VspDetails vspDetails = getValidatedVsp(vspId, version);
189     Collection<ComponentDependencyModelEntity> componentDependencies =
190         componentDependencyModelDao.list(new ComponentDependencyModelEntity(vspId, version, null));
191
192     ValidationResponse validationResponse = new ValidationResponse();
193     validationResponse.setQuestionnaireValidationResult(
194         validateQuestionnaire(vspDetails.getId(), vspDetails.getVersion(),
195             vspDetails.getOnboardingMethod()));
196
197     List<ErrorCode> vspErrors = new ArrayList<>();
198     vspErrors.addAll(validateVspFields(vspDetails));
199     if (validateComponentDependencies(componentDependencies)) {
200       vspErrors
201           .add(ComponentDependencyModelErrorBuilder.getcyclicDependencyComponentErrorBuilder());
202     }
203     if (Objects.nonNull(vspDetails.getOnboardingMethod()) &&
204         OnboardingMethod.Manual.name().equals(vspDetails.getOnboardingMethod())) {
205       validateManualOnboardingMethod(vspDetails, validationResponse, vspErrors);
206     } else {
207       if (vspDetails.getVlmVersion() != null || vspDetails.getLicenseAgreement() != null
208           || vspDetails.getFeatureGroups() != null) {
209         vspErrors.addAll(validateMandatoryLicenseFields(vspDetails));
210       }
211       OrchestrationTemplateEntity orchestrationTemplate =
212           orchestrationTemplateDao.get(vspId, version);
213       ToscaServiceModel serviceModel =
214           serviceModelDao.getServiceModel(vspId, vspDetails.getVersion());
215       if (!isOrchestrationTemplateExist(orchestrationTemplate) ||
216           !isServiceModelExist(serviceModel)) {
217         vspErrors.add(VendorSoftwareProductInvalidErrorBuilder
218             .vendorSoftwareProductMissingServiceModelErrorBuilder(vspDetails.getId(),
219                 vspDetails.getVersion()));
220       }
221       validationResponse.setUploadDataErrors(validateOrchestrationTemplate(orchestrationTemplate),
222               LoggerTragetServiceName.SUBMIT_VSP);
223     }
224     validationResponse
225         .setVspErrors(vspErrors, LoggerTragetServiceName.SUBMIT_VSP);
226     validationResponse.setLicensingDataErrors(validateLicensingData(vspDetails));
227
228
229     MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID, vspId);
230     return validationResponse;
231   }
232
233   private void validateManualOnboardingMethod(VspDetails vspDetails,
234                                               ValidationResponse validationResponse,
235                                               List<ErrorCode> vspErrors) {
236     vspErrors.addAll(validateMandatoryLicenseFields(vspDetails));
237
238     Collection<DeploymentFlavorEntity> deploymentFlavors = deploymentFlavorDao
239         .list(new DeploymentFlavorEntity(vspDetails.getId(), vspDetails.getVersion(), null));
240     if (CollectionUtils.isEmpty(deploymentFlavors)) {
241       vspErrors
242           .add(VendorSoftwareProductInvalidErrorBuilder.vspMissingDeploymentFlavorErrorBuilder());
243     }
244     vspErrors.addAll(validateDeploymentFlavors(deploymentFlavors));
245
246     Set<CompositionEntityValidationData> componentValidationResult =
247         componentValidation(vspDetails.getId(), vspDetails.getVersion());
248     if (!CollectionUtils.isEmpty(componentValidationResult)) {
249       if (validationResponse.getQuestionnaireValidationResult() == null ||
250           validationResponse.getQuestionnaireValidationResult().getValidationData() == null) {
251         validationResponse.setQuestionnaireValidationResult(
252             new QuestionnaireValidationResult(componentValidationResult));
253       } else {
254         validationResponse.getQuestionnaireValidationResult().getValidationData()
255             .addAll(componentValidationResult);
256       }
257     }
258   }
259
260   @Override
261   public Map<String, List<ErrorMessage>> compile(String vspId, Version version) {
262     MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(VSP_ID, vspId);
263
264     ToscaServiceModel serviceModel =
265         OnboardingMethod.Manual.name().equals(getValidatedVsp(vspId, version).getOnboardingMethod())
266             //Generate Tosca service model for Manual Onboarding flow
267             ? manualVspToscaManager
268             .generateToscaModel(manualVspToscaManager.gatherVspInformation(vspId, version))
269             : serviceModelDao.getServiceModel(vspId, version);
270
271     Map<String, List<ErrorMessage>> compilationErrors = compile(vspId, version, serviceModel);
272     MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID, vspId);
273     return compilationErrors;
274   }
275
276   private boolean validateComponentDependencies(
277       Collection<ComponentDependencyModelEntity> componentDependencies) {
278     ComponentDependencyTracker dependencyTracker = new ComponentDependencyTracker();
279
280     for (ComponentDependencyModelEntity componentDependency : componentDependencies) {
281       dependencyTracker.addDependency(componentDependency.getSourceComponentId(),
282           componentDependency.getTargetComponentId());
283     }
284     return dependencyTracker.isCyclicDependencyPresent();
285   }
286
287   private Collection<ErrorCode> validateDeploymentFlavors(
288       Collection<DeploymentFlavorEntity> deploymentFlavors) {
289
290     Collection<ErrorCode> errorCodeList = new ArrayList<>();
291     if (!CollectionUtils.isEmpty(deploymentFlavors)) {
292       deploymentFlavors.forEach(deploymentFlavor -> {
293         DeploymentFlavorEntity deployment = deploymentFlavorDao.get(deploymentFlavor);
294         DeploymentFlavor deploymentlocalFlavor = deployment.getDeploymentFlavorCompositionData();
295         if (deploymentlocalFlavor != null) {
296           if (deploymentlocalFlavor.getFeatureGroupId() == null) {
297             ErrorCode deploymentFlavorErrorBuilder = DeploymentFlavorErrorBuilder.
298                 getFeatureGroupMandatoryErrorBuilder(deploymentlocalFlavor.getModel());
299             errorCodeList.add(deploymentFlavorErrorBuilder);
300           }
301           validateComponetComputeAssociations(errorCodeList, deploymentFlavor,
302                   deployment, deploymentlocalFlavor);
303         }
304       });
305     }
306     return errorCodeList;
307   }
308
309   private void validateComponetComputeAssociations(Collection<ErrorCode> errorCodeList,
310                                                    DeploymentFlavorEntity deploymentFlavor,
311                                                    DeploymentFlavorEntity deployment,
312                                                    DeploymentFlavor deploymentlocalFlavor) {
313     List<ComponentComputeAssociation> componetComputeAssociations =
314         deploymentlocalFlavor.getComponentComputeAssociations();
315     if (CollectionUtils.isEmpty(componetComputeAssociations)) {
316       CompositionEntityValidationData compositionEntityValidationData = new
317           CompositionEntityValidationData(CompositionEntityType.deployment, deploymentFlavor
318           .getId());
319       compositionEntityValidationData.setEntityName(deployment
320           .getDeploymentFlavorCompositionData().getModel());
321       ErrorCode deploymentFlavorErrorBuilder = DeploymentFlavorErrorBuilder
322           .getInvalidComponentComputeAssociationErrorBuilder(
323               deploymentlocalFlavor.getModel());
324
325       errorCodeList.add(deploymentFlavorErrorBuilder);
326     } else {
327       componetComputeAssociations.forEach(componetComputeAssociation -> {
328         if (componetComputeAssociation == null
329             || !(componetComputeAssociation.getComponentId() != null
330             && componetComputeAssociation.getComputeFlavorId() != null)) {
331           CompositionEntityValidationData compositionEntityValidationData = new
332               CompositionEntityValidationData(CompositionEntityType.deployment,
333               deploymentFlavor.getId());
334           compositionEntityValidationData.setEntityName(deployment
335               .getDeploymentFlavorCompositionData().getModel());
336           ErrorCode deploymentFlavorErrorBuilder = DeploymentFlavorErrorBuilder
337               .getInvalidComponentComputeAssociationErrorBuilder(
338                   deploymentlocalFlavor.getModel());
339
340           errorCodeList.add(deploymentFlavorErrorBuilder);
341         }
342       });
343     }
344   }
345
346   private Set<CompositionEntityValidationData> componentValidation(String vspId, Version version) {
347     MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(VSP_ID, vspId);
348
349     Set<CompositionEntityValidationData> validationData = new HashSet<>();
350     Collection<ComponentEntity> components =
351         componentDao.list(new ComponentEntity(vspId, version, null));
352     if (!CollectionUtils.isEmpty(components)) {
353       components.forEach(component -> {
354         validateImage(vspId, version, validationData, component);
355         validateNic(vspId, version, validationData, component);
356
357       });
358     }
359
360     return validationData;
361   }
362
363   private void validateNic(String vspId, Version version,
364                            Set<CompositionEntityValidationData> validationData,
365                            ComponentEntity component) {
366     Collection<NicEntity> nics =
367         nicDao.list(new NicEntity(vspId, version, component.getId(), null));
368     if (CollectionUtils.isNotEmpty(nics)) {
369       nics.forEach(nicEntity -> {
370         NicEntity nic = nicDao.get(new NicEntity(vspId, version, component.getId(),
371             nicEntity.getId()));
372         NetworkType networkType = nic.getNicCompositionData().getNetworkType();
373         String networkId = nic.getNicCompositionData().getNetworkId();
374         if (networkType.equals(NetworkType.Internal) && networkId == null) {
375           CompositionEntityValidationData compositionEntityValidationData = new
376               CompositionEntityValidationData(CompositionEntityType.nic, nic.getId());
377           compositionEntityValidationData.setEntityName(nic.getNicCompositionData().getName());
378           ErrorCode nicInternalNetworkErrorBuilder = NicInternalNetworkErrorBuilder
379               .getNicNullNetworkIdInternalNetworkIdErrorBuilder();
380           List<String> errors = new ArrayList<>();
381           errors.add(nicInternalNetworkErrorBuilder.message());
382           compositionEntityValidationData.setErrors(errors);
383           validationData.add(compositionEntityValidationData);
384         }
385       });
386     }
387   }
388
389   private void validateImage(String vspId, Version version,
390                              Set<CompositionEntityValidationData> validationData,
391                              ComponentEntity component) {
392     Collection<ImageEntity> images =
393         imageDao.list(new ImageEntity(vspId, version, component.getId(), null));
394     if (CollectionUtils.isEmpty(images)) {
395       CompositionEntityValidationData compositionEntityValidationData = new
396           CompositionEntityValidationData(component.getType(), component.getId());
397       compositionEntityValidationData
398           .setEntityName(component.getComponentCompositionData().getDisplayName());
399       ErrorCode vfcMissingImageErrorBuilder =
400           ComponentErrorBuilder.vfcMissingImageErrorBuilder();
401       List<String> errors = new ArrayList<>();
402       errors.add(vfcMissingImageErrorBuilder.message());
403       compositionEntityValidationData.setErrors(errors);
404       validationData.add(compositionEntityValidationData);
405     }
406   }
407
408   private List<ErrorCode> validateVspFields(VspDetails vspDetails) {
409     List<ErrorCode> errors = new ArrayList<>();
410
411     if (vspDetails.getName() == null) {
412       errors.add(createMissingMandatoryFieldError("name"));
413     }
414     if (vspDetails.getDescription() == null) {
415       errors.add(createMissingMandatoryFieldError("description"));
416     }
417     if (vspDetails.getVendorId() == null) {
418       errors.add(createMissingMandatoryFieldError("vendor Id"));
419     }
420     if (vspDetails.getCategory() == null) {
421       errors.add(createMissingMandatoryFieldError("category"));
422     }
423     if (vspDetails.getSubCategory() == null) {
424       errors.add(createMissingMandatoryFieldError("sub category"));
425     }
426     return errors;
427   }
428
429   private List<ErrorCode> validateMandatoryLicenseFields(VspDetails vspDetails) {
430     List<ErrorCode> errors = new ArrayList<>();
431     if (vspDetails.getVlmVersion() == null) {
432       errors.add(createMissingMandatoryFieldError("licensing version"));
433     }
434     if (vspDetails.getLicenseAgreement() == null) {
435       errors.add(createMissingMandatoryFieldError("license agreement"));
436     }
437     if (CollectionUtils.isEmpty(vspDetails.getFeatureGroups())) {
438       errors.add(createMissingMandatoryFieldError("feature groups"));
439     }
440     return errors;
441   }
442
443   private static ErrorCode createMissingMandatoryFieldError(String fieldName) {
444     return new ValidationErrorBuilder("must be supplied", fieldName).build();
445   }
446
447   private Map<String, List<ErrorMessage>> compile(String vendorSoftwareProductId, Version version,
448                                                   ToscaServiceModel serviceModel) {
449     if (!isServiceModelExist(serviceModel)) {
450       return null;
451     }
452
453     enrichedServiceModelDao.deleteAll(vendorSoftwareProductId, version);
454
455     EnrichmentManager<ToscaServiceModel> enrichmentManager =
456         EnrichmentManagerFactory.getInstance().createInterface();
457     enrichmentManager.init(vendorSoftwareProductId, version);
458     enrichmentManager.setModel(serviceModel);
459     Map<String, List<ErrorMessage>> enrichErrors = enrichmentManager.enrich();
460
461     if (MapUtils.isEmpty(MessageContainerUtil.getMessageByLevel(ErrorLevel.ERROR, enrichErrors))) {
462       LOGGER.audit(AuditMessages.AUDIT_MSG + AuditMessages.ENRICHMENT_COMPLETED
463           + vendorSoftwareProductId);
464     } else {
465       enrichErrors.values().forEach(errorList ->
466           auditIfContainsErrors(errorList, vendorSoftwareProductId,
467               AuditMessages.ENRICHMENT_ERROR));
468     }
469
470     enrichedServiceModelDao
471         .storeServiceModel(vendorSoftwareProductId, version, enrichmentManager.getModel());
472
473     return enrichErrors;
474   }
475
476   private Collection<ErrorCode> validateLicensingData(VspDetails vspDetails) {
477     MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(VSP_ID, vspDetails.getId());
478
479     if (vspDetails.getVendorId() == null || vspDetails.getVlmVersion() == null
480         || vspDetails.getLicenseAgreement() == null
481         || CollectionUtils.isEmpty(vspDetails.getFeatureGroups())) {
482       return Collections.emptyList();
483     }
484
485     MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID, vspDetails.getId());
486     return vendorLicenseFacade
487         .validateLicensingData(vspDetails.getVendorId(), vspDetails.getVlmVersion(),
488             vspDetails.getLicenseAgreement(), vspDetails.getFeatureGroups());
489   }
490
491   @Override
492   public VspDetails createVsp(VspDetails vspDetails) {
493     MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(null);
494
495     vspInfoDao.create(vspDetails);
496     vspInfoDao.updateQuestionnaireData(vspDetails.getId(), vspDetails.getVersion(),
497         new JsonSchemaDataGenerator(getVspQuestionnaireSchema(null)).generateData());
498
499     MDC_DATA_DEBUG_MESSAGE.debugExitMessage(null);
500     return vspDetails;
501   }
502
503   @Override
504   public void updateVsp(VspDetails vspDetails) {
505     MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(VSP_ID, vspDetails.getId());
506
507     VspDetails retrieved = vspInfoDao.get(vspDetails);
508     // TODO: 6/21/2017 remove this validation when validation will be added in the REST level
509     if (retrieved == null) {
510       throw new RuntimeException(String.format("Vsp with id %s and version %s does not exist.",
511           vspDetails.getId(), vspDetails.getVersion().getId()));
512     }
513     vspDetails.setOnboardingMethod(retrieved.getOnboardingMethod());
514
515     //If any existing feature group is removed from VSP which is also associated in DF then
516     //update DF to remove feature group associations.
517     updateDeploymentFlavor(vspDetails);
518
519     updateUniqueName(retrieved.getName(), vspDetails.getName());
520     vspInfoDao.update(vspDetails);
521
522     MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID, vspDetails.getId());
523   }
524
525   private void updateDeploymentFlavor(VspDetails vspDetails) {
526     final List<String> featureGroups = vspDetails.getFeatureGroups();
527     if (featureGroups != null) {
528       final Collection<DeploymentFlavorEntity> deploymentFlavorEntities = deploymentFlavorDao
529           .list(new DeploymentFlavorEntity(vspDetails.getId(), vspDetails
530               .getVersion(), null));
531       if (Objects.nonNull(deploymentFlavorEntities)) {
532         for (DeploymentFlavorEntity deploymentFlavorEntity : deploymentFlavorEntities) {
533           updateDeploymentFlavourEntity(featureGroups, deploymentFlavorEntity);
534         }
535       }
536     }
537   }
538
539   private void updateDeploymentFlavourEntity(List<String> featureGroups,
540                                              DeploymentFlavorEntity deploymentFlavorEntity) {
541     final String featureGroupId =
542         deploymentFlavorEntity.getDeploymentFlavorCompositionData().getFeatureGroupId();
543     if (!featureGroups.contains(featureGroupId)) {
544       DeploymentFlavor deploymentFlavorCompositionData =
545           deploymentFlavorEntity.getDeploymentFlavorCompositionData();
546       deploymentFlavorCompositionData.setFeatureGroupId(null);
547       deploymentFlavorEntity.setDeploymentFlavorCompositionData
548           (deploymentFlavorCompositionData);
549       deploymentFlavorDao.update(deploymentFlavorEntity);
550     }
551   }
552
553
554   @Override
555   public VspDetails getVsp(String vspId, Version version) {
556     MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(VSP_ID, vspId);
557
558     VspDetails vsp = getValidatedVsp(vspId, version);
559
560     MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID, vspId);
561     return vsp;
562   }
563
564   private VspDetails getValidatedVsp(String vspId, Version version) {
565     VspDetails vsp = vspInfoDao.get(new VspDetails(vspId, version));
566     if (vsp == null) {
567       MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
568           LoggerTragetServiceName.GET_VSP, ErrorLevel.ERROR.name(),
569           LoggerErrorCode.DATA_ERROR.getErrorCode(), "Requested VSP not found");
570       throw new CoreException(new VendorSoftwareProductNotFoundErrorBuilder(vspId).build());
571     }
572     return vsp;
573   }
574
575   @Override
576   public void deleteVsp(String vspId) {
577     MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(VSP_ID, vspId);
578
579     MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
580         LoggerTragetServiceName.DELETE_VSP, ErrorLevel.ERROR.name(),
581         LoggerErrorCode.PERMISSION_ERROR.getErrorCode(), "Unsupported operation");
582     MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID, vspId);
583
584     throw new UnsupportedOperationException(
585         VendorSoftwareProductConstants.UNSUPPORTED_OPERATION_ERROR);
586   }
587
588   @Override
589   public List<PackageInfo> listPackages(String category, String subCategory) {
590     return packageInfoDao.listByCategory(category, subCategory);
591   }
592
593   @Override
594   public File getTranslatedFile(String vspId, Version version) {
595     MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(VSP_ID, vspId);
596     String errorMessage;
597
598     PackageInfo packageInfo = packageInfoDao.get(new PackageInfo(vspId, version));
599     if (packageInfo == null) {
600       errorMessage = PACKAGE_NOT_FOUND;
601       MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
602           LoggerTragetServiceName.GET_TRANSLATED_FILE, ErrorLevel.ERROR.name(),
603           LoggerErrorCode.DATA_ERROR.getErrorCode(), errorMessage);
604       throw new CoreException(new PackageNotFoundErrorBuilder(vspId, version).build());
605     }
606
607     ByteBuffer translatedFileBuffer = packageInfo.getTranslatedFile();
608     if (translatedFileBuffer == null) {
609       errorMessage = PACKAGE_NOT_FOUND;
610       MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
611           LoggerTragetServiceName.GET_TRANSLATED_FILE, ErrorLevel.ERROR.name(),
612           LoggerErrorCode.DATA_ERROR.getErrorCode(), errorMessage);
613       throw new CoreException(new PackageInvalidErrorBuilder(vspId, version).build());
614     }
615
616     File translatedFile = new File(VendorSoftwareProductConstants.VSP_PACKAGE_ZIP);
617
618     try (FileOutputStream fos = new FileOutputStream(translatedFile)) {
619       fos.write(translatedFileBuffer.array());
620     } catch (IOException exception) {
621       errorMessage = "Can't create package";
622       MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
623           LoggerTragetServiceName.CREATE_TRANSLATED_FILE, ErrorLevel.ERROR.name(),
624           LoggerErrorCode.DATA_ERROR.getErrorCode(), errorMessage);
625       throw new CoreException(new TranslationFileCreationErrorBuilder(vspId, version).build(),
626           exception);
627     }
628
629     MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID, vspId);
630
631     return translatedFile;
632   }
633
634   @Override
635
636   public byte[] getOrchestrationTemplateFile(String vspId, Version version) {
637     MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(VSP_ID, vspId);
638
639     OrchestrationTemplateEntity uploadData = orchestrationTemplateDao.get(vspId, version);
640     ByteBuffer contentData = uploadData.getContentData();
641     if (contentData == null) {
642       return null;
643     }
644
645     ByteArrayOutputStream baos = new ByteArrayOutputStream();
646
647     try (final ZipOutputStream zos = new ZipOutputStream(baos);
648          ZipInputStream ignored = new ZipInputStream(
649              new ByteArrayInputStream(contentData.array()))) {
650       zos.write(contentData.array());
651     } catch (IOException exception) {
652       MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
653           LoggerTragetServiceName.GET_UPLOADED_HEAT, ErrorLevel.ERROR.name(),
654           LoggerErrorCode.DATA_ERROR.getErrorCode(), "Can't get uploaded HEAT");
655       throw new CoreException(new FileCreationErrorBuilder(vspId).build(), exception);
656     }
657
658     MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID, vspId);
659     return baos.toByteArray();
660   }
661
662   @Override
663   public OrchestrationTemplateEntity getOrchestrationTemplateInfo(String vspId, Version version) {
664     return orchestrationTemplateDao.getInfo(vspId, version);
665   }
666
667   @Override
668   public PackageInfo createPackage(String vspId, Version version) throws IOException {
669     MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(VSP_ID, vspId);
670
671     ToscaServiceModel toscaServiceModel = enrichedServiceModelDao.getServiceModel(vspId, version);
672     VspDetails vspDetails = vspInfoDao.get(new VspDetails(vspId, version));
673     Version vlmVersion = vspDetails.getVlmVersion();
674
675     PackageInfo packageInfo = createPackageInfo(vspDetails);
676
677     ToscaFileOutputServiceCsarImpl toscaServiceTemplateServiceCsar =
678         new ToscaFileOutputServiceCsarImpl();
679     FileContentHandler licenseArtifacts = licenseArtifactsService
680         .createLicenseArtifacts(vspDetails.getId(), vspDetails.getVendorId(), vlmVersion,
681             vspDetails.getFeatureGroups());
682     //todo add tosca validation here
683     packageInfo.setTranslatedFile(ByteBuffer.wrap(
684         toscaServiceTemplateServiceCsar.createOutputFile(toscaServiceModel, licenseArtifacts)));
685
686     packageInfoDao.create(packageInfo);
687
688     LOGGER.audit(AuditMessages.AUDIT_MSG + AuditMessages.CREATE_PACKAGE + vspId);
689
690     MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID, vspId);
691     return packageInfo;
692   }
693
694   private PackageInfo createPackageInfo(VspDetails vspDetails) {
695     PackageInfo packageInfo = new PackageInfo(vspDetails.getId(), vspDetails.getVersion());
696     packageInfo.setVspName(vspDetails.getName());
697     packageInfo.setVspDescription(vspDetails.getDescription());
698     packageInfo.setCategory(vspDetails.getCategory());
699     packageInfo.setSubCategory(vspDetails.getSubCategory());
700     packageInfo.setVendorName(vspDetails.getVendorName());
701     packageInfo.setPackageType(VendorSoftwareProductConstants.CSAR);
702     packageInfo.setVendorRelease("1.0"); //todo TBD
703     return packageInfo;
704   }
705
706   @Override
707
708   public QuestionnaireResponse getVspQuestionnaire(String vspId, Version version) {
709     MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(VSP_ID, vspId);
710
711     VspQuestionnaireEntity retrieved = vspInfoDao.getQuestionnaire(vspId, version);
712     VersioningUtil.validateEntityExistence(retrieved, new VspQuestionnaireEntity(vspId, version),
713         VspDetails.ENTITY_TYPE);
714
715     String questionnaireData = retrieved.getQuestionnaireData();
716
717     QuestionnaireResponse questionnaireResponse = new QuestionnaireResponse();
718     questionnaireResponse.setData(questionnaireData);
719     questionnaireResponse.setSchema(getVspQuestionnaireSchema(null));
720
721     MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID, vspId);
722
723     return questionnaireResponse;
724   }
725
726   @Override
727   public void updateVspQuestionnaire(String vspId, Version version, String questionnaireData) {
728     MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(VSP_ID, vspId);
729
730     vspInfoDao.updateQuestionnaireData(vspId, version, questionnaireData);
731
732     MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID, vspId);
733   }
734
735
736   private Map<String, List<ErrorMessage>> validateOrchestrationTemplate(
737       OrchestrationTemplateEntity orchestrationTemplate) throws IOException {
738
739     if (!isOrchestrationTemplateExist(orchestrationTemplate)) {
740       return null;
741     }
742     Map<String, List<ErrorMessage>> validationErrors = new HashMap<>();
743
744     FileContentHandler fileContentMap = CommonUtil.validateAndUploadFileContent(
745         OnboardingTypesEnum.getOnboardingTypesEnum(orchestrationTemplate.getFileSuffix()),
746         orchestrationTemplate.getContentData().array());
747
748     if (CommonUtil.isFileOriginFromZip(orchestrationTemplate.getFileSuffix())) {
749       ValidationManager validationManager =
750           ValidationManagerUtil.initValidationManager(fileContentMap);
751       validationErrors.putAll(validationManager.validate());
752     }
753
754     return
755         MapUtils.isEmpty(MessageContainerUtil.getMessageByLevel(ErrorLevel.ERROR, validationErrors))
756             ? null : validationErrors;
757   }
758
759   private QuestionnaireValidationResult validateQuestionnaire(String vspId, Version version,
760                                                               String onboardingMethod) {
761     MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(VSP_ID, vspId);
762
763     // The apis of CompositionEntityDataManager used here are stateful!
764     // so, it must be re-created from scratch when it is used!
765     CompositionEntityDataManager compositionEntityDataManager =
766         CompositionEntityDataManagerFactory.getInstance().createInterface();
767     compositionEntityDataManager
768         .addEntity(vspInfoDao.getQuestionnaire(vspId, version), null);
769
770     Collection<NicEntity> nics = nicDao.listByVsp(vspId, version);
771
772     Map<String, List<String>> nicNamesByComponent = new HashMap<>();
773     for (NicEntity nicEntity : nics) {
774       compositionEntityDataManager.addEntity(nicEntity, null);
775
776       Nic nic = nicEntity.getNicCompositionData();
777       if (nic != null && nic.getName() != null) {
778         List<String> nicNames =
779             nicNamesByComponent.computeIfAbsent(nicEntity.getComponentId(), k -> new ArrayList<>());
780         nicNames.add(nic.getName());
781       }
782     }
783
784     Collection<ComponentEntity> components =
785         componentDao.listCompositionAndQuestionnaire(vspId, version);
786     components.forEach(component -> compositionEntityDataManager.addEntity(component,
787         new ComponentQuestionnaireSchemaInput(nicNamesByComponent.get(component.getId()),
788             JsonUtil.json2Object(component.getQuestionnaireData(), Map.class))));
789
790     Collection<ComputeEntity> computes = computeDao.listByVsp(vspId, version);
791     computes.forEach(compute -> compositionEntityDataManager.addEntity(compute, null));
792
793     if (OnboardingMethod.Manual.name().equals(onboardingMethod)) {
794       Collection<ImageEntity> images = imageDao.listByVsp(vspId, version);
795       images.forEach(image -> compositionEntityDataManager.addEntity(image, null));
796     }
797
798     Map<CompositionEntityId, Collection<String>> errorsByEntityId =
799         compositionEntityDataManager.validateEntitiesQuestionnaire();
800     if (MapUtils.isNotEmpty(errorsByEntityId)) {
801       compositionEntityDataManager.buildTrees();
802       compositionEntityDataManager.addErrorsToTrees(errorsByEntityId);
803
804       MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID, vspId);
805       return new QuestionnaireValidationResult(
806           compositionEntityDataManager.getAllErrorsByVsp(vspId));
807     }
808
809     MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID, vspId);
810     return null;
811   }
812
813   @Override
814   public File getInformationArtifact(String vspId, Version version) {
815     MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(VSP_ID, vspId);
816     VspDetails vspDetails = vspInfoDao.get(new VspDetails(vspId, version));
817
818     if (vspDetails == null) {
819       return null;
820     }
821
822     String vspName = vspDetails.getName();
823     ByteBuffer infoArtifactAsByteBuffer;
824     File infoArtifactFile;
825     try {
826       infoArtifactAsByteBuffer = ByteBuffer.wrap(informationArtifactGenerator.generate(vspId,
827           version).getBytes());
828
829       infoArtifactFile =
830           new File(
831               String.format(VendorSoftwareProductConstants.INFORMATION_ARTIFACT_NAME, vspName));
832       try (OutputStream out = new BufferedOutputStream(new FileOutputStream(infoArtifactFile))) {
833         out.write(infoArtifactAsByteBuffer.array());
834       }
835
836     } catch (IOException ex) {
837       throw new CoreException(new InformationArtifactCreationErrorBuilder(vspId).build(), ex);
838     }
839
840     MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID, vspId);
841     return infoArtifactFile;
842   }
843
844   String getVspQuestionnaireSchema(SchemaTemplateInput schemaInput) {
845     MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(null);
846     MDC_DATA_DEBUG_MESSAGE.debugExitMessage(null);
847     return SchemaGenerator
848         .generate(SchemaTemplateContext.questionnaire, CompositionEntityType.vsp, schemaInput);
849   }
850
851   void updateUniqueName(String oldVspName, String newVspName) {
852     UniqueValueUtil.updateUniqueValue(
853         VendorSoftwareProductConstants.UniqueValues.VENDOR_SOFTWARE_PRODUCT_NAME,
854         oldVspName, newVspName);
855   }
856
857   @Override
858   public Collection<ComputeEntity> getComputeByVsp(String vspId, Version version) {
859     return computeDao.listByVsp(vspId, version);
860   }
861
862   private boolean isOrchestrationTemplateExist(OrchestrationTemplateEntity orchestrationTemplate) {
863     return orchestrationTemplate != null &&
864         orchestrationTemplate.getContentData() != null &&
865         orchestrationTemplate.getFileSuffix() != null &&
866         orchestrationTemplate.getFileName() != null;
867   }
868
869   private boolean isServiceModelExist(ToscaServiceModel serviceModel) {
870     return serviceModel != null && serviceModel.getEntryDefinitionServiceTemplate() != null;
871   }
872
873   private void auditIfContainsErrors(List<ErrorMessage> errorList, String vspId, String auditType) {
874     errorList.forEach(errorMessage -> {
875       if (errorMessage.getLevel().equals(ErrorLevel.ERROR)) {
876         LOGGER.audit(AuditMessages.AUDIT_MSG + String.format(auditType, errorMessage.getMessage(),
877             vspId));
878       }
879     });
880   }
881
882
883 }