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.datatypes.error.ErrorLevel;
29 import org.openecomp.sdc.logging.context.impl.MdcDataDebugMessage;
30 import org.openecomp.sdc.logging.context.impl.MdcDataErrorMessage;
31 import org.openecomp.sdc.logging.types.LoggerConstants;
32 import org.openecomp.sdc.logging.types.LoggerErrorCode;
33 import org.openecomp.sdc.logging.types.LoggerErrorDescription;
34 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
35 import org.openecomp.sdc.vendorlicense.VendorLicenseConstants;
36 import org.openecomp.sdc.vendorlicense.dao.EntitlementPoolDao;
37 import org.openecomp.sdc.vendorlicense.dao.EntitlementPoolDaoFactory;
38 import org.openecomp.sdc.vendorlicense.dao.FeatureGroupDao;
39 import org.openecomp.sdc.vendorlicense.dao.FeatureGroupDaoFactory;
40 import org.openecomp.sdc.vendorlicense.dao.LicenseAgreementDao;
41 import org.openecomp.sdc.vendorlicense.dao.LicenseAgreementDaoFactory;
42 import org.openecomp.sdc.vendorlicense.dao.LicenseKeyGroupDao;
43 import org.openecomp.sdc.vendorlicense.dao.LicenseKeyGroupDaoFactory;
44 import org.openecomp.sdc.vendorlicense.dao.LimitDao;
45 import org.openecomp.sdc.vendorlicense.dao.LimitDaoFactory;
46 import org.openecomp.sdc.vendorlicense.dao.VendorLicenseModelDao;
47 import org.openecomp.sdc.vendorlicense.dao.VendorLicenseModelDaoFactory;
48 import org.openecomp.sdc.vendorlicense.dao.types.EntitlementPoolEntity;
49 import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity;
50 import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupModel;
51 import org.openecomp.sdc.vendorlicense.dao.types.LicenseAgreementEntity;
52 import org.openecomp.sdc.vendorlicense.dao.types.LicenseAgreementModel;
53 import org.openecomp.sdc.vendorlicense.dao.types.LicenseKeyGroupEntity;
54 import org.openecomp.sdc.vendorlicense.dao.types.LimitEntity;
55 import org.openecomp.sdc.vendorlicense.dao.types.VendorLicenseModelEntity;
56 import org.openecomp.sdc.vendorlicense.errors.SubmitUncompletedLicenseModelErrorBuilder;
57 import org.openecomp.sdc.vendorlicense.errors.VendorLicenseModelNotFoundErrorBuilder;
58 import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacade;
59 import org.openecomp.sdc.versioning.VersioningUtil;
60 import org.openecomp.sdc.versioning.dao.types.Version;
61 import org.openecomp.sdc.versioning.errors.VersionableSubEntityNotFoundErrorBuilder;
63 import java.util.ArrayList;
64 import java.util.Collection;
65 import java.util.HashSet;
66 import java.util.List;
67 import java.util.Objects;
69 import static org.openecomp.sdc.vendorlicense.VendorLicenseConstants.VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE;
70 import static org.openecomp.sdc.vendorlicense.errors.UncompletedVendorLicenseModelErrorType.SUBMIT_UNCOMPLETED_VLM_MSG_FG_MISSING_EP;
71 import static org.openecomp.sdc.vendorlicense.errors.UncompletedVendorLicenseModelErrorType.SUBMIT_UNCOMPLETED_VLM_MSG_LA_MISSING_FG;
73 public class VendorLicenseFacadeImpl implements VendorLicenseFacade {
74 private static final VendorLicenseModelDao
75 vendorLicenseModelDao = VendorLicenseModelDaoFactory.getInstance().createInterface();
76 private static final LicenseAgreementDao
77 licenseAgreementDao = LicenseAgreementDaoFactory.getInstance().createInterface();
78 private static final FeatureGroupDao featureGroupDao =
79 FeatureGroupDaoFactory.getInstance().createInterface();
80 private static final EntitlementPoolDao
81 entitlementPoolDao = EntitlementPoolDaoFactory.getInstance().createInterface();
82 private static final LicenseKeyGroupDao
83 licenseKeyGroupDao = LicenseKeyGroupDaoFactory.getInstance().createInterface();
84 private static final LimitDao limitDao = LimitDaoFactory.getInstance().createInterface();
85 private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
88 * Instantiates a new Vendor license facade.
90 public VendorLicenseFacadeImpl() {
91 vendorLicenseModelDao.registerVersioning(VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE);
92 licenseAgreementDao.registerVersioning(VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE);
93 featureGroupDao.registerVersioning(VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE);
94 entitlementPoolDao.registerVersioning(VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE);
95 licenseKeyGroupDao.registerVersioning(VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE);
96 limitDao.registerVersioning(VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE);
100 public FeatureGroupEntity getFeatureGroup(FeatureGroupEntity featureGroup) {
101 FeatureGroupEntity retrieved = featureGroupDao.get(featureGroup);
103 .validateEntityExistence(retrieved, featureGroup, VendorLicenseModelEntity.ENTITY_TYPE);
104 if (retrieved.getManufacturerReferenceNumber() == null) {
105 updateManufacturerNumberInFeatureGroup(retrieved);
111 public FeatureGroupModel getFeatureGroupModel(FeatureGroupEntity featureGroup) {
112 FeatureGroupEntity retrieved = getFeatureGroup(featureGroup);
114 FeatureGroupModel featureGroupModel = new FeatureGroupModel();
115 featureGroupModel.setFeatureGroup(retrieved);
117 for (String licenseKeyGroupId : retrieved.getLicenseKeyGroupIds()) {
118 featureGroupModel.getLicenseKeyGroups().add(licenseKeyGroupDao.get(
119 new LicenseKeyGroupEntity(retrieved.getVendorLicenseModelId(), retrieved.getVersion(),
120 licenseKeyGroupId)));
122 for (String entitlementPoolId : retrieved.getEntitlementPoolIds()) {
123 featureGroupModel.getEntitlementPools().add(entitlementPoolDao.get(
124 new EntitlementPoolEntity(retrieved.getVendorLicenseModelId(), retrieved.getVersion(),
125 entitlementPoolId)));
128 return featureGroupModel;
132 public LicenseAgreementModel getLicenseAgreementModel(String vlmId, Version version,
133 String licenseAgreementId) {
134 LicenseAgreementEntity retrieved =
135 getLicenseAgreement(vlmId, version, licenseAgreementId);
137 LicenseAgreementModel licenseAgreementModel = new LicenseAgreementModel();
138 licenseAgreementModel.setLicenseAgreement(retrieved);
140 for (String featureGroupId : retrieved.getFeatureGroupIds()) {
141 licenseAgreementModel.getFeatureGroups().add(featureGroupDao
142 .get(new FeatureGroupEntity(vlmId, retrieved.getVersion(), featureGroupId)));
145 return licenseAgreementModel;
149 public EntitlementPoolEntity createEntitlementPool(EntitlementPoolEntity entitlementPool) {
150 entitlementPool.setVersionUuId(CommonMethods.nextUuId());
151 UniqueValueUtil.createUniqueValue(VendorLicenseConstants.UniqueValues.ENTITLEMENT_POOL_NAME,
152 entitlementPool.getVendorLicenseModelId(), entitlementPool.getVersion().getId(),
153 entitlementPool.getName());
154 entitlementPoolDao.create(entitlementPool);
155 return entitlementPool;
159 public void updateEntitlementPool(EntitlementPoolEntity entitlementPool) {
160 EntitlementPoolEntity retrieved = entitlementPoolDao.get(entitlementPool);
162 .validateEntityExistence(retrieved, entitlementPool, VendorLicenseModelEntity.ENTITY_TYPE);
163 if (retrieved.equals(entitlementPool)) {
166 UniqueValueUtil.updateUniqueValue(VendorLicenseConstants.UniqueValues.ENTITLEMENT_POOL_NAME,
167 retrieved.getName(), entitlementPool.getName(), entitlementPool.getVendorLicenseModelId(),
168 entitlementPool.getVersion().getId());
169 entitlementPool.setVersionUuId(CommonMethods.nextUuId());
170 entitlementPoolDao.update(entitlementPool);
174 public Collection<LicenseKeyGroupEntity> listLicenseKeyGroups(String vlmId, Version version) {
175 return licenseKeyGroupDao.list(new LicenseKeyGroupEntity(vlmId, version, null));
179 public Collection<EntitlementPoolEntity> listEntitlementPools(String vlmId, Version version) {
180 return entitlementPoolDao.list(new EntitlementPoolEntity(vlmId, version, null));
184 public void updateLicenseKeyGroup(LicenseKeyGroupEntity licenseKeyGroup) {
185 LicenseKeyGroupEntity retrieved = licenseKeyGroupDao.get(licenseKeyGroup);
186 if (retrieved.equals(licenseKeyGroup)) {
189 licenseKeyGroup.setVersionUuId((CommonMethods.nextUuId()));
191 .validateEntityExistence(retrieved, licenseKeyGroup, VendorLicenseModelEntity.ENTITY_TYPE);
192 UniqueValueUtil.updateUniqueValue(VendorLicenseConstants.UniqueValues.LICENSE_KEY_GROUP_NAME,
193 retrieved.getName(), licenseKeyGroup.getName(), licenseKeyGroup.getVendorLicenseModelId(),
194 licenseKeyGroup.getVersion().getId());
195 licenseKeyGroupDao.update(licenseKeyGroup);
199 public LicenseKeyGroupEntity createLicenseKeyGroup(LicenseKeyGroupEntity licenseKeyGroup) {
200 licenseKeyGroup.setVersionUuId(CommonMethods.nextUuId());
201 UniqueValueUtil.createUniqueValue(VendorLicenseConstants.UniqueValues.LICENSE_KEY_GROUP_NAME,
202 licenseKeyGroup.getVendorLicenseModelId(), licenseKeyGroup.getVersion().getId(),
203 licenseKeyGroup.getName());
204 licenseKeyGroupDao.create(licenseKeyGroup);
205 return licenseKeyGroup;
209 public VendorLicenseModelEntity getVendorLicenseModel(String vlmId, Version version) {
210 mdcDataDebugMessage.debugEntryMessage("VLM id", vlmId);
212 VendorLicenseModelEntity vendorLicenseModel =
213 vendorLicenseModelDao.get(new VendorLicenseModelEntity(vlmId, version));
214 if (vendorLicenseModel == null) {
215 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
216 LoggerTragetServiceName.GET_VLM, ErrorLevel.ERROR.name(),
217 LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.ENTITY_NOT_FOUND);
218 throw new CoreException(new VendorLicenseModelNotFoundErrorBuilder(vlmId).build());
221 mdcDataDebugMessage.debugExitMessage("VLM id", vlmId);
222 return vendorLicenseModel;
226 public LicenseAgreementEntity createLicenseAgreement(LicenseAgreementEntity licenseAgreement) {
227 //licenseAgreement.setId(CommonMethods.nextUuId());
228 VersioningUtil.validateEntitiesExistence(licenseAgreement.getFeatureGroupIds(),
229 new FeatureGroupEntity(licenseAgreement.getVendorLicenseModelId(),
230 licenseAgreement.getVersion(),
232 featureGroupDao, VendorLicenseModelEntity.ENTITY_TYPE);
234 UniqueValueUtil.validateUniqueValue(VendorLicenseConstants.UniqueValues.LICENSE_AGREEMENT_NAME,
235 licenseAgreement.getVendorLicenseModelId(), licenseAgreement.getVersion().getId(),
236 licenseAgreement.getName());
238 licenseAgreementDao.create(licenseAgreement);
239 UniqueValueUtil.createUniqueValue(VendorLicenseConstants.UniqueValues.LICENSE_AGREEMENT_NAME,
240 licenseAgreement.getVendorLicenseModelId(), licenseAgreement.getVersion().getId(),
241 licenseAgreement.getName());
242 if (licenseAgreement.getFeatureGroupIds() != null) {
243 for (String addedFgId : licenseAgreement.getFeatureGroupIds()) {
244 featureGroupDao.addReferencingLicenseAgreement(
245 new FeatureGroupEntity(licenseAgreement.getVendorLicenseModelId(),
246 licenseAgreement.getVersion(),
247 addedFgId), licenseAgreement.getId());
250 return licenseAgreement;
254 public FeatureGroupEntity createFeatureGroup(FeatureGroupEntity featureGroup) {
255 VersioningUtil.validateEntitiesExistence(featureGroup.getLicenseKeyGroupIds(),
256 new LicenseKeyGroupEntity(featureGroup.getVendorLicenseModelId(), featureGroup.getVersion(),
258 licenseKeyGroupDao, VendorLicenseModelEntity.ENTITY_TYPE);
259 VersioningUtil.validateEntitiesExistence(featureGroup.getEntitlementPoolIds(),
260 new EntitlementPoolEntity(featureGroup.getVendorLicenseModelId(), featureGroup.getVersion(),
262 entitlementPoolDao, VendorLicenseModelEntity.ENTITY_TYPE);
263 UniqueValueUtil.validateUniqueValue(VendorLicenseConstants.UniqueValues.FEATURE_GROUP_NAME,
264 featureGroup.getVendorLicenseModelId(), featureGroup.getVersion().getId(),
265 featureGroup.getName());
267 featureGroupDao.create(featureGroup);
268 UniqueValueUtil.createUniqueValue(VendorLicenseConstants.UniqueValues.FEATURE_GROUP_NAME,
269 featureGroup.getVendorLicenseModelId(), featureGroup.getVersion().getId(),
270 featureGroup.getName());
272 if (featureGroup.getLicenseKeyGroupIds() != null) {
273 for (String addedLkgId : featureGroup.getLicenseKeyGroupIds()) {
274 licenseKeyGroupDao.addReferencingFeatureGroup(
275 new LicenseKeyGroupEntity(featureGroup.getVendorLicenseModelId(),
276 featureGroup.getVersion(), addedLkgId),
277 featureGroup.getId());
281 if (featureGroup.getEntitlementPoolIds() != null) {
282 for (String addedEpId : featureGroup.getEntitlementPoolIds()) {
283 entitlementPoolDao.addReferencingFeatureGroup(
284 new EntitlementPoolEntity(featureGroup.getVendorLicenseModelId(),
285 featureGroup.getVersion(), addedEpId), featureGroup.getId());
292 public Collection<FeatureGroupEntity> listFeatureGroups(String vlmId, Version version) {
293 Collection<FeatureGroupEntity> featureGroupEntities =
294 featureGroupDao.list(new FeatureGroupEntity(vlmId, version, null));
295 featureGroupEntities.stream()
296 .filter(fgEntity -> Objects.isNull(fgEntity.getManufacturerReferenceNumber()))
297 .forEach(this::updateManufacturerNumberInFeatureGroup);
298 return featureGroupEntities;
303 public Collection<ErrorCode> validateLicensingData(String vlmId, Version version,
304 String licenseAgreementId,
305 Collection<String> featureGroupIds) {
306 // TODO: 5/21/2017 validate version exists and final
308 VersionInfo versionInfo = getVersionInfo(vlmId, VersionableEntityAction.Read, "");
309 if (version == null || !version.isFinal()
310 || !versionInfo.getViewableVersions().contains(version)) {
311 return Collections.singletonList(new RequestedVersionInvalidErrorBuilder().build());
313 } catch (CoreException exception) {
314 return Collections.singletonList(exception.code());
317 List<ErrorCode> errorMessages = new ArrayList<>();
320 getLicenseAgreement(vlmId, version, licenseAgreementId);
321 } catch (CoreException exception) {
322 errorMessages.add(exception.code());
325 for (String featureGroupId : featureGroupIds) {
327 FeatureGroupEntity featureGroup =
328 getFeatureGroup(new FeatureGroupEntity(vlmId, version, featureGroupId));
329 if (!featureGroup.getReferencingLicenseAgreements().contains(licenseAgreementId)) {
330 errorMessages.add(new VersionableSubEntityNotFoundErrorBuilder(
331 featureGroup.getEntityType(),
333 LicenseAgreementEntity.ENTITY_TYPE,
337 } catch (CoreException exception) {
338 errorMessages.add(exception.code());
342 return errorMessages;
346 public LicenseAgreementEntity getLicenseAgreement(String vlmId, Version version,
347 String licenseAgreementId) {
348 LicenseAgreementEntity input = new LicenseAgreementEntity(vlmId, version, licenseAgreementId);
349 LicenseAgreementEntity retrieved = licenseAgreementDao.get(input);
350 VersioningUtil.validateEntityExistence(retrieved, input, VendorLicenseModelEntity.ENTITY_TYPE);
355 public Collection<LimitEntity> listLimits(String vlmId, Version version, String epLkgId) {
356 return limitDao.list(new LimitEntity(vlmId, version, epLkgId, null));
360 public LimitEntity createLimit(LimitEntity limit) {
361 limitDao.create(limit);
366 public void updateLimit(LimitEntity limit) {
367 limitDao.update(limit);
370 private void updateManufacturerNumberInFeatureGroup(FeatureGroupEntity featureGroupEntity) {
371 if (CollectionUtils.isNotEmpty(featureGroupEntity.getEntitlementPoolIds())) {
372 Object[] entitlementPoolIdsList = featureGroupEntity.getEntitlementPoolIds().toArray();
373 if (entitlementPoolIdsList.length > 0) {
374 String entitlementPoolId = entitlementPoolIdsList[0].toString();
375 EntitlementPoolEntity entitlementPoolEntity =
376 new EntitlementPoolEntity(featureGroupEntity.getVendorLicenseModelId(),
377 featureGroupEntity.getVersion(), entitlementPoolId);
378 entitlementPoolEntity = entitlementPoolDao.get(entitlementPoolEntity);
379 featureGroupEntity.setManufacturerReferenceNumber(
380 entitlementPoolDao.getManufacturerReferenceNumber(entitlementPoolEntity));
381 featureGroupDao.update(featureGroupEntity);
387 public void validate(String vendorLicenseModelId, Version version) {
388 Collection<String> allFeatureGroupEntities = new HashSet<>();
389 Collection<LicenseAgreementEntity> licenseAgreements = licenseAgreementDao
390 .list(new LicenseAgreementEntity(vendorLicenseModelId, version, null));
392 if (CollectionUtils.isNotEmpty(licenseAgreements)) {
393 licenseAgreements.forEach(licenseAgreement -> {
394 if (CollectionUtils.isEmpty(licenseAgreement.getFeatureGroupIds())) {
395 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
396 LoggerTragetServiceName.SUBMIT_ENTITY, ErrorLevel.ERROR.name(),
397 LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.SUBMIT_ENTITY);
398 throw new CoreException(
399 new SubmitUncompletedLicenseModelErrorBuilder(
400 SUBMIT_UNCOMPLETED_VLM_MSG_LA_MISSING_FG).build());
402 allFeatureGroupEntities.addAll(licenseAgreement.getFeatureGroupIds());
405 allFeatureGroupEntities.forEach(fg -> {
406 FeatureGroupEntity featureGroupEntity =
407 featureGroupDao.get(new FeatureGroupEntity(vendorLicenseModelId, version, fg));
408 if (CollectionUtils.isEmpty(featureGroupEntity.getEntitlementPoolIds())) {
409 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
410 LoggerTragetServiceName.SUBMIT_ENTITY, ErrorLevel.ERROR.name(),
411 LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.SUBMIT_ENTITY);
412 throw new CoreException(
413 new SubmitUncompletedLicenseModelErrorBuilder(
414 SUBMIT_UNCOMPLETED_VLM_MSG_FG_MISSING_EP).build());