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.vendorlicense.VendorLicenseConstants;
30 import org.openecomp.sdc.vendorlicense.dao.EntitlementPoolDao;
31 import org.openecomp.sdc.vendorlicense.dao.EntitlementPoolDaoFactory;
32 import org.openecomp.sdc.vendorlicense.dao.FeatureGroupDao;
33 import org.openecomp.sdc.vendorlicense.dao.FeatureGroupDaoFactory;
34 import org.openecomp.sdc.vendorlicense.dao.LicenseAgreementDao;
35 import org.openecomp.sdc.vendorlicense.dao.LicenseAgreementDaoFactory;
36 import org.openecomp.sdc.vendorlicense.dao.LicenseKeyGroupDao;
37 import org.openecomp.sdc.vendorlicense.dao.LicenseKeyGroupDaoFactory;
38 import org.openecomp.sdc.vendorlicense.dao.LimitDao;
39 import org.openecomp.sdc.vendorlicense.dao.LimitDaoFactory;
40 import org.openecomp.sdc.vendorlicense.dao.VendorLicenseModelDao;
41 import org.openecomp.sdc.vendorlicense.dao.VendorLicenseModelDaoFactory;
42 import org.openecomp.sdc.vendorlicense.dao.types.EntitlementPoolEntity;
43 import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity;
44 import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupModel;
45 import org.openecomp.sdc.vendorlicense.dao.types.LicenseAgreementEntity;
46 import org.openecomp.sdc.vendorlicense.dao.types.LicenseAgreementModel;
47 import org.openecomp.sdc.vendorlicense.dao.types.LicenseKeyGroupEntity;
48 import org.openecomp.sdc.vendorlicense.dao.types.LimitEntity;
49 import org.openecomp.sdc.vendorlicense.dao.types.VendorLicenseModelEntity;
50 import org.openecomp.sdc.vendorlicense.errors.SubmitUncompletedLicenseModelErrorBuilder;
51 import org.openecomp.sdc.vendorlicense.errors.VendorLicenseModelNotFoundErrorBuilder;
52 import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacade;
53 import org.openecomp.sdc.versioning.VersioningUtil;
54 import org.openecomp.sdc.versioning.dao.types.Version;
55 import org.openecomp.sdc.versioning.errors.VersionableSubEntityNotFoundErrorBuilder;
57 import java.util.ArrayList;
58 import java.util.Collection;
59 import java.util.HashSet;
60 import java.util.List;
61 import java.util.Objects;
63 import static org.openecomp.sdc.vendorlicense.VendorLicenseConstants.VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE;
64 import static org.openecomp.sdc.vendorlicense.errors.UncompletedVendorLicenseModelErrorType.SUBMIT_UNCOMPLETED_VLM_MSG_FG_MISSING_EP;
65 import static org.openecomp.sdc.vendorlicense.errors.UncompletedVendorLicenseModelErrorType.SUBMIT_UNCOMPLETED_VLM_MSG_LA_MISSING_FG;
67 public class VendorLicenseFacadeImpl implements VendorLicenseFacade {
68 private static final VendorLicenseModelDao
69 vendorLicenseModelDao = VendorLicenseModelDaoFactory.getInstance().createInterface();
70 private static final LicenseAgreementDao
71 licenseAgreementDao = LicenseAgreementDaoFactory.getInstance().createInterface();
72 private static final FeatureGroupDao featureGroupDao =
73 FeatureGroupDaoFactory.getInstance().createInterface();
74 private static final EntitlementPoolDao
75 entitlementPoolDao = EntitlementPoolDaoFactory.getInstance().createInterface();
76 private static final LicenseKeyGroupDao
77 licenseKeyGroupDao = LicenseKeyGroupDaoFactory.getInstance().createInterface();
78 private static final LimitDao limitDao = LimitDaoFactory.getInstance().createInterface();
79 private static final UniqueValueUtil uniqueValueUtil = new UniqueValueUtil
80 (UniqueValueDaoFactory.getInstance().createInterface());
82 * Instantiates a new Vendor license facade.
84 public VendorLicenseFacadeImpl() {
85 vendorLicenseModelDao.registerVersioning(VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE);
86 licenseAgreementDao.registerVersioning(VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE);
87 featureGroupDao.registerVersioning(VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE);
88 entitlementPoolDao.registerVersioning(VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE);
89 licenseKeyGroupDao.registerVersioning(VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE);
90 limitDao.registerVersioning(VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE);
94 public FeatureGroupEntity getFeatureGroup(FeatureGroupEntity featureGroup) {
95 FeatureGroupEntity retrieved = featureGroupDao.get(featureGroup);
97 .validateEntityExistence(retrieved, featureGroup, VendorLicenseModelEntity.ENTITY_TYPE);
98 if (retrieved.getManufacturerReferenceNumber() == null) {
99 updateManufacturerNumberInFeatureGroup(retrieved);
105 public FeatureGroupModel getFeatureGroupModel(FeatureGroupEntity featureGroup) {
106 FeatureGroupEntity retrieved = getFeatureGroup(featureGroup);
108 FeatureGroupModel featureGroupModel = new FeatureGroupModel();
109 featureGroupModel.setFeatureGroup(retrieved);
111 for (String licenseKeyGroupId : retrieved.getLicenseKeyGroupIds()) {
112 featureGroupModel.getLicenseKeyGroups().add(licenseKeyGroupDao.get(
113 new LicenseKeyGroupEntity(retrieved.getVendorLicenseModelId(), retrieved.getVersion(),
114 licenseKeyGroupId)));
116 for (String entitlementPoolId : retrieved.getEntitlementPoolIds()) {
117 featureGroupModel.getEntitlementPools().add(entitlementPoolDao.get(
118 new EntitlementPoolEntity(retrieved.getVendorLicenseModelId(), retrieved.getVersion(),
119 entitlementPoolId)));
122 return featureGroupModel;
126 public LicenseAgreementModel getLicenseAgreementModel(String vlmId, Version version,
127 String licenseAgreementId) {
128 LicenseAgreementEntity retrieved =
129 getLicenseAgreement(vlmId, version, licenseAgreementId);
131 LicenseAgreementModel licenseAgreementModel = new LicenseAgreementModel();
132 licenseAgreementModel.setLicenseAgreement(retrieved);
134 for (String featureGroupId : retrieved.getFeatureGroupIds()) {
135 licenseAgreementModel.getFeatureGroups().add(featureGroupDao
136 .get(new FeatureGroupEntity(vlmId, retrieved.getVersion(), featureGroupId)));
139 return licenseAgreementModel;
143 public EntitlementPoolEntity createEntitlementPool(EntitlementPoolEntity entitlementPool) {
144 entitlementPool.setVersionUuId(CommonMethods.nextUuId());
145 uniqueValueUtil.createUniqueValue(VendorLicenseConstants.UniqueValues.ENTITLEMENT_POOL_NAME,
146 entitlementPool.getVendorLicenseModelId(), entitlementPool.getVersion().getId(),
147 entitlementPool.getName());
148 entitlementPoolDao.create(entitlementPool);
149 return entitlementPool;
153 public void updateEntitlementPool(EntitlementPoolEntity entitlementPool) {
154 EntitlementPoolEntity retrieved = entitlementPoolDao.get(entitlementPool);
156 .validateEntityExistence(retrieved, entitlementPool, VendorLicenseModelEntity.ENTITY_TYPE);
157 if (retrieved.equals(entitlementPool)) {
160 uniqueValueUtil.updateUniqueValue(VendorLicenseConstants.UniqueValues.ENTITLEMENT_POOL_NAME,
161 retrieved.getName(), entitlementPool.getName(), entitlementPool.getVendorLicenseModelId(),
162 entitlementPool.getVersion().getId());
163 entitlementPool.setVersionUuId(CommonMethods.nextUuId());
164 entitlementPoolDao.update(entitlementPool);
168 public Collection<LicenseKeyGroupEntity> listLicenseKeyGroups(String vlmId, Version version) {
169 return licenseKeyGroupDao.list(new LicenseKeyGroupEntity(vlmId, version, null));
173 public Collection<EntitlementPoolEntity> listEntitlementPools(String vlmId, Version version) {
174 return entitlementPoolDao.list(new EntitlementPoolEntity(vlmId, version, null));
178 public void updateLicenseKeyGroup(LicenseKeyGroupEntity licenseKeyGroup) {
179 LicenseKeyGroupEntity retrieved = licenseKeyGroupDao.get(licenseKeyGroup);
180 if (retrieved.equals(licenseKeyGroup)) {
183 licenseKeyGroup.setVersionUuId((CommonMethods.nextUuId()));
185 .validateEntityExistence(retrieved, licenseKeyGroup, VendorLicenseModelEntity.ENTITY_TYPE);
186 uniqueValueUtil.updateUniqueValue(VendorLicenseConstants.UniqueValues.LICENSE_KEY_GROUP_NAME,
187 retrieved.getName(), licenseKeyGroup.getName(), licenseKeyGroup.getVendorLicenseModelId(),
188 licenseKeyGroup.getVersion().getId());
189 licenseKeyGroupDao.update(licenseKeyGroup);
193 public LicenseKeyGroupEntity createLicenseKeyGroup(LicenseKeyGroupEntity licenseKeyGroup) {
194 licenseKeyGroup.setVersionUuId(CommonMethods.nextUuId());
195 uniqueValueUtil.createUniqueValue(VendorLicenseConstants.UniqueValues.LICENSE_KEY_GROUP_NAME,
196 licenseKeyGroup.getVendorLicenseModelId(), licenseKeyGroup.getVersion().getId(),
197 licenseKeyGroup.getName());
198 licenseKeyGroupDao.create(licenseKeyGroup);
199 return licenseKeyGroup;
203 public VendorLicenseModelEntity getVendorLicenseModel(String vlmId, Version version) {
204 VendorLicenseModelEntity vendorLicenseModel =
205 vendorLicenseModelDao.get(new VendorLicenseModelEntity(vlmId, version));
206 if (vendorLicenseModel == null) {
207 throw new CoreException(new VendorLicenseModelNotFoundErrorBuilder(vlmId).build());
209 return vendorLicenseModel;
213 public LicenseAgreementEntity createLicenseAgreement(LicenseAgreementEntity licenseAgreement) {
214 VersioningUtil.validateEntitiesExistence(licenseAgreement.getFeatureGroupIds(),
215 new FeatureGroupEntity(licenseAgreement.getVendorLicenseModelId(),
216 licenseAgreement.getVersion(),
218 featureGroupDao, VendorLicenseModelEntity.ENTITY_TYPE);
220 uniqueValueUtil.validateUniqueValue(VendorLicenseConstants.UniqueValues.LICENSE_AGREEMENT_NAME,
221 licenseAgreement.getVendorLicenseModelId(), licenseAgreement.getVersion().getId(),
222 licenseAgreement.getName());
224 licenseAgreementDao.create(licenseAgreement);
225 uniqueValueUtil.createUniqueValue(VendorLicenseConstants.UniqueValues.LICENSE_AGREEMENT_NAME,
226 licenseAgreement.getVendorLicenseModelId(), licenseAgreement.getVersion().getId(),
227 licenseAgreement.getName());
228 if (licenseAgreement.getFeatureGroupIds() != null) {
229 for (String addedFgId : licenseAgreement.getFeatureGroupIds()) {
230 featureGroupDao.addReferencingLicenseAgreement(
231 new FeatureGroupEntity(licenseAgreement.getVendorLicenseModelId(),
232 licenseAgreement.getVersion(),
233 addedFgId), licenseAgreement.getId());
236 return licenseAgreement;
240 public FeatureGroupEntity createFeatureGroup(FeatureGroupEntity featureGroup) {
241 VersioningUtil.validateEntitiesExistence(featureGroup.getLicenseKeyGroupIds(),
242 new LicenseKeyGroupEntity(featureGroup.getVendorLicenseModelId(), featureGroup.getVersion(),
244 licenseKeyGroupDao, VendorLicenseModelEntity.ENTITY_TYPE);
245 VersioningUtil.validateEntitiesExistence(featureGroup.getEntitlementPoolIds(),
246 new EntitlementPoolEntity(featureGroup.getVendorLicenseModelId(), featureGroup.getVersion(),
248 entitlementPoolDao, VendorLicenseModelEntity.ENTITY_TYPE);
249 uniqueValueUtil.validateUniqueValue(VendorLicenseConstants.UniqueValues.FEATURE_GROUP_NAME,
250 featureGroup.getVendorLicenseModelId(), featureGroup.getVersion().getId(),
251 featureGroup.getName());
253 featureGroupDao.create(featureGroup);
254 uniqueValueUtil.createUniqueValue(VendorLicenseConstants.UniqueValues.FEATURE_GROUP_NAME,
255 featureGroup.getVendorLicenseModelId(), featureGroup.getVersion().getId(),
256 featureGroup.getName());
258 if (featureGroup.getLicenseKeyGroupIds() != null) {
259 for (String addedLkgId : featureGroup.getLicenseKeyGroupIds()) {
260 licenseKeyGroupDao.addReferencingFeatureGroup(
261 new LicenseKeyGroupEntity(featureGroup.getVendorLicenseModelId(),
262 featureGroup.getVersion(), addedLkgId),
263 featureGroup.getId());
267 if (featureGroup.getEntitlementPoolIds() != null) {
268 for (String addedEpId : featureGroup.getEntitlementPoolIds()) {
269 entitlementPoolDao.addReferencingFeatureGroup(
270 new EntitlementPoolEntity(featureGroup.getVendorLicenseModelId(),
271 featureGroup.getVersion(), addedEpId), featureGroup.getId());
278 public Collection<FeatureGroupEntity> listFeatureGroups(String vlmId, Version version) {
279 Collection<FeatureGroupEntity> featureGroupEntities =
280 featureGroupDao.list(new FeatureGroupEntity(vlmId, version, null));
281 featureGroupEntities.stream()
282 .filter(fgEntity -> Objects.isNull(fgEntity.getManufacturerReferenceNumber()))
283 .forEach(this::updateManufacturerNumberInFeatureGroup);
284 return featureGroupEntities;
289 public Collection<ErrorCode> validateLicensingData(String vlmId, Version version,
290 String licenseAgreementId,
291 Collection<String> featureGroupIds) {
293 List<ErrorCode> errorMessages = new ArrayList<>();
296 getLicenseAgreement(vlmId, version, licenseAgreementId);
297 } catch (CoreException exception) {
298 errorMessages.add(exception.code());
301 for (String featureGroupId : featureGroupIds) {
303 FeatureGroupEntity featureGroup =
304 getFeatureGroup(new FeatureGroupEntity(vlmId, version, featureGroupId));
305 if (!featureGroup.getReferencingLicenseAgreements().contains(licenseAgreementId)) {
306 errorMessages.add(new VersionableSubEntityNotFoundErrorBuilder(
307 featureGroup.getEntityType(),
309 LicenseAgreementEntity.ENTITY_TYPE,
313 } catch (CoreException exception) {
314 errorMessages.add(exception.code());
318 return errorMessages;
322 public LicenseAgreementEntity getLicenseAgreement(String vlmId, Version version,
323 String licenseAgreementId) {
324 LicenseAgreementEntity input = new LicenseAgreementEntity(vlmId, version, licenseAgreementId);
325 LicenseAgreementEntity retrieved = licenseAgreementDao.get(input);
326 VersioningUtil.validateEntityExistence(retrieved, input, VendorLicenseModelEntity.ENTITY_TYPE);
331 public Collection<LimitEntity> listLimits(String vlmId, Version version, String epLkgId) {
332 return limitDao.list(new LimitEntity(vlmId, version, epLkgId, null));
336 public LimitEntity createLimit(LimitEntity limit) {
337 limitDao.create(limit);
342 public void updateLimit(LimitEntity limit) {
343 limitDao.update(limit);
346 private void updateManufacturerNumberInFeatureGroup(FeatureGroupEntity featureGroupEntity) {
347 if (CollectionUtils.isNotEmpty(featureGroupEntity.getEntitlementPoolIds())) {
348 Object[] entitlementPoolIdsList = featureGroupEntity.getEntitlementPoolIds().toArray();
349 if (entitlementPoolIdsList.length > 0) {
350 String entitlementPoolId = entitlementPoolIdsList[0].toString();
351 EntitlementPoolEntity entitlementPoolEntity =
352 new EntitlementPoolEntity(featureGroupEntity.getVendorLicenseModelId(),
353 featureGroupEntity.getVersion(), entitlementPoolId);
354 entitlementPoolEntity = entitlementPoolDao.get(entitlementPoolEntity);
355 featureGroupEntity.setManufacturerReferenceNumber(
356 entitlementPoolDao.getManufacturerReferenceNumber(entitlementPoolEntity));
357 featureGroupDao.update(featureGroupEntity);
363 public void validate(String vendorLicenseModelId, Version version) {
364 Collection<String> allFeatureGroupEntities = new HashSet<>();
365 Collection<LicenseAgreementEntity> licenseAgreements = licenseAgreementDao
366 .list(new LicenseAgreementEntity(vendorLicenseModelId, version, null));
368 if (CollectionUtils.isNotEmpty(licenseAgreements)) {
369 licenseAgreements.forEach(licenseAgreement -> {
370 if (CollectionUtils.isEmpty(licenseAgreement.getFeatureGroupIds())) {
371 throw new CoreException(
372 new SubmitUncompletedLicenseModelErrorBuilder(
373 SUBMIT_UNCOMPLETED_VLM_MSG_LA_MISSING_FG).build());
375 allFeatureGroupEntities.addAll(licenseAgreement.getFeatureGroupIds());
378 allFeatureGroupEntities.forEach(fg -> {
379 FeatureGroupEntity featureGroupEntity =
380 featureGroupDao.get(new FeatureGroupEntity(vendorLicenseModelId, version, fg));
381 if (CollectionUtils.isEmpty(featureGroupEntity.getEntitlementPoolIds())) {
382 throw new CoreException(
383 new SubmitUncompletedLicenseModelErrorBuilder(
384 SUBMIT_UNCOMPLETED_VLM_MSG_FG_MISSING_EP).build());