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.*;
31 import org.openecomp.sdc.vendorlicense.dao.types.*;
32 import org.openecomp.sdc.vendorlicense.errors.SubmitUncompletedLicenseModelErrorBuilder;
33 import org.openecomp.sdc.vendorlicense.errors.VendorLicenseModelNotFoundErrorBuilder;
34 import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacade;
35 import org.openecomp.sdc.versioning.VersioningUtil;
36 import org.openecomp.sdc.versioning.dao.types.Version;
37 import org.openecomp.sdc.versioning.errors.VersionableSubEntityNotFoundErrorBuilder;
39 import java.util.ArrayList;
40 import java.util.Collection;
41 import java.util.HashSet;
42 import java.util.List;
45 import static org.openecomp.sdc.vendorlicense.VendorLicenseConstants.VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE;
46 import static org.openecomp.sdc.vendorlicense.errors.UncompletedVendorLicenseModelErrorType.SUBMIT_UNCOMPLETED_VLM_MSG_FG_MISSING_EP;
47 import static org.openecomp.sdc.vendorlicense.errors.UncompletedVendorLicenseModelErrorType.SUBMIT_UNCOMPLETED_VLM_MSG_LA_MISSING_FG;
49 public class VendorLicenseFacadeImpl implements VendorLicenseFacade {
50 private static final VendorLicenseModelDao
51 vendorLicenseModelDao = VendorLicenseModelDaoFactory.getInstance().createInterface();
52 private static final LicenseAgreementDao
53 licenseAgreementDao = LicenseAgreementDaoFactory.getInstance().createInterface();
54 private static final FeatureGroupDao featureGroupDao =
55 FeatureGroupDaoFactory.getInstance().createInterface();
56 private static final EntitlementPoolDao
57 entitlementPoolDao = EntitlementPoolDaoFactory.getInstance().createInterface();
58 private static final LicenseKeyGroupDao
59 licenseKeyGroupDao = LicenseKeyGroupDaoFactory.getInstance().createInterface();
60 private static final LimitDao limitDao = LimitDaoFactory.getInstance().createInterface();
61 private static final UniqueValueUtil uniqueValueUtil = new UniqueValueUtil
62 (UniqueValueDaoFactory.getInstance().createInterface());
64 * Instantiates a new Vendor license facade.
66 public VendorLicenseFacadeImpl() {
67 vendorLicenseModelDao.registerVersioning(VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE);
68 licenseAgreementDao.registerVersioning(VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE);
69 featureGroupDao.registerVersioning(VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE);
70 entitlementPoolDao.registerVersioning(VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE);
71 licenseKeyGroupDao.registerVersioning(VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE);
72 limitDao.registerVersioning(VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE);
76 public FeatureGroupEntity getFeatureGroup(FeatureGroupEntity featureGroup) {
77 FeatureGroupEntity retrieved = featureGroupDao.get(featureGroup);
79 .validateEntityExistence(retrieved, featureGroup, VendorLicenseModelEntity.ENTITY_TYPE);
85 public FeatureGroupModel getFeatureGroupModel(FeatureGroupEntity featureGroup) {
86 FeatureGroupEntity retrieved = getFeatureGroup(featureGroup);
88 FeatureGroupModel featureGroupModel = new FeatureGroupModel();
89 featureGroupModel.setFeatureGroup(retrieved);
91 for (String licenseKeyGroupId : retrieved.getLicenseKeyGroupIds()) {
92 featureGroupModel.getLicenseKeyGroups().add(licenseKeyGroupDao.get(
93 new LicenseKeyGroupEntity(retrieved.getVendorLicenseModelId(), retrieved.getVersion(),
96 for (String entitlementPoolId : retrieved.getEntitlementPoolIds()) {
97 featureGroupModel.getEntitlementPools().add(entitlementPoolDao.get(
98 new EntitlementPoolEntity(retrieved.getVendorLicenseModelId(), retrieved.getVersion(),
102 return featureGroupModel;
106 public LicenseAgreementModel getLicenseAgreementModel(String vlmId, Version version,
107 String licenseAgreementId) {
108 LicenseAgreementEntity retrieved =
109 getLicenseAgreement(vlmId, version, licenseAgreementId);
111 LicenseAgreementModel licenseAgreementModel = new LicenseAgreementModel();
112 licenseAgreementModel.setLicenseAgreement(retrieved);
114 for (String featureGroupId : retrieved.getFeatureGroupIds()) {
115 licenseAgreementModel.getFeatureGroups().add(featureGroupDao
116 .get(new FeatureGroupEntity(vlmId, retrieved.getVersion(), featureGroupId)));
119 return licenseAgreementModel;
123 public EntitlementPoolEntity createEntitlementPool(EntitlementPoolEntity entitlementPool) {
124 entitlementPool.setVersionUuId(CommonMethods.nextUuId());
125 uniqueValueUtil.createUniqueValue(VendorLicenseConstants.UniqueValues.ENTITLEMENT_POOL_NAME,
126 entitlementPool.getVendorLicenseModelId(), entitlementPool.getVersion().getId(),
127 entitlementPool.getName());
128 entitlementPoolDao.create(entitlementPool);
129 return entitlementPool;
133 public void updateEntitlementPool(EntitlementPoolEntity entitlementPool) {
134 EntitlementPoolEntity retrieved = entitlementPoolDao.get(entitlementPool);
136 .validateEntityExistence(retrieved, entitlementPool, VendorLicenseModelEntity.ENTITY_TYPE);
137 if (retrieved.equals(entitlementPool)) {
140 uniqueValueUtil.updateUniqueValue(VendorLicenseConstants.UniqueValues.ENTITLEMENT_POOL_NAME,
141 retrieved.getName(), entitlementPool.getName(), entitlementPool.getVendorLicenseModelId(),
142 entitlementPool.getVersion().getId());
143 entitlementPool.setVersionUuId(CommonMethods.nextUuId());
144 entitlementPoolDao.update(entitlementPool);
148 public Collection<LicenseKeyGroupEntity> listLicenseKeyGroups(String vlmId, Version version) {
149 return licenseKeyGroupDao.list(new LicenseKeyGroupEntity(vlmId, version, null));
153 public Collection<EntitlementPoolEntity> listEntitlementPools(String vlmId, Version version) {
154 return entitlementPoolDao.list(new EntitlementPoolEntity(vlmId, version, null));
158 public void updateLicenseKeyGroup(LicenseKeyGroupEntity licenseKeyGroup) {
159 LicenseKeyGroupEntity retrieved = licenseKeyGroupDao.get(licenseKeyGroup);
160 if (retrieved.equals(licenseKeyGroup)) {
163 licenseKeyGroup.setVersionUuId((CommonMethods.nextUuId()));
165 .validateEntityExistence(retrieved, licenseKeyGroup, VendorLicenseModelEntity.ENTITY_TYPE);
166 uniqueValueUtil.updateUniqueValue(VendorLicenseConstants.UniqueValues.LICENSE_KEY_GROUP_NAME,
167 retrieved.getName(), licenseKeyGroup.getName(), licenseKeyGroup.getVendorLicenseModelId(),
168 licenseKeyGroup.getVersion().getId());
169 licenseKeyGroupDao.update(licenseKeyGroup);
173 public LicenseKeyGroupEntity createLicenseKeyGroup(LicenseKeyGroupEntity licenseKeyGroup) {
174 licenseKeyGroup.setVersionUuId(CommonMethods.nextUuId());
175 uniqueValueUtil.createUniqueValue(VendorLicenseConstants.UniqueValues.LICENSE_KEY_GROUP_NAME,
176 licenseKeyGroup.getVendorLicenseModelId(), licenseKeyGroup.getVersion().getId(),
177 licenseKeyGroup.getName());
178 licenseKeyGroupDao.create(licenseKeyGroup);
179 return licenseKeyGroup;
183 public VendorLicenseModelEntity getVendorLicenseModel(String vlmId, Version version) {
184 VendorLicenseModelEntity vendorLicenseModel =
185 vendorLicenseModelDao.get(new VendorLicenseModelEntity(vlmId, version));
186 if (vendorLicenseModel == null) {
187 throw new CoreException(new VendorLicenseModelNotFoundErrorBuilder(vlmId).build());
189 return vendorLicenseModel;
193 public LicenseAgreementEntity createLicenseAgreement(LicenseAgreementEntity licenseAgreement) {
194 VersioningUtil.validateEntitiesExistence(licenseAgreement.getFeatureGroupIds(),
195 new FeatureGroupEntity(licenseAgreement.getVendorLicenseModelId(),
196 licenseAgreement.getVersion(),
198 featureGroupDao, VendorLicenseModelEntity.ENTITY_TYPE);
200 uniqueValueUtil.validateUniqueValue(VendorLicenseConstants.UniqueValues.LICENSE_AGREEMENT_NAME,
201 licenseAgreement.getVendorLicenseModelId(), licenseAgreement.getVersion().getId(),
202 licenseAgreement.getName());
204 licenseAgreementDao.create(licenseAgreement);
205 uniqueValueUtil.createUniqueValue(VendorLicenseConstants.UniqueValues.LICENSE_AGREEMENT_NAME,
206 licenseAgreement.getVendorLicenseModelId(), licenseAgreement.getVersion().getId(),
207 licenseAgreement.getName());
208 if (licenseAgreement.getFeatureGroupIds() != null) {
209 for (String addedFgId : licenseAgreement.getFeatureGroupIds()) {
210 featureGroupDao.addReferencingLicenseAgreement(
211 new FeatureGroupEntity(licenseAgreement.getVendorLicenseModelId(),
212 licenseAgreement.getVersion(),
213 addedFgId), licenseAgreement.getId());
216 return licenseAgreement;
220 public FeatureGroupEntity createFeatureGroup(FeatureGroupEntity featureGroup) {
221 VersioningUtil.validateEntitiesExistence(featureGroup.getLicenseKeyGroupIds(),
222 new LicenseKeyGroupEntity(featureGroup.getVendorLicenseModelId(), featureGroup.getVersion(),
224 licenseKeyGroupDao, VendorLicenseModelEntity.ENTITY_TYPE);
225 VersioningUtil.validateEntitiesExistence(featureGroup.getEntitlementPoolIds(),
226 new EntitlementPoolEntity(featureGroup.getVendorLicenseModelId(), featureGroup.getVersion(),
228 entitlementPoolDao, VendorLicenseModelEntity.ENTITY_TYPE);
229 uniqueValueUtil.validateUniqueValue(VendorLicenseConstants.UniqueValues.FEATURE_GROUP_NAME,
230 featureGroup.getVendorLicenseModelId(), featureGroup.getVersion().getId(),
231 featureGroup.getName());
233 featureGroupDao.create(featureGroup);
234 uniqueValueUtil.createUniqueValue(VendorLicenseConstants.UniqueValues.FEATURE_GROUP_NAME,
235 featureGroup.getVendorLicenseModelId(), featureGroup.getVersion().getId(),
236 featureGroup.getName());
238 if (featureGroup.getLicenseKeyGroupIds() != null) {
239 for (String addedLkgId : featureGroup.getLicenseKeyGroupIds()) {
240 licenseKeyGroupDao.addReferencingFeatureGroup(
241 new LicenseKeyGroupEntity(featureGroup.getVendorLicenseModelId(),
242 featureGroup.getVersion(), addedLkgId),
243 featureGroup.getId());
247 if (featureGroup.getEntitlementPoolIds() != null) {
248 for (String addedEpId : featureGroup.getEntitlementPoolIds()) {
249 entitlementPoolDao.addReferencingFeatureGroup(
250 new EntitlementPoolEntity(featureGroup.getVendorLicenseModelId(),
251 featureGroup.getVersion(), addedEpId), featureGroup.getId());
258 public Collection<FeatureGroupEntity> listFeatureGroups(String vlmId, Version version) {
259 return featureGroupDao.list(new FeatureGroupEntity(vlmId, version, null));
264 public Collection<ErrorCode> validateLicensingData(String vlmId, Version version,
265 String licenseAgreementId,
266 Collection<String> featureGroupIds) {
268 List<ErrorCode> errorMessages = new ArrayList<>();
271 getLicenseAgreement(vlmId, version, licenseAgreementId);
272 } catch (CoreException exception) {
273 errorMessages.add(exception.code());
276 for (String featureGroupId : featureGroupIds) {
278 FeatureGroupEntity featureGroup =
279 getFeatureGroup(new FeatureGroupEntity(vlmId, version, featureGroupId));
280 if (!featureGroup.getReferencingLicenseAgreements().contains(licenseAgreementId)) {
281 errorMessages.add(new VersionableSubEntityNotFoundErrorBuilder(
282 featureGroup.getEntityType(),
284 LicenseAgreementEntity.ENTITY_TYPE,
288 } catch (CoreException exception) {
289 errorMessages.add(exception.code());
293 return errorMessages;
297 public LicenseAgreementEntity getLicenseAgreement(String vlmId, Version version,
298 String licenseAgreementId) {
299 LicenseAgreementEntity input = new LicenseAgreementEntity(vlmId, version, licenseAgreementId);
300 LicenseAgreementEntity retrieved = licenseAgreementDao.get(input);
301 VersioningUtil.validateEntityExistence(retrieved, input, VendorLicenseModelEntity.ENTITY_TYPE);
306 public Collection<LimitEntity> listLimits(String vlmId, Version version, String epLkgId) {
307 return limitDao.list(new LimitEntity(vlmId, version, epLkgId, null));
311 public LimitEntity createLimit(LimitEntity limit) {
312 limitDao.create(limit);
317 public void updateLimit(LimitEntity limit) {
318 limitDao.update(limit);
322 public void validate(String vendorLicenseModelId, Version version) {
323 Collection<String> allFeatureGroupEntities = new HashSet<>();
324 Collection<LicenseAgreementEntity> licenseAgreements = licenseAgreementDao
325 .list(new LicenseAgreementEntity(vendorLicenseModelId, version, null));
327 if (CollectionUtils.isNotEmpty(licenseAgreements)) {
328 licenseAgreements.forEach(licenseAgreement -> {
329 if (CollectionUtils.isEmpty(licenseAgreement.getFeatureGroupIds())) {
330 throw new CoreException(
331 new SubmitUncompletedLicenseModelErrorBuilder(
332 SUBMIT_UNCOMPLETED_VLM_MSG_LA_MISSING_FG).build());
334 allFeatureGroupEntities.addAll(licenseAgreement.getFeatureGroupIds());
337 allFeatureGroupEntities.forEach(fg -> {
338 FeatureGroupEntity featureGroupEntity =
339 featureGroupDao.get(new FeatureGroupEntity(vendorLicenseModelId, version, fg));
340 if (CollectionUtils.isEmpty(featureGroupEntity.getEntitlementPoolIds())) {
341 throw new CoreException(
342 new SubmitUncompletedLicenseModelErrorBuilder(
343 SUBMIT_UNCOMPLETED_VLM_MSG_FG_MISSING_EP).build());