b2f2d488e9811988d8ba9fe79b5b952434552a27
[sdc.git] /
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.facade.impl;
22
23 import static org.openecomp.sdc.vendorlicense.VendorLicenseConstants.VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE;
24 import static org.openecomp.sdc.vendorlicense.errors.UncompletedVendorLicenseModelErrorType.SUBMIT_UNCOMPLETED_VLM_MSG_FG_MISSING_EP;
25 import static org.openecomp.sdc.vendorlicense.errors.UncompletedVendorLicenseModelErrorType.SUBMIT_UNCOMPLETED_VLM_MSG_LA_MISSING_FG;
26
27 import org.apache.commons.collections4.CollectionUtils;
28 import org.openecomp.core.util.UniqueValueUtil;
29 import org.openecomp.core.utilities.CommonMethods;
30 import org.openecomp.sdc.common.errors.CoreException;
31 import org.openecomp.sdc.common.errors.ErrorCode;
32 import org.openecomp.sdc.datatypes.error.ErrorLevel;
33 import org.openecomp.sdc.logging.api.Logger;
34 import org.openecomp.sdc.logging.api.LoggerFactory;
35 import org.openecomp.sdc.logging.context.impl.MdcDataDebugMessage;
36 import org.openecomp.sdc.logging.context.impl.MdcDataErrorMessage;
37 import org.openecomp.sdc.logging.types.LoggerConstants;
38 import org.openecomp.sdc.logging.types.LoggerErrorCode;
39 import org.openecomp.sdc.logging.types.LoggerErrorDescription;
40 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
41 import org.openecomp.sdc.vendorlicense.VendorLicenseConstants;
42 import org.openecomp.sdc.vendorlicense.dao.EntitlementPoolDao;
43 import org.openecomp.sdc.vendorlicense.dao.EntitlementPoolDaoFactory;
44 import org.openecomp.sdc.vendorlicense.dao.FeatureGroupDao;
45 import org.openecomp.sdc.vendorlicense.dao.FeatureGroupDaoFactory;
46 import org.openecomp.sdc.vendorlicense.dao.LicenseAgreementDao;
47 import org.openecomp.sdc.vendorlicense.dao.LicenseAgreementDaoFactory;
48 import org.openecomp.sdc.vendorlicense.dao.LicenseKeyGroupDao;
49 import org.openecomp.sdc.vendorlicense.dao.LicenseKeyGroupDaoFactory;
50 import org.openecomp.sdc.vendorlicense.dao.LimitDao;
51 import org.openecomp.sdc.vendorlicense.dao.LimitDaoFactory;
52 import org.openecomp.sdc.vendorlicense.dao.VendorLicenseModelDao;
53 import org.openecomp.sdc.vendorlicense.dao.VendorLicenseModelDaoFactory;
54 import org.openecomp.sdc.vendorlicense.dao.types.EntitlementPoolEntity;
55 import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity;
56 import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupModel;
57 import org.openecomp.sdc.vendorlicense.dao.types.LicenseAgreementEntity;
58 import org.openecomp.sdc.vendorlicense.dao.types.LicenseAgreementModel;
59 import org.openecomp.sdc.vendorlicense.dao.types.LicenseKeyGroupEntity;
60 import org.openecomp.sdc.vendorlicense.dao.types.LimitEntity;
61 import org.openecomp.sdc.vendorlicense.dao.types.VendorLicenseModelEntity;
62 import org.openecomp.sdc.vendorlicense.errors.SubmitUncompletedLicenseModelErrorBuilder;
63 import org.openecomp.sdc.vendorlicense.errors.VendorLicenseModelNotFoundErrorBuilder;
64 import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacade;
65 import org.openecomp.sdc.vendorlicense.types.VersionedVendorLicenseModel;
66 import org.openecomp.sdc.versioning.VersioningManager;
67 import org.openecomp.sdc.versioning.VersioningManagerFactory;
68 import org.openecomp.sdc.versioning.VersioningUtil;
69 import org.openecomp.sdc.versioning.dao.types.Version;
70 import org.openecomp.sdc.versioning.errors.RequestedVersionInvalidErrorBuilder;
71 import org.openecomp.sdc.versioning.errors.VersionableSubEntityNotFoundErrorBuilder;
72 import org.openecomp.sdc.versioning.types.VersionInfo;
73 import org.openecomp.sdc.versioning.types.VersionableEntityAction;
74
75 import java.util.*;
76
77 public class VendorLicenseFacadeImpl implements VendorLicenseFacade {
78
79   private static final VersioningManager versioningManager =
80       VersioningManagerFactory.getInstance().createInterface();
81
82   private static final VendorLicenseModelDao
83       vendorLicenseModelDao = VendorLicenseModelDaoFactory.getInstance().createInterface();
84   private static final LicenseAgreementDao
85       licenseAgreementDao = LicenseAgreementDaoFactory.getInstance().createInterface();
86   private static final FeatureGroupDao featureGroupDao =
87       FeatureGroupDaoFactory.getInstance().createInterface();
88   private static final EntitlementPoolDao
89       entitlementPoolDao = EntitlementPoolDaoFactory.getInstance().createInterface();
90   private static final LicenseKeyGroupDao
91       licenseKeyGroupDao = LicenseKeyGroupDaoFactory.getInstance().createInterface();
92   private static final LimitDao limitDao = LimitDaoFactory.getInstance().createInterface();
93   private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
94   private final Logger log = (Logger) LoggerFactory.getLogger(this.getClass().getName());
95
96   /**
97    * Instantiates a new Vendor license facade.
98    */
99   public VendorLicenseFacadeImpl() {
100     vendorLicenseModelDao.registerVersioning(VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE);
101     licenseAgreementDao.registerVersioning(VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE);
102     featureGroupDao.registerVersioning(VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE);
103     entitlementPoolDao.registerVersioning(VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE);
104     licenseKeyGroupDao.registerVersioning(VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE);
105     limitDao.registerVersioning(VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE);
106   }
107
108   @Override
109   public Version checkin(String vendorLicenseModelId, String user) {
110     Version newVersion = versioningManager
111         .checkin(VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE, vendorLicenseModelId, user, null);
112     updateVlmLastModificationTime(vendorLicenseModelId, newVersion);
113     return newVersion;
114   }
115
116   @Override
117   public Version submit(String vendorLicenseModelId, String user) {
118     validateCompletedVendorLicenseModel(vendorLicenseModelId, user);
119     Version newVersion = versioningManager
120         .submit(VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE, vendorLicenseModelId, user, null);
121     updateVlmLastModificationTime(vendorLicenseModelId, newVersion);
122     return newVersion;
123   }
124
125   @Override
126   public FeatureGroupEntity getFeatureGroup(FeatureGroupEntity featureGroup, String user) {
127     Version version = VersioningUtil.resolveVersion(featureGroup.getVersion(),
128         getVersionInfo(featureGroup.getVendorLicenseModelId(), VersionableEntityAction.Read,
129             user), user);
130     featureGroup.setVersion(version);
131     return getFeatureGroup(featureGroup);
132   }
133
134   private FeatureGroupEntity getFeatureGroup(FeatureGroupEntity featureGroup) {
135     FeatureGroupEntity retrieved = featureGroupDao.get(featureGroup);
136     VersioningUtil
137         .validateEntityExistence(retrieved, featureGroup, VendorLicenseModelEntity.ENTITY_TYPE);
138     if (retrieved.getManufacturerReferenceNumber() == null) {
139       updateManufacturerNumberInFeatureGroup(retrieved);
140     }
141     return retrieved;
142   }
143
144   @Override
145   public FeatureGroupModel getFeatureGroupModel(FeatureGroupEntity featureGroup, String user) {
146     FeatureGroupEntity retrieved = getFeatureGroup(featureGroup, user);
147
148     FeatureGroupModel featureGroupModel = new FeatureGroupModel();
149     featureGroupModel.setFeatureGroup(retrieved);
150
151     for (String licenseKeyGroupId : retrieved.getLicenseKeyGroupIds()) {
152       featureGroupModel.getLicenseKeyGroups().add(licenseKeyGroupDao.get(
153           new LicenseKeyGroupEntity(retrieved.getVendorLicenseModelId(), retrieved.getVersion(),
154               licenseKeyGroupId)));
155     }
156     for (String entitlementPoolId : retrieved.getEntitlementPoolIds()) {
157       featureGroupModel.getEntitlementPools().add(entitlementPoolDao.get(
158           new EntitlementPoolEntity(retrieved.getVendorLicenseModelId(), retrieved.getVersion(),
159               entitlementPoolId)));
160     }
161
162     return featureGroupModel;
163   }
164
165   @Override
166   public LicenseAgreementModel getLicenseAgreementModel(String vlmId, Version version,
167                                                         String licenseAgreementId, String user) {
168     LicenseAgreementEntity retrieved =
169         getLicenseAgreement(vlmId, version, licenseAgreementId, user);
170
171     LicenseAgreementModel licenseAgreementModel = new LicenseAgreementModel();
172     licenseAgreementModel.setLicenseAgreement(retrieved);
173
174     for (String featureGroupId : retrieved.getFeatureGroupIds()) {
175       licenseAgreementModel.getFeatureGroups().add(featureGroupDao
176           .get(new FeatureGroupEntity(vlmId, retrieved.getVersion(), featureGroupId)));
177     }
178
179     return licenseAgreementModel;
180   }
181
182   @Override
183   public EntitlementPoolEntity createEntitlementPool(EntitlementPoolEntity entitlementPool,
184                                                      String user) {
185     entitlementPool.setVersion(VersioningUtil.resolveVersion(entitlementPool.getVersion(),
186         getVersionInfo(entitlementPool.getVendorLicenseModelId(), VersionableEntityAction.Write,
187             user), user));
188     //entitlementPool.setId(CommonMethods.nextUuId());
189     entitlementPool.setVersionUuId(CommonMethods.nextUuId());
190     UniqueValueUtil.createUniqueValue(VendorLicenseConstants.UniqueValues.ENTITLEMENT_POOL_NAME,
191         entitlementPool.getVendorLicenseModelId(), entitlementPool.getVersion().toString(),
192         entitlementPool.getName());
193     entitlementPoolDao.create(entitlementPool);
194     updateVlmLastModificationTime(entitlementPool.getVendorLicenseModelId(),
195         entitlementPool.getVersion());
196     return entitlementPool;
197   }
198
199   @Override
200   public void updateEntitlementPool(EntitlementPoolEntity entitlementPool, String user) {
201     entitlementPool.setVersion(VersioningUtil.resolveVersion(entitlementPool.getVersion(),
202         getVersionInfo(entitlementPool.getVendorLicenseModelId(), VersionableEntityAction.Write,
203             user), user));
204     EntitlementPoolEntity retrieved = entitlementPoolDao.get(entitlementPool);
205     VersioningUtil
206         .validateEntityExistence(retrieved, entitlementPool, VendorLicenseModelEntity.ENTITY_TYPE);
207
208     UniqueValueUtil.updateUniqueValue(VendorLicenseConstants.UniqueValues.ENTITLEMENT_POOL_NAME,
209         retrieved.getName(), entitlementPool.getName(), entitlementPool.getVendorLicenseModelId(),
210         entitlementPool.getVersion().toString());
211     entitlementPool.setVersionUuId(CommonMethods.nextUuId());
212     entitlementPoolDao.update(entitlementPool);
213
214     updateVlmLastModificationTime(entitlementPool.getVendorLicenseModelId(),
215         entitlementPool.getVersion());
216
217   }
218
219   @Override
220   public Collection<LicenseKeyGroupEntity> listLicenseKeyGroups(String vlmId, Version version,
221                                                                 String user) {
222     return licenseKeyGroupDao.list(new LicenseKeyGroupEntity(vlmId, VersioningUtil
223         .resolveVersion(version, getVersionInfo(vlmId, VersionableEntityAction.Read, user), user),
224         null));
225   }
226
227   @Override
228   public Collection<EntitlementPoolEntity> listEntitlementPools(String vlmId, Version version,
229                                                                 String user) {
230     return entitlementPoolDao.list(new EntitlementPoolEntity(vlmId, VersioningUtil
231         .resolveVersion(version, getVersionInfo(vlmId, VersionableEntityAction.Read, user), user),
232         null));
233   }
234
235   @Override
236   public void updateLicenseKeyGroup(LicenseKeyGroupEntity licenseKeyGroup, String user) {
237     licenseKeyGroup.setVersion(VersioningUtil.resolveVersion(licenseKeyGroup.getVersion(),
238         getVersionInfo(licenseKeyGroup.getVendorLicenseModelId(), VersionableEntityAction.Write,
239             user), user));
240     LicenseKeyGroupEntity retrieved = licenseKeyGroupDao.get(licenseKeyGroup);
241     licenseKeyGroup.setVersionUuId((CommonMethods.nextUuId()));
242     VersioningUtil
243         .validateEntityExistence(retrieved, licenseKeyGroup, VendorLicenseModelEntity.ENTITY_TYPE);
244     UniqueValueUtil.updateUniqueValue(VendorLicenseConstants.UniqueValues.LICENSE_KEY_GROUP_NAME,
245         retrieved.getName(), licenseKeyGroup.getName(), licenseKeyGroup.getVendorLicenseModelId(),
246         licenseKeyGroup.getVersion().toString());
247     licenseKeyGroupDao.update(licenseKeyGroup);
248
249     updateVlmLastModificationTime(licenseKeyGroup.getVendorLicenseModelId(),
250         licenseKeyGroup.getVersion());
251   }
252
253   @Override
254   public LicenseKeyGroupEntity createLicenseKeyGroup(LicenseKeyGroupEntity licenseKeyGroup,
255                                                      String user) {
256     licenseKeyGroup.setVersion(VersioningUtil.resolveVersion(licenseKeyGroup.getVersion(),
257         getVersionInfo(licenseKeyGroup.getVendorLicenseModelId(), VersionableEntityAction.Write,
258             user), user));
259     //licenseKeyGroup.setId(CommonMethods.nextUuId());
260     licenseKeyGroup.setVersionUuId(CommonMethods.nextUuId());
261     UniqueValueUtil.createUniqueValue(VendorLicenseConstants.UniqueValues.LICENSE_KEY_GROUP_NAME,
262         licenseKeyGroup.getVendorLicenseModelId(), licenseKeyGroup.getVersion().toString(),
263         licenseKeyGroup.getName());
264     licenseKeyGroupDao.create(licenseKeyGroup);
265     updateVlmLastModificationTime(licenseKeyGroup.getVendorLicenseModelId(),
266         licenseKeyGroup.getVersion());
267     return licenseKeyGroup;
268   }
269
270   @Override
271   public VersionedVendorLicenseModel getVendorLicenseModel(String vlmId, Version version,
272                                                            String user) {
273     mdcDataDebugMessage.debugEntryMessage("VLM id", vlmId);
274
275     VersionInfo versionInfo = getVersionInfo(vlmId, VersionableEntityAction.Read, user);
276
277     VendorLicenseModelEntity vendorLicenseModel = vendorLicenseModelDao.get(
278         new VendorLicenseModelEntity(vlmId,
279             VersioningUtil.resolveVersion(version, versionInfo, user)));
280     if (vendorLicenseModel == null) {
281       MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
282           LoggerTragetServiceName.GET_VLM, ErrorLevel.ERROR.name(),
283           LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.ENTITY_NOT_FOUND);
284       throw new CoreException(new VendorLicenseModelNotFoundErrorBuilder(vlmId).build());
285     }
286
287     mdcDataDebugMessage.debugExitMessage("VLM id", vlmId);
288     return new VersionedVendorLicenseModel(vendorLicenseModel, versionInfo);
289   }
290
291   @Override
292   public VendorLicenseModelEntity createVendorLicenseModel(
293       VendorLicenseModelEntity vendorLicenseModelEntity, String user) {
294
295     mdcDataDebugMessage.debugEntryMessage(null, null);
296
297     UniqueValueUtil.validateUniqueValue(VendorLicenseConstants.UniqueValues.VENDOR_NAME,
298         vendorLicenseModelEntity.getVendorName());
299     //vendorLicenseModelEntity.setId(CommonMethods.nextUuId());
300
301     vendorLicenseModelDao.create(vendorLicenseModelEntity);
302     UniqueValueUtil.createUniqueValue(VendorLicenseConstants.UniqueValues.VENDOR_NAME,
303         vendorLicenseModelEntity.getVendorName());
304
305     Version version = versioningManager
306         .create(VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE, vendorLicenseModelEntity.getId(), user);
307     vendorLicenseModelEntity.setVersion(version);
308
309     mdcDataDebugMessage.debugExitMessage(null, null);
310     return vendorLicenseModelEntity;
311   }
312
313   @Override
314   public LicenseAgreementEntity createLicenseAgreement(LicenseAgreementEntity licenseAgreement,
315                                                        String user) {
316     Version version = VersioningUtil.resolveVersion(licenseAgreement.getVersion(),
317         getVersionInfo(licenseAgreement.getVendorLicenseModelId(), VersionableEntityAction.Write,
318             user), user);
319     licenseAgreement.setVersion(version);
320     //licenseAgreement.setId(CommonMethods.nextUuId());
321     VersioningUtil.validateEntitiesExistence(licenseAgreement.getFeatureGroupIds(),
322         new FeatureGroupEntity(licenseAgreement.getVendorLicenseModelId(), version, null),
323         featureGroupDao, VendorLicenseModelEntity.ENTITY_TYPE);
324
325     UniqueValueUtil.validateUniqueValue(VendorLicenseConstants.UniqueValues.LICENSE_AGREEMENT_NAME,
326         licenseAgreement.getVendorLicenseModelId(), licenseAgreement.getVersion().toString(),
327         licenseAgreement.getName());
328
329     licenseAgreementDao.create(licenseAgreement);
330     UniqueValueUtil.createUniqueValue(VendorLicenseConstants.UniqueValues.LICENSE_AGREEMENT_NAME,
331         licenseAgreement.getVendorLicenseModelId(), licenseAgreement.getVersion().toString(),
332         licenseAgreement.getName());
333     if (licenseAgreement.getFeatureGroupIds() != null) {
334       for (String addedFgId : licenseAgreement.getFeatureGroupIds()) {
335         featureGroupDao.addReferencingLicenseAgreement(
336             new FeatureGroupEntity(licenseAgreement.getVendorLicenseModelId(), version,
337                 addedFgId), licenseAgreement.getId());
338       }
339     }
340     updateVlmLastModificationTime(licenseAgreement.getVendorLicenseModelId(),
341         licenseAgreement.getVersion());
342
343     return licenseAgreement;
344   }
345
346   @Override
347   public FeatureGroupEntity createFeatureGroup(FeatureGroupEntity featureGroup, String user) {
348     Version version = VersioningUtil.resolveVersion(featureGroup.getVersion(),
349         getVersionInfo(featureGroup.getVendorLicenseModelId(), VersionableEntityAction.Write,
350             user), user);
351     //featureGroup.setId(CommonMethods.nextUuId());
352     featureGroup.setVersion(version);
353     VersioningUtil.validateEntitiesExistence(featureGroup.getLicenseKeyGroupIds(),
354         new LicenseKeyGroupEntity(featureGroup.getVendorLicenseModelId(), version, null),
355         licenseKeyGroupDao, VendorLicenseModelEntity.ENTITY_TYPE);
356     VersioningUtil.validateEntitiesExistence(featureGroup.getEntitlementPoolIds(),
357         new EntitlementPoolEntity(featureGroup.getVendorLicenseModelId(), version, null),
358         entitlementPoolDao, VendorLicenseModelEntity.ENTITY_TYPE);
359     UniqueValueUtil.validateUniqueValue(VendorLicenseConstants.UniqueValues.FEATURE_GROUP_NAME,
360         featureGroup.getVendorLicenseModelId(), featureGroup.getVersion().toString(),
361         featureGroup.getName());
362
363     featureGroupDao.create(featureGroup);
364     UniqueValueUtil.createUniqueValue(VendorLicenseConstants.UniqueValues.FEATURE_GROUP_NAME,
365         featureGroup.getVendorLicenseModelId(), featureGroup.getVersion().toString(),
366         featureGroup.getName());
367
368     if (featureGroup.getLicenseKeyGroupIds() != null) {
369       for (String addedLkgId : featureGroup.getLicenseKeyGroupIds()) {
370         licenseKeyGroupDao.addReferencingFeatureGroup(
371             new LicenseKeyGroupEntity(featureGroup.getVendorLicenseModelId(), version, addedLkgId),
372             featureGroup.getId());
373       }
374     }
375
376     if (featureGroup.getEntitlementPoolIds() != null) {
377       for (String addedEpId : featureGroup.getEntitlementPoolIds()) {
378         entitlementPoolDao.addReferencingFeatureGroup(
379             new EntitlementPoolEntity(featureGroup.getVendorLicenseModelId(), version, addedEpId),
380             featureGroup.getId());
381       }
382     }
383
384     updateVlmLastModificationTime(featureGroup.getVendorLicenseModelId(),
385         featureGroup.getVersion());
386
387     return featureGroup;
388   }
389
390   @Override
391   public Collection<FeatureGroupEntity> listFeatureGroups(String vlmId, Version version,
392                                                                 String user) {
393     Collection<FeatureGroupEntity> featureGroupEntities =
394         featureGroupDao.list(new FeatureGroupEntity(vlmId, VersioningUtil
395             .resolveVersion(version, getVersionInfo(vlmId, VersionableEntityAction.Read, user),
396                 user), null));
397     featureGroupEntities.stream()
398         .filter(fgEntity -> Objects.isNull(fgEntity.getManufacturerReferenceNumber()))
399         .forEach(fgEntity -> updateManufacturerNumberInFeatureGroup(fgEntity));
400     return featureGroupEntities;
401   }
402
403
404   @Override
405   public Collection<ErrorCode> validateLicensingData(String vlmId, Version version,
406                                                      String licenseAgreementId,
407                                                      Collection<String> featureGroupIds) {
408     try {
409       VersionInfo versionInfo = getVersionInfo(vlmId, VersionableEntityAction.Read, "");
410       if (version == null || !version.isFinal()
411           || !versionInfo.getViewableVersions().contains(version)) {
412         return Collections.singletonList(new RequestedVersionInvalidErrorBuilder().build());
413       }
414     } catch (CoreException exception) {
415       log.debug("",exception);
416       return Collections.singletonList(exception.code());
417     }
418
419     List<ErrorCode> errorMessages = new ArrayList<>();
420
421     try {
422       getLicenseAgreement(vlmId, licenseAgreementId, version);
423     } catch (CoreException exception) {
424       log.debug("",exception);
425       errorMessages.add(exception.code());
426     }
427
428     for (String featureGroupId : featureGroupIds) {
429       try {
430         FeatureGroupEntity featureGroup =
431             getFeatureGroup(new FeatureGroupEntity(vlmId, version, featureGroupId));
432         if (!featureGroup.getReferencingLicenseAgreements().contains(licenseAgreementId)) {
433           errorMessages.add(new VersionableSubEntityNotFoundErrorBuilder(
434               featureGroup.getEntityType(),
435               featureGroupId,
436               LicenseAgreementEntity.ENTITY_TYPE,
437               licenseAgreementId,
438               version).build());
439         }
440       } catch (CoreException exception) {
441         log.debug("",exception);
442         errorMessages.add(exception.code());
443       }
444     }
445
446     return errorMessages;
447   }
448
449   @Override
450   public VersionInfo getVersionInfo(String vendorLicenseModelId, VersionableEntityAction action,
451                                     String user) {
452     return versioningManager
453         .getEntityVersionInfo(VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE, vendorLicenseModelId, user,
454             action);
455   }
456
457   @Override
458   public void updateVlmLastModificationTime(String vendorLicenseModelId, Version version) {
459     VendorLicenseModelEntity retrieved =
460         vendorLicenseModelDao.get(new VendorLicenseModelEntity(vendorLicenseModelId, version));
461     vendorLicenseModelDao.update(retrieved);
462   }
463
464   @Override
465   public LicenseAgreementEntity getLicenseAgreement(String vlmId, Version version,
466                                                     String licenseAgreementId, String user) {
467     return getLicenseAgreement(vlmId, licenseAgreementId, VersioningUtil
468         .resolveVersion(version, getVersionInfo(vlmId, VersionableEntityAction.Read, user), user));
469   }
470
471   @Override
472   public LimitEntity createLimit(LimitEntity limit, String user) {
473     limit.setVersion(VersioningUtil.resolveVersion(limit.getVersion(),
474         getVersionInfo(limit.getVendorLicenseModelId(), VersionableEntityAction.Write,
475             user), user));
476     //limit.setVersionUuId(CommonMethods.nextUuId());
477     limitDao.create(limit);
478     updateVlmLastModificationTime(limit.getVendorLicenseModelId(),
479         limit.getVersion());
480     return limit;
481   }
482
483   @Override
484   public Collection<LimitEntity> listLimits(String vlmId, Version version, String epLkgId,
485                                                       String user) {
486     return limitDao.list(new LimitEntity(vlmId, VersioningUtil
487         .resolveVersion(version, getVersionInfo(vlmId, VersionableEntityAction.Read, user), user),
488         epLkgId, null));
489
490   }
491
492   @Override
493   public void updateLimit(LimitEntity limit, String user) {
494     limit.setVersion(VersioningUtil.resolveVersion(limit.getVersion(),
495         getVersionInfo(limit.getVendorLicenseModelId(), VersionableEntityAction.Write,
496             user), user));
497     //limit.setVersionUuId(CommonMethods.nextUuId());
498     limitDao.update(limit);
499     updateVlmLastModificationTime(limit.getVendorLicenseModelId(),
500         limit.getVersion());
501   }
502
503   private LicenseAgreementEntity getLicenseAgreement(String vlmId, String licenseAgreementId,
504                                                      Version version) {
505     LicenseAgreementEntity input = new LicenseAgreementEntity(vlmId, version, licenseAgreementId);
506     LicenseAgreementEntity retrieved = licenseAgreementDao.get(input);
507     VersioningUtil.validateEntityExistence(retrieved, input, VendorLicenseModelEntity.ENTITY_TYPE);
508     return retrieved;
509   }
510
511   private void updateManufacturerNumberInFeatureGroup(FeatureGroupEntity featureGroupEntity) {
512     if (CollectionUtils.isNotEmpty(featureGroupEntity.getEntitlementPoolIds())) {
513       Object[] entitlementPoolIdsList = featureGroupEntity.getEntitlementPoolIds().toArray();
514       if (entitlementPoolIdsList != null && entitlementPoolIdsList.length > 0) {
515         String entitlementPoolId = entitlementPoolIdsList[0].toString();
516         EntitlementPoolEntity entitlementPoolEntity =
517             new EntitlementPoolEntity(featureGroupEntity.getVendorLicenseModelId(),
518                 featureGroupEntity.getVersion(), entitlementPoolId);
519         entitlementPoolEntity = entitlementPoolDao.get(entitlementPoolEntity);
520         featureGroupEntity.setManufacturerReferenceNumber(
521             entitlementPoolDao.getManufacturerReferenceNumber(entitlementPoolEntity));
522         featureGroupDao.update(featureGroupEntity);
523       }
524     }
525   }
526   private void validateCompletedVendorLicenseModel(String vendorLicenseModelId, String user) {
527     Collection<String> allFeatureGroupEntities = new HashSet<>();
528     Version version = VersioningUtil.resolveVersion(null,
529             getVersionInfo(vendorLicenseModelId, VersionableEntityAction.Read, user), user);
530     Collection<LicenseAgreementEntity> licenseAgreements = licenseAgreementDao
531             .list(new LicenseAgreementEntity(vendorLicenseModelId, version, null));
532
533     if (CollectionUtils.isNotEmpty(licenseAgreements)) {
534       licenseAgreements.forEach(licenseAgreement -> {
535         if (CollectionUtils.isEmpty(licenseAgreement.getFeatureGroupIds())) {
536           MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
537                   LoggerTragetServiceName.SUBMIT_ENTITY, ErrorLevel.ERROR.name(),
538                   LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.SUBMIT_ENTITY);
539           throw new CoreException(
540                   new SubmitUncompletedLicenseModelErrorBuilder(SUBMIT_UNCOMPLETED_VLM_MSG_LA_MISSING_FG).build());
541         }
542         allFeatureGroupEntities.addAll(licenseAgreement.getFeatureGroupIds());
543       });
544
545       allFeatureGroupEntities.forEach(fg -> {
546         FeatureGroupEntity featureGroupEntity = featureGroupDao.get(new FeatureGroupEntity(vendorLicenseModelId, version, fg));
547         if(CollectionUtils.isEmpty(featureGroupEntity.getEntitlementPoolIds())) {
548           MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
549                   LoggerTragetServiceName.SUBMIT_ENTITY, ErrorLevel.ERROR.name(),
550                   LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.SUBMIT_ENTITY);
551           throw new CoreException(
552                   new SubmitUncompletedLicenseModelErrorBuilder(SUBMIT_UNCOMPLETED_VLM_MSG_FG_MISSING_EP).build());
553         }
554       });
555     }
556   }
557 }