[SDC] Onboarding 1710 rebase.
[sdc.git] / openecomp-be / lib / openecomp-sdc-vendor-license-lib / openecomp-sdc-vendor-license-core / src / main / java / org / openecomp / sdc / vendorlicense / facade / impl / VendorLicenseFacadeImpl.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.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.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.ArrayList;
74 import java.util.Collection;
75 import java.util.Collections;
76 import java.util.List;
77
78 public class VendorLicenseFacadeImpl implements VendorLicenseFacade {
79
80   private static final VersioningManager versioningManager =
81       VersioningManagerFactory.getInstance().createInterface();
82
83   private static final VendorLicenseModelDao
84       vendorLicenseModelDao = VendorLicenseModelDaoFactory.getInstance().createInterface();
85   private static final LicenseAgreementDao
86       licenseAgreementDao = LicenseAgreementDaoFactory.getInstance().createInterface();
87   private static final FeatureGroupDao featureGroupDao =
88       FeatureGroupDaoFactory.getInstance().createInterface();
89   private static final EntitlementPoolDao
90       entitlementPoolDao = EntitlementPoolDaoFactory.getInstance().createInterface();
91   private static final LicenseKeyGroupDao
92       licenseKeyGroupDao = LicenseKeyGroupDaoFactory.getInstance().createInterface();
93   private static final LimitDao limitDao = LimitDaoFactory.getInstance().createInterface();
94   private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
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
132
133     return getFeatureGroup(featureGroup);
134   }
135
136   private FeatureGroupEntity getFeatureGroup(FeatureGroupEntity featureGroup) {
137     FeatureGroupEntity retrieved = featureGroupDao.get(featureGroup);
138     VersioningUtil
139         .validateEntityExistence(retrieved, featureGroup, VendorLicenseModelEntity.ENTITY_TYPE);
140     if(retrieved.getManufacturerReferenceNumber() == null){
141       Object[] entitlementPoolIdsList = retrieved.getEntitlementPoolIds().toArray();
142       if(entitlementPoolIdsList != null && entitlementPoolIdsList.length > 0){
143         String entitlementPoolId = entitlementPoolIdsList[0].toString();
144         EntitlementPoolEntity entitlementPoolEntity = new EntitlementPoolEntity(retrieved.getVendorLicenseModelId(),
145                 retrieved.getVersion(), entitlementPoolId);
146         entitlementPoolEntity = entitlementPoolDao.get(entitlementPoolEntity);
147         retrieved.setManufacturerReferenceNumber(entitlementPoolDao.getManufacturerReferenceNumber(
148                 entitlementPoolEntity));
149         featureGroupDao.update(retrieved);
150       }
151     }
152
153     return retrieved;
154   }
155
156   @Override
157   public FeatureGroupModel getFeatureGroupModel(FeatureGroupEntity featureGroup, String user) {
158     FeatureGroupEntity retrieved = getFeatureGroup(featureGroup, user);
159
160     FeatureGroupModel featureGroupModel = new FeatureGroupModel();
161     featureGroupModel.setFeatureGroup(retrieved);
162
163     for (String licenseKeyGroupId : retrieved.getLicenseKeyGroupIds()) {
164       featureGroupModel.getLicenseKeyGroups().add(licenseKeyGroupDao.get(
165           new LicenseKeyGroupEntity(retrieved.getVendorLicenseModelId(), retrieved.getVersion(),
166               licenseKeyGroupId)));
167     }
168     for (String entitlementPoolId : retrieved.getEntitlementPoolIds()) {
169       featureGroupModel.getEntitlementPools().add(entitlementPoolDao.get(
170           new EntitlementPoolEntity(retrieved.getVendorLicenseModelId(), retrieved.getVersion(),
171               entitlementPoolId)));
172     }
173
174     return featureGroupModel;
175   }
176
177   @Override
178   public LicenseAgreementModel getLicenseAgreementModel(String vlmId, Version version,
179                                                         String licenseAgreementId, String user) {
180     LicenseAgreementEntity retrieved =
181         getLicenseAgreement(vlmId, version, licenseAgreementId, user);
182
183     LicenseAgreementModel licenseAgreementModel = new LicenseAgreementModel();
184     licenseAgreementModel.setLicenseAgreement(retrieved);
185
186     for (String featureGroupId : retrieved.getFeatureGroupIds()) {
187       licenseAgreementModel.getFeatureGroups().add(featureGroupDao
188           .get(new FeatureGroupEntity(vlmId, retrieved.getVersion(), featureGroupId)));
189     }
190
191     return licenseAgreementModel;
192   }
193
194   @Override
195   public EntitlementPoolEntity createEntitlementPool(EntitlementPoolEntity entitlementPool,
196                                                      String user) {
197     entitlementPool.setVersion(VersioningUtil.resolveVersion(entitlementPool.getVersion(),
198         getVersionInfo(entitlementPool.getVendorLicenseModelId(), VersionableEntityAction.Write,
199             user), user));
200     //entitlementPool.setId(CommonMethods.nextUuId());
201     entitlementPool.setVersionUuId(CommonMethods.nextUuId());
202     UniqueValueUtil.createUniqueValue(VendorLicenseConstants.UniqueValues.ENTITLEMENT_POOL_NAME,
203         entitlementPool.getVendorLicenseModelId(), entitlementPool.getVersion().toString(),
204         entitlementPool.getName());
205     entitlementPoolDao.create(entitlementPool);
206     updateVlmLastModificationTime(entitlementPool.getVendorLicenseModelId(),
207         entitlementPool.getVersion());
208     return entitlementPool;
209   }
210
211   @Override
212   public void updateEntitlementPool(EntitlementPoolEntity entitlementPool, String user) {
213     entitlementPool.setVersion(VersioningUtil.resolveVersion(entitlementPool.getVersion(),
214         getVersionInfo(entitlementPool.getVendorLicenseModelId(), VersionableEntityAction.Write,
215             user), user));
216     EntitlementPoolEntity retrieved = entitlementPoolDao.get(entitlementPool);
217     VersioningUtil
218         .validateEntityExistence(retrieved, entitlementPool, VendorLicenseModelEntity.ENTITY_TYPE);
219
220     UniqueValueUtil.updateUniqueValue(VendorLicenseConstants.UniqueValues.ENTITLEMENT_POOL_NAME,
221         retrieved.getName(), entitlementPool.getName(), entitlementPool.getVendorLicenseModelId(),
222         entitlementPool.getVersion().toString());
223     entitlementPool.setVersionUuId(CommonMethods.nextUuId());
224     entitlementPoolDao.update(entitlementPool);
225
226     updateVlmLastModificationTime(entitlementPool.getVendorLicenseModelId(),
227         entitlementPool.getVersion());
228
229   }
230
231   @Override
232   public Collection<LicenseKeyGroupEntity> listLicenseKeyGroups(String vlmId, Version version,
233                                                                 String user) {
234     return licenseKeyGroupDao.list(new LicenseKeyGroupEntity(vlmId, VersioningUtil
235         .resolveVersion(version, getVersionInfo(vlmId, VersionableEntityAction.Read, user), user),
236         null));
237   }
238
239   @Override
240   public Collection<EntitlementPoolEntity> listEntitlementPools(String vlmId, Version version,
241                                                                 String user) {
242     return entitlementPoolDao.list(new EntitlementPoolEntity(vlmId, VersioningUtil
243         .resolveVersion(version, getVersionInfo(vlmId, VersionableEntityAction.Read, user), user),
244         null));
245   }
246
247   @Override
248   public void updateLicenseKeyGroup(LicenseKeyGroupEntity licenseKeyGroup, String user) {
249     licenseKeyGroup.setVersion(VersioningUtil.resolveVersion(licenseKeyGroup.getVersion(),
250         getVersionInfo(licenseKeyGroup.getVendorLicenseModelId(), VersionableEntityAction.Write,
251             user), user));
252     LicenseKeyGroupEntity retrieved = licenseKeyGroupDao.get(licenseKeyGroup);
253     licenseKeyGroup.setVersionUuId((CommonMethods.nextUuId()));
254     VersioningUtil
255         .validateEntityExistence(retrieved, licenseKeyGroup, VendorLicenseModelEntity.ENTITY_TYPE);
256     UniqueValueUtil.updateUniqueValue(VendorLicenseConstants.UniqueValues.LICENSE_KEY_GROUP_NAME,
257         retrieved.getName(), licenseKeyGroup.getName(), licenseKeyGroup.getVendorLicenseModelId(),
258         licenseKeyGroup.getVersion().toString());
259     licenseKeyGroupDao.update(licenseKeyGroup);
260
261     updateVlmLastModificationTime(licenseKeyGroup.getVendorLicenseModelId(),
262         licenseKeyGroup.getVersion());
263   }
264
265   @Override
266   public LicenseKeyGroupEntity createLicenseKeyGroup(LicenseKeyGroupEntity licenseKeyGroup,
267                                                      String user) {
268     licenseKeyGroup.setVersion(VersioningUtil.resolveVersion(licenseKeyGroup.getVersion(),
269         getVersionInfo(licenseKeyGroup.getVendorLicenseModelId(), VersionableEntityAction.Write,
270             user), user));
271     //licenseKeyGroup.setId(CommonMethods.nextUuId());
272     licenseKeyGroup.setVersionUuId(CommonMethods.nextUuId());
273     UniqueValueUtil.createUniqueValue(VendorLicenseConstants.UniqueValues.LICENSE_KEY_GROUP_NAME,
274         licenseKeyGroup.getVendorLicenseModelId(), licenseKeyGroup.getVersion().toString(),
275         licenseKeyGroup.getName());
276     licenseKeyGroupDao.create(licenseKeyGroup);
277     updateVlmLastModificationTime(licenseKeyGroup.getVendorLicenseModelId(),
278         licenseKeyGroup.getVersion());
279     return licenseKeyGroup;
280   }
281
282   @Override
283   public VersionedVendorLicenseModel getVendorLicenseModel(String vlmId, Version version,
284                                                            String user) {
285     mdcDataDebugMessage.debugEntryMessage("VLM id", vlmId);
286
287     VersionInfo versionInfo = getVersionInfo(vlmId, VersionableEntityAction.Read, user);
288
289     VendorLicenseModelEntity vendorLicenseModel = vendorLicenseModelDao.get(
290         new VendorLicenseModelEntity(vlmId,
291             VersioningUtil.resolveVersion(version, versionInfo, user)));
292     if (vendorLicenseModel == null) {
293       MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
294           LoggerTragetServiceName.GET_VLM, ErrorLevel.ERROR.name(),
295           LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.ENTITY_NOT_FOUND);
296       throw new CoreException(new VendorLicenseModelNotFoundErrorBuilder(vlmId).build());
297     }
298
299     mdcDataDebugMessage.debugExitMessage("VLM id", vlmId);
300     return new VersionedVendorLicenseModel(vendorLicenseModel, versionInfo);
301   }
302
303   @Override
304   public VendorLicenseModelEntity createVendorLicenseModel(
305       VendorLicenseModelEntity vendorLicenseModelEntity, String user) {
306
307     mdcDataDebugMessage.debugEntryMessage(null, null);
308
309     UniqueValueUtil.validateUniqueValue(VendorLicenseConstants.UniqueValues.VENDOR_NAME,
310         vendorLicenseModelEntity.getVendorName());
311     //vendorLicenseModelEntity.setId(CommonMethods.nextUuId());
312
313     vendorLicenseModelDao.create(vendorLicenseModelEntity);
314     UniqueValueUtil.createUniqueValue(VendorLicenseConstants.UniqueValues.VENDOR_NAME,
315         vendorLicenseModelEntity.getVendorName());
316
317     Version version = versioningManager
318         .create(VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE, vendorLicenseModelEntity.getId(), user);
319     vendorLicenseModelEntity.setVersion(version);
320
321     mdcDataDebugMessage.debugExitMessage(null, null);
322     return vendorLicenseModelEntity;
323   }
324
325   @Override
326   public LicenseAgreementEntity createLicenseAgreement(LicenseAgreementEntity licenseAgreement,
327                                                        String user) {
328     Version version = VersioningUtil.resolveVersion(licenseAgreement.getVersion(),
329         getVersionInfo(licenseAgreement.getVendorLicenseModelId(), VersionableEntityAction.Write,
330             user), user);
331     licenseAgreement.setVersion(version);
332     //licenseAgreement.setId(CommonMethods.nextUuId());
333     VersioningUtil.validateEntitiesExistence(licenseAgreement.getFeatureGroupIds(),
334         new FeatureGroupEntity(licenseAgreement.getVendorLicenseModelId(), version, null),
335         featureGroupDao, VendorLicenseModelEntity.ENTITY_TYPE);
336
337     UniqueValueUtil.validateUniqueValue(VendorLicenseConstants.UniqueValues.LICENSE_AGREEMENT_NAME,
338         licenseAgreement.getVendorLicenseModelId(), licenseAgreement.getVersion().toString(),
339         licenseAgreement.getName());
340
341     licenseAgreementDao.create(licenseAgreement);
342     UniqueValueUtil.createUniqueValue(VendorLicenseConstants.UniqueValues.LICENSE_AGREEMENT_NAME,
343         licenseAgreement.getVendorLicenseModelId(), licenseAgreement.getVersion().toString(),
344         licenseAgreement.getName());
345     if (licenseAgreement.getFeatureGroupIds() != null) {
346       for (String addedFgId : licenseAgreement.getFeatureGroupIds()) {
347         featureGroupDao.addReferencingLicenseAgreement(
348             new FeatureGroupEntity(licenseAgreement.getVendorLicenseModelId(), version,
349                 addedFgId), licenseAgreement.getId());
350       }
351     }
352     updateVlmLastModificationTime(licenseAgreement.getVendorLicenseModelId(),
353         licenseAgreement.getVersion());
354
355     return licenseAgreement;
356   }
357
358   @Override
359   public FeatureGroupEntity createFeatureGroup(FeatureGroupEntity featureGroup, String user) {
360     Version version = VersioningUtil.resolveVersion(featureGroup.getVersion(),
361         getVersionInfo(featureGroup.getVendorLicenseModelId(), VersionableEntityAction.Write,
362             user), user);
363     //featureGroup.setId(CommonMethods.nextUuId());
364     featureGroup.setVersion(version);
365     VersioningUtil.validateEntitiesExistence(featureGroup.getLicenseKeyGroupIds(),
366         new LicenseKeyGroupEntity(featureGroup.getVendorLicenseModelId(), version, null),
367         licenseKeyGroupDao, VendorLicenseModelEntity.ENTITY_TYPE);
368     VersioningUtil.validateEntitiesExistence(featureGroup.getEntitlementPoolIds(),
369         new EntitlementPoolEntity(featureGroup.getVendorLicenseModelId(), version, null),
370         entitlementPoolDao, VendorLicenseModelEntity.ENTITY_TYPE);
371     UniqueValueUtil.validateUniqueValue(VendorLicenseConstants.UniqueValues.FEATURE_GROUP_NAME,
372         featureGroup.getVendorLicenseModelId(), featureGroup.getVersion().toString(),
373         featureGroup.getName());
374
375     featureGroupDao.create(featureGroup);
376     UniqueValueUtil.createUniqueValue(VendorLicenseConstants.UniqueValues.FEATURE_GROUP_NAME,
377         featureGroup.getVendorLicenseModelId(), featureGroup.getVersion().toString(),
378         featureGroup.getName());
379
380     if (featureGroup.getLicenseKeyGroupIds() != null) {
381       for (String addedLkgId : featureGroup.getLicenseKeyGroupIds()) {
382         licenseKeyGroupDao.addReferencingFeatureGroup(
383             new LicenseKeyGroupEntity(featureGroup.getVendorLicenseModelId(), version, addedLkgId),
384             featureGroup.getId());
385       }
386     }
387
388     if (featureGroup.getEntitlementPoolIds() != null) {
389       for (String addedEpId : featureGroup.getEntitlementPoolIds()) {
390         entitlementPoolDao.addReferencingFeatureGroup(
391             new EntitlementPoolEntity(featureGroup.getVendorLicenseModelId(), version, addedEpId),
392             featureGroup.getId());
393       }
394     }
395
396     updateVlmLastModificationTime(featureGroup.getVendorLicenseModelId(),
397         featureGroup.getVersion());
398
399     return featureGroup;
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 validateCompletedVendorLicenseModel(String vendorLicenseModelId, String user) {
507     Version version = VersioningUtil.resolveVersion(null,
508         getVersionInfo(vendorLicenseModelId, VersionableEntityAction.Read, user), user);
509     Collection<LicenseAgreementEntity> licenseAgreements = licenseAgreementDao
510         .list(new LicenseAgreementEntity(vendorLicenseModelId, version, null));
511
512     if (licenseAgreements == null || licenseAgreements.isEmpty()) {
513       MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
514           LoggerTragetServiceName.SUBMIT_ENTITY, ErrorLevel.ERROR.name(),
515           LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.SUBMIT_ENTITY);
516       throw new CoreException(
517               new SubmitUncompletedLicenseModelErrorBuilder(SUBMIT_UNCOMPLETED_VLM_MSG_MISSING_LA).build());
518     }
519
520     for (LicenseAgreementEntity licenseAgreement : licenseAgreements) {
521         if (licenseAgreement.getFeatureGroupIds() == null || licenseAgreement.getFeatureGroupIds().isEmpty()) {
522         MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
523             LoggerTragetServiceName.SUBMIT_ENTITY, ErrorLevel.ERROR.name(),
524             LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.SUBMIT_ENTITY);
525         throw new CoreException(
526                 new SubmitUncompletedLicenseModelErrorBuilder(SUBMIT_UNCOMPLETED_VLM_MSG_LA_MISSING_FG).build());
527       }
528     }
529
530     Collection<FeatureGroupEntity> featureGroupEntities =
531         featureGroupDao.list(new FeatureGroupEntity(vendorLicenseModelId, version, null));
532     for (FeatureGroupEntity featureGroupEntity : featureGroupEntities) {
533         if (featureGroupEntity.getEntitlementPoolIds() == null || featureGroupEntity.getEntitlementPoolIds().isEmpty()) {
534         MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
535             LoggerTragetServiceName.SUBMIT_ENTITY, ErrorLevel.ERROR.name(),
536             LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.SUBMIT_ENTITY);
537         throw new CoreException(
538                 new SubmitUncompletedLicenseModelErrorBuilder(SUBMIT_UNCOMPLETED_VLM_MSG_FG_MISSING_EP).build());
539       }
540     }
541
542   }
543 }