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.vendorlicense.impl;
23 import org.openecomp.core.util.UniqueValueUtil;
24 import org.openecomp.sdc.activityLog.ActivityLogManager;
25 import org.openecomp.sdc.activityLog.ActivityLogManagerFactory;
26 import org.openecomp.sdc.activitylog.dao.type.ActivityLogEntity;
27 import org.openecomp.sdc.common.errors.CoreException;
28 import org.openecomp.sdc.common.errors.ErrorCode;
29 import org.openecomp.sdc.datatypes.error.ErrorLevel;
30 import org.openecomp.sdc.logging.api.Logger;
31 import org.openecomp.sdc.logging.api.LoggerFactory;
32 import org.openecomp.sdc.logging.context.impl.MdcDataDebugMessage;
33 import org.openecomp.sdc.logging.context.impl.MdcDataErrorMessage;
34 import org.openecomp.sdc.logging.types.LoggerConstants;
35 import org.openecomp.sdc.logging.types.LoggerErrorCode;
36 import org.openecomp.sdc.logging.types.LoggerErrorDescription;
37 import org.openecomp.sdc.logging.types.LoggerServiceName;
38 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
39 import org.openecomp.sdc.vendorlicense.VendorLicenseConstants;
40 import org.openecomp.sdc.vendorlicense.VendorLicenseManager;
41 import org.openecomp.sdc.vendorlicense.dao.EntitlementPoolDao;
42 import org.openecomp.sdc.vendorlicense.dao.EntitlementPoolDaoFactory;
43 import org.openecomp.sdc.vendorlicense.dao.FeatureGroupDao;
44 import org.openecomp.sdc.vendorlicense.dao.FeatureGroupDaoFactory;
45 import org.openecomp.sdc.vendorlicense.dao.LicenseAgreementDao;
46 import org.openecomp.sdc.vendorlicense.dao.LicenseAgreementDaoFactory;
47 import org.openecomp.sdc.vendorlicense.dao.LicenseKeyGroupDao;
48 import org.openecomp.sdc.vendorlicense.dao.LicenseKeyGroupDaoFactory;
49 import org.openecomp.sdc.vendorlicense.dao.LimitDao;
50 import org.openecomp.sdc.vendorlicense.dao.LimitDaoFactory;
51 import org.openecomp.sdc.vendorlicense.dao.VendorLicenseModelDao;
52 import org.openecomp.sdc.vendorlicense.dao.VendorLicenseModelDaoFactory;
53 import org.openecomp.sdc.vendorlicense.dao.types.EntitlementPoolEntity;
54 import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity;
55 import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupModel;
56 import org.openecomp.sdc.vendorlicense.dao.types.LicenseAgreementEntity;
57 import org.openecomp.sdc.vendorlicense.dao.types.LicenseAgreementModel;
58 import org.openecomp.sdc.vendorlicense.dao.types.LicenseKeyGroupEntity;
59 import org.openecomp.sdc.vendorlicense.dao.types.LimitEntity;
60 import org.openecomp.sdc.vendorlicense.dao.types.LimitType;
61 import org.openecomp.sdc.vendorlicense.dao.types.VendorLicenseModelEntity;
62 import org.openecomp.sdc.vendorlicense.errors.InvalidDateErrorBuilder;
63 import org.openecomp.sdc.vendorlicense.errors.LimitErrorBuilder;
64 import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacade;
65 import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacadeFactory;
66 import org.openecomp.sdc.vendorlicense.types.VersionedVendorLicenseModel;
67 import org.openecomp.sdc.versioning.VersioningManager;
68 import org.openecomp.sdc.versioning.VersioningManagerFactory;
69 import org.openecomp.sdc.versioning.VersioningUtil;
70 import org.openecomp.sdc.versioning.dao.types.Version;
71 import org.openecomp.sdc.versioning.dao.types.VersionStatus;
72 import org.openecomp.sdc.versioning.types.VersionInfo;
73 import org.openecomp.sdc.versioning.types.VersionableEntityAction;
74 import org.openecomp.sdcrests.activitylog.types.ActivityType;
76 import java.time.LocalDate;
77 import java.time.format.DateTimeFormatter;
78 import java.util.ArrayList;
79 import java.util.Collection;
80 import java.util.Iterator;
81 import java.util.List;
83 import java.util.Optional;
86 import static org.openecomp.sdc.vendorlicense.VendorLicenseConstants.VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE;
88 public class VendorLicenseManagerImpl implements VendorLicenseManager {
89 private static final VersioningManager versioningManager =
90 VersioningManagerFactory.getInstance().createInterface();
91 private VendorLicenseFacade vendorLicenseFacade =
92 VendorLicenseFacadeFactory.getInstance().createInterface();
93 private static final VendorLicenseModelDao vendorLicenseModelDao =
94 VendorLicenseModelDaoFactory.getInstance().createInterface();
95 private static final LicenseAgreementDao licenseAgreementDao =
96 LicenseAgreementDaoFactory.getInstance().createInterface();
97 private static final FeatureGroupDao featureGroupDao =
98 FeatureGroupDaoFactory.getInstance().createInterface();
99 private static final EntitlementPoolDao entitlementPoolDao =
100 EntitlementPoolDaoFactory.getInstance().createInterface();
101 private static final LicenseKeyGroupDao licenseKeyGroupDao =
102 LicenseKeyGroupDaoFactory.getInstance().createInterface();
103 private static final LimitDao limitDao =
104 LimitDaoFactory.getInstance().createInterface();
106 private ActivityLogManager activityLogManager = ActivityLogManagerFactory.getInstance().createInterface();
107 private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
108 private static final Logger logger =
109 LoggerFactory.getLogger(VendorLicenseManagerImpl.class);
111 private static void sortVlmListByModificationTimeDescOrder(
112 List<VersionedVendorLicenseModel> vendorLicenseModels) {
113 vendorLicenseModels.sort((o1, o2) -> o2.getVendorLicenseModel().getWritetimeMicroSeconds()
114 .compareTo(o1.getVendorLicenseModel().getWritetimeMicroSeconds()));
118 public void checkout(String vendorLicenseModelId, String user) {
120 mdcDataDebugMessage.debugEntryMessage("VLM id", vendorLicenseModelId);
122 Version newVersion = versioningManager
123 .checkout(VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE, vendorLicenseModelId, user);
125 ActivityLogEntity activityLogEntity = new ActivityLogEntity(vendorLicenseModelId, String.valueOf(newVersion.getMajor()+1),
126 ActivityType.CHECKOUT.toString(), user, true, "", "");
127 activityLogManager.addActionLog(activityLogEntity, user);
129 newVersion.setStatus(VersionStatus.Locked);
130 vendorLicenseFacade.updateVlmLastModificationTime(vendorLicenseModelId, newVersion);
132 mdcDataDebugMessage.debugExitMessage("VLM id", vendorLicenseModelId);
136 public void undoCheckout(String vendorLicenseModelId, String user) {
138 mdcDataDebugMessage.debugEntryMessage("VLM id", vendorLicenseModelId);
140 Version newVersion = versioningManager
141 .undoCheckout(VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE, vendorLicenseModelId, user);
142 vendorLicenseFacade.updateVlmLastModificationTime(vendorLicenseModelId, newVersion);
144 mdcDataDebugMessage.debugExitMessage("VLM id", vendorLicenseModelId);
148 public void checkin(String vendorLicenseModelId, String user) {
150 mdcDataDebugMessage.debugEntryMessage("VLM id", vendorLicenseModelId);
152 Version newVersion = vendorLicenseFacade.checkin(vendorLicenseModelId, user);
154 ActivityLogEntity activityLogEntity = new ActivityLogEntity(vendorLicenseModelId,
155 String.valueOf(newVersion.getMajor()+1), ActivityType.CHECKIN.toString(), user, true, "", "");
156 activityLogManager.addActionLog(activityLogEntity, user);
158 mdcDataDebugMessage.debugExitMessage("VLM id", vendorLicenseModelId);
162 public void submit(String vendorLicenseModelId, String user) {
164 mdcDataDebugMessage.debugEntryMessage("VLM id", vendorLicenseModelId);
166 Version newVersion = vendorLicenseFacade.submit(vendorLicenseModelId, user);
168 ActivityLogEntity activityLogEntity = new ActivityLogEntity(vendorLicenseModelId, String.valueOf(newVersion.getMajor()),
169 ActivityType.SUBMIT.toString(), user, true, "", "");
170 activityLogManager.addActionLog(activityLogEntity, user);
172 mdcDataDebugMessage.debugExitMessage("VLM id", vendorLicenseModelId);
176 public Collection<VersionedVendorLicenseModel> listVendorLicenseModels(String versionFilter,
178 mdcDataDebugMessage.debugEntryMessage(null);
180 Map<String, VersionInfo> idToVersionsInfo = versioningManager
181 .listEntitiesVersionInfo(VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE, user,
182 VersionableEntityAction.Read);
184 List<VersionedVendorLicenseModel> vendorLicenseModels = new ArrayList<>();
185 for (Map.Entry<String, VersionInfo> entry : idToVersionsInfo.entrySet()) {
186 VersionInfo versionInfo = entry.getValue();
187 if (versionFilter != null && versionFilter.equals(VersionStatus.Final.name())) {
188 if (versionInfo.getLatestFinalVersion() == null) {
191 versionInfo.setActiveVersion(versionInfo.getLatestFinalVersion());
192 versionInfo.setStatus(VersionStatus.Final);
193 versionInfo.setLockingUser(null);
196 Version version = versionInfo.getActiveVersion();
197 if (user.equals(versionInfo.getLockingUser())) {
198 version.setStatus(VersionStatus.Locked);
202 VendorLicenseModelEntity vlm =
203 vendorLicenseModelDao.get(new VendorLicenseModelEntity(entry.getKey(), version));
205 VersionedVendorLicenseModel versionedVlm = new VersionedVendorLicenseModel();
206 versionedVlm.setVendorLicenseModel(vlm);
207 versionedVlm.setVersionInfo(versionInfo);
208 vendorLicenseModels.add(versionedVlm);
210 }catch(RuntimeException rte){
211 logger.error("Error trying to retrieve vlm["+entry.getKey()+"] version["+version.toString
218 sortVlmListByModificationTimeDescOrder(vendorLicenseModels);
220 mdcDataDebugMessage.debugExitMessage(null);
221 return vendorLicenseModels;
225 public VendorLicenseModelEntity createVendorLicenseModel(VendorLicenseModelEntity vendorLicenseModelEntity, String user) {
227 VendorLicenseModelEntity vendorLicenseModelCreated = vendorLicenseFacade.createVendorLicenseModel(vendorLicenseModelEntity, user);
229 ActivityLogEntity activityLogEntity = new ActivityLogEntity(vendorLicenseModelCreated.getId(),
230 String.valueOf(vendorLicenseModelCreated.getVersion().getMajor()+1),
231 ActivityType.CREATE_NEW.toString(), user, true, "", "");
232 activityLogManager.addActionLog(activityLogEntity, user);
234 return vendorLicenseModelCreated;
238 public void updateVendorLicenseModel(VendorLicenseModelEntity vendorLicenseModelEntity,
240 mdcDataDebugMessage.debugEntryMessage("VLM id", vendorLicenseModelEntity.getId());
242 Version version = VersioningUtil.resolveVersion(null,
243 getVersionInfo(vendorLicenseModelEntity.getId(), VersionableEntityAction.Write, user),
245 vendorLicenseModelEntity.setVersion(version);
247 String existingVendorName = vendorLicenseModelDao.get(vendorLicenseModelEntity).getVendorName();
249 .updateUniqueValue(VendorLicenseConstants.UniqueValues.VENDOR_NAME, existingVendorName,
250 vendorLicenseModelEntity.getVendorName());
251 vendorLicenseModelDao.update(vendorLicenseModelEntity);
254 .updateVlmLastModificationTime(vendorLicenseModelEntity.getId(), version);
256 mdcDataDebugMessage.debugExitMessage("VLM id", vendorLicenseModelEntity.getId());
260 public VersionedVendorLicenseModel getVendorLicenseModel(String vlmId, Version version,
262 return vendorLicenseFacade.getVendorLicenseModel(vlmId, version, user);
266 public void deleteVendorLicenseModel(String vlmId, String user) {
267 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
268 LoggerTragetServiceName.DELETE_ENTITY, ErrorLevel.ERROR.name(),
269 LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.UNSUPPORTED_OPERATION);
270 throw new UnsupportedOperationException(VendorLicenseConstants.UNSUPPORTED_OPERATION_ERROR);
274 public Collection<LicenseAgreementEntity> listLicenseAgreements(String vlmId, Version version,
276 mdcDataDebugMessage.debugEntryMessage("VLM id", vlmId);
277 mdcDataDebugMessage.debugExitMessage("VLM id", vlmId);
278 return licenseAgreementDao.list(new LicenseAgreementEntity(vlmId, VersioningUtil
279 .resolveVersion(version, getVersionInfo(vlmId, VersionableEntityAction.Read, user), user),
284 public LicenseAgreementEntity createLicenseAgreement(LicenseAgreementEntity licenseAgreement,
287 .debugEntryMessage("VLM id", licenseAgreement.getVendorLicenseModelId());
289 .debugExitMessage("VLM id", licenseAgreement.getVendorLicenseModelId());
290 return vendorLicenseFacade.createLicenseAgreement(licenseAgreement, user);
294 public void updateLicenseAgreement(LicenseAgreementEntity licenseAgreement,
295 Set<String> addedFeatureGroupIds,
296 Set<String> removedFeatureGroupIds, String user) {
297 mdcDataDebugMessage.debugEntryMessage("VLM id, LA id", licenseAgreement
298 .getVendorLicenseModelId(), licenseAgreement.getId());
300 Version version = VersioningUtil.resolveVersion(licenseAgreement.getVersion(),
301 getVersionInfo(licenseAgreement.getVendorLicenseModelId(), VersionableEntityAction.Write,
303 licenseAgreement.setVersion(version);
304 LicenseAgreementEntity retrieved = licenseAgreementDao.get(licenseAgreement);
306 .validateEntityExistence(retrieved, licenseAgreement, VendorLicenseModelEntity.ENTITY_TYPE);
307 VersioningUtil.validateContainedEntitiesExistence(new FeatureGroupEntity().getEntityType(),
308 removedFeatureGroupIds, retrieved, retrieved.getFeatureGroupIds());
309 VersioningUtil.validateEntitiesExistence(addedFeatureGroupIds,
310 new FeatureGroupEntity(licenseAgreement.getVendorLicenseModelId(), version, null),
311 featureGroupDao, VendorLicenseModelEntity.ENTITY_TYPE);
313 UniqueValueUtil.updateUniqueValue(VendorLicenseConstants.UniqueValues.LICENSE_AGREEMENT_NAME,
314 retrieved.getName(), licenseAgreement.getName(), licenseAgreement.getVendorLicenseModelId(),
315 licenseAgreement.getVersion().toString());
316 licenseAgreementDao.updateColumnsAndDeltaFeatureGroupIds(licenseAgreement, addedFeatureGroupIds,
317 removedFeatureGroupIds);
319 addFeatureGroupsToLicenseAgreementRef(addedFeatureGroupIds, licenseAgreement);
320 removeFeatureGroupsToLicenseAgreementRef(removedFeatureGroupIds, licenseAgreement);
323 .updateVlmLastModificationTime(licenseAgreement.getVendorLicenseModelId(), version);
325 mdcDataDebugMessage.debugExitMessage("VLM id, LA id", licenseAgreement
326 .getVendorLicenseModelId(), licenseAgreement.getId());
330 public LicenseAgreementModel getLicenseAgreementModel(String vlmId, Version version,
331 String licenseAgreementId, String user) {
333 mdcDataDebugMessage.debugEntryMessage("VLM id, LA id", vlmId, licenseAgreementId);
334 mdcDataDebugMessage.debugExitMessage("VLM id, LA id", vlmId, licenseAgreementId);
335 return vendorLicenseFacade.getLicenseAgreementModel(vlmId, version, licenseAgreementId, user);
339 public void deleteLicenseAgreement(String vlmId, Version version, String licenseAgreementId,
341 mdcDataDebugMessage.debugEntryMessage("VLM id, LA id", vlmId, licenseAgreementId);
343 version = VersioningUtil
344 .resolveVersion(version, getVersionInfo(vlmId, VersionableEntityAction.Write, user), user);
345 LicenseAgreementEntity input =
346 new LicenseAgreementEntity(vlmId, version, licenseAgreementId);
347 LicenseAgreementEntity retrieved = licenseAgreementDao.get(input);
348 VersioningUtil.validateEntityExistence(retrieved, input, VendorLicenseModelEntity.ENTITY_TYPE);
350 removeFeatureGroupsToLicenseAgreementRef(retrieved.getFeatureGroupIds(), retrieved);
352 licenseAgreementDao.delete(input);
353 UniqueValueUtil.deleteUniqueValue(VendorLicenseConstants.UniqueValues.LICENSE_AGREEMENT_NAME,
354 retrieved.getVendorLicenseModelId(), retrieved.getVersion().toString(),
355 retrieved.getName());
358 .updateVlmLastModificationTime(input.getVendorLicenseModelId(), input.getVersion());
360 mdcDataDebugMessage.debugExitMessage("VLM id, LA id", vlmId, licenseAgreementId);
364 public Collection<FeatureGroupEntity> listFeatureGroups(String vlmId, Version version,
366 mdcDataDebugMessage.debugEntryMessage("VLM id", vlmId);
367 mdcDataDebugMessage.debugExitMessage("VLM id", vlmId);
368 return featureGroupDao.list(new FeatureGroupEntity(vlmId, VersioningUtil
369 .resolveVersion(version, getVersionInfo(vlmId, VersionableEntityAction.Read, user), user),
374 public FeatureGroupEntity createFeatureGroup(FeatureGroupEntity featureGroup, String user) {
376 .debugEntryMessage("VLM id", featureGroup.getVendorLicenseModelId());
377 mdcDataDebugMessage.debugExitMessage("VLM id", featureGroup.getId());
378 return vendorLicenseFacade.createFeatureGroup(featureGroup, user);
382 public void updateFeatureGroup(FeatureGroupEntity featureGroup,
383 Set<String> addedLicenseKeyGroups,
384 Set<String> removedLicenseKeyGroups,
385 Set<String> addedEntitlementPools,
386 Set<String> removedEntitlementPools,
388 mdcDataDebugMessage.debugEntryMessage("VLM id, FG id", featureGroup
389 .getVendorLicenseModelId(), featureGroup.getId());
391 Version version = VersioningUtil.resolveVersion(featureGroup.getVersion(),
392 getVersionInfo(featureGroup.getVendorLicenseModelId(), VersionableEntityAction.Write, user),
394 featureGroup.setVersion(version);
396 FeatureGroupEntity retrieved = featureGroupDao.get(featureGroup);
398 .validateEntityExistence(retrieved, featureGroup, VendorLicenseModelEntity.ENTITY_TYPE);
400 VersioningUtil.validateContainedEntitiesExistence(new LicenseKeyGroupEntity().getEntityType(),
401 removedLicenseKeyGroups, retrieved, retrieved.getLicenseKeyGroupIds());
402 VersioningUtil.validateContainedEntitiesExistence(new EntitlementPoolEntity().getEntityType(),
403 removedEntitlementPools, retrieved, retrieved.getEntitlementPoolIds());
405 VersioningUtil.validateEntitiesExistence(addedLicenseKeyGroups,
406 new LicenseKeyGroupEntity(featureGroup.getVendorLicenseModelId(), version, null),
407 licenseKeyGroupDao, VendorLicenseModelEntity.ENTITY_TYPE);
408 VersioningUtil.validateEntitiesExistence(addedEntitlementPools,
409 new EntitlementPoolEntity(featureGroup.getVendorLicenseModelId(), version, null),
410 entitlementPoolDao, VendorLicenseModelEntity.ENTITY_TYPE);
411 UniqueValueUtil.updateUniqueValue(VendorLicenseConstants.UniqueValues.FEATURE_GROUP_NAME,
412 retrieved.getName(), featureGroup.getName(), featureGroup.getVendorLicenseModelId(),
413 featureGroup.getVersion().toString());
415 addLicenseKeyGroupsToFeatureGroupsRef(addedLicenseKeyGroups, featureGroup);
416 removeLicenseKeyGroupsToFeatureGroupsRef(removedLicenseKeyGroups, featureGroup);
417 addEntitlementPoolsToFeatureGroupsRef(addedEntitlementPools, featureGroup);
418 removeEntitlementPoolsToFeatureGroupsRef(removedEntitlementPools, featureGroup);
420 featureGroupDao.updateFeatureGroup(featureGroup, addedEntitlementPools, removedEntitlementPools,
421 addedLicenseKeyGroups, removedLicenseKeyGroups);
424 .updateVlmLastModificationTime(featureGroup.getVendorLicenseModelId(), version);
426 mdcDataDebugMessage.debugExitMessage("VLM id, FG id", featureGroup
427 .getVendorLicenseModelId(), featureGroup.getId());
431 public FeatureGroupModel getFeatureGroupModel(FeatureGroupEntity featureGroup, String user) {
432 mdcDataDebugMessage.debugEntryMessage("VLM id, FG id",
433 featureGroup.getVendorLicenseModelId(), featureGroup.getId());
435 mdcDataDebugMessage.debugExitMessage("VLM id, FG id",
436 featureGroup.getVendorLicenseModelId(), featureGroup.getId());
437 return vendorLicenseFacade.getFeatureGroupModel(featureGroup, user);
441 public void deleteFeatureGroup(FeatureGroupEntity featureGroup, String user) {
442 mdcDataDebugMessage.debugEntryMessage("VLM id, FG id",
443 featureGroup.getVendorLicenseModelId(), featureGroup.getId());
445 Version version = VersioningUtil.resolveVersion(featureGroup.getVersion(),
446 getVersionInfo(featureGroup.getVendorLicenseModelId(), VersionableEntityAction.Write,
448 featureGroup.setVersion(version);
449 FeatureGroupEntity retrieved = featureGroupDao.get(featureGroup);
451 .validateEntityExistence(retrieved, featureGroup, VendorLicenseModelEntity.ENTITY_TYPE);
453 removeLicenseKeyGroupsToFeatureGroupsRef(retrieved.getLicenseKeyGroupIds(), featureGroup);
454 removeEntitlementPoolsToFeatureGroupsRef(retrieved.getEntitlementPoolIds(), featureGroup);
456 for (String licenceAgreementId : retrieved.getReferencingLicenseAgreements()) {
457 licenseAgreementDao.removeFeatureGroup(
458 new LicenseAgreementEntity(featureGroup.getVendorLicenseModelId(), version,
459 licenceAgreementId), featureGroup.getId());
462 featureGroupDao.delete(featureGroup);
463 UniqueValueUtil.deleteUniqueValue(VendorLicenseConstants.UniqueValues.FEATURE_GROUP_NAME,
464 retrieved.getVendorLicenseModelId(), retrieved.getVersion().toString(),
465 retrieved.getName());
467 vendorLicenseFacade.updateVlmLastModificationTime(featureGroup.getVendorLicenseModelId(),
468 featureGroup.getVersion());
471 .debugExitMessage("VLM id, FG id",
472 featureGroup.getVendorLicenseModelId(), featureGroup.getId());
476 public Collection<EntitlementPoolEntity> listEntitlementPools(String vlmId, Version version,
478 mdcDataDebugMessage.debugEntryMessage("VLM id", vlmId);
479 mdcDataDebugMessage.debugExitMessage("VLM id", vlmId);
480 return vendorLicenseFacade.listEntitlementPools(vlmId, version, user);
484 public EntitlementPoolEntity createEntitlementPool(EntitlementPoolEntity entitlementPool,
487 .debugEntryMessage("VLM id", entitlementPool.getVendorLicenseModelId());
489 .debugExitMessage("VLM id", entitlementPool.getVendorLicenseModelId());
490 validateCreateDate(entitlementPool);
491 return vendorLicenseFacade.createEntitlementPool(entitlementPool, user);
494 private void validateCreateDate(EntitlementPoolEntity entitlementPool){
495 mdcDataDebugMessage.debugEntryMessage("Start date and end date", entitlementPool.getStartDate
496 ()+" "+entitlementPool.getExpiryDate());
498 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy'T'HH:mm:ss'Z'");
500 entitlementPool.setStartDate(entitlementPool.getStartDate() != null ? (entitlementPool
501 .getStartDate().trim().length() != 0 ? entitlementPool.getStartDate()+"T00:00:00Z"
503 entitlementPool.setExpiryDate(entitlementPool.getExpiryDate() != null ? (entitlementPool
504 .getExpiryDate().trim().length() != 0 ? entitlementPool.getExpiryDate()+"T23:59:59Z"
507 if(entitlementPool.getStartDate() != null && entitlementPool.getExpiryDate() != null) {
508 if (LocalDate.parse(entitlementPool.getStartDate(), formatter).atStartOfDay().isBefore
509 (LocalDate.now().atStartOfDay()) ||
510 LocalDate.parse(entitlementPool.getExpiryDate(), formatter).atStartOfDay()
511 .isEqual(LocalDate.now().atStartOfDay()) ||
512 LocalDate.parse(entitlementPool.getExpiryDate(), formatter)
513 .isBefore(LocalDate.parse(entitlementPool.getStartDate(), formatter))) {
514 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
515 LoggerTragetServiceName.VALIDATE_DATE_RANGE,ErrorLevel.ERROR.name(),
516 LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.INVALID_VALUE);
517 throw new CoreException(
518 new InvalidDateErrorBuilder(entitlementPool.getVendorLicenseModelId())
523 if(entitlementPool.getStartDate() != null && entitlementPool.getExpiryDate() == null) {
524 if (LocalDate.parse(entitlementPool.getStartDate(), formatter).atStartOfDay().isBefore
525 (LocalDate.now().atStartOfDay())) {
526 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
527 LoggerTragetServiceName.VALIDATE_DATE_RANGE,ErrorLevel.ERROR.name(),
528 LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.INVALID_VALUE);
529 throw new CoreException(
530 new InvalidDateErrorBuilder(entitlementPool.getVendorLicenseModelId())
535 if(entitlementPool.getStartDate() == null && entitlementPool.getExpiryDate() != null) {
536 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
537 LoggerTragetServiceName.VALIDATE_DATE_RANGE,ErrorLevel.ERROR.name(),
538 LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.INVALID_VALUE);
539 throw new CoreException(
540 new InvalidDateErrorBuilder(entitlementPool.getVendorLicenseModelId())
545 mdcDataDebugMessage.debugExitMessage(null,null);
548 private void validateUpdateDate(EntitlementPoolEntity entitlementPool){
549 mdcDataDebugMessage.debugEntryMessage("Start date and end date", entitlementPool.getStartDate
550 ()+" "+entitlementPool.getExpiryDate());
552 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy'T'HH:mm:ss'Z'");
554 entitlementPool.setStartDate(entitlementPool.getStartDate() != null ? (entitlementPool
555 .getStartDate().trim().length() != 0 ? entitlementPool.getStartDate()+"T00:00:00Z"
557 entitlementPool.setExpiryDate(entitlementPool.getExpiryDate() != null ? (entitlementPool
558 .getExpiryDate().trim().length() != 0 ? entitlementPool.getExpiryDate()+"T23:59:59Z"
561 if(entitlementPool.getStartDate() != null && entitlementPool.getExpiryDate() != null) {
562 if (LocalDate.parse(entitlementPool.getExpiryDate(), formatter).atStartOfDay()
563 .isEqual(LocalDate.parse(entitlementPool.getStartDate(), formatter).atStartOfDay()) ||
564 LocalDate.parse(entitlementPool.getExpiryDate(), formatter)
565 .isBefore(LocalDate.parse(entitlementPool.getStartDate(), formatter))) {
566 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
567 LoggerTragetServiceName.VALIDATE_DATE_RANGE,ErrorLevel.ERROR.name(),
568 LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.INVALID_VALUE);
569 throw new CoreException(
570 new InvalidDateErrorBuilder(entitlementPool.getVendorLicenseModelId())
575 if(entitlementPool.getStartDate() == null && entitlementPool.getExpiryDate() != null) {
576 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
577 LoggerTragetServiceName.VALIDATE_DATE_RANGE,ErrorLevel.ERROR.name(),
578 LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.INVALID_VALUE);
579 throw new CoreException(
580 new InvalidDateErrorBuilder(entitlementPool.getVendorLicenseModelId())
585 mdcDataDebugMessage.debugExitMessage(null,null);
589 public void updateEntitlementPool(EntitlementPoolEntity entitlementPool, String user) {
590 mdcDataDebugMessage.debugEntryMessage("VLM id, EP id", entitlementPool
591 .getVendorLicenseModelId(), entitlementPool.getId());
593 validateUpdateDate(entitlementPool);
594 Version version = VersioningUtil.resolveVersion(entitlementPool.getVersion(),
595 getVersionInfo(entitlementPool.getVendorLicenseModelId(), VersionableEntityAction.Write,
598 .updateVlmLastModificationTime(entitlementPool.getVendorLicenseModelId(), version);
599 vendorLicenseFacade.updateEntitlementPool(entitlementPool, user);
601 mdcDataDebugMessage.debugExitMessage("VLM id, EP id", entitlementPool
602 .getVendorLicenseModelId(), entitlementPool.getId());
606 public EntitlementPoolEntity getEntitlementPool(EntitlementPoolEntity entitlementPool,
608 mdcDataDebugMessage.debugEntryMessage("VLM id, EP id", entitlementPool
609 .getVendorLicenseModelId(), entitlementPool.getId());
611 entitlementPool.setVersion(VersioningUtil.resolveVersion(entitlementPool.getVersion(),
612 getVersionInfo(entitlementPool.getVendorLicenseModelId(), VersionableEntityAction.Read,
615 EntitlementPoolEntity retrieved = entitlementPoolDao.get(entitlementPool);
617 .validateEntityExistence(retrieved, entitlementPool, VendorLicenseModelEntity.ENTITY_TYPE);
619 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy'T'HH:mm:ss'Z'");
620 DateTimeFormatter targetFormatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
621 if(retrieved.getStartDate() != null){
622 retrieved.setStartDate(LocalDate.parse(retrieved.getStartDate(),formatter).format
626 if(retrieved.getExpiryDate() != null){
627 retrieved.setExpiryDate(LocalDate.parse(retrieved.getExpiryDate(),formatter).format
631 mdcDataDebugMessage.debugExitMessage("VLM id, EP id", entitlementPool
632 .getVendorLicenseModelId(), entitlementPool.getId());
637 public void deleteEntitlementPool(EntitlementPoolEntity entitlementPool, String user) {
638 mdcDataDebugMessage.debugEntryMessage("VLM id, EP id", entitlementPool
639 .getVendorLicenseModelId(), entitlementPool.getId());
641 Version version = VersioningUtil.resolveVersion(entitlementPool.getVersion(),
642 getVersionInfo(entitlementPool.getVendorLicenseModelId(), VersionableEntityAction.Write,
644 entitlementPool.setVersion(version);
646 EntitlementPoolEntity retrieved = entitlementPoolDao.get(entitlementPool);
648 .validateEntityExistence(retrieved, entitlementPool, VendorLicenseModelEntity.ENTITY_TYPE);
650 for (String referencingFeatureGroupId : retrieved.getReferencingFeatureGroups()) {
651 featureGroupDao.removeEntitlementPool(
652 new FeatureGroupEntity(entitlementPool.getVendorLicenseModelId(), version,
653 referencingFeatureGroupId), entitlementPool.getId());
656 deleteChildLimits(entitlementPool.getVendorLicenseModelId(), entitlementPool.getVersion(), entitlementPool.getId(), user);
658 entitlementPoolDao.delete(entitlementPool);
660 UniqueValueUtil.deleteUniqueValue(VendorLicenseConstants.UniqueValues.ENTITLEMENT_POOL_NAME,
661 retrieved.getVendorLicenseModelId(), retrieved.getVersion().toString(),
662 retrieved.getName());
664 vendorLicenseFacade.updateVlmLastModificationTime(entitlementPool.getVendorLicenseModelId(),
665 entitlementPool.getVersion());
667 mdcDataDebugMessage.debugExitMessage("VLM id, EP id", entitlementPool
668 .getVendorLicenseModelId(), entitlementPool.getId());
671 private void deleteChildLimits(String vlmId, Version version, String epLkgId, String user) {
672 Optional<Collection<LimitEntity>> limitEntities = Optional.ofNullable(
673 listLimits(vlmId, version, epLkgId, user));
674 limitEntities.ifPresent(entities->
675 entities.forEach(entity->
676 deleteLimit(entity, user)));
680 public Collection<LicenseKeyGroupEntity> listLicenseKeyGroups(String vlmId, Version version,
682 mdcDataDebugMessage.debugEntryMessage("VLM id", vlmId);
683 mdcDataDebugMessage.debugExitMessage("VLM id", vlmId);
684 return vendorLicenseFacade.listLicenseKeyGroups(vlmId, version, user);
688 public LicenseKeyGroupEntity createLicenseKeyGroup(LicenseKeyGroupEntity licenseKeyGroup,
691 .debugEntryMessage("VLM id", licenseKeyGroup.getVendorLicenseModelId());
693 mdcDataDebugMessage.debugExitMessage("VLM id", licenseKeyGroup
694 .getVendorLicenseModelId());
695 return vendorLicenseFacade.createLicenseKeyGroup(licenseKeyGroup, user);
699 public void updateLicenseKeyGroup(LicenseKeyGroupEntity licenseKeyGroup, String user) {
700 mdcDataDebugMessage.debugEntryMessage("VLM id, LKG id", licenseKeyGroup
701 .getVendorLicenseModelId(), licenseKeyGroup.getId());
703 Version version = VersioningUtil.resolveVersion(licenseKeyGroup.getVersion(),
704 getVersionInfo(licenseKeyGroup.getVendorLicenseModelId(), VersionableEntityAction.Write,
707 .updateVlmLastModificationTime(licenseKeyGroup.getVendorLicenseModelId(), version);
709 vendorLicenseFacade.updateLicenseKeyGroup(licenseKeyGroup, user);
711 mdcDataDebugMessage.debugExitMessage("VLM id, LKG id", licenseKeyGroup
712 .getVendorLicenseModelId(), licenseKeyGroup.getId());
716 public LicenseKeyGroupEntity getLicenseKeyGroup(LicenseKeyGroupEntity licenseKeyGroup,
718 mdcDataDebugMessage.debugEntryMessage("VLM id, LKG id", licenseKeyGroup
719 .getVendorLicenseModelId(), licenseKeyGroup.getId());
721 licenseKeyGroup.setVersion(VersioningUtil.resolveVersion(licenseKeyGroup.getVersion(),
722 getVersionInfo(licenseKeyGroup.getVendorLicenseModelId(), VersionableEntityAction.Read,
725 LicenseKeyGroupEntity retrieved = licenseKeyGroupDao.get(licenseKeyGroup);
727 .validateEntityExistence(retrieved, licenseKeyGroup, VendorLicenseModelEntity.ENTITY_TYPE);
729 mdcDataDebugMessage.debugExitMessage("VLM id, LKG id", licenseKeyGroup
730 .getVendorLicenseModelId(), licenseKeyGroup.getId());
735 public void deleteLicenseKeyGroup(LicenseKeyGroupEntity licenseKeyGroup, String user) {
736 mdcDataDebugMessage.debugEntryMessage("VLM id, LKG id", licenseKeyGroup
737 .getVendorLicenseModelId(), licenseKeyGroup.getId());
739 Version version = VersioningUtil.resolveVersion(licenseKeyGroup.getVersion(),
740 getVersionInfo(licenseKeyGroup.getVendorLicenseModelId(), VersionableEntityAction.Write,
742 licenseKeyGroup.setVersion(version);
744 LicenseKeyGroupEntity retrieved = licenseKeyGroupDao.get(licenseKeyGroup);
746 .validateEntityExistence(retrieved, licenseKeyGroup, VendorLicenseModelEntity.ENTITY_TYPE);
748 for (String referencingFeatureGroupId : retrieved.getReferencingFeatureGroups()) {
749 featureGroupDao.removeLicenseKeyGroup(
750 new FeatureGroupEntity(licenseKeyGroup.getVendorLicenseModelId(), version,
751 referencingFeatureGroupId), licenseKeyGroup.getId());
754 deleteChildLimits(licenseKeyGroup.getVendorLicenseModelId(), licenseKeyGroup.getVersion(), licenseKeyGroup.getId(), user);
756 licenseKeyGroupDao.delete(licenseKeyGroup);
758 UniqueValueUtil.deleteUniqueValue(VendorLicenseConstants.UniqueValues.LICENSE_KEY_GROUP_NAME,
759 retrieved.getVendorLicenseModelId(), retrieved.getVersion().toString(),
760 retrieved.getName());
762 vendorLicenseFacade.updateVlmLastModificationTime(licenseKeyGroup.getVendorLicenseModelId(),
763 licenseKeyGroup.getVersion());
765 mdcDataDebugMessage.debugExitMessage("VLM id, LKG id", licenseKeyGroup
766 .getVendorLicenseModelId(), licenseKeyGroup.getId());
770 public LimitEntity createLimit(LimitEntity limit, String user) {
772 .debugEntryMessage("VLM id", limit.getVendorLicenseModelId(), "EP/LKGId", limit
775 .debugExitMessage("VLM id", limit.getVendorLicenseModelId(), "EP/LKGId", limit
777 validateLimit(limit, user);
778 return vendorLicenseFacade.createLimit(limit, user);
781 private void validateLimit(LimitEntity limit, String user) {
782 Version version = VersioningUtil.resolveVersion(limit.getVersion(),
783 getVersionInfo(limit.getVendorLicenseModelId(), VersionableEntityAction.Write,
785 Collection<LimitEntity> limitList = listLimits(limit.getVendorLicenseModelId(),version
786 ,limit.getEpLkgId(), user);
788 if (!isLimitNameUnique(limitList,limit.getName(), limit.getType(), limit.getId())) {
789 final ErrorCode duplicateLimitNameErrorBuilder =
790 LimitErrorBuilder.getDuplicateNameErrorbuilder(limit.getName(), limit.getType().name());
791 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
792 LoggerServiceName.Create_LIMIT.toString(), ErrorLevel.ERROR.name(),
793 LoggerErrorCode.DATA_ERROR.getErrorCode(),
794 duplicateLimitNameErrorBuilder.message());
795 throw new CoreException(duplicateLimitNameErrorBuilder);
799 private boolean isLimitNameUnique(Collection<LimitEntity> limitList, String name, LimitType
801 for (LimitEntity limit : limitList) {
802 if(limit.getName().equalsIgnoreCase(name) &&
803 limit.getType().name().equalsIgnoreCase(type.name())) {
804 if(id != null && limit.getId().equals(id)){
814 public Collection<LimitEntity> listLimits(String vlmId, Version version, String epLkgId,
816 mdcDataDebugMessage.debugEntryMessage("VLM id", vlmId, "EP/LKGId", epLkgId);
817 mdcDataDebugMessage.debugExitMessage("VLM id", vlmId, "EP/LKGId", epLkgId);
818 return vendorLicenseFacade.listLimits(vlmId, version, epLkgId, user);
822 public void deleteLimit(LimitEntity limitEntity, String user) {
823 mdcDataDebugMessage.debugEntryMessage("VLM id, EP id, Limit Id", limitEntity
824 .getVendorLicenseModelId(), limitEntity.getEpLkgId(), limitEntity.getId());
826 Version version = VersioningUtil.resolveVersion(limitEntity.getVersion(),
827 getVersionInfo(limitEntity.getVendorLicenseModelId(), VersionableEntityAction.Write,
829 limitEntity.setVersion(version);
831 if(!isLimitPresent(limitEntity)){
833 .validateEntityExistence(null, limitEntity, VendorLicenseModelEntity.ENTITY_TYPE);
835 LimitEntity retrieved = limitDao.get(limitEntity);
837 .validateEntityExistence(retrieved, limitEntity, VendorLicenseModelEntity.ENTITY_TYPE);
839 limitDao.delete(limitEntity);
841 vendorLicenseFacade.updateVlmLastModificationTime(limitEntity.getVendorLicenseModelId(),
842 limitEntity.getVersion());
844 mdcDataDebugMessage.debugExitMessage("VLM id, EP id, Limit Id", limitEntity
845 .getVendorLicenseModelId(), limitEntity.getEpLkgId(), limitEntity.getId());
849 public void updateLimit(LimitEntity limit, String user) {
851 .debugEntryMessage("VLM id", limit.getVendorLicenseModelId(), "EP/LKGId", limit
853 getLimit(limit,user);
854 validateLimit(limit, user);
855 vendorLicenseFacade.updateLimit(limit, user);
857 .debugExitMessage("VLM id", limit.getVendorLicenseModelId(), "EP/LKGId", limit
861 private boolean isLimitPresent(LimitEntity limit) {
862 return limitDao.isLimitPresent(limit);
866 public LimitEntity getLimit(LimitEntity limitEntity,
868 mdcDataDebugMessage.debugEntryMessage("VLM id", limitEntity.getVendorLicenseModelId(),
869 "EP/LKGId", limitEntity.getEpLkgId());
871 limitEntity.setVersion(VersioningUtil.resolveVersion(limitEntity.getVersion(),
872 getVersionInfo(limitEntity.getVendorLicenseModelId(), VersionableEntityAction.Read,
874 if(!isLimitPresent(limitEntity)){
876 .validateEntityExistence(null, limitEntity, VendorLicenseModelEntity.ENTITY_TYPE);
878 LimitEntity retrieved = limitDao.get(limitEntity);
880 .validateEntityExistence(retrieved, limitEntity, VendorLicenseModelEntity.ENTITY_TYPE);
882 mdcDataDebugMessage.debugExitMessage("VLM id", limitEntity.getVendorLicenseModelId(),
883 "EP/LKGId", limitEntity.getEpLkgId());
887 private void addFeatureGroupsToLicenseAgreementRef(Set<String> featureGroupIds,
888 LicenseAgreementEntity licenseAgreement) {
889 if (featureGroupIds != null) {
890 for (String featureGroupId : featureGroupIds) {
891 featureGroupDao.addReferencingLicenseAgreement(
892 new FeatureGroupEntity(licenseAgreement.getVendorLicenseModelId(),
893 licenseAgreement.getVersion(), featureGroupId), licenseAgreement.getId());
898 private void removeFeatureGroupsToLicenseAgreementRef(Set<String> featureGroupIds,
899 LicenseAgreementEntity licenseAgreement) {
900 if (featureGroupIds != null) {
901 for (String featureGroupId : featureGroupIds) {
902 featureGroupDao.removeReferencingLicenseAgreement(
903 new FeatureGroupEntity(licenseAgreement.getVendorLicenseModelId(),
904 licenseAgreement.getVersion(), featureGroupId), licenseAgreement.getId());
909 private void addLicenseKeyGroupsToFeatureGroupsRef(Set<String> licenseKeyGroupIds,
910 FeatureGroupEntity featureGroup) {
911 if (licenseKeyGroupIds != null) {
912 for (String licenseKeyGroupId : licenseKeyGroupIds) {
913 licenseKeyGroupDao.addReferencingFeatureGroup(
914 new LicenseKeyGroupEntity(featureGroup.getVendorLicenseModelId(),
915 featureGroup.getVersion(), licenseKeyGroupId), featureGroup.getId());
920 private void removeLicenseKeyGroupsToFeatureGroupsRef(Set<String> licenseKeyGroupIds,
921 FeatureGroupEntity featureGroup) {
922 if (licenseKeyGroupIds != null) {
923 for (String licenseKeyGroupId : licenseKeyGroupIds) {
924 licenseKeyGroupDao.removeReferencingFeatureGroup(
925 new LicenseKeyGroupEntity(featureGroup.getVendorLicenseModelId(),
926 featureGroup.getVersion(), licenseKeyGroupId), featureGroup.getId());
931 private void addEntitlementPoolsToFeatureGroupsRef(Set<String> entitlementPoolIds,
932 FeatureGroupEntity featureGroup) {
933 if (entitlementPoolIds != null) {
934 for (String entitlementPoolId : entitlementPoolIds) {
935 entitlementPoolDao.addReferencingFeatureGroup(
936 new EntitlementPoolEntity(featureGroup.getVendorLicenseModelId(),
937 featureGroup.getVersion(), entitlementPoolId), featureGroup.getId());
942 private void removeEntitlementPoolsToFeatureGroupsRef(Set<String> entitlementPoolIds,
943 FeatureGroupEntity featureGroup) {
944 if (entitlementPoolIds != null) {
945 for (String entitlementPoolId : entitlementPoolIds) {
946 entitlementPoolDao.removeReferencingFeatureGroup(
947 new EntitlementPoolEntity(featureGroup.getVendorLicenseModelId(),
948 featureGroup.getVersion(), entitlementPoolId), featureGroup.getId());
953 private VersionInfo getVersionInfo(String vendorLicenseModelId, VersionableEntityAction action,
955 return vendorLicenseFacade.getVersionInfo(vendorLicenseModelId, action, user);