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