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.Arrays;
115 import java.util.Collection;
116 import java.util.HashMap;
117 import java.util.List;
118 import java.util.Map;
119 import java.util.zip.ZipInputStream;
120 import java.util.zip.ZipOutputStream;
122 public class VendorSoftwareProductManagerImpl implements VendorSoftwareProductManager {
123 private static String VALIDATION_VSP_ID = "validationOnlyVspId";
124 private static final String VALIDATION_VSP_NAME = "validationOnlyVspName";
125 //private static final String VALIDATION_VSP_USER = "validationOnlyVspUser";
127 private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
128 private static final Logger logger =
129 LoggerFactory.getLogger(VendorSoftwareProductManagerImpl.class);
131 private OrchestrationTemplateDao orchestrationTemplateDao;
132 private VendorSoftwareProductInfoDao vspInfoDao;
133 private VersioningManager versioningManager;
134 private VendorSoftwareProductDao vendorSoftwareProductDao;
135 private VendorLicenseFacade vendorLicenseFacade;
136 private ServiceModelDao<ToscaServiceModel, ServiceElement> serviceModelDao;
137 private EnrichedServiceModelDao<ToscaServiceModel, ServiceElement> enrichedServiceModelDao;
138 private HealingManager healingManager;
139 private VendorLicenseArtifactsService licenseArtifactsService;
140 private CompositionEntityDataManager compositionEntityDataManager;
141 private InformationArtifactGenerator informationArtifactGenerator;
142 private PackageInfoDao packageInfoDao;
143 private ActivityLogManager activityLogManager;
146 public VendorSoftwareProductManagerImpl(
147 VersioningManager versioningManager,
148 VendorSoftwareProductDao vendorSoftwareProductDao,
149 OrchestrationTemplateDao orchestrationTemplateDataDao,
150 VendorSoftwareProductInfoDao vspInfoDao,
151 VendorLicenseFacade vendorLicenseFacade,
152 ServiceModelDao<ToscaServiceModel, ServiceElement> serviceModelDao,
153 EnrichedServiceModelDao<ToscaServiceModel, ServiceElement> enrichedServiceModelDao,
154 HealingManager healingManager,
155 VendorLicenseArtifactsService licenseArtifactsService,
156 CompositionEntityDataManager compositionEntityDataManager,
157 InformationArtifactGenerator informationArtifactGenerator,
158 PackageInfoDao packageInfoDao,
159 ActivityLogManager activityLogManager) {
160 this.versioningManager = versioningManager;
161 this.vendorSoftwareProductDao = vendorSoftwareProductDao;
162 this.orchestrationTemplateDao = orchestrationTemplateDataDao;
163 this.vspInfoDao = vspInfoDao;
164 this.vendorLicenseFacade = vendorLicenseFacade;
165 this.serviceModelDao = serviceModelDao;
166 this.enrichedServiceModelDao = enrichedServiceModelDao;
167 this.healingManager = healingManager;
168 this.licenseArtifactsService = licenseArtifactsService;
169 this.compositionEntityDataManager = compositionEntityDataManager;
170 this.informationArtifactGenerator = informationArtifactGenerator;
171 this.packageInfoDao = packageInfoDao;
172 this.activityLogManager = activityLogManager;
174 registerToVersioning();
177 private void registerToVersioning() {
178 vendorSoftwareProductDao.registerVersioning(
179 VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE);
180 serviceModelDao.registerVersioning(
181 VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE);
182 enrichedServiceModelDao.registerVersioning(
183 VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE);
187 public Version checkout(String vendorSoftwareProductId, String user) {
188 mdcDataDebugMessage.debugEntryMessage("VSP id", vendorSoftwareProductId);
189 MDC.put(LoggerConstants.SERVICE_NAME, LoggerServiceName.Checkout_Entity.toString());
191 Version newVersion = versioningManager
192 .checkout(VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE,
193 vendorSoftwareProductId, user);
195 if (newVersion != null) {
196 ActivityLogEntity activityLogEntity =
197 new ActivityLogEntity(vendorSoftwareProductId, String.valueOf(newVersion.getMajor() + 1),
198 ActivityType.CHECKOUT.toString(), user, true, "", "");
199 activityLogManager.addActionLog(activityLogEntity, user);
202 mdcDataDebugMessage.debugExitMessage("VSP id", vendorSoftwareProductId);
208 public Version undoCheckout(String vendorSoftwareProductId, String user) {
209 mdcDataDebugMessage.debugEntryMessage("VSP id", vendorSoftwareProductId);
212 getVersionInfo(vendorSoftwareProductId, VersionableEntityAction.Read, user)
214 String preVspName = vspInfoDao
215 .get(new VspDetails(vendorSoftwareProductId, version)).getName();
217 Version newVersion = versioningManager.undoCheckout(
218 VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE,
219 vendorSoftwareProductId, user);
221 String postVspName = vspInfoDao
222 .get(new VspDetails(vendorSoftwareProductId, newVersion))
225 updateUniqueName(preVspName, postVspName);
227 mdcDataDebugMessage.debugExitMessage("VSP id", vendorSoftwareProductId);
233 public Version checkin(String vendorSoftwareProductId, String user) {
234 mdcDataDebugMessage.debugEntryMessage("VSP id", vendorSoftwareProductId);
236 Version newVersion = versioningManager.checkin(
237 VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE,
238 vendorSoftwareProductId, user, null);
240 if (newVersion != null) {
241 ActivityLogEntity activityLogEntity =
242 new ActivityLogEntity(vendorSoftwareProductId, String.valueOf(newVersion.getMajor() + 1),
243 ActivityType.CHECKIN.toString(), user, true, "", "");
244 activityLogManager.addActionLog(activityLogEntity, user);
247 mdcDataDebugMessage.debugExitMessage("VSP id", vendorSoftwareProductId);
253 public ValidationResponse submit(String vspId, String user) throws IOException {
254 mdcDataDebugMessage.debugEntryMessage("VSP id", vspId);
256 Version version = getVersionInfo(vspId, VersionableEntityAction.Read, user).getActiveVersion();
257 VspDetails vspDetails = getVsp(vspId, version, user);
258 UploadDataEntity uploadData = orchestrationTemplateDao.getOrchestrationTemplate(vspId, version);
259 ToscaServiceModel serviceModel =
260 serviceModelDao.getServiceModel(vspId, vspDetails.getVersion());
262 ValidationResponse validationResponse = new ValidationResponse();
264 .setVspErrors(validateCompletedVendorSoftwareProduct(vspDetails, uploadData, serviceModel),
265 LoggerServiceName.Submit_VSP, LoggerTragetServiceName.SUBMIT_VSP);
267 if (isCyclicDependencyInComponents(vspId, vspDetails.getVersion())) {
268 Collection<ErrorCode> vspErrors = validationResponse.getVspErrors() == null
270 : validationResponse.getVspErrors();
271 vspErrors.add(ComponentDependencyModelErrorBuilder
272 .getcyclicDependencyComponentErrorBuilder());
273 validationResponse.setVspErrors(vspErrors, LoggerServiceName.Submit_VSP,
274 LoggerTragetServiceName.SUBMIT_VSP);
277 validationResponse.setLicensingDataErrors(validateLicensingData(vspDetails));
279 .setUploadDataErrors(validateUploadData(uploadData), LoggerServiceName.Submit_VSP,
280 LoggerTragetServiceName.SUBMIT_VSP);
281 validationResponse.setQuestionnaireValidationResult(
282 validateQuestionnaire(vspDetails.getId(), vspDetails.getVersion()));
284 validationResponse.setCompilationErrors(
285 compile(vspId, vspDetails.getVersion(), serviceModel),
286 LoggerServiceName.Submit_VSP, LoggerTragetServiceName.SUBMIT_VSP);
288 if (validationResponse.isValid()) {
289 Version newVersion = versioningManager.submit(
290 VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE,
292 ActivityLogEntity activityLogEntity = new ActivityLogEntity(vspDetails.getId(), String
293 .valueOf(newVersion.getMajor()),
294 ActivityType.SUBMIT.toString(), user, true, "", "");
295 activityLogManager.addActionLog(activityLogEntity, user);
298 mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
300 return validationResponse;
303 private boolean isCyclicDependencyInComponents(String vendorSoftwareProductId,
305 final Collection<ComponentDependencyModelEntity> componentDependencyModelEntities =
306 vendorSoftwareProductDao.listComponentDependencies(vendorSoftwareProductId, version);
307 ComponentDependencyTracker dependencyTracker = new ComponentDependencyTracker();
309 for (ComponentDependencyModelEntity entity : componentDependencyModelEntities) {
310 dependencyTracker.addDependency(entity.getSourceComponentId(), entity.getTargetComponentId());
312 return dependencyTracker.isCyclicDependencyPresent();
315 private static List<ErrorCode> validateCompletedVendorSoftwareProduct(
316 VspDetails vspDetails, UploadDataEntity uploadData, Object serviceModel) {
318 List<ErrorCode> errros = new ArrayList<>();
320 if (vspDetails.getName() == null) {
321 errros.add(createMissingMandatoryFieldError("name"));
323 if (vspDetails.getDescription() == null) {
324 errros.add(createMissingMandatoryFieldError("description"));
326 if (vspDetails.getVendorId() == null) {
327 errros.add(createMissingMandatoryFieldError("vendor Id"));
329 if (vspDetails.getVlmVersion() == null) {
330 errros.add(createMissingMandatoryFieldError(
331 "licensing version (in the format of: {integer}.{integer})"));
333 if (vspDetails.getCategory() == null) {
334 errros.add(createMissingMandatoryFieldError("category"));
336 if (vspDetails.getSubCategory() == null) {
337 errros.add(createMissingMandatoryFieldError("sub category"));
339 if (vspDetails.getLicenseAgreement() == null) {
340 errros.add(createMissingMandatoryFieldError("license agreement"));
342 if (CollectionUtils.isEmpty(vspDetails.getFeatureGroups())) {
343 errros.add(createMissingMandatoryFieldError("feature groups"));
345 if (uploadData == null || uploadData.getContentData() == null || serviceModel == null) {
347 new VendorSoftwareProductInvalidErrorBuilder(vspDetails.getId(), vspDetails.getVersion())
351 return errros.isEmpty() ? null : errros;
354 private static ErrorCode createMissingMandatoryFieldError(String fieldName) {
355 return new ValidationErrorBuilder("must be supplied", fieldName).build();
358 String getVspQuestionnaireSchema(SchemaTemplateInput schemaInput) {
359 mdcDataDebugMessage.debugEntryMessage(null);
360 mdcDataDebugMessage.debugExitMessage(null);
361 return SchemaGenerator
362 .generate(SchemaTemplateContext.questionnaire, CompositionEntityType.vsp, schemaInput);
365 private static void sortVspListByModificationTimeDescOrder(
366 List<VersionedVendorSoftwareProductInfo> vsps) {
367 vsps.sort((o1, o2) -> o2.getVspDetails().getWritetimeMicroSeconds()
368 .compareTo(o1.getVspDetails().getWritetimeMicroSeconds()));
372 private Map<String, List<ErrorMessage>> compile(String vendorSoftwareProductId, Version version,
373 ToscaServiceModel serviceModel) {
374 if (serviceModel == null) {
378 enrichedServiceModelDao.deleteAll(vendorSoftwareProductId, version);
380 EnrichmentManager<ToscaServiceModel> enrichmentManager =
381 EnrichmentManagerFactory.getInstance().createInterface();
382 enrichmentManager.init(vendorSoftwareProductId, version);
383 enrichmentManager.setModel(serviceModel);
384 Map<String, List<ErrorMessage>> enrichErrors = enrichmentManager.enrich();
386 enrichedServiceModelDao
387 .storeServiceModel(vendorSoftwareProductId, version, enrichmentManager.getModel());
392 private Collection<ErrorCode> validateLicensingData(VspDetails vspDetails) {
393 mdcDataDebugMessage.debugEntryMessage("VSP id", vspDetails.getId());
395 if (vspDetails.getVendorId() == null || vspDetails.getVlmVersion() == null
396 || vspDetails.getLicenseAgreement() == null
397 || CollectionUtils.isEmpty(vspDetails.getFeatureGroups())) {
401 mdcDataDebugMessage.debugExitMessage("VSP id", vspDetails.getId());
402 return vendorLicenseFacade
403 .validateLicensingData(vspDetails.getVendorId(), vspDetails.getVlmVersion(),
404 vspDetails.getLicenseAgreement(), vspDetails.getFeatureGroups());
408 public String fetchValidationVsp(String user) {
410 validateUniqueName(VALIDATION_VSP_NAME);
411 } catch (Exception ignored) {
412 return VALIDATION_VSP_ID;
414 VspDetails validationVsp = new VspDetails();
415 validationVsp.setName(VALIDATION_VSP_NAME);
417 vspInfoDao.create(validationVsp);
418 Version version = versioningManager.create(
419 VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE,
420 validationVsp.getId(), user);
421 validationVsp.setVersion(version);
423 createUniqueName(VALIDATION_VSP_NAME);
424 VALIDATION_VSP_ID = validationVsp.getId();
425 return VALIDATION_VSP_ID;
429 public VspDetails createVsp(VspDetails vspDetails, String user) {
430 mdcDataDebugMessage.debugEntryMessage(null);
432 validateUniqueName(vspDetails.getName());
434 vspInfoDao.create(vspDetails);//id will be set in the dao
435 vspInfoDao.updateQuestionnaireData(vspDetails.getId(), null,
436 new JsonSchemaDataGenerator(getVspQuestionnaireSchema(null)).generateData());
438 Version version = versioningManager
439 .create(VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE,
440 vspDetails.getId(), user);
441 vspDetails.setVersion(version);
442 ActivityLogEntity activityLogEntity = new ActivityLogEntity(vspDetails.getId(), String
443 .valueOf(vspDetails.getVersion().getMajor() + 1),
444 ActivityType.CREATE_NEW.toString(), user, true, "", "");
445 activityLogManager.addActionLog(activityLogEntity, user);
446 String vspName = vspDetails.getName();
447 createUniqueName(vspName);
448 mdcDataDebugMessage.debugExitMessage(null);
453 public List<VersionedVendorSoftwareProductInfo> listVsps(String versionFilter, String user) {
454 mdcDataDebugMessage.debugEntryMessage(null);
456 Map<String, VersionInfo> idToVersionsInfo = versioningManager.listEntitiesVersionInfo(
457 VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE, user,
458 VersionableEntityAction.Read);
460 List<VersionedVendorSoftwareProductInfo> vsps = new ArrayList<>();
461 for (Map.Entry<String, VersionInfo> entry : idToVersionsInfo.entrySet()) {
462 VersionInfo versionInfo = entry.getValue();
463 if (versionFilter != null && versionFilter.equals(VersionStatus.Final.name())) {
464 if (versionInfo.getLatestFinalVersion() == null) {
467 versionInfo.setActiveVersion(versionInfo.getLatestFinalVersion());
468 versionInfo.setStatus(VersionStatus.Final);
469 versionInfo.setLockingUser(null);
472 Version version = versionInfo.getActiveVersion();
473 if (user.equals(versionInfo.getLockingUser())) {
474 version.setStatus(VersionStatus.Locked);
477 VspDetails vsp = vspInfoDao.get(new VspDetails(entry.getKey(), version));
478 if (vsp != null && !vsp.getId().equals(VALIDATION_VSP_ID)) {
479 vsp.setValidationDataStructure(null);
480 vsps.add(new VersionedVendorSoftwareProductInfo(vsp, versionInfo));
482 }catch(RuntimeException rte){
483 logger.error("Error trying to retrieve vsp["+entry.getKey()+"] version["+version.toString
490 sortVspListByModificationTimeDescOrder(vsps);
492 mdcDataDebugMessage.debugExitMessage(null);
498 public void updateVsp(VspDetails vspDetails, String user) {
499 mdcDataDebugMessage.debugEntryMessage("VSP id", vspDetails.getId());
501 VspDetails retrieved = vspInfoDao.get(vspDetails);
503 updateUniqueName(retrieved.getName(), vspDetails.getName());
504 vspDetails.setOldVersion(retrieved.getOldVersion());
506 vspInfoDao.update(vspDetails);
507 //vendorSoftwareProductDao.updateVspLatestModificationTime(vspDetails.getId(), activeVersion);
509 mdcDataDebugMessage.debugExitMessage("VSP id", vspDetails.getId());
514 public VspDetails getVsp(String vspId, Version version, String user) {
515 mdcDataDebugMessage.debugEntryMessage("VSP id", vspId);
517 VspDetails vsp = vspInfoDao.get(new VspDetails(vspId, version));
519 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
520 LoggerTragetServiceName.GET_VSP, ErrorLevel.ERROR.name(),
521 LoggerErrorCode.DATA_ERROR.getErrorCode(), "Requested VSP not found");
522 throw new CoreException(new VendorSoftwareProductNotFoundErrorBuilder(vspId).build());
524 vsp.setValidationData(orchestrationTemplateDao.getValidationData(vspId, version));
526 mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
531 public Version callAutoHeal(String vspId, VersionInfo versionInfo,
532 VspDetails vendorSoftwareProductInfo, String user)
534 switch (versionInfo.getStatus()) {
536 if (user.equals(versionInfo.getLockingUser())) {
537 autoHeal(vspId, versionInfo.getActiveVersion(), vendorSoftwareProductInfo,
538 versionInfo.getLockingUser());
540 return versionInfo.getActiveVersion();
542 Version checkoutVersion = checkout(vspId, user);
543 autoHeal(vspId, checkoutVersion, vendorSoftwareProductInfo, user);
544 return checkin(vspId, user);
546 Version checkoutFinalVersion = checkout(vspId,user);
547 autoHeal(vspId, checkoutFinalVersion, vendorSoftwareProductInfo, user);
548 Version checkinFinalVersion = checkin(vspId,user);
549 ValidationResponse response = submit(vspId, user);
550 if(!response.isValid()) {
551 return checkout(vspId, user);
555 Version finalVersion = checkinFinalVersion.calculateNextFinal();
556 createPackage(vspId, finalVersion, user);
558 } catch (IOException e) {
559 throw new Exception(e.getMessage());
562 return versionInfo.getActiveVersion();
567 public void deleteVsp(String vspId, String user) {
568 mdcDataDebugMessage.debugEntryMessage("VSP id", vspId);
570 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
571 LoggerTragetServiceName.DELETE_VSP, ErrorLevel.ERROR.name(),
572 LoggerErrorCode.PERMISSION_ERROR.getErrorCode(), "Unsupported operation");
573 mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
575 throw new UnsupportedOperationException(
576 VendorSoftwareProductConstants.UNSUPPORTED_OPERATION_ERROR);
580 public void heal(String vspId, Version version, String user) {
581 mdcDataDebugMessage.debugEntryMessage("VSP id", vspId);
583 VersionInfo versionInfo = getVersionInfo(vspId, VersionableEntityAction.Read, user);
585 version = VersionStatus.Locked.equals(versionInfo.getStatus())
586 ? versionInfo.getActiveVersion()
587 : checkout(vspId, user);
588 version.setStatus(VersionStatus.Locked);
590 healingManager.healAll(getHealingParamsAsMap(vspId, version, user));
592 VspDetails vspDetails = new VspDetails(vspId, version);
593 vspDetails.setOldVersion(null);
594 vspInfoDao.updateOldVersionIndication(vspDetails);
596 logger.audit("Healed VSP " + vspDetails.getId());
597 mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
600 private void autoHeal(String vspId, Version checkoutVersion, VspDetails vspDetails, String user) {
601 mdcDataDebugMessage.debugEntryMessage("VSP id", vspId);
603 checkoutVersion.setStatus(VersionStatus.Locked);
604 Map<String, Object> healingParams = getHealingParamsAsMap(vspId, checkoutVersion, user);
605 healingManager.healAll(healingParams);
606 vspDetails.setVersion(checkoutVersion);
607 vspDetails.setOldVersion(null);
608 vspInfoDao.updateOldVersionIndication(vspDetails);
610 logger.audit("Healed VSP " + vspDetails.getName());
612 mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
615 private Map<String, Object> getHealingParamsAsMap(String vspId, Version version, String user) {
616 Map<String, Object> healingParams = new HashMap<>();
618 healingParams.put(SdcCommon.VSP_ID, vspId);
619 healingParams.put(SdcCommon.VERSION, version);
620 healingParams.put(SdcCommon.USER, user);
622 return healingParams;
626 public List<PackageInfo> listPackages(String category, String subCategory) {
627 return packageInfoDao.listByCategory(category, subCategory);
631 public File getTranslatedFile(String vspId, Version version, String user) {
632 mdcDataDebugMessage.debugEntryMessage("VSP id", vspId);
634 if (version == null) {
635 errorMessage = "Package not found";
636 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
637 LoggerTragetServiceName.GET_TRANSLATED_FILE, ErrorLevel.ERROR.name(),
638 LoggerErrorCode.DATA_ERROR.getErrorCode(), errorMessage);
639 throw new CoreException(new PackageNotFoundErrorBuilder(vspId).build());
640 } else if (!version.isFinal()) {
641 errorMessage = "Invalid requested version";
642 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
643 LoggerTragetServiceName.GET_VERSION_INFO, ErrorLevel.ERROR.name(),
644 LoggerErrorCode.DATA_ERROR.getErrorCode(), errorMessage);
645 throw new CoreException(new RequestedVersionInvalidErrorBuilder().build());
648 PackageInfo packageInfo =
649 packageInfoDao.get(new PackageInfo(vspId, version));
650 if (packageInfo == null) {
651 errorMessage = "Package not found";
652 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
653 LoggerTragetServiceName.GET_TRANSLATED_FILE, ErrorLevel.ERROR.name(),
654 LoggerErrorCode.DATA_ERROR.getErrorCode(), errorMessage);
655 throw new CoreException(new PackageNotFoundErrorBuilder(vspId, version).build());
658 ByteBuffer translatedFileBuffer = packageInfo.getTranslatedFile();
659 if (translatedFileBuffer == null) {
660 errorMessage = "Package not found";
661 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
662 LoggerTragetServiceName.GET_TRANSLATED_FILE, ErrorLevel.ERROR.name(),
663 LoggerErrorCode.DATA_ERROR.getErrorCode(), errorMessage);
664 throw new CoreException(new PackageInvalidErrorBuilder(vspId, version).build());
667 File translatedFile = new File(VendorSoftwareProductConstants.VSP_PACKAGE_ZIP);
670 FileOutputStream fos = new FileOutputStream(translatedFile);
671 fos.write(translatedFileBuffer.array());
673 } catch (IOException exception) {
674 errorMessage = "Can't create package";
675 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
676 LoggerTragetServiceName.CREATE_TRANSLATED_FILE, ErrorLevel.ERROR.name(),
677 LoggerErrorCode.DATA_ERROR.getErrorCode(), errorMessage);
678 throw new CoreException(new TranslationFileCreationErrorBuilder(vspId, version).build(),
682 mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
684 return translatedFile;
689 public byte[] getOrchestrationTemplateFile(String vspId, Version version, String user) {
690 mdcDataDebugMessage.debugEntryMessage("VSP id", vspId);
692 UploadDataEntity uploadData = orchestrationTemplateDao.getOrchestrationTemplate(vspId, version);
693 ByteBuffer contentData = uploadData.getContentData();
694 if (contentData == null) {
698 ByteArrayOutputStream baos = new ByteArrayOutputStream();
700 try (final ZipOutputStream zos = new ZipOutputStream(baos);
701 ZipInputStream zipStream = new ZipInputStream(
702 new ByteArrayInputStream(contentData.array()))) {
703 zos.write(contentData.array());
704 } catch (IOException exception) {
705 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
706 LoggerTragetServiceName.GET_UPLOADED_HEAT, ErrorLevel.ERROR.name(),
707 LoggerErrorCode.DATA_ERROR.getErrorCode(), "Can't get uploaded HEAT");
708 throw new CoreException(new FileCreationErrorBuilder(vspId).build(), exception);
711 mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
712 return baos.toByteArray();
716 public PackageInfo createPackage(String vspId, Version version, String user) throws IOException {
717 mdcDataDebugMessage.debugEntryMessage("VSP id", vspId);
719 if (!version.isFinal()) {
720 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
721 LoggerTragetServiceName.CREATE_PACKAGE, ErrorLevel.ERROR.name(),
722 LoggerErrorCode.PERMISSION_ERROR.getErrorCode(), "Can't create package");
723 throw new CoreException(
724 new CreatePackageForNonFinalVendorSoftwareProductErrorBuilder(vspId, version)
728 ToscaServiceModel toscaServiceModel = enrichedServiceModelDao.getServiceModel(vspId, version);
729 VspDetails vspDetails = vspInfoDao.get(new VspDetails(vspId, version));
730 Version vlmVersion = vspDetails.getVlmVersion();
732 PackageInfo packageInfo = createPackageInfo(vspId, vspDetails);
734 ToscaFileOutputServiceCsarImpl toscaServiceTemplateServiceCsar =
735 new ToscaFileOutputServiceCsarImpl();
736 FileContentHandler licenseArtifacts = licenseArtifactsService
737 .createLicenseArtifacts(vspDetails.getId(), vspDetails.getVendorId(), vlmVersion,
738 vspDetails.getFeatureGroups(), user);
739 //todo add tosca validation here
740 packageInfo.setTranslatedFile(ByteBuffer.wrap(
741 toscaServiceTemplateServiceCsar.createOutputFile(toscaServiceModel, licenseArtifacts)));
743 packageInfoDao.create(packageInfo);
745 mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
749 private PackageInfo createPackageInfo(String vspId, VspDetails vspDetails) {
750 PackageInfo packageInfo = new PackageInfo();
751 packageInfo.setVspId(vspId);
752 packageInfo.setVersion(vspDetails.getVersion());
753 packageInfo.setVspName(vspDetails.getName());
754 packageInfo.setVspDescription(vspDetails.getDescription());
755 packageInfo.setCategory(vspDetails.getCategory());
756 packageInfo.setSubCategory(vspDetails.getSubCategory());
757 packageInfo.setVendorName(vspDetails.getVendorName());
758 packageInfo.setPackageType(VendorSoftwareProductConstants.CSAR);
759 packageInfo.setVendorRelease("1.0"); //todo TBD
765 public QuestionnaireResponse getVspQuestionnaire(String vspId, Version version, String user) {
766 mdcDataDebugMessage.debugEntryMessage("VSP id", vspId);
768 VspQuestionnaireEntity retrieved = vspInfoDao.getQuestionnaire(vspId, version);
769 VersioningUtil.validateEntityExistence(retrieved, new VspQuestionnaireEntity(vspId, version),
770 VspDetails.ENTITY_TYPE);
772 String questionnaireData = retrieved.getQuestionnaireData();
774 QuestionnaireResponse questionnaireResponse = new QuestionnaireResponse();
775 questionnaireResponse.setData(questionnaireData);
776 questionnaireResponse.setSchema(getVspQuestionnaireSchema(null));
778 mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
780 return questionnaireResponse;
784 public void updateVspQuestionnaire(String vspId, Version version, String questionnaireData,
786 mdcDataDebugMessage.debugEntryMessage("VSP id", vspId);
788 vspInfoDao.updateQuestionnaireData(vspId, version, questionnaireData);
790 mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
794 private Map<String, List<ErrorMessage>> validateUploadData(UploadDataEntity uploadData)
797 Map<String, List<ErrorMessage>> validationErrors = new HashMap<>();
798 if (uploadData == null || uploadData.getContentData() == null) {
802 FileContentHandler fileContentMap = new FileContentHandler();
806 CommonUtil.loadUploadFileContent(uploadData.getContentData().array());
807 } catch (Exception e){
808 ErrorMessage errorMessage = new ErrorMessage(ErrorLevel.ERROR, e.getMessage());
809 validationErrors.put("Upload file", Arrays.asList(errorMessage));
811 ValidationManager validationManager =
812 ValidationManagerUtil.initValidationManager(fileContentMap);
813 validationErrors.putAll(validationManager.validate());
816 MapUtils.isEmpty(MessageContainerUtil.getMessageByLevel(ErrorLevel.ERROR, validationErrors))
817 ? null : validationErrors;
820 private VersionInfo getVersionInfo(String vendorSoftwareProductId, VersionableEntityAction action,
822 return versioningManager.getEntityVersionInfo(
823 VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE,
824 vendorSoftwareProductId, user, action);
828 private QuestionnaireValidationResult validateQuestionnaire(String vspId, Version version) {
829 mdcDataDebugMessage.debugEntryMessage("VSP id", vspId);
832 compositionEntityDataManager
833 .addEntity(vspInfoDao.getQuestionnaire(vspId, version), null);
835 Collection<NicEntity> nics = vendorSoftwareProductDao.listNicsByVsp(vspId, version);
837 Map<String, List<String>> nicNamesByComponent = new HashMap<>();
838 for (NicEntity nicEntity : nics) {
839 compositionEntityDataManager.addEntity(nicEntity, null);
841 Nic nic = nicEntity.getNicCompositionData();
842 if (nic != null && nic.getName() != null) {
843 List<String> nicNames =
844 nicNamesByComponent.computeIfAbsent(nicEntity.getComponentId(), k -> new ArrayList<>());
845 nicNames.add(nic.getName());
849 Collection<ComponentEntity> components =
850 vendorSoftwareProductDao.listComponentsCompositionAndQuestionnaire(vspId, version);
851 components.forEach(component -> compositionEntityDataManager.addEntity(component,
852 new ComponentQuestionnaireSchemaInput(nicNamesByComponent.get(component.getId()),
853 JsonUtil.json2Object(component.getQuestionnaireData(), Map.class))));
855 Map<CompositionEntityId, Collection<String>> errorsByEntityId =
856 compositionEntityDataManager.validateEntitiesQuestionnaire();
857 if (MapUtils.isNotEmpty(errorsByEntityId)) {
858 compositionEntityDataManager.buildTrees();
859 compositionEntityDataManager.addErrorsToTrees(errorsByEntityId);
860 /* Set<CompositionEntityValidationData> entitiesWithValidationErrors =
861 compositionEntityDataManager.getEntityListWithErrors();*/
862 //Collection<CompositionEntityValidationData> roots = compositionEntityDataManager.getTrees();
864 mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
865 return new QuestionnaireValidationResult(
866 compositionEntityDataManager.getAllErrorsByVsp(vspId));
869 mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
874 public File getInformationArtifact(String vspId, Version version, String user) {
875 mdcDataDebugMessage.debugEntryMessage("VSP id", vspId);
876 VspDetails vspDetails = vspInfoDao.get(new VspDetails(vspId, version));
878 if (vspDetails == null) {
882 String vspName = vspDetails.getName();
883 ByteBuffer infoArtifactAsByteBuffer;
884 File infoArtifactFile;
886 infoArtifactAsByteBuffer = ByteBuffer.wrap(informationArtifactGenerator.generate(vspId,
887 version).getBytes());
891 String.format(VendorSoftwareProductConstants.INFORMATION_ARTIFACT_NAME, vspName));
892 OutputStream out = new BufferedOutputStream(new FileOutputStream(infoArtifactFile));
893 out.write(infoArtifactAsByteBuffer.array());
895 } catch (IOException e) {
896 throw new CoreException(new InformationArtifactCreationErrorBuilder(vspId).build(), e);
899 mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
900 return infoArtifactFile;
903 void validateUniqueName(String vspName) {
904 UniqueValueUtil.validateUniqueValue(
905 VendorSoftwareProductConstants.UniqueValues.VENDOR_SOFTWARE_PRODUCT_NAME, vspName);
908 void createUniqueName(String vspName) {
909 UniqueValueUtil.createUniqueValue(
910 VendorSoftwareProductConstants.UniqueValues.VENDOR_SOFTWARE_PRODUCT_NAME, vspName);
913 void updateUniqueName(String oldVspName, String newVspName) {
914 UniqueValueUtil.updateUniqueValue(
915 VendorSoftwareProductConstants.UniqueValues.VENDOR_SOFTWARE_PRODUCT_NAME,
916 oldVspName, newVspName);