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