2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.vendorlicense.facade.impl;
23 import static org.openecomp.sdc.common.errors.ValidationErrorBuilder.FIELD_VALIDATION_ERROR_ERR_ID;
24 import static org.openecomp.sdc.vendorlicense.VendorLicenseConstants.VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE;
25 import static org.openecomp.sdc.vendorlicense.errors.UncompletedVendorLicenseModelErrorType.SUBMIT_UNCOMPLETED_VLM_MSG_FG_MISSING_EP;
26 import static org.openecomp.sdc.vendorlicense.errors.UncompletedVendorLicenseModelErrorType.SUBMIT_UNCOMPLETED_VLM_MSG_LA_MISSING_FG;
28 import java.util.ArrayList;
29 import java.util.Collection;
30 import java.util.HashSet;
31 import java.util.List;
32 import java.util.Optional;
33 import org.apache.commons.collections4.CollectionUtils;
34 import org.openecomp.core.dao.UniqueValueDaoFactory;
35 import org.openecomp.core.util.UniqueValueUtil;
36 import org.openecomp.core.utilities.CommonMethods;
37 import org.openecomp.sdc.common.errors.CoreException;
38 import org.openecomp.sdc.common.errors.ErrorCategory;
39 import org.openecomp.sdc.common.errors.ErrorCode;
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.versioning.ItemManagerFactory;
65 import org.openecomp.sdc.versioning.VersioningUtil;
66 import org.openecomp.sdc.versioning.dao.types.Version;
67 import org.openecomp.sdc.versioning.dao.types.VersionStatus;
68 import org.openecomp.sdc.versioning.errors.VersionableSubEntityNotFoundErrorBuilder;
69 import org.openecomp.sdc.versioning.types.Item;
70 import org.openecomp.sdc.versioning.types.ItemStatus;
72 public class VendorLicenseFacadeImpl implements VendorLicenseFacade {
74 private static final VendorLicenseModelDao
75 vendorLicenseModelDao = VendorLicenseModelDaoFactory.getInstance().createInterface();
76 private static final LicenseAgreementDao
77 licenseAgreementDao = LicenseAgreementDaoFactory.getInstance().createInterface();
78 private static final FeatureGroupDao featureGroupDao =
79 FeatureGroupDaoFactory.getInstance().createInterface();
80 private static final EntitlementPoolDao
81 entitlementPoolDao = EntitlementPoolDaoFactory.getInstance().createInterface();
82 private static final LicenseKeyGroupDao
83 licenseKeyGroupDao = LicenseKeyGroupDaoFactory.getInstance().createInterface();
84 private static final LimitDao limitDao = LimitDaoFactory.getInstance().createInterface();
85 private static final UniqueValueUtil uniqueValueUtil = new UniqueValueUtil
86 (UniqueValueDaoFactory.getInstance().createInterface());
87 private static final ErrorCode USED_VLM_NOT_EXIST_ERROR =
88 new ErrorCode.ErrorCodeBuilder().withCategory(ErrorCategory.APPLICATION).withId(FIELD_VALIDATION_ERROR_ERR_ID)
89 .withMessage("The supplied vendor does not exist").build();
90 private static final ErrorCode USED_VLM_ARCHIVE_ERROR =
91 new ErrorCode.ErrorCodeBuilder().withCategory(ErrorCategory.APPLICATION).withId(FIELD_VALIDATION_ERROR_ERR_ID)
92 .withMessage("The supplied vendor is archived and therefore cannot be used").build();
93 private static final ErrorCode USED_VLM_IS_DRAFT_ERROR =
94 new ErrorCode.ErrorCodeBuilder().withCategory(ErrorCategory.APPLICATION).withId(FIELD_VALIDATION_ERROR_ERR_ID)
95 .withMessage("The supplied vendor version is draft and therefor can not be used").build();
99 * Instantiates a new Vendor license facade.
101 public VendorLicenseFacadeImpl() {
102 vendorLicenseModelDao.registerVersioning(VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE);
103 licenseAgreementDao.registerVersioning(VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE);
104 featureGroupDao.registerVersioning(VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE);
105 entitlementPoolDao.registerVersioning(VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE);
106 licenseKeyGroupDao.registerVersioning(VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE);
107 limitDao.registerVersioning(VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE);
111 public FeatureGroupEntity getFeatureGroup(FeatureGroupEntity featureGroup) {
112 FeatureGroupEntity retrieved = featureGroupDao.get(featureGroup);
114 .validateEntityExistence(retrieved, featureGroup, VendorLicenseModelEntity.ENTITY_TYPE);
120 public FeatureGroupModel getFeatureGroupModel(FeatureGroupEntity featureGroup) {
121 FeatureGroupEntity retrieved = getFeatureGroup(featureGroup);
123 FeatureGroupModel featureGroupModel = new FeatureGroupModel();
124 featureGroupModel.setFeatureGroup(retrieved);
126 for (String licenseKeyGroupId : retrieved.getLicenseKeyGroupIds()) {
127 featureGroupModel.getLicenseKeyGroups().add(licenseKeyGroupDao.get(
128 new LicenseKeyGroupEntity(retrieved.getVendorLicenseModelId(), retrieved.getVersion(),
129 licenseKeyGroupId)));
131 for (String entitlementPoolId : retrieved.getEntitlementPoolIds()) {
132 featureGroupModel.getEntitlementPools().add(entitlementPoolDao.get(
133 new EntitlementPoolEntity(retrieved.getVendorLicenseModelId(), retrieved.getVersion(),
134 entitlementPoolId)));
137 return featureGroupModel;
141 public LicenseAgreementModel getLicenseAgreementModel(String vlmId, Version version,
142 String licenseAgreementId) {
143 LicenseAgreementEntity retrieved =
144 getLicenseAgreement(vlmId, version, licenseAgreementId);
146 LicenseAgreementModel licenseAgreementModel = new LicenseAgreementModel();
147 licenseAgreementModel.setLicenseAgreement(retrieved);
149 for (String featureGroupId : retrieved.getFeatureGroupIds()) {
150 licenseAgreementModel.getFeatureGroups().add(featureGroupDao
151 .get(new FeatureGroupEntity(vlmId, retrieved.getVersion(), featureGroupId)));
154 return licenseAgreementModel;
158 public EntitlementPoolEntity createEntitlementPool(EntitlementPoolEntity entitlementPool) {
159 entitlementPool.setVersionUuId(CommonMethods.nextUuId());
160 uniqueValueUtil.createUniqueValue(VendorLicenseConstants.UniqueValues.ENTITLEMENT_POOL_NAME,
161 entitlementPool.getVendorLicenseModelId(), entitlementPool.getVersion().getId(),
162 entitlementPool.getName());
163 entitlementPoolDao.create(entitlementPool);
164 return entitlementPool;
168 public void updateEntitlementPool(EntitlementPoolEntity entitlementPool) {
169 EntitlementPoolEntity retrieved = entitlementPoolDao.get(entitlementPool);
171 .validateEntityExistence(retrieved, entitlementPool, VendorLicenseModelEntity.ENTITY_TYPE);
172 if (retrieved.equals(entitlementPool)) {
175 uniqueValueUtil.updateUniqueValue(VendorLicenseConstants.UniqueValues.ENTITLEMENT_POOL_NAME,
176 retrieved.getName(), entitlementPool.getName(), entitlementPool.getVendorLicenseModelId(),
177 entitlementPool.getVersion().getId());
178 entitlementPool.setVersionUuId(CommonMethods.nextUuId());
179 entitlementPoolDao.update(entitlementPool);
183 public Collection<LicenseKeyGroupEntity> listLicenseKeyGroups(String vlmId, Version version) {
184 return licenseKeyGroupDao.list(new LicenseKeyGroupEntity(vlmId, version, null));
188 public Collection<EntitlementPoolEntity> listEntitlementPools(String vlmId, Version version) {
189 return entitlementPoolDao.list(new EntitlementPoolEntity(vlmId, version, null));
193 public void updateLicenseKeyGroup(LicenseKeyGroupEntity licenseKeyGroup) {
194 LicenseKeyGroupEntity retrieved = licenseKeyGroupDao.get(licenseKeyGroup);
195 if (retrieved.equals(licenseKeyGroup)) {
198 licenseKeyGroup.setVersionUuId((CommonMethods.nextUuId()));
200 .validateEntityExistence(retrieved, licenseKeyGroup, VendorLicenseModelEntity.ENTITY_TYPE);
201 uniqueValueUtil.updateUniqueValue(VendorLicenseConstants.UniqueValues.LICENSE_KEY_GROUP_NAME,
202 retrieved.getName(), licenseKeyGroup.getName(), licenseKeyGroup.getVendorLicenseModelId(),
203 licenseKeyGroup.getVersion().getId());
204 licenseKeyGroupDao.update(licenseKeyGroup);
208 public LicenseKeyGroupEntity createLicenseKeyGroup(LicenseKeyGroupEntity licenseKeyGroup) {
209 licenseKeyGroup.setVersionUuId(CommonMethods.nextUuId());
210 uniqueValueUtil.createUniqueValue(VendorLicenseConstants.UniqueValues.LICENSE_KEY_GROUP_NAME,
211 licenseKeyGroup.getVendorLicenseModelId(), licenseKeyGroup.getVersion().getId(),
212 licenseKeyGroup.getName());
213 licenseKeyGroupDao.create(licenseKeyGroup);
214 return licenseKeyGroup;
218 public VendorLicenseModelEntity getVendorLicenseModel(String vlmId, Version version) {
219 VendorLicenseModelEntity vendorLicenseModel =
220 vendorLicenseModelDao.get(new VendorLicenseModelEntity(vlmId, version));
221 if (vendorLicenseModel == null) {
222 throw new CoreException(new VendorLicenseModelNotFoundErrorBuilder(vlmId).build());
224 return vendorLicenseModel;
228 public LicenseAgreementEntity createLicenseAgreement(LicenseAgreementEntity licenseAgreement) {
229 VersioningUtil.validateEntitiesExistence(licenseAgreement.getFeatureGroupIds(),
230 new FeatureGroupEntity(licenseAgreement.getVendorLicenseModelId(),
231 licenseAgreement.getVersion(),
233 featureGroupDao, VendorLicenseModelEntity.ENTITY_TYPE);
235 uniqueValueUtil.validateUniqueValue(VendorLicenseConstants.UniqueValues.LICENSE_AGREEMENT_NAME,
236 licenseAgreement.getVendorLicenseModelId(), licenseAgreement.getVersion().getId(),
237 licenseAgreement.getName());
239 licenseAgreementDao.create(licenseAgreement);
240 uniqueValueUtil.createUniqueValue(VendorLicenseConstants.UniqueValues.LICENSE_AGREEMENT_NAME,
241 licenseAgreement.getVendorLicenseModelId(), licenseAgreement.getVersion().getId(),
242 licenseAgreement.getName());
243 if (licenseAgreement.getFeatureGroupIds() != null) {
244 for (String addedFgId : licenseAgreement.getFeatureGroupIds()) {
245 featureGroupDao.addReferencingLicenseAgreement(
246 new FeatureGroupEntity(licenseAgreement.getVendorLicenseModelId(),
247 licenseAgreement.getVersion(),
248 addedFgId), licenseAgreement.getId());
251 return licenseAgreement;
255 public FeatureGroupEntity createFeatureGroup(FeatureGroupEntity featureGroup) {
256 VersioningUtil.validateEntitiesExistence(featureGroup.getLicenseKeyGroupIds(),
257 new LicenseKeyGroupEntity(featureGroup.getVendorLicenseModelId(), featureGroup.getVersion(),
259 licenseKeyGroupDao, VendorLicenseModelEntity.ENTITY_TYPE);
260 VersioningUtil.validateEntitiesExistence(featureGroup.getEntitlementPoolIds(),
261 new EntitlementPoolEntity(featureGroup.getVendorLicenseModelId(), featureGroup.getVersion(),
263 entitlementPoolDao, VendorLicenseModelEntity.ENTITY_TYPE);
264 uniqueValueUtil.validateUniqueValue(VendorLicenseConstants.UniqueValues.FEATURE_GROUP_NAME,
265 featureGroup.getVendorLicenseModelId(), featureGroup.getVersion().getId(),
266 featureGroup.getName());
268 featureGroupDao.create(featureGroup);
269 uniqueValueUtil.createUniqueValue(VendorLicenseConstants.UniqueValues.FEATURE_GROUP_NAME,
270 featureGroup.getVendorLicenseModelId(), featureGroup.getVersion().getId(),
271 featureGroup.getName());
273 if (featureGroup.getLicenseKeyGroupIds() != null) {
274 for (String addedLkgId : featureGroup.getLicenseKeyGroupIds()) {
275 licenseKeyGroupDao.addReferencingFeatureGroup(
276 new LicenseKeyGroupEntity(featureGroup.getVendorLicenseModelId(),
277 featureGroup.getVersion(), addedLkgId),
278 featureGroup.getId());
282 if (featureGroup.getEntitlementPoolIds() != null) {
283 for (String addedEpId : featureGroup.getEntitlementPoolIds()) {
284 entitlementPoolDao.addReferencingFeatureGroup(
285 new EntitlementPoolEntity(featureGroup.getVendorLicenseModelId(),
286 featureGroup.getVersion(), addedEpId), featureGroup.getId());
293 public Collection<FeatureGroupEntity> listFeatureGroups(String vlmId, Version version) {
294 return featureGroupDao.list(new FeatureGroupEntity(vlmId, version, null));
299 public Collection<ErrorCode> validateLicensingData(String vlmId, Version version,
300 String licenseAgreementId,
301 Collection<String> featureGroupIds) {
303 List<ErrorCode> errorMessages = new ArrayList<>();
306 getLicenseAgreement(vlmId, version, licenseAgreementId);
307 } catch (CoreException exception) {
308 errorMessages.add(exception.code());
311 for (String featureGroupId : featureGroupIds) {
313 FeatureGroupEntity featureGroup =
314 getFeatureGroup(new FeatureGroupEntity(vlmId, version, featureGroupId));
315 if (!featureGroup.getReferencingLicenseAgreements().contains(licenseAgreementId)) {
316 errorMessages.add(new VersionableSubEntityNotFoundErrorBuilder(
317 featureGroup.getEntityType(),
319 LicenseAgreementEntity.ENTITY_TYPE,
323 } catch (CoreException exception) {
324 errorMessages.add(exception.code());
328 return errorMessages;
332 public Optional<ErrorCode> validateVendorForUsage(String vlmId, Version version) {
333 Item vlm = ItemManagerFactory.getInstance().createInterface().get(vlmId);
335 return Optional.of(USED_VLM_NOT_EXIST_ERROR);
338 if (ItemStatus.ARCHIVED == vlm.getStatus()){
339 return Optional.of(USED_VLM_ARCHIVE_ERROR);
342 if(VersionStatus.Draft.equals(version.getStatus())){
343 return Optional.of(USED_VLM_IS_DRAFT_ERROR);
346 return Optional.empty();
350 public LicenseAgreementEntity getLicenseAgreement(String vlmId, Version version,
351 String licenseAgreementId) {
352 LicenseAgreementEntity input = new LicenseAgreementEntity(vlmId, version, licenseAgreementId);
353 LicenseAgreementEntity retrieved = licenseAgreementDao.get(input);
354 VersioningUtil.validateEntityExistence(retrieved, input, VendorLicenseModelEntity.ENTITY_TYPE);
359 public Collection<LimitEntity> listLimits(String vlmId, Version version, String epLkgId) {
360 return limitDao.list(new LimitEntity(vlmId, version, epLkgId, null));
364 public LimitEntity createLimit(LimitEntity limit) {
365 limitDao.create(limit);
370 public void updateLimit(LimitEntity limit) {
371 limitDao.update(limit);
375 public void validate(String vendorLicenseModelId, Version version) {
376 Collection<String> allFeatureGroupEntities = new HashSet<>();
377 Collection<LicenseAgreementEntity> licenseAgreements = licenseAgreementDao
378 .list(new LicenseAgreementEntity(vendorLicenseModelId, version, null));
380 if (CollectionUtils.isNotEmpty(licenseAgreements)) {
381 licenseAgreements.forEach(licenseAgreement -> {
382 if (CollectionUtils.isEmpty(licenseAgreement.getFeatureGroupIds())) {
383 throw new CoreException(
384 new SubmitUncompletedLicenseModelErrorBuilder(
385 SUBMIT_UNCOMPLETED_VLM_MSG_LA_MISSING_FG).build());
387 allFeatureGroupEntities.addAll(licenseAgreement.getFeatureGroupIds());
390 allFeatureGroupEntities.forEach(fg -> {
391 FeatureGroupEntity featureGroupEntity =
392 featureGroupDao.get(new FeatureGroupEntity(vendorLicenseModelId, version, fg));
393 if (CollectionUtils.isEmpty(featureGroupEntity.getEntitlementPoolIds())) {
394 throw new CoreException(
395 new SubmitUncompletedLicenseModelErrorBuilder(
396 SUBMIT_UNCOMPLETED_VLM_MSG_FG_MISSING_EP).build());