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