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