aboutsummaryrefslogtreecommitdiffstats
path: root/openecomp-be/lib/openecomp-sdc-vendor-license-lib/openecomp-sdc-vendor-license-core/src/main/java/org/openecomp/sdc/vendorlicense/facade/impl/VendorLicenseFacadeImpl.java
blob: 576cb639c7987267f443b362d2a7658fd3719ee4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.sdc.vendorlicense.facade.impl;

import org.apache.commons.collections4.CollectionUtils;
import org.openecomp.core.dao.UniqueValueDaoFactory;
import org.openecomp.core.util.UniqueValueUtil;
import org.openecomp.core.utilities.CommonMethods;
import org.openecomp.sdc.common.errors.CoreException;
import org.openecomp.sdc.common.errors.ErrorCode;
import org.openecomp.sdc.common.togglz.ToggleableFeature;
import org.openecomp.sdc.vendorlicense.VendorLicenseConstants;
import org.openecomp.sdc.vendorlicense.dao.EntitlementPoolDao;
import org.openecomp.sdc.vendorlicense.dao.EntitlementPoolDaoFactory;
import org.openecomp.sdc.vendorlicense.dao.FeatureGroupDao;
import org.openecomp.sdc.vendorlicense.dao.FeatureGroupDaoFactory;
import org.openecomp.sdc.vendorlicense.dao.LicenseAgreementDao;
import org.openecomp.sdc.vendorlicense.dao.LicenseAgreementDaoFactory;
import org.openecomp.sdc.vendorlicense.dao.LicenseKeyGroupDao;
import org.openecomp.sdc.vendorlicense.dao.LicenseKeyGroupDaoFactory;
import org.openecomp.sdc.vendorlicense.dao.LimitDao;
import org.openecomp.sdc.vendorlicense.dao.LimitDaoFactory;
import org.openecomp.sdc.vendorlicense.dao.VendorLicenseModelDao;
import org.openecomp.sdc.vendorlicense.dao.VendorLicenseModelDaoFactory;
import org.openecomp.sdc.vendorlicense.dao.types.EntitlementPoolEntity;
import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity;
import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupModel;
import org.openecomp.sdc.vendorlicense.dao.types.LicenseAgreementEntity;
import org.openecomp.sdc.vendorlicense.dao.types.LicenseAgreementModel;
import org.openecomp.sdc.vendorlicense.dao.types.LicenseKeyGroupEntity;
import org.openecomp.sdc.vendorlicense.dao.types.LimitEntity;
import org.openecomp.sdc.vendorlicense.dao.types.VendorLicenseModelEntity;
import org.openecomp.sdc.vendorlicense.errors.SubmitUncompletedLicenseModelErrorBuilder;
import org.openecomp.sdc.vendorlicense.errors.VendorLicenseModelNotFoundErrorBuilder;
import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacade;
import org.openecomp.sdc.versioning.VersioningUtil;
import org.openecomp.sdc.versioning.dao.types.Version;
import org.openecomp.sdc.versioning.errors.VersionableSubEntityNotFoundErrorBuilder;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;

import static org.openecomp.sdc.vendorlicense.VendorLicenseConstants.VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE;
import static org.openecomp.sdc.vendorlicense.errors.UncompletedVendorLicenseModelErrorType.SUBMIT_UNCOMPLETED_VLM_MSG_FG_MISSING_EP;
import static org.openecomp.sdc.vendorlicense.errors.UncompletedVendorLicenseModelErrorType.SUBMIT_UNCOMPLETED_VLM_MSG_LA_MISSING_FG;

public class VendorLicenseFacadeImpl implements VendorLicenseFacade {
  private static final VendorLicenseModelDao
      vendorLicenseModelDao = VendorLicenseModelDaoFactory.getInstance().createInterface();
  private static final LicenseAgreementDao
      licenseAgreementDao = LicenseAgreementDaoFactory.getInstance().createInterface();
  private static final FeatureGroupDao featureGroupDao =
      FeatureGroupDaoFactory.getInstance().createInterface();
  private static final EntitlementPoolDao
      entitlementPoolDao = EntitlementPoolDaoFactory.getInstance().createInterface();
  private static final LicenseKeyGroupDao
      licenseKeyGroupDao = LicenseKeyGroupDaoFactory.getInstance().createInterface();
  private static final LimitDao limitDao = LimitDaoFactory.getInstance().createInterface();
  private static final UniqueValueUtil uniqueValueUtil = new UniqueValueUtil
      (UniqueValueDaoFactory.getInstance().createInterface());
  /**
   * Instantiates a new Vendor license facade.
   */
  public VendorLicenseFacadeImpl() {
    vendorLicenseModelDao.registerVersioning(VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE);
    licenseAgreementDao.registerVersioning(VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE);
    featureGroupDao.registerVersioning(VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE);
    entitlementPoolDao.registerVersioning(VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE);
    licenseKeyGroupDao.registerVersioning(VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE);
    limitDao.registerVersioning(VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE);
  }

  @Override
  public FeatureGroupEntity getFeatureGroup(FeatureGroupEntity featureGroup) {
    FeatureGroupEntity retrieved = featureGroupDao.get(featureGroup);
    VersioningUtil
        .validateEntityExistence(retrieved, featureGroup, VendorLicenseModelEntity.ENTITY_TYPE);
    if (!ToggleableFeature.MRN.isActive()) { //Remove updateManufacturerNumberInFeatureGroup() while removing remove toggle
      if (retrieved.getManufacturerReferenceNumber() == null) {
        updateManufacturerNumberInFeatureGroup(retrieved);
      }
    }
    return retrieved;
  }

  @Override
  public FeatureGroupModel getFeatureGroupModel(FeatureGroupEntity featureGroup) {
    FeatureGroupEntity retrieved = getFeatureGroup(featureGroup);

    FeatureGroupModel featureGroupModel = new FeatureGroupModel();
    featureGroupModel.setFeatureGroup(retrieved);

    for (String licenseKeyGroupId : retrieved.getLicenseKeyGroupIds()) {
      featureGroupModel.getLicenseKeyGroups().add(licenseKeyGroupDao.get(
          new LicenseKeyGroupEntity(retrieved.getVendorLicenseModelId(), retrieved.getVersion(),
              licenseKeyGroupId)));
    }
    for (String entitlementPoolId : retrieved.getEntitlementPoolIds()) {
      featureGroupModel.getEntitlementPools().add(entitlementPoolDao.get(
          new EntitlementPoolEntity(retrieved.getVendorLicenseModelId(), retrieved.getVersion(),
              entitlementPoolId)));
    }

    return featureGroupModel;
  }

  @Override
  public LicenseAgreementModel getLicenseAgreementModel(String vlmId, Version version,
                                                        String licenseAgreementId) {
    LicenseAgreementEntity retrieved =
        getLicenseAgreement(vlmId, version, licenseAgreementId);

    LicenseAgreementModel licenseAgreementModel = new LicenseAgreementModel();
    licenseAgreementModel.setLicenseAgreement(retrieved);

    for (String featureGroupId : retrieved.getFeatureGroupIds()) {
      licenseAgreementModel.getFeatureGroups().add(featureGroupDao
          .get(new FeatureGroupEntity(vlmId, retrieved.getVersion(), featureGroupId)));
    }

    return licenseAgreementModel;
  }

  @Override
  public EntitlementPoolEntity createEntitlementPool(EntitlementPoolEntity entitlementPool) {
    entitlementPool.setVersionUuId(CommonMethods.nextUuId());
    uniqueValueUtil.createUniqueValue(VendorLicenseConstants.UniqueValues.ENTITLEMENT_POOL_NAME,
        entitlementPool.getVendorLicenseModelId(), entitlementPool.getVersion().getId(),
        entitlementPool.getName());
    entitlementPoolDao.create(entitlementPool);
    return entitlementPool;
  }

  @Override
  public void updateEntitlementPool(EntitlementPoolEntity entitlementPool) {
    EntitlementPoolEntity retrieved = entitlementPoolDao.get(entitlementPool);
    VersioningUtil
        .validateEntityExistence(retrieved, entitlementPool, VendorLicenseModelEntity.ENTITY_TYPE);
    if (retrieved.equals(entitlementPool)) {
      return;
    }
    uniqueValueUtil.updateUniqueValue(VendorLicenseConstants.UniqueValues.ENTITLEMENT_POOL_NAME,
        retrieved.getName(), entitlementPool.getName(), entitlementPool.getVendorLicenseModelId(),
        entitlementPool.getVersion().getId());
    entitlementPool.setVersionUuId(CommonMethods.nextUuId());
    entitlementPoolDao.update(entitlementPool);
  }

  @Override
  public Collection<LicenseKeyGroupEntity> listLicenseKeyGroups(String vlmId, Version version) {
    return licenseKeyGroupDao.list(new LicenseKeyGroupEntity(vlmId, version, null));
  }

  @Override
  public Collection<EntitlementPoolEntity> listEntitlementPools(String vlmId, Version version) {
    return entitlementPoolDao.list(new EntitlementPoolEntity(vlmId, version, null));
  }

  @Override
  public void updateLicenseKeyGroup(LicenseKeyGroupEntity licenseKeyGroup) {
    LicenseKeyGroupEntity retrieved = licenseKeyGroupDao.get(licenseKeyGroup);
    if (retrieved.equals(licenseKeyGroup)) {
      return;
    }
    licenseKeyGroup.setVersionUuId((CommonMethods.nextUuId()));
    VersioningUtil
        .validateEntityExistence(retrieved, licenseKeyGroup, VendorLicenseModelEntity.ENTITY_TYPE);
    uniqueValueUtil.updateUniqueValue(VendorLicenseConstants.UniqueValues.LICENSE_KEY_GROUP_NAME,
        retrieved.getName(), licenseKeyGroup.getName(), licenseKeyGroup.getVendorLicenseModelId(),
        licenseKeyGroup.getVersion().getId());
    licenseKeyGroupDao.update(licenseKeyGroup);
  }

  @Override
  public LicenseKeyGroupEntity createLicenseKeyGroup(LicenseKeyGroupEntity licenseKeyGroup) {
    licenseKeyGroup.setVersionUuId(CommonMethods.nextUuId());
    uniqueValueUtil.createUniqueValue(VendorLicenseConstants.UniqueValues.LICENSE_KEY_GROUP_NAME,
        licenseKeyGroup.getVendorLicenseModelId(), licenseKeyGroup.getVersion().getId(),
        licenseKeyGroup.getName());
    licenseKeyGroupDao.create(licenseKeyGroup);
    return licenseKeyGroup;
  }

  @Override
  public VendorLicenseModelEntity getVendorLicenseModel(String vlmId, Version version) {
    VendorLicenseModelEntity vendorLicenseModel =
        vendorLicenseModelDao.get(new VendorLicenseModelEntity(vlmId, version));
    if (vendorLicenseModel == null) {
      throw new CoreException(new VendorLicenseModelNotFoundErrorBuilder(vlmId).build());
    }
    return vendorLicenseModel;
  }

  @Override
  public LicenseAgreementEntity createLicenseAgreement(LicenseAgreementEntity licenseAgreement) {
    VersioningUtil.validateEntitiesExistence(licenseAgreement.getFeatureGroupIds(),
        new FeatureGroupEntity(licenseAgreement.getVendorLicenseModelId(),
            licenseAgreement.getVersion(),
            null),
        featureGroupDao, VendorLicenseModelEntity.ENTITY_TYPE);

    uniqueValueUtil.validateUniqueValue(VendorLicenseConstants.UniqueValues.LICENSE_AGREEMENT_NAME,
        licenseAgreement.getVendorLicenseModelId(), licenseAgreement.getVersion().getId(),
        licenseAgreement.getName());

    licenseAgreementDao.create(licenseAgreement);
    uniqueValueUtil.createUniqueValue(VendorLicenseConstants.UniqueValues.LICENSE_AGREEMENT_NAME,
        licenseAgreement.getVendorLicenseModelId(), licenseAgreement.getVersion().getId(),
        licenseAgreement.getName());
    if (licenseAgreement.getFeatureGroupIds() != null) {
      for (String addedFgId : licenseAgreement.getFeatureGroupIds()) {
        featureGroupDao.addReferencingLicenseAgreement(
            new FeatureGroupEntity(licenseAgreement.getVendorLicenseModelId(),
                licenseAgreement.getVersion(),
                addedFgId), licenseAgreement.getId());
      }
    }
    return licenseAgreement;
  }

  @Override
  public FeatureGroupEntity createFeatureGroup(FeatureGroupEntity featureGroup) {
    VersioningUtil.validateEntitiesExistence(featureGroup.getLicenseKeyGroupIds(),
        new LicenseKeyGroupEntity(featureGroup.getVendorLicenseModelId(), featureGroup.getVersion(),
            null),
        licenseKeyGroupDao, VendorLicenseModelEntity.ENTITY_TYPE);
    VersioningUtil.validateEntitiesExistence(featureGroup.getEntitlementPoolIds(),
        new EntitlementPoolEntity(featureGroup.getVendorLicenseModelId(), featureGroup.getVersion(),
            null),
        entitlementPoolDao, VendorLicenseModelEntity.ENTITY_TYPE);
    uniqueValueUtil.validateUniqueValue(VendorLicenseConstants.UniqueValues.FEATURE_GROUP_NAME,
        featureGroup.getVendorLicenseModelId(), featureGroup.getVersion().getId(),
        featureGroup.getName());

    featureGroupDao.create(featureGroup);
    uniqueValueUtil.createUniqueValue(VendorLicenseConstants.UniqueValues.FEATURE_GROUP_NAME,
        featureGroup.getVendorLicenseModelId(), featureGroup.getVersion().getId(),
        featureGroup.getName());

    if (featureGroup.getLicenseKeyGroupIds() != null) {
      for (String addedLkgId : featureGroup.getLicenseKeyGroupIds()) {
        licenseKeyGroupDao.addReferencingFeatureGroup(
            new LicenseKeyGroupEntity(featureGroup.getVendorLicenseModelId(),
                featureGroup.getVersion(), addedLkgId),
            featureGroup.getId());
      }
    }

    if (featureGroup.getEntitlementPoolIds() != null) {
      for (String addedEpId : featureGroup.getEntitlementPoolIds()) {
        entitlementPoolDao.addReferencingFeatureGroup(
            new EntitlementPoolEntity(featureGroup.getVendorLicenseModelId(),
                featureGroup.getVersion(), addedEpId), featureGroup.getId());
      }
    }
    return featureGroup;
  }

  @Override
  public Collection<FeatureGroupEntity> listFeatureGroups(String vlmId, Version version) {
    Collection<FeatureGroupEntity> featureGroupEntities =
        featureGroupDao.list(new FeatureGroupEntity(vlmId, version, null));
    featureGroupEntities.stream()
        .filter(fgEntity -> Objects.isNull(fgEntity.getManufacturerReferenceNumber()))
        .forEach(this::updateManufacturerNumberInFeatureGroup);
    return featureGroupEntities;
  }


  @Override
  public Collection<ErrorCode> validateLicensingData(String vlmId, Version version,
                                                     String licenseAgreementId,
                                                     Collection<String> featureGroupIds) {

    List<ErrorCode> errorMessages = new ArrayList<>();

    try {
      getLicenseAgreement(vlmId, version, licenseAgreementId);
    } catch (CoreException exception) {
      errorMessages.add(exception.code());
    }

    for (String featureGroupId : featureGroupIds) {
      try {
        FeatureGroupEntity featureGroup =
            getFeatureGroup(new FeatureGroupEntity(vlmId, version, featureGroupId));
        if (!featureGroup.getReferencingLicenseAgreements().contains(licenseAgreementId)) {
          errorMessages.add(new VersionableSubEntityNotFoundErrorBuilder(
              featureGroup.getEntityType(),
              featureGroupId,
              LicenseAgreementEntity.ENTITY_TYPE,
              licenseAgreementId,
              version).build());
        }
      } catch (CoreException exception) {
        errorMessages.add(exception.code());
      }
    }

    return errorMessages;
  }

  @Override
  public LicenseAgreementEntity getLicenseAgreement(String vlmId, Version version,
                                                    String licenseAgreementId) {
    LicenseAgreementEntity input = new LicenseAgreementEntity(vlmId, version, licenseAgreementId);
    LicenseAgreementEntity retrieved = licenseAgreementDao.get(input);
    VersioningUtil.validateEntityExistence(retrieved, input, VendorLicenseModelEntity.ENTITY_TYPE);
    return retrieved;
  }

  @Override
  public Collection<LimitEntity> listLimits(String vlmId, Version version, String epLkgId) {
    return limitDao.list(new LimitEntity(vlmId, version, epLkgId, null));
  }

  @Override
  public LimitEntity createLimit(LimitEntity limit) {
    limitDao.create(limit);
    return limit;
  }

  @Override
  public void updateLimit(LimitEntity limit) {
    limitDao.update(limit);
  }

  private void updateManufacturerNumberInFeatureGroup(FeatureGroupEntity featureGroupEntity) {
    if (CollectionUtils.isNotEmpty(featureGroupEntity.getEntitlementPoolIds())) {
      Object[] entitlementPoolIdsList = featureGroupEntity.getEntitlementPoolIds().toArray();
      if (entitlementPoolIdsList.length > 0) {
        String entitlementPoolId = entitlementPoolIdsList[0].toString();
        EntitlementPoolEntity entitlementPoolEntity =
            new EntitlementPoolEntity(featureGroupEntity.getVendorLicenseModelId(),
                featureGroupEntity.getVersion(), entitlementPoolId);
        entitlementPoolEntity = entitlementPoolDao.get(entitlementPoolEntity);
        featureGroupEntity.setManufacturerReferenceNumber(
            entitlementPoolDao.getManufacturerReferenceNumber(entitlementPoolEntity));
        featureGroupDao.update(featureGroupEntity);
      }
    }
  }

  @Override
  public void validate(String vendorLicenseModelId, Version version) {
    Collection<String> allFeatureGroupEntities = new HashSet<>();
    Collection<LicenseAgreementEntity> licenseAgreements = licenseAgreementDao
        .list(new LicenseAgreementEntity(vendorLicenseModelId, version, null));

    if (CollectionUtils.isNotEmpty(licenseAgreements)) {
      licenseAgreements.forEach(licenseAgreement -> {
        if (CollectionUtils.isEmpty(licenseAgreement.getFeatureGroupIds())) {
          throw new CoreException(
              new SubmitUncompletedLicenseModelErrorBuilder(
                  SUBMIT_UNCOMPLETED_VLM_MSG_LA_MISSING_FG).build());
        }
        allFeatureGroupEntities.addAll(licenseAgreement.getFeatureGroupIds());
      });

      allFeatureGroupEntities.forEach(fg -> {
        FeatureGroupEntity featureGroupEntity =
            featureGroupDao.get(new FeatureGroupEntity(vendorLicenseModelId, version, fg));
        if (CollectionUtils.isEmpty(featureGroupEntity.getEntitlementPoolIds())) {
          throw new CoreException(
              new SubmitUncompletedLicenseModelErrorBuilder(
                  SUBMIT_UNCOMPLETED_VLM_MSG_FG_MISSING_EP).build());
        }
      });
    }
  }


}