33e3b4c545e0249c1beaf4c34ed42f9354236303
[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.datatypes.error.ErrorLevel;
29 import org.openecomp.sdc.logging.context.impl.MdcDataErrorMessage;
30 import org.openecomp.sdc.logging.types.LoggerConstants;
31 import org.openecomp.sdc.logging.types.LoggerErrorCode;
32 import org.openecomp.sdc.logging.types.LoggerErrorDescription;
33 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
34 import org.openecomp.sdc.vendorlicense.VendorLicenseConstants;
35 import org.openecomp.sdc.vendorlicense.dao.EntitlementPoolDao;
36 import org.openecomp.sdc.vendorlicense.dao.EntitlementPoolDaoFactory;
37 import org.openecomp.sdc.vendorlicense.dao.FeatureGroupDao;
38 import org.openecomp.sdc.vendorlicense.dao.FeatureGroupDaoFactory;
39 import org.openecomp.sdc.vendorlicense.dao.LicenseAgreementDao;
40 import org.openecomp.sdc.vendorlicense.dao.LicenseAgreementDaoFactory;
41 import org.openecomp.sdc.vendorlicense.dao.LicenseKeyGroupDao;
42 import org.openecomp.sdc.vendorlicense.dao.LicenseKeyGroupDaoFactory;
43 import org.openecomp.sdc.vendorlicense.dao.LimitDao;
44 import org.openecomp.sdc.vendorlicense.dao.LimitDaoFactory;
45 import org.openecomp.sdc.vendorlicense.dao.VendorLicenseModelDao;
46 import org.openecomp.sdc.vendorlicense.dao.VendorLicenseModelDaoFactory;
47 import org.openecomp.sdc.vendorlicense.dao.types.EntitlementPoolEntity;
48 import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity;
49 import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupModel;
50 import org.openecomp.sdc.vendorlicense.dao.types.LicenseAgreementEntity;
51 import org.openecomp.sdc.vendorlicense.dao.types.LicenseAgreementModel;
52 import org.openecomp.sdc.vendorlicense.dao.types.LicenseKeyGroupEntity;
53 import org.openecomp.sdc.vendorlicense.dao.types.LimitEntity;
54 import org.openecomp.sdc.vendorlicense.dao.types.VendorLicenseModelEntity;
55 import org.openecomp.sdc.vendorlicense.errors.SubmitUncompletedLicenseModelErrorBuilder;
56 import org.openecomp.sdc.vendorlicense.errors.VendorLicenseModelNotFoundErrorBuilder;
57 import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacade;
58 import org.openecomp.sdc.versioning.VersioningUtil;
59 import org.openecomp.sdc.versioning.dao.types.Version;
60 import org.openecomp.sdc.versioning.errors.VersionableSubEntityNotFoundErrorBuilder;
61
62 import java.util.ArrayList;
63 import java.util.Collection;
64 import java.util.HashSet;
65 import java.util.List;
66 import java.util.Objects;
67
68 import static org.openecomp.sdc.vendorlicense.VendorLicenseConstants.VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE;
69 import static org.openecomp.sdc.vendorlicense.errors.UncompletedVendorLicenseModelErrorType.SUBMIT_UNCOMPLETED_VLM_MSG_FG_MISSING_EP;
70 import static org.openecomp.sdc.vendorlicense.errors.UncompletedVendorLicenseModelErrorType.SUBMIT_UNCOMPLETED_VLM_MSG_LA_MISSING_FG;
71
72 public class VendorLicenseFacadeImpl implements VendorLicenseFacade {
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   /**
85    * Instantiates a new Vendor license facade.
86    */
87   public VendorLicenseFacadeImpl() {
88     vendorLicenseModelDao.registerVersioning(VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE);
89     licenseAgreementDao.registerVersioning(VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE);
90     featureGroupDao.registerVersioning(VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE);
91     entitlementPoolDao.registerVersioning(VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE);
92     licenseKeyGroupDao.registerVersioning(VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE);
93     limitDao.registerVersioning(VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE);
94   }
95
96   @Override
97   public FeatureGroupEntity getFeatureGroup(FeatureGroupEntity featureGroup) {
98     FeatureGroupEntity retrieved = featureGroupDao.get(featureGroup);
99     VersioningUtil
100         .validateEntityExistence(retrieved, featureGroup, VendorLicenseModelEntity.ENTITY_TYPE);
101     if (retrieved.getManufacturerReferenceNumber() == null) {
102       updateManufacturerNumberInFeatureGroup(retrieved);
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       MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
211           LoggerTragetServiceName.GET_VLM, ErrorLevel.ERROR.name(),
212           LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.ENTITY_NOT_FOUND);
213       throw new CoreException(new VendorLicenseModelNotFoundErrorBuilder(vlmId).build());
214     }
215     return vendorLicenseModel;
216   }
217
218   @Override
219   public LicenseAgreementEntity createLicenseAgreement(LicenseAgreementEntity licenseAgreement) {
220     //licenseAgreement.setId(CommonMethods.nextUuId());
221     VersioningUtil.validateEntitiesExistence(licenseAgreement.getFeatureGroupIds(),
222         new FeatureGroupEntity(licenseAgreement.getVendorLicenseModelId(),
223             licenseAgreement.getVersion(),
224             null),
225         featureGroupDao, VendorLicenseModelEntity.ENTITY_TYPE);
226
227     UniqueValueUtil.validateUniqueValue(VendorLicenseConstants.UniqueValues.LICENSE_AGREEMENT_NAME,
228         licenseAgreement.getVendorLicenseModelId(), licenseAgreement.getVersion().getId(),
229         licenseAgreement.getName());
230
231     licenseAgreementDao.create(licenseAgreement);
232     UniqueValueUtil.createUniqueValue(VendorLicenseConstants.UniqueValues.LICENSE_AGREEMENT_NAME,
233         licenseAgreement.getVendorLicenseModelId(), licenseAgreement.getVersion().getId(),
234         licenseAgreement.getName());
235     if (licenseAgreement.getFeatureGroupIds() != null) {
236       for (String addedFgId : licenseAgreement.getFeatureGroupIds()) {
237         featureGroupDao.addReferencingLicenseAgreement(
238             new FeatureGroupEntity(licenseAgreement.getVendorLicenseModelId(),
239                 licenseAgreement.getVersion(),
240                 addedFgId), licenseAgreement.getId());
241       }
242     }
243     return licenseAgreement;
244   }
245
246   @Override
247   public FeatureGroupEntity createFeatureGroup(FeatureGroupEntity featureGroup) {
248     VersioningUtil.validateEntitiesExistence(featureGroup.getLicenseKeyGroupIds(),
249         new LicenseKeyGroupEntity(featureGroup.getVendorLicenseModelId(), featureGroup.getVersion(),
250             null),
251         licenseKeyGroupDao, VendorLicenseModelEntity.ENTITY_TYPE);
252     VersioningUtil.validateEntitiesExistence(featureGroup.getEntitlementPoolIds(),
253         new EntitlementPoolEntity(featureGroup.getVendorLicenseModelId(), featureGroup.getVersion(),
254             null),
255         entitlementPoolDao, VendorLicenseModelEntity.ENTITY_TYPE);
256     UniqueValueUtil.validateUniqueValue(VendorLicenseConstants.UniqueValues.FEATURE_GROUP_NAME,
257         featureGroup.getVendorLicenseModelId(), featureGroup.getVersion().getId(),
258         featureGroup.getName());
259
260     featureGroupDao.create(featureGroup);
261     UniqueValueUtil.createUniqueValue(VendorLicenseConstants.UniqueValues.FEATURE_GROUP_NAME,
262         featureGroup.getVendorLicenseModelId(), featureGroup.getVersion().getId(),
263         featureGroup.getName());
264
265     if (featureGroup.getLicenseKeyGroupIds() != null) {
266       for (String addedLkgId : featureGroup.getLicenseKeyGroupIds()) {
267         licenseKeyGroupDao.addReferencingFeatureGroup(
268             new LicenseKeyGroupEntity(featureGroup.getVendorLicenseModelId(),
269                 featureGroup.getVersion(), addedLkgId),
270             featureGroup.getId());
271       }
272     }
273
274     if (featureGroup.getEntitlementPoolIds() != null) {
275       for (String addedEpId : featureGroup.getEntitlementPoolIds()) {
276         entitlementPoolDao.addReferencingFeatureGroup(
277             new EntitlementPoolEntity(featureGroup.getVendorLicenseModelId(),
278                 featureGroup.getVersion(), addedEpId), featureGroup.getId());
279       }
280     }
281     return featureGroup;
282   }
283
284   @Override
285   public Collection<FeatureGroupEntity> listFeatureGroups(String vlmId, Version version) {
286     Collection<FeatureGroupEntity> featureGroupEntities =
287         featureGroupDao.list(new FeatureGroupEntity(vlmId, version, null));
288     featureGroupEntities.stream()
289         .filter(fgEntity -> Objects.isNull(fgEntity.getManufacturerReferenceNumber()))
290         .forEach(this::updateManufacturerNumberInFeatureGroup);
291     return featureGroupEntities;
292   }
293
294
295   @Override
296   public Collection<ErrorCode> validateLicensingData(String vlmId, Version version,
297                                                      String licenseAgreementId,
298                                                      Collection<String> featureGroupIds) {
299     // TODO: 5/21/2017 validate version exists and final
300 /*    try {
301       VersionInfo versionInfo = getVersionInfo(vlmId, VersionableEntityAction.Read, "");
302       if (version == null || !version.isFinal()
303           || !versionInfo.getViewableVersions().contains(version)) {
304         return Collections.singletonList(new RequestedVersionInvalidErrorBuilder().build());
305       }
306     } catch (CoreException exception) {
307       return Collections.singletonList(exception.code());
308     }*/
309
310     List<ErrorCode> errorMessages = new ArrayList<>();
311
312     try {
313       getLicenseAgreement(vlmId, version, licenseAgreementId);
314     } catch (CoreException exception) {
315       errorMessages.add(exception.code());
316     }
317
318     for (String featureGroupId : featureGroupIds) {
319       try {
320         FeatureGroupEntity featureGroup =
321             getFeatureGroup(new FeatureGroupEntity(vlmId, version, featureGroupId));
322         if (!featureGroup.getReferencingLicenseAgreements().contains(licenseAgreementId)) {
323           errorMessages.add(new VersionableSubEntityNotFoundErrorBuilder(
324               featureGroup.getEntityType(),
325               featureGroupId,
326               LicenseAgreementEntity.ENTITY_TYPE,
327               licenseAgreementId,
328               version).build());
329         }
330       } catch (CoreException exception) {
331         errorMessages.add(exception.code());
332       }
333     }
334
335     return errorMessages;
336   }
337
338   @Override
339   public LicenseAgreementEntity getLicenseAgreement(String vlmId, Version version,
340                                                     String licenseAgreementId) {
341     LicenseAgreementEntity input = new LicenseAgreementEntity(vlmId, version, licenseAgreementId);
342     LicenseAgreementEntity retrieved = licenseAgreementDao.get(input);
343     VersioningUtil.validateEntityExistence(retrieved, input, VendorLicenseModelEntity.ENTITY_TYPE);
344     return retrieved;
345   }
346
347   @Override
348   public Collection<LimitEntity> listLimits(String vlmId, Version version, String epLkgId) {
349     return limitDao.list(new LimitEntity(vlmId, version, epLkgId, null));
350   }
351
352   @Override
353   public LimitEntity createLimit(LimitEntity limit) {
354     limitDao.create(limit);
355     return limit;
356   }
357
358   @Override
359   public void updateLimit(LimitEntity limit) {
360     limitDao.update(limit);
361   }
362
363   private void updateManufacturerNumberInFeatureGroup(FeatureGroupEntity featureGroupEntity) {
364     if (CollectionUtils.isNotEmpty(featureGroupEntity.getEntitlementPoolIds())) {
365       Object[] entitlementPoolIdsList = featureGroupEntity.getEntitlementPoolIds().toArray();
366       if (entitlementPoolIdsList.length > 0) {
367         String entitlementPoolId = entitlementPoolIdsList[0].toString();
368         EntitlementPoolEntity entitlementPoolEntity =
369             new EntitlementPoolEntity(featureGroupEntity.getVendorLicenseModelId(),
370                 featureGroupEntity.getVersion(), entitlementPoolId);
371         entitlementPoolEntity = entitlementPoolDao.get(entitlementPoolEntity);
372         featureGroupEntity.setManufacturerReferenceNumber(
373             entitlementPoolDao.getManufacturerReferenceNumber(entitlementPoolEntity));
374         featureGroupDao.update(featureGroupEntity);
375       }
376     }
377   }
378
379   @Override
380   public void validate(String vendorLicenseModelId, Version version) {
381     Collection<String> allFeatureGroupEntities = new HashSet<>();
382     Collection<LicenseAgreementEntity> licenseAgreements = licenseAgreementDao
383         .list(new LicenseAgreementEntity(vendorLicenseModelId, version, null));
384
385     if (CollectionUtils.isNotEmpty(licenseAgreements)) {
386       licenseAgreements.forEach(licenseAgreement -> {
387         if (CollectionUtils.isEmpty(licenseAgreement.getFeatureGroupIds())) {
388           MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
389               LoggerTragetServiceName.SUBMIT_ENTITY, ErrorLevel.ERROR.name(),
390               LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.SUBMIT_ENTITY);
391           throw new CoreException(
392               new SubmitUncompletedLicenseModelErrorBuilder(
393                   SUBMIT_UNCOMPLETED_VLM_MSG_LA_MISSING_FG).build());
394         }
395         allFeatureGroupEntities.addAll(licenseAgreement.getFeatureGroupIds());
396       });
397
398       allFeatureGroupEntities.forEach(fg -> {
399         FeatureGroupEntity featureGroupEntity =
400             featureGroupDao.get(new FeatureGroupEntity(vendorLicenseModelId, version, fg));
401         if (CollectionUtils.isEmpty(featureGroupEntity.getEntitlementPoolIds())) {
402           MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
403               LoggerTragetServiceName.SUBMIT_ENTITY, ErrorLevel.ERROR.name(),
404               LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.SUBMIT_ENTITY);
405           throw new CoreException(
406               new SubmitUncompletedLicenseModelErrorBuilder(
407                   SUBMIT_UNCOMPLETED_VLM_MSG_FG_MISSING_EP).build());
408         }
409       });
410     }
411   }
412
413
414 }