2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.vendorsoftwareproduct.impl;
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;
112 import java.io.BufferedOutputStream;
113 import java.io.ByteArrayInputStream;
114 import java.io.ByteArrayOutputStream;
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;
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);
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;
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,
165 ComputeDao computeDao,
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;
184 registerToVersioning();
187 private void registerToVersioning() {
188 serviceModelDao.registerVersioning(
189 VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE);
190 enrichedServiceModelDao.registerVersioning(
191 VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE);
196 public ValidationResponse validate(String vspId, Version version) throws IOException {
197 MDC_DATA_DEBUG_MESSAGE.debugEntryMessage("VSP id", vspId);
199 VspDetails vspDetails = getValidatedVsp(vspId, version);
200 Collection<ComponentDependencyModelEntity> componentDependencies =
201 componentDependencyModelDao.list(new ComponentDependencyModelEntity(vspId, version, null));
203 ValidationResponse validationResponse = new ValidationResponse();
204 validationResponse.setQuestionnaireValidationResult(
205 validateQuestionnaire(vspDetails.getId(), vspDetails.getVersion(),
206 vspDetails.getOnboardingMethod()));
208 List<ErrorCode> vspErrors = new ArrayList<>();
209 vspErrors.addAll(validateVspFields(vspDetails));
210 if (validateComponentDependencies(componentDependencies)) {
212 .add(ComponentDependencyModelErrorBuilder.getcyclicDependencyComponentErrorBuilder());
214 if (Objects.nonNull(vspDetails.getOnboardingMethod()) &&
215 OnboardingMethod.Manual.name().equals(vspDetails.getOnboardingMethod())) {
216 vspErrors.addAll(validateMandatoryLicenseFields(vspDetails));
218 Collection<DeploymentFlavorEntity> deploymentFlavors = deploymentFlavorDao
219 .list(new DeploymentFlavorEntity(vspDetails.getId(), vspDetails.getVersion(), null));
220 if (CollectionUtils.isEmpty(deploymentFlavors)) {
222 .add(VendorSoftwareProductInvalidErrorBuilder.VspMissingDeploymentFlavorErrorBuilder());
224 vspErrors.addAll(validateDeploymentFlavors(deploymentFlavors));
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));
234 validationResponse.getQuestionnaireValidationResult().getValidationData()
235 .addAll(componentValidationResult);
239 if (vspDetails.getVlmVersion() != null || vspDetails.getLicenseAgreement() != null
240 || vspDetails.getFeatureGroups() != null) {
241 vspErrors.addAll(validateMandatoryLicenseFields(vspDetails));
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()));
253 validationResponse.setUploadDataErrors(validateOrchestrationTemplate(orchestrationTemplate),
254 LoggerServiceName.Submit_VSP, LoggerTragetServiceName.SUBMIT_VSP);
257 .setVspErrors(vspErrors, LoggerServiceName.Submit_VSP, LoggerTragetServiceName.SUBMIT_VSP);
258 validationResponse.setLicensingDataErrors(validateLicensingData(vspDetails));
261 MDC_DATA_DEBUG_MESSAGE.debugExitMessage("VSP id", vspId);
262 return validationResponse;
266 public Map<String, List<ErrorMessage>> compile(String vspId, Version version) {
267 MDC_DATA_DEBUG_MESSAGE.debugEntryMessage("VSP id", vspId);
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);
276 Map<String, List<ErrorMessage>> compilationErrors = compile(vspId, version, serviceModel);
277 MDC_DATA_DEBUG_MESSAGE.debugExitMessage("VSP id", vspId);
278 return compilationErrors;
281 private boolean validateComponentDependencies(
282 Collection<ComponentDependencyModelEntity> componentDependencies) {
283 ComponentDependencyTracker dependencyTracker = new ComponentDependencyTracker();
285 for (ComponentDependencyModelEntity componentDependency : componentDependencies) {
286 dependencyTracker.addDependency(componentDependency.getSourceComponentId(),
287 componentDependency.getTargetComponentId());
289 return dependencyTracker.isCyclicDependencyPresent();
292 private Collection<ErrorCode> validateDeploymentFlavors(
293 Collection<DeploymentFlavorEntity> deploymentFlavors) {
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);
306 List<ComponentComputeAssociation> componetComputeAssociations =
307 deploymentlocalFlavor.getComponentComputeAssociations();
308 if (CollectionUtils.isEmpty(componetComputeAssociations)) {
309 CompositionEntityValidationData compositionEntityValidationData = new
310 CompositionEntityValidationData(CompositionEntityType.deployment, deploymentFlavor
312 compositionEntityValidationData.setEntityName(deployment
313 .getDeploymentFlavorCompositionData().getModel());
314 ErrorCode deploymentFlavorErrorBuilder = DeploymentFlavorErrorBuilder
315 .getInvalidComponentComputeAssociationErrorBuilder(
316 deploymentlocalFlavor.getModel());
318 errorCodeList.add(deploymentFlavorErrorBuilder);
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());
333 errorCodeList.add(deploymentFlavorErrorBuilder);
340 return errorCodeList;
343 private Set<CompositionEntityValidationData> componentValidation(String vspId, Version version) {
344 MDC_DATA_DEBUG_MESSAGE.debugEntryMessage("VSP id", vspId);
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);
357 return validationData;
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(),
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);
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);
405 private List<ErrorCode> validateVspFields(VspDetails vspDetails) {
406 List<ErrorCode> errors = new ArrayList<>();
408 if (vspDetails.getName() == null) {
409 errors.add(createMissingMandatoryFieldError("name"));
411 if (vspDetails.getDescription() == null) {
412 errors.add(createMissingMandatoryFieldError("description"));
414 if (vspDetails.getVendorId() == null) {
415 errors.add(createMissingMandatoryFieldError("vendor Id"));
417 if (vspDetails.getCategory() == null) {
418 errors.add(createMissingMandatoryFieldError("category"));
420 if (vspDetails.getSubCategory() == null) {
421 errors.add(createMissingMandatoryFieldError("sub category"));
426 private List<ErrorCode> validateMandatoryLicenseFields(VspDetails vspDetails) {
427 List<ErrorCode> errors = new ArrayList<>();
428 if (vspDetails.getVlmVersion() == null) {
429 errors.add(createMissingMandatoryFieldError("licensing version"));
431 if (vspDetails.getLicenseAgreement() == null) {
432 errors.add(createMissingMandatoryFieldError("license agreement"));
434 if (CollectionUtils.isEmpty(vspDetails.getFeatureGroups())) {
435 errors.add(createMissingMandatoryFieldError("feature groups"));
440 private static ErrorCode createMissingMandatoryFieldError(String fieldName) {
441 return new ValidationErrorBuilder("must be supplied", fieldName).build();
444 private Map<String, List<ErrorMessage>> compile(String vendorSoftwareProductId, Version version,
445 ToscaServiceModel serviceModel) {
446 if (!isServiceModelExist(serviceModel)) {
450 enrichedServiceModelDao.deleteAll(vendorSoftwareProductId, version);
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();
458 if (MapUtils.isEmpty(MessageContainerUtil.getMessageByLevel(ErrorLevel.ERROR, enrichErrors))) {
459 LOGGER.audit(AuditMessages.AUDIT_MSG + AuditMessages.ENRICHMENT_COMPLETED
460 + vendorSoftwareProductId);
462 enrichErrors.values().forEach(errorList ->
463 auditIfContainsErrors(errorList, vendorSoftwareProductId,
464 AuditMessages.ENRICHMENT_ERROR));
467 enrichedServiceModelDao
468 .storeServiceModel(vendorSoftwareProductId, version, enrichmentManager.getModel());
473 private Collection<ErrorCode> validateLicensingData(VspDetails vspDetails) {
474 MDC_DATA_DEBUG_MESSAGE.debugEntryMessage("VSP id", vspDetails.getId());
476 if (vspDetails.getVendorId() == null || vspDetails.getVlmVersion() == null
477 || vspDetails.getLicenseAgreement() == null
478 || CollectionUtils.isEmpty(vspDetails.getFeatureGroups())) {
482 MDC_DATA_DEBUG_MESSAGE.debugExitMessage("VSP id", vspDetails.getId());
483 return vendorLicenseFacade
484 .validateLicensingData(vspDetails.getVendorId(), vspDetails.getVlmVersion(),
485 vspDetails.getLicenseAgreement(), vspDetails.getFeatureGroups());
489 public VspDetails createVsp(VspDetails vspDetails) {
490 MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(null);
492 vspInfoDao.create(vspDetails);
493 vspInfoDao.updateQuestionnaireData(vspDetails.getId(), vspDetails.getVersion(),
494 new JsonSchemaDataGenerator(getVspQuestionnaireSchema(null)).generateData());
496 MDC_DATA_DEBUG_MESSAGE.debugExitMessage(null);
501 public void updateVsp(VspDetails vspDetails) {
502 MDC_DATA_DEBUG_MESSAGE.debugEntryMessage("VSP id", vspDetails.getId());
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()));
510 vspDetails.setOnboardingMethod(retrieved.getOnboardingMethod());
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);
516 updateUniqueName(retrieved.getName(), vspDetails.getName());
517 vspInfoDao.update(vspDetails);
519 MDC_DATA_DEBUG_MESSAGE.debugExitMessage("VSP id", vspDetails.getId());
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);
547 public VspDetails getVsp(String vspId, Version version) {
548 MDC_DATA_DEBUG_MESSAGE.debugEntryMessage("VSP id", vspId);
550 VspDetails vsp = getValidatedVsp(vspId, version);
552 MDC_DATA_DEBUG_MESSAGE.debugExitMessage("VSP id", vspId);
556 private VspDetails getValidatedVsp(String vspId, Version version) {
557 VspDetails vsp = vspInfoDao.get(new VspDetails(vspId, version));
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());
568 public void deleteVsp(String vspId) {
569 MDC_DATA_DEBUG_MESSAGE.debugEntryMessage("VSP id", vspId);
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);
576 throw new UnsupportedOperationException(
577 VendorSoftwareProductConstants.UNSUPPORTED_OPERATION_ERROR);
581 public List<PackageInfo> listPackages(String category, String subCategory) {
582 return packageInfoDao.listByCategory(category, subCategory);
586 public File getTranslatedFile(String vspId, Version version) {
587 MDC_DATA_DEBUG_MESSAGE.debugEntryMessage("VSP id", vspId);
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());
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());
608 File translatedFile = new File(VendorSoftwareProductConstants.VSP_PACKAGE_ZIP);
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(),
621 MDC_DATA_DEBUG_MESSAGE.debugExitMessage("VSP id", vspId);
623 return translatedFile;
628 public byte[] getOrchestrationTemplateFile(String vspId, Version version) {
629 MDC_DATA_DEBUG_MESSAGE.debugEntryMessage("VSP id", vspId);
631 OrchestrationTemplateEntity uploadData = orchestrationTemplateDao.get(vspId, version);
632 ByteBuffer contentData = uploadData.getContentData();
633 if (contentData == null) {
637 ByteArrayOutputStream baos = new ByteArrayOutputStream();
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);
650 MDC_DATA_DEBUG_MESSAGE.debugExitMessage("VSP id", vspId);
651 return baos.toByteArray();
655 public OrchestrationTemplateEntity getOrchestrationTemplateInfo(String vspId, Version version) {
656 return orchestrationTemplateDao.getInfo(vspId, version);
660 public PackageInfo createPackage(String vspId, Version version) throws IOException {
661 MDC_DATA_DEBUG_MESSAGE.debugEntryMessage("VSP id", vspId);
663 ToscaServiceModel toscaServiceModel = enrichedServiceModelDao.getServiceModel(vspId, version);
664 VspDetails vspDetails = vspInfoDao.get(new VspDetails(vspId, version));
665 Version vlmVersion = vspDetails.getVlmVersion();
667 PackageInfo packageInfo = createPackageInfo(vspDetails);
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)));
678 packageInfoDao.create(packageInfo);
680 LOGGER.audit(AuditMessages.AUDIT_MSG + AuditMessages.CREATE_PACKAGE + vspId);
682 MDC_DATA_DEBUG_MESSAGE.debugExitMessage("VSP id", vspId);
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
700 public QuestionnaireResponse getVspQuestionnaire(String vspId, Version version) {
701 MDC_DATA_DEBUG_MESSAGE.debugEntryMessage("VSP id", vspId);
703 VspQuestionnaireEntity retrieved = vspInfoDao.getQuestionnaire(vspId, version);
704 VersioningUtil.validateEntityExistence(retrieved, new VspQuestionnaireEntity(vspId, version),
705 VspDetails.ENTITY_TYPE);
707 String questionnaireData = retrieved.getQuestionnaireData();
709 QuestionnaireResponse questionnaireResponse = new QuestionnaireResponse();
710 questionnaireResponse.setData(questionnaireData);
711 questionnaireResponse.setSchema(getVspQuestionnaireSchema(null));
713 MDC_DATA_DEBUG_MESSAGE.debugExitMessage("VSP id", vspId);
715 return questionnaireResponse;
719 public void updateVspQuestionnaire(String vspId, Version version, String questionnaireData) {
720 MDC_DATA_DEBUG_MESSAGE.debugEntryMessage("VSP id", vspId);
722 vspInfoDao.updateQuestionnaireData(vspId, version, questionnaireData);
724 MDC_DATA_DEBUG_MESSAGE.debugExitMessage("VSP id", vspId);
728 private Map<String, List<ErrorMessage>> validateOrchestrationTemplate(
729 OrchestrationTemplateEntity orchestrationTemplate) throws IOException {
731 if (!isOrchestrationTemplateExist(orchestrationTemplate)) {
734 Map<String, List<ErrorMessage>> validationErrors = new HashMap<>();
736 FileContentHandler fileContentMap = CommonUtil.validateAndUploadFileContent(
737 OnboardingTypesEnum.getOnboardingTypesEnum(orchestrationTemplate.getFileSuffix()),
738 orchestrationTemplate.getContentData().array());
740 if (CommonUtil.isFileOriginFromZip(orchestrationTemplate.getFileSuffix())) {
741 ValidationManager validationManager =
742 ValidationManagerUtil.initValidationManager(fileContentMap);
743 validationErrors.putAll(validationManager.validate());
747 MapUtils.isEmpty(MessageContainerUtil.getMessageByLevel(ErrorLevel.ERROR, validationErrors))
748 ? null : validationErrors;
751 private QuestionnaireValidationResult validateQuestionnaire(String vspId, Version version,
752 String onboardingMethod) {
753 MDC_DATA_DEBUG_MESSAGE.debugEntryMessage("VSP id", vspId);
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);
762 Collection<NicEntity> nics = nicDao.listByVsp(vspId, version);
764 Map<String, List<String>> nicNamesByComponent = new HashMap<>();
765 for (NicEntity nicEntity : nics) {
766 compositionEntityDataManager.addEntity(nicEntity, null);
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());
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))));
782 Collection<ComputeEntity> computes = computeDao.listByVsp(vspId, version);
783 computes.forEach(compute -> compositionEntityDataManager.addEntity(compute, null));
785 if (OnboardingMethod.Manual.name().equals(onboardingMethod)) {
786 Collection<ImageEntity> images = imageDao.listByVsp(vspId, version);
787 images.forEach(image -> compositionEntityDataManager.addEntity(image, null));
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();
799 MDC_DATA_DEBUG_MESSAGE.debugExitMessage("VSP id", vspId);
800 return new QuestionnaireValidationResult(
801 compositionEntityDataManager.getAllErrorsByVsp(vspId));
804 MDC_DATA_DEBUG_MESSAGE.debugExitMessage("VSP id", vspId);
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));
813 if (vspDetails == null) {
817 String vspName = vspDetails.getName();
818 ByteBuffer infoArtifactAsByteBuffer;
819 File infoArtifactFile;
821 infoArtifactAsByteBuffer = ByteBuffer.wrap(informationArtifactGenerator.generate(vspId,
822 version).getBytes());
826 String.format(VendorSoftwareProductConstants.INFORMATION_ARTIFACT_NAME, vspName));
827 try (OutputStream out = new BufferedOutputStream(new FileOutputStream(infoArtifactFile))) {
828 out.write(infoArtifactAsByteBuffer.array());
831 } catch (IOException ex) {
832 throw new CoreException(new InformationArtifactCreationErrorBuilder(vspId).build(), ex);
835 MDC_DATA_DEBUG_MESSAGE.debugExitMessage("VSP id", vspId);
836 return infoArtifactFile;
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);
846 void updateUniqueName(String oldVspName, String newVspName) {
847 UniqueValueUtil.updateUniqueValue(
848 VendorSoftwareProductConstants.UniqueValues.VENDOR_SOFTWARE_PRODUCT_NAME,
849 oldVspName, newVspName);
853 public Collection<ComputeEntity> getComputeByVsp(String vspId, Version version) {
854 return computeDao.listByVsp(vspId, version);
857 private boolean isOrchestrationTemplateExist(OrchestrationTemplateEntity orchestrationTemplate) {
858 return orchestrationTemplate != null &&
859 orchestrationTemplate.getContentData() != null &&
860 orchestrationTemplate.getFileSuffix() != null &&
861 orchestrationTemplate.getFileName() != null;
864 private boolean isServiceModelExist(ToscaServiceModel serviceModel) {
865 return serviceModel != null && serviceModel.getEntryDefinitionServiceTemplate() != null;
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(),