9d2508fde2fa2434d55f17a7dc481953a535ca57
[sdc.git] /
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.vendorsoftwareproduct.impl;
22
23 import org.apache.commons.collections4.CollectionUtils;
24 import org.apache.commons.collections4.MapUtils;
25 import org.openecomp.core.enrichment.api.EnrichmentManager;
26 import org.openecomp.core.enrichment.factory.EnrichmentManagerFactory;
27 import org.openecomp.core.model.dao.EnrichedServiceModelDao;
28 import org.openecomp.core.model.dao.ServiceModelDao;
29 import org.openecomp.core.model.types.ServiceElement;
30 import org.openecomp.core.util.UniqueValueUtil;
31 import org.openecomp.core.utilities.file.FileContentHandler;
32 import org.openecomp.core.utilities.json.JsonSchemaDataGenerator;
33 import org.openecomp.core.utilities.json.JsonUtil;
34 import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
35 import org.openecomp.core.validation.api.ValidationManager;
36 import org.openecomp.core.validation.util.MessageContainerUtil;
37 import org.openecomp.sdc.activityLog.ActivityLogManager;
38 import org.openecomp.sdc.activitylog.dao.type.ActivityLogEntity;
39 import org.openecomp.sdc.common.errors.CoreException;
40 import org.openecomp.sdc.common.errors.ErrorCategory;
41 import org.openecomp.sdc.common.errors.ErrorCode;
42 import org.openecomp.sdc.common.errors.ValidationErrorBuilder;
43 import org.openecomp.sdc.common.utils.CommonUtil;
44 import org.openecomp.sdc.common.utils.SdcCommon;
45 import org.openecomp.sdc.datatypes.error.ErrorLevel;
46 import org.openecomp.sdc.datatypes.error.ErrorMessage;
47 import org.openecomp.sdc.generator.datatypes.tosca.VspModelInfo;
48 import org.openecomp.sdc.healing.api.HealingManager;
49 import org.openecomp.sdc.logging.api.Logger;
50 import org.openecomp.sdc.logging.api.LoggerFactory;
51 import org.openecomp.sdc.logging.context.impl.MdcDataDebugMessage;
52 import org.openecomp.sdc.logging.context.impl.MdcDataErrorMessage;
53 import org.openecomp.sdc.logging.messages.AuditMessages;
54 import org.openecomp.sdc.logging.types.LoggerConstants;
55 import org.openecomp.sdc.logging.types.LoggerErrorCode;
56 import org.openecomp.sdc.logging.types.LoggerServiceName;
57 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
58 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
59 import org.openecomp.sdc.tosca.services.impl.ToscaFileOutputServiceCsarImpl;
60 import org.openecomp.sdc.validation.util.ValidationManagerUtil;
61 import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacade;
62 import org.openecomp.sdc.vendorlicense.licenseartifacts.VendorLicenseArtifactsService;
63 import org.openecomp.sdc.vendorsoftwareproduct.ManualVspToscaManager;
64 import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductConstants;
65 import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductManager;
66 import org.openecomp.sdc.vendorsoftwareproduct.dao.DeploymentFlavorDao;
67 import org.openecomp.sdc.vendorsoftwareproduct.dao.NicDao;
68 import org.openecomp.sdc.vendorsoftwareproduct.dao.OrchestrationTemplateDao;
69 import org.openecomp.sdc.vendorsoftwareproduct.dao.PackageInfoDao;
70 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductDao;
71 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductInfoDao;
72 import org.openecomp.sdc.vendorsoftwareproduct.dao.errors.VendorSoftwareProductNotFoundErrorBuilder;
73 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentDependencyModelEntity;
74 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity;
75 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComputeEntity;
76 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.DeploymentFlavorEntity;
77 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ImageEntity;
78 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.NicEntity;
79 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.PackageInfo;
80 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.UploadDataEntity;
81 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
82 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspQuestionnaireEntity;
83 import org.openecomp.sdc.vendorsoftwareproduct.errors.ComponentDependencyModelErrorBuilder;
84 import org.openecomp.sdc.vendorsoftwareproduct.errors.ComponentErrorBuilder;
85 import org.openecomp.sdc.vendorsoftwareproduct.errors.CreatePackageForNonFinalVendorSoftwareProductErrorBuilder;
86 import org.openecomp.sdc.vendorsoftwareproduct.errors.DeploymentFlavorErrorBuilder;
87 import org.openecomp.sdc.vendorsoftwareproduct.errors.FileCreationErrorBuilder;
88 import org.openecomp.sdc.vendorsoftwareproduct.errors.InformationArtifactCreationErrorBuilder;
89 import org.openecomp.sdc.vendorsoftwareproduct.errors.NicInternalNetworkErrorBuilder;
90 import org.openecomp.sdc.vendorsoftwareproduct.errors.OnboardingMethodErrorBuilder;
91 import org.openecomp.sdc.vendorsoftwareproduct.errors.PackageInvalidErrorBuilder;
92 import org.openecomp.sdc.vendorsoftwareproduct.errors.PackageNotFoundErrorBuilder;
93 import org.openecomp.sdc.vendorsoftwareproduct.errors.TranslationFileCreationErrorBuilder;
94 import org.openecomp.sdc.vendorsoftwareproduct.errors.VendorSoftwareProductInvalidErrorBuilder;
95 import org.openecomp.sdc.vendorsoftwareproduct.factory.CompositionEntityDataManagerFactory;
96 import org.openecomp.sdc.vendorsoftwareproduct.informationArtifact.InformationArtifactGenerator;
97 import org.openecomp.sdc.vendorsoftwareproduct.services.composition.CompositionEntityDataManager;
98 import org.openecomp.sdc.vendorsoftwareproduct.services.schemagenerator.SchemaGenerator;
99 import org.openecomp.sdc.vendorsoftwareproduct.types.QuestionnaireResponse;
100 import org.openecomp.sdc.vendorsoftwareproduct.types.QuestionnaireValidationResult;
101 import org.openecomp.sdc.vendorsoftwareproduct.types.ValidationResponse;
102 import org.openecomp.sdc.vendorsoftwareproduct.types.VersionedVendorSoftwareProductInfo;
103 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.ComponentComputeAssociation;
104 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityId;
105 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityType;
106 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityValidationData;
107 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.DeploymentFlavor;
108 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.NetworkType;
109 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.Nic;
110 import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.ComponentQuestionnaireSchemaInput;
111 import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.SchemaTemplateContext;
112 import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.SchemaTemplateInput;
113 import org.openecomp.sdc.vendorsoftwareproduct.utils.ComponentDependencyTracker;
114 import org.openecomp.sdc.versioning.VersioningManager;
115 import org.openecomp.sdc.versioning.VersioningUtil;
116 import org.openecomp.sdc.versioning.dao.types.Version;
117 import org.openecomp.sdc.versioning.dao.types.VersionStatus;
118 import org.openecomp.sdc.versioning.errors.RequestedVersionInvalidErrorBuilder;
119 import org.openecomp.sdc.versioning.types.VersionInfo;
120 import org.openecomp.sdc.versioning.types.VersionableEntityAction;
121 import org.openecomp.sdcrests.activitylog.types.ActivityType;
122 import org.slf4j.MDC;
123
124 import java.io.BufferedOutputStream;
125 import java.io.ByteArrayInputStream;
126 import java.io.ByteArrayOutputStream;
127 import java.io.File;
128 import java.io.FileOutputStream;
129 import java.io.IOException;
130 import java.io.OutputStream;
131 import java.nio.ByteBuffer;
132 import java.util.ArrayList;
133 import java.util.Collection;
134 import java.util.HashMap;
135 import java.util.HashSet;
136 import java.util.List;
137 import java.util.Map;
138 import java.util.Objects;
139 import java.util.Optional;
140 import java.util.Set;
141 import java.util.zip.ZipInputStream;
142 import java.util.zip.ZipOutputStream;
143
144 public class VendorSoftwareProductManagerImpl implements VendorSoftwareProductManager {
145   private static String VALIDATION_VSP_ID = "validationOnlyVspId";
146   private static final String VALIDATION_VSP_NAME = "validationOnlyVspName";
147   //private static final String VALIDATION_VSP_USER = "validationOnlyVspUser";
148
149   private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
150   private static final Logger logger =
151       LoggerFactory.getLogger(VendorSoftwareProductManagerImpl.class);
152
153   private OrchestrationTemplateDao orchestrationTemplateDao;
154   private VendorSoftwareProductInfoDao vspInfoDao;
155   private VersioningManager versioningManager;
156   private VendorSoftwareProductDao vendorSoftwareProductDao;
157   private VendorLicenseFacade vendorLicenseFacade;
158   private ServiceModelDao<ToscaServiceModel, ServiceElement> serviceModelDao;
159   private EnrichedServiceModelDao<ToscaServiceModel, ServiceElement> enrichedServiceModelDao;
160   private HealingManager healingManager;
161   private VendorLicenseArtifactsService licenseArtifactsService;
162   private InformationArtifactGenerator informationArtifactGenerator;
163   private PackageInfoDao packageInfoDao;
164   private ActivityLogManager activityLogManager;
165   private DeploymentFlavorDao deploymentFlavorDao;
166   private NicDao nicDao;
167   private ManualVspToscaManager manualVspToscaManager;
168
169   /**
170    * Instantiates a new Vendor software product manager.
171    *
172    * @param versioningManager            the versioning manager
173    * @param vendorSoftwareProductDao     the vendor software product dao
174    * @param orchestrationTemplateDataDao the orchestration template data dao
175    * @param vspInfoDao                   the vsp info dao
176    * @param vendorLicenseFacade          the vendor license facade
177    * @param serviceModelDao              the service model dao
178    * @param enrichedServiceModelDao      the enriched service model dao
179    * @param healingManager               the healing manager
180    * @param licenseArtifactsService      the license artifacts service
181    * @param informationArtifactGenerator the information artifact generator
182    * @param packageInfoDao               the package info dao
183    * @param activityLogManager           the activity log manager
184    * @param deploymentFlavorDao          the deployment flavor dao
185    * @param nicDao                       the nic dao
186    */
187   public VendorSoftwareProductManagerImpl(
188       VersioningManager versioningManager,
189       VendorSoftwareProductDao vendorSoftwareProductDao,
190       OrchestrationTemplateDao orchestrationTemplateDataDao,
191       VendorSoftwareProductInfoDao vspInfoDao,
192       VendorLicenseFacade vendorLicenseFacade,
193       ServiceModelDao<ToscaServiceModel, ServiceElement> serviceModelDao,
194       EnrichedServiceModelDao<ToscaServiceModel, ServiceElement> enrichedServiceModelDao,
195       HealingManager healingManager,
196       VendorLicenseArtifactsService licenseArtifactsService,
197       InformationArtifactGenerator informationArtifactGenerator,
198       PackageInfoDao packageInfoDao,
199       ActivityLogManager activityLogManager,
200       DeploymentFlavorDao deploymentFlavorDao,
201       NicDao nicDao,
202       ManualVspToscaManager manualVspToscaManager) {
203     this.versioningManager = versioningManager;
204     this.vendorSoftwareProductDao = vendorSoftwareProductDao;
205     this.orchestrationTemplateDao = orchestrationTemplateDataDao;
206     this.vspInfoDao = vspInfoDao;
207     this.vendorLicenseFacade = vendorLicenseFacade;
208     this.serviceModelDao = serviceModelDao;
209     this.enrichedServiceModelDao = enrichedServiceModelDao;
210     this.healingManager = healingManager;
211     this.licenseArtifactsService = licenseArtifactsService;
212     this.informationArtifactGenerator = informationArtifactGenerator;
213     this.packageInfoDao = packageInfoDao;
214     this.activityLogManager = activityLogManager;
215     this.deploymentFlavorDao = deploymentFlavorDao;
216     this.nicDao = nicDao;
217     this.manualVspToscaManager = manualVspToscaManager;
218
219     registerToVersioning();
220   }
221
222   private void registerToVersioning() {
223     vendorSoftwareProductDao.registerVersioning(
224         VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE);
225     serviceModelDao.registerVersioning(
226         VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE);
227     enrichedServiceModelDao.registerVersioning(
228         VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE);
229   }
230
231   @Override
232   public Version checkout(String vendorSoftwareProductId, String user) {
233     mdcDataDebugMessage.debugEntryMessage("VSP id", vendorSoftwareProductId);
234     MDC.put(LoggerConstants.SERVICE_NAME, LoggerServiceName.Checkout_Entity.toString());
235
236     Version newVersion = versioningManager
237         .checkout(VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE,
238             vendorSoftwareProductId, user);
239
240     if (newVersion != null) {
241       ActivityLogEntity activityLogEntity =
242           new ActivityLogEntity(vendorSoftwareProductId, String.valueOf(newVersion.getMajor() + 1),
243               ActivityType.CHECKOUT.toString(), user, true, "", "");
244       activityLogManager.addActionLog(activityLogEntity, user);
245     }
246
247     mdcDataDebugMessage.debugExitMessage("VSP id", vendorSoftwareProductId);
248     return newVersion;
249   }
250
251
252   @Override
253   public Version undoCheckout(String vendorSoftwareProductId, String user) {
254     mdcDataDebugMessage.debugEntryMessage("VSP id", vendorSoftwareProductId);
255
256     Version version =
257         getVersionInfo(vendorSoftwareProductId, VersionableEntityAction.Read, user)
258             .getActiveVersion();
259     String preVspName = vspInfoDao
260         .get(new VspDetails(vendorSoftwareProductId, version)).getName();
261
262     Version newVersion = versioningManager.undoCheckout(
263         VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE,
264         vendorSoftwareProductId, user);
265
266     String postVspName = vspInfoDao
267         .get(new VspDetails(vendorSoftwareProductId, newVersion))
268         .getName();
269
270     updateUniqueName(preVspName, postVspName);
271
272     mdcDataDebugMessage.debugExitMessage("VSP id", vendorSoftwareProductId);
273
274     return newVersion;
275   }
276
277   @Override
278   public Version checkin(String vendorSoftwareProductId, String user) {
279     mdcDataDebugMessage.debugEntryMessage("VSP id", vendorSoftwareProductId);
280
281     Version newVersion = versioningManager.checkin(
282         VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE,
283         vendorSoftwareProductId, user, null);
284
285     if (newVersion != null) {
286       ActivityLogEntity activityLogEntity =
287           new ActivityLogEntity(vendorSoftwareProductId, String.valueOf(newVersion.getMajor() + 1),
288               ActivityType.CHECKIN.toString(), user, true, "", "");
289       activityLogManager.addActionLog(activityLogEntity, user);
290     }
291
292     mdcDataDebugMessage.debugExitMessage("VSP id", vendorSoftwareProductId);
293
294     return newVersion;
295   }
296
297   @Override
298   public ValidationResponse submit(String vspId, String user) throws IOException {
299     mdcDataDebugMessage.debugEntryMessage("VSP id", vspId);
300
301     Version version = getVersionInfo(vspId, VersionableEntityAction.Read, user).getActiveVersion();
302     VspDetails vspDetails = getVsp(vspId, version, user);
303     UploadDataEntity uploadData = orchestrationTemplateDao.getOrchestrationTemplate(vspId, version);
304     ToscaServiceModel serviceModel =
305         serviceModelDao.getServiceModel(vspId, vspDetails.getVersion());
306
307     ValidationResponse validationResponse = new ValidationResponse();
308     validationResponse
309         .setVspErrors(validateCompletedVendorSoftwareProduct(vspDetails, uploadData, serviceModel),
310             LoggerServiceName.Submit_VSP, LoggerTragetServiceName.SUBMIT_VSP);
311
312     if (isCyclicDependencyInComponents(vspId, vspDetails.getVersion())) {
313       Collection<ErrorCode> vspErrors = validationResponse.getVspErrors() == null
314           ? new ArrayList<>()
315           : validationResponse.getVspErrors();
316       vspErrors.add(ComponentDependencyModelErrorBuilder
317           .getcyclicDependencyComponentErrorBuilder());
318       validationResponse.setVspErrors(vspErrors, LoggerServiceName.Submit_VSP,
319           LoggerTragetServiceName.SUBMIT_VSP);
320     }
321
322     validationResponse.setLicensingDataErrors(validateLicensingData(vspDetails));
323     validationResponse
324         .setUploadDataErrors(validateUploadData(uploadData, vspDetails),
325             LoggerServiceName.Submit_VSP,
326             LoggerTragetServiceName.SUBMIT_VSP);
327
328     validationResponse.setQuestionnaireValidationResult(
329         validateQuestionnaire(vspDetails.getId(), vspDetails.getVersion(), vspDetails
330             .getOnboardingMethod()));
331
332     if (vspDetails.getOnboardingMethod().equals("Manual")) {
333       Collection<ErrorCode> deploymentFlavourValidationErrList =
334           deploymentFlavorValidation(vspDetails.getId(), vspDetails.getVersion());
335       if (validationResponse.getVspErrors() != null) {
336         if (deploymentFlavourValidationErrList != null) {
337           validationResponse.getVspErrors().addAll(deploymentFlavourValidationErrList);
338         }
339       } else {
340         validationResponse
341             .setVspErrors(deploymentFlavourValidationErrList, LoggerServiceName.Submit_VSP,
342                 LoggerTragetServiceName.SUBMIT_VSP);
343       }
344
345       Set<CompositionEntityValidationData> compositionEntityValidationData =
346           componentValidation(vspDetails.getId(), vspDetails.getVersion());
347       if (validationResponse.getQuestionnaireValidationResult() != null) {
348         if (!CollectionUtils.isEmpty(compositionEntityValidationData)) {
349           validationResponse.getQuestionnaireValidationResult().getValidationData()
350               .addAll(compositionEntityValidationData);
351         }
352       } else {
353         validationResponse.setQuestionnaireValidationResult(
354             CollectionUtils.isEmpty(compositionEntityValidationData) ? null :
355                 new QuestionnaireValidationResult(compositionEntityValidationData));
356       }
357
358       //Generate Tosca service model for Manual Onboarding flow
359       VspModelInfo vspModelInfo = manualVspToscaManager.gatherVspInformation(vspId, version, user);
360       serviceModel = manualVspToscaManager.generateToscaModel(vspModelInfo);
361     }
362     validationResponse.setCompilationErrors(
363         compile(vspId, vspDetails.getVersion(), serviceModel),
364         LoggerServiceName.Submit_VSP, LoggerTragetServiceName.SUBMIT_VSP);
365
366     if (validationResponse.isValid()) {
367       Version newVersion = versioningManager.submit(
368           VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE,
369           vspId, user, null);
370       ActivityLogEntity activityLogEntity = new ActivityLogEntity(vspDetails.getId(), String
371           .valueOf(newVersion.getMajor()),
372           ActivityType.SUBMIT.toString(), user, true, "", "");
373       activityLogManager.addActionLog(activityLogEntity, user);
374     }
375
376     mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
377     return validationResponse;
378   }
379
380   private boolean isCyclicDependencyInComponents(String vendorSoftwareProductId,
381                                                  Version version) {
382     final Collection<ComponentDependencyModelEntity> componentDependencyModelEntities =
383         vendorSoftwareProductDao.listComponentDependencies(vendorSoftwareProductId, version);
384     ComponentDependencyTracker dependencyTracker = new ComponentDependencyTracker();
385
386     for (ComponentDependencyModelEntity entity : componentDependencyModelEntities) {
387       dependencyTracker.addDependency(entity.getSourceComponentId(), entity.getTargetComponentId());
388     }
389     return dependencyTracker.isCyclicDependencyPresent();
390   }
391
392   private Collection<ErrorCode> deploymentFlavorValidation(String vspId,
393                                                            Version version) {
394     mdcDataDebugMessage.debugEntryMessage("VSP id", vspId);
395     Set<CompositionEntityValidationData> validationData = new HashSet<>();
396     Collection<ErrorCode> errorCodeList = new ArrayList<>();
397     Collection<DeploymentFlavorEntity> deploymentFlavors =
398         vendorSoftwareProductDao.listDeploymentFlavors(vspId, version);
399     if (!CollectionUtils.isEmpty(deploymentFlavors)) {
400       deploymentFlavors.forEach(deploymentFlavor -> {
401         DeploymentFlavorEntity deployment = vendorSoftwareProductDao.getDeploymentFlavor(vspId,
402             version, deploymentFlavor.getId());
403         DeploymentFlavor deploymentlocalFlavor = deployment.getDeploymentFlavorCompositionData();
404         if (deploymentlocalFlavor != null) {
405           if (deploymentlocalFlavor.getFeatureGroupId() == null) {
406             ErrorCode deploymentFlavorErrorBuilder = DeploymentFlavorErrorBuilder.
407                 getFeatureGroupMandatoryErrorBuilder(deploymentlocalFlavor.getModel());
408             errorCodeList.add(deploymentFlavorErrorBuilder);
409           }
410           List<ComponentComputeAssociation> componetComputeAssociations = new ArrayList<>();
411           componetComputeAssociations = deploymentlocalFlavor.getComponentComputeAssociations();
412           if (CollectionUtils.isEmpty(componetComputeAssociations)) {
413             CompositionEntityValidationData compositionEntityValidationData = new
414                 CompositionEntityValidationData(CompositionEntityType.deployment, deploymentFlavor
415                 .getId());
416             compositionEntityValidationData.setEntityName(deployment
417                 .getDeploymentFlavorCompositionData().getModel());
418             ErrorCode deploymentFlavorErrorBuilder = DeploymentFlavorErrorBuilder
419                 .getInvalidComponentComputeAssociationErrorBuilder(
420                     deploymentlocalFlavor.getModel());
421
422             errorCodeList.add(deploymentFlavorErrorBuilder);
423           } else {
424             componetComputeAssociations.forEach(componetComputeAssociation -> {
425               if (componetComputeAssociation == null
426                   || !(componetComputeAssociation.getComponentId() != null
427                   && componetComputeAssociation.getComputeFlavorId() != null)) {
428                 CompositionEntityValidationData compositionEntityValidationData = new
429                     CompositionEntityValidationData(CompositionEntityType.deployment,
430                     deploymentFlavor.getId());
431                 compositionEntityValidationData.setEntityName(deployment
432                     .getDeploymentFlavorCompositionData().getModel());
433                 ErrorCode deploymentFlavorErrorBuilder = DeploymentFlavorErrorBuilder
434                     .getInvalidComponentComputeAssociationErrorBuilder(
435                         deploymentlocalFlavor.getModel());
436
437                 errorCodeList.add(deploymentFlavorErrorBuilder);
438               }
439             });
440           }
441         }
442       });
443     }
444     return errorCodeList;
445   }
446
447   private Set<CompositionEntityValidationData> componentValidation(String vspId, Version version) {
448     mdcDataDebugMessage.debugEntryMessage("VSP id", vspId);
449
450     Set<CompositionEntityValidationData> validationData = new HashSet<>();
451     Collection<ComponentEntity> components =
452         vendorSoftwareProductDao.listComponents(vspId, version);
453     if (!CollectionUtils.isEmpty(components)) {
454       components.forEach(component -> {
455         validateImage(vspId, version, validationData, component);
456         validateNic(vspId, version, validationData, component);
457
458       });
459     }
460
461     return validationData;
462   }
463
464   private void validateNic(String vspId, Version version,
465                            Set<CompositionEntityValidationData> validationData,
466                            ComponentEntity component) {
467     Collection<NicEntity> nics =
468         nicDao.list(new NicEntity(vspId, version, component.getId(), null));
469     if (CollectionUtils.isNotEmpty(nics)) {
470       nics.forEach(nicEntity -> {
471         NicEntity nic = nicDao.get(new NicEntity(vspId, version, component.getId(),
472             nicEntity.getId()));
473         NetworkType networkType = nic.getNicCompositionData().getNetworkType();
474         String networkId = nic.getNicCompositionData().getNetworkId();
475         if (networkType.equals(NetworkType.Internal) && networkId == null) {
476           CompositionEntityValidationData compositionEntityValidationData = new
477               CompositionEntityValidationData(CompositionEntityType.nic, nic.getId());
478           compositionEntityValidationData.setEntityName(nic.getNicCompositionData().getName());
479           ErrorCode nicInternalNetworkErrorBuilder = NicInternalNetworkErrorBuilder
480               .getNicNullNetworkIdInternalNetworkIdErrorBuilder();
481           List<String> errors = new ArrayList<>();
482           errors.add(nicInternalNetworkErrorBuilder.message());
483           compositionEntityValidationData.setErrors(errors);
484           validationData.add(compositionEntityValidationData);
485         }
486       });
487     }
488   }
489
490   private void validateImage(String vspId, Version version,
491                              Set<CompositionEntityValidationData> validationData,
492                              ComponentEntity component) {
493     Collection<ImageEntity> images = vendorSoftwareProductDao.listImages(vspId, version,
494         component.getId());
495     if (CollectionUtils.isEmpty(images)) {
496       CompositionEntityValidationData compositionEntityValidationData = new
497           CompositionEntityValidationData(component.getType(), component.getId());
498       compositionEntityValidationData.setEntityName(component.getComponentCompositionData()
499           .getDisplayName());
500       ErrorCode vfcMissingImageErrorBuilder =
501           ComponentErrorBuilder.VfcMissingImageErrorBuilder();
502       List<String> errors = new ArrayList<>();
503       errors.add(vfcMissingImageErrorBuilder.message());
504       compositionEntityValidationData.setErrors(errors);
505       validationData.add(compositionEntityValidationData);
506     }
507   }
508
509
510   private List<ErrorCode> validateCompletedVendorSoftwareProduct(
511       VspDetails vspDetails, UploadDataEntity uploadData, Object serviceModel) {
512
513     List<ErrorCode> errors = new ArrayList<>();
514
515     if (vspDetails.getName() == null) {
516       errors.add(createMissingMandatoryFieldError("name"));
517     }
518     if (vspDetails.getDescription() == null) {
519       errors.add(createMissingMandatoryFieldError("description"));
520     }
521     if (vspDetails.getVendorId() == null) {
522       errors.add(createMissingMandatoryFieldError("vendor Id"));
523     }
524     if (vspDetails.getCategory() == null) {
525       errors.add(createMissingMandatoryFieldError("category"));
526     }
527     if (vspDetails.getSubCategory() == null) {
528       errors.add(createMissingMandatoryFieldError("sub category"));
529     }
530     if (vspDetails.getOnboardingMethod().equals("Manual")) {
531       //Manual Onboarding specific validations
532       Collection<DeploymentFlavorEntity> deploymentFlavorEntities = vendorSoftwareProductDao
533           .listDeploymentFlavors(vspDetails.getId(), vspDetails.getVersion());
534       if (CollectionUtils.isEmpty(deploymentFlavorEntities)) {
535         ErrorCode vspMissingDeploymentFlavorErrorBuilder =
536             VendorSoftwareProductInvalidErrorBuilder.VspMissingDeploymentFlavorErrorBuilder();
537         errors.add(vspMissingDeploymentFlavorErrorBuilder);
538       }
539       errors.addAll(validateMandatoryLicenseFields(vspDetails));
540     } else {
541       //Heat flow specific VSP validations
542       if (uploadData == null || uploadData.getContentData() == null || serviceModel == null) {
543         errors.add(VendorSoftwareProductInvalidErrorBuilder
544             .VendorSoftwareProductMissingServiceModelErrorBuilder(vspDetails.getId(),
545                 vspDetails.getVersion()));
546       }
547       if (vspDetails.getVlmVersion() != null || vspDetails.getLicenseAgreement() != null
548           || vspDetails.getFeatureGroups() != null) {
549         errors.addAll(validateMandatoryLicenseFields(vspDetails));
550       }
551     }
552     return errors.isEmpty() ? null : errors;
553   }
554
555   private List<ErrorCode> validateMandatoryLicenseFields(VspDetails vspDetails) {
556     List<ErrorCode> errors = new ArrayList<>();
557     if (vspDetails.getVlmVersion() == null) {
558       errors.add(createMissingMandatoryFieldError(
559           "licensing version (in the format of: {integer}.{integer})"));
560     }
561     if (vspDetails.getLicenseAgreement() == null) {
562       errors.add(createMissingMandatoryFieldError("license agreement"));
563     }
564     if (CollectionUtils.isEmpty(vspDetails.getFeatureGroups())) {
565       errors.add(createMissingMandatoryFieldError("feature groups"));
566     }
567     return errors;
568   }
569
570   private static ErrorCode createMissingMandatoryFieldError(String fieldName) {
571     return new ValidationErrorBuilder("must be supplied", fieldName).build();
572   }
573
574   String getVspQuestionnaireSchema(SchemaTemplateInput schemaInput) {
575     mdcDataDebugMessage.debugEntryMessage(null);
576     mdcDataDebugMessage.debugExitMessage(null);
577     return SchemaGenerator
578         .generate(SchemaTemplateContext.questionnaire, CompositionEntityType.vsp, schemaInput);
579   }
580
581   private static void sortVspListByModificationTimeDescOrder(
582       List<VersionedVendorSoftwareProductInfo> vsps) {
583     vsps.sort((o1, o2) -> o2.getVspDetails().getWritetimeMicroSeconds()
584         .compareTo(o1.getVspDetails().getWritetimeMicroSeconds()));
585   }
586
587
588   private Map<String, List<ErrorMessage>> compile(String vendorSoftwareProductId, Version version,
589                                                   ToscaServiceModel serviceModel) {
590     if (serviceModel == null) {
591       return null;
592     }
593
594     enrichedServiceModelDao.deleteAll(vendorSoftwareProductId, version);
595
596     EnrichmentManager<ToscaServiceModel> enrichmentManager =
597         EnrichmentManagerFactory.getInstance().createInterface();
598     enrichmentManager.init(vendorSoftwareProductId, version);
599     enrichmentManager.setModel(serviceModel);
600     Map<String, List<ErrorMessage>> enrichErrors = enrichmentManager.enrich();
601
602     if (MapUtils.isEmpty(MessageContainerUtil.getMessageByLevel(ErrorLevel.ERROR, enrichErrors))) {
603       logger.audit(AuditMessages.AUDIT_MSG + AuditMessages.ENRICHMENT_COMPLETED
604           + vendorSoftwareProductId);
605     } else {
606       enrichErrors.values().forEach(errorList ->
607           auditIfContainsErrors(errorList, vendorSoftwareProductId,
608               AuditMessages.ENRICHMENT_ERROR));
609     }
610
611     enrichedServiceModelDao
612         .storeServiceModel(vendorSoftwareProductId, version, enrichmentManager.getModel());
613
614     return enrichErrors;
615   }
616
617   private Collection<ErrorCode> validateLicensingData(VspDetails vspDetails) {
618     mdcDataDebugMessage.debugEntryMessage("VSP id", vspDetails.getId());
619
620     if (vspDetails.getVendorId() == null || vspDetails.getVlmVersion() == null
621         || vspDetails.getLicenseAgreement() == null
622         || CollectionUtils.isEmpty(vspDetails.getFeatureGroups())) {
623       return null;
624     }
625
626     mdcDataDebugMessage.debugExitMessage("VSP id", vspDetails.getId());
627     return vendorLicenseFacade
628         .validateLicensingData(vspDetails.getVendorId(), vspDetails.getVlmVersion(),
629             vspDetails.getLicenseAgreement(), vspDetails.getFeatureGroups());
630   }
631
632   @Override
633   public String fetchValidationVsp(String user) {
634     try {
635       validateUniqueName(VALIDATION_VSP_NAME);
636     } catch (Exception ignored) {
637       return VALIDATION_VSP_ID;
638     }
639     VspDetails validationVsp = new VspDetails();
640     validationVsp.setName(VALIDATION_VSP_NAME);
641
642     vspInfoDao.create(validationVsp);
643     Version version = versioningManager.create(
644         VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE,
645         validationVsp.getId(), user);
646     validationVsp.setVersion(version);
647
648     createUniqueName(VALIDATION_VSP_NAME);
649     VALIDATION_VSP_ID = validationVsp.getId();
650     return VALIDATION_VSP_ID;
651   }
652
653   @Override
654   public VspDetails createVsp(VspDetails vspDetails, String user) {
655     mdcDataDebugMessage.debugEntryMessage(null);
656
657     validateUniqueName(vspDetails.getName());
658
659     vspDetails.setOnboardingOrigin(OnboardingTypesEnum.NONE.toString());
660
661     vspInfoDao.create(vspDetails);//id will be set in the dao
662     vspInfoDao.updateQuestionnaireData(vspDetails.getId(), null,
663         new JsonSchemaDataGenerator(getVspQuestionnaireSchema(null)).generateData());
664
665     Version version = versioningManager
666         .create(VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE,
667             vspDetails.getId(), user);
668     vspDetails.setVersion(version);
669     ActivityLogEntity activityLogEntity = new ActivityLogEntity(vspDetails.getId(), String
670         .valueOf(vspDetails.getVersion().getMajor() + 1),
671         ActivityType.CREATE_NEW.toString(), user, true, "", "");
672     activityLogManager.addActionLog(activityLogEntity, user);
673     String vspName = vspDetails.getName();
674     createUniqueName(vspName);
675     mdcDataDebugMessage.debugExitMessage(null);
676     return vspDetails;
677   }
678
679   @Override
680   public List<VersionedVendorSoftwareProductInfo> listVsps(String versionFilter, String user) {
681     mdcDataDebugMessage.debugEntryMessage(null);
682
683     Map<String, VersionInfo> idToVersionsInfo = versioningManager.listEntitiesVersionInfo(
684         VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE, user,
685         VersionableEntityAction.Read);
686
687     List<VersionedVendorSoftwareProductInfo> vsps = new ArrayList<>();
688     for (Map.Entry<String, VersionInfo> entry : idToVersionsInfo.entrySet()) {
689       VersionInfo versionInfo = entry.getValue();
690       if (versionFilter != null && versionFilter.equals(VersionStatus.Final.name())) {
691         if (versionInfo.getLatestFinalVersion() == null) {
692           continue;
693         }
694         versionInfo.setActiveVersion(versionInfo.getLatestFinalVersion());
695         versionInfo.setStatus(VersionStatus.Final);
696         versionInfo.setLockingUser(null);
697       }
698
699       Version version = versionInfo.getActiveVersion();
700       if (user.equals(versionInfo.getLockingUser())) {
701         version.setStatus(VersionStatus.Locked);
702       }
703       try {
704         VspDetails vsp = vspInfoDao.get(new VspDetails(entry.getKey(), version));
705         if (vsp != null && !vsp.getId().equals(VALIDATION_VSP_ID)) {
706           vsp.setValidationDataStructure(null);
707           vsps.add(new VersionedVendorSoftwareProductInfo(vsp, versionInfo));
708         }
709       } catch (RuntimeException rte) {
710         logger.error(
711             "Error trying to retrieve vsp[" + entry.getKey() + "] version[" + version.toString
712                 () + "] " +
713                 "message:" + rte
714                 .getMessage());
715       }
716     }
717
718     sortVspListByModificationTimeDescOrder(vsps);
719
720     mdcDataDebugMessage.debugExitMessage(null);
721
722     return vsps;
723   }
724
725   @Override
726   public void updateVsp(VspDetails vspDetails, String user) {
727     mdcDataDebugMessage.debugEntryMessage("VSP id", vspDetails.getId());
728
729     VspDetails retrieved = vspInfoDao.get(vspDetails);
730     if (!retrieved.getOnboardingMethod().equals(vspDetails.getOnboardingMethod())) {
731       final ErrorCode onboardingMethodUpdateErrorCode = OnboardingMethodErrorBuilder
732           .getOnboardingUpdateError();
733
734       MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
735           LoggerTragetServiceName.UPDATE_VSP, ErrorLevel.ERROR.name(),
736           LoggerErrorCode.DATA_ERROR.getErrorCode(), onboardingMethodUpdateErrorCode.message());
737
738       throw new CoreException(onboardingMethodUpdateErrorCode);
739     }
740
741     //If any existing feature group is removed from VSP which is also associated in DF then
742     //update DF to remove feature group associations.
743     updateDeploymentFlavor(vspDetails, user);
744
745     updateUniqueName(retrieved.getName(), vspDetails.getName());
746     vspDetails.setOldVersion(retrieved.getOldVersion());
747
748     vspInfoDao.update(vspDetails);
749     //vendorSoftwareProductDao.updateVspLatestModificationTime(vspDetails.getId(), activeVersion);
750
751     mdcDataDebugMessage.debugExitMessage("VSP id", vspDetails.getId());
752   }
753
754   private void updateDeploymentFlavor(VspDetails vspDetails, String user) {
755     final List<String> featureGroups = vspDetails.getFeatureGroups();
756     if (featureGroups != null) {
757       final Collection<DeploymentFlavorEntity> deploymentFlavorEntities = deploymentFlavorDao
758           .list(new DeploymentFlavorEntity(vspDetails.getId(), vspDetails
759               .getVersion(), null));
760       if (Objects.nonNull(deploymentFlavorEntities)) {
761         deploymentFlavorEntities.forEach(deploymentFlavorEntity -> {
762           final String featureGroupId =
763               deploymentFlavorEntity.getDeploymentFlavorCompositionData().getFeatureGroupId();
764           if (!featureGroups.contains(featureGroupId)) {
765             DeploymentFlavor deploymentFlavorCompositionData =
766                 deploymentFlavorEntity.getDeploymentFlavorCompositionData();
767             deploymentFlavorCompositionData.setFeatureGroupId(null);
768             deploymentFlavorEntity.setDeploymentFlavorCompositionData
769                 (deploymentFlavorCompositionData);
770             vendorSoftwareProductDao.updateDeploymentFlavor(deploymentFlavorEntity);
771           }
772         });
773       }
774     }
775   }
776
777
778   @Override
779   public VspDetails getVsp(String vspId, Version version, String user) {
780     mdcDataDebugMessage.debugEntryMessage("VSP id", vspId);
781
782     VspDetails vsp = vspInfoDao.get(new VspDetails(vspId, version));
783     if (vsp == null) {
784       MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
785           LoggerTragetServiceName.GET_VSP, ErrorLevel.ERROR.name(),
786           LoggerErrorCode.DATA_ERROR.getErrorCode(), "Requested VSP not found");
787       throw new CoreException(new VendorSoftwareProductNotFoundErrorBuilder(vspId).build());
788     }
789     vsp.setValidationData(orchestrationTemplateDao.getValidationData(vspId, version));
790
791     if (Objects.isNull(vsp.getOnboardingOrigin())) {
792       vsp.setOnboardingOrigin(OnboardingTypesEnum.ZIP.toString());
793     }
794
795     if (Objects.isNull(vsp.getNetworkPackageName())) {
796       vsp.setNetworkPackageName("Upload File");
797     }
798
799     mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
800     return vsp;
801   }
802
803   @Override
804   public Version callAutoHeal(String vspId, VersionInfo versionInfo,
805                               VspDetails vendorSoftwareProductInfo, String user)
806       throws Exception {
807     switch (versionInfo.getStatus()) {
808       case Locked:
809         if (user.equals(versionInfo.getLockingUser())) {
810           autoHeal(vspId, versionInfo.getActiveVersion(), vendorSoftwareProductInfo,
811               versionInfo.getLockingUser());
812         }
813         return versionInfo.getActiveVersion();
814       case Available:
815         Version checkoutVersion = checkout(vspId, user);
816         autoHeal(vspId, checkoutVersion, vendorSoftwareProductInfo, user);
817         return checkin(vspId, user);
818       case Final:
819         Version checkoutFinalVersion = checkout(vspId, user);
820         autoHeal(vspId, checkoutFinalVersion, vendorSoftwareProductInfo, user);
821         Version checkinFinalVersion = checkin(vspId, user);
822         ValidationResponse response = submit(vspId, user);
823         if (!response.isValid()) {
824           return checkout(vspId, user);
825         }
826
827         try {
828           Version finalVersion = checkinFinalVersion.calculateNextFinal();
829           createPackage(vspId, finalVersion, user);
830           return finalVersion;
831         } catch (IOException ex) {
832           throw new Exception(ex.getMessage());
833         }
834       default:
835         //do nothing
836         break;
837     }
838     return versionInfo.getActiveVersion();
839   }
840
841   @Override
842
843   public void deleteVsp(String vspId, String user) {
844     mdcDataDebugMessage.debugEntryMessage("VSP id", vspId);
845
846     MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
847         LoggerTragetServiceName.DELETE_VSP, ErrorLevel.ERROR.name(),
848         LoggerErrorCode.PERMISSION_ERROR.getErrorCode(), "Unsupported operation");
849     mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
850
851     throw new UnsupportedOperationException(
852         VendorSoftwareProductConstants.UNSUPPORTED_OPERATION_ERROR);
853   }
854
855   @Override
856   public void heal(String vspId, Version version, String user) {
857     mdcDataDebugMessage.debugEntryMessage("VSP id", vspId);
858
859     VersionInfo versionInfo = getVersionInfo(vspId, VersionableEntityAction.Read, user);
860
861     version = VersionStatus.Locked.equals(versionInfo.getStatus())
862         ? versionInfo.getActiveVersion()
863         : checkout(vspId, user);
864     version.setStatus(VersionStatus.Locked);
865
866     Optional<String> errorMessages =
867         healingManager.healAll(getHealingParamsAsMap(vspId, version, user));
868
869     VspDetails vspDetails = new VspDetails(vspId, version);
870     vspDetails.setOldVersion(null);
871     vspInfoDao.updateOldVersionIndication(vspDetails);
872
873     logger.audit("Healed VSP " + vspDetails.getId());
874     mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
875
876     if (errorMessages.isPresent()) {
877       throw new CoreException(new ErrorCode.ErrorCodeBuilder().withId("HEALING_ERROR")
878           .withCategory(ErrorCategory.APPLICATION).withMessage(errorMessages.get()).build());
879     }
880   }
881
882   private void autoHeal(String vspId, Version checkoutVersion, VspDetails vspDetails, String user) {
883     mdcDataDebugMessage.debugEntryMessage("VSP id", vspId);
884
885     checkoutVersion.setStatus(VersionStatus.Locked);
886     Map<String, Object> healingParams = getHealingParamsAsMap(vspId, checkoutVersion, user);
887
888     Optional<String> errorMessages = healingManager.healAll(healingParams);
889
890     vspDetails.setVersion(checkoutVersion);
891     vspDetails.setOldVersion(null);
892     vspInfoDao.updateOldVersionIndication(vspDetails);
893
894     logger.audit("Healed VSP " + vspDetails.getName());
895     mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
896
897     if (errorMessages.isPresent()) {
898       throw new CoreException(new ErrorCode.ErrorCodeBuilder().withId("HEALING_ERROR")
899           .withCategory(ErrorCategory.APPLICATION).withMessage(errorMessages.get()).build());
900     }
901   }
902
903   private Map<String, Object> getHealingParamsAsMap(String vspId, Version version, String user) {
904     Map<String, Object> healingParams = new HashMap<>();
905
906     healingParams.put(SdcCommon.VSP_ID, vspId);
907     healingParams.put(SdcCommon.VERSION, version);
908     healingParams.put(SdcCommon.USER, user);
909
910     return healingParams;
911   }
912
913   @Override
914   public List<PackageInfo> listPackages(String category, String subCategory) {
915     return packageInfoDao.listByCategory(category, subCategory);
916   }
917
918   @Override
919   public File getTranslatedFile(String vspId, Version version, String user) {
920     mdcDataDebugMessage.debugEntryMessage("VSP id", vspId);
921     String errorMessage;
922     if (version == null) {
923       errorMessage = "Package not found";
924       MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
925           LoggerTragetServiceName.GET_TRANSLATED_FILE, ErrorLevel.ERROR.name(),
926           LoggerErrorCode.DATA_ERROR.getErrorCode(), errorMessage);
927       throw new CoreException(new PackageNotFoundErrorBuilder(vspId).build());
928     } else if (!version.isFinal()) {
929       errorMessage = "Invalid requested version";
930       MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
931           LoggerTragetServiceName.GET_VERSION_INFO, ErrorLevel.ERROR.name(),
932           LoggerErrorCode.DATA_ERROR.getErrorCode(), errorMessage);
933       throw new CoreException(new RequestedVersionInvalidErrorBuilder().build());
934     }
935
936     PackageInfo packageInfo =
937         packageInfoDao.get(new PackageInfo(vspId, version));
938     if (packageInfo == null) {
939       errorMessage = "Package not found";
940       MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
941           LoggerTragetServiceName.GET_TRANSLATED_FILE, ErrorLevel.ERROR.name(),
942           LoggerErrorCode.DATA_ERROR.getErrorCode(), errorMessage);
943       throw new CoreException(new PackageNotFoundErrorBuilder(vspId, version).build());
944     }
945
946     ByteBuffer translatedFileBuffer = packageInfo.getTranslatedFile();
947     if (translatedFileBuffer == null) {
948       errorMessage = "Package not found";
949       MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
950           LoggerTragetServiceName.GET_TRANSLATED_FILE, ErrorLevel.ERROR.name(),
951           LoggerErrorCode.DATA_ERROR.getErrorCode(), errorMessage);
952       throw new CoreException(new PackageInvalidErrorBuilder(vspId, version).build());
953     }
954
955     File translatedFile = new File(VendorSoftwareProductConstants.VSP_PACKAGE_ZIP);
956
957     try {
958       FileOutputStream fos = new FileOutputStream(translatedFile);
959       fos.write(translatedFileBuffer.array());
960       fos.close();
961     } catch (IOException exception) {
962       errorMessage = "Can't create package";
963       MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
964           LoggerTragetServiceName.CREATE_TRANSLATED_FILE, ErrorLevel.ERROR.name(),
965           LoggerErrorCode.DATA_ERROR.getErrorCode(), errorMessage);
966       throw new CoreException(new TranslationFileCreationErrorBuilder(vspId, version).build(),
967           exception);
968     }
969
970     mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
971
972     return translatedFile;
973   }
974
975   @Override
976
977   public byte[] getOrchestrationTemplateFile(String vspId, Version version, String user) {
978     mdcDataDebugMessage.debugEntryMessage("VSP id", vspId);
979
980     UploadDataEntity uploadData = orchestrationTemplateDao.getOrchestrationTemplate(vspId, version);
981     ByteBuffer contentData = uploadData.getContentData();
982     if (contentData == null) {
983       return null;
984     }
985
986     ByteArrayOutputStream baos = new ByteArrayOutputStream();
987
988     try (final ZipOutputStream zos = new ZipOutputStream(baos);
989          ZipInputStream zipStream = new ZipInputStream(
990              new ByteArrayInputStream(contentData.array()))) {
991       zos.write(contentData.array());
992     } catch (IOException exception) {
993       MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
994           LoggerTragetServiceName.GET_UPLOADED_HEAT, ErrorLevel.ERROR.name(),
995           LoggerErrorCode.DATA_ERROR.getErrorCode(), "Can't get uploaded HEAT");
996       throw new CoreException(new FileCreationErrorBuilder(vspId).build(), exception);
997     }
998
999     mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
1000     return baos.toByteArray();
1001   }
1002
1003   @Override
1004   public PackageInfo createPackage(String vspId, Version version, String user) throws IOException {
1005     mdcDataDebugMessage.debugEntryMessage("VSP id", vspId);
1006
1007     if (!version.isFinal()) {
1008       MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
1009           LoggerTragetServiceName.CREATE_PACKAGE, ErrorLevel.ERROR.name(),
1010           LoggerErrorCode.PERMISSION_ERROR.getErrorCode(), "Can't create package");
1011       throw new CoreException(
1012           new CreatePackageForNonFinalVendorSoftwareProductErrorBuilder(vspId, version)
1013               .build());
1014     }
1015
1016     ToscaServiceModel toscaServiceModel = enrichedServiceModelDao.getServiceModel(vspId, version);
1017     VspDetails vspDetails = vspInfoDao.get(new VspDetails(vspId, version));
1018     Version vlmVersion = vspDetails.getVlmVersion();
1019
1020     PackageInfo packageInfo = createPackageInfo(vspId, vspDetails);
1021
1022     ToscaFileOutputServiceCsarImpl toscaServiceTemplateServiceCsar =
1023         new ToscaFileOutputServiceCsarImpl();
1024     FileContentHandler licenseArtifacts = licenseArtifactsService
1025         .createLicenseArtifacts(vspDetails.getId(), vspDetails.getVendorId(), vlmVersion,
1026             vspDetails.getFeatureGroups(), user);
1027     //todo add tosca validation here
1028     packageInfo.setTranslatedFile(ByteBuffer.wrap(
1029         toscaServiceTemplateServiceCsar.createOutputFile(toscaServiceModel, licenseArtifacts)));
1030
1031     packageInfoDao.create(packageInfo);
1032
1033     logger.audit(AuditMessages.AUDIT_MSG + AuditMessages.CREATE_PACKAGE + vspId);
1034
1035     mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
1036     return packageInfo;
1037   }
1038
1039   private PackageInfo createPackageInfo(String vspId, VspDetails vspDetails) {
1040     PackageInfo packageInfo = new PackageInfo();
1041     packageInfo.setVspId(vspId);
1042     packageInfo.setVersion(vspDetails.getVersion());
1043     packageInfo.setVspName(vspDetails.getName());
1044     packageInfo.setVspDescription(vspDetails.getDescription());
1045     packageInfo.setCategory(vspDetails.getCategory());
1046     packageInfo.setSubCategory(vspDetails.getSubCategory());
1047     packageInfo.setVendorName(vspDetails.getVendorName());
1048     packageInfo.setPackageType(VendorSoftwareProductConstants.CSAR);
1049     packageInfo.setVendorRelease("1.0"); //todo TBD
1050     return packageInfo;
1051   }
1052
1053   @Override
1054
1055   public QuestionnaireResponse getVspQuestionnaire(String vspId, Version version, String user) {
1056     mdcDataDebugMessage.debugEntryMessage("VSP id", vspId);
1057
1058     VspQuestionnaireEntity retrieved = vspInfoDao.getQuestionnaire(vspId, version);
1059     VersioningUtil.validateEntityExistence(retrieved, new VspQuestionnaireEntity(vspId, version),
1060         VspDetails.ENTITY_TYPE);
1061
1062     String questionnaireData = retrieved.getQuestionnaireData();
1063
1064     QuestionnaireResponse questionnaireResponse = new QuestionnaireResponse();
1065     questionnaireResponse.setData(questionnaireData);
1066     questionnaireResponse.setSchema(getVspQuestionnaireSchema(null));
1067
1068     mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
1069
1070     return questionnaireResponse;
1071   }
1072
1073   @Override
1074   public void updateVspQuestionnaire(String vspId, Version version, String questionnaireData,
1075                                      String user) {
1076     mdcDataDebugMessage.debugEntryMessage("VSP id", vspId);
1077
1078     vspInfoDao.updateQuestionnaireData(vspId, version, questionnaireData);
1079
1080     mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
1081   }
1082
1083
1084   private Map<String, List<ErrorMessage>> validateUploadData(UploadDataEntity uploadData,
1085                                                              VspDetails vspDetails)
1086       throws IOException {
1087
1088     Map<String, List<ErrorMessage>> validationErrors = new HashMap<>();
1089     if (uploadData == null || uploadData.getContentData() == null) {
1090       return null;
1091     }
1092
1093     FileContentHandler fileContentMap =
1094         CommonUtil.validateAndUploadFileContent(OnboardingTypesEnum.getOnboardingTypesEnum
1095                 (vspDetails.getOnboardingOrigin()),
1096             uploadData.getContentData().array());
1097
1098     if (vspDetails.getOnboardingOrigin().equals(OnboardingTypesEnum.ZIP.name().toLowerCase())) {
1099       ValidationManager validationManager =
1100           ValidationManagerUtil.initValidationManager(fileContentMap);
1101       validationErrors.putAll(validationManager.validate());
1102     }
1103
1104     return
1105         MapUtils.isEmpty(MessageContainerUtil.getMessageByLevel(ErrorLevel.ERROR, validationErrors))
1106             ? null : validationErrors;
1107   }
1108
1109   private VersionInfo getVersionInfo(String vendorSoftwareProductId, VersionableEntityAction action,
1110                                      String user) {
1111     return versioningManager.getEntityVersionInfo(
1112         VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE,
1113         vendorSoftwareProductId, user, action);
1114   }
1115
1116
1117   private QuestionnaireValidationResult validateQuestionnaire(String vspId, Version version,
1118                                                               String onboardingMethod) {
1119     mdcDataDebugMessage.debugEntryMessage("VSP id", vspId);
1120
1121     // The apis of CompositionEntityDataManager used here are stateful!
1122     // so, it must be re-created from scratch when it is used!
1123     CompositionEntityDataManager compositionEntityDataManager =
1124         CompositionEntityDataManagerFactory.getInstance().createInterface();
1125     compositionEntityDataManager
1126         .addEntity(vspInfoDao.getQuestionnaire(vspId, version), null);
1127
1128     Collection<NicEntity> nics = vendorSoftwareProductDao.listNicsByVsp(vspId, version);
1129
1130     Map<String, List<String>> nicNamesByComponent = new HashMap<>();
1131     for (NicEntity nicEntity : nics) {
1132       compositionEntityDataManager.addEntity(nicEntity, null);
1133
1134       Nic nic = nicEntity.getNicCompositionData();
1135       if (nic != null && nic.getName() != null) {
1136         List<String> nicNames =
1137             nicNamesByComponent.computeIfAbsent(nicEntity.getComponentId(), k -> new ArrayList<>());
1138         nicNames.add(nic.getName());
1139       }
1140     }
1141
1142     Collection<ComponentEntity> components =
1143         vendorSoftwareProductDao.listComponentsCompositionAndQuestionnaire(vspId, version);
1144     components.forEach(component -> compositionEntityDataManager.addEntity(component,
1145         new ComponentQuestionnaireSchemaInput(nicNamesByComponent.get(component.getId()),
1146             JsonUtil.json2Object(component.getQuestionnaireData(), Map.class))));
1147
1148     Collection<ComputeEntity> computes = vendorSoftwareProductDao.listComputesByVsp(vspId, version);
1149     computes.forEach(compute -> compositionEntityDataManager.addEntity(compute, null));
1150
1151     if ("Manual".equals(onboardingMethod)) {
1152       Collection<ImageEntity> images = vendorSoftwareProductDao.listImagesByVsp(vspId, version);
1153       images.forEach(image -> compositionEntityDataManager.addEntity(image, null));
1154     }
1155
1156     Map<CompositionEntityId, Collection<String>> errorsByEntityId =
1157         compositionEntityDataManager.validateEntitiesQuestionnaire();
1158     if (MapUtils.isNotEmpty(errorsByEntityId)) {
1159       compositionEntityDataManager.buildTrees();
1160       compositionEntityDataManager.addErrorsToTrees(errorsByEntityId);
1161 /*      Set<CompositionEntityValidationData> entitiesWithValidationErrors =
1162           compositionEntityDataManager.getEntityListWithErrors();*/
1163       //Collection<CompositionEntityValidationData> roots = compositionEntityDataManager.getTrees();
1164
1165       mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
1166       return new QuestionnaireValidationResult(
1167           compositionEntityDataManager.getAllErrorsByVsp(vspId));
1168     }
1169
1170     mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
1171     return null;
1172   }
1173
1174   @Override
1175   public File getInformationArtifact(String vspId, Version version, String user) {
1176     mdcDataDebugMessage.debugEntryMessage("VSP id", vspId);
1177     VspDetails vspDetails = vspInfoDao.get(new VspDetails(vspId, version));
1178
1179     if (vspDetails == null) {
1180       return null;
1181     }
1182
1183     String vspName = vspDetails.getName();
1184     ByteBuffer infoArtifactAsByteBuffer;
1185     File infoArtifactFile;
1186     try {
1187       infoArtifactAsByteBuffer = ByteBuffer.wrap(informationArtifactGenerator.generate(vspId,
1188           version).getBytes());
1189
1190       infoArtifactFile =
1191           new File(
1192               String.format(VendorSoftwareProductConstants.INFORMATION_ARTIFACT_NAME, vspName));
1193       OutputStream out = new BufferedOutputStream(new FileOutputStream(infoArtifactFile));
1194       out.write(infoArtifactAsByteBuffer.array());
1195       out.close();
1196     } catch (IOException ex) {
1197       throw new CoreException(new InformationArtifactCreationErrorBuilder(vspId).build(), ex);
1198     }
1199
1200     mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
1201     return infoArtifactFile;
1202   }
1203
1204   void validateUniqueName(String vspName) {
1205     UniqueValueUtil.validateUniqueValue(
1206         VendorSoftwareProductConstants.UniqueValues.VENDOR_SOFTWARE_PRODUCT_NAME, vspName);
1207   }
1208
1209   void createUniqueName(String vspName) {
1210     UniqueValueUtil.createUniqueValue(
1211         VendorSoftwareProductConstants.UniqueValues.VENDOR_SOFTWARE_PRODUCT_NAME, vspName);
1212   }
1213
1214   void updateUniqueName(String oldVspName, String newVspName) {
1215     UniqueValueUtil.updateUniqueValue(
1216         VendorSoftwareProductConstants.UniqueValues.VENDOR_SOFTWARE_PRODUCT_NAME,
1217         oldVspName, newVspName);
1218   }
1219
1220   @Override
1221   public Collection<ComputeEntity> getComputeByVsp(String vspId, Version version,
1222                                                    String user) {
1223     return vendorSoftwareProductDao.listComputesByVsp(vspId, version);
1224   }
1225
1226   private void auditIfContainsErrors(List<ErrorMessage> errorList, String vspId, String auditType) {
1227
1228     errorList.forEach(errorMessage -> {
1229       if (errorMessage.getLevel().equals(ErrorLevel.ERROR)) {
1230         logger.audit(AuditMessages.AUDIT_MSG + String.format(auditType, errorMessage.getMessage(),
1231             vspId));
1232       }
1233     });
1234   }
1235 }