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 org.apache.commons.collections4.CollectionUtils;
24 import org.openecomp.core.dao.UniqueValueDaoFactory;
25 import org.openecomp.core.util.UniqueValueUtil;
26 import org.openecomp.core.utilities.CommonMethods;
27 import org.openecomp.sdc.common.errors.CoreException;
28 import org.openecomp.sdc.common.errors.ErrorCode;
29 import org.openecomp.sdc.common.togglz.ToggleableFeature;
30 import org.openecomp.sdc.vendorlicense.VendorLicenseConstants;
31 import org.openecomp.sdc.vendorlicense.dao.*;
32 import org.openecomp.sdc.vendorlicense.dao.types.*;
33 import org.openecomp.sdc.vendorlicense.errors.SubmitUncompletedLicenseModelErrorBuilder;
34 import org.openecomp.sdc.vendorlicense.errors.VendorLicenseModelNotFoundErrorBuilder;
35 import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacade;
36 import org.openecomp.sdc.versioning.VersioningUtil;
37 import org.openecomp.sdc.versioning.dao.types.Version;
38 import org.openecomp.sdc.versioning.errors.VersionableSubEntityNotFoundErrorBuilder;
40 import java.util.ArrayList;
41 import java.util.Collection;
42 import java.util.HashSet;
43 import java.util.List;
44 import java.util.Objects;
47 import static org.openecomp.sdc.vendorlicense.VendorLicenseConstants.VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE;
48 import static org.openecomp.sdc.vendorlicense.errors.UncompletedVendorLicenseModelErrorType.SUBMIT_UNCOMPLETED_VLM_MSG_FG_MISSING_EP;
49 import static org.openecomp.sdc.vendorlicense.errors.UncompletedVendorLicenseModelErrorType.SUBMIT_UNCOMPLETED_VLM_MSG_LA_MISSING_FG;
51 public class VendorLicenseFacadeImpl implements VendorLicenseFacade {
52 private static final VendorLicenseModelDao
53 vendorLicenseModelDao = VendorLicenseModelDaoFactory.getInstance().createInterface();
54 private static final LicenseAgreementDao
55 licenseAgreementDao = LicenseAgreementDaoFactory.getInstance().createInterface();
56 private static final FeatureGroupDao featureGroupDao =
57 FeatureGroupDaoFactory.getInstance().createInterface();
58 private static final EntitlementPoolDao
59 entitlementPoolDao = EntitlementPoolDaoFactory.getInstance().createInterface();
60 private static final LicenseKeyGroupDao
61 licenseKeyGroupDao = LicenseKeyGroupDaoFactory.getInstance().createInterface();
62 private static final LimitDao limitDao = LimitDaoFactory.getInstance().createInterface();
63 private static final UniqueValueUtil uniqueValueUtil = new UniqueValueUtil
64 (UniqueValueDaoFactory.getInstance().createInterface());
66 * Instantiates a new Vendor license facade.
68 public VendorLicenseFacadeImpl() {
69 vendorLicenseModelDao.registerVersioning(VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE);
70 licenseAgreementDao.registerVersioning(VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE);
71 featureGroupDao.registerVersioning(VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE);
72 entitlementPoolDao.registerVersioning(VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE);
73 licenseKeyGroupDao.registerVersioning(VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE);
74 limitDao.registerVersioning(VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE);
78 public FeatureGroupEntity getFeatureGroup(FeatureGroupEntity featureGroup) {
79 FeatureGroupEntity retrieved = featureGroupDao.get(featureGroup);
81 .validateEntityExistence(retrieved, featureGroup, VendorLicenseModelEntity.ENTITY_TYPE);
82 if (!ToggleableFeature.MRN.isActive()) { //Remove updateManufacturerNumberInFeatureGroup() while removing remove toggle
83 if (retrieved.getManufacturerReferenceNumber() == null) {
84 updateManufacturerNumberInFeatureGroup(retrieved);
91 public FeatureGroupModel getFeatureGroupModel(FeatureGroupEntity featureGroup) {
92 FeatureGroupEntity retrieved = getFeatureGroup(featureGroup);
94 FeatureGroupModel featureGroupModel = new FeatureGroupModel();
95 featureGroupModel.setFeatureGroup(retrieved);
97 for (String licenseKeyGroupId : retrieved.getLicenseKeyGroupIds()) {
98 featureGroupModel.getLicenseKeyGroups().add(licenseKeyGroupDao.get(
99 new LicenseKeyGroupEntity(retrieved.getVendorLicenseModelId(), retrieved.getVersion(),
100 licenseKeyGroupId)));
102 for (String entitlementPoolId : retrieved.getEntitlementPoolIds()) {
103 featureGroupModel.getEntitlementPools().add(entitlementPoolDao.get(
104 new EntitlementPoolEntity(retrieved.getVendorLicenseModelId(), retrieved.getVersion(),
105 entitlementPoolId)));
108 return featureGroupModel;
112 public LicenseAgreementModel getLicenseAgreementModel(String vlmId, Version version,
113 String licenseAgreementId) {
114 LicenseAgreementEntity retrieved =
115 getLicenseAgreement(vlmId, version, licenseAgreementId);
117 LicenseAgreementModel licenseAgreementModel = new LicenseAgreementModel();
118 licenseAgreementModel.setLicenseAgreement(retrieved);
120 for (String featureGroupId : retrieved.getFeatureGroupIds()) {
121 licenseAgreementModel.getFeatureGroups().add(featureGroupDao
122 .get(new FeatureGroupEntity(vlmId, retrieved.getVersion(), featureGroupId)));
125 return licenseAgreementModel;
129 public EntitlementPoolEntity createEntitlementPool(EntitlementPoolEntity entitlementPool) {
130 entitlementPool.setVersionUuId(CommonMethods.nextUuId());
131 uniqueValueUtil.createUniqueValue(VendorLicenseConstants.UniqueValues.ENTITLEMENT_POOL_NAME,
132 entitlementPool.getVendorLicenseModelId(), entitlementPool.getVersion().getId(),
133 entitlementPool.getName());
134 entitlementPoolDao.create(entitlementPool);
135 return entitlementPool;
139 public void updateEntitlementPool(EntitlementPoolEntity entitlementPool) {
140 EntitlementPoolEntity retrieved = entitlementPoolDao.get(entitlementPool);
142 .validateEntityExistence(retrieved, entitlementPool, VendorLicenseModelEntity.ENTITY_TYPE);
143 if (retrieved.equals(entitlementPool)) {
146 uniqueValueUtil.updateUniqueValue(VendorLicenseConstants.UniqueValues.ENTITLEMENT_POOL_NAME,
147 retrieved.getName(), entitlementPool.getName(), entitlementPool.getVendorLicenseModelId(),
148 entitlementPool.getVersion().getId());
149 entitlementPool.setVersionUuId(CommonMethods.nextUuId());
150 entitlementPoolDao.update(entitlementPool);
154 public Collection<LicenseKeyGroupEntity> listLicenseKeyGroups(String vlmId, Version version) {
155 return licenseKeyGroupDao.list(new LicenseKeyGroupEntity(vlmId, version, null));
159 public Collection<EntitlementPoolEntity> listEntitlementPools(String vlmId, Version version) {
160 return entitlementPoolDao.list(new EntitlementPoolEntity(vlmId, version, null));
164 public void updateLicenseKeyGroup(LicenseKeyGroupEntity licenseKeyGroup) {
165 LicenseKeyGroupEntity retrieved = licenseKeyGroupDao.get(licenseKeyGroup);
166 if (retrieved.equals(licenseKeyGroup)) {
169 licenseKeyGroup.setVersionUuId((CommonMethods.nextUuId()));
171 .validateEntityExistence(retrieved, licenseKeyGroup, VendorLicenseModelEntity.ENTITY_TYPE);
172 uniqueValueUtil.updateUniqueValue(VendorLicenseConstants.UniqueValues.LICENSE_KEY_GROUP_NAME,
173 retrieved.getName(), licenseKeyGroup.getName(), licenseKeyGroup.getVendorLicenseModelId(),
174 licenseKeyGroup.getVersion().getId());
175 licenseKeyGroupDao.update(licenseKeyGroup);
179 public LicenseKeyGroupEntity createLicenseKeyGroup(LicenseKeyGroupEntity licenseKeyGroup) {
180 licenseKeyGroup.setVersionUuId(CommonMethods.nextUuId());
181 uniqueValueUtil.createUniqueValue(VendorLicenseConstants.UniqueValues.LICENSE_KEY_GROUP_NAME,
182 licenseKeyGroup.getVendorLicenseModelId(), licenseKeyGroup.getVersion().getId(),
183 licenseKeyGroup.getName());
184 licenseKeyGroupDao.create(licenseKeyGroup);
185 return licenseKeyGroup;
189 public VendorLicenseModelEntity getVendorLicenseModel(String vlmId, Version version) {
190 VendorLicenseModelEntity vendorLicenseModel =
191 vendorLicenseModelDao.get(new VendorLicenseModelEntity(vlmId, version));
192 if (vendorLicenseModel == null) {
193 throw new CoreException(new VendorLicenseModelNotFoundErrorBuilder(vlmId).build());
195 return vendorLicenseModel;
199 public LicenseAgreementEntity createLicenseAgreement(LicenseAgreementEntity licenseAgreement) {
200 VersioningUtil.validateEntitiesExistence(licenseAgreement.getFeatureGroupIds(),
201 new FeatureGroupEntity(licenseAgreement.getVendorLicenseModelId(),
202 licenseAgreement.getVersion(),
204 featureGroupDao, VendorLicenseModelEntity.ENTITY_TYPE);
206 uniqueValueUtil.validateUniqueValue(VendorLicenseConstants.UniqueValues.LICENSE_AGREEMENT_NAME,
207 licenseAgreement.getVendorLicenseModelId(), licenseAgreement.getVersion().getId(),
208 licenseAgreement.getName());
210 licenseAgreementDao.create(licenseAgreement);
211 uniqueValueUtil.createUniqueValue(VendorLicenseConstants.UniqueValues.LICENSE_AGREEMENT_NAME,
212 licenseAgreement.getVendorLicenseModelId(), licenseAgreement.getVersion().getId(),
213 licenseAgreement.getName());
214 if (licenseAgreement.getFeatureGroupIds() != null) {
215 for (String addedFgId : licenseAgreement.getFeatureGroupIds()) {
216 featureGroupDao.addReferencingLicenseAgreement(
217 new FeatureGroupEntity(licenseAgreement.getVendorLicenseModelId(),
218 licenseAgreement.getVersion(),
219 addedFgId), licenseAgreement.getId());
222 return licenseAgreement;
226 public FeatureGroupEntity createFeatureGroup(FeatureGroupEntity featureGroup) {
227 VersioningUtil.validateEntitiesExistence(featureGroup.getLicenseKeyGroupIds(),
228 new LicenseKeyGroupEntity(featureGroup.getVendorLicenseModelId(), featureGroup.getVersion(),
230 licenseKeyGroupDao, VendorLicenseModelEntity.ENTITY_TYPE);
231 VersioningUtil.validateEntitiesExistence(featureGroup.getEntitlementPoolIds(),
232 new EntitlementPoolEntity(featureGroup.getVendorLicenseModelId(), featureGroup.getVersion(),
234 entitlementPoolDao, VendorLicenseModelEntity.ENTITY_TYPE);
235 uniqueValueUtil.validateUniqueValue(VendorLicenseConstants.UniqueValues.FEATURE_GROUP_NAME,
236 featureGroup.getVendorLicenseModelId(), featureGroup.getVersion().getId(),
237 featureGroup.getName());
239 featureGroupDao.create(featureGroup);
240 uniqueValueUtil.createUniqueValue(VendorLicenseConstants.UniqueValues.FEATURE_GROUP_NAME,
241 featureGroup.getVendorLicenseModelId(), featureGroup.getVersion().getId(),
242 featureGroup.getName());
244 if (featureGroup.getLicenseKeyGroupIds() != null) {
245 for (String addedLkgId : featureGroup.getLicenseKeyGroupIds()) {
246 licenseKeyGroupDao.addReferencingFeatureGroup(
247 new LicenseKeyGroupEntity(featureGroup.getVendorLicenseModelId(),
248 featureGroup.getVersion(), addedLkgId),
249 featureGroup.getId());
253 if (featureGroup.getEntitlementPoolIds() != null) {
254 for (String addedEpId : featureGroup.getEntitlementPoolIds()) {
255 entitlementPoolDao.addReferencingFeatureGroup(
256 new EntitlementPoolEntity(featureGroup.getVendorLicenseModelId(),
257 featureGroup.getVersion(), addedEpId), featureGroup.getId());
264 public Collection<FeatureGroupEntity> listFeatureGroups(String vlmId, Version version) {
265 Collection<FeatureGroupEntity> featureGroupEntities =
266 featureGroupDao.list(new FeatureGroupEntity(vlmId, version, null));
267 featureGroupEntities.stream()
268 .filter(fgEntity -> Objects.isNull(fgEntity.getManufacturerReferenceNumber()))
269 .forEach(this::updateManufacturerNumberInFeatureGroup);
270 return featureGroupEntities;
275 public Collection<ErrorCode> validateLicensingData(String vlmId, Version version,
276 String licenseAgreementId,
277 Collection<String> featureGroupIds) {
279 List<ErrorCode> errorMessages = new ArrayList<>();
282 getLicenseAgreement(vlmId, version, licenseAgreementId);
283 } catch (CoreException exception) {
284 errorMessages.add(exception.code());
287 for (String featureGroupId : featureGroupIds) {
289 FeatureGroupEntity featureGroup =
290 getFeatureGroup(new FeatureGroupEntity(vlmId, version, featureGroupId));
291 if (!featureGroup.getReferencingLicenseAgreements().contains(licenseAgreementId)) {
292 errorMessages.add(new VersionableSubEntityNotFoundErrorBuilder(
293 featureGroup.getEntityType(),
295 LicenseAgreementEntity.ENTITY_TYPE,
299 } catch (CoreException exception) {
300 errorMessages.add(exception.code());
304 return errorMessages;
308 public LicenseAgreementEntity getLicenseAgreement(String vlmId, Version version,
309 String licenseAgreementId) {
310 LicenseAgreementEntity input = new LicenseAgreementEntity(vlmId, version, licenseAgreementId);
311 LicenseAgreementEntity retrieved = licenseAgreementDao.get(input);
312 VersioningUtil.validateEntityExistence(retrieved, input, VendorLicenseModelEntity.ENTITY_TYPE);
317 public Collection<LimitEntity> listLimits(String vlmId, Version version, String epLkgId) {
318 return limitDao.list(new LimitEntity(vlmId, version, epLkgId, null));
322 public LimitEntity createLimit(LimitEntity limit) {
323 limitDao.create(limit);
328 public void updateLimit(LimitEntity limit) {
329 limitDao.update(limit);
332 private void updateManufacturerNumberInFeatureGroup(FeatureGroupEntity featureGroupEntity) {
333 if (CollectionUtils.isNotEmpty(featureGroupEntity.getEntitlementPoolIds())) {
334 Object[] entitlementPoolIdsList = featureGroupEntity.getEntitlementPoolIds().toArray();
335 if (entitlementPoolIdsList.length > 0) {
336 String entitlementPoolId = entitlementPoolIdsList[0].toString();
337 EntitlementPoolEntity entitlementPoolEntity =
338 new EntitlementPoolEntity(featureGroupEntity.getVendorLicenseModelId(),
339 featureGroupEntity.getVersion(), entitlementPoolId);
340 entitlementPoolEntity = entitlementPoolDao.get(entitlementPoolEntity);
341 featureGroupEntity.setManufacturerReferenceNumber(
342 entitlementPoolDao.getManufacturerReferenceNumber(entitlementPoolEntity));
343 featureGroupDao.update(featureGroupEntity);
349 public void validate(String vendorLicenseModelId, Version version) {
350 Collection<String> allFeatureGroupEntities = new HashSet<>();
351 Collection<LicenseAgreementEntity> licenseAgreements = licenseAgreementDao
352 .list(new LicenseAgreementEntity(vendorLicenseModelId, version, null));
354 if (CollectionUtils.isNotEmpty(licenseAgreements)) {
355 licenseAgreements.forEach(licenseAgreement -> {
356 if (CollectionUtils.isEmpty(licenseAgreement.getFeatureGroupIds())) {
357 throw new CoreException(
358 new SubmitUncompletedLicenseModelErrorBuilder(
359 SUBMIT_UNCOMPLETED_VLM_MSG_LA_MISSING_FG).build());
361 allFeatureGroupEntities.addAll(licenseAgreement.getFeatureGroupIds());
364 allFeatureGroupEntities.forEach(fg -> {
365 FeatureGroupEntity featureGroupEntity =
366 featureGroupDao.get(new FeatureGroupEntity(vendorLicenseModelId, version, fg));
367 if (CollectionUtils.isEmpty(featureGroupEntity.getEntitlementPoolIds())) {
368 throw new CoreException(
369 new SubmitUncompletedLicenseModelErrorBuilder(
370 SUBMIT_UNCOMPLETED_VLM_MSG_FG_MISSING_EP).build());