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.errors.VersionableSubEntityNotFoundErrorBuilder;
68 import org.openecomp.sdc.versioning.types.Item;
69 import org.openecomp.sdc.versioning.types.ItemStatus;
71 public class VendorLicenseFacadeImpl implements VendorLicenseFacade {
73 private static final VendorLicenseModelDao
74 vendorLicenseModelDao = VendorLicenseModelDaoFactory.getInstance().createInterface();
75 private static final LicenseAgreementDao
76 licenseAgreementDao = LicenseAgreementDaoFactory.getInstance().createInterface();
77 private static final FeatureGroupDao featureGroupDao =
78 FeatureGroupDaoFactory.getInstance().createInterface();
79 private static final EntitlementPoolDao
80 entitlementPoolDao = EntitlementPoolDaoFactory.getInstance().createInterface();
81 private static final LicenseKeyGroupDao
82 licenseKeyGroupDao = LicenseKeyGroupDaoFactory.getInstance().createInterface();
83 private static final LimitDao limitDao = LimitDaoFactory.getInstance().createInterface();
84 private static final UniqueValueUtil uniqueValueUtil = new UniqueValueUtil
85 (UniqueValueDaoFactory.getInstance().createInterface());
86 private static final ErrorCode USED_VLM_NOT_EXIST_ERROR =
87 new ErrorCode.ErrorCodeBuilder().withCategory(ErrorCategory.APPLICATION).withId(FIELD_VALIDATION_ERROR_ERR_ID)
88 .withMessage("The supplied vendor does not exist").build();
89 private static final ErrorCode USED_VLM_ARCHIVE_ERROR =
90 new ErrorCode.ErrorCodeBuilder().withCategory(ErrorCategory.APPLICATION).withId(FIELD_VALIDATION_ERROR_ERR_ID)
91 .withMessage("The supplied vendor is archived and therefore cannot be used").build();
94 * Instantiates a new Vendor license facade.
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);
106 public FeatureGroupEntity getFeatureGroup(FeatureGroupEntity featureGroup) {
107 FeatureGroupEntity retrieved = featureGroupDao.get(featureGroup);
109 .validateEntityExistence(retrieved, featureGroup, VendorLicenseModelEntity.ENTITY_TYPE);
115 public FeatureGroupModel getFeatureGroupModel(FeatureGroupEntity featureGroup) {
116 FeatureGroupEntity retrieved = getFeatureGroup(featureGroup);
118 FeatureGroupModel featureGroupModel = new FeatureGroupModel();
119 featureGroupModel.setFeatureGroup(retrieved);
121 for (String licenseKeyGroupId : retrieved.getLicenseKeyGroupIds()) {
122 featureGroupModel.getLicenseKeyGroups().add(licenseKeyGroupDao.get(
123 new LicenseKeyGroupEntity(retrieved.getVendorLicenseModelId(), retrieved.getVersion(),
124 licenseKeyGroupId)));
126 for (String entitlementPoolId : retrieved.getEntitlementPoolIds()) {
127 featureGroupModel.getEntitlementPools().add(entitlementPoolDao.get(
128 new EntitlementPoolEntity(retrieved.getVendorLicenseModelId(), retrieved.getVersion(),
129 entitlementPoolId)));
132 return featureGroupModel;
136 public LicenseAgreementModel getLicenseAgreementModel(String vlmId, Version version,
137 String licenseAgreementId) {
138 LicenseAgreementEntity retrieved =
139 getLicenseAgreement(vlmId, version, licenseAgreementId);
141 LicenseAgreementModel licenseAgreementModel = new LicenseAgreementModel();
142 licenseAgreementModel.setLicenseAgreement(retrieved);
144 for (String featureGroupId : retrieved.getFeatureGroupIds()) {
145 licenseAgreementModel.getFeatureGroups().add(featureGroupDao
146 .get(new FeatureGroupEntity(vlmId, retrieved.getVersion(), featureGroupId)));
149 return licenseAgreementModel;
153 public EntitlementPoolEntity createEntitlementPool(EntitlementPoolEntity entitlementPool) {
154 entitlementPool.setVersionUuId(CommonMethods.nextUuId());
155 uniqueValueUtil.createUniqueValue(VendorLicenseConstants.UniqueValues.ENTITLEMENT_POOL_NAME,
156 entitlementPool.getVendorLicenseModelId(), entitlementPool.getVersion().getId(),
157 entitlementPool.getName());
158 entitlementPoolDao.create(entitlementPool);
159 return entitlementPool;
163 public void updateEntitlementPool(EntitlementPoolEntity entitlementPool) {
164 EntitlementPoolEntity retrieved = entitlementPoolDao.get(entitlementPool);
166 .validateEntityExistence(retrieved, entitlementPool, VendorLicenseModelEntity.ENTITY_TYPE);
167 if (retrieved.equals(entitlementPool)) {
170 uniqueValueUtil.updateUniqueValue(VendorLicenseConstants.UniqueValues.ENTITLEMENT_POOL_NAME,
171 retrieved.getName(), entitlementPool.getName(), entitlementPool.getVendorLicenseModelId(),
172 entitlementPool.getVersion().getId());
173 entitlementPool.setVersionUuId(CommonMethods.nextUuId());
174 entitlementPoolDao.update(entitlementPool);
178 public Collection<LicenseKeyGroupEntity> listLicenseKeyGroups(String vlmId, Version version) {
179 return licenseKeyGroupDao.list(new LicenseKeyGroupEntity(vlmId, version, null));
183 public Collection<EntitlementPoolEntity> listEntitlementPools(String vlmId, Version version) {
184 return entitlementPoolDao.list(new EntitlementPoolEntity(vlmId, version, null));
188 public void updateLicenseKeyGroup(LicenseKeyGroupEntity licenseKeyGroup) {
189 LicenseKeyGroupEntity retrieved = licenseKeyGroupDao.get(licenseKeyGroup);
190 if (retrieved.equals(licenseKeyGroup)) {
193 licenseKeyGroup.setVersionUuId((CommonMethods.nextUuId()));
195 .validateEntityExistence(retrieved, licenseKeyGroup, VendorLicenseModelEntity.ENTITY_TYPE);
196 uniqueValueUtil.updateUniqueValue(VendorLicenseConstants.UniqueValues.LICENSE_KEY_GROUP_NAME,
197 retrieved.getName(), licenseKeyGroup.getName(), licenseKeyGroup.getVendorLicenseModelId(),
198 licenseKeyGroup.getVersion().getId());
199 licenseKeyGroupDao.update(licenseKeyGroup);
203 public LicenseKeyGroupEntity createLicenseKeyGroup(LicenseKeyGroupEntity licenseKeyGroup) {
204 licenseKeyGroup.setVersionUuId(CommonMethods.nextUuId());
205 uniqueValueUtil.createUniqueValue(VendorLicenseConstants.UniqueValues.LICENSE_KEY_GROUP_NAME,
206 licenseKeyGroup.getVendorLicenseModelId(), licenseKeyGroup.getVersion().getId(),
207 licenseKeyGroup.getName());
208 licenseKeyGroupDao.create(licenseKeyGroup);
209 return licenseKeyGroup;
213 public VendorLicenseModelEntity getVendorLicenseModel(String vlmId, Version version) {
214 VendorLicenseModelEntity vendorLicenseModel =
215 vendorLicenseModelDao.get(new VendorLicenseModelEntity(vlmId, version));
216 if (vendorLicenseModel == null) {
217 throw new CoreException(new VendorLicenseModelNotFoundErrorBuilder(vlmId).build());
219 return vendorLicenseModel;
223 public LicenseAgreementEntity createLicenseAgreement(LicenseAgreementEntity licenseAgreement) {
224 VersioningUtil.validateEntitiesExistence(licenseAgreement.getFeatureGroupIds(),
225 new FeatureGroupEntity(licenseAgreement.getVendorLicenseModelId(),
226 licenseAgreement.getVersion(),
228 featureGroupDao, VendorLicenseModelEntity.ENTITY_TYPE);
230 uniqueValueUtil.validateUniqueValue(VendorLicenseConstants.UniqueValues.LICENSE_AGREEMENT_NAME,
231 licenseAgreement.getVendorLicenseModelId(), licenseAgreement.getVersion().getId(),
232 licenseAgreement.getName());
234 licenseAgreementDao.create(licenseAgreement);
235 uniqueValueUtil.createUniqueValue(VendorLicenseConstants.UniqueValues.LICENSE_AGREEMENT_NAME,
236 licenseAgreement.getVendorLicenseModelId(), licenseAgreement.getVersion().getId(),
237 licenseAgreement.getName());
238 if (licenseAgreement.getFeatureGroupIds() != null) {
239 for (String addedFgId : licenseAgreement.getFeatureGroupIds()) {
240 featureGroupDao.addReferencingLicenseAgreement(
241 new FeatureGroupEntity(licenseAgreement.getVendorLicenseModelId(),
242 licenseAgreement.getVersion(),
243 addedFgId), licenseAgreement.getId());
246 return licenseAgreement;
250 public FeatureGroupEntity createFeatureGroup(FeatureGroupEntity featureGroup) {
251 VersioningUtil.validateEntitiesExistence(featureGroup.getLicenseKeyGroupIds(),
252 new LicenseKeyGroupEntity(featureGroup.getVendorLicenseModelId(), featureGroup.getVersion(),
254 licenseKeyGroupDao, VendorLicenseModelEntity.ENTITY_TYPE);
255 VersioningUtil.validateEntitiesExistence(featureGroup.getEntitlementPoolIds(),
256 new EntitlementPoolEntity(featureGroup.getVendorLicenseModelId(), featureGroup.getVersion(),
258 entitlementPoolDao, VendorLicenseModelEntity.ENTITY_TYPE);
259 uniqueValueUtil.validateUniqueValue(VendorLicenseConstants.UniqueValues.FEATURE_GROUP_NAME,
260 featureGroup.getVendorLicenseModelId(), featureGroup.getVersion().getId(),
261 featureGroup.getName());
263 featureGroupDao.create(featureGroup);
264 uniqueValueUtil.createUniqueValue(VendorLicenseConstants.UniqueValues.FEATURE_GROUP_NAME,
265 featureGroup.getVendorLicenseModelId(), featureGroup.getVersion().getId(),
266 featureGroup.getName());
268 if (featureGroup.getLicenseKeyGroupIds() != null) {
269 for (String addedLkgId : featureGroup.getLicenseKeyGroupIds()) {
270 licenseKeyGroupDao.addReferencingFeatureGroup(
271 new LicenseKeyGroupEntity(featureGroup.getVendorLicenseModelId(),
272 featureGroup.getVersion(), addedLkgId),
273 featureGroup.getId());
277 if (featureGroup.getEntitlementPoolIds() != null) {
278 for (String addedEpId : featureGroup.getEntitlementPoolIds()) {
279 entitlementPoolDao.addReferencingFeatureGroup(
280 new EntitlementPoolEntity(featureGroup.getVendorLicenseModelId(),
281 featureGroup.getVersion(), addedEpId), featureGroup.getId());
288 public Collection<FeatureGroupEntity> listFeatureGroups(String vlmId, Version version) {
289 return featureGroupDao.list(new FeatureGroupEntity(vlmId, version, null));
294 public Collection<ErrorCode> validateLicensingData(String vlmId, Version version,
295 String licenseAgreementId,
296 Collection<String> featureGroupIds) {
298 List<ErrorCode> errorMessages = new ArrayList<>();
301 getLicenseAgreement(vlmId, version, licenseAgreementId);
302 } catch (CoreException exception) {
303 errorMessages.add(exception.code());
306 for (String featureGroupId : featureGroupIds) {
308 FeatureGroupEntity featureGroup =
309 getFeatureGroup(new FeatureGroupEntity(vlmId, version, featureGroupId));
310 if (!featureGroup.getReferencingLicenseAgreements().contains(licenseAgreementId)) {
311 errorMessages.add(new VersionableSubEntityNotFoundErrorBuilder(
312 featureGroup.getEntityType(),
314 LicenseAgreementEntity.ENTITY_TYPE,
318 } catch (CoreException exception) {
319 errorMessages.add(exception.code());
323 return errorMessages;
327 public Optional<ErrorCode> validateVendorForUsage(String vlmId) {
328 Item vlm = ItemManagerFactory.getInstance().createInterface().get(vlmId);
330 ? Optional.of(USED_VLM_NOT_EXIST_ERROR)
331 : ItemStatus.ARCHIVED == vlm.getStatus()
332 ? Optional.of(USED_VLM_ARCHIVE_ERROR)
337 public LicenseAgreementEntity getLicenseAgreement(String vlmId, Version version,
338 String licenseAgreementId) {
339 LicenseAgreementEntity input = new LicenseAgreementEntity(vlmId, version, licenseAgreementId);
340 LicenseAgreementEntity retrieved = licenseAgreementDao.get(input);
341 VersioningUtil.validateEntityExistence(retrieved, input, VendorLicenseModelEntity.ENTITY_TYPE);
346 public Collection<LimitEntity> listLimits(String vlmId, Version version, String epLkgId) {
347 return limitDao.list(new LimitEntity(vlmId, version, epLkgId, null));
351 public LimitEntity createLimit(LimitEntity limit) {
352 limitDao.create(limit);
357 public void updateLimit(LimitEntity limit) {
358 limitDao.update(limit);
362 public void validate(String vendorLicenseModelId, Version version) {
363 Collection<String> allFeatureGroupEntities = new HashSet<>();
364 Collection<LicenseAgreementEntity> licenseAgreements = licenseAgreementDao
365 .list(new LicenseAgreementEntity(vendorLicenseModelId, version, null));
367 if (CollectionUtils.isNotEmpty(licenseAgreements)) {
368 licenseAgreements.forEach(licenseAgreement -> {
369 if (CollectionUtils.isEmpty(licenseAgreement.getFeatureGroupIds())) {
370 throw new CoreException(
371 new SubmitUncompletedLicenseModelErrorBuilder(
372 SUBMIT_UNCOMPLETED_VLM_MSG_LA_MISSING_FG).build());
374 allFeatureGroupEntities.addAll(licenseAgreement.getFeatureGroupIds());
377 allFeatureGroupEntities.forEach(fg -> {
378 FeatureGroupEntity featureGroupEntity =
379 featureGroupDao.get(new FeatureGroupEntity(vendorLicenseModelId, version, fg));
380 if (CollectionUtils.isEmpty(featureGroupEntity.getEntitlementPoolIds())) {
381 throw new CoreException(
382 new SubmitUncompletedLicenseModelErrorBuilder(
383 SUBMIT_UNCOMPLETED_VLM_MSG_FG_MISSING_EP).build());