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