push addional code
[sdc.git] / openecomp-be / backend / openecomp-sdc-vendor-license-manager / src / main / java / org / openecomp / sdc / vendorlicense / impl / VendorLicenseManagerImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.vendorlicense.impl;
22
23 import static org.openecomp.sdc.vendorlicense.VendorLicenseConstants
24     .VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE;
25
26 import org.openecomp.core.util.UniqueValueUtil;
27 import org.openecomp.sdc.vendorlicense.VendorLicenseConstants;
28 import org.openecomp.sdc.vendorlicense.VendorLicenseManager;
29 import org.openecomp.sdc.vendorlicense.dao.EntitlementPoolDao;
30 import org.openecomp.sdc.vendorlicense.dao.EntitlementPoolDaoFactory;
31 import org.openecomp.sdc.vendorlicense.dao.FeatureGroupDao;
32 import org.openecomp.sdc.vendorlicense.dao.FeatureGroupDaoFactory;
33 import org.openecomp.sdc.vendorlicense.dao.LicenseAgreementDaoFactory;
34 import org.openecomp.sdc.vendorlicense.dao.LicenseKeyGroupDao;
35 import org.openecomp.sdc.vendorlicense.dao.LicenseKeyGroupDaoFactory;
36 import org.openecomp.sdc.vendorlicense.dao.VendorLicenseModelDao;
37 import org.openecomp.sdc.vendorlicense.dao.VendorLicenseModelDaoFactory;
38 import org.openecomp.sdc.vendorlicense.dao.types.EntitlementPoolEntity;
39 import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupModel;
40 import org.openecomp.sdc.vendorlicense.dao.types.LicenseAgreementEntity;
41 import org.openecomp.sdc.vendorlicense.dao.types.LicenseAgreementModel;
42 import org.openecomp.sdc.vendorlicense.dao.types.LicenseKeyGroupEntity;
43 import org.openecomp.sdc.vendorlicense.dao.types.VendorLicenseModelEntity;
44 import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacade;
45 import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacadeFactory;
46 import org.openecomp.sdc.vendorlicense.types.VersionedVendorLicenseModel;
47 import org.openecomp.sdc.versioning.VersioningManager;
48 import org.openecomp.sdc.versioning.VersioningUtil;
49 import org.openecomp.sdc.versioning.dao.types.Version;
50 import org.openecomp.sdc.versioning.dao.types.VersionStatus;
51 import org.openecomp.sdc.versioning.types.VersionInfo;
52 import org.openecomp.sdc.versioning.types.VersionableEntityAction;
53
54 import java.util.ArrayList;
55 import java.util.Collection;
56 import java.util.Collections;
57 import java.util.Comparator;
58 import java.util.List;
59 import java.util.Map;
60 import java.util.Set;
61
62 public class VendorLicenseManagerImpl implements VendorLicenseManager {
63
64   private static final VersioningManager versioningManager =
65       org.openecomp.sdc.versioning.VersioningManagerFactory.getInstance().createInterface();
66   private static final VendorLicenseFacade vendorLicenseFacade =
67       VendorLicenseFacadeFactory.getInstance().createInterface();
68
69   private static final VendorLicenseModelDao
70       vendorLicenseModelDao = VendorLicenseModelDaoFactory.getInstance().createInterface();
71   private static final org.openecomp.sdc.vendorlicense.dao.LicenseAgreementDao
72       licenseAgreementDao = LicenseAgreementDaoFactory.getInstance().createInterface();
73   private static final FeatureGroupDao featureGroupDao =
74       FeatureGroupDaoFactory.getInstance().createInterface();
75   private static final EntitlementPoolDao
76       entitlementPoolDao = EntitlementPoolDaoFactory.getInstance().createInterface();
77   private static final LicenseKeyGroupDao
78       licenseKeyGroupDao = LicenseKeyGroupDaoFactory.getInstance().createInterface();
79
80   private static void sortVlmListByModificationTimeDescOrder(
81       List<VersionedVendorLicenseModel> vendorLicenseModels) {
82     Collections.sort(vendorLicenseModels, new Comparator<VersionedVendorLicenseModel>() {
83       @Override
84       public int compare(VersionedVendorLicenseModel o1, VersionedVendorLicenseModel o2) {
85         return o2.getVendorLicenseModel().getWritetimeMicroSeconds()
86             .compareTo(o1.getVendorLicenseModel().getWritetimeMicroSeconds());
87       }
88     });
89   }
90
91   @Override
92   public void checkout(String vendorLicenseModelId, String user) {
93     Version newVersion = versioningManager
94         .checkout(VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE, vendorLicenseModelId, user);
95     vendorLicenseFacade.updateVlmLastModificationTime(vendorLicenseModelId, newVersion);
96   }
97
98   @Override
99   public void undoCheckout(String vendorLicenseModelId, String user) {
100     Version newVersion = versioningManager
101         .undoCheckout(VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE, vendorLicenseModelId, user);
102     vendorLicenseFacade.updateVlmLastModificationTime(vendorLicenseModelId, newVersion);
103   }
104
105   @Override
106   public void checkin(String vendorLicenseModelId, String user) {
107     vendorLicenseFacade.checkin(vendorLicenseModelId, user);
108   }
109
110   @Override
111   public void submit(String vendorLicenseModelId, String user) {
112     vendorLicenseFacade.submit(vendorLicenseModelId, user);
113   }
114
115   @Override
116   public Collection<VersionedVendorLicenseModel> listVendorLicenseModels(String versionFilter,
117                                                                          String user) {
118     Map<String, VersionInfo> idToVersionsInfo = versioningManager
119         .listEntitiesVersionInfo(VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE, user,
120             VersionableEntityAction.Read);
121
122     List<VersionedVendorLicenseModel> vendorLicenseModels = new ArrayList<>();
123     for (Map.Entry<String, VersionInfo> entry : idToVersionsInfo.entrySet()) {
124       VersionInfo versionInfo = entry.getValue();
125       if (versionFilter != null && versionFilter.equals(VersionStatus.Final.name())) {
126         if (versionInfo.getLatestFinalVersion() == null) {
127           continue;
128         }
129         versionInfo.setActiveVersion(versionInfo.getLatestFinalVersion());
130         versionInfo.setStatus(VersionStatus.Final);
131         versionInfo.setLockingUser(null);
132       }
133
134       VendorLicenseModelEntity
135           vlm = vendorLicenseModelDao
136           .get(new VendorLicenseModelEntity(entry.getKey(), versionInfo.getActiveVersion()));
137       if (vlm != null) {
138         VersionedVendorLicenseModel versionedVlm = new VersionedVendorLicenseModel();
139         versionedVlm.setVendorLicenseModel(vlm);
140         versionedVlm.setVersionInfo(versionInfo);
141         vendorLicenseModels.add(versionedVlm);
142       }
143     }
144
145     sortVlmListByModificationTimeDescOrder(vendorLicenseModels);
146
147     return vendorLicenseModels;
148   }
149
150   @Override
151   public VendorLicenseModelEntity createVendorLicenseModel(
152       VendorLicenseModelEntity vendorLicenseModelEntity, String user) {
153     return vendorLicenseFacade.createVendorLicenseModel(vendorLicenseModelEntity, user);
154   }
155
156   @Override
157   public void updateVendorLicenseModel(VendorLicenseModelEntity vendorLicenseModelEntity,
158                                        String user) {
159     Version activeVersion =
160         getVersionInfo(vendorLicenseModelEntity.getId(), VersionableEntityAction.Write, user)
161             .getActiveVersion();
162     vendorLicenseModelEntity.setVersion(activeVersion);
163
164     String existingVendorName = vendorLicenseModelDao.get(vendorLicenseModelEntity).getVendorName();
165     UniqueValueUtil
166         .updateUniqueValue(VendorLicenseConstants.UniqueValues.VENDOR_NAME, existingVendorName,
167             vendorLicenseModelEntity.getVendorName());
168     vendorLicenseModelDao.update(vendorLicenseModelEntity);
169
170     vendorLicenseFacade
171         .updateVlmLastModificationTime(vendorLicenseModelEntity.getId(), activeVersion);
172   }
173
174   @Override
175   public VersionedVendorLicenseModel getVendorLicenseModel(String vlmId, Version version,
176                                                            String user) {
177     return vendorLicenseFacade.getVendorLicenseModel(vlmId, version, user);
178   }
179
180   @Override
181   public void deleteVendorLicenseModel(String vlmId, String user) {
182     throw new UnsupportedOperationException("Unsupported operation for 1607 release.");
183
184     /*        Version activeVersion = getVersionInfo(vlmId, VersionableEntityAction.Write, user)
185     .getActiveVersion();
186
187         vendorLicenseModelDao.delete(new VendorLicenseModelEntity(vlmId, activeVersion));
188         licenseAgreementDao.deleteAll(new LicenseAgreementEntity(vlmId, activeVersion, null));
189         featureGroupDao.deleteAll(new FeatureGroupEntity(vlmId, activeVersion, null));
190         licenseKeyGroupDao.deleteAll(new LicenseKeyGroupEntity(vlmId, activeVersion, null));
191         entitlementPoolDao.deleteAll(new EntitlementPoolEntity(vlmId, activeVersion, null));*/
192   }
193
194   @Override
195   public Collection<LicenseAgreementEntity> listLicenseAgreements(String vlmId, Version version,
196                                                                   String user) {
197     return licenseAgreementDao.list(new LicenseAgreementEntity(vlmId, VersioningUtil
198         .resolveVersion(version, getVersionInfo(vlmId, VersionableEntityAction.Read, user)), null));
199   }
200
201   @Override
202   public LicenseAgreementEntity createLicenseAgreement(LicenseAgreementEntity licenseAgreement,
203                                                        String user) {
204     return vendorLicenseFacade.createLicenseAgreement(licenseAgreement, user);
205   }
206
207   @Override
208   public void updateLicenseAgreement(LicenseAgreementEntity licenseAgreement,
209                                      Set<String> addedFeatureGroupIds,
210                                      Set<String> removedFeatureGroupIds, String user) {
211     Version activeVersion =
212         getVersionInfo(licenseAgreement.getVendorLicenseModelId(), VersionableEntityAction.Write,
213             user).getActiveVersion();
214     licenseAgreement.setVersion(activeVersion);
215     LicenseAgreementEntity retrieved = licenseAgreementDao.get(licenseAgreement);
216     VersioningUtil
217         .validateEntityExistence(retrieved, licenseAgreement, VendorLicenseModelEntity.ENTITY_TYPE);
218     VersioningUtil.validateContainedEntitiesExistence(
219         new org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity().getEntityType(),
220         removedFeatureGroupIds, retrieved, retrieved.getFeatureGroupIds());
221     VersioningUtil.validateEntitiesExistence(addedFeatureGroupIds,
222         new org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity(
223             licenseAgreement.getVendorLicenseModelId(), activeVersion, null),
224         featureGroupDao, VendorLicenseModelEntity.ENTITY_TYPE);
225
226     UniqueValueUtil.updateUniqueValue(VendorLicenseConstants.UniqueValues.LICENSE_AGREEMENT_NAME,
227         retrieved.getName(), licenseAgreement.getName(), licenseAgreement.getVendorLicenseModelId(),
228         licenseAgreement.getVersion().toString());
229     licenseAgreementDao.updateColumnsAndDeltaFeatureGroupIds(licenseAgreement, addedFeatureGroupIds,
230         removedFeatureGroupIds);
231
232     addFeatureGroupsToLicenseAgreementRef(addedFeatureGroupIds, licenseAgreement);
233     removeFeatureGroupsToLicenseAgreementRef(removedFeatureGroupIds, licenseAgreement);
234
235     vendorLicenseFacade
236         .updateVlmLastModificationTime(licenseAgreement.getVendorLicenseModelId(), activeVersion);
237   }
238
239   @Override
240   public LicenseAgreementModel getLicenseAgreementModel(String vlmId, Version version,
241                                                         String licenseAgreementId, String user) {
242     return vendorLicenseFacade.getLicenseAgreementModel(vlmId, version, licenseAgreementId, user);
243   }
244
245   @Override
246   public void deleteLicenseAgreement(String vlmId, String licenseAgreementId, String user) {
247     Version activeVersion =
248         getVersionInfo(vlmId, VersionableEntityAction.Write, user).getActiveVersion();
249     LicenseAgreementEntity input =
250         new LicenseAgreementEntity(vlmId, activeVersion, licenseAgreementId);
251     LicenseAgreementEntity retrieved = licenseAgreementDao.get(input);
252     VersioningUtil.validateEntityExistence(retrieved, input, VendorLicenseModelEntity.ENTITY_TYPE);
253
254     removeFeatureGroupsToLicenseAgreementRef(retrieved.getFeatureGroupIds(), retrieved);
255
256     licenseAgreementDao.delete(input);
257     UniqueValueUtil.deleteUniqueValue(VendorLicenseConstants.UniqueValues.LICENSE_AGREEMENT_NAME,
258         retrieved.getVendorLicenseModelId(), retrieved.getVersion().toString(),
259         retrieved.getName());
260
261     vendorLicenseFacade
262         .updateVlmLastModificationTime(input.getVendorLicenseModelId(), input.getVersion());
263   }
264
265   @Override
266   public Collection<org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity> listFeatureGroups(
267       String vlmId, Version version,
268       String user) {
269     return featureGroupDao
270         .list(new org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity(vlmId, VersioningUtil
271             .resolveVersion(version, getVersionInfo(vlmId, VersionableEntityAction.Read, user)),
272             null));
273   }
274
275   @Override
276   public org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity createFeatureGroup(
277       org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity featureGroup, String user) {
278     return vendorLicenseFacade.createFeatureGroup(featureGroup, user);
279   }
280
281   @Override
282   public void updateFeatureGroup(
283       org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity featureGroup,
284       Set<String> addedLicenseKeyGroups,
285       Set<String> removedLicenseKeyGroups,
286       Set<String> addedEntitlementPools,
287       Set<String> removedEntitlementPools,
288       String user) {
289     Version activeVersion =
290         getVersionInfo(featureGroup.getVendorLicenseModelId(), VersionableEntityAction.Write, user)
291             .getActiveVersion();
292     featureGroup.setVersion(activeVersion);
293
294     org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity retrieved =
295         featureGroupDao.get(featureGroup);
296     VersioningUtil
297         .validateEntityExistence(retrieved, featureGroup, VendorLicenseModelEntity.ENTITY_TYPE);
298
299     VersioningUtil.validateContainedEntitiesExistence(new LicenseKeyGroupEntity().getEntityType(),
300         removedLicenseKeyGroups, retrieved, retrieved.getLicenseKeyGroupIds());
301     VersioningUtil.validateContainedEntitiesExistence(new EntitlementPoolEntity().getEntityType(),
302         removedEntitlementPools, retrieved, retrieved.getEntitlementPoolIds());
303
304     VersioningUtil.validateEntitiesExistence(addedLicenseKeyGroups,
305         new LicenseKeyGroupEntity(featureGroup.getVendorLicenseModelId(), activeVersion, null),
306         licenseKeyGroupDao, VendorLicenseModelEntity.ENTITY_TYPE);
307     VersioningUtil.validateEntitiesExistence(addedEntitlementPools,
308         new EntitlementPoolEntity(featureGroup.getVendorLicenseModelId(), activeVersion, null),
309         entitlementPoolDao, VendorLicenseModelEntity.ENTITY_TYPE);
310     UniqueValueUtil.updateUniqueValue(VendorLicenseConstants.UniqueValues.FEATURE_GROUP_NAME,
311         retrieved.getName(), featureGroup.getName(), featureGroup.getVendorLicenseModelId(),
312         featureGroup.getVersion().toString());
313
314     addLicenseKeyGroupsToFeatureGroupsRef(addedLicenseKeyGroups, featureGroup);
315     removeLicenseKeyGroupsToFeatureGroupsRef(removedLicenseKeyGroups, featureGroup);
316     addEntitlementPoolsToFeatureGroupsRef(addedEntitlementPools, featureGroup);
317     removeEntitlementPoolsToFeatureGroupsRef(removedEntitlementPools, featureGroup);
318
319     featureGroupDao.updateFeatureGroup(featureGroup, addedEntitlementPools, removedEntitlementPools,
320         addedLicenseKeyGroups, removedLicenseKeyGroups);
321
322     vendorLicenseFacade
323         .updateVlmLastModificationTime(featureGroup.getVendorLicenseModelId(), activeVersion);
324   }
325
326   @Override
327   public FeatureGroupModel getFeatureGroupModel(
328       org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity featureGroup, String user) {
329     return vendorLicenseFacade.getFeatureGroupModel(featureGroup, user);
330   }
331
332   @Override
333   public void deleteFeatureGroup(
334       org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity featureGroup, String user) {
335     Version activeVersion =
336         getVersionInfo(featureGroup.getVendorLicenseModelId(), VersionableEntityAction.Write, user)
337             .getActiveVersion();
338     featureGroup.setVersion(activeVersion);
339     org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity retrieved =
340         featureGroupDao.get(featureGroup);
341     VersioningUtil
342         .validateEntityExistence(retrieved, featureGroup, VendorLicenseModelEntity.ENTITY_TYPE);
343
344     removeLicenseKeyGroupsToFeatureGroupsRef(retrieved.getLicenseKeyGroupIds(), featureGroup);
345     removeEntitlementPoolsToFeatureGroupsRef(retrieved.getEntitlementPoolIds(), featureGroup);
346
347     for (String licenceAgreementId : retrieved.getReferencingLicenseAgreements()) {
348       licenseAgreementDao.removeFeatureGroup(
349           new LicenseAgreementEntity(featureGroup.getVendorLicenseModelId(), activeVersion,
350               licenceAgreementId), featureGroup.getId());
351     }
352
353     featureGroupDao.delete(featureGroup);
354     UniqueValueUtil.deleteUniqueValue(VendorLicenseConstants.UniqueValues.FEATURE_GROUP_NAME,
355         retrieved.getVendorLicenseModelId(), retrieved.getVersion().toString(),
356         retrieved.getName());
357
358     vendorLicenseFacade.updateVlmLastModificationTime(featureGroup.getVendorLicenseModelId(),
359         featureGroup.getVersion());
360   }
361
362   @Override
363   public Collection<EntitlementPoolEntity> listEntitlementPools(String vlmId, Version version,
364                                                                 String user) {
365     return vendorLicenseFacade.listEntitlementPools(vlmId, version, user);
366   }
367
368   @Override
369   public EntitlementPoolEntity createEntitlementPool(EntitlementPoolEntity entitlementPool,
370                                                      String user) {
371     return vendorLicenseFacade.createEntitlementPool(entitlementPool, user);
372   }
373
374   @Override
375   public void updateEntitlementPool(EntitlementPoolEntity entitlementPool, String user) {
376     Version activeVersion =
377         getVersionInfo(entitlementPool.getVendorLicenseModelId(), VersionableEntityAction.Write,
378             user).getActiveVersion();
379     vendorLicenseFacade
380         .updateVlmLastModificationTime(entitlementPool.getVendorLicenseModelId(), activeVersion);
381     vendorLicenseFacade.updateEntitlementPool(entitlementPool, user);
382   }
383
384   @Override
385   public EntitlementPoolEntity getEntitlementPool(EntitlementPoolEntity entitlementPool,
386                                                   String user) {
387     entitlementPool.setVersion(VersioningUtil.resolveVersion(entitlementPool.getVersion(),
388         getVersionInfo(entitlementPool.getVendorLicenseModelId(), VersionableEntityAction.Read,
389             user)));
390
391     EntitlementPoolEntity retrieved = entitlementPoolDao.get(entitlementPool);
392     VersioningUtil
393         .validateEntityExistence(retrieved, entitlementPool, VendorLicenseModelEntity.ENTITY_TYPE);
394     return retrieved;
395   }
396
397   @Override
398   public void deleteEntitlementPool(EntitlementPoolEntity entitlementPool, String user) {
399     Version activeVersion =
400         getVersionInfo(entitlementPool.getVendorLicenseModelId(), VersionableEntityAction.Write,
401             user).getActiveVersion();
402     entitlementPool.setVersion(activeVersion);
403
404     EntitlementPoolEntity retrieved = entitlementPoolDao.get(entitlementPool);
405     VersioningUtil
406         .validateEntityExistence(retrieved, entitlementPool, VendorLicenseModelEntity.ENTITY_TYPE);
407
408     for (String referencingFeatureGroupId : retrieved.getReferencingFeatureGroups()) {
409       featureGroupDao.removeEntitlementPool(
410           new org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity(
411               entitlementPool.getVendorLicenseModelId(), activeVersion,
412               referencingFeatureGroupId), entitlementPool.getId());
413     }
414
415     entitlementPoolDao.delete(entitlementPool);
416     UniqueValueUtil.deleteUniqueValue(VendorLicenseConstants.UniqueValues.ENTITLEMENT_POOL_NAME,
417         retrieved.getVendorLicenseModelId(), retrieved.getVersion().toString(),
418         retrieved.getName());
419
420     vendorLicenseFacade.updateVlmLastModificationTime(entitlementPool.getVendorLicenseModelId(),
421         entitlementPool.getVersion());
422   }
423
424   @Override
425   public Collection<LicenseKeyGroupEntity> listLicenseKeyGroups(String vlmId, Version version,
426                                                                 String user) {
427     return vendorLicenseFacade.listLicenseKeyGroups(vlmId, version, user);
428   }
429
430   @Override
431   public LicenseKeyGroupEntity createLicenseKeyGroup(LicenseKeyGroupEntity licenseKeyGroup,
432                                                      String user) {
433     return vendorLicenseFacade.createLicenseKeyGroup(licenseKeyGroup, user);
434   }
435
436   @Override
437   public void updateLicenseKeyGroup(LicenseKeyGroupEntity licenseKeyGroup, String user) {
438     Version activeVersion =
439         getVersionInfo(licenseKeyGroup.getVendorLicenseModelId(), VersionableEntityAction.Write,
440             user).getActiveVersion();
441     vendorLicenseFacade
442         .updateVlmLastModificationTime(licenseKeyGroup.getVendorLicenseModelId(), activeVersion);
443
444     vendorLicenseFacade.updateLicenseKeyGroup(licenseKeyGroup, user);
445   }
446
447   @Override
448   public LicenseKeyGroupEntity getLicenseKeyGroup(LicenseKeyGroupEntity licenseKeyGroup,
449                                                   String user) {
450     licenseKeyGroup.setVersion(VersioningUtil.resolveVersion(licenseKeyGroup.getVersion(),
451         getVersionInfo(licenseKeyGroup.getVendorLicenseModelId(), VersionableEntityAction.Read,
452             user)));
453
454     LicenseKeyGroupEntity retrieved = licenseKeyGroupDao.get(licenseKeyGroup);
455     VersioningUtil
456         .validateEntityExistence(retrieved, licenseKeyGroup, VendorLicenseModelEntity.ENTITY_TYPE);
457     return retrieved;
458   }
459
460   @Override
461   public void deleteLicenseKeyGroup(LicenseKeyGroupEntity licenseKeyGroup, String user) {
462     Version activeVersion =
463         getVersionInfo(licenseKeyGroup.getVendorLicenseModelId(), VersionableEntityAction.Write,
464             user).getActiveVersion();
465     licenseKeyGroup.setVersion(activeVersion);
466
467     LicenseKeyGroupEntity retrieved = licenseKeyGroupDao.get(licenseKeyGroup);
468     VersioningUtil
469         .validateEntityExistence(retrieved, licenseKeyGroup, VendorLicenseModelEntity.ENTITY_TYPE);
470
471     licenseKeyGroupDao.delete(licenseKeyGroup);
472     for (String referencingFeatureGroupId : retrieved.getReferencingFeatureGroups()) {
473       featureGroupDao.removeLicenseKeyGroup(
474           new org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity(
475               licenseKeyGroup.getVendorLicenseModelId(), activeVersion,
476               referencingFeatureGroupId), licenseKeyGroup.getId());
477     }
478     UniqueValueUtil.deleteUniqueValue(VendorLicenseConstants.UniqueValues.LICENSE_KEY_GROUP_NAME,
479         retrieved.getVendorLicenseModelId(), retrieved.getVersion().toString(),
480         retrieved.getName());
481
482     vendorLicenseFacade.updateVlmLastModificationTime(licenseKeyGroup.getVendorLicenseModelId(),
483         licenseKeyGroup.getVersion());
484   }
485
486   private void addFeatureGroupsToLicenseAgreementRef(Set<String> featureGroupIds,
487                                                      LicenseAgreementEntity licenseAgreement) {
488     if (featureGroupIds != null) {
489       for (String featureGroupId : featureGroupIds) {
490         featureGroupDao.addReferencingLicenseAgreement(
491             new org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity(
492                 licenseAgreement.getVendorLicenseModelId(),
493                 licenseAgreement.getVersion(), featureGroupId), licenseAgreement.getId());
494       }
495     }
496   }
497
498   private void removeFeatureGroupsToLicenseAgreementRef(Set<String> featureGroupIds,
499                                                         LicenseAgreementEntity licenseAgreement) {
500     if (featureGroupIds != null) {
501       for (String featureGroupId : featureGroupIds) {
502         featureGroupDao.removeReferencingLicenseAgreement(
503             new org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity(
504                 licenseAgreement.getVendorLicenseModelId(),
505                 licenseAgreement.getVersion(), featureGroupId), licenseAgreement.getId());
506       }
507     }
508   }
509
510   private void addLicenseKeyGroupsToFeatureGroupsRef(Set<String> licenseKeyGroupIds,
511               org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity featureGroup) {
512     if (licenseKeyGroupIds != null) {
513       for (String licenseKeyGroupId : licenseKeyGroupIds) {
514         licenseKeyGroupDao.addReferencingFeatureGroup(
515             new LicenseKeyGroupEntity(featureGroup.getVendorLicenseModelId(),
516                 featureGroup.getVersion(), licenseKeyGroupId), featureGroup.getId());
517       }
518     }
519   }
520
521   private void removeLicenseKeyGroupsToFeatureGroupsRef(Set<String> licenseKeyGroupIds,
522                org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity featureGroup) {
523     if (licenseKeyGroupIds != null) {
524       for (String licenseKeyGroupId : licenseKeyGroupIds) {
525         licenseKeyGroupDao.removeReferencingFeatureGroup(
526             new LicenseKeyGroupEntity(featureGroup.getVendorLicenseModelId(),
527                 featureGroup.getVersion(), licenseKeyGroupId), featureGroup.getId());
528       }
529     }
530   }
531
532   private void addEntitlementPoolsToFeatureGroupsRef(Set<String> entitlementPoolIds,
533               org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity featureGroup) {
534     if (entitlementPoolIds != null) {
535       for (String entitlementPoolId : entitlementPoolIds) {
536         entitlementPoolDao.addReferencingFeatureGroup(
537             new EntitlementPoolEntity(featureGroup.getVendorLicenseModelId(),
538                 featureGroup.getVersion(), entitlementPoolId), featureGroup.getId());
539       }
540     }
541   }
542
543   private void removeEntitlementPoolsToFeatureGroupsRef(Set<String> entitlementPoolIds,
544                org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity featureGroup) {
545     if (entitlementPoolIds != null) {
546       for (String entitlementPoolId : entitlementPoolIds) {
547         entitlementPoolDao.removeReferencingFeatureGroup(
548             new EntitlementPoolEntity(featureGroup.getVendorLicenseModelId(),
549                 featureGroup.getVersion(), entitlementPoolId), featureGroup.getId());
550       }
551     }
552   }
553
554   private VersionInfo getVersionInfo(String vendorLicenseModelId, VersionableEntityAction action,
555                                      String user) {
556     return vendorLicenseFacade.getVersionInfo(vendorLicenseModelId, action, user);
557   }
558 }