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.validation.api.ValidationManager;
35 import org.openecomp.core.validation.util.MessageContainerUtil;
36 import org.openecomp.sdc.activityLog.ActivityLogManager;
37 import org.openecomp.sdc.activitylog.dao.type.ActivityLogEntity;
38 import org.openecomp.sdc.common.errors.CoreException;
39 import org.openecomp.sdc.common.errors.ErrorCode;
40 import org.openecomp.sdc.common.errors.ValidationErrorBuilder;
41 import org.openecomp.sdc.common.utils.CommonUtil;
42 import org.openecomp.sdc.common.utils.SdcCommon;
43 import org.openecomp.sdc.datatypes.error.ErrorLevel;
44 import org.openecomp.sdc.datatypes.error.ErrorMessage;
45 import org.openecomp.sdc.healing.api.HealingManager;
46 import org.openecomp.sdc.logging.api.Logger;
47 import org.openecomp.sdc.logging.api.LoggerFactory;
48 import org.openecomp.sdc.logging.context.impl.MdcDataDebugMessage;
49 import org.openecomp.sdc.logging.context.impl.MdcDataErrorMessage;
50 import org.openecomp.sdc.logging.types.LoggerConstants;
51 import org.openecomp.sdc.logging.types.LoggerErrorCode;
52 import org.openecomp.sdc.logging.types.LoggerServiceName;
53 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
54 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
55 import org.openecomp.sdc.tosca.services.impl.ToscaFileOutputServiceCsarImpl;
56 import org.openecomp.sdc.validation.util.ValidationManagerUtil;
57 import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacade;
58 import org.openecomp.sdc.vendorlicense.licenseartifacts.VendorLicenseArtifactsService;
59 import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductConstants;
60 import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductManager;
61 import org.openecomp.sdc.vendorsoftwareproduct.dao.OrchestrationTemplateDao;
62 import org.openecomp.sdc.vendorsoftwareproduct.dao.PackageInfoDao;
63 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductDao;
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.NicEntity;
69 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.PackageInfo;
70 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.UploadDataEntity;
71 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
72 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspQuestionnaireEntity;
73 import org.openecomp.sdc.vendorsoftwareproduct.errors.ComponentDependencyModelErrorBuilder;
74 import org.openecomp.sdc.vendorsoftwareproduct.errors.CreatePackageForNonFinalVendorSoftwareProductErrorBuilder;
75 import org.openecomp.sdc.vendorsoftwareproduct.errors.FileCreationErrorBuilder;
76 import org.openecomp.sdc.vendorsoftwareproduct.errors.InformationArtifactCreationErrorBuilder;
77 import org.openecomp.sdc.vendorsoftwareproduct.errors.PackageInvalidErrorBuilder;
78 import org.openecomp.sdc.vendorsoftwareproduct.errors.PackageNotFoundErrorBuilder;
79 import org.openecomp.sdc.vendorsoftwareproduct.errors.TranslationFileCreationErrorBuilder;
80 import org.openecomp.sdc.vendorsoftwareproduct.errors.VendorSoftwareProductInvalidErrorBuilder;
81 import org.openecomp.sdc.vendorsoftwareproduct.informationArtifact.InformationArtifactGenerator;
82 import org.openecomp.sdc.vendorsoftwareproduct.services.composition.CompositionEntityDataManager;
83 import org.openecomp.sdc.vendorsoftwareproduct.services.schemagenerator.SchemaGenerator;
84 import org.openecomp.sdc.vendorsoftwareproduct.types.QuestionnaireResponse;
85 import org.openecomp.sdc.vendorsoftwareproduct.types.QuestionnaireValidationResult;
86 import org.openecomp.sdc.vendorsoftwareproduct.types.ValidationResponse;
87 import org.openecomp.sdc.vendorsoftwareproduct.types.VersionedVendorSoftwareProductInfo;
88 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityId;
89 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityType;
90 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.Nic;
91 import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.ComponentQuestionnaireSchemaInput;
92 import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.SchemaTemplateContext;
93 import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.SchemaTemplateInput;
94 import org.openecomp.sdc.vendorsoftwareproduct.utils.ComponentDependencyTracker;
95 import org.openecomp.sdc.versioning.VersioningManager;
96 import org.openecomp.sdc.versioning.VersioningUtil;
97 import org.openecomp.sdc.versioning.dao.types.Version;
98 import org.openecomp.sdc.versioning.dao.types.VersionStatus;
99 import org.openecomp.sdc.versioning.errors.RequestedVersionInvalidErrorBuilder;
100 import org.openecomp.sdc.versioning.types.VersionInfo;
101 import org.openecomp.sdc.versioning.types.VersionableEntityAction;
102 import org.openecomp.sdcrests.activitylog.types.ActivityType;
103 import org.slf4j.MDC;
105 import java.io.BufferedOutputStream;
106 import java.io.ByteArrayInputStream;
107 import java.io.ByteArrayOutputStream;
109 import java.io.FileOutputStream;
110 import java.io.IOException;
111 import java.io.OutputStream;
112 import java.nio.ByteBuffer;
113 import java.util.ArrayList;
114 import java.util.Collection;
115 import java.util.HashMap;
116 import java.util.List;
117 import java.util.Map;
118 import java.util.zip.ZipInputStream;
119 import java.util.zip.ZipOutputStream;
121 public class VendorSoftwareProductManagerImpl implements VendorSoftwareProductManager {
122 private static final String VALIDATION_VSP_ID = "validationOnlyVspId";
123 private static final String VALIDATION_VSP_NAME = "validationOnlyVspName";
124 //private static final String VALIDATION_VSP_USER = "validationOnlyVspUser";
126 private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
127 private static final Logger logger =
128 LoggerFactory.getLogger(VendorSoftwareProductManagerImpl.class);
130 private OrchestrationTemplateDao orchestrationTemplateDao;
131 private VendorSoftwareProductInfoDao vspInfoDao;
132 private VersioningManager versioningManager;
133 private VendorSoftwareProductDao vendorSoftwareProductDao;
134 private VendorLicenseFacade vendorLicenseFacade;
135 private ServiceModelDao<ToscaServiceModel, ServiceElement> serviceModelDao;
136 private EnrichedServiceModelDao<ToscaServiceModel, ServiceElement> enrichedServiceModelDao;
137 private HealingManager healingManager;
138 private VendorLicenseArtifactsService licenseArtifactsService;
139 private CompositionEntityDataManager compositionEntityDataManager;
140 private InformationArtifactGenerator informationArtifactGenerator;
141 private PackageInfoDao packageInfoDao;
142 private ActivityLogManager activityLogManager;
145 public VendorSoftwareProductManagerImpl(
146 VersioningManager versioningManager,
147 VendorSoftwareProductDao vendorSoftwareProductDao,
148 OrchestrationTemplateDao orchestrationTemplateDataDao,
149 VendorSoftwareProductInfoDao vspInfoDao,
150 VendorLicenseFacade vendorLicenseFacade,
151 ServiceModelDao<ToscaServiceModel, ServiceElement> serviceModelDao,
152 EnrichedServiceModelDao<ToscaServiceModel, ServiceElement> enrichedServiceModelDao,
153 HealingManager healingManager,
154 VendorLicenseArtifactsService licenseArtifactsService,
155 CompositionEntityDataManager compositionEntityDataManager,
156 InformationArtifactGenerator informationArtifactGenerator,
157 PackageInfoDao packageInfoDao,
158 ActivityLogManager activityLogManager) {
159 this.versioningManager = versioningManager;
160 this.vendorSoftwareProductDao = vendorSoftwareProductDao;
161 this.orchestrationTemplateDao = orchestrationTemplateDataDao;
162 this.vspInfoDao = vspInfoDao;
163 this.vendorLicenseFacade = vendorLicenseFacade;
164 this.serviceModelDao = serviceModelDao;
165 this.enrichedServiceModelDao = enrichedServiceModelDao;
166 this.healingManager = healingManager;
167 this.licenseArtifactsService = licenseArtifactsService;
168 this.compositionEntityDataManager = compositionEntityDataManager;
169 this.informationArtifactGenerator = informationArtifactGenerator;
170 this.packageInfoDao = packageInfoDao;
171 this.activityLogManager = activityLogManager;
173 registerToVersioning();
176 private void registerToVersioning() {
177 vendorSoftwareProductDao.registerVersioning(
178 VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE);
179 serviceModelDao.registerVersioning(
180 VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE);
181 enrichedServiceModelDao.registerVersioning(
182 VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE);
186 public Version checkout(String vendorSoftwareProductId, String user) {
187 mdcDataDebugMessage.debugEntryMessage("VSP id", vendorSoftwareProductId);
188 MDC.put(LoggerConstants.SERVICE_NAME, LoggerServiceName.Checkout_Entity.toString());
190 Version newVersion = versioningManager
191 .checkout(VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE,
192 vendorSoftwareProductId, user);
194 if (newVersion != null) {
195 ActivityLogEntity activityLogEntity =
196 new ActivityLogEntity(vendorSoftwareProductId, String.valueOf(newVersion.getMajor() + 1),
197 ActivityType.CHECKOUT.toString(), user, true, "", "");
198 activityLogManager.addActionLog(activityLogEntity, user);
201 mdcDataDebugMessage.debugExitMessage("VSP id", vendorSoftwareProductId);
207 public Version undoCheckout(String vendorSoftwareProductId, String user) {
208 mdcDataDebugMessage.debugEntryMessage("VSP id", vendorSoftwareProductId);
211 getVersionInfo(vendorSoftwareProductId, VersionableEntityAction.Read, user)
213 String preVspName = vspInfoDao
214 .get(new VspDetails(vendorSoftwareProductId, version)).getName();
216 Version newVersion = versioningManager.undoCheckout(
217 VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE,
218 vendorSoftwareProductId, user);
220 String postVspName = vspInfoDao
221 .get(new VspDetails(vendorSoftwareProductId, newVersion))
224 updateUniqueName(preVspName, postVspName);
226 mdcDataDebugMessage.debugExitMessage("VSP id", vendorSoftwareProductId);
232 public Version checkin(String vendorSoftwareProductId, String user) {
233 mdcDataDebugMessage.debugEntryMessage("VSP id", vendorSoftwareProductId);
235 Version newVersion = versioningManager.checkin(
236 VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE,
237 vendorSoftwareProductId, user, null);
239 if (newVersion != null) {
240 ActivityLogEntity activityLogEntity =
241 new ActivityLogEntity(vendorSoftwareProductId, String.valueOf(newVersion.getMajor() + 1),
242 ActivityType.CHECKIN.toString(), user, true, "", "");
243 activityLogManager.addActionLog(activityLogEntity, user);
246 mdcDataDebugMessage.debugExitMessage("VSP id", vendorSoftwareProductId);
252 public ValidationResponse submit(String vspId, String user) throws IOException {
253 mdcDataDebugMessage.debugEntryMessage("VSP id", vspId);
255 Version version = getVersionInfo(vspId, VersionableEntityAction.Read, user).getActiveVersion();
256 VspDetails vspDetails = getVsp(vspId, version, user);
257 UploadDataEntity uploadData = orchestrationTemplateDao.getOrchestrationTemplate(vspId, version);
258 ToscaServiceModel serviceModel =
259 serviceModelDao.getServiceModel(vspId, vspDetails.getVersion());
261 ValidationResponse validationResponse = new ValidationResponse();
263 .setVspErrors(validateCompletedVendorSoftwareProduct(vspDetails, uploadData, serviceModel),
264 LoggerServiceName.Submit_VSP, LoggerTragetServiceName.SUBMIT_VSP);
266 if (isCyclicDependencyInComponents(vspId, vspDetails.getVersion())) {
267 Collection<ErrorCode> vspErrors = validationResponse.getVspErrors() == null
269 : validationResponse.getVspErrors();
270 vspErrors.add(ComponentDependencyModelErrorBuilder
271 .getcyclicDependencyComponentErrorBuilder());
272 validationResponse.setVspErrors(vspErrors, LoggerServiceName.Submit_VSP,
273 LoggerTragetServiceName.SUBMIT_VSP);
276 validationResponse.setLicensingDataErrors(validateLicensingData(vspDetails));
278 .setUploadDataErrors(validateUploadData(uploadData), LoggerServiceName.Submit_VSP,
279 LoggerTragetServiceName.SUBMIT_VSP);
280 validationResponse.setQuestionnaireValidationResult(
281 validateQuestionnaire(vspDetails.getId(), vspDetails.getVersion()));
283 validationResponse.setCompilationErrors(
284 compile(vspId, vspDetails.getVersion(), serviceModel),
285 LoggerServiceName.Submit_VSP, LoggerTragetServiceName.SUBMIT_VSP);
287 if (validationResponse.isValid()) {
288 Version newVersion = versioningManager.submit(
289 VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE,
291 ActivityLogEntity activityLogEntity = new ActivityLogEntity(vspDetails.getId(), String
292 .valueOf(newVersion.getMajor()),
293 ActivityType.SUBMIT.toString(), user, true, "", "");
294 activityLogManager.addActionLog(activityLogEntity, user);
297 mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
299 return validationResponse;
302 private boolean isCyclicDependencyInComponents(String vendorSoftwareProductId,
304 final Collection<ComponentDependencyModelEntity> componentDependencyModelEntities =
305 vendorSoftwareProductDao.listComponentDependencies(vendorSoftwareProductId, version);
306 ComponentDependencyTracker dependencyTracker = new ComponentDependencyTracker();
308 for (ComponentDependencyModelEntity entity : componentDependencyModelEntities) {
309 dependencyTracker.addDependency(entity.getSourceComponentId(), entity.getTargetComponentId());
311 return dependencyTracker.isCyclicDependencyPresent();
314 private static List<ErrorCode> validateCompletedVendorSoftwareProduct(
315 VspDetails vspDetails, UploadDataEntity uploadData, Object serviceModel) {
317 List<ErrorCode> errros = new ArrayList<>();
319 if (vspDetails.getName() == null) {
320 errros.add(createMissingMandatoryFieldError("name"));
322 if (vspDetails.getDescription() == null) {
323 errros.add(createMissingMandatoryFieldError("description"));
325 if (vspDetails.getVendorId() == null) {
326 errros.add(createMissingMandatoryFieldError("vendor Id"));
328 if (vspDetails.getVlmVersion() == null) {
329 errros.add(createMissingMandatoryFieldError(
330 "licensing version (in the format of: {integer}.{integer})"));
332 if (vspDetails.getCategory() == null) {
333 errros.add(createMissingMandatoryFieldError("category"));
335 if (vspDetails.getSubCategory() == null) {
336 errros.add(createMissingMandatoryFieldError("sub category"));
338 if (vspDetails.getLicenseAgreement() == null) {
339 errros.add(createMissingMandatoryFieldError("license agreement"));
341 if (CollectionUtils.isEmpty(vspDetails.getFeatureGroups())) {
342 errros.add(createMissingMandatoryFieldError("feature groups"));
344 if (uploadData == null || uploadData.getContentData() == null || serviceModel == null) {
346 new VendorSoftwareProductInvalidErrorBuilder(vspDetails.getId(), vspDetails.getVersion())
350 return errros.isEmpty() ? null : errros;
353 private static ErrorCode createMissingMandatoryFieldError(String fieldName) {
354 return new ValidationErrorBuilder("must be supplied", fieldName).build();
357 String getVspQuestionnaireSchema(SchemaTemplateInput schemaInput) {
358 mdcDataDebugMessage.debugEntryMessage(null);
359 mdcDataDebugMessage.debugExitMessage(null);
360 return SchemaGenerator
361 .generate(SchemaTemplateContext.questionnaire, CompositionEntityType.vsp, schemaInput);
364 private static void sortVspListByModificationTimeDescOrder(
365 List<VersionedVendorSoftwareProductInfo> vsps) {
366 vsps.sort((o1, o2) -> o2.getVspDetails().getWritetimeMicroSeconds()
367 .compareTo(o1.getVspDetails().getWritetimeMicroSeconds()));
371 private Map<String, List<ErrorMessage>> compile(String vendorSoftwareProductId, Version version,
372 ToscaServiceModel serviceModel) {
373 if (serviceModel == null) {
377 enrichedServiceModelDao.deleteAll(vendorSoftwareProductId, version);
379 EnrichmentManager<ToscaServiceModel> enrichmentManager =
380 EnrichmentManagerFactory.getInstance().createInterface();
381 enrichmentManager.init(vendorSoftwareProductId, version);
382 enrichmentManager.setModel(serviceModel);
383 Map<String, List<ErrorMessage>> enrichErrors = enrichmentManager.enrich();
385 enrichedServiceModelDao
386 .storeServiceModel(vendorSoftwareProductId, version, enrichmentManager.getModel());
391 private Collection<ErrorCode> validateLicensingData(VspDetails vspDetails) {
392 mdcDataDebugMessage.debugEntryMessage("VSP id", vspDetails.getId());
394 if (vspDetails.getVendorId() == null || vspDetails.getVlmVersion() == null
395 || vspDetails.getLicenseAgreement() == null
396 || CollectionUtils.isEmpty(vspDetails.getFeatureGroups())) {
400 mdcDataDebugMessage.debugExitMessage("VSP id", vspDetails.getId());
401 return vendorLicenseFacade
402 .validateLicensingData(vspDetails.getVendorId(), vspDetails.getVlmVersion(),
403 vspDetails.getLicenseAgreement(), vspDetails.getFeatureGroups());
407 public String fetchValidationVsp(String user) {
409 validateUniqueName(VALIDATION_VSP_NAME);
410 } catch (Exception ignored) {
411 return VALIDATION_VSP_ID;
413 VspDetails validationVsp = new VspDetails();
414 validationVsp.setName(VALIDATION_VSP_NAME);
415 validationVsp.setId(VALIDATION_VSP_ID);
416 Version version = versioningManager.create(
417 VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE,
418 validationVsp.getId(),
420 validationVsp.setVersion(version);
422 vspInfoDao.create(validationVsp);
423 createUniqueName(VALIDATION_VSP_NAME);
424 return VALIDATION_VSP_ID;
428 public VspDetails createVsp(VspDetails vspDetails, String user) {
429 mdcDataDebugMessage.debugEntryMessage(null);
431 validateUniqueName(vspDetails.getName());
433 vspInfoDao.create(vspDetails);//id will be set in the dao
434 vspInfoDao.updateQuestionnaireData(vspDetails.getId(), null,
435 new JsonSchemaDataGenerator(getVspQuestionnaireSchema(null)).generateData());
437 Version version = versioningManager
438 .create(VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE,
439 vspDetails.getId(), user);
440 vspDetails.setVersion(version);
441 ActivityLogEntity activityLogEntity = new ActivityLogEntity(vspDetails.getId(), String
442 .valueOf(vspDetails.getVersion().getMajor() + 1),
443 ActivityType.CREATE_NEW.toString(), user, true, "", "");
444 activityLogManager.addActionLog(activityLogEntity, user);
445 String vspName = vspDetails.getName();
446 createUniqueName(vspName);
447 mdcDataDebugMessage.debugExitMessage(null);
452 public List<VersionedVendorSoftwareProductInfo> listVsps(String versionFilter, String user) {
453 mdcDataDebugMessage.debugEntryMessage(null);
455 Map<String, VersionInfo> idToVersionsInfo = versioningManager.listEntitiesVersionInfo(
456 VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE, user,
457 VersionableEntityAction.Read);
459 List<VersionedVendorSoftwareProductInfo> vsps = new ArrayList<>();
460 for (Map.Entry<String, VersionInfo> entry : idToVersionsInfo.entrySet()) {
461 VersionInfo versionInfo = entry.getValue();
462 if (versionFilter != null && versionFilter.equals(VersionStatus.Final.name())) {
463 if (versionInfo.getLatestFinalVersion() == null) {
466 versionInfo.setActiveVersion(versionInfo.getLatestFinalVersion());
467 versionInfo.setStatus(VersionStatus.Final);
468 versionInfo.setLockingUser(null);
471 Version version = versionInfo.getActiveVersion();
472 if (user.equals(versionInfo.getLockingUser())) {
473 version.setStatus(VersionStatus.Locked);
475 VspDetails vsp = vspInfoDao.get(new VspDetails(entry.getKey(), version));
476 if (vsp != null && !vsp.getId().equals(VALIDATION_VSP_ID)) {
477 vsp.setValidationDataStructure(null);
478 vsps.add(new VersionedVendorSoftwareProductInfo(vsp, versionInfo));
482 sortVspListByModificationTimeDescOrder(vsps);
484 mdcDataDebugMessage.debugExitMessage(null);
490 public void updateVsp(VspDetails vspDetails, String user) {
491 mdcDataDebugMessage.debugEntryMessage("VSP id", vspDetails.getId());
493 VspDetails retrieved = vspInfoDao.get(vspDetails);
495 updateUniqueName(retrieved.getName(), vspDetails.getName());
496 vspDetails.setOldVersion(retrieved.getOldVersion());
498 vspInfoDao.update(vspDetails);
499 //vendorSoftwareProductDao.updateVspLatestModificationTime(vspDetails.getId(), activeVersion);
501 mdcDataDebugMessage.debugExitMessage("VSP id", vspDetails.getId());
506 public VspDetails getVsp(String vspId, Version version, String user) {
507 mdcDataDebugMessage.debugEntryMessage("VSP id", vspId);
509 VspDetails vsp = vspInfoDao.get(new VspDetails(vspId, version));
511 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
512 LoggerTragetServiceName.GET_VSP, ErrorLevel.ERROR.name(),
513 LoggerErrorCode.DATA_ERROR.getErrorCode(), "Requested VSP not found");
514 throw new CoreException(new VendorSoftwareProductNotFoundErrorBuilder(vspId).build());
516 vsp.setValidationData(orchestrationTemplateDao.getValidationData(vspId, version));
518 mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
523 public Version callAutoHeal(String vspId, VersionInfo versionInfo,
524 VspDetails vendorSoftwareProductInfo, String user)
526 switch (versionInfo.getStatus()) {
528 if (user.equals(versionInfo.getLockingUser())) {
529 autoHeal(vspId, versionInfo.getActiveVersion(), vendorSoftwareProductInfo,
530 versionInfo.getLockingUser());
532 return versionInfo.getActiveVersion();
534 Version checkoutVersion = checkout(vspId, user);
535 autoHeal(vspId, checkoutVersion, vendorSoftwareProductInfo, user);
536 return checkin(vspId, user);
538 Version checkoutFinalVersion = checkout(vspId,user);
539 autoHeal(vspId, checkoutFinalVersion, vendorSoftwareProductInfo, user);
540 Version checkinFinalVersion = checkin(vspId,user);
541 ValidationResponse response = submit(vspId, user);
542 if(!response.isValid()) {
543 return checkout(vspId, user);
547 Version finalVersion = checkinFinalVersion.calculateNextFinal();
548 createPackage(vspId, finalVersion, user);
550 } catch (IOException e) {
551 throw new Exception(e.getMessage());
554 return versionInfo.getActiveVersion();
559 public void deleteVsp(String vspId, String user) {
560 mdcDataDebugMessage.debugEntryMessage("VSP id", vspId);
562 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
563 LoggerTragetServiceName.DELETE_VSP, ErrorLevel.ERROR.name(),
564 LoggerErrorCode.PERMISSION_ERROR.getErrorCode(), "Unsupported operation");
565 mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
567 throw new UnsupportedOperationException(
568 VendorSoftwareProductConstants.UNSUPPORTED_OPERATION_ERROR);
572 public void heal(String vspId, Version version, String user) {
573 mdcDataDebugMessage.debugEntryMessage("VSP id", vspId);
575 VersionInfo versionInfo = getVersionInfo(vspId, VersionableEntityAction.Read, user);
577 version = VersionStatus.Locked.equals(versionInfo.getStatus())
578 ? versionInfo.getActiveVersion()
579 : checkout(vspId, user);
580 version.setStatus(VersionStatus.Locked);
582 healingManager.healAll(getHealingParamsAsMap(vspId, version, user));
584 VspDetails vspDetails = new VspDetails(vspId, version);
585 vspDetails.setOldVersion(null);
586 vspInfoDao.updateOldVersionIndication(vspDetails);
588 logger.audit("Healed VSP " + vspDetails.getId());
589 mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
592 private void autoHeal(String vspId, Version checkoutVersion, VspDetails vspDetails, String user) {
593 mdcDataDebugMessage.debugEntryMessage("VSP id", vspId);
595 checkoutVersion.setStatus(VersionStatus.Locked);
596 Map<String, Object> healingParams = getHealingParamsAsMap(vspId, checkoutVersion, user);
597 healingManager.healAll(healingParams);
598 vspDetails.setVersion(checkoutVersion);
599 vspDetails.setOldVersion(null);
600 vspInfoDao.updateOldVersionIndication(vspDetails);
602 logger.audit("Healed VSP " + vspDetails.getName());
604 mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
607 private Map<String, Object> getHealingParamsAsMap(String vspId, Version version, String user) {
608 Map<String, Object> healingParams = new HashMap<>();
610 healingParams.put(SdcCommon.VSP_ID, vspId);
611 healingParams.put(SdcCommon.VERSION, version);
612 healingParams.put(SdcCommon.USER, user);
614 return healingParams;
618 public List<PackageInfo> listPackages(String category, String subCategory) {
619 return packageInfoDao.listByCategory(category, subCategory);
623 public File getTranslatedFile(String vspId, Version version, String user) {
624 mdcDataDebugMessage.debugEntryMessage("VSP id", vspId);
626 if (version == null) {
627 errorMessage = "Package not found";
628 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
629 LoggerTragetServiceName.GET_TRANSLATED_FILE, ErrorLevel.ERROR.name(),
630 LoggerErrorCode.DATA_ERROR.getErrorCode(), errorMessage);
631 throw new CoreException(new PackageNotFoundErrorBuilder(vspId).build());
632 } else if (!version.isFinal()) {
633 errorMessage = "Invalid requested version";
634 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
635 LoggerTragetServiceName.GET_VERSION_INFO, ErrorLevel.ERROR.name(),
636 LoggerErrorCode.DATA_ERROR.getErrorCode(), errorMessage);
637 throw new CoreException(new RequestedVersionInvalidErrorBuilder().build());
640 PackageInfo packageInfo =
641 packageInfoDao.get(new PackageInfo(vspId, version));
642 if (packageInfo == null) {
643 errorMessage = "Package not found";
644 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
645 LoggerTragetServiceName.GET_TRANSLATED_FILE, ErrorLevel.ERROR.name(),
646 LoggerErrorCode.DATA_ERROR.getErrorCode(), errorMessage);
647 throw new CoreException(new PackageNotFoundErrorBuilder(vspId, version).build());
650 ByteBuffer translatedFileBuffer = packageInfo.getTranslatedFile();
651 if (translatedFileBuffer == null) {
652 errorMessage = "Package not found";
653 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
654 LoggerTragetServiceName.GET_TRANSLATED_FILE, ErrorLevel.ERROR.name(),
655 LoggerErrorCode.DATA_ERROR.getErrorCode(), errorMessage);
656 throw new CoreException(new PackageInvalidErrorBuilder(vspId, version).build());
659 File translatedFile = new File(VendorSoftwareProductConstants.VSP_PACKAGE_ZIP);
662 FileOutputStream fos = new FileOutputStream(translatedFile);
663 fos.write(translatedFileBuffer.array());
665 } catch (IOException exception) {
666 errorMessage = "Can't create package";
667 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
668 LoggerTragetServiceName.CREATE_TRANSLATED_FILE, ErrorLevel.ERROR.name(),
669 LoggerErrorCode.DATA_ERROR.getErrorCode(), errorMessage);
670 throw new CoreException(new TranslationFileCreationErrorBuilder(vspId, version).build(),
674 mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
676 return translatedFile;
681 public byte[] getOrchestrationTemplateFile(String vspId, Version version, String user) {
682 mdcDataDebugMessage.debugEntryMessage("VSP id", vspId);
684 UploadDataEntity uploadData = orchestrationTemplateDao.getOrchestrationTemplate(vspId, version);
685 ByteBuffer contentData = uploadData.getContentData();
686 if (contentData == null) {
690 ByteArrayOutputStream baos = new ByteArrayOutputStream();
692 try (final ZipOutputStream zos = new ZipOutputStream(baos);
693 ZipInputStream zipStream = new ZipInputStream(
694 new ByteArrayInputStream(contentData.array()))) {
695 zos.write(contentData.array());
696 } catch (IOException exception) {
697 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
698 LoggerTragetServiceName.GET_UPLOADED_HEAT, ErrorLevel.ERROR.name(),
699 LoggerErrorCode.DATA_ERROR.getErrorCode(), "Can't get uploaded HEAT");
700 throw new CoreException(new FileCreationErrorBuilder(vspId).build(), exception);
703 mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
704 return baos.toByteArray();
708 public PackageInfo createPackage(String vspId, Version version, String user) throws IOException {
709 mdcDataDebugMessage.debugEntryMessage("VSP id", vspId);
711 if (!version.isFinal()) {
712 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
713 LoggerTragetServiceName.CREATE_PACKAGE, ErrorLevel.ERROR.name(),
714 LoggerErrorCode.PERMISSION_ERROR.getErrorCode(), "Can't create package");
715 throw new CoreException(
716 new CreatePackageForNonFinalVendorSoftwareProductErrorBuilder(vspId, version)
720 ToscaServiceModel toscaServiceModel = enrichedServiceModelDao.getServiceModel(vspId, version);
721 VspDetails vspDetails = vspInfoDao.get(new VspDetails(vspId, version));
722 Version vlmVersion = vspDetails.getVlmVersion();
724 PackageInfo packageInfo = createPackageInfo(vspId, vspDetails);
726 ToscaFileOutputServiceCsarImpl toscaServiceTemplateServiceCsar =
727 new ToscaFileOutputServiceCsarImpl();
728 FileContentHandler licenseArtifacts = licenseArtifactsService
729 .createLicenseArtifacts(vspDetails.getId(), vspDetails.getVendorId(), vlmVersion,
730 vspDetails.getFeatureGroups(), user);
731 //todo add tosca validation here
732 packageInfo.setTranslatedFile(ByteBuffer.wrap(
733 toscaServiceTemplateServiceCsar.createOutputFile(toscaServiceModel, licenseArtifacts)));
735 packageInfoDao.create(packageInfo);
737 mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
741 private PackageInfo createPackageInfo(String vspId, VspDetails vspDetails) {
742 PackageInfo packageInfo = new PackageInfo();
743 packageInfo.setVspId(vspId);
744 packageInfo.setVersion(vspDetails.getVersion());
745 packageInfo.setVspName(vspDetails.getName());
746 packageInfo.setVspDescription(vspDetails.getDescription());
747 packageInfo.setCategory(vspDetails.getCategory());
748 packageInfo.setSubCategory(vspDetails.getSubCategory());
749 packageInfo.setVendorName(vspDetails.getVendorName());
750 packageInfo.setPackageType(VendorSoftwareProductConstants.CSAR);
751 packageInfo.setVendorRelease("1.0"); //todo TBD
757 public QuestionnaireResponse getVspQuestionnaire(String vspId, Version version, String user) {
758 mdcDataDebugMessage.debugEntryMessage("VSP id", vspId);
760 VspQuestionnaireEntity retrieved = vspInfoDao.getQuestionnaire(vspId, version);
761 VersioningUtil.validateEntityExistence(retrieved, new VspQuestionnaireEntity(vspId, version),
762 VspDetails.ENTITY_TYPE);
764 String questionnaireData = retrieved.getQuestionnaireData();
766 QuestionnaireResponse questionnaireResponse = new QuestionnaireResponse();
767 questionnaireResponse.setData(questionnaireData);
768 questionnaireResponse.setSchema(getVspQuestionnaireSchema(null));
770 mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
772 return questionnaireResponse;
776 public void updateVspQuestionnaire(String vspId, Version version, String questionnaireData,
778 mdcDataDebugMessage.debugEntryMessage("VSP id", vspId);
780 vspInfoDao.updateQuestionnaireData(vspId, version, questionnaireData);
782 mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
786 private Map<String, List<ErrorMessage>> validateUploadData(UploadDataEntity uploadData)
788 if (uploadData == null || uploadData.getContentData() == null) {
792 FileContentHandler fileContentMap =
793 CommonUtil.loadUploadFileContent(uploadData.getContentData().array());
795 ValidationManager validationManager =
796 ValidationManagerUtil.initValidationManager(fileContentMap);
797 Map<String, List<ErrorMessage>> validationErrors = validationManager.validate();
800 MapUtils.isEmpty(MessageContainerUtil.getMessageByLevel(ErrorLevel.ERROR, validationErrors))
801 ? null : validationErrors;
804 private VersionInfo getVersionInfo(String vendorSoftwareProductId, VersionableEntityAction action,
806 return versioningManager.getEntityVersionInfo(
807 VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE,
808 vendorSoftwareProductId, user, action);
812 private QuestionnaireValidationResult validateQuestionnaire(String vspId, Version version) {
813 mdcDataDebugMessage.debugEntryMessage("VSP id", vspId);
816 compositionEntityDataManager
817 .addEntity(vspInfoDao.getQuestionnaire(vspId, version), null);
819 Collection<NicEntity> nics = vendorSoftwareProductDao.listNicsByVsp(vspId, version);
821 Map<String, List<String>> nicNamesByComponent = new HashMap<>();
822 for (NicEntity nicEntity : nics) {
823 compositionEntityDataManager.addEntity(nicEntity, null);
825 Nic nic = nicEntity.getNicCompositionData();
826 if (nic != null && nic.getName() != null) {
827 List<String> nicNames =
828 nicNamesByComponent.computeIfAbsent(nicEntity.getComponentId(), k -> new ArrayList<>());
829 nicNames.add(nic.getName());
833 Collection<ComponentEntity> components =
834 vendorSoftwareProductDao.listComponentsCompositionAndQuestionnaire(vspId, version);
835 components.forEach(component -> compositionEntityDataManager.addEntity(component,
836 new ComponentQuestionnaireSchemaInput(nicNamesByComponent.get(component.getId()),
837 JsonUtil.json2Object(component.getQuestionnaireData(), Map.class))));
839 Map<CompositionEntityId, Collection<String>> errorsByEntityId =
840 compositionEntityDataManager.validateEntitiesQuestionnaire();
841 if (MapUtils.isNotEmpty(errorsByEntityId)) {
842 compositionEntityDataManager.buildTrees();
843 compositionEntityDataManager.addErrorsToTrees(errorsByEntityId);
844 /* Set<CompositionEntityValidationData> entitiesWithValidationErrors =
845 compositionEntityDataManager.getEntityListWithErrors();*/
846 //Collection<CompositionEntityValidationData> roots = compositionEntityDataManager.getTrees();
848 mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
849 return new QuestionnaireValidationResult(
850 compositionEntityDataManager.getAllErrorsByVsp(vspId));
853 mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
858 public File getInformationArtifact(String vspId, Version version, String user) {
859 mdcDataDebugMessage.debugEntryMessage("VSP id", vspId);
860 VspDetails vspDetails = vspInfoDao.get(new VspDetails(vspId, version));
862 if (vspDetails == null) {
866 String vspName = vspDetails.getName();
867 ByteBuffer infoArtifactAsByteBuffer;
868 File infoArtifactFile;
870 infoArtifactAsByteBuffer = ByteBuffer.wrap(informationArtifactGenerator.generate(vspId,
871 version).getBytes());
875 String.format(VendorSoftwareProductConstants.INFORMATION_ARTIFACT_NAME, vspName));
876 OutputStream out = new BufferedOutputStream(new FileOutputStream(infoArtifactFile));
877 out.write(infoArtifactAsByteBuffer.array());
879 } catch (IOException e) {
880 throw new CoreException(new InformationArtifactCreationErrorBuilder(vspId).build(), e);
883 mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
884 return infoArtifactFile;
887 void validateUniqueName(String vspName) {
888 UniqueValueUtil.validateUniqueValue(
889 VendorSoftwareProductConstants.UniqueValues.VENDOR_SOFTWARE_PRODUCT_NAME, vspName);
892 void createUniqueName(String vspName) {
893 UniqueValueUtil.createUniqueValue(
894 VendorSoftwareProductConstants.UniqueValues.VENDOR_SOFTWARE_PRODUCT_NAME, vspName);
897 void updateUniqueName(String oldVspName, String newVspName) {
898 UniqueValueUtil.updateUniqueValue(
899 VendorSoftwareProductConstants.UniqueValues.VENDOR_SOFTWARE_PRODUCT_NAME,
900 oldVspName, newVspName);