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