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.factory.CompositionEntityDataManagerFactory;
82 import org.openecomp.sdc.vendorsoftwareproduct.informationArtifact.InformationArtifactGenerator;
83 import org.openecomp.sdc.vendorsoftwareproduct.services.composition.CompositionEntityDataManager;
84 import org.openecomp.sdc.vendorsoftwareproduct.services.schemagenerator.SchemaGenerator;
85 import org.openecomp.sdc.vendorsoftwareproduct.types.QuestionnaireResponse;
86 import org.openecomp.sdc.vendorsoftwareproduct.types.QuestionnaireValidationResult;
87 import org.openecomp.sdc.vendorsoftwareproduct.types.ValidationResponse;
88 import org.openecomp.sdc.vendorsoftwareproduct.types.VersionedVendorSoftwareProductInfo;
89 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityId;
90 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityType;
91 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.Nic;
92 import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.ComponentQuestionnaireSchemaInput;
93 import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.SchemaTemplateContext;
94 import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.SchemaTemplateInput;
95 import org.openecomp.sdc.vendorsoftwareproduct.utils.ComponentDependencyTracker;
96 import org.openecomp.sdc.versioning.VersioningManager;
97 import org.openecomp.sdc.versioning.VersioningUtil;
98 import org.openecomp.sdc.versioning.dao.types.Version;
99 import org.openecomp.sdc.versioning.dao.types.VersionStatus;
100 import org.openecomp.sdc.versioning.errors.RequestedVersionInvalidErrorBuilder;
101 import org.openecomp.sdc.versioning.types.VersionInfo;
102 import org.openecomp.sdc.versioning.types.VersionableEntityAction;
103 import org.openecomp.sdcrests.activitylog.types.ActivityType;
104 import org.slf4j.MDC;
106 import java.io.BufferedOutputStream;
107 import java.io.ByteArrayInputStream;
108 import java.io.ByteArrayOutputStream;
110 import java.io.FileOutputStream;
111 import java.io.IOException;
112 import java.io.OutputStream;
113 import java.nio.ByteBuffer;
114 import java.util.ArrayList;
115 import java.util.Arrays;
116 import java.util.Collection;
117 import java.util.HashMap;
118 import java.util.List;
119 import java.util.Map;
120 import java.util.zip.ZipInputStream;
121 import java.util.zip.ZipOutputStream;
123 public class VendorSoftwareProductManagerImpl implements VendorSoftwareProductManager {
124 private static String VALIDATION_VSP_ID = "validationOnlyVspId";
125 private static final String VALIDATION_VSP_NAME = "validationOnlyVspName";
126 //private static final String VALIDATION_VSP_USER = "validationOnlyVspUser";
128 private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
129 private static final Logger logger =
130 LoggerFactory.getLogger(VendorSoftwareProductManagerImpl.class);
132 private OrchestrationTemplateDao orchestrationTemplateDao;
133 private VendorSoftwareProductInfoDao vspInfoDao;
134 private VersioningManager versioningManager;
135 private VendorSoftwareProductDao vendorSoftwareProductDao;
136 private VendorLicenseFacade vendorLicenseFacade;
137 private ServiceModelDao<ToscaServiceModel, ServiceElement> serviceModelDao;
138 private EnrichedServiceModelDao<ToscaServiceModel, ServiceElement> enrichedServiceModelDao;
139 private HealingManager healingManager;
140 private VendorLicenseArtifactsService licenseArtifactsService;
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 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.informationArtifactGenerator = informationArtifactGenerator;
169 this.packageInfoDao = packageInfoDao;
170 this.activityLogManager = activityLogManager;
172 registerToVersioning();
175 private void registerToVersioning() {
176 vendorSoftwareProductDao.registerVersioning(
177 VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE);
178 serviceModelDao.registerVersioning(
179 VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE);
180 enrichedServiceModelDao.registerVersioning(
181 VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE);
185 public Version checkout(String vendorSoftwareProductId, String user) {
186 mdcDataDebugMessage.debugEntryMessage("VSP id", vendorSoftwareProductId);
187 MDC.put(LoggerConstants.SERVICE_NAME, LoggerServiceName.Checkout_Entity.toString());
189 Version newVersion = versioningManager
190 .checkout(VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE,
191 vendorSoftwareProductId, user);
193 if (newVersion != null) {
194 ActivityLogEntity activityLogEntity =
195 new ActivityLogEntity(vendorSoftwareProductId, String.valueOf(newVersion.getMajor() + 1),
196 ActivityType.CHECKOUT.toString(), user, true, "", "");
197 activityLogManager.addActionLog(activityLogEntity, user);
200 mdcDataDebugMessage.debugExitMessage("VSP id", vendorSoftwareProductId);
206 public Version undoCheckout(String vendorSoftwareProductId, String user) {
207 mdcDataDebugMessage.debugEntryMessage("VSP id", vendorSoftwareProductId);
210 getVersionInfo(vendorSoftwareProductId, VersionableEntityAction.Read, user)
212 String preVspName = vspInfoDao
213 .get(new VspDetails(vendorSoftwareProductId, version)).getName();
215 Version newVersion = versioningManager.undoCheckout(
216 VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE,
217 vendorSoftwareProductId, user);
219 String postVspName = vspInfoDao
220 .get(new VspDetails(vendorSoftwareProductId, newVersion))
223 updateUniqueName(preVspName, postVspName);
225 mdcDataDebugMessage.debugExitMessage("VSP id", vendorSoftwareProductId);
231 public Version checkin(String vendorSoftwareProductId, String user) {
232 mdcDataDebugMessage.debugEntryMessage("VSP id", vendorSoftwareProductId);
234 Version newVersion = versioningManager.checkin(
235 VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE,
236 vendorSoftwareProductId, user, null);
238 if (newVersion != null) {
239 ActivityLogEntity activityLogEntity =
240 new ActivityLogEntity(vendorSoftwareProductId, String.valueOf(newVersion.getMajor() + 1),
241 ActivityType.CHECKIN.toString(), user, true, "", "");
242 activityLogManager.addActionLog(activityLogEntity, user);
245 mdcDataDebugMessage.debugExitMessage("VSP id", vendorSoftwareProductId);
251 public ValidationResponse submit(String vspId, String user) throws IOException {
252 mdcDataDebugMessage.debugEntryMessage("VSP id", vspId);
254 Version version = getVersionInfo(vspId, VersionableEntityAction.Read, user).getActiveVersion();
255 VspDetails vspDetails = getVsp(vspId, version, user);
256 UploadDataEntity uploadData = orchestrationTemplateDao.getOrchestrationTemplate(vspId, version);
257 ToscaServiceModel serviceModel =
258 serviceModelDao.getServiceModel(vspId, vspDetails.getVersion());
260 ValidationResponse validationResponse = new ValidationResponse();
262 .setVspErrors(validateCompletedVendorSoftwareProduct(vspDetails, uploadData, serviceModel),
263 LoggerServiceName.Submit_VSP, LoggerTragetServiceName.SUBMIT_VSP);
265 if (isCyclicDependencyInComponents(vspId, vspDetails.getVersion())) {
266 Collection<ErrorCode> vspErrors = validationResponse.getVspErrors() == null
268 : validationResponse.getVspErrors();
269 vspErrors.add(ComponentDependencyModelErrorBuilder
270 .getcyclicDependencyComponentErrorBuilder());
271 validationResponse.setVspErrors(vspErrors, LoggerServiceName.Submit_VSP,
272 LoggerTragetServiceName.SUBMIT_VSP);
275 validationResponse.setLicensingDataErrors(validateLicensingData(vspDetails));
277 .setUploadDataErrors(validateUploadData(uploadData), LoggerServiceName.Submit_VSP,
278 LoggerTragetServiceName.SUBMIT_VSP);
279 validationResponse.setQuestionnaireValidationResult(
280 validateQuestionnaire(vspDetails.getId(), vspDetails.getVersion()));
282 validationResponse.setCompilationErrors(
283 compile(vspId, vspDetails.getVersion(), serviceModel),
284 LoggerServiceName.Submit_VSP, LoggerTragetServiceName.SUBMIT_VSP);
286 if (validationResponse.isValid()) {
287 Version newVersion = versioningManager.submit(
288 VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE,
290 ActivityLogEntity activityLogEntity = new ActivityLogEntity(vspDetails.getId(), String
291 .valueOf(newVersion.getMajor()),
292 ActivityType.SUBMIT.toString(), user, true, "", "");
293 activityLogManager.addActionLog(activityLogEntity, user);
296 mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
298 return validationResponse;
301 private boolean isCyclicDependencyInComponents(String vendorSoftwareProductId,
303 final Collection<ComponentDependencyModelEntity> componentDependencyModelEntities =
304 vendorSoftwareProductDao.listComponentDependencies(vendorSoftwareProductId, version);
305 ComponentDependencyTracker dependencyTracker = new ComponentDependencyTracker();
307 for (ComponentDependencyModelEntity entity : componentDependencyModelEntities) {
308 dependencyTracker.addDependency(entity.getSourceComponentId(), entity.getTargetComponentId());
310 return dependencyTracker.isCyclicDependencyPresent();
313 private static List<ErrorCode> validateCompletedVendorSoftwareProduct(
314 VspDetails vspDetails, UploadDataEntity uploadData, Object serviceModel) {
316 List<ErrorCode> errros = new ArrayList<>();
318 if (vspDetails.getName() == null) {
319 errros.add(createMissingMandatoryFieldError("name"));
321 if (vspDetails.getDescription() == null) {
322 errros.add(createMissingMandatoryFieldError("description"));
324 if (vspDetails.getVendorId() == null) {
325 errros.add(createMissingMandatoryFieldError("vendor Id"));
327 if (vspDetails.getVlmVersion() == null) {
328 errros.add(createMissingMandatoryFieldError(
329 "licensing version (in the format of: {integer}.{integer})"));
331 if (vspDetails.getCategory() == null) {
332 errros.add(createMissingMandatoryFieldError("category"));
334 if (vspDetails.getSubCategory() == null) {
335 errros.add(createMissingMandatoryFieldError("sub category"));
337 if (vspDetails.getLicenseAgreement() == null) {
338 errros.add(createMissingMandatoryFieldError("license agreement"));
340 if (CollectionUtils.isEmpty(vspDetails.getFeatureGroups())) {
341 errros.add(createMissingMandatoryFieldError("feature groups"));
343 if (uploadData == null || uploadData.getContentData() == null || serviceModel == null) {
345 new VendorSoftwareProductInvalidErrorBuilder(vspDetails.getId(), vspDetails.getVersion())
349 return errros.isEmpty() ? null : errros;
352 private static ErrorCode createMissingMandatoryFieldError(String fieldName) {
353 return new ValidationErrorBuilder("must be supplied", fieldName).build();
356 String getVspQuestionnaireSchema(SchemaTemplateInput schemaInput) {
357 mdcDataDebugMessage.debugEntryMessage(null);
358 mdcDataDebugMessage.debugExitMessage(null);
359 return SchemaGenerator
360 .generate(SchemaTemplateContext.questionnaire, CompositionEntityType.vsp, schemaInput);
363 private static void sortVspListByModificationTimeDescOrder(
364 List<VersionedVendorSoftwareProductInfo> vsps) {
365 vsps.sort((o1, o2) -> o2.getVspDetails().getWritetimeMicroSeconds()
366 .compareTo(o1.getVspDetails().getWritetimeMicroSeconds()));
370 private Map<String, List<ErrorMessage>> compile(String vendorSoftwareProductId, Version version,
371 ToscaServiceModel serviceModel) {
372 if (serviceModel == null) {
376 enrichedServiceModelDao.deleteAll(vendorSoftwareProductId, version);
378 EnrichmentManager<ToscaServiceModel> enrichmentManager =
379 EnrichmentManagerFactory.getInstance().createInterface();
380 enrichmentManager.init(vendorSoftwareProductId, version);
381 enrichmentManager.setModel(serviceModel);
382 Map<String, List<ErrorMessage>> enrichErrors = enrichmentManager.enrich();
384 enrichedServiceModelDao
385 .storeServiceModel(vendorSoftwareProductId, version, enrichmentManager.getModel());
390 private Collection<ErrorCode> validateLicensingData(VspDetails vspDetails) {
391 mdcDataDebugMessage.debugEntryMessage("VSP id", vspDetails.getId());
393 if (vspDetails.getVendorId() == null || vspDetails.getVlmVersion() == null
394 || vspDetails.getLicenseAgreement() == null
395 || CollectionUtils.isEmpty(vspDetails.getFeatureGroups())) {
399 mdcDataDebugMessage.debugExitMessage("VSP id", vspDetails.getId());
400 return vendorLicenseFacade
401 .validateLicensingData(vspDetails.getVendorId(), vspDetails.getVlmVersion(),
402 vspDetails.getLicenseAgreement(), vspDetails.getFeatureGroups());
406 public String fetchValidationVsp(String user) {
408 validateUniqueName(VALIDATION_VSP_NAME);
409 } catch (Exception ignored) {
410 return VALIDATION_VSP_ID;
412 VspDetails validationVsp = new VspDetails();
413 validationVsp.setName(VALIDATION_VSP_NAME);
415 vspInfoDao.create(validationVsp);
416 Version version = versioningManager.create(
417 VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE,
418 validationVsp.getId(), user);
419 validationVsp.setVersion(version);
421 createUniqueName(VALIDATION_VSP_NAME);
422 VALIDATION_VSP_ID = validationVsp.getId();
423 return VALIDATION_VSP_ID;
427 public VspDetails createVsp(VspDetails vspDetails, String user) {
428 mdcDataDebugMessage.debugEntryMessage(null);
430 validateUniqueName(vspDetails.getName());
432 vspInfoDao.create(vspDetails);//id will be set in the dao
433 vspInfoDao.updateQuestionnaireData(vspDetails.getId(), null,
434 new JsonSchemaDataGenerator(getVspQuestionnaireSchema(null)).generateData());
436 Version version = versioningManager
437 .create(VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE,
438 vspDetails.getId(), user);
439 vspDetails.setVersion(version);
440 ActivityLogEntity activityLogEntity = new ActivityLogEntity(vspDetails.getId(), String
441 .valueOf(vspDetails.getVersion().getMajor() + 1),
442 ActivityType.CREATE_NEW.toString(), user, true, "", "");
443 activityLogManager.addActionLog(activityLogEntity, user);
444 String vspName = vspDetails.getName();
445 createUniqueName(vspName);
446 mdcDataDebugMessage.debugExitMessage(null);
451 public List<VersionedVendorSoftwareProductInfo> listVsps(String versionFilter, String user) {
452 mdcDataDebugMessage.debugEntryMessage(null);
454 Map<String, VersionInfo> idToVersionsInfo = versioningManager.listEntitiesVersionInfo(
455 VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE, user,
456 VersionableEntityAction.Read);
458 List<VersionedVendorSoftwareProductInfo> vsps = new ArrayList<>();
459 for (Map.Entry<String, VersionInfo> entry : idToVersionsInfo.entrySet()) {
460 VersionInfo versionInfo = entry.getValue();
461 if (versionFilter != null && versionFilter.equals(VersionStatus.Final.name())) {
462 if (versionInfo.getLatestFinalVersion() == null) {
465 versionInfo.setActiveVersion(versionInfo.getLatestFinalVersion());
466 versionInfo.setStatus(VersionStatus.Final);
467 versionInfo.setLockingUser(null);
470 Version version = versionInfo.getActiveVersion();
471 if (user.equals(versionInfo.getLockingUser())) {
472 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));
480 } catch (RuntimeException rte) {
482 "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)
796 Map<String, List<ErrorMessage>> validationErrors = new HashMap<>();
797 if (uploadData == null || uploadData.getContentData() == null) {
801 FileContentHandler fileContentMap = new FileContentHandler();
805 CommonUtil.loadUploadFileContent(uploadData.getContentData().array());
806 } catch (Exception e){
807 ErrorMessage errorMessage = new ErrorMessage(ErrorLevel.ERROR, e.getMessage());
808 validationErrors.put("Upload file", Arrays.asList(errorMessage));
810 ValidationManager validationManager =
811 ValidationManagerUtil.initValidationManager(fileContentMap);
812 validationErrors.putAll(validationManager.validate());
815 MapUtils.isEmpty(MessageContainerUtil.getMessageByLevel(ErrorLevel.ERROR, validationErrors))
816 ? null : validationErrors;
819 private VersionInfo getVersionInfo(String vendorSoftwareProductId, VersionableEntityAction action,
821 return versioningManager.getEntityVersionInfo(
822 VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE,
823 vendorSoftwareProductId, user, action);
827 private QuestionnaireValidationResult validateQuestionnaire(String vspId, Version version) {
828 mdcDataDebugMessage.debugEntryMessage("VSP id", vspId);
830 // The apis of CompositionEntityDataManager used here are stateful!
831 // so, it must be re-created from scratch when it is used!
832 CompositionEntityDataManager compositionEntityDataManager =
833 CompositionEntityDataManagerFactory.getInstance().createInterface();
834 compositionEntityDataManager
835 .addEntity(vspInfoDao.getQuestionnaire(vspId, version), null);
837 Collection<NicEntity> nics = vendorSoftwareProductDao.listNicsByVsp(vspId, version);
839 Map<String, List<String>> nicNamesByComponent = new HashMap<>();
840 for (NicEntity nicEntity : nics) {
841 compositionEntityDataManager.addEntity(nicEntity, null);
843 Nic nic = nicEntity.getNicCompositionData();
844 if (nic != null && nic.getName() != null) {
845 List<String> nicNames =
846 nicNamesByComponent.computeIfAbsent(nicEntity.getComponentId(), k -> new ArrayList<>());
847 nicNames.add(nic.getName());
851 Collection<ComponentEntity> components =
852 vendorSoftwareProductDao.listComponentsCompositionAndQuestionnaire(vspId, version);
853 components.forEach(component -> compositionEntityDataManager.addEntity(component,
854 new ComponentQuestionnaireSchemaInput(nicNamesByComponent.get(component.getId()),
855 JsonUtil.json2Object(component.getQuestionnaireData(), Map.class))));
857 Map<CompositionEntityId, Collection<String>> errorsByEntityId =
858 compositionEntityDataManager.validateEntitiesQuestionnaire();
859 if (MapUtils.isNotEmpty(errorsByEntityId)) {
860 compositionEntityDataManager.buildTrees();
861 compositionEntityDataManager.addErrorsToTrees(errorsByEntityId);
862 /* Set<CompositionEntityValidationData> entitiesWithValidationErrors =
863 compositionEntityDataManager.getEntityListWithErrors();*/
864 //Collection<CompositionEntityValidationData> roots = compositionEntityDataManager.getTrees();
866 mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
867 return new QuestionnaireValidationResult(
868 compositionEntityDataManager.getAllErrorsByVsp(vspId));
871 mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
876 public File getInformationArtifact(String vspId, Version version, String user) {
877 mdcDataDebugMessage.debugEntryMessage("VSP id", vspId);
878 VspDetails vspDetails = vspInfoDao.get(new VspDetails(vspId, version));
880 if (vspDetails == null) {
884 String vspName = vspDetails.getName();
885 ByteBuffer infoArtifactAsByteBuffer;
886 File infoArtifactFile;
888 infoArtifactAsByteBuffer = ByteBuffer.wrap(informationArtifactGenerator.generate(vspId,
889 version).getBytes());
893 String.format(VendorSoftwareProductConstants.INFORMATION_ARTIFACT_NAME, vspName));
894 OutputStream out = new BufferedOutputStream(new FileOutputStream(infoArtifactFile));
895 out.write(infoArtifactAsByteBuffer.array());
897 } catch (IOException e) {
898 throw new CoreException(new InformationArtifactCreationErrorBuilder(vspId).build(), e);
901 mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
902 return infoArtifactFile;
905 void validateUniqueName(String vspName) {
906 UniqueValueUtil.validateUniqueValue(
907 VendorSoftwareProductConstants.UniqueValues.VENDOR_SOFTWARE_PRODUCT_NAME, vspName);
910 void createUniqueName(String vspName) {
911 UniqueValueUtil.createUniqueValue(
912 VendorSoftwareProductConstants.UniqueValues.VENDOR_SOFTWARE_PRODUCT_NAME, vspName);
915 void updateUniqueName(String oldVspName, String newVspName) {
916 UniqueValueUtil.updateUniqueValue(
917 VendorSoftwareProductConstants.UniqueValues.VENDOR_SOFTWARE_PRODUCT_NAME,
918 oldVspName, newVspName);