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.openecomp.core.util.UniqueValueUtil;
24 import org.openecomp.core.utilities.CommonMethods;
25 import org.openecomp.sdc.common.errors.CoreException;
26 import org.openecomp.sdc.common.errors.ErrorCode;
27 import org.openecomp.sdc.vendorlicense.VendorLicenseConstants;
28 import org.openecomp.sdc.vendorlicense.dao.EntitlementPoolDao;
29 import org.openecomp.sdc.vendorlicense.dao.EntitlementPoolDaoFactory;
30 import org.openecomp.sdc.vendorlicense.dao.FeatureGroupDao;
31 import org.openecomp.sdc.vendorlicense.dao.FeatureGroupDaoFactory;
32 import org.openecomp.sdc.vendorlicense.dao.LicenseAgreementDao;
33 import org.openecomp.sdc.vendorlicense.dao.LicenseAgreementDaoFactory;
34 import org.openecomp.sdc.vendorlicense.dao.LicenseKeyGroupDao;
35 import org.openecomp.sdc.vendorlicense.dao.LicenseKeyGroupDaoFactory;
36 import org.openecomp.sdc.vendorlicense.dao.VendorLicenseModelDao;
37 import org.openecomp.sdc.vendorlicense.dao.VendorLicenseModelDaoFactory;
38 import org.openecomp.sdc.vendorlicense.dao.types.EntitlementPoolEntity;
39 import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity;
40 import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupModel;
41 import org.openecomp.sdc.vendorlicense.dao.types.LicenseAgreementEntity;
42 import org.openecomp.sdc.vendorlicense.dao.types.LicenseAgreementModel;
43 import org.openecomp.sdc.vendorlicense.dao.types.LicenseKeyGroupEntity;
44 import org.openecomp.sdc.vendorlicense.dao.types.VendorLicenseModelEntity;
45 import org.openecomp.sdc.vendorlicense.errors.SubmitUncompletedLicenseModelErrorBuilder;
46 import org.openecomp.sdc.vendorlicense.errors.VendorLicenseModelNotFoundErrorBuilder;
47 import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacade;
48 import org.openecomp.sdc.vendorlicense.types.VersionedVendorLicenseModel;
49 import org.openecomp.sdc.versioning.VersioningManager;
50 import org.openecomp.sdc.versioning.VersioningManagerFactory;
51 import org.openecomp.sdc.versioning.VersioningUtil;
52 import org.openecomp.sdc.versioning.dao.types.Version;
53 import org.openecomp.sdc.versioning.errors.RequestedVersionInvalidErrorBuilder;
54 import org.openecomp.sdc.versioning.errors.VersionableSubEntityNotFoundErrorBuilder;
55 import org.openecomp.sdc.versioning.types.VersionInfo;
56 import org.openecomp.sdc.versioning.types.VersionableEntityAction;
58 import java.util.ArrayList;
59 import java.util.Collection;
60 import java.util.Collections;
61 import java.util.List;
64 public class VendorLicenseFacadeImpl
65 implements VendorLicenseFacade {
68 private static final VersioningManager versioningManager =
69 VersioningManagerFactory.getInstance().createInterface();
71 private static final VendorLicenseModelDao vendorLicenseModelDao =
72 VendorLicenseModelDaoFactory.getInstance().createInterface();
73 private static final LicenseAgreementDao licenseAgreementDao =
74 LicenseAgreementDaoFactory.getInstance().createInterface();
75 private static final FeatureGroupDao featureGroupDao =
76 FeatureGroupDaoFactory.getInstance().createInterface();
77 private static final EntitlementPoolDao entitlementPoolDao =
78 EntitlementPoolDaoFactory.getInstance().createInterface();
79 private static final LicenseKeyGroupDao licenseKeyGroupDao =
80 LicenseKeyGroupDaoFactory.getInstance().createInterface();
83 * Instantiates a new Vendor license facade.
85 public VendorLicenseFacadeImpl() {
87 .registerVersioning(VendorLicenseConstants.VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE);
89 .registerVersioning(VendorLicenseConstants.VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE);
91 .registerVersioning(VendorLicenseConstants.VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE);
93 .registerVersioning(VendorLicenseConstants.VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE);
95 .registerVersioning(VendorLicenseConstants.VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE);
99 public void checkin(String vendorLicenseModelId, String user) {
100 Version newVersion = versioningManager
101 .checkin(VendorLicenseConstants
102 .VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE,
103 vendorLicenseModelId, user, null);
104 updateVlmLastModificationTime(vendorLicenseModelId, newVersion);
108 public void submit(String vendorLicenseModelId, String user) {
109 validateCompletedVendorLicenseModel(vendorLicenseModelId, user);
110 Version newVersion = versioningManager
111 .submit(VendorLicenseConstants
112 .VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE,
113 vendorLicenseModelId, user, null);
114 updateVlmLastModificationTime(vendorLicenseModelId, newVersion);
118 public FeatureGroupEntity getFeatureGroup(FeatureGroupEntity featureGroup, String user) {
119 Version version = VersioningUtil.resolveVersion(featureGroup.getVersion(),
120 getVersionInfo(featureGroup.getVendorLicenseModelId(), VersionableEntityAction.Read, user));
121 featureGroup.setVersion(version);
122 return getFeatureGroup(featureGroup);
125 private FeatureGroupEntity getFeatureGroup(FeatureGroupEntity featureGroup) {
126 FeatureGroupEntity retrieved = featureGroupDao.get(featureGroup);
128 .validateEntityExistence(retrieved, featureGroup, VendorLicenseModelEntity.ENTITY_TYPE);
133 public FeatureGroupModel getFeatureGroupModel(FeatureGroupEntity featureGroup, String user) {
134 FeatureGroupEntity retrieved = getFeatureGroup(featureGroup, user);
136 FeatureGroupModel featureGroupModel = new FeatureGroupModel();
137 featureGroupModel.setFeatureGroup(retrieved);
139 for (String licenseKeyGroupId : retrieved.getLicenseKeyGroupIds()) {
140 featureGroupModel.getLicenseKeyGroups().add(licenseKeyGroupDao.get(
141 new LicenseKeyGroupEntity(retrieved.getVendorLicenseModelId(), retrieved.getVersion(),
142 licenseKeyGroupId)));
144 for (String entitlementPoolId : retrieved.getEntitlementPoolIds()) {
145 featureGroupModel.getEntitlementPools().add(entitlementPoolDao.get(
146 new EntitlementPoolEntity(retrieved.getVendorLicenseModelId(), retrieved.getVersion(),
147 entitlementPoolId)));
150 return featureGroupModel;
154 public LicenseAgreementEntity getLicenseAgreement(String vlmId, Version version,
155 String licenseAgreementId, String user) {
156 return getLicenseAgreement(vlmId, licenseAgreementId, VersioningUtil
157 .resolveVersion(version, getVersionInfo(vlmId, VersionableEntityAction.Read, user)));
160 private LicenseAgreementEntity getLicenseAgreement(String vlmId, String licenseAgreementId,
162 LicenseAgreementEntity input = new LicenseAgreementEntity(vlmId, version, licenseAgreementId);
163 LicenseAgreementEntity retrieved = licenseAgreementDao.get(input);
164 VersioningUtil.validateEntityExistence(retrieved, input, VendorLicenseModelEntity.ENTITY_TYPE);
169 public LicenseAgreementModel getLicenseAgreementModel(String vlmId, Version version,
170 String licenseAgreementId, String user) {
171 LicenseAgreementEntity retrieved =
172 getLicenseAgreement(vlmId, version, licenseAgreementId, user);
174 LicenseAgreementModel licenseAgreementModel = new LicenseAgreementModel();
175 licenseAgreementModel.setLicenseAgreement(retrieved);
177 for (String featureGroupId : retrieved.getFeatureGroupIds()) {
178 licenseAgreementModel.getFeatureGroups().add(featureGroupDao
179 .get(new FeatureGroupEntity(vlmId, retrieved.getVersion(), featureGroupId)));
182 return licenseAgreementModel;
186 public EntitlementPoolEntity createEntitlementPool(EntitlementPoolEntity entitlementPool,
188 entitlementPool.setVersion(
189 getVersionInfo(entitlementPool.getVendorLicenseModelId(), VersionableEntityAction.Write,
190 user).getActiveVersion());
191 entitlementPool.setId(CommonMethods.nextUuId());
192 entitlementPool.setVersionUuId(CommonMethods.nextUuId());
193 UniqueValueUtil.createUniqueValue(VendorLicenseConstants.UniqueValues.ENTITLEMENT_POOL_NAME,
194 entitlementPool.getVendorLicenseModelId(), entitlementPool.getVersion().toString(),
195 entitlementPool.getName());
196 entitlementPoolDao.create(entitlementPool);
197 updateVlmLastModificationTime(entitlementPool.getVendorLicenseModelId(),
198 entitlementPool.getVersion());
199 return entitlementPool;
203 public LicenseKeyGroupEntity createLicenseKeyGroup(LicenseKeyGroupEntity licenseKeyGroup,
205 licenseKeyGroup.setVersion(
206 getVersionInfo(licenseKeyGroup.getVendorLicenseModelId(), VersionableEntityAction.Write,
207 user).getActiveVersion());
208 licenseKeyGroup.setId(CommonMethods.nextUuId());
209 licenseKeyGroup.setVersionUuId(CommonMethods.nextUuId());
210 UniqueValueUtil.createUniqueValue(VendorLicenseConstants.UniqueValues.LICENSE_KEY_GROUP_NAME,
211 licenseKeyGroup.getVendorLicenseModelId(), licenseKeyGroup.getVersion().toString(),
212 licenseKeyGroup.getName());
213 licenseKeyGroupDao.create(licenseKeyGroup);
214 updateVlmLastModificationTime(licenseKeyGroup.getVendorLicenseModelId(),
215 licenseKeyGroup.getVersion());
216 return licenseKeyGroup;
220 public void updateEntitlementPool(EntitlementPoolEntity entitlementPool, String user) {
221 entitlementPool.setVersion(
222 getVersionInfo(entitlementPool.getVendorLicenseModelId(), VersionableEntityAction.Write,
223 user).getActiveVersion());
224 EntitlementPoolEntity retrieved = entitlementPoolDao.get(entitlementPool);
226 .validateEntityExistence(retrieved, entitlementPool, VendorLicenseModelEntity.ENTITY_TYPE);
228 UniqueValueUtil.updateUniqueValue(VendorLicenseConstants.UniqueValues.ENTITLEMENT_POOL_NAME,
229 retrieved.getName(), entitlementPool.getName(), entitlementPool.getVendorLicenseModelId(),
230 entitlementPool.getVersion().toString());
231 entitlementPool.setVersionUuId(CommonMethods.nextUuId());
232 entitlementPoolDao.update(entitlementPool);
234 updateVlmLastModificationTime(entitlementPool.getVendorLicenseModelId(),
235 entitlementPool.getVersion());
240 public Collection<LicenseKeyGroupEntity> listLicenseKeyGroups(String vlmId, Version version,
242 return licenseKeyGroupDao.list(new LicenseKeyGroupEntity(vlmId, VersioningUtil
243 .resolveVersion(version, getVersionInfo(vlmId, VersionableEntityAction.Read, user)), null));
247 public Collection<EntitlementPoolEntity> listEntitlementPools(String vlmId, Version version,
249 return entitlementPoolDao.list(new EntitlementPoolEntity(vlmId, VersioningUtil
250 .resolveVersion(version, getVersionInfo(vlmId, VersionableEntityAction.Read, user)), null));
254 public void updateLicenseKeyGroup(LicenseKeyGroupEntity licenseKeyGroup, String user) {
255 licenseKeyGroup.setVersion(
256 getVersionInfo(licenseKeyGroup.getVendorLicenseModelId(), VersionableEntityAction.Write,
257 user).getActiveVersion());
258 LicenseKeyGroupEntity retrieved = licenseKeyGroupDao.get(licenseKeyGroup);
259 licenseKeyGroup.setVersionUuId((CommonMethods.nextUuId()));
261 .validateEntityExistence(retrieved, licenseKeyGroup, VendorLicenseModelEntity.ENTITY_TYPE);
262 UniqueValueUtil.updateUniqueValue(VendorLicenseConstants.UniqueValues.LICENSE_KEY_GROUP_NAME,
263 retrieved.getName(), licenseKeyGroup.getName(), licenseKeyGroup.getVendorLicenseModelId(),
264 licenseKeyGroup.getVersion().toString());
265 licenseKeyGroupDao.update(licenseKeyGroup);
267 updateVlmLastModificationTime(licenseKeyGroup.getVendorLicenseModelId(),
268 licenseKeyGroup.getVersion());
273 public VersionInfo getVersionInfo(String vendorLicenseModelId, VersionableEntityAction action,
275 return versioningManager
276 .getEntityVersionInfo(VendorLicenseConstants
277 .VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE,
278 vendorLicenseModelId, user,
283 public VersionedVendorLicenseModel getVendorLicenseModel(String vlmId, Version version,
285 VersionInfo versionInfo = getVersionInfo(vlmId, VersionableEntityAction.Read, user);
287 VendorLicenseModelEntity vendorLicenseModel = vendorLicenseModelDao.get(
288 new VendorLicenseModelEntity(vlmId, VersioningUtil.resolveVersion(version, versionInfo)));
289 if (vendorLicenseModel == null) {
290 throw new CoreException(new VendorLicenseModelNotFoundErrorBuilder(vlmId).build());
293 return new VersionedVendorLicenseModel(vendorLicenseModel, versionInfo);
297 public VendorLicenseModelEntity createVendorLicenseModel(
298 VendorLicenseModelEntity vendorLicenseModelEntity, String user) {
299 UniqueValueUtil.validateUniqueValue(VendorLicenseConstants.UniqueValues.VENDOR_NAME,
300 vendorLicenseModelEntity.getVendorName());
301 vendorLicenseModelEntity.setId(CommonMethods.nextUuId());
303 Version version = versioningManager
304 .create(VendorLicenseConstants
305 .VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE,
306 vendorLicenseModelEntity.getId(), user);
307 vendorLicenseModelEntity.setVersion(version);
309 // vendorLicenseModelEntity.setLastModificationTime(new Date());
311 vendorLicenseModelDao.create(vendorLicenseModelEntity);
312 UniqueValueUtil.createUniqueValue(VendorLicenseConstants.UniqueValues.VENDOR_NAME,
313 vendorLicenseModelEntity.getVendorName());
315 return vendorLicenseModelEntity;
319 public LicenseAgreementEntity createLicenseAgreement(LicenseAgreementEntity licenseAgreement,
321 Version activeVersion =
322 getVersionInfo(licenseAgreement.getVendorLicenseModelId(), VersionableEntityAction.Write,
323 user).getActiveVersion();
324 licenseAgreement.setVersion(activeVersion);
325 licenseAgreement.setId(CommonMethods.nextUuId());
326 VersioningUtil.validateEntitiesExistence(licenseAgreement.getFeatureGroupIds(),
327 new FeatureGroupEntity(licenseAgreement.getVendorLicenseModelId(), activeVersion, null),
328 featureGroupDao, VendorLicenseModelEntity.ENTITY_TYPE);
329 UniqueValueUtil.validateUniqueValue(VendorLicenseConstants.UniqueValues.LICENSE_AGREEMENT_NAME,
330 licenseAgreement.getVendorLicenseModelId(), licenseAgreement.getVersion().toString(),
331 licenseAgreement.getName());
333 if (licenseAgreement.getFeatureGroupIds() != null) {
334 for (String addedFgId : licenseAgreement.getFeatureGroupIds()) {
335 featureGroupDao.addReferencingLicenseAgreement(
336 new FeatureGroupEntity(licenseAgreement.getVendorLicenseModelId(), activeVersion,
337 addedFgId), licenseAgreement.getId());
341 licenseAgreementDao.create(licenseAgreement);
342 UniqueValueUtil.createUniqueValue(VendorLicenseConstants.UniqueValues.LICENSE_AGREEMENT_NAME,
343 licenseAgreement.getVendorLicenseModelId(), licenseAgreement.getVersion().toString(),
344 licenseAgreement.getName());
346 updateVlmLastModificationTime(licenseAgreement.getVendorLicenseModelId(),
347 licenseAgreement.getVersion());
349 return licenseAgreement;
353 public FeatureGroupEntity createFeatureGroup(FeatureGroupEntity featureGroup, String user) {
354 Version activeVersion =
355 getVersionInfo(featureGroup.getVendorLicenseModelId(), VersionableEntityAction.Write, user)
357 featureGroup.setId(CommonMethods.nextUuId());
358 featureGroup.setVersion(activeVersion);
359 VersioningUtil.validateEntitiesExistence(featureGroup.getLicenseKeyGroupIds(),
360 new LicenseKeyGroupEntity(featureGroup.getVendorLicenseModelId(), activeVersion, null),
361 licenseKeyGroupDao, VendorLicenseModelEntity.ENTITY_TYPE);
362 VersioningUtil.validateEntitiesExistence(featureGroup.getEntitlementPoolIds(),
363 new EntitlementPoolEntity(featureGroup.getVendorLicenseModelId(), activeVersion, null),
364 entitlementPoolDao, VendorLicenseModelEntity.ENTITY_TYPE);
365 UniqueValueUtil.validateUniqueValue(VendorLicenseConstants.UniqueValues.FEATURE_GROUP_NAME,
366 featureGroup.getVendorLicenseModelId(), featureGroup.getVersion().toString(),
367 featureGroup.getName());
369 if (featureGroup.getLicenseKeyGroupIds() != null) {
370 for (String addedLkgId : featureGroup.getLicenseKeyGroupIds()) {
371 licenseKeyGroupDao.addReferencingFeatureGroup(
372 new LicenseKeyGroupEntity(featureGroup.getVendorLicenseModelId(), activeVersion,
373 addedLkgId), featureGroup.getId());
377 if (featureGroup.getEntitlementPoolIds() != null) {
378 for (String addedEpId : featureGroup.getEntitlementPoolIds()) {
379 entitlementPoolDao.addReferencingFeatureGroup(
380 new EntitlementPoolEntity(featureGroup.getVendorLicenseModelId(), activeVersion,
381 addedEpId), featureGroup.getId());
385 featureGroupDao.create(featureGroup);
386 UniqueValueUtil.createUniqueValue(VendorLicenseConstants.UniqueValues.FEATURE_GROUP_NAME,
387 featureGroup.getVendorLicenseModelId(), featureGroup.getVersion().toString(),
388 featureGroup.getName());
390 updateVlmLastModificationTime(featureGroup.getVendorLicenseModelId(),
391 featureGroup.getVersion());
398 public Collection<ErrorCode> validateLicensingData(String vlmId, Version version,
399 String licenseAgreementId,
400 Collection<String> featureGroupIds) {
402 VersionInfo versionInfo = getVersionInfo(vlmId, VersionableEntityAction.Read, "");
403 if (version == null || !version.isFinal()
404 || !versionInfo.getViewableVersions().contains(version)) {
405 return Collections.singletonList(new RequestedVersionInvalidErrorBuilder().build());
407 } catch (CoreException coreException) {
408 return Collections.singletonList(coreException.code());
411 List<ErrorCode> errorMessages = new ArrayList<>();
414 getLicenseAgreement(vlmId, licenseAgreementId, version);
415 } catch (CoreException coreException) {
416 errorMessages.add(coreException.code());
419 for (String featureGroupId : featureGroupIds) {
421 FeatureGroupEntity featureGroup =
422 getFeatureGroup(new FeatureGroupEntity(vlmId, version, featureGroupId));
423 if (!featureGroup.getReferencingLicenseAgreements().contains(licenseAgreementId)) {
424 errorMessages.add(new VersionableSubEntityNotFoundErrorBuilder(
425 featureGroup.getEntityType(),
427 LicenseAgreementEntity.ENTITY_TYPE,
431 } catch (CoreException coreException) {
432 errorMessages.add(coreException.code());
436 return errorMessages;
440 public void updateVlmLastModificationTime(String vendorLicenseModelId, Version version) {
441 VendorLicenseModelEntity retrieved =
442 vendorLicenseModelDao.get(new VendorLicenseModelEntity(vendorLicenseModelId, version));
443 vendorLicenseModelDao.update(retrieved);
444 // entity.setLastModificationTime(new Date());
446 // vendorLicenseModelDao.updateLastModificationTime(entity);
449 private void validateCompletedVendorLicenseModel(String vendorLicenseModelId, String user) {
450 Version activeVersion =
451 getVersionInfo(vendorLicenseModelId, VersionableEntityAction.Read, user).getActiveVersion();
452 Collection<LicenseAgreementEntity> licenseAgreements = licenseAgreementDao
453 .list(new LicenseAgreementEntity(vendorLicenseModelId, activeVersion, null));
455 if (licenseAgreements == null || licenseAgreements.isEmpty()) {
456 throw new CoreException(
457 new SubmitUncompletedLicenseModelErrorBuilder(vendorLicenseModelId).build());
460 for (LicenseAgreementEntity licenseAgreement : licenseAgreements) {
461 if (licenseAgreement.getFeatureGroupIds() == null
462 || licenseAgreement.getFeatureGroupIds().isEmpty()) {
463 throw new CoreException(
464 new SubmitUncompletedLicenseModelErrorBuilder(vendorLicenseModelId).build());
468 Collection<FeatureGroupEntity> featureGroupEntities = featureGroupDao
469 .list(new FeatureGroupEntity(vendorLicenseModelId, activeVersion, null));
470 for (FeatureGroupEntity featureGroupEntity : featureGroupEntities) {
471 if (featureGroupEntity.getEntitlementPoolIds() == null
472 || featureGroupEntity.getEntitlementPoolIds().isEmpty()) {
473 throw new CoreException(
474 new SubmitUncompletedLicenseModelErrorBuilder(vendorLicenseModelId).build());