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