995044588c833d2eaefd30a315676eeee9da479a
[sdc.git] / openecomp-be / backend / openecomp-sdc-vendor-software-product-manager / src / main / java / org / openecomp / sdc / vendorsoftwareproduct / impl / VendorSoftwareProductManagerImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.openecomp.sdc.vendorsoftwareproduct.impl;
22
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;
105
106 import java.io.BufferedOutputStream;
107 import java.io.ByteArrayInputStream;
108 import java.io.ByteArrayOutputStream;
109 import java.io.File;
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;
122
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";
127
128   private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
129   private static final Logger logger =
130       LoggerFactory.getLogger(VendorSoftwareProductManagerImpl.class);
131
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;
144
145
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;
171
172     registerToVersioning();
173   }
174
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);
182   }
183
184   @Override
185   public Version checkout(String vendorSoftwareProductId, String user) {
186     mdcDataDebugMessage.debugEntryMessage("VSP id", vendorSoftwareProductId);
187     MDC.put(LoggerConstants.SERVICE_NAME, LoggerServiceName.Checkout_Entity.toString());
188
189     Version newVersion = versioningManager
190         .checkout(VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE,
191             vendorSoftwareProductId, user);
192
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);
198     }
199
200     mdcDataDebugMessage.debugExitMessage("VSP id", vendorSoftwareProductId);
201     return newVersion;
202   }
203
204
205   @Override
206   public Version undoCheckout(String vendorSoftwareProductId, String user) {
207     mdcDataDebugMessage.debugEntryMessage("VSP id", vendorSoftwareProductId);
208
209     Version version =
210         getVersionInfo(vendorSoftwareProductId, VersionableEntityAction.Read, user)
211             .getActiveVersion();
212     String preVspName = vspInfoDao
213         .get(new VspDetails(vendorSoftwareProductId, version)).getName();
214
215     Version newVersion = versioningManager.undoCheckout(
216         VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE,
217         vendorSoftwareProductId, user);
218
219     String postVspName = vspInfoDao
220         .get(new VspDetails(vendorSoftwareProductId, newVersion))
221         .getName();
222
223     updateUniqueName(preVspName, postVspName);
224
225     mdcDataDebugMessage.debugExitMessage("VSP id", vendorSoftwareProductId);
226
227     return newVersion;
228   }
229
230   @Override
231   public Version checkin(String vendorSoftwareProductId, String user) {
232     mdcDataDebugMessage.debugEntryMessage("VSP id", vendorSoftwareProductId);
233
234     Version newVersion = versioningManager.checkin(
235         VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE,
236         vendorSoftwareProductId, user, null);
237
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);
243     }
244
245     mdcDataDebugMessage.debugExitMessage("VSP id", vendorSoftwareProductId);
246
247     return newVersion;
248   }
249
250   @Override
251   public ValidationResponse submit(String vspId, String user) throws IOException {
252     mdcDataDebugMessage.debugEntryMessage("VSP id", vspId);
253
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());
259
260     ValidationResponse validationResponse = new ValidationResponse();
261     validationResponse
262         .setVspErrors(validateCompletedVendorSoftwareProduct(vspDetails, uploadData, serviceModel),
263             LoggerServiceName.Submit_VSP, LoggerTragetServiceName.SUBMIT_VSP);
264
265     if (isCyclicDependencyInComponents(vspId, vspDetails.getVersion())) {
266       Collection<ErrorCode> vspErrors = validationResponse.getVspErrors() == null
267           ? new ArrayList<>()
268           : validationResponse.getVspErrors();
269       vspErrors.add(ComponentDependencyModelErrorBuilder
270           .getcyclicDependencyComponentErrorBuilder());
271       validationResponse.setVspErrors(vspErrors, LoggerServiceName.Submit_VSP,
272           LoggerTragetServiceName.SUBMIT_VSP);
273     }
274
275     validationResponse.setLicensingDataErrors(validateLicensingData(vspDetails));
276     validationResponse
277         .setUploadDataErrors(validateUploadData(uploadData), LoggerServiceName.Submit_VSP,
278             LoggerTragetServiceName.SUBMIT_VSP);
279     validationResponse.setQuestionnaireValidationResult(
280         validateQuestionnaire(vspDetails.getId(), vspDetails.getVersion()));
281
282     validationResponse.setCompilationErrors(
283         compile(vspId, vspDetails.getVersion(), serviceModel),
284         LoggerServiceName.Submit_VSP, LoggerTragetServiceName.SUBMIT_VSP);
285
286     if (validationResponse.isValid()) {
287       Version newVersion = versioningManager.submit(
288           VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE,
289           vspId, user, null);
290       ActivityLogEntity activityLogEntity = new ActivityLogEntity(vspDetails.getId(), String
291           .valueOf(newVersion.getMajor()),
292           ActivityType.SUBMIT.toString(), user, true, "", "");
293       activityLogManager.addActionLog(activityLogEntity, user);
294     }
295
296     mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
297
298     return validationResponse;
299   }
300
301   private boolean isCyclicDependencyInComponents(String vendorSoftwareProductId,
302                                                  Version version) {
303     final Collection<ComponentDependencyModelEntity> componentDependencyModelEntities =
304         vendorSoftwareProductDao.listComponentDependencies(vendorSoftwareProductId, version);
305     ComponentDependencyTracker dependencyTracker = new ComponentDependencyTracker();
306
307     for (ComponentDependencyModelEntity entity : componentDependencyModelEntities) {
308       dependencyTracker.addDependency(entity.getSourceComponentId(), entity.getTargetComponentId());
309     }
310     return dependencyTracker.isCyclicDependencyPresent();
311   }
312
313   private static List<ErrorCode> validateCompletedVendorSoftwareProduct(
314       VspDetails vspDetails, UploadDataEntity uploadData, Object serviceModel) {
315
316     List<ErrorCode> errros = new ArrayList<>();
317
318     if (vspDetails.getName() == null) {
319       errros.add(createMissingMandatoryFieldError("name"));
320     }
321     if (vspDetails.getDescription() == null) {
322       errros.add(createMissingMandatoryFieldError("description"));
323     }
324     if (vspDetails.getVendorId() == null) {
325       errros.add(createMissingMandatoryFieldError("vendor Id"));
326     }
327     if (vspDetails.getVlmVersion() == null) {
328       errros.add(createMissingMandatoryFieldError(
329           "licensing version (in the format of: {integer}.{integer})"));
330     }
331     if (vspDetails.getCategory() == null) {
332       errros.add(createMissingMandatoryFieldError("category"));
333     }
334     if (vspDetails.getSubCategory() == null) {
335       errros.add(createMissingMandatoryFieldError("sub category"));
336     }
337     if (vspDetails.getLicenseAgreement() == null) {
338       errros.add(createMissingMandatoryFieldError("license agreement"));
339     }
340     if (CollectionUtils.isEmpty(vspDetails.getFeatureGroups())) {
341       errros.add(createMissingMandatoryFieldError("feature groups"));
342     }
343     if (uploadData == null || uploadData.getContentData() == null || serviceModel == null) {
344       errros.add(
345           new VendorSoftwareProductInvalidErrorBuilder(vspDetails.getId(), vspDetails.getVersion())
346               .build());
347     }
348
349     return errros.isEmpty() ? null : errros;
350   }
351
352   private static ErrorCode createMissingMandatoryFieldError(String fieldName) {
353     return new ValidationErrorBuilder("must be supplied", fieldName).build();
354   }
355
356   String getVspQuestionnaireSchema(SchemaTemplateInput schemaInput) {
357     mdcDataDebugMessage.debugEntryMessage(null);
358     mdcDataDebugMessage.debugExitMessage(null);
359     return SchemaGenerator
360         .generate(SchemaTemplateContext.questionnaire, CompositionEntityType.vsp, schemaInput);
361   }
362
363   private static void sortVspListByModificationTimeDescOrder(
364       List<VersionedVendorSoftwareProductInfo> vsps) {
365     vsps.sort((o1, o2) -> o2.getVspDetails().getWritetimeMicroSeconds()
366         .compareTo(o1.getVspDetails().getWritetimeMicroSeconds()));
367   }
368
369
370   private Map<String, List<ErrorMessage>> compile(String vendorSoftwareProductId, Version version,
371                                                   ToscaServiceModel serviceModel) {
372     if (serviceModel == null) {
373       return null;
374     }
375
376     enrichedServiceModelDao.deleteAll(vendorSoftwareProductId, version);
377
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();
383
384     enrichedServiceModelDao
385         .storeServiceModel(vendorSoftwareProductId, version, enrichmentManager.getModel());
386
387     return enrichErrors;
388   }
389
390   private Collection<ErrorCode> validateLicensingData(VspDetails vspDetails) {
391     mdcDataDebugMessage.debugEntryMessage("VSP id", vspDetails.getId());
392
393     if (vspDetails.getVendorId() == null || vspDetails.getVlmVersion() == null
394         || vspDetails.getLicenseAgreement() == null
395         || CollectionUtils.isEmpty(vspDetails.getFeatureGroups())) {
396       return null;
397     }
398
399     mdcDataDebugMessage.debugExitMessage("VSP id", vspDetails.getId());
400     return vendorLicenseFacade
401         .validateLicensingData(vspDetails.getVendorId(), vspDetails.getVlmVersion(),
402             vspDetails.getLicenseAgreement(), vspDetails.getFeatureGroups());
403   }
404
405   @Override
406   public String fetchValidationVsp(String user) {
407     try {
408       validateUniqueName(VALIDATION_VSP_NAME);
409     } catch (Exception ignored) {
410       return VALIDATION_VSP_ID;
411     }
412     VspDetails validationVsp = new VspDetails();
413     validationVsp.setName(VALIDATION_VSP_NAME);
414
415     vspInfoDao.create(validationVsp);
416     Version version = versioningManager.create(
417         VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE,
418         validationVsp.getId(), user);
419     validationVsp.setVersion(version);
420
421     createUniqueName(VALIDATION_VSP_NAME);
422     VALIDATION_VSP_ID = validationVsp.getId();
423     return VALIDATION_VSP_ID;
424   }
425
426   @Override
427   public VspDetails createVsp(VspDetails vspDetails, String user) {
428     mdcDataDebugMessage.debugEntryMessage(null);
429
430     validateUniqueName(vspDetails.getName());
431
432     vspInfoDao.create(vspDetails);//id will be set in the dao
433     vspInfoDao.updateQuestionnaireData(vspDetails.getId(), null,
434         new JsonSchemaDataGenerator(getVspQuestionnaireSchema(null)).generateData());
435
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);
447     return vspDetails;
448   }
449
450   @Override
451   public List<VersionedVendorSoftwareProductInfo> listVsps(String versionFilter, String user) {
452     mdcDataDebugMessage.debugEntryMessage(null);
453
454     Map<String, VersionInfo> idToVersionsInfo = versioningManager.listEntitiesVersionInfo(
455         VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE, user,
456         VersionableEntityAction.Read);
457
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) {
463           continue;
464         }
465         versionInfo.setActiveVersion(versionInfo.getLatestFinalVersion());
466         versionInfo.setStatus(VersionStatus.Final);
467         versionInfo.setLockingUser(null);
468       }
469
470       Version version = versionInfo.getActiveVersion();
471       if (user.equals(versionInfo.getLockingUser())) {
472         version.setStatus(VersionStatus.Locked);
473       }
474       try {
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));
479         }
480       } catch (RuntimeException rte) {
481         logger.error(
482             "Error trying to retrieve vsp[" + entry.getKey() + "] version[" + version.toString
483                 () + "] " +
484                 "message:" + rte
485                 .getMessage());
486       }
487     }
488
489     sortVspListByModificationTimeDescOrder(vsps);
490
491     mdcDataDebugMessage.debugExitMessage(null);
492
493     return vsps;
494   }
495
496   @Override
497   public void updateVsp(VspDetails vspDetails, String user) {
498     mdcDataDebugMessage.debugEntryMessage("VSP id", vspDetails.getId());
499
500     VspDetails retrieved = vspInfoDao.get(vspDetails);
501
502     updateUniqueName(retrieved.getName(), vspDetails.getName());
503     vspDetails.setOldVersion(retrieved.getOldVersion());
504
505     vspInfoDao.update(vspDetails);
506     //vendorSoftwareProductDao.updateVspLatestModificationTime(vspDetails.getId(), activeVersion);
507
508     mdcDataDebugMessage.debugExitMessage("VSP id", vspDetails.getId());
509   }
510
511
512   @Override
513   public VspDetails getVsp(String vspId, Version version, String user) {
514     mdcDataDebugMessage.debugEntryMessage("VSP id", vspId);
515
516     VspDetails vsp = vspInfoDao.get(new VspDetails(vspId, version));
517     if (vsp == null) {
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());
522     }
523     vsp.setValidationData(orchestrationTemplateDao.getValidationData(vspId, version));
524
525     mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
526     return vsp;
527   }
528
529   @Override
530   public Version callAutoHeal(String vspId, VersionInfo versionInfo,
531                               VspDetails vendorSoftwareProductInfo, String user)
532       throws Exception {
533     switch (versionInfo.getStatus()) {
534       case Locked:
535         if (user.equals(versionInfo.getLockingUser())) {
536           autoHeal(vspId, versionInfo.getActiveVersion(), vendorSoftwareProductInfo,
537               versionInfo.getLockingUser());
538         }
539         return versionInfo.getActiveVersion();
540       case Available:
541         Version checkoutVersion = checkout(vspId, user);
542         autoHeal(vspId, checkoutVersion, vendorSoftwareProductInfo, user);
543         return checkin(vspId, user);
544       case Final:
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);
551         }
552
553         try {
554           Version finalVersion = checkinFinalVersion.calculateNextFinal();
555           createPackage(vspId, finalVersion, user);
556           return finalVersion;
557         } catch (IOException e) {
558           throw new Exception(e.getMessage());
559         }
560     }
561     return versionInfo.getActiveVersion();
562   }
563
564   @Override
565
566   public void deleteVsp(String vspId, String user) {
567     mdcDataDebugMessage.debugEntryMessage("VSP id", vspId);
568
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);
573
574     throw new UnsupportedOperationException(
575         VendorSoftwareProductConstants.UNSUPPORTED_OPERATION_ERROR);
576   }
577
578   @Override
579   public void heal(String vspId, Version version, String user) {
580     mdcDataDebugMessage.debugEntryMessage("VSP id", vspId);
581
582     VersionInfo versionInfo = getVersionInfo(vspId, VersionableEntityAction.Read, user);
583
584     version = VersionStatus.Locked.equals(versionInfo.getStatus())
585         ? versionInfo.getActiveVersion()
586         : checkout(vspId, user);
587     version.setStatus(VersionStatus.Locked);
588
589     healingManager.healAll(getHealingParamsAsMap(vspId, version, user));
590
591     VspDetails vspDetails = new VspDetails(vspId, version);
592     vspDetails.setOldVersion(null);
593     vspInfoDao.updateOldVersionIndication(vspDetails);
594
595     logger.audit("Healed VSP " + vspDetails.getId());
596     mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
597   }
598
599   private void autoHeal(String vspId, Version checkoutVersion, VspDetails vspDetails, String user) {
600     mdcDataDebugMessage.debugEntryMessage("VSP id", vspId);
601
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);
608
609     logger.audit("Healed VSP " + vspDetails.getName());
610
611     mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
612   }
613
614   private Map<String, Object> getHealingParamsAsMap(String vspId, Version version, String user) {
615     Map<String, Object> healingParams = new HashMap<>();
616
617     healingParams.put(SdcCommon.VSP_ID, vspId);
618     healingParams.put(SdcCommon.VERSION, version);
619     healingParams.put(SdcCommon.USER, user);
620
621     return healingParams;
622   }
623
624   @Override
625   public List<PackageInfo> listPackages(String category, String subCategory) {
626     return packageInfoDao.listByCategory(category, subCategory);
627   }
628
629   @Override
630   public File getTranslatedFile(String vspId, Version version, String user) {
631     mdcDataDebugMessage.debugEntryMessage("VSP id", vspId);
632     String errorMessage;
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());
645     }
646
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());
655     }
656
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());
664     }
665
666     File translatedFile = new File(VendorSoftwareProductConstants.VSP_PACKAGE_ZIP);
667
668     try {
669       FileOutputStream fos = new FileOutputStream(translatedFile);
670       fos.write(translatedFileBuffer.array());
671       fos.close();
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(),
678           exception);
679     }
680
681     mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
682
683     return translatedFile;
684   }
685
686   @Override
687
688   public byte[] getOrchestrationTemplateFile(String vspId, Version version, String user) {
689     mdcDataDebugMessage.debugEntryMessage("VSP id", vspId);
690
691     UploadDataEntity uploadData = orchestrationTemplateDao.getOrchestrationTemplate(vspId, version);
692     ByteBuffer contentData = uploadData.getContentData();
693     if (contentData == null) {
694       return null;
695     }
696
697     ByteArrayOutputStream baos = new ByteArrayOutputStream();
698
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);
708     }
709
710     mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
711     return baos.toByteArray();
712   }
713
714   @Override
715   public PackageInfo createPackage(String vspId, Version version, String user) throws IOException {
716     mdcDataDebugMessage.debugEntryMessage("VSP id", vspId);
717
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)
724               .build());
725     }
726
727     ToscaServiceModel toscaServiceModel = enrichedServiceModelDao.getServiceModel(vspId, version);
728     VspDetails vspDetails = vspInfoDao.get(new VspDetails(vspId, version));
729     Version vlmVersion = vspDetails.getVlmVersion();
730
731     PackageInfo packageInfo = createPackageInfo(vspId, vspDetails);
732
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)));
741
742     packageInfoDao.create(packageInfo);
743
744     mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
745     return packageInfo;
746   }
747
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
759     return packageInfo;
760   }
761
762   @Override
763
764   public QuestionnaireResponse getVspQuestionnaire(String vspId, Version version, String user) {
765     mdcDataDebugMessage.debugEntryMessage("VSP id", vspId);
766
767     VspQuestionnaireEntity retrieved = vspInfoDao.getQuestionnaire(vspId, version);
768     VersioningUtil.validateEntityExistence(retrieved, new VspQuestionnaireEntity(vspId, version),
769         VspDetails.ENTITY_TYPE);
770
771     String questionnaireData = retrieved.getQuestionnaireData();
772
773     QuestionnaireResponse questionnaireResponse = new QuestionnaireResponse();
774     questionnaireResponse.setData(questionnaireData);
775     questionnaireResponse.setSchema(getVspQuestionnaireSchema(null));
776
777     mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
778
779     return questionnaireResponse;
780   }
781
782   @Override
783   public void updateVspQuestionnaire(String vspId, Version version, String questionnaireData,
784                                      String user) {
785     mdcDataDebugMessage.debugEntryMessage("VSP id", vspId);
786
787     vspInfoDao.updateQuestionnaireData(vspId, version, questionnaireData);
788
789     mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
790   }
791
792
793   private Map<String, List<ErrorMessage>> validateUploadData(UploadDataEntity uploadData)
794       throws IOException {
795
796     Map<String, List<ErrorMessage>> validationErrors = new HashMap<>();
797     if (uploadData == null || uploadData.getContentData() == null) {
798       return null;
799     }
800
801     FileContentHandler fileContentMap = new FileContentHandler();
802
803     try {
804       fileContentMap =
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));
809     }
810     ValidationManager validationManager =
811         ValidationManagerUtil.initValidationManager(fileContentMap);
812     validationErrors.putAll(validationManager.validate());
813
814     return
815         MapUtils.isEmpty(MessageContainerUtil.getMessageByLevel(ErrorLevel.ERROR, validationErrors))
816             ? null : validationErrors;
817   }
818
819   private VersionInfo getVersionInfo(String vendorSoftwareProductId, VersionableEntityAction action,
820                                      String user) {
821     return versioningManager.getEntityVersionInfo(
822         VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE,
823         vendorSoftwareProductId, user, action);
824   }
825
826
827   private QuestionnaireValidationResult validateQuestionnaire(String vspId, Version version) {
828     mdcDataDebugMessage.debugEntryMessage("VSP id", vspId);
829
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);
836
837     Collection<NicEntity> nics = vendorSoftwareProductDao.listNicsByVsp(vspId, version);
838
839     Map<String, List<String>> nicNamesByComponent = new HashMap<>();
840     for (NicEntity nicEntity : nics) {
841       compositionEntityDataManager.addEntity(nicEntity, null);
842
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());
848       }
849     }
850
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))));
856
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();
865
866       mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
867       return new QuestionnaireValidationResult(
868           compositionEntityDataManager.getAllErrorsByVsp(vspId));
869     }
870
871     mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
872     return null;
873   }
874
875   @Override
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));
879
880     if (vspDetails == null) {
881       return null;
882     }
883
884     String vspName = vspDetails.getName();
885     ByteBuffer infoArtifactAsByteBuffer;
886     File infoArtifactFile;
887     try {
888       infoArtifactAsByteBuffer = ByteBuffer.wrap(informationArtifactGenerator.generate(vspId,
889           version).getBytes());
890
891       infoArtifactFile =
892           new File(
893               String.format(VendorSoftwareProductConstants.INFORMATION_ARTIFACT_NAME, vspName));
894       OutputStream out = new BufferedOutputStream(new FileOutputStream(infoArtifactFile));
895       out.write(infoArtifactAsByteBuffer.array());
896       out.close();
897     } catch (IOException e) {
898       throw new CoreException(new InformationArtifactCreationErrorBuilder(vspId).build(), e);
899     }
900
901     mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
902     return infoArtifactFile;
903   }
904
905   void validateUniqueName(String vspName) {
906     UniqueValueUtil.validateUniqueValue(
907         VendorSoftwareProductConstants.UniqueValues.VENDOR_SOFTWARE_PRODUCT_NAME, vspName);
908   }
909
910   void createUniqueName(String vspName) {
911     UniqueValueUtil.createUniqueValue(
912         VendorSoftwareProductConstants.UniqueValues.VENDOR_SOFTWARE_PRODUCT_NAME, vspName);
913   }
914
915   void updateUniqueName(String oldVspName, String newVspName) {
916     UniqueValueUtil.updateUniqueValue(
917         VendorSoftwareProductConstants.UniqueValues.VENDOR_SOFTWARE_PRODUCT_NAME,
918         oldVspName, newVspName);
919   }
920 }