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