2 * Copyright © 2016-2017 European Support Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.openecomp.sdc.vendorlicense.impl;
19 import org.apache.commons.collections.CollectionUtils;
20 import org.openecomp.core.util.UniqueValueUtil;
21 import org.openecomp.core.utilities.CommonMethods;
22 import org.openecomp.sdc.common.errors.CoreException;
23 import org.openecomp.sdc.common.errors.ErrorCode;
24 import org.openecomp.sdc.datatypes.error.ErrorLevel;
25 import org.openecomp.sdc.logging.context.impl.MdcDataDebugMessage;
26 import org.openecomp.sdc.logging.context.impl.MdcDataErrorMessage;
27 import org.openecomp.sdc.logging.types.LoggerConstants;
28 import org.openecomp.sdc.logging.types.LoggerErrorCode;
29 import org.openecomp.sdc.logging.types.LoggerErrorDescription;
30 import org.openecomp.sdc.logging.types.LoggerServiceName;
31 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
32 import org.openecomp.sdc.vendorlicense.VendorLicenseConstants;
33 import org.openecomp.sdc.vendorlicense.VendorLicenseManager;
34 import org.openecomp.sdc.vendorlicense.dao.EntitlementPoolDao;
35 import org.openecomp.sdc.vendorlicense.dao.FeatureGroupDao;
36 import org.openecomp.sdc.vendorlicense.dao.LicenseAgreementDao;
37 import org.openecomp.sdc.vendorlicense.dao.LicenseKeyGroupDao;
38 import org.openecomp.sdc.vendorlicense.dao.LimitDao;
39 import org.openecomp.sdc.vendorlicense.dao.VendorLicenseModelDao;
40 import org.openecomp.sdc.vendorlicense.dao.types.EntitlementPoolEntity;
41 import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity;
42 import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupModel;
43 import org.openecomp.sdc.vendorlicense.dao.types.LicenseAgreementEntity;
44 import org.openecomp.sdc.vendorlicense.dao.types.LicenseAgreementModel;
45 import org.openecomp.sdc.vendorlicense.dao.types.LicenseKeyGroupEntity;
46 import org.openecomp.sdc.vendorlicense.dao.types.LimitEntity;
47 import org.openecomp.sdc.vendorlicense.dao.types.LimitType;
48 import org.openecomp.sdc.vendorlicense.dao.types.VendorLicenseModelEntity;
49 import org.openecomp.sdc.vendorlicense.errors.InvalidDateErrorBuilder;
50 import org.openecomp.sdc.vendorlicense.errors.LimitErrorBuilder;
51 import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacade;
52 import org.openecomp.sdc.versioning.VersioningUtil;
53 import org.openecomp.sdc.versioning.dao.types.Version;
55 import java.time.LocalDate;
56 import java.time.format.DateTimeFormatter;
57 import java.util.Collection;
58 import java.util.Objects;
59 import java.util.Optional;
62 public class VendorLicenseManagerImpl implements VendorLicenseManager {
63 private VendorLicenseFacade vendorLicenseFacade;
64 private VendorLicenseModelDao vendorLicenseModelDao;
65 private LicenseAgreementDao licenseAgreementDao;
66 private FeatureGroupDao featureGroupDao;
67 private EntitlementPoolDao entitlementPoolDao;
68 private LicenseKeyGroupDao licenseKeyGroupDao;
69 private LimitDao limitDao;
70 private static final String VLM_ID = "VLM id";
71 private static final String EP_LKGID = "EP/LKGId";
72 private static final String VLM_ID_LKG_ID = "VLM id, LKG id";
73 private static final String VLM_ID_LA_ID = "VLM id, LA id";
74 private static final String VLM_ID_FG_ID = "VLM id, FG id";
75 private static final String VLM_ID_EP_ID = "VLM id, EP id";
76 private static final String EP_POOL_START_TIME = "T00:00:00Z";
77 private static final String EP_POOL_EXPIRY_TIME = "T23:59:59Z";
78 private static final DateTimeFormatter FORMATTER
79 = DateTimeFormatter.ofPattern("MM/dd/yyyy'T'HH:mm:ss'Z'");
82 private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
84 public VendorLicenseManagerImpl(VendorLicenseFacade vendorLicenseFacade,
85 VendorLicenseModelDao vendorLicenseModelDao,
86 LicenseAgreementDao licenseAgreementDao,
87 FeatureGroupDao featureGroupDao,
88 EntitlementPoolDao entitlementPoolDao,
89 LicenseKeyGroupDao licenseKeyGroupDao,
91 this.vendorLicenseFacade = vendorLicenseFacade;
92 this.vendorLicenseModelDao = vendorLicenseModelDao;
93 this.licenseAgreementDao = licenseAgreementDao;
94 this.featureGroupDao = featureGroupDao;
95 this.entitlementPoolDao = entitlementPoolDao;
96 this.licenseKeyGroupDao = licenseKeyGroupDao;
97 this.limitDao = limitDao;
102 public void validate(String vendorLicenseModelId, Version version) {
103 mdcDataDebugMessage.debugEntryMessage(VLM_ID, vendorLicenseModelId);
105 vendorLicenseFacade.validate(vendorLicenseModelId, version);
107 mdcDataDebugMessage.debugExitMessage(VLM_ID, vendorLicenseModelId);
111 public VendorLicenseModelEntity createVendorLicenseModel(
112 VendorLicenseModelEntity vendorLicenseModelEntity) {
113 mdcDataDebugMessage.debugEntryMessage(null);
115 vendorLicenseModelDao.create(vendorLicenseModelEntity);
116 mdcDataDebugMessage.debugExitMessage(null);
118 return vendorLicenseModelEntity;
122 public void updateVendorLicenseModel(VendorLicenseModelEntity vendorLicenseModelEntity) {
123 mdcDataDebugMessage.debugEntryMessage(VLM_ID, vendorLicenseModelEntity.getId());
125 String existingVendorName = vendorLicenseModelDao.get(vendorLicenseModelEntity).getVendorName();
127 updateUniqueName(VendorLicenseConstants.UniqueValues.VENDOR_NAME, existingVendorName,
128 vendorLicenseModelEntity.getVendorName());
129 vendorLicenseModelDao.update(vendorLicenseModelEntity);
131 mdcDataDebugMessage.debugExitMessage(VLM_ID, vendorLicenseModelEntity.getId());
135 public VendorLicenseModelEntity getVendorLicenseModel(String vlmId, Version version) {
136 return vendorLicenseFacade.getVendorLicenseModel(vlmId, version);
140 public void deleteVendorLicenseModel(String vlmId, Version version) {
141 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
142 LoggerTragetServiceName.DELETE_ENTITY, ErrorLevel.ERROR.name(),
143 LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.UNSUPPORTED_OPERATION);
144 throw new UnsupportedOperationException(VendorLicenseConstants.UNSUPPORTED_OPERATION_ERROR);
148 public Collection<LicenseAgreementEntity> listLicenseAgreements(String vlmId, Version version) {
149 mdcDataDebugMessage.debugEntryMessage(VLM_ID, vlmId);
150 mdcDataDebugMessage.debugExitMessage(VLM_ID, vlmId);
151 return licenseAgreementDao.list(new LicenseAgreementEntity(vlmId, version, null));
155 public LicenseAgreementEntity createLicenseAgreement(LicenseAgreementEntity licenseAgreement) {
157 .debugEntryMessage(VLM_ID, licenseAgreement.getVendorLicenseModelId());
159 .debugExitMessage(VLM_ID, licenseAgreement.getVendorLicenseModelId());
160 return vendorLicenseFacade.createLicenseAgreement(licenseAgreement);
164 public void updateLicenseAgreement(LicenseAgreementEntity licenseAgreement,
165 Set<String> addedFeatureGroupIds,
166 Set<String> removedFeatureGroupIds) {
167 mdcDataDebugMessage.debugEntryMessage(VLM_ID_LA_ID, licenseAgreement
168 .getVendorLicenseModelId(), licenseAgreement.getId());
170 LicenseAgreementEntity retrieved = licenseAgreementDao.get(licenseAgreement);
172 .validateEntityExistence(retrieved, licenseAgreement, VendorLicenseModelEntity.ENTITY_TYPE);
173 VersioningUtil.validateContainedEntitiesExistence(new FeatureGroupEntity().getEntityType(),
174 removedFeatureGroupIds, retrieved, retrieved.getFeatureGroupIds());
175 VersioningUtil.validateEntitiesExistence(addedFeatureGroupIds,
176 new FeatureGroupEntity(licenseAgreement.getVendorLicenseModelId(),
177 licenseAgreement.getVersion(),
179 featureGroupDao, VendorLicenseModelEntity.ENTITY_TYPE);
181 updateUniqueName(VendorLicenseConstants.UniqueValues.LICENSE_AGREEMENT_NAME,
182 retrieved.getName(), licenseAgreement.getName(), licenseAgreement.getVendorLicenseModelId(),
183 licenseAgreement.getVersion().getId());
184 licenseAgreementDao.updateColumnsAndDeltaFeatureGroupIds(licenseAgreement, addedFeatureGroupIds,
185 removedFeatureGroupIds);
187 addFeatureGroupsToLicenseAgreementRef(addedFeatureGroupIds, licenseAgreement);
188 removeFeatureGroupsToLicenseAgreementRef(removedFeatureGroupIds, licenseAgreement);
190 mdcDataDebugMessage.debugExitMessage(VLM_ID_LA_ID, licenseAgreement
191 .getVendorLicenseModelId(), licenseAgreement.getId());
195 public LicenseAgreementModel getLicenseAgreementModel(String vlmId, Version version,
196 String licenseAgreementId) {
198 mdcDataDebugMessage.debugEntryMessage(VLM_ID_LA_ID, vlmId, licenseAgreementId);
199 mdcDataDebugMessage.debugExitMessage(VLM_ID_LA_ID, vlmId, licenseAgreementId);
200 return vendorLicenseFacade.getLicenseAgreementModel(vlmId, version, licenseAgreementId);
204 public void deleteLicenseAgreement(String vlmId, Version version, String licenseAgreementId) {
205 mdcDataDebugMessage.debugEntryMessage(VLM_ID_LA_ID, vlmId, licenseAgreementId);
207 LicenseAgreementEntity input =
208 new LicenseAgreementEntity(vlmId, version, licenseAgreementId);
209 LicenseAgreementEntity retrieved = licenseAgreementDao.get(input);
210 VersioningUtil.validateEntityExistence(retrieved, input, VendorLicenseModelEntity.ENTITY_TYPE);
212 removeFeatureGroupsToLicenseAgreementRef(retrieved.getFeatureGroupIds(), retrieved);
214 licenseAgreementDao.delete(retrieved);
216 deleteUniqueName(VendorLicenseConstants.UniqueValues.LICENSE_AGREEMENT_NAME,
217 retrieved.getVendorLicenseModelId(), retrieved.getVersion().toString(),
218 retrieved.getName());
220 mdcDataDebugMessage.debugExitMessage(VLM_ID_LA_ID, vlmId, licenseAgreementId);
224 public Collection<FeatureGroupEntity> listFeatureGroups(String vlmId, Version version) {
225 mdcDataDebugMessage.debugEntryMessage(VLM_ID, vlmId);
226 mdcDataDebugMessage.debugExitMessage(VLM_ID, vlmId);
227 return vendorLicenseFacade.listFeatureGroups(vlmId, version);
231 public FeatureGroupEntity createFeatureGroup(FeatureGroupEntity featureGroup) {
233 .debugEntryMessage(VLM_ID, featureGroup.getVendorLicenseModelId());
234 mdcDataDebugMessage.debugExitMessage(VLM_ID, featureGroup.getId());
235 return vendorLicenseFacade.createFeatureGroup(featureGroup);
239 public void updateFeatureGroup(FeatureGroupEntity featureGroup,
240 Set<String> addedLicenseKeyGroups,
241 Set<String> removedLicenseKeyGroups,
242 Set<String> addedEntitlementPools,
243 Set<String> removedEntitlementPools) {
244 mdcDataDebugMessage.debugEntryMessage(VLM_ID_FG_ID, featureGroup
245 .getVendorLicenseModelId(), featureGroup.getId());
247 FeatureGroupEntity retrieved = featureGroupDao.get(featureGroup);
249 .validateEntityExistence(retrieved, featureGroup, VendorLicenseModelEntity.ENTITY_TYPE);
251 VersioningUtil.validateContainedEntitiesExistence(new LicenseKeyGroupEntity().getEntityType(),
252 removedLicenseKeyGroups, retrieved, retrieved.getLicenseKeyGroupIds());
253 VersioningUtil.validateContainedEntitiesExistence(new EntitlementPoolEntity().getEntityType(),
254 removedEntitlementPools, retrieved, retrieved.getEntitlementPoolIds());
256 VersioningUtil.validateEntitiesExistence(addedLicenseKeyGroups,
257 new LicenseKeyGroupEntity(featureGroup.getVendorLicenseModelId(), featureGroup.getVersion(),
259 licenseKeyGroupDao, VendorLicenseModelEntity.ENTITY_TYPE);
260 VersioningUtil.validateEntitiesExistence(addedEntitlementPools,
261 new EntitlementPoolEntity(featureGroup.getVendorLicenseModelId(), featureGroup.getVersion(),
263 entitlementPoolDao, VendorLicenseModelEntity.ENTITY_TYPE);
265 updateUniqueName(VendorLicenseConstants.UniqueValues.FEATURE_GROUP_NAME,
266 retrieved.getName(), featureGroup.getName(), featureGroup.getVendorLicenseModelId(),
267 featureGroup.getVersion().getId());
269 addLicenseKeyGroupsToFeatureGroupsRef(addedLicenseKeyGroups, featureGroup);
270 removeLicenseKeyGroupsToFeatureGroupsRef(removedLicenseKeyGroups, featureGroup);
271 addEntitlementPoolsToFeatureGroupsRef(addedEntitlementPools, featureGroup);
272 removeEntitlementPoolsToFeatureGroupsRef(removedEntitlementPools, featureGroup);
274 featureGroupDao.updateFeatureGroup(featureGroup, addedEntitlementPools, removedEntitlementPools,
275 addedLicenseKeyGroups, removedLicenseKeyGroups);
277 updateEpLkgOnMrnChange(featureGroup, addedLicenseKeyGroups, addedEntitlementPools, retrieved);
279 mdcDataDebugMessage.debugExitMessage(VLM_ID_FG_ID, featureGroup
280 .getVendorLicenseModelId(), featureGroup.getId());
284 * If MRN is updated in feature group then update all linked EPs and Lkgs with new versionUuId
285 * @param featureGroup - Feature Group entity which is requested for update
286 * @param addedLicenseKeyGroups - LicenseKeyGroups added with Feature Group
287 * @param addedEntitlementPools - EntitlementPools added with Feature Group
288 * @param retrieved - Feature Group entity fetched from database
290 private void updateEpLkgOnMrnChange(FeatureGroupEntity featureGroup,
291 Set<String> addedLicenseKeyGroups,
292 Set<String> addedEntitlementPools,
293 FeatureGroupEntity retrieved) {
294 if (Objects.nonNull(retrieved.getManufacturerReferenceNumber())
295 && !retrieved.getManufacturerReferenceNumber().equals(featureGroup
296 .getManufacturerReferenceNumber())) {
297 if (CollectionUtils.isEmpty(addedEntitlementPools)) {
298 updateEntitlementPool(featureGroup, retrieved.getEntitlementPoolIds());
300 updateEntitlementPool(featureGroup, addedEntitlementPools);
303 if (CollectionUtils.isEmpty(addedLicenseKeyGroups)) {
304 updateLicenseKeyGroup(featureGroup, retrieved.getLicenseKeyGroupIds());
306 updateLicenseKeyGroup(featureGroup, addedLicenseKeyGroups);
311 private void updateEntitlementPool(FeatureGroupEntity featureGroup,
312 Set<String> entitlementPoolIds) {
313 for (String epId: entitlementPoolIds) {
314 final EntitlementPoolEntity entitlementPoolEntity = entitlementPoolDao
315 .get(new EntitlementPoolEntity(featureGroup.getVendorLicenseModelId(), featureGroup
316 .getVersion(), epId));
317 if (Objects.nonNull(entitlementPoolEntity)) {
318 entitlementPoolEntity.setVersionUuId(CommonMethods.nextUuId());
319 entitlementPoolDao.update(entitlementPoolEntity);
324 private void updateLicenseKeyGroup(FeatureGroupEntity featureGroup,
325 Set<String> licenseKeyGroupIds) {
326 for (String lkgId: licenseKeyGroupIds) {
327 final LicenseKeyGroupEntity licenseKeyGroupEntity = licenseKeyGroupDao
328 .get(new LicenseKeyGroupEntity(featureGroup.getVendorLicenseModelId(),
329 featureGroup.getVersion(), lkgId));
330 if (Objects.nonNull(licenseKeyGroupEntity)) {
331 licenseKeyGroupEntity.setVersionUuId(CommonMethods.nextUuId());
332 licenseKeyGroupDao.update(licenseKeyGroupEntity);
338 public FeatureGroupModel getFeatureGroupModel(FeatureGroupEntity featureGroup) {
339 mdcDataDebugMessage.debugEntryMessage(VLM_ID_FG_ID,
340 featureGroup.getVendorLicenseModelId(), featureGroup.getId());
342 mdcDataDebugMessage.debugExitMessage(VLM_ID_FG_ID,
343 featureGroup.getVendorLicenseModelId(), featureGroup.getId());
344 return vendorLicenseFacade.getFeatureGroupModel(featureGroup);
348 public void deleteFeatureGroup(FeatureGroupEntity featureGroup) {
349 mdcDataDebugMessage.debugEntryMessage(VLM_ID_FG_ID,
350 featureGroup.getVendorLicenseModelId(), featureGroup.getId());
352 FeatureGroupEntity retrieved = featureGroupDao.get(featureGroup);
354 .validateEntityExistence(retrieved, featureGroup, VendorLicenseModelEntity.ENTITY_TYPE);
356 removeLicenseKeyGroupsToFeatureGroupsRef(retrieved.getLicenseKeyGroupIds(), featureGroup);
357 removeEntitlementPoolsToFeatureGroupsRef(retrieved.getEntitlementPoolIds(), featureGroup);
359 for (String licenceAgreementId : retrieved.getReferencingLicenseAgreements()) {
360 licenseAgreementDao.removeFeatureGroup(
361 new LicenseAgreementEntity(featureGroup.getVendorLicenseModelId(),
362 featureGroup.getVersion(),
363 licenceAgreementId), featureGroup.getId());
366 featureGroupDao.delete(featureGroup);
368 deleteUniqueName(VendorLicenseConstants.UniqueValues.FEATURE_GROUP_NAME,
369 retrieved.getVendorLicenseModelId(), retrieved.getVersion().toString(),
370 retrieved.getName());
373 .debugExitMessage(VLM_ID_FG_ID,
374 featureGroup.getVendorLicenseModelId(), featureGroup.getId());
378 public Collection<EntitlementPoolEntity> listEntitlementPools(String vlmId, Version version) {
379 mdcDataDebugMessage.debugEntryMessage(VLM_ID, vlmId);
380 mdcDataDebugMessage.debugExitMessage(VLM_ID, vlmId);
381 return vendorLicenseFacade.listEntitlementPools(vlmId, version);
385 public EntitlementPoolEntity createEntitlementPool(EntitlementPoolEntity entitlementPool) {
387 .debugEntryMessage(VLM_ID, entitlementPool.getVendorLicenseModelId());
389 .debugExitMessage(VLM_ID, entitlementPool.getVendorLicenseModelId());
391 entitlementPool.setStartDate(entitlementPool.getStartDate() != null ? (entitlementPool
392 .getStartDate().trim().length() != 0 ? entitlementPool.getStartDate() + EP_POOL_START_TIME
394 entitlementPool.setExpiryDate(entitlementPool.getExpiryDate() != null ? (entitlementPool
395 .getExpiryDate().trim().length() != 0 ? entitlementPool.getExpiryDate() + EP_POOL_EXPIRY_TIME
398 validateCreateDate(entitlementPool.getStartDate(), entitlementPool.getExpiryDate(),
399 entitlementPool.getVendorLicenseModelId());
400 return vendorLicenseFacade.createEntitlementPool(entitlementPool);
403 private void validateCreateDate(String startDate, String expiryDate,
404 String vendorLicenseModelId) {
405 mdcDataDebugMessage.debugEntryMessage("Start date and end date", startDate
408 LocalDate parsedStartDate = parseLocalDate(startDate);
409 LocalDate parsedExpiryDate = parseLocalDate(expiryDate);
412 validateIfStartAndExpiryDateIsNotNull(startDate, expiryDate,
413 vendorLicenseModelId, parsedStartDate, parsedExpiryDate);
415 if (startDate != null && expiryDate == null
416 && parsedStartDate.atStartOfDay().isBefore
417 (LocalDate.now().atStartOfDay())) {
418 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
419 LoggerTragetServiceName.VALIDATE_DATE_RANGE, ErrorLevel.ERROR.name(),
420 LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.INVALID_VALUE);
421 throw new CoreException(
422 new InvalidDateErrorBuilder(vendorLicenseModelId)
426 if (startDate == null && expiryDate != null) {
427 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
428 LoggerTragetServiceName.VALIDATE_DATE_RANGE, ErrorLevel.ERROR.name(),
429 LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.INVALID_VALUE);
430 throw new CoreException(
431 new InvalidDateErrorBuilder(vendorLicenseModelId)
436 mdcDataDebugMessage.debugExitMessage(null);
439 private void validateIfStartAndExpiryDateIsNotNull(String startDate, String expiryDate,
440 String vendorLicenseModelId,
441 LocalDate parsedStartDate,
442 LocalDate parsedExpiryDate) {
443 if (startDate != null && expiryDate != null
444 && isValidatStartAndExpiryDate(parsedStartDate, parsedExpiryDate)) {
445 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
446 LoggerTragetServiceName.VALIDATE_DATE_RANGE, ErrorLevel.ERROR.name(),
447 LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.INVALID_VALUE);
448 throw new CoreException(
449 new InvalidDateErrorBuilder(vendorLicenseModelId)
454 private boolean isValidatStartAndExpiryDate(LocalDate parsedStartDate,
455 LocalDate parsedExpiryDate) {
456 return parsedStartDate.atStartOfDay().isBefore(LocalDate.now().atStartOfDay())
457 || parsedExpiryDate.atStartOfDay().isEqual(parsedStartDate.atStartOfDay())
458 || parsedExpiryDate.isBefore(parsedStartDate);
461 private static LocalDate parseLocalDate(String date) {
462 if (date == null || date.isEmpty()) {
466 return LocalDate.parse(date, FORMATTER );
469 private void validateUpdateDate(String startDate, String expiryDate,
470 String vendorLicenseModelId) {
471 mdcDataDebugMessage.debugEntryMessage("Start date and end date", startDate
474 LocalDate parsedStartDate = parseLocalDate(startDate);
475 LocalDate parsedExpiryDate = parseLocalDate(expiryDate);
477 if (startDate != null && expiryDate != null
478 && (parsedExpiryDate.atStartOfDay()
479 .isEqual(parsedStartDate.atStartOfDay())
480 || parsedExpiryDate.isBefore(parsedStartDate ))) {
481 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
482 LoggerTragetServiceName.VALIDATE_DATE_RANGE,ErrorLevel.ERROR.name(),
483 LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.INVALID_VALUE);
484 throw new CoreException(
485 new InvalidDateErrorBuilder(vendorLicenseModelId)
489 if (startDate == null && expiryDate != null) {
490 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
491 LoggerTragetServiceName.VALIDATE_DATE_RANGE, ErrorLevel.ERROR.name(),
492 LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.INVALID_VALUE);
493 throw new CoreException(
494 new InvalidDateErrorBuilder(vendorLicenseModelId)
499 mdcDataDebugMessage.debugExitMessage(null);
503 public void updateEntitlementPool(EntitlementPoolEntity entitlementPool) {
504 mdcDataDebugMessage.debugEntryMessage(VLM_ID_EP_ID, entitlementPool
505 .getVendorLicenseModelId(), entitlementPool.getId());
507 entitlementPool.setStartDate(entitlementPool.getStartDate() != null ? (entitlementPool
508 .getStartDate().trim().length() != 0 ? entitlementPool.getStartDate() + EP_POOL_START_TIME
510 entitlementPool.setExpiryDate(entitlementPool.getExpiryDate() != null ? (entitlementPool
511 .getExpiryDate().trim().length() != 0 ? entitlementPool.getExpiryDate() + EP_POOL_EXPIRY_TIME
514 validateUpdateDate(entitlementPool.getStartDate(), entitlementPool.getExpiryDate(),
515 entitlementPool.getVendorLicenseModelId());
516 vendorLicenseFacade.updateEntitlementPool(entitlementPool);
518 mdcDataDebugMessage.debugExitMessage(VLM_ID_EP_ID, entitlementPool
519 .getVendorLicenseModelId(), entitlementPool.getId());
523 public EntitlementPoolEntity getEntitlementPool(EntitlementPoolEntity entitlementPool) {
524 mdcDataDebugMessage.debugEntryMessage(VLM_ID_EP_ID, entitlementPool
525 .getVendorLicenseModelId(), entitlementPool.getId());
527 EntitlementPoolEntity retrieved = entitlementPoolDao.get(entitlementPool);
529 .validateEntityExistence(retrieved, entitlementPool, VendorLicenseModelEntity.ENTITY_TYPE);
530 DateTimeFormatter targetFormatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
531 if (retrieved.getStartDate() != null) {
532 retrieved.setStartDate(LocalDate.parse(retrieved.getStartDate(), FORMATTER ).format
536 if (retrieved.getExpiryDate() != null) {
537 retrieved.setExpiryDate(LocalDate.parse(retrieved.getExpiryDate(), FORMATTER ).format
541 mdcDataDebugMessage.debugExitMessage(VLM_ID_EP_ID, entitlementPool
542 .getVendorLicenseModelId(), entitlementPool.getId());
547 public void deleteEntitlementPool(EntitlementPoolEntity entitlementPool) {
548 mdcDataDebugMessage.debugEntryMessage(VLM_ID_EP_ID, entitlementPool
549 .getVendorLicenseModelId(), entitlementPool.getId());
551 EntitlementPoolEntity retrieved = entitlementPoolDao.get(entitlementPool);
553 .validateEntityExistence(retrieved, entitlementPool, VendorLicenseModelEntity.ENTITY_TYPE);
555 for (String referencingFeatureGroupId : retrieved.getReferencingFeatureGroups()) {
556 featureGroupDao.removeEntitlementPool(
557 new FeatureGroupEntity(entitlementPool.getVendorLicenseModelId(),
558 entitlementPool.getVersion(),
559 referencingFeatureGroupId), entitlementPool.getId());
562 deleteChildLimits(entitlementPool.getVendorLicenseModelId(), entitlementPool.getVersion(),
563 entitlementPool.getId());
565 entitlementPoolDao.delete(entitlementPool);
567 deleteUniqueName(VendorLicenseConstants.UniqueValues.ENTITLEMENT_POOL_NAME,
568 retrieved.getVendorLicenseModelId(), retrieved.getVersion().toString(),
569 retrieved.getName());
571 mdcDataDebugMessage.debugExitMessage(VLM_ID_EP_ID, entitlementPool
572 .getVendorLicenseModelId(), entitlementPool.getId());
575 protected void deleteChildLimits(String vlmId, Version version, String epLkgId) {
576 Optional<Collection<LimitEntity>> limitEntities = Optional.ofNullable(
577 listLimits(vlmId, version, epLkgId));
578 limitEntities.ifPresent(entities -> entities.forEach(this::deleteLimit));
582 public Collection<LicenseKeyGroupEntity> listLicenseKeyGroups(String vlmId, Version version) {
583 mdcDataDebugMessage.debugEntryMessage(VLM_ID, vlmId);
584 mdcDataDebugMessage.debugExitMessage(VLM_ID, vlmId);
585 return vendorLicenseFacade.listLicenseKeyGroups(vlmId, version);
589 public LicenseKeyGroupEntity createLicenseKeyGroup(LicenseKeyGroupEntity licenseKeyGroup) {
591 .debugEntryMessage(VLM_ID, licenseKeyGroup.getVendorLicenseModelId());
593 mdcDataDebugMessage.debugExitMessage(VLM_ID, licenseKeyGroup
594 .getVendorLicenseModelId());
596 licenseKeyGroup.setStartDate(licenseKeyGroup.getStartDate() != null ? (licenseKeyGroup
597 .getStartDate().trim().length() != 0 ? licenseKeyGroup.getStartDate() + EP_POOL_START_TIME
599 licenseKeyGroup.setExpiryDate(licenseKeyGroup.getExpiryDate() != null ? (licenseKeyGroup
600 .getExpiryDate().trim().length() != 0 ? licenseKeyGroup.getExpiryDate() + EP_POOL_EXPIRY_TIME
603 validateCreateDate(licenseKeyGroup.getStartDate(), licenseKeyGroup.getExpiryDate(),
604 licenseKeyGroup.getVendorLicenseModelId());
605 return vendorLicenseFacade.createLicenseKeyGroup(licenseKeyGroup);
609 public void updateLicenseKeyGroup(LicenseKeyGroupEntity licenseKeyGroup) {
610 mdcDataDebugMessage.debugEntryMessage(VLM_ID_LKG_ID, licenseKeyGroup
611 .getVendorLicenseModelId(), licenseKeyGroup.getId());
613 licenseKeyGroup.setStartDate(licenseKeyGroup.getStartDate() != null ? (licenseKeyGroup
614 .getStartDate().trim().length() != 0 ? licenseKeyGroup.getStartDate() + EP_POOL_START_TIME
616 licenseKeyGroup.setExpiryDate(licenseKeyGroup.getExpiryDate() != null ? (licenseKeyGroup
617 .getExpiryDate().trim().length() != 0 ? licenseKeyGroup.getExpiryDate() + EP_POOL_EXPIRY_TIME
620 validateUpdateDate(licenseKeyGroup.getStartDate(), licenseKeyGroup.getExpiryDate(),
621 licenseKeyGroup.getVendorLicenseModelId());
622 vendorLicenseFacade.updateLicenseKeyGroup(licenseKeyGroup);
624 mdcDataDebugMessage.debugExitMessage(VLM_ID_LKG_ID, licenseKeyGroup
625 .getVendorLicenseModelId(), licenseKeyGroup.getId());
629 public LicenseKeyGroupEntity getLicenseKeyGroup(LicenseKeyGroupEntity licenseKeyGroup) {
630 mdcDataDebugMessage.debugEntryMessage(VLM_ID_LKG_ID, licenseKeyGroup
631 .getVendorLicenseModelId(), licenseKeyGroup.getId());
633 LicenseKeyGroupEntity retrieved = licenseKeyGroupDao.get(licenseKeyGroup);
635 .validateEntityExistence(retrieved, licenseKeyGroup, VendorLicenseModelEntity.ENTITY_TYPE);
637 mdcDataDebugMessage.debugExitMessage(VLM_ID_LKG_ID, licenseKeyGroup
638 .getVendorLicenseModelId(), licenseKeyGroup.getId());
643 public void deleteLicenseKeyGroup(LicenseKeyGroupEntity licenseKeyGroup) {
644 mdcDataDebugMessage.debugEntryMessage(VLM_ID_LKG_ID, licenseKeyGroup
645 .getVendorLicenseModelId(), licenseKeyGroup.getId());
647 LicenseKeyGroupEntity retrieved = licenseKeyGroupDao.get(licenseKeyGroup);
649 .validateEntityExistence(retrieved, licenseKeyGroup, VendorLicenseModelEntity.ENTITY_TYPE);
651 for (String referencingFeatureGroupId : retrieved.getReferencingFeatureGroups()) {
652 featureGroupDao.removeLicenseKeyGroup(
653 new FeatureGroupEntity(licenseKeyGroup.getVendorLicenseModelId(),
654 licenseKeyGroup.getVersion(),
655 referencingFeatureGroupId), licenseKeyGroup.getId());
658 deleteChildLimits(licenseKeyGroup.getVendorLicenseModelId(), licenseKeyGroup.getVersion(),
659 licenseKeyGroup.getId());
661 licenseKeyGroupDao.delete(licenseKeyGroup);
663 deleteUniqueName(VendorLicenseConstants.UniqueValues.LICENSE_KEY_GROUP_NAME,
664 retrieved.getVendorLicenseModelId(), retrieved.getVersion().toString(),
665 retrieved.getName());
667 mdcDataDebugMessage.debugExitMessage(VLM_ID_LKG_ID, licenseKeyGroup
668 .getVendorLicenseModelId(), licenseKeyGroup.getId());
672 public LimitEntity createLimit(LimitEntity limit) {
674 .debugEntryMessage(VLM_ID, limit.getVendorLicenseModelId(), EP_LKGID, limit
677 .debugExitMessage(VLM_ID, limit.getVendorLicenseModelId(), EP_LKGID, limit
679 validateLimit(limit);
680 LimitEntity createdLimit = vendorLicenseFacade.createLimit(limit);
681 updateParentForLimit(limit);
685 private void validateLimit(LimitEntity limit) {
686 Collection<LimitEntity> limitList =
687 listLimits(limit.getVendorLicenseModelId(), limit.getVersion()
688 , limit.getEpLkgId());
690 if (!isLimitNameUnique(limitList, limit.getName(), limit.getType(), limit.getId())) {
691 final ErrorCode duplicateLimitNameErrorBuilder =
692 LimitErrorBuilder.getDuplicateNameErrorbuilder(limit.getName(), limit.getType().name());
693 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
694 LoggerServiceName.Create_LIMIT.toString(), ErrorLevel.ERROR.name(),
695 LoggerErrorCode.DATA_ERROR.getErrorCode(),
696 duplicateLimitNameErrorBuilder.message());
697 throw new CoreException(duplicateLimitNameErrorBuilder);
701 private boolean isLimitNameUnique(Collection<LimitEntity> limitList, String name, LimitType
703 for (LimitEntity limit : limitList) {
704 if (limit.getName().equalsIgnoreCase(name) &&
705 limit.getType().name().equalsIgnoreCase(type.name())) {
706 if (id != null && limit.getId().equals(id)) {
716 public Collection<LimitEntity> listLimits(String vlmId, Version version, String epLkgId) {
717 mdcDataDebugMessage.debugEntryMessage(VLM_ID, vlmId, EP_LKGID, epLkgId);
718 mdcDataDebugMessage.debugExitMessage(VLM_ID, vlmId, EP_LKGID, epLkgId);
719 return vendorLicenseFacade.listLimits(vlmId, version, epLkgId);
723 public void deleteLimit(LimitEntity limitEntity) {
724 mdcDataDebugMessage.debugEntryMessage("VLM id, EP id, Limit Id", limitEntity
725 .getVendorLicenseModelId(), limitEntity.getEpLkgId(), limitEntity.getId());
727 if (!isLimitPresent(limitEntity)) {
729 .validateEntityExistence(null, limitEntity, VendorLicenseModelEntity.ENTITY_TYPE);
731 LimitEntity retrieved = limitDao.get(limitEntity);
733 .validateEntityExistence(retrieved, limitEntity, VendorLicenseModelEntity.ENTITY_TYPE);
735 limitDao.delete(limitEntity);
737 updateParentForLimit(limitEntity);
739 mdcDataDebugMessage.debugExitMessage("VLM id, EP id, Limit Id", limitEntity
740 .getVendorLicenseModelId(), limitEntity.getEpLkgId(), limitEntity.getId());
744 public void updateLimit(LimitEntity limit) {
746 .debugEntryMessage(VLM_ID, limit.getVendorLicenseModelId(), EP_LKGID, limit
749 validateLimit(limit);
750 vendorLicenseFacade.updateLimit(limit);
751 updateParentForLimit(limit);
753 .debugExitMessage(VLM_ID, limit.getVendorLicenseModelId(), EP_LKGID, limit
757 private boolean isLimitPresent(LimitEntity limit) {
758 return limitDao.isLimitPresent(limit);
762 public LimitEntity getLimit(LimitEntity limitEntity) {
763 mdcDataDebugMessage.debugEntryMessage(VLM_ID, limitEntity.getVendorLicenseModelId(),
764 EP_LKGID, limitEntity.getEpLkgId());
766 if (!isLimitPresent(limitEntity)) {
768 .validateEntityExistence(null, limitEntity, VendorLicenseModelEntity.ENTITY_TYPE);
770 LimitEntity retrieved = limitDao.get(limitEntity);
772 .validateEntityExistence(retrieved, limitEntity, VendorLicenseModelEntity.ENTITY_TYPE);
774 mdcDataDebugMessage.debugExitMessage(VLM_ID, limitEntity.getVendorLicenseModelId(),
775 EP_LKGID, limitEntity.getEpLkgId());
780 * update Parent of limit (EP/LKG) versionuuid when limit is modified so that limit updates are
781 * captured in VLM XML
783 private void updateParentForLimit(LimitEntity limit) {
784 mdcDataDebugMessage.debugEntryMessage(VLM_ID, limit.getVendorLicenseModelId(),
785 EP_LKGID, limit.getEpLkgId(), "Limit Parent ", limit.getParent());
786 if ("EntitlementPool".equals(limit.getParent())) {
787 EntitlementPoolEntity entitlementPoolEntity =
788 entitlementPoolDao.get(new EntitlementPoolEntity(limit.getVendorLicenseModelId(),
789 limit.getVersion(), limit.getEpLkgId()));
790 vendorLicenseFacade.updateEntitlementPool(entitlementPoolEntity);
793 if ("LicenseKeyGroup".equals(limit.getParent())) {
794 LicenseKeyGroupEntity licenseKeyGroupEntity = licenseKeyGroupDao.get(
795 new LicenseKeyGroupEntity(limit.getVendorLicenseModelId(), limit.getVersion(),
796 limit.getEpLkgId()));
797 vendorLicenseFacade.updateLicenseKeyGroup(licenseKeyGroupEntity);
800 mdcDataDebugMessage.debugEntryMessage(VLM_ID, limit.getVendorLicenseModelId(),
801 EP_LKGID, limit.getEpLkgId(), "Limit Parent ", limit.getParent());
804 protected void addFeatureGroupsToLicenseAgreementRef(Set<String> featureGroupIds,
805 LicenseAgreementEntity licenseAgreement) {
806 if (featureGroupIds != null) {
807 for (String featureGroupId : featureGroupIds) {
808 featureGroupDao.addReferencingLicenseAgreement(
809 new FeatureGroupEntity(licenseAgreement.getVendorLicenseModelId(),
810 licenseAgreement.getVersion(), featureGroupId), licenseAgreement.getId());
815 protected void removeFeatureGroupsToLicenseAgreementRef(Set<String> featureGroupIds,
816 LicenseAgreementEntity licenseAgreement) {
817 if (featureGroupIds != null) {
818 for (String featureGroupId : featureGroupIds) {
819 featureGroupDao.removeReferencingLicenseAgreement(
820 new FeatureGroupEntity(licenseAgreement.getVendorLicenseModelId(),
821 licenseAgreement.getVersion(), featureGroupId), licenseAgreement.getId());
826 protected void addLicenseKeyGroupsToFeatureGroupsRef(Set<String> licenseKeyGroupIds,
827 FeatureGroupEntity featureGroup) {
828 if (licenseKeyGroupIds != null) {
829 for (String licenseKeyGroupId : licenseKeyGroupIds) {
830 licenseKeyGroupDao.addReferencingFeatureGroup(
831 new LicenseKeyGroupEntity(featureGroup.getVendorLicenseModelId(),
832 featureGroup.getVersion(), licenseKeyGroupId), featureGroup.getId());
837 protected void removeLicenseKeyGroupsToFeatureGroupsRef(Set<String> licenseKeyGroupIds,
838 FeatureGroupEntity featureGroup) {
839 if (licenseKeyGroupIds != null) {
840 for (String licenseKeyGroupId : licenseKeyGroupIds) {
841 licenseKeyGroupDao.removeReferencingFeatureGroup(
842 new LicenseKeyGroupEntity(featureGroup.getVendorLicenseModelId(),
843 featureGroup.getVersion(), licenseKeyGroupId), featureGroup.getId());
848 protected void addEntitlementPoolsToFeatureGroupsRef(Set<String> entitlementPoolIds,
849 FeatureGroupEntity featureGroup) {
850 if (entitlementPoolIds != null) {
851 for (String entitlementPoolId : entitlementPoolIds) {
852 entitlementPoolDao.addReferencingFeatureGroup(
853 new EntitlementPoolEntity(featureGroup.getVendorLicenseModelId(),
854 featureGroup.getVersion(), entitlementPoolId), featureGroup.getId());
859 protected void removeEntitlementPoolsToFeatureGroupsRef(Set<String> entitlementPoolIds,
860 FeatureGroupEntity featureGroup) {
861 if (entitlementPoolIds != null) {
862 for (String entitlementPoolId : entitlementPoolIds) {
863 entitlementPoolDao.removeReferencingFeatureGroup(
864 new EntitlementPoolEntity(featureGroup.getVendorLicenseModelId(),
865 featureGroup.getVersion(), entitlementPoolId), featureGroup.getId());
870 protected void updateUniqueName(String uniqueValueType, String oldName, String newName, String...
873 .updateUniqueValue(uniqueValueType, oldName, newName, context);
876 protected void deleteUniqueName(String uniqueValueType, String... uniqueCombination) {
877 UniqueValueUtil.deleteUniqueValue(uniqueValueType, uniqueCombination);