2 * Copyright © 2016-2017 European Support Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.openecomp.sdc.vendorlicense.licenseartifacts.impl;
18 import static org.openecomp.sdc.vendorlicense.VendorLicenseConstants.VENDOR_LICENSE_MODEL_ARTIFACT_NAME_WITH_PATH;
19 import static org.openecomp.sdc.vendorlicense.VendorLicenseConstants.VNF_ARTIFACT_NAME_WITH_PATH;
20 import static org.openecomp.sdc.vendorlicense.licenseartifacts.impl.util.VendorLicenseArtifactsServiceUtils.filterChangedEntities;
21 import static org.openecomp.sdc.vendorlicense.licenseartifacts.impl.util.VendorLicenseArtifactsServiceUtils.getFinalVersionsForVlm;
22 import static org.openecomp.sdc.vendorlicense.licenseartifacts.impl.util.VendorLicenseArtifactsServiceUtils.getVendorName;
23 import static org.openecomp.sdc.vendorlicense.licenseartifacts.impl.util.VendorLicenseArtifactsServiceUtils.healEPs;
24 import static org.openecomp.sdc.vendorlicense.licenseartifacts.impl.util.VendorLicenseArtifactsServiceUtils.healLkgs;
25 import static org.openecomp.sdc.vendorlicense.licenseartifacts.impl.util.VendorLicenseArtifactsServiceUtils.prepareForFiltering;
27 import java.util.Collection;
28 import java.util.HashSet;
29 import java.util.List;
30 import java.util.Objects;
31 import java.util.Optional;
33 import java.util.stream.Collectors;
34 import org.apache.commons.collections.CollectionUtils;
35 import org.apache.commons.lang3.StringUtils;
36 import org.openecomp.core.utilities.file.FileContentHandler;
37 import org.openecomp.sdc.vendorlicense.HealingServiceFactory;
38 import org.openecomp.sdc.vendorlicense.dao.types.EntitlementPoolEntity;
39 import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity;
40 import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupModel;
41 import org.openecomp.sdc.vendorlicense.dao.types.LicenseKeyGroupEntity;
42 import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacade;
43 import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacadeFactory;
44 import org.openecomp.sdc.vendorlicense.healing.HealingService;
45 import org.openecomp.sdc.vendorlicense.licenseartifacts.VendorLicenseArtifactsService;
46 import org.openecomp.sdc.vendorlicense.licenseartifacts.impl.types.VendorLicenseArtifact;
47 import org.openecomp.sdc.vendorlicense.licenseartifacts.impl.types.VnfLicenseArtifact;
48 import org.openecomp.sdc.versioning.dao.types.Version;
50 public class VendorLicenseArtifactsServiceImpl implements VendorLicenseArtifactsService {
52 public static final VendorLicenseFacade vendorLicenseFacade = VendorLicenseFacadeFactory.getInstance().createInterface();
53 public static final HealingService healingService = HealingServiceFactory.getInstance().createInterface();
55 private static byte[] createVnfArtifact(String vspId, String vlmId, Version vlmVersion, String vendorName, List<String> featureGroups) {
56 VnfLicenseArtifact artifact = new VnfLicenseArtifact();
57 artifact.setVspId(vspId);
58 artifact.setVendorName(vendorName);
59 if (featureGroups != null) {
60 for (String featureGroupId : featureGroups) {
61 FeatureGroupModel featureGroupModel = vendorLicenseFacade
62 .getFeatureGroupModel(new FeatureGroupEntity(vlmId, vlmVersion, featureGroupId));
63 Set<EntitlementPoolEntity> entitlementPoolEntities = featureGroupModel.getEntitlementPools();
64 String manufacturerReferenceNumber = featureGroupModel.getEntityManufacturerReferenceNumber();
65 for (EntitlementPoolEntity entitlementPoolEntity : entitlementPoolEntities) {
66 entitlementPoolEntity.setLimits(vendorLicenseFacade.listLimits(vlmId, vlmVersion, entitlementPoolEntity.getId()));
67 if (Objects.nonNull(manufacturerReferenceNumber) && !manufacturerReferenceNumber.trim().isEmpty()) {
68 entitlementPoolEntity.setManufacturerReferenceNumber(manufacturerReferenceNumber);
71 Set<LicenseKeyGroupEntity> licenseKeyGroupEntities = featureGroupModel.getLicenseKeyGroups();
72 for (LicenseKeyGroupEntity licenseKeyGroupEntity : licenseKeyGroupEntities) {
73 licenseKeyGroupEntity.setLimits(vendorLicenseFacade.listLimits(vlmId, vlmVersion, licenseKeyGroupEntity.getId()));
74 if (Objects.nonNull(manufacturerReferenceNumber) && !manufacturerReferenceNumber.trim().isEmpty()) {
75 licenseKeyGroupEntity.setManufacturerReferenceNumber(manufacturerReferenceNumber);
78 featureGroupModel.setEntitlementPools(
79 entitlementPoolEntities.stream().map(entitlementPoolEntity -> (EntitlementPoolEntity) healingService.heal(entitlementPoolEntity))
80 .collect(Collectors.toSet()));
81 featureGroupModel.setLicenseKeyGroups(
82 licenseKeyGroupEntities.stream().map(licenseKeyGroupEntity -> (LicenseKeyGroupEntity) healingService.heal(licenseKeyGroupEntity))
83 .collect(Collectors.toSet()));
84 artifact.getFeatureGroups().add(featureGroupModel);
87 return artifact.toXml().getBytes();
90 private static byte[] createVendorLicenseArtifact(String vlmId, String vendorName) {
91 VendorLicenseArtifact vendorLicenseArtifact = new VendorLicenseArtifact();
92 vendorLicenseArtifact.setVendorName(vendorName);
93 Set<EntitlementPoolEntity> entitlementPoolEntities = new HashSet<>();
94 Set<LicenseKeyGroupEntity> licenseKeyGroupEntities = new HashSet<>();
95 List<Version> finalVersions = getFinalVersionsForVlm(vlmId);
96 for (Version finalVersion : finalVersions) {
97 Collection<EntitlementPoolEntity> eps = vendorLicenseFacade.listEntitlementPools(vlmId, finalVersion);
98 eps.forEach(entitlementPoolEntity -> {
99 entitlementPoolEntity.setLimits(vendorLicenseFacade.listLimits(vlmId, finalVersion, entitlementPoolEntity.getId()));
100 Optional<String> manufacturerReferenceNumber = getFeatureGroupManufactureRefNumber(
101 entitlementPoolEntity.getReferencingFeatureGroups(), vlmId, finalVersion);
102 manufacturerReferenceNumber.ifPresent(entitlementPoolEntity::setManufacturerReferenceNumber);
104 entitlementPoolEntities.addAll(eps);
105 Collection<LicenseKeyGroupEntity> lkgs = vendorLicenseFacade.listLicenseKeyGroups(vlmId, finalVersion);
106 lkgs.forEach(licenseKeyGroupEntity -> {
107 licenseKeyGroupEntity.setLimits(vendorLicenseFacade.listLimits(vlmId, finalVersion, licenseKeyGroupEntity.getId()));
108 Optional<String> manufacturerReferenceNumber = getFeatureGroupManufactureRefNumber(
109 licenseKeyGroupEntity.getReferencingFeatureGroups(), vlmId, finalVersion);
110 manufacturerReferenceNumber.ifPresent(licenseKeyGroupEntity::setManufacturerReferenceNumber);
112 licenseKeyGroupEntities.addAll(lkgs);
114 entitlementPoolEntities = healEPs(filterChangedEntities(prepareForFiltering(entitlementPoolEntities, true)));
115 licenseKeyGroupEntities = healLkgs(filterChangedEntities(prepareForFiltering(licenseKeyGroupEntities, false)));
116 vendorLicenseArtifact.setEntitlementPoolEntities(entitlementPoolEntities);
117 vendorLicenseArtifact.setLicenseKeyGroupEntities(licenseKeyGroupEntities);
118 return vendorLicenseArtifact.toXml().getBytes();
121 private static Optional<String> getFeatureGroupManufactureRefNumber(Set<String> featureGroupIds, String vlmId, Version finalVersion) {
122 String manufactureReferenceNumber = null;
123 if (CollectionUtils.isNotEmpty(featureGroupIds)) {
124 Object[] featureGroupIdsList = featureGroupIds.toArray();
125 if (featureGroupIdsList.length > 0) {
126 FeatureGroupEntity featureGroup = vendorLicenseFacade
127 .getFeatureGroup(new FeatureGroupEntity(vlmId, finalVersion, featureGroupIdsList[0].toString()));
128 manufactureReferenceNumber = featureGroup != null ? featureGroup.getManufacturerReferenceNumber() : null;
131 return StringUtils.isNotEmpty(manufactureReferenceNumber) ? Optional.of(manufactureReferenceNumber) : Optional.empty();
135 * Create License Artifacts.
139 * @param vlmVersion vlmVersion
140 * @param featureGroups featureGroups
141 * @return FileContentHandler
144 public FileContentHandler createLicenseArtifacts(String vspId, String vlmId, Version vlmVersion, List<String> featureGroups) {
145 FileContentHandler artifacts = new FileContentHandler();
146 String vendorName = getVendorName(vlmId);
147 artifacts.addFile(VNF_ARTIFACT_NAME_WITH_PATH, createVnfArtifact(vspId, vlmId, vlmVersion, vendorName, featureGroups));
148 artifacts.addFile(VENDOR_LICENSE_MODEL_ARTIFACT_NAME_WITH_PATH, createVendorLicenseArtifact(vlmId, vendorName));