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