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