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.util.UniqueValueUtil;
25 import org.openecomp.core.utilities.CommonMethods;
26 import org.openecomp.sdc.common.errors.CoreException;
27 import org.openecomp.sdc.common.errors.ErrorCode;
28 import org.openecomp.sdc.vendorlicense.VendorLicenseConstants;
29 import org.openecomp.sdc.vendorlicense.dao.EntitlementPoolDao;
30 import org.openecomp.sdc.vendorlicense.dao.EntitlementPoolDaoFactory;
31 import org.openecomp.sdc.vendorlicense.dao.FeatureGroupDao;
32 import org.openecomp.sdc.vendorlicense.dao.FeatureGroupDaoFactory;
33 import org.openecomp.sdc.vendorlicense.dao.LicenseAgreementDao;
34 import org.openecomp.sdc.vendorlicense.dao.LicenseAgreementDaoFactory;
35 import org.openecomp.sdc.vendorlicense.dao.LicenseKeyGroupDao;
36 import org.openecomp.sdc.vendorlicense.dao.LicenseKeyGroupDaoFactory;
37 import org.openecomp.sdc.vendorlicense.dao.LimitDao;
38 import org.openecomp.sdc.vendorlicense.dao.LimitDaoFactory;
39 import org.openecomp.sdc.vendorlicense.dao.VendorLicenseModelDao;
40 import org.openecomp.sdc.vendorlicense.dao.VendorLicenseModelDaoFactory;
41 import org.openecomp.sdc.vendorlicense.dao.types.EntitlementPoolEntity;
42 import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity;
43 import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupModel;
44 import org.openecomp.sdc.vendorlicense.dao.types.LicenseAgreementEntity;
45 import org.openecomp.sdc.vendorlicense.dao.types.LicenseAgreementModel;
46 import org.openecomp.sdc.vendorlicense.dao.types.LicenseKeyGroupEntity;
47 import org.openecomp.sdc.vendorlicense.dao.types.LimitEntity;
48 import org.openecomp.sdc.vendorlicense.dao.types.VendorLicenseModelEntity;
49 import org.openecomp.sdc.vendorlicense.errors.SubmitUncompletedLicenseModelErrorBuilder;
50 import org.openecomp.sdc.vendorlicense.errors.VendorLicenseModelNotFoundErrorBuilder;
51 import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacade;
52 import org.openecomp.sdc.versioning.VersioningUtil;
53 import org.openecomp.sdc.versioning.dao.types.Version;
54 import org.openecomp.sdc.versioning.errors.VersionableSubEntityNotFoundErrorBuilder;
56 import java.util.ArrayList;
57 import java.util.Collection;
58 import java.util.HashSet;
59 import java.util.List;
60 import java.util.Objects;
62 import static org.openecomp.sdc.vendorlicense.VendorLicenseConstants.VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE;
63 import static org.openecomp.sdc.vendorlicense.errors.UncompletedVendorLicenseModelErrorType.SUBMIT_UNCOMPLETED_VLM_MSG_FG_MISSING_EP;
64 import static org.openecomp.sdc.vendorlicense.errors.UncompletedVendorLicenseModelErrorType.SUBMIT_UNCOMPLETED_VLM_MSG_LA_MISSING_FG;
66 public class VendorLicenseFacadeImpl implements VendorLicenseFacade {
67 private static final VendorLicenseModelDao
68 vendorLicenseModelDao = VendorLicenseModelDaoFactory.getInstance().createInterface();
69 private static final LicenseAgreementDao
70 licenseAgreementDao = LicenseAgreementDaoFactory.getInstance().createInterface();
71 private static final FeatureGroupDao featureGroupDao =
72 FeatureGroupDaoFactory.getInstance().createInterface();
73 private static final EntitlementPoolDao
74 entitlementPoolDao = EntitlementPoolDaoFactory.getInstance().createInterface();
75 private static final LicenseKeyGroupDao
76 licenseKeyGroupDao = LicenseKeyGroupDaoFactory.getInstance().createInterface();
77 private static final LimitDao limitDao = LimitDaoFactory.getInstance().createInterface();
79 * Instantiates a new Vendor license facade.
81 public VendorLicenseFacadeImpl() {
82 vendorLicenseModelDao.registerVersioning(VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE);
83 licenseAgreementDao.registerVersioning(VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE);
84 featureGroupDao.registerVersioning(VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE);
85 entitlementPoolDao.registerVersioning(VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE);
86 licenseKeyGroupDao.registerVersioning(VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE);
87 limitDao.registerVersioning(VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE);
91 public FeatureGroupEntity getFeatureGroup(FeatureGroupEntity featureGroup) {
92 FeatureGroupEntity retrieved = featureGroupDao.get(featureGroup);
94 .validateEntityExistence(retrieved, featureGroup, VendorLicenseModelEntity.ENTITY_TYPE);
95 if (retrieved.getManufacturerReferenceNumber() == null) {
96 updateManufacturerNumberInFeatureGroup(retrieved);
102 public FeatureGroupModel getFeatureGroupModel(FeatureGroupEntity featureGroup) {
103 FeatureGroupEntity retrieved = getFeatureGroup(featureGroup);
105 FeatureGroupModel featureGroupModel = new FeatureGroupModel();
106 featureGroupModel.setFeatureGroup(retrieved);
108 for (String licenseKeyGroupId : retrieved.getLicenseKeyGroupIds()) {
109 featureGroupModel.getLicenseKeyGroups().add(licenseKeyGroupDao.get(
110 new LicenseKeyGroupEntity(retrieved.getVendorLicenseModelId(), retrieved.getVersion(),
111 licenseKeyGroupId)));
113 for (String entitlementPoolId : retrieved.getEntitlementPoolIds()) {
114 featureGroupModel.getEntitlementPools().add(entitlementPoolDao.get(
115 new EntitlementPoolEntity(retrieved.getVendorLicenseModelId(), retrieved.getVersion(),
116 entitlementPoolId)));
119 return featureGroupModel;
123 public LicenseAgreementModel getLicenseAgreementModel(String vlmId, Version version,
124 String licenseAgreementId) {
125 LicenseAgreementEntity retrieved =
126 getLicenseAgreement(vlmId, version, licenseAgreementId);
128 LicenseAgreementModel licenseAgreementModel = new LicenseAgreementModel();
129 licenseAgreementModel.setLicenseAgreement(retrieved);
131 for (String featureGroupId : retrieved.getFeatureGroupIds()) {
132 licenseAgreementModel.getFeatureGroups().add(featureGroupDao
133 .get(new FeatureGroupEntity(vlmId, retrieved.getVersion(), featureGroupId)));
136 return licenseAgreementModel;
140 public EntitlementPoolEntity createEntitlementPool(EntitlementPoolEntity entitlementPool) {
141 entitlementPool.setVersionUuId(CommonMethods.nextUuId());
142 UniqueValueUtil.createUniqueValue(VendorLicenseConstants.UniqueValues.ENTITLEMENT_POOL_NAME,
143 entitlementPool.getVendorLicenseModelId(), entitlementPool.getVersion().getId(),
144 entitlementPool.getName());
145 entitlementPoolDao.create(entitlementPool);
146 return entitlementPool;
150 public void updateEntitlementPool(EntitlementPoolEntity entitlementPool) {
151 EntitlementPoolEntity retrieved = entitlementPoolDao.get(entitlementPool);
153 .validateEntityExistence(retrieved, entitlementPool, VendorLicenseModelEntity.ENTITY_TYPE);
154 if (retrieved.equals(entitlementPool)) {
157 UniqueValueUtil.updateUniqueValue(VendorLicenseConstants.UniqueValues.ENTITLEMENT_POOL_NAME,
158 retrieved.getName(), entitlementPool.getName(), entitlementPool.getVendorLicenseModelId(),
159 entitlementPool.getVersion().getId());
160 entitlementPool.setVersionUuId(CommonMethods.nextUuId());
161 entitlementPoolDao.update(entitlementPool);
165 public Collection<LicenseKeyGroupEntity> listLicenseKeyGroups(String vlmId, Version version) {
166 return licenseKeyGroupDao.list(new LicenseKeyGroupEntity(vlmId, version, null));
170 public Collection<EntitlementPoolEntity> listEntitlementPools(String vlmId, Version version) {
171 return entitlementPoolDao.list(new EntitlementPoolEntity(vlmId, version, null));
175 public void updateLicenseKeyGroup(LicenseKeyGroupEntity licenseKeyGroup) {
176 LicenseKeyGroupEntity retrieved = licenseKeyGroupDao.get(licenseKeyGroup);
177 if (retrieved.equals(licenseKeyGroup)) {
180 licenseKeyGroup.setVersionUuId((CommonMethods.nextUuId()));
182 .validateEntityExistence(retrieved, licenseKeyGroup, VendorLicenseModelEntity.ENTITY_TYPE);
183 UniqueValueUtil.updateUniqueValue(VendorLicenseConstants.UniqueValues.LICENSE_KEY_GROUP_NAME,
184 retrieved.getName(), licenseKeyGroup.getName(), licenseKeyGroup.getVendorLicenseModelId(),
185 licenseKeyGroup.getVersion().getId());
186 licenseKeyGroupDao.update(licenseKeyGroup);
190 public LicenseKeyGroupEntity createLicenseKeyGroup(LicenseKeyGroupEntity licenseKeyGroup) {
191 licenseKeyGroup.setVersionUuId(CommonMethods.nextUuId());
192 UniqueValueUtil.createUniqueValue(VendorLicenseConstants.UniqueValues.LICENSE_KEY_GROUP_NAME,
193 licenseKeyGroup.getVendorLicenseModelId(), licenseKeyGroup.getVersion().getId(),
194 licenseKeyGroup.getName());
195 licenseKeyGroupDao.create(licenseKeyGroup);
196 return licenseKeyGroup;
200 public VendorLicenseModelEntity getVendorLicenseModel(String vlmId, Version version) {
201 VendorLicenseModelEntity vendorLicenseModel =
202 vendorLicenseModelDao.get(new VendorLicenseModelEntity(vlmId, version));
203 if (vendorLicenseModel == null) {
204 throw new CoreException(new VendorLicenseModelNotFoundErrorBuilder(vlmId).build());
206 return vendorLicenseModel;
210 public LicenseAgreementEntity createLicenseAgreement(LicenseAgreementEntity licenseAgreement) {
211 VersioningUtil.validateEntitiesExistence(licenseAgreement.getFeatureGroupIds(),
212 new FeatureGroupEntity(licenseAgreement.getVendorLicenseModelId(),
213 licenseAgreement.getVersion(),
215 featureGroupDao, VendorLicenseModelEntity.ENTITY_TYPE);
217 UniqueValueUtil.validateUniqueValue(VendorLicenseConstants.UniqueValues.LICENSE_AGREEMENT_NAME,
218 licenseAgreement.getVendorLicenseModelId(), licenseAgreement.getVersion().getId(),
219 licenseAgreement.getName());
221 licenseAgreementDao.create(licenseAgreement);
222 UniqueValueUtil.createUniqueValue(VendorLicenseConstants.UniqueValues.LICENSE_AGREEMENT_NAME,
223 licenseAgreement.getVendorLicenseModelId(), licenseAgreement.getVersion().getId(),
224 licenseAgreement.getName());
225 if (licenseAgreement.getFeatureGroupIds() != null) {
226 for (String addedFgId : licenseAgreement.getFeatureGroupIds()) {
227 featureGroupDao.addReferencingLicenseAgreement(
228 new FeatureGroupEntity(licenseAgreement.getVendorLicenseModelId(),
229 licenseAgreement.getVersion(),
230 addedFgId), licenseAgreement.getId());
233 return licenseAgreement;
237 public FeatureGroupEntity createFeatureGroup(FeatureGroupEntity featureGroup) {
238 VersioningUtil.validateEntitiesExistence(featureGroup.getLicenseKeyGroupIds(),
239 new LicenseKeyGroupEntity(featureGroup.getVendorLicenseModelId(), featureGroup.getVersion(),
241 licenseKeyGroupDao, VendorLicenseModelEntity.ENTITY_TYPE);
242 VersioningUtil.validateEntitiesExistence(featureGroup.getEntitlementPoolIds(),
243 new EntitlementPoolEntity(featureGroup.getVendorLicenseModelId(), featureGroup.getVersion(),
245 entitlementPoolDao, VendorLicenseModelEntity.ENTITY_TYPE);
246 UniqueValueUtil.validateUniqueValue(VendorLicenseConstants.UniqueValues.FEATURE_GROUP_NAME,
247 featureGroup.getVendorLicenseModelId(), featureGroup.getVersion().getId(),
248 featureGroup.getName());
250 featureGroupDao.create(featureGroup);
251 UniqueValueUtil.createUniqueValue(VendorLicenseConstants.UniqueValues.FEATURE_GROUP_NAME,
252 featureGroup.getVendorLicenseModelId(), featureGroup.getVersion().getId(),
253 featureGroup.getName());
255 if (featureGroup.getLicenseKeyGroupIds() != null) {
256 for (String addedLkgId : featureGroup.getLicenseKeyGroupIds()) {
257 licenseKeyGroupDao.addReferencingFeatureGroup(
258 new LicenseKeyGroupEntity(featureGroup.getVendorLicenseModelId(),
259 featureGroup.getVersion(), addedLkgId),
260 featureGroup.getId());
264 if (featureGroup.getEntitlementPoolIds() != null) {
265 for (String addedEpId : featureGroup.getEntitlementPoolIds()) {
266 entitlementPoolDao.addReferencingFeatureGroup(
267 new EntitlementPoolEntity(featureGroup.getVendorLicenseModelId(),
268 featureGroup.getVersion(), addedEpId), featureGroup.getId());
275 public Collection<FeatureGroupEntity> listFeatureGroups(String vlmId, Version version) {
276 Collection<FeatureGroupEntity> featureGroupEntities =
277 featureGroupDao.list(new FeatureGroupEntity(vlmId, version, null));
278 featureGroupEntities.stream()
279 .filter(fgEntity -> Objects.isNull(fgEntity.getManufacturerReferenceNumber()))
280 .forEach(this::updateManufacturerNumberInFeatureGroup);
281 return featureGroupEntities;
286 public Collection<ErrorCode> validateLicensingData(String vlmId, Version version,
287 String licenseAgreementId,
288 Collection<String> featureGroupIds) {
290 List<ErrorCode> errorMessages = new ArrayList<>();
293 getLicenseAgreement(vlmId, version, licenseAgreementId);
294 } catch (CoreException exception) {
295 errorMessages.add(exception.code());
298 for (String featureGroupId : featureGroupIds) {
300 FeatureGroupEntity featureGroup =
301 getFeatureGroup(new FeatureGroupEntity(vlmId, version, featureGroupId));
302 if (!featureGroup.getReferencingLicenseAgreements().contains(licenseAgreementId)) {
303 errorMessages.add(new VersionableSubEntityNotFoundErrorBuilder(
304 featureGroup.getEntityType(),
306 LicenseAgreementEntity.ENTITY_TYPE,
310 } catch (CoreException exception) {
311 errorMessages.add(exception.code());
315 return errorMessages;
319 public LicenseAgreementEntity getLicenseAgreement(String vlmId, Version version,
320 String licenseAgreementId) {
321 LicenseAgreementEntity input = new LicenseAgreementEntity(vlmId, version, licenseAgreementId);
322 LicenseAgreementEntity retrieved = licenseAgreementDao.get(input);
323 VersioningUtil.validateEntityExistence(retrieved, input, VendorLicenseModelEntity.ENTITY_TYPE);
328 public Collection<LimitEntity> listLimits(String vlmId, Version version, String epLkgId) {
329 return limitDao.list(new LimitEntity(vlmId, version, epLkgId, null));
333 public LimitEntity createLimit(LimitEntity limit) {
334 limitDao.create(limit);
339 public void updateLimit(LimitEntity limit) {
340 limitDao.update(limit);
343 private void updateManufacturerNumberInFeatureGroup(FeatureGroupEntity featureGroupEntity) {
344 if (CollectionUtils.isNotEmpty(featureGroupEntity.getEntitlementPoolIds())) {
345 Object[] entitlementPoolIdsList = featureGroupEntity.getEntitlementPoolIds().toArray();
346 if (entitlementPoolIdsList.length > 0) {
347 String entitlementPoolId = entitlementPoolIdsList[0].toString();
348 EntitlementPoolEntity entitlementPoolEntity =
349 new EntitlementPoolEntity(featureGroupEntity.getVendorLicenseModelId(),
350 featureGroupEntity.getVersion(), entitlementPoolId);
351 entitlementPoolEntity = entitlementPoolDao.get(entitlementPoolEntity);
352 featureGroupEntity.setManufacturerReferenceNumber(
353 entitlementPoolDao.getManufacturerReferenceNumber(entitlementPoolEntity));
354 featureGroupDao.update(featureGroupEntity);
360 public void validate(String vendorLicenseModelId, Version version) {
361 Collection<String> allFeatureGroupEntities = new HashSet<>();
362 Collection<LicenseAgreementEntity> licenseAgreements = licenseAgreementDao
363 .list(new LicenseAgreementEntity(vendorLicenseModelId, version, null));
365 if (CollectionUtils.isNotEmpty(licenseAgreements)) {
366 licenseAgreements.forEach(licenseAgreement -> {
367 if (CollectionUtils.isEmpty(licenseAgreement.getFeatureGroupIds())) {
368 throw new CoreException(
369 new SubmitUncompletedLicenseModelErrorBuilder(
370 SUBMIT_UNCOMPLETED_VLM_MSG_LA_MISSING_FG).build());
372 allFeatureGroupEntities.addAll(licenseAgreement.getFeatureGroupIds());
375 allFeatureGroupEntities.forEach(fg -> {
376 FeatureGroupEntity featureGroupEntity =
377 featureGroupDao.get(new FeatureGroupEntity(vendorLicenseModelId, version, fg));
378 if (CollectionUtils.isEmpty(featureGroupEntity.getEntitlementPoolIds())) {
379 throw new CoreException(
380 new SubmitUncompletedLicenseModelErrorBuilder(
381 SUBMIT_UNCOMPLETED_VLM_MSG_FG_MISSING_EP).build());