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