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