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