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