50772a3de39d3a0e3451209f784fece384d37415
[sdc.git] / openecomp-be / lib / openecomp-sdc-vendor-license-lib / openecomp-sdc-vendor-license-core / src / main / java / org / openecomp / sdc / vendorlicense / facade / impl / VendorLicenseFacadeImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.openecomp.sdc.vendorlicense.facade.impl;
22
23 import static org.openecomp.sdc.common.errors.ValidationErrorBuilder.FIELD_VALIDATION_ERROR_ERR_ID;
24 import static org.openecomp.sdc.vendorlicense.VendorLicenseConstants.VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE;
25 import static org.openecomp.sdc.vendorlicense.errors.UncompletedVendorLicenseModelErrorType.SUBMIT_UNCOMPLETED_VLM_MSG_FG_MISSING_EP;
26 import static org.openecomp.sdc.vendorlicense.errors.UncompletedVendorLicenseModelErrorType.SUBMIT_UNCOMPLETED_VLM_MSG_LA_MISSING_FG;
27
28 import java.util.ArrayList;
29 import java.util.Collection;
30 import java.util.HashSet;
31 import java.util.List;
32 import java.util.Optional;
33 import org.apache.commons.collections4.CollectionUtils;
34 import org.openecomp.core.dao.UniqueValueDaoFactory;
35 import org.openecomp.core.util.UniqueValueUtil;
36 import org.openecomp.core.utilities.CommonMethods;
37 import org.openecomp.sdc.common.errors.CoreException;
38 import org.openecomp.sdc.common.errors.ErrorCategory;
39 import org.openecomp.sdc.common.errors.ErrorCode;
40 import org.openecomp.sdc.vendorlicense.VendorLicenseConstants;
41 import org.openecomp.sdc.vendorlicense.dao.EntitlementPoolDao;
42 import org.openecomp.sdc.vendorlicense.dao.EntitlementPoolDaoFactory;
43 import org.openecomp.sdc.vendorlicense.dao.FeatureGroupDao;
44 import org.openecomp.sdc.vendorlicense.dao.FeatureGroupDaoFactory;
45 import org.openecomp.sdc.vendorlicense.dao.LicenseAgreementDao;
46 import org.openecomp.sdc.vendorlicense.dao.LicenseAgreementDaoFactory;
47 import org.openecomp.sdc.vendorlicense.dao.LicenseKeyGroupDao;
48 import org.openecomp.sdc.vendorlicense.dao.LicenseKeyGroupDaoFactory;
49 import org.openecomp.sdc.vendorlicense.dao.LimitDao;
50 import org.openecomp.sdc.vendorlicense.dao.LimitDaoFactory;
51 import org.openecomp.sdc.vendorlicense.dao.VendorLicenseModelDao;
52 import org.openecomp.sdc.vendorlicense.dao.VendorLicenseModelDaoFactory;
53 import org.openecomp.sdc.vendorlicense.dao.types.EntitlementPoolEntity;
54 import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity;
55 import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupModel;
56 import org.openecomp.sdc.vendorlicense.dao.types.LicenseAgreementEntity;
57 import org.openecomp.sdc.vendorlicense.dao.types.LicenseAgreementModel;
58 import org.openecomp.sdc.vendorlicense.dao.types.LicenseKeyGroupEntity;
59 import org.openecomp.sdc.vendorlicense.dao.types.LimitEntity;
60 import org.openecomp.sdc.vendorlicense.dao.types.VendorLicenseModelEntity;
61 import org.openecomp.sdc.vendorlicense.errors.SubmitUncompletedLicenseModelErrorBuilder;
62 import org.openecomp.sdc.vendorlicense.errors.VendorLicenseModelNotFoundErrorBuilder;
63 import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacade;
64 import org.openecomp.sdc.versioning.ItemManagerFactory;
65 import org.openecomp.sdc.versioning.VersioningUtil;
66 import org.openecomp.sdc.versioning.dao.types.Version;
67 import org.openecomp.sdc.versioning.dao.types.VersionStatus;
68 import org.openecomp.sdc.versioning.errors.VersionableSubEntityNotFoundErrorBuilder;
69 import org.openecomp.sdc.versioning.types.Item;
70 import org.openecomp.sdc.versioning.types.ItemStatus;
71
72 public class VendorLicenseFacadeImpl implements VendorLicenseFacade {
73
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 final UniqueValueUtil uniqueValueUtil = new UniqueValueUtil
86       (UniqueValueDaoFactory.getInstance().createInterface());
87   private static final ErrorCode USED_VLM_NOT_EXIST_ERROR =
88           new ErrorCode.ErrorCodeBuilder().withCategory(ErrorCategory.APPLICATION).withId(FIELD_VALIDATION_ERROR_ERR_ID)
89                   .withMessage("The supplied vendor does not exist").build();
90   private static final ErrorCode USED_VLM_ARCHIVE_ERROR =
91           new ErrorCode.ErrorCodeBuilder().withCategory(ErrorCategory.APPLICATION).withId(FIELD_VALIDATION_ERROR_ERR_ID)
92                   .withMessage("The supplied vendor is archived and therefore cannot be used").build();
93   private static final ErrorCode USED_VLM_IS_DRAFT_ERROR =
94           new ErrorCode.ErrorCodeBuilder().withCategory(ErrorCategory.APPLICATION).withId(FIELD_VALIDATION_ERROR_ERR_ID)
95                                           .withMessage("The supplied vendor version is draft and therefor can not be used").build();
96
97
98   /**
99    * Instantiates a new Vendor license facade.
100    */
101   public VendorLicenseFacadeImpl() {
102     vendorLicenseModelDao.registerVersioning(VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE);
103     licenseAgreementDao.registerVersioning(VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE);
104     featureGroupDao.registerVersioning(VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE);
105     entitlementPoolDao.registerVersioning(VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE);
106     licenseKeyGroupDao.registerVersioning(VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE);
107     limitDao.registerVersioning(VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE);
108   }
109
110   @Override
111   public FeatureGroupEntity getFeatureGroup(FeatureGroupEntity featureGroup) {
112     FeatureGroupEntity retrieved = featureGroupDao.get(featureGroup);
113     VersioningUtil
114         .validateEntityExistence(retrieved, featureGroup, VendorLicenseModelEntity.ENTITY_TYPE);
115
116     return retrieved;
117   }
118
119   @Override
120   public FeatureGroupModel getFeatureGroupModel(FeatureGroupEntity featureGroup) {
121     FeatureGroupEntity retrieved = getFeatureGroup(featureGroup);
122
123     FeatureGroupModel featureGroupModel = new FeatureGroupModel();
124     featureGroupModel.setFeatureGroup(retrieved);
125
126     for (String licenseKeyGroupId : retrieved.getLicenseKeyGroupIds()) {
127       featureGroupModel.getLicenseKeyGroups().add(licenseKeyGroupDao.get(
128           new LicenseKeyGroupEntity(retrieved.getVendorLicenseModelId(), retrieved.getVersion(),
129               licenseKeyGroupId)));
130     }
131     for (String entitlementPoolId : retrieved.getEntitlementPoolIds()) {
132       featureGroupModel.getEntitlementPools().add(entitlementPoolDao.get(
133           new EntitlementPoolEntity(retrieved.getVendorLicenseModelId(), retrieved.getVersion(),
134               entitlementPoolId)));
135     }
136
137     return featureGroupModel;
138   }
139
140   @Override
141   public LicenseAgreementModel getLicenseAgreementModel(String vlmId, Version version,
142                                                         String licenseAgreementId) {
143     LicenseAgreementEntity retrieved =
144         getLicenseAgreement(vlmId, version, licenseAgreementId);
145
146     LicenseAgreementModel licenseAgreementModel = new LicenseAgreementModel();
147     licenseAgreementModel.setLicenseAgreement(retrieved);
148
149     for (String featureGroupId : retrieved.getFeatureGroupIds()) {
150       licenseAgreementModel.getFeatureGroups().add(featureGroupDao
151           .get(new FeatureGroupEntity(vlmId, retrieved.getVersion(), featureGroupId)));
152     }
153
154     return licenseAgreementModel;
155   }
156
157   @Override
158   public EntitlementPoolEntity createEntitlementPool(EntitlementPoolEntity entitlementPool) {
159     entitlementPool.setVersionUuId(CommonMethods.nextUuId());
160     uniqueValueUtil.createUniqueValue(VendorLicenseConstants.UniqueValues.ENTITLEMENT_POOL_NAME,
161         entitlementPool.getVendorLicenseModelId(), entitlementPool.getVersion().getId(),
162         entitlementPool.getName());
163     entitlementPoolDao.create(entitlementPool);
164     return entitlementPool;
165   }
166
167   @Override
168   public void updateEntitlementPool(EntitlementPoolEntity entitlementPool) {
169     EntitlementPoolEntity retrieved = entitlementPoolDao.get(entitlementPool);
170     VersioningUtil
171         .validateEntityExistence(retrieved, entitlementPool, VendorLicenseModelEntity.ENTITY_TYPE);
172     if (retrieved.equals(entitlementPool)) {
173       return;
174     }
175     uniqueValueUtil.updateUniqueValue(VendorLicenseConstants.UniqueValues.ENTITLEMENT_POOL_NAME,
176         retrieved.getName(), entitlementPool.getName(), entitlementPool.getVendorLicenseModelId(),
177         entitlementPool.getVersion().getId());
178     entitlementPool.setVersionUuId(CommonMethods.nextUuId());
179     entitlementPoolDao.update(entitlementPool);
180   }
181
182   @Override
183   public Collection<LicenseKeyGroupEntity> listLicenseKeyGroups(String vlmId, Version version) {
184     return licenseKeyGroupDao.list(new LicenseKeyGroupEntity(vlmId, version, null));
185   }
186
187   @Override
188   public Collection<EntitlementPoolEntity> listEntitlementPools(String vlmId, Version version) {
189     return entitlementPoolDao.list(new EntitlementPoolEntity(vlmId, version, null));
190   }
191
192   @Override
193   public void updateLicenseKeyGroup(LicenseKeyGroupEntity licenseKeyGroup) {
194     LicenseKeyGroupEntity retrieved = licenseKeyGroupDao.get(licenseKeyGroup);
195     if (retrieved.equals(licenseKeyGroup)) {
196       return;
197     }
198     licenseKeyGroup.setVersionUuId((CommonMethods.nextUuId()));
199     VersioningUtil
200         .validateEntityExistence(retrieved, licenseKeyGroup, VendorLicenseModelEntity.ENTITY_TYPE);
201     uniqueValueUtil.updateUniqueValue(VendorLicenseConstants.UniqueValues.LICENSE_KEY_GROUP_NAME,
202         retrieved.getName(), licenseKeyGroup.getName(), licenseKeyGroup.getVendorLicenseModelId(),
203         licenseKeyGroup.getVersion().getId());
204     licenseKeyGroupDao.update(licenseKeyGroup);
205   }
206
207   @Override
208   public LicenseKeyGroupEntity createLicenseKeyGroup(LicenseKeyGroupEntity licenseKeyGroup) {
209     licenseKeyGroup.setVersionUuId(CommonMethods.nextUuId());
210     uniqueValueUtil.createUniqueValue(VendorLicenseConstants.UniqueValues.LICENSE_KEY_GROUP_NAME,
211         licenseKeyGroup.getVendorLicenseModelId(), licenseKeyGroup.getVersion().getId(),
212         licenseKeyGroup.getName());
213     licenseKeyGroupDao.create(licenseKeyGroup);
214     return licenseKeyGroup;
215   }
216
217   @Override
218   public VendorLicenseModelEntity getVendorLicenseModel(String vlmId, Version version) {
219     VendorLicenseModelEntity vendorLicenseModel =
220         vendorLicenseModelDao.get(new VendorLicenseModelEntity(vlmId, version));
221     if (vendorLicenseModel == null) {
222       throw new CoreException(new VendorLicenseModelNotFoundErrorBuilder(vlmId).build());
223     }
224     return vendorLicenseModel;
225   }
226
227   @Override
228   public LicenseAgreementEntity createLicenseAgreement(LicenseAgreementEntity licenseAgreement) {
229     VersioningUtil.validateEntitiesExistence(licenseAgreement.getFeatureGroupIds(),
230         new FeatureGroupEntity(licenseAgreement.getVendorLicenseModelId(),
231             licenseAgreement.getVersion(),
232             null),
233         featureGroupDao, VendorLicenseModelEntity.ENTITY_TYPE);
234
235     uniqueValueUtil.validateUniqueValue(VendorLicenseConstants.UniqueValues.LICENSE_AGREEMENT_NAME,
236         licenseAgreement.getVendorLicenseModelId(), licenseAgreement.getVersion().getId(),
237         licenseAgreement.getName());
238
239     licenseAgreementDao.create(licenseAgreement);
240     uniqueValueUtil.createUniqueValue(VendorLicenseConstants.UniqueValues.LICENSE_AGREEMENT_NAME,
241         licenseAgreement.getVendorLicenseModelId(), licenseAgreement.getVersion().getId(),
242         licenseAgreement.getName());
243     if (licenseAgreement.getFeatureGroupIds() != null) {
244       for (String addedFgId : licenseAgreement.getFeatureGroupIds()) {
245         featureGroupDao.addReferencingLicenseAgreement(
246             new FeatureGroupEntity(licenseAgreement.getVendorLicenseModelId(),
247                 licenseAgreement.getVersion(),
248                 addedFgId), licenseAgreement.getId());
249       }
250     }
251     return licenseAgreement;
252   }
253
254   @Override
255   public FeatureGroupEntity createFeatureGroup(FeatureGroupEntity featureGroup) {
256     VersioningUtil.validateEntitiesExistence(featureGroup.getLicenseKeyGroupIds(),
257         new LicenseKeyGroupEntity(featureGroup.getVendorLicenseModelId(), featureGroup.getVersion(),
258             null),
259         licenseKeyGroupDao, VendorLicenseModelEntity.ENTITY_TYPE);
260     VersioningUtil.validateEntitiesExistence(featureGroup.getEntitlementPoolIds(),
261         new EntitlementPoolEntity(featureGroup.getVendorLicenseModelId(), featureGroup.getVersion(),
262             null),
263         entitlementPoolDao, VendorLicenseModelEntity.ENTITY_TYPE);
264     uniqueValueUtil.validateUniqueValue(VendorLicenseConstants.UniqueValues.FEATURE_GROUP_NAME,
265         featureGroup.getVendorLicenseModelId(), featureGroup.getVersion().getId(),
266         featureGroup.getName());
267
268     featureGroupDao.create(featureGroup);
269     uniqueValueUtil.createUniqueValue(VendorLicenseConstants.UniqueValues.FEATURE_GROUP_NAME,
270         featureGroup.getVendorLicenseModelId(), featureGroup.getVersion().getId(),
271         featureGroup.getName());
272
273     if (featureGroup.getLicenseKeyGroupIds() != null) {
274       for (String addedLkgId : featureGroup.getLicenseKeyGroupIds()) {
275         licenseKeyGroupDao.addReferencingFeatureGroup(
276             new LicenseKeyGroupEntity(featureGroup.getVendorLicenseModelId(),
277                 featureGroup.getVersion(), addedLkgId),
278             featureGroup.getId());
279       }
280     }
281
282     if (featureGroup.getEntitlementPoolIds() != null) {
283       for (String addedEpId : featureGroup.getEntitlementPoolIds()) {
284         entitlementPoolDao.addReferencingFeatureGroup(
285             new EntitlementPoolEntity(featureGroup.getVendorLicenseModelId(),
286                 featureGroup.getVersion(), addedEpId), featureGroup.getId());
287       }
288     }
289     return featureGroup;
290   }
291
292   @Override
293   public Collection<FeatureGroupEntity> listFeatureGroups(String vlmId, Version version) {
294     return featureGroupDao.list(new FeatureGroupEntity(vlmId, version, null));
295   }
296
297
298   @Override
299   public Collection<ErrorCode> validateLicensingData(String vlmId, Version version,
300                                                      String licenseAgreementId,
301                                                      Collection<String> featureGroupIds) {
302
303     List<ErrorCode> errorMessages = new ArrayList<>();
304
305     try {
306       getLicenseAgreement(vlmId, version, licenseAgreementId);
307     } catch (CoreException exception) {
308       errorMessages.add(exception.code());
309     }
310
311     for (String featureGroupId : featureGroupIds) {
312       try {
313         FeatureGroupEntity featureGroup =
314             getFeatureGroup(new FeatureGroupEntity(vlmId, version, featureGroupId));
315         if (!featureGroup.getReferencingLicenseAgreements().contains(licenseAgreementId)) {
316           errorMessages.add(new VersionableSubEntityNotFoundErrorBuilder(
317               featureGroup.getEntityType(),
318               featureGroupId,
319               LicenseAgreementEntity.ENTITY_TYPE,
320               licenseAgreementId,
321               version).build());
322         }
323       } catch (CoreException exception) {
324         errorMessages.add(exception.code());
325       }
326     }
327
328     return errorMessages;
329   }
330
331   @Override
332   public Optional<ErrorCode> validateVendorForUsage(String vlmId, Version version) {
333     Item vlm = ItemManagerFactory.getInstance().createInterface().get(vlmId);
334     if(vlm == null) {
335       return Optional.of(USED_VLM_NOT_EXIST_ERROR);
336     }
337
338     if (ItemStatus.ARCHIVED == vlm.getStatus()){
339       return  Optional.of(USED_VLM_ARCHIVE_ERROR);
340     }
341
342    if(VersionStatus.Draft.equals(version.getStatus())){
343      return Optional.of(USED_VLM_IS_DRAFT_ERROR);
344     }
345
346     return Optional.empty();
347   }
348
349   @Override
350   public LicenseAgreementEntity getLicenseAgreement(String vlmId, Version version,
351                                                     String licenseAgreementId) {
352     LicenseAgreementEntity input = new LicenseAgreementEntity(vlmId, version, licenseAgreementId);
353     LicenseAgreementEntity retrieved = licenseAgreementDao.get(input);
354     VersioningUtil.validateEntityExistence(retrieved, input, VendorLicenseModelEntity.ENTITY_TYPE);
355     return retrieved;
356   }
357
358   @Override
359   public Collection<LimitEntity> listLimits(String vlmId, Version version, String epLkgId) {
360     return limitDao.list(new LimitEntity(vlmId, version, epLkgId, null));
361   }
362
363   @Override
364   public LimitEntity createLimit(LimitEntity limit) {
365     limitDao.create(limit);
366     return limit;
367   }
368
369   @Override
370   public void updateLimit(LimitEntity limit) {
371     limitDao.update(limit);
372   }
373
374   @Override
375   public void validate(String vendorLicenseModelId, Version version) {
376     Collection<String> allFeatureGroupEntities = new HashSet<>();
377     Collection<LicenseAgreementEntity> licenseAgreements = licenseAgreementDao
378         .list(new LicenseAgreementEntity(vendorLicenseModelId, version, null));
379
380     if (CollectionUtils.isNotEmpty(licenseAgreements)) {
381       licenseAgreements.forEach(licenseAgreement -> {
382         if (CollectionUtils.isEmpty(licenseAgreement.getFeatureGroupIds())) {
383           throw new CoreException(
384               new SubmitUncompletedLicenseModelErrorBuilder(
385                   SUBMIT_UNCOMPLETED_VLM_MSG_LA_MISSING_FG).build());
386         }
387         allFeatureGroupEntities.addAll(licenseAgreement.getFeatureGroupIds());
388       });
389
390       allFeatureGroupEntities.forEach(fg -> {
391         FeatureGroupEntity featureGroupEntity =
392             featureGroupDao.get(new FeatureGroupEntity(vendorLicenseModelId, version, fg));
393         if (CollectionUtils.isEmpty(featureGroupEntity.getEntitlementPoolIds())) {
394           throw new CoreException(
395               new SubmitUncompletedLicenseModelErrorBuilder(
396                   SUBMIT_UNCOMPLETED_VLM_MSG_FG_MISSING_EP).build());
397         }
398       });
399     }
400   }
401
402
403 }