re base code
[sdc.git] / openecomp-be / backend / openecomp-sdc-vendor-license-manager / src / main / java / org / openecomp / sdc / vendorlicense / impl / VendorLicenseManagerImpl.java
1 /*
2  * Copyright © 2016-2017 European Support Limited
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.openecomp.sdc.vendorlicense.impl;
18
19 import org.apache.commons.collections.CollectionUtils;
20 import org.openecomp.core.dao.UniqueValueDao;
21 import org.openecomp.core.util.UniqueValueUtil;
22 import org.openecomp.core.utilities.CommonMethods;
23 import org.openecomp.sdc.common.errors.CoreException;
24 import org.openecomp.sdc.common.errors.ErrorCode;
25 import org.openecomp.sdc.vendorlicense.VendorLicenseConstants;
26 import org.openecomp.sdc.vendorlicense.VendorLicenseManager;
27 import org.openecomp.sdc.vendorlicense.dao.*;
28 import org.openecomp.sdc.vendorlicense.dao.types.*;
29 import org.openecomp.sdc.vendorlicense.errors.InvalidDateErrorBuilder;
30 import org.openecomp.sdc.vendorlicense.errors.LimitErrorBuilder;
31 import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacade;
32 import org.openecomp.sdc.versioning.VersioningUtil;
33 import org.openecomp.sdc.versioning.dao.types.Version;
34
35 import java.time.LocalDate;
36 import java.time.format.DateTimeFormatter;
37 import java.util.Collection;
38 import java.util.Objects;
39 import java.util.Optional;
40 import java.util.Set;
41
42 public class VendorLicenseManagerImpl implements VendorLicenseManager {
43   private final UniqueValueUtil uniqueValueUtil;
44   private final VendorLicenseFacade vendorLicenseFacade;
45   private final VendorLicenseModelDao vendorLicenseModelDao;
46   private final LicenseAgreementDao licenseAgreementDao;
47   private final FeatureGroupDao featureGroupDao;
48   private final EntitlementPoolDao entitlementPoolDao;
49   private final LicenseKeyGroupDao licenseKeyGroupDao;
50   private final LimitDao limitDao;
51
52   private static final String EP_POOL_START_TIME = "T00:00:00Z";
53   private static final String EP_POOL_EXPIRY_TIME = "T23:59:59Z";
54   private static final  DateTimeFormatter FORMATTER
55           = DateTimeFormatter.ofPattern("MM/dd/yyyy'T'HH:mm:ss'Z'");
56   public VendorLicenseManagerImpl(VendorLicenseFacade vendorLicenseFacade,
57                                   VendorLicenseModelDao vendorLicenseModelDao,
58                                   LicenseAgreementDao licenseAgreementDao,
59                                   FeatureGroupDao featureGroupDao,
60                                   EntitlementPoolDao entitlementPoolDao,
61                                   LicenseKeyGroupDao licenseKeyGroupDao,
62                                   LimitDao limitDao,
63                                   UniqueValueDao uniqueValueDao) {
64     this.vendorLicenseFacade = vendorLicenseFacade;
65     this.vendorLicenseModelDao = vendorLicenseModelDao;
66     this.licenseAgreementDao = licenseAgreementDao;
67     this.featureGroupDao = featureGroupDao;
68     this.entitlementPoolDao = entitlementPoolDao;
69     this.licenseKeyGroupDao = licenseKeyGroupDao;
70     this.limitDao = limitDao;
71     this.uniqueValueUtil = new UniqueValueUtil(uniqueValueDao);
72   }
73
74
75   @Override
76   public void validate(String vendorLicenseModelId, Version version) {
77     vendorLicenseFacade.validate(vendorLicenseModelId, version);
78   }
79
80   @Override
81   public VendorLicenseModelEntity createVendorLicenseModel(
82       VendorLicenseModelEntity vendorLicenseModelEntity) {
83     vendorLicenseModelDao.create(vendorLicenseModelEntity);
84     return vendorLicenseModelEntity;
85   }
86
87   @Override
88   public void updateVendorLicenseModel(VendorLicenseModelEntity vendorLicenseModelEntity) {
89     VendorLicenseModelEntity retrieved = vendorLicenseModelDao.get(vendorLicenseModelEntity);
90     if (retrieved == null){
91       throw new CoreException((new ErrorCode.ErrorCodeBuilder()
92               .withMessage(String.format("Vlm with id %s and version %s does not exist.",
93                       vendorLicenseModelEntity.getId(), vendorLicenseModelEntity.getVersion().getId()))).build());
94     }
95
96     String existingVendorName = retrieved.getVendorName();
97
98     updateUniqueName(VendorLicenseConstants.UniqueValues.VENDOR_NAME, existingVendorName,
99         vendorLicenseModelEntity.getVendorName());
100     vendorLicenseModelDao.update(vendorLicenseModelEntity);
101   }
102
103   @Override
104   public VendorLicenseModelEntity getVendorLicenseModel(String vlmId, Version version) {
105     return vendorLicenseFacade.getVendorLicenseModel(vlmId, version);
106   }
107
108   @Override
109   public void deleteVendorLicenseModel(String vlmId, Version version) {
110     throw new UnsupportedOperationException(VendorLicenseConstants.UNSUPPORTED_OPERATION_ERROR);
111   }
112
113   @Override
114   public Collection<LicenseAgreementEntity> listLicenseAgreements(String vlmId, Version version) {
115     return licenseAgreementDao.list(new LicenseAgreementEntity(vlmId, version, null));
116   }
117
118   @Override
119   public LicenseAgreementEntity createLicenseAgreement(LicenseAgreementEntity licenseAgreement) {
120     return vendorLicenseFacade.createLicenseAgreement(licenseAgreement);
121   }
122
123   @Override
124   public void updateLicenseAgreement(LicenseAgreementEntity licenseAgreement,
125                                      Set<String> addedFeatureGroupIds,
126                                      Set<String> removedFeatureGroupIds) {
127     LicenseAgreementEntity retrieved = licenseAgreementDao.get(licenseAgreement);
128     VersioningUtil
129         .validateEntityExistence(retrieved, licenseAgreement, VendorLicenseModelEntity.ENTITY_TYPE);
130     VersioningUtil.validateContainedEntitiesExistence(new FeatureGroupEntity().getEntityType(),
131         removedFeatureGroupIds, retrieved, retrieved.getFeatureGroupIds());
132     VersioningUtil.validateEntitiesExistence(addedFeatureGroupIds,
133         new FeatureGroupEntity(licenseAgreement.getVendorLicenseModelId(),
134             licenseAgreement.getVersion(),
135             null),
136         featureGroupDao, VendorLicenseModelEntity.ENTITY_TYPE);
137
138     updateUniqueName(VendorLicenseConstants.UniqueValues.LICENSE_AGREEMENT_NAME,
139         retrieved.getName(), licenseAgreement.getName(), licenseAgreement.getVendorLicenseModelId(),
140         licenseAgreement.getVersion().getId());
141     licenseAgreementDao.updateColumnsAndDeltaFeatureGroupIds(licenseAgreement, addedFeatureGroupIds,
142         removedFeatureGroupIds);
143
144     addFeatureGroupsToLicenseAgreementRef(addedFeatureGroupIds, licenseAgreement);
145     removeFeatureGroupsToLicenseAgreementRef(removedFeatureGroupIds, licenseAgreement);
146   }
147
148   @Override
149   public LicenseAgreementModel getLicenseAgreementModel(String vlmId, Version version,
150                                                         String licenseAgreementId) {
151     return vendorLicenseFacade.getLicenseAgreementModel(vlmId, version, licenseAgreementId);
152   }
153
154   @Override
155   public void deleteLicenseAgreement(String vlmId, Version version, String licenseAgreementId) {
156     LicenseAgreementEntity input =
157         new LicenseAgreementEntity(vlmId, version, licenseAgreementId);
158     LicenseAgreementEntity retrieved = licenseAgreementDao.get(input);
159     VersioningUtil.validateEntityExistence(retrieved, input, VendorLicenseModelEntity.ENTITY_TYPE);
160
161     removeFeatureGroupsToLicenseAgreementRef(retrieved.getFeatureGroupIds(), retrieved);
162
163     licenseAgreementDao.delete(retrieved);
164
165     deleteUniqueName(VendorLicenseConstants.UniqueValues.LICENSE_AGREEMENT_NAME,
166         retrieved.getVendorLicenseModelId(), retrieved.getVersion().toString(),
167         retrieved.getName());
168   }
169
170   @Override
171   public Collection<FeatureGroupEntity> listFeatureGroups(String vlmId, Version version) {
172     return vendorLicenseFacade.listFeatureGroups(vlmId, version);
173   }
174
175   @Override
176   public FeatureGroupEntity createFeatureGroup(FeatureGroupEntity featureGroup) {
177     return vendorLicenseFacade.createFeatureGroup(featureGroup);
178   }
179
180   @Override
181   public void updateFeatureGroup(FeatureGroupEntity featureGroup,
182                                  Set<String> addedLicenseKeyGroups,
183                                  Set<String> removedLicenseKeyGroups,
184                                  Set<String> addedEntitlementPools,
185                                  Set<String> removedEntitlementPools) {
186     FeatureGroupEntity retrieved = featureGroupDao.get(featureGroup);
187     VersioningUtil
188         .validateEntityExistence(retrieved, featureGroup, VendorLicenseModelEntity.ENTITY_TYPE);
189
190     VersioningUtil.validateContainedEntitiesExistence(new LicenseKeyGroupEntity().getEntityType(),
191         removedLicenseKeyGroups, retrieved, retrieved.getLicenseKeyGroupIds());
192     VersioningUtil.validateContainedEntitiesExistence(new EntitlementPoolEntity().getEntityType(),
193         removedEntitlementPools, retrieved, retrieved.getEntitlementPoolIds());
194
195     VersioningUtil.validateEntitiesExistence(addedLicenseKeyGroups,
196         new LicenseKeyGroupEntity(featureGroup.getVendorLicenseModelId(), featureGroup.getVersion(),
197             null),
198         licenseKeyGroupDao, VendorLicenseModelEntity.ENTITY_TYPE);
199     VersioningUtil.validateEntitiesExistence(addedEntitlementPools,
200         new EntitlementPoolEntity(featureGroup.getVendorLicenseModelId(), featureGroup.getVersion(),
201             null),
202         entitlementPoolDao, VendorLicenseModelEntity.ENTITY_TYPE);
203
204     updateUniqueName(VendorLicenseConstants.UniqueValues.FEATURE_GROUP_NAME,
205         retrieved.getName(), featureGroup.getName(), featureGroup.getVendorLicenseModelId(),
206         featureGroup.getVersion().getId());
207
208     addLicenseKeyGroupsToFeatureGroupsRef(addedLicenseKeyGroups, featureGroup);
209     removeLicenseKeyGroupsToFeatureGroupsRef(removedLicenseKeyGroups, featureGroup);
210     addEntitlementPoolsToFeatureGroupsRef(addedEntitlementPools, featureGroup);
211     removeEntitlementPoolsToFeatureGroupsRef(removedEntitlementPools, featureGroup);
212
213     featureGroupDao.updateFeatureGroup(featureGroup, addedEntitlementPools, removedEntitlementPools,
214         addedLicenseKeyGroups, removedLicenseKeyGroups);
215
216     updateEpLkgOnMrnChange(featureGroup, addedLicenseKeyGroups, addedEntitlementPools, retrieved);
217   }
218
219   /**
220    * If MRN is updated in feature group then update all linked EPs and Lkgs with new versionUuId
221    * @param featureGroup - Feature Group entity which is requested for update
222    * @param addedLicenseKeyGroups - LicenseKeyGroups added with Feature Group
223    * @param addedEntitlementPools - EntitlementPools added with Feature Group
224    * @param retrieved - Feature Group entity fetched from database
225    */
226   private void updateEpLkgOnMrnChange(FeatureGroupEntity featureGroup,
227                                       Set<String> addedLicenseKeyGroups,
228                                       Set<String> addedEntitlementPools,
229                                       FeatureGroupEntity retrieved) {
230     if (Objects.nonNull(retrieved.getManufacturerReferenceNumber())
231         && !retrieved.getManufacturerReferenceNumber().equals(featureGroup
232         .getManufacturerReferenceNumber())) {
233       if (CollectionUtils.isEmpty(addedEntitlementPools)) {
234         updateEntitlementPool(featureGroup, retrieved.getEntitlementPoolIds());
235       } else {
236         updateEntitlementPool(featureGroup, addedEntitlementPools);
237       }
238
239       if (CollectionUtils.isEmpty(addedLicenseKeyGroups)) {
240         updateLicenseKeyGroup(featureGroup, retrieved.getLicenseKeyGroupIds());
241       } else {
242         updateLicenseKeyGroup(featureGroup, addedLicenseKeyGroups);
243       }
244     }
245   }
246
247   private void updateEntitlementPool(FeatureGroupEntity featureGroup,
248                                      Set<String> entitlementPoolIds) {
249     for (String epId: entitlementPoolIds) {
250       final EntitlementPoolEntity entitlementPoolEntity = entitlementPoolDao
251           .get(new EntitlementPoolEntity(featureGroup.getVendorLicenseModelId(), featureGroup
252               .getVersion(), epId));
253       if (Objects.nonNull(entitlementPoolEntity)) {
254         entitlementPoolEntity.setVersionUuId(CommonMethods.nextUuId());
255         entitlementPoolDao.update(entitlementPoolEntity);
256       }
257     }
258   }
259
260   private void updateLicenseKeyGroup(FeatureGroupEntity featureGroup,
261                                      Set<String> licenseKeyGroupIds) {
262     for (String lkgId: licenseKeyGroupIds) {
263       final LicenseKeyGroupEntity licenseKeyGroupEntity = licenseKeyGroupDao
264           .get(new LicenseKeyGroupEntity(featureGroup.getVendorLicenseModelId(),
265               featureGroup.getVersion(), lkgId));
266       if (Objects.nonNull(licenseKeyGroupEntity)) {
267         licenseKeyGroupEntity.setVersionUuId(CommonMethods.nextUuId());
268         licenseKeyGroupDao.update(licenseKeyGroupEntity);
269       }
270     }
271   }
272
273   @Override
274   public FeatureGroupModel getFeatureGroupModel(FeatureGroupEntity featureGroup) {
275     return vendorLicenseFacade.getFeatureGroupModel(featureGroup);
276   }
277
278   @Override
279   public void deleteFeatureGroup(FeatureGroupEntity featureGroup) {
280     FeatureGroupEntity retrieved = featureGroupDao.get(featureGroup);
281     VersioningUtil
282         .validateEntityExistence(retrieved, featureGroup, VendorLicenseModelEntity.ENTITY_TYPE);
283
284     removeLicenseKeyGroupsToFeatureGroupsRef(retrieved.getLicenseKeyGroupIds(), featureGroup);
285     removeEntitlementPoolsToFeatureGroupsRef(retrieved.getEntitlementPoolIds(), featureGroup);
286
287     for (String licenceAgreementId : retrieved.getReferencingLicenseAgreements()) {
288       licenseAgreementDao.removeFeatureGroup(
289           new LicenseAgreementEntity(featureGroup.getVendorLicenseModelId(),
290               featureGroup.getVersion(),
291               licenceAgreementId), featureGroup.getId());
292     }
293
294     featureGroupDao.delete(featureGroup);
295
296     deleteUniqueName(VendorLicenseConstants.UniqueValues.FEATURE_GROUP_NAME,
297         retrieved.getVendorLicenseModelId(), retrieved.getVersion().toString(),
298         retrieved.getName());
299   }
300
301   @Override
302   public Collection<EntitlementPoolEntity> listEntitlementPools(String vlmId, Version version) {
303     return vendorLicenseFacade.listEntitlementPools(vlmId, version);
304   }
305
306   @Override
307   public EntitlementPoolEntity createEntitlementPool(EntitlementPoolEntity entitlementPool) {
308     entitlementPool.setStartDate(entitlementPool.getStartDate() != null ? (entitlementPool
309         .getStartDate().trim().length() != 0 ? entitlementPool.getStartDate() + EP_POOL_START_TIME
310         : null) : null);
311     entitlementPool.setExpiryDate(entitlementPool.getExpiryDate() != null ? (entitlementPool
312         .getExpiryDate().trim().length() != 0 ? entitlementPool.getExpiryDate() + EP_POOL_EXPIRY_TIME 
313         : null) : null);
314
315     validateCreateDate(entitlementPool.getStartDate(), entitlementPool.getExpiryDate(),
316         entitlementPool.getVendorLicenseModelId());
317     return vendorLicenseFacade.createEntitlementPool(entitlementPool);
318   }
319
320   private void validateCreateDate(String startDate, String expiryDate,
321                                   String vendorLicenseModelId) {
322   LocalDate parsedStartDate = parseLocalDate(startDate);
323   LocalDate parsedExpiryDate = parseLocalDate(expiryDate);
324
325
326     validateIfStartAndExpiryDateIsNotNull(startDate, expiryDate,
327             vendorLicenseModelId, parsedStartDate, parsedExpiryDate);
328
329     if (startDate != null && expiryDate == null
330                       && parsedStartDate.atStartOfDay().isBefore
331           (LocalDate.now().atStartOfDay())) {
332         throw new CoreException(
333             new InvalidDateErrorBuilder(vendorLicenseModelId)
334                 .build());
335     }
336
337     if (startDate == null && expiryDate != null) {
338       throw new CoreException(
339           new InvalidDateErrorBuilder(vendorLicenseModelId)
340               .build());
341
342     }
343   }
344
345   private void validateIfStartAndExpiryDateIsNotNull(String startDate, String expiryDate,
346                                                      String vendorLicenseModelId,
347                                                      LocalDate parsedStartDate,
348                                                      LocalDate parsedExpiryDate) {
349     if (startDate != null && expiryDate != null
350             && isValidatStartAndExpiryDate(parsedStartDate, parsedExpiryDate)) {
351       throw new CoreException(
352               new InvalidDateErrorBuilder(vendorLicenseModelId)
353                       .build());
354     }
355   }
356
357   private boolean isValidatStartAndExpiryDate(LocalDate parsedStartDate,
358                                               LocalDate parsedExpiryDate) {
359     return parsedStartDate.atStartOfDay().isBefore(LocalDate.now().atStartOfDay())
360     || parsedExpiryDate.atStartOfDay().isEqual(parsedStartDate.atStartOfDay())
361     || parsedExpiryDate.isBefore(parsedStartDate);
362   }
363
364   private static LocalDate parseLocalDate(String date) {
365     if (date == null || date.isEmpty()) {
366       return null;
367     }
368
369     return LocalDate.parse(date, FORMATTER );
370   }
371
372   private void validateUpdateDate(String startDate, String expiryDate,
373                                   String vendorLicenseModelId) {
374     LocalDate parsedStartDate = parseLocalDate(startDate);
375     LocalDate parsedExpiryDate = parseLocalDate(expiryDate);
376
377     if (startDate != null && expiryDate != null
378             && (parsedExpiryDate.atStartOfDay()
379             .isEqual(parsedStartDate.atStartOfDay())
380             || parsedExpiryDate.isBefore(parsedStartDate ))) {
381       throw new CoreException(
382               new InvalidDateErrorBuilder(vendorLicenseModelId)
383                       .build());
384     }
385
386     if (startDate == null && expiryDate != null) {
387       throw new CoreException(
388           new InvalidDateErrorBuilder(vendorLicenseModelId)
389               .build());
390
391     }
392   }
393
394   @Override
395   public void updateEntitlementPool(EntitlementPoolEntity entitlementPool) {
396     entitlementPool.setStartDate(entitlementPool.getStartDate() != null ? (entitlementPool
397         .getStartDate().trim().length() != 0 ? entitlementPool.getStartDate() + EP_POOL_START_TIME
398         : null) : null);
399     entitlementPool.setExpiryDate(entitlementPool.getExpiryDate() != null ? (entitlementPool
400         .getExpiryDate().trim().length() != 0 ? entitlementPool.getExpiryDate() + EP_POOL_EXPIRY_TIME 
401         : null) : null);
402
403     validateUpdateDate(entitlementPool.getStartDate(), entitlementPool.getExpiryDate(),
404         entitlementPool.getVendorLicenseModelId());
405     vendorLicenseFacade.updateEntitlementPool(entitlementPool);
406   }
407
408   @Override
409   public EntitlementPoolEntity getEntitlementPool(EntitlementPoolEntity entitlementPool) {
410     EntitlementPoolEntity retrieved = entitlementPoolDao.get(entitlementPool);
411     VersioningUtil
412         .validateEntityExistence(retrieved, entitlementPool, VendorLicenseModelEntity.ENTITY_TYPE);
413     DateTimeFormatter targetFormatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
414     if (retrieved.getStartDate() != null) {
415       retrieved.setStartDate(LocalDate.parse(retrieved.getStartDate(), FORMATTER ).format
416           (targetFormatter));
417     }
418
419     if (retrieved.getExpiryDate() != null) {
420       retrieved.setExpiryDate(LocalDate.parse(retrieved.getExpiryDate(), FORMATTER ).format
421           (targetFormatter));
422     }
423     return retrieved;
424   }
425
426   @Override
427   public void deleteEntitlementPool(EntitlementPoolEntity entitlementPool) {
428     EntitlementPoolEntity retrieved = entitlementPoolDao.get(entitlementPool);
429     VersioningUtil
430         .validateEntityExistence(retrieved, entitlementPool, VendorLicenseModelEntity.ENTITY_TYPE);
431
432     for (String referencingFeatureGroupId : retrieved.getReferencingFeatureGroups()) {
433       featureGroupDao.removeEntitlementPool(
434           new FeatureGroupEntity(entitlementPool.getVendorLicenseModelId(),
435               entitlementPool.getVersion(),
436               referencingFeatureGroupId), entitlementPool.getId());
437     }
438
439     deleteChildLimits(entitlementPool.getVendorLicenseModelId(), entitlementPool.getVersion(),
440         entitlementPool.getId());
441
442     entitlementPoolDao.delete(entitlementPool);
443
444     deleteUniqueName(VendorLicenseConstants.UniqueValues.ENTITLEMENT_POOL_NAME,
445         retrieved.getVendorLicenseModelId(), retrieved.getVersion().toString(),
446         retrieved.getName());
447   }
448
449   protected void deleteChildLimits(String vlmId, Version version, String epLkgId) {
450     Optional<Collection<LimitEntity>> limitEntities = Optional.ofNullable(
451         listLimits(vlmId, version, epLkgId));
452     limitEntities.ifPresent(entities -> entities.forEach(this::deleteLimit));
453   }
454
455   @Override
456   public Collection<LicenseKeyGroupEntity> listLicenseKeyGroups(String vlmId, Version version) {
457     return vendorLicenseFacade.listLicenseKeyGroups(vlmId, version);
458   }
459
460   @Override
461   public LicenseKeyGroupEntity createLicenseKeyGroup(LicenseKeyGroupEntity licenseKeyGroup) {
462     licenseKeyGroup.setStartDate(licenseKeyGroup.getStartDate() != null ? (licenseKeyGroup
463         .getStartDate().trim().length() != 0 ? licenseKeyGroup.getStartDate() + EP_POOL_START_TIME
464         : null) : null);
465     licenseKeyGroup.setExpiryDate(licenseKeyGroup.getExpiryDate() != null ? (licenseKeyGroup
466         .getExpiryDate().trim().length() != 0 ? licenseKeyGroup.getExpiryDate() + EP_POOL_EXPIRY_TIME 
467         : null) : null);
468
469     validateCreateDate(licenseKeyGroup.getStartDate(), licenseKeyGroup.getExpiryDate(),
470         licenseKeyGroup.getVendorLicenseModelId());
471     return vendorLicenseFacade.createLicenseKeyGroup(licenseKeyGroup);
472   }
473
474   @Override
475   public void updateLicenseKeyGroup(LicenseKeyGroupEntity licenseKeyGroup) {
476     licenseKeyGroup.setStartDate(licenseKeyGroup.getStartDate() != null ? (licenseKeyGroup
477         .getStartDate().trim().length() != 0 ? licenseKeyGroup.getStartDate() + EP_POOL_START_TIME
478         : null) : null);
479     licenseKeyGroup.setExpiryDate(licenseKeyGroup.getExpiryDate() != null ? (licenseKeyGroup
480         .getExpiryDate().trim().length() != 0 ? licenseKeyGroup.getExpiryDate() + EP_POOL_EXPIRY_TIME 
481         : null) : null);
482
483     validateUpdateDate(licenseKeyGroup.getStartDate(), licenseKeyGroup.getExpiryDate(),
484         licenseKeyGroup.getVendorLicenseModelId());
485     vendorLicenseFacade.updateLicenseKeyGroup(licenseKeyGroup);
486   }
487
488   @Override
489   public LicenseKeyGroupEntity getLicenseKeyGroup(LicenseKeyGroupEntity licenseKeyGroup) {
490     LicenseKeyGroupEntity retrieved = licenseKeyGroupDao.get(licenseKeyGroup);
491     VersioningUtil
492         .validateEntityExistence(retrieved, licenseKeyGroup, VendorLicenseModelEntity.ENTITY_TYPE);
493     return retrieved;
494   }
495
496   @Override
497   public void deleteLicenseKeyGroup(LicenseKeyGroupEntity licenseKeyGroup) {
498     LicenseKeyGroupEntity retrieved = licenseKeyGroupDao.get(licenseKeyGroup);
499     VersioningUtil
500         .validateEntityExistence(retrieved, licenseKeyGroup, VendorLicenseModelEntity.ENTITY_TYPE);
501
502     for (String referencingFeatureGroupId : retrieved.getReferencingFeatureGroups()) {
503       featureGroupDao.removeLicenseKeyGroup(
504           new FeatureGroupEntity(licenseKeyGroup.getVendorLicenseModelId(),
505               licenseKeyGroup.getVersion(),
506               referencingFeatureGroupId), licenseKeyGroup.getId());
507     }
508
509     deleteChildLimits(licenseKeyGroup.getVendorLicenseModelId(), licenseKeyGroup.getVersion(),
510         licenseKeyGroup.getId());
511
512     licenseKeyGroupDao.delete(licenseKeyGroup);
513
514     deleteUniqueName(VendorLicenseConstants.UniqueValues.LICENSE_KEY_GROUP_NAME,
515         retrieved.getVendorLicenseModelId(), retrieved.getVersion().toString(),
516         retrieved.getName());
517   }
518
519   @Override
520   public LimitEntity createLimit(LimitEntity limit) {
521     validateLimit(limit);
522     LimitEntity createdLimit = vendorLicenseFacade.createLimit(limit);
523     updateParentForLimit(limit);
524     return createdLimit;
525   }
526
527   private void validateLimit(LimitEntity limit) {
528     Collection<LimitEntity> limitList =
529         listLimits(limit.getVendorLicenseModelId(), limit.getVersion()
530             , limit.getEpLkgId());
531
532     if (!isLimitNameUnique(limitList, limit.getName(), limit.getType(), limit.getId())) {
533       final ErrorCode duplicateLimitNameErrorBuilder =
534           LimitErrorBuilder.getDuplicateNameErrorbuilder(limit.getName(), limit.getType().name());
535       throw new CoreException(duplicateLimitNameErrorBuilder);
536     }
537   }
538
539   private boolean isLimitNameUnique(Collection<LimitEntity> limitList, String name, LimitType
540       type, String id) {
541     for (LimitEntity limit : limitList) {
542       if (limit.getName().equalsIgnoreCase(name) &&
543           limit.getType().name().equalsIgnoreCase(type.name())) {
544         if (id != null && limit.getId().equals(id)) {
545           continue;
546         }
547         return false;
548       }
549     }
550     return true;
551   }
552
553   @Override
554   public Collection<LimitEntity> listLimits(String vlmId, Version version, String epLkgId) {
555     return vendorLicenseFacade.listLimits(vlmId, version, epLkgId);
556   }
557
558   @Override
559   public void deleteLimit(LimitEntity limitEntity) {
560     if (!isLimitPresent(limitEntity)) {
561       VersioningUtil
562           .validateEntityExistence(null, limitEntity, VendorLicenseModelEntity.ENTITY_TYPE);
563     }
564     LimitEntity retrieved = limitDao.get(limitEntity);
565     VersioningUtil
566         .validateEntityExistence(retrieved, limitEntity, VendorLicenseModelEntity.ENTITY_TYPE);
567
568     limitDao.delete(limitEntity);
569
570     updateParentForLimit(limitEntity);
571   }
572
573   @Override
574   public void updateLimit(LimitEntity limit) {
575     getLimit(limit);
576     validateLimit(limit);
577     vendorLicenseFacade.updateLimit(limit);
578     updateParentForLimit(limit);
579   }
580
581   private boolean isLimitPresent(LimitEntity limit) {
582     return limitDao.isLimitPresent(limit);
583   }
584
585   @Override
586   public LimitEntity getLimit(LimitEntity limitEntity) {
587     if (!isLimitPresent(limitEntity)) {
588       VersioningUtil
589           .validateEntityExistence(null, limitEntity, VendorLicenseModelEntity.ENTITY_TYPE);
590     }
591     LimitEntity retrieved = limitDao.get(limitEntity);
592     VersioningUtil
593         .validateEntityExistence(retrieved, limitEntity, VendorLicenseModelEntity.ENTITY_TYPE);
594     return retrieved;
595   }
596
597   /**
598    * update Parent of limit (EP/LKG) versionuuid when limit is modified so that limit updates are
599    * captured in VLM XML
600    */
601   private void updateParentForLimit(LimitEntity limit) {
602     if ("EntitlementPool".equals(limit.getParent())) {
603       EntitlementPoolEntity entitlementPoolEntity =
604           entitlementPoolDao.get(new EntitlementPoolEntity(limit.getVendorLicenseModelId(),
605               limit.getVersion(), limit.getEpLkgId()));
606       vendorLicenseFacade.updateEntitlementPool(entitlementPoolEntity);
607     }
608
609     if ("LicenseKeyGroup".equals(limit.getParent())) {
610       LicenseKeyGroupEntity licenseKeyGroupEntity = licenseKeyGroupDao.get(
611           new LicenseKeyGroupEntity(limit.getVendorLicenseModelId(), limit.getVersion(),
612               limit.getEpLkgId()));
613       vendorLicenseFacade.updateLicenseKeyGroup(licenseKeyGroupEntity);
614     }
615   }
616
617   protected void addFeatureGroupsToLicenseAgreementRef(Set<String> featureGroupIds,
618                                                        LicenseAgreementEntity licenseAgreement) {
619     if (featureGroupIds != null) {
620       for (String featureGroupId : featureGroupIds) {
621         featureGroupDao.addReferencingLicenseAgreement(
622             new FeatureGroupEntity(licenseAgreement.getVendorLicenseModelId(),
623                 licenseAgreement.getVersion(), featureGroupId), licenseAgreement.getId());
624       }
625     }
626   }
627
628   protected void removeFeatureGroupsToLicenseAgreementRef(Set<String> featureGroupIds,
629                                                           LicenseAgreementEntity licenseAgreement) {
630     if (featureGroupIds != null) {
631       for (String featureGroupId : featureGroupIds) {
632         featureGroupDao.removeReferencingLicenseAgreement(
633             new FeatureGroupEntity(licenseAgreement.getVendorLicenseModelId(),
634                 licenseAgreement.getVersion(), featureGroupId), licenseAgreement.getId());
635       }
636     }
637   }
638
639   protected void addLicenseKeyGroupsToFeatureGroupsRef(Set<String> licenseKeyGroupIds,
640                                                        FeatureGroupEntity featureGroup) {
641     if (licenseKeyGroupIds != null) {
642       for (String licenseKeyGroupId : licenseKeyGroupIds) {
643         licenseKeyGroupDao.addReferencingFeatureGroup(
644             new LicenseKeyGroupEntity(featureGroup.getVendorLicenseModelId(),
645                 featureGroup.getVersion(), licenseKeyGroupId), featureGroup.getId());
646       }
647     }
648   }
649
650   protected void removeLicenseKeyGroupsToFeatureGroupsRef(Set<String> licenseKeyGroupIds,
651                                                           FeatureGroupEntity featureGroup) {
652     if (licenseKeyGroupIds != null) {
653       for (String licenseKeyGroupId : licenseKeyGroupIds) {
654         licenseKeyGroupDao.removeReferencingFeatureGroup(
655             new LicenseKeyGroupEntity(featureGroup.getVendorLicenseModelId(),
656                 featureGroup.getVersion(), licenseKeyGroupId), featureGroup.getId());
657       }
658     }
659   }
660
661   protected void addEntitlementPoolsToFeatureGroupsRef(Set<String> entitlementPoolIds,
662                                                        FeatureGroupEntity featureGroup) {
663     if (entitlementPoolIds != null) {
664       for (String entitlementPoolId : entitlementPoolIds) {
665         entitlementPoolDao.addReferencingFeatureGroup(
666             new EntitlementPoolEntity(featureGroup.getVendorLicenseModelId(),
667                 featureGroup.getVersion(), entitlementPoolId), featureGroup.getId());
668       }
669     }
670   }
671
672   protected void removeEntitlementPoolsToFeatureGroupsRef(Set<String> entitlementPoolIds,
673                                                           FeatureGroupEntity featureGroup) {
674     if (entitlementPoolIds != null) {
675       for (String entitlementPoolId : entitlementPoolIds) {
676         entitlementPoolDao.removeReferencingFeatureGroup(
677             new EntitlementPoolEntity(featureGroup.getVendorLicenseModelId(),
678                 featureGroup.getVersion(), entitlementPoolId), featureGroup.getId());
679       }
680     }
681   }
682
683   protected void updateUniqueName(String uniqueValueType, String oldName, String newName, String...
684       context) {
685     uniqueValueUtil
686         .updateUniqueValue(uniqueValueType, oldName, newName, context);
687   }
688
689   protected void deleteUniqueName(String uniqueValueType, String... uniqueCombination) {
690     uniqueValueUtil.deleteUniqueValue(uniqueValueType, uniqueCombination);
691   }
692 }