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