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);
476 VspDetails vsp = vspInfoDao.get(new VspDetails(entry.getKey(), version));
477 if (vsp != null && !vsp.getId().equals(VALIDATION_VSP_ID)) {
478 vsp.setValidationDataStructure(null);
479 vsps.add(new VersionedVendorSoftwareProductInfo(vsp, versionInfo));
481 }catch(RuntimeException rte){
482 logger.error("Error trying to retrieve vsp["+entry.getKey()+"] version["+version.toString
489 sortVspListByModificationTimeDescOrder(vsps);
491 mdcDataDebugMessage.debugExitMessage(null);
497 public void updateVsp(VspDetails vspDetails, String user) {
498 mdcDataDebugMessage.debugEntryMessage("VSP id", vspDetails.getId());
500 VspDetails retrieved = vspInfoDao.get(vspDetails);
502 updateUniqueName(retrieved.getName(), vspDetails.getName());
503 vspDetails.setOldVersion(retrieved.getOldVersion());
505 vspInfoDao.update(vspDetails);
506 //vendorSoftwareProductDao.updateVspLatestModificationTime(vspDetails.getId(), activeVersion);
508 mdcDataDebugMessage.debugExitMessage("VSP id", vspDetails.getId());
513 public VspDetails getVsp(String vspId, Version version, String user) {
514 mdcDataDebugMessage.debugEntryMessage("VSP id", vspId);
516 VspDetails vsp = vspInfoDao.get(new VspDetails(vspId, version));
518 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
519 LoggerTragetServiceName.GET_VSP, ErrorLevel.ERROR.name(),
520 LoggerErrorCode.DATA_ERROR.getErrorCode(), "Requested VSP not found");
521 throw new CoreException(new VendorSoftwareProductNotFoundErrorBuilder(vspId).build());
523 vsp.setValidationData(orchestrationTemplateDao.getValidationData(vspId, version));
525 mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
530 public Version callAutoHeal(String vspId, VersionInfo versionInfo,
531 VspDetails vendorSoftwareProductInfo, String user)
533 switch (versionInfo.getStatus()) {
535 if (user.equals(versionInfo.getLockingUser())) {
536 autoHeal(vspId, versionInfo.getActiveVersion(), vendorSoftwareProductInfo,
537 versionInfo.getLockingUser());
539 return versionInfo.getActiveVersion();
541 Version checkoutVersion = checkout(vspId, user);
542 autoHeal(vspId, checkoutVersion, vendorSoftwareProductInfo, user);
543 return checkin(vspId, user);
545 Version checkoutFinalVersion = checkout(vspId,user);
546 autoHeal(vspId, checkoutFinalVersion, vendorSoftwareProductInfo, user);
547 Version checkinFinalVersion = checkin(vspId,user);
548 ValidationResponse response = submit(vspId, user);
549 if(!response.isValid()) {
550 return checkout(vspId, user);
554 Version finalVersion = checkinFinalVersion.calculateNextFinal();
555 createPackage(vspId, finalVersion, user);
557 } catch (IOException e) {
558 throw new Exception(e.getMessage());
561 return versionInfo.getActiveVersion();
566 public void deleteVsp(String vspId, String user) {
567 mdcDataDebugMessage.debugEntryMessage("VSP id", vspId);
569 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
570 LoggerTragetServiceName.DELETE_VSP, ErrorLevel.ERROR.name(),
571 LoggerErrorCode.PERMISSION_ERROR.getErrorCode(), "Unsupported operation");
572 mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
574 throw new UnsupportedOperationException(
575 VendorSoftwareProductConstants.UNSUPPORTED_OPERATION_ERROR);
579 public void heal(String vspId, Version version, String user) {
580 mdcDataDebugMessage.debugEntryMessage("VSP id", vspId);
582 VersionInfo versionInfo = getVersionInfo(vspId, VersionableEntityAction.Read, user);
584 version = VersionStatus.Locked.equals(versionInfo.getStatus())
585 ? versionInfo.getActiveVersion()
586 : checkout(vspId, user);
587 version.setStatus(VersionStatus.Locked);
589 healingManager.healAll(getHealingParamsAsMap(vspId, version, user));
591 VspDetails vspDetails = new VspDetails(vspId, version);
592 vspDetails.setOldVersion(null);
593 vspInfoDao.updateOldVersionIndication(vspDetails);
595 logger.audit("Healed VSP " + vspDetails.getId());
596 mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
599 private void autoHeal(String vspId, Version checkoutVersion, VspDetails vspDetails, String user) {
600 mdcDataDebugMessage.debugEntryMessage("VSP id", vspId);
602 checkoutVersion.setStatus(VersionStatus.Locked);
603 Map<String, Object> healingParams = getHealingParamsAsMap(vspId, checkoutVersion, user);
604 healingManager.healAll(healingParams);
605 vspDetails.setVersion(checkoutVersion);
606 vspDetails.setOldVersion(null);
607 vspInfoDao.updateOldVersionIndication(vspDetails);
609 logger.audit("Healed VSP " + vspDetails.getName());
611 mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
614 private Map<String, Object> getHealingParamsAsMap(String vspId, Version version, String user) {
615 Map<String, Object> healingParams = new HashMap<>();
617 healingParams.put(SdcCommon.VSP_ID, vspId);
618 healingParams.put(SdcCommon.VERSION, version);
619 healingParams.put(SdcCommon.USER, user);
621 return healingParams;
625 public List<PackageInfo> listPackages(String category, String subCategory) {
626 return packageInfoDao.listByCategory(category, subCategory);
630 public File getTranslatedFile(String vspId, Version version, String user) {
631 mdcDataDebugMessage.debugEntryMessage("VSP id", vspId);
633 if (version == null) {
634 errorMessage = "Package not found";
635 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
636 LoggerTragetServiceName.GET_TRANSLATED_FILE, ErrorLevel.ERROR.name(),
637 LoggerErrorCode.DATA_ERROR.getErrorCode(), errorMessage);
638 throw new CoreException(new PackageNotFoundErrorBuilder(vspId).build());
639 } else if (!version.isFinal()) {
640 errorMessage = "Invalid requested version";
641 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
642 LoggerTragetServiceName.GET_VERSION_INFO, ErrorLevel.ERROR.name(),
643 LoggerErrorCode.DATA_ERROR.getErrorCode(), errorMessage);
644 throw new CoreException(new RequestedVersionInvalidErrorBuilder().build());
647 PackageInfo packageInfo =
648 packageInfoDao.get(new PackageInfo(vspId, version));
649 if (packageInfo == null) {
650 errorMessage = "Package not found";
651 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
652 LoggerTragetServiceName.GET_TRANSLATED_FILE, ErrorLevel.ERROR.name(),
653 LoggerErrorCode.DATA_ERROR.getErrorCode(), errorMessage);
654 throw new CoreException(new PackageNotFoundErrorBuilder(vspId, version).build());
657 ByteBuffer translatedFileBuffer = packageInfo.getTranslatedFile();
658 if (translatedFileBuffer == null) {
659 errorMessage = "Package not found";
660 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
661 LoggerTragetServiceName.GET_TRANSLATED_FILE, ErrorLevel.ERROR.name(),
662 LoggerErrorCode.DATA_ERROR.getErrorCode(), errorMessage);
663 throw new CoreException(new PackageInvalidErrorBuilder(vspId, version).build());
666 File translatedFile = new File(VendorSoftwareProductConstants.VSP_PACKAGE_ZIP);
669 FileOutputStream fos = new FileOutputStream(translatedFile);
670 fos.write(translatedFileBuffer.array());
672 } catch (IOException exception) {
673 errorMessage = "Can't create package";
674 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
675 LoggerTragetServiceName.CREATE_TRANSLATED_FILE, ErrorLevel.ERROR.name(),
676 LoggerErrorCode.DATA_ERROR.getErrorCode(), errorMessage);
677 throw new CoreException(new TranslationFileCreationErrorBuilder(vspId, version).build(),
681 mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
683 return translatedFile;
688 public byte[] getOrchestrationTemplateFile(String vspId, Version version, String user) {
689 mdcDataDebugMessage.debugEntryMessage("VSP id", vspId);
691 UploadDataEntity uploadData = orchestrationTemplateDao.getOrchestrationTemplate(vspId, version);
692 ByteBuffer contentData = uploadData.getContentData();
693 if (contentData == null) {
697 ByteArrayOutputStream baos = new ByteArrayOutputStream();
699 try (final ZipOutputStream zos = new ZipOutputStream(baos);
700 ZipInputStream zipStream = new ZipInputStream(
701 new ByteArrayInputStream(contentData.array()))) {
702 zos.write(contentData.array());
703 } catch (IOException exception) {
704 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
705 LoggerTragetServiceName.GET_UPLOADED_HEAT, ErrorLevel.ERROR.name(),
706 LoggerErrorCode.DATA_ERROR.getErrorCode(), "Can't get uploaded HEAT");
707 throw new CoreException(new FileCreationErrorBuilder(vspId).build(), exception);
710 mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
711 return baos.toByteArray();
715 public PackageInfo createPackage(String vspId, Version version, String user) throws IOException {
716 mdcDataDebugMessage.debugEntryMessage("VSP id", vspId);
718 if (!version.isFinal()) {
719 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
720 LoggerTragetServiceName.CREATE_PACKAGE, ErrorLevel.ERROR.name(),
721 LoggerErrorCode.PERMISSION_ERROR.getErrorCode(), "Can't create package");
722 throw new CoreException(
723 new CreatePackageForNonFinalVendorSoftwareProductErrorBuilder(vspId, version)
727 ToscaServiceModel toscaServiceModel = enrichedServiceModelDao.getServiceModel(vspId, version);
728 VspDetails vspDetails = vspInfoDao.get(new VspDetails(vspId, version));
729 Version vlmVersion = vspDetails.getVlmVersion();
731 PackageInfo packageInfo = createPackageInfo(vspId, vspDetails);
733 ToscaFileOutputServiceCsarImpl toscaServiceTemplateServiceCsar =
734 new ToscaFileOutputServiceCsarImpl();
735 FileContentHandler licenseArtifacts = licenseArtifactsService
736 .createLicenseArtifacts(vspDetails.getId(), vspDetails.getVendorId(), vlmVersion,
737 vspDetails.getFeatureGroups(), user);
738 //todo add tosca validation here
739 packageInfo.setTranslatedFile(ByteBuffer.wrap(
740 toscaServiceTemplateServiceCsar.createOutputFile(toscaServiceModel, licenseArtifacts)));
742 packageInfoDao.create(packageInfo);
744 mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
748 private PackageInfo createPackageInfo(String vspId, VspDetails vspDetails) {
749 PackageInfo packageInfo = new PackageInfo();
750 packageInfo.setVspId(vspId);
751 packageInfo.setVersion(vspDetails.getVersion());
752 packageInfo.setVspName(vspDetails.getName());
753 packageInfo.setVspDescription(vspDetails.getDescription());
754 packageInfo.setCategory(vspDetails.getCategory());
755 packageInfo.setSubCategory(vspDetails.getSubCategory());
756 packageInfo.setVendorName(vspDetails.getVendorName());
757 packageInfo.setPackageType(VendorSoftwareProductConstants.CSAR);
758 packageInfo.setVendorRelease("1.0"); //todo TBD
764 public QuestionnaireResponse getVspQuestionnaire(String vspId, Version version, String user) {
765 mdcDataDebugMessage.debugEntryMessage("VSP id", vspId);
767 VspQuestionnaireEntity retrieved = vspInfoDao.getQuestionnaire(vspId, version);
768 VersioningUtil.validateEntityExistence(retrieved, new VspQuestionnaireEntity(vspId, version),
769 VspDetails.ENTITY_TYPE);
771 String questionnaireData = retrieved.getQuestionnaireData();
773 QuestionnaireResponse questionnaireResponse = new QuestionnaireResponse();
774 questionnaireResponse.setData(questionnaireData);
775 questionnaireResponse.setSchema(getVspQuestionnaireSchema(null));
777 mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
779 return questionnaireResponse;
783 public void updateVspQuestionnaire(String vspId, Version version, String questionnaireData,
785 mdcDataDebugMessage.debugEntryMessage("VSP id", vspId);
787 vspInfoDao.updateQuestionnaireData(vspId, version, questionnaireData);
789 mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
793 private Map<String, List<ErrorMessage>> validateUploadData(UploadDataEntity uploadData)
795 if (uploadData == null || uploadData.getContentData() == null) {
799 FileContentHandler fileContentMap =
800 CommonUtil.loadUploadFileContent(uploadData.getContentData().array());
802 ValidationManager validationManager =
803 ValidationManagerUtil.initValidationManager(fileContentMap);
804 Map<String, List<ErrorMessage>> validationErrors = validationManager.validate();
807 MapUtils.isEmpty(MessageContainerUtil.getMessageByLevel(ErrorLevel.ERROR, validationErrors))
808 ? null : validationErrors;
811 private VersionInfo getVersionInfo(String vendorSoftwareProductId, VersionableEntityAction action,
813 return versioningManager.getEntityVersionInfo(
814 VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE,
815 vendorSoftwareProductId, user, action);
819 private QuestionnaireValidationResult validateQuestionnaire(String vspId, Version version) {
820 mdcDataDebugMessage.debugEntryMessage("VSP id", vspId);
823 compositionEntityDataManager
824 .addEntity(vspInfoDao.getQuestionnaire(vspId, version), null);
826 Collection<NicEntity> nics = vendorSoftwareProductDao.listNicsByVsp(vspId, version);
828 Map<String, List<String>> nicNamesByComponent = new HashMap<>();
829 for (NicEntity nicEntity : nics) {
830 compositionEntityDataManager.addEntity(nicEntity, null);
832 Nic nic = nicEntity.getNicCompositionData();
833 if (nic != null && nic.getName() != null) {
834 List<String> nicNames =
835 nicNamesByComponent.computeIfAbsent(nicEntity.getComponentId(), k -> new ArrayList<>());
836 nicNames.add(nic.getName());
840 Collection<ComponentEntity> components =
841 vendorSoftwareProductDao.listComponentsCompositionAndQuestionnaire(vspId, version);
842 components.forEach(component -> compositionEntityDataManager.addEntity(component,
843 new ComponentQuestionnaireSchemaInput(nicNamesByComponent.get(component.getId()),
844 JsonUtil.json2Object(component.getQuestionnaireData(), Map.class))));
846 Map<CompositionEntityId, Collection<String>> errorsByEntityId =
847 compositionEntityDataManager.validateEntitiesQuestionnaire();
848 if (MapUtils.isNotEmpty(errorsByEntityId)) {
849 compositionEntityDataManager.buildTrees();
850 compositionEntityDataManager.addErrorsToTrees(errorsByEntityId);
851 /* Set<CompositionEntityValidationData> entitiesWithValidationErrors =
852 compositionEntityDataManager.getEntityListWithErrors();*/
853 //Collection<CompositionEntityValidationData> roots = compositionEntityDataManager.getTrees();
855 mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
856 return new QuestionnaireValidationResult(
857 compositionEntityDataManager.getAllErrorsByVsp(vspId));
860 mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
865 public File getInformationArtifact(String vspId, Version version, String user) {
866 mdcDataDebugMessage.debugEntryMessage("VSP id", vspId);
867 VspDetails vspDetails = vspInfoDao.get(new VspDetails(vspId, version));
869 if (vspDetails == null) {
873 String vspName = vspDetails.getName();
874 ByteBuffer infoArtifactAsByteBuffer;
875 File infoArtifactFile;
877 infoArtifactAsByteBuffer = ByteBuffer.wrap(informationArtifactGenerator.generate(vspId,
878 version).getBytes());
882 String.format(VendorSoftwareProductConstants.INFORMATION_ARTIFACT_NAME, vspName));
883 OutputStream out = new BufferedOutputStream(new FileOutputStream(infoArtifactFile));
884 out.write(infoArtifactAsByteBuffer.array());
886 } catch (IOException e) {
887 throw new CoreException(new InformationArtifactCreationErrorBuilder(vspId).build(), e);
890 mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
891 return infoArtifactFile;
894 void validateUniqueName(String vspName) {
895 UniqueValueUtil.validateUniqueValue(
896 VendorSoftwareProductConstants.UniqueValues.VENDOR_SOFTWARE_PRODUCT_NAME, vspName);
899 void createUniqueName(String vspName) {
900 UniqueValueUtil.createUniqueValue(
901 VendorSoftwareProductConstants.UniqueValues.VENDOR_SOFTWARE_PRODUCT_NAME, vspName);
904 void updateUniqueName(String oldVspName, String newVspName) {
905 UniqueValueUtil.updateUniqueValue(
906 VendorSoftwareProductConstants.UniqueValues.VENDOR_SOFTWARE_PRODUCT_NAME,
907 oldVspName, newVspName);