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