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