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.
17 package org.openecomp.sdc.vendorlicense.licenseartifacts.impl;
19 import org.apache.commons.collections.CollectionUtils;
20 import org.apache.commons.lang.StringUtils;
21 import org.openecomp.core.utilities.file.FileContentHandler;
22 import org.openecomp.sdc.vendorlicense.HealingServiceFactory;
23 import org.openecomp.sdc.vendorlicense.dao.types.EntitlementPoolEntity;
24 import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity;
25 import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupModel;
26 import org.openecomp.sdc.vendorlicense.dao.types.LicenseKeyGroupEntity;
27 import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacade;
28 import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacadeFactory;
29 import org.openecomp.sdc.vendorlicense.healing.HealingService;
30 import org.openecomp.sdc.vendorlicense.licenseartifacts.VendorLicenseArtifactsService;
31 import org.openecomp.sdc.vendorlicense.licenseartifacts.impl.types.VendorLicenseArtifact;
32 import org.openecomp.sdc.vendorlicense.licenseartifacts.impl.types.VnfLicenseArtifact;
33 import org.openecomp.sdc.versioning.dao.types.Version;
35 import java.util.Collection;
36 import java.util.HashSet;
37 import java.util.List;
38 import java.util.Objects;
39 import java.util.Optional;
41 import java.util.stream.Collectors;
43 import static org.openecomp.sdc.vendorlicense.VendorLicenseConstants.VENDOR_LICENSE_MODEL_ARTIFACT_NAME_WITH_PATH;
44 import static org.openecomp.sdc.vendorlicense.VendorLicenseConstants.VNF_ARTIFACT_NAME_WITH_PATH;
45 import static org.openecomp.sdc.vendorlicense.licenseartifacts.impl.util.VendorLicenseArtifactsServiceUtils.filterChangedEntities;
46 import static org.openecomp.sdc.vendorlicense.licenseartifacts.impl.util.VendorLicenseArtifactsServiceUtils.getFinalVersionsForVlm;
47 import static org.openecomp.sdc.vendorlicense.licenseartifacts.impl.util.VendorLicenseArtifactsServiceUtils.getVendorName;
48 import static org.openecomp.sdc.vendorlicense.licenseartifacts.impl.util.VendorLicenseArtifactsServiceUtils.healEPs;
49 import static org.openecomp.sdc.vendorlicense.licenseartifacts.impl.util.VendorLicenseArtifactsServiceUtils.healLkgs;
50 import static org.openecomp.sdc.vendorlicense.licenseartifacts.impl.util.VendorLicenseArtifactsServiceUtils.prepareForFiltering;
52 public class VendorLicenseArtifactsServiceImpl implements VendorLicenseArtifactsService {
54 public static final VendorLicenseFacade vendorLicenseFacade =
55 VendorLicenseFacadeFactory.getInstance().createInterface();
56 public static final HealingService healingService =
57 HealingServiceFactory.getInstance().createInterface();
58 private static byte[] createVnfArtifact(String vspId, String vlmId, Version vlmVersion, String vendorName,
59 List<String> featureGroups) {
60 VnfLicenseArtifact artifact = new VnfLicenseArtifact();
62 artifact.setVspId(vspId);
63 artifact.setVendorName(vendorName);
64 if (featureGroups != null) {
65 for (String featureGroupId : featureGroups) {
66 FeatureGroupModel featureGroupModel = vendorLicenseFacade
67 .getFeatureGroupModel(new FeatureGroupEntity(vlmId, vlmVersion, featureGroupId));
68 Set<EntitlementPoolEntity> entitlementPoolEntities =
69 featureGroupModel.getEntitlementPools();
70 String manufacturerReferenceNumber = featureGroupModel.getEntityManufacturerReferenceNumber();
71 for (EntitlementPoolEntity entitlementPoolEntity : entitlementPoolEntities) {
72 entitlementPoolEntity.setLimits(vendorLicenseFacade.listLimits(vlmId, vlmVersion,
73 entitlementPoolEntity.getId()));
74 if (Objects.nonNull(manufacturerReferenceNumber) && !manufacturerReferenceNumber.trim().isEmpty()) {
75 entitlementPoolEntity.setManufacturerReferenceNumber(manufacturerReferenceNumber);
79 Set<LicenseKeyGroupEntity> licenseKeyGroupEntities =
80 featureGroupModel.getLicenseKeyGroups();
81 for (LicenseKeyGroupEntity licenseKeyGroupEntity : licenseKeyGroupEntities) {
82 licenseKeyGroupEntity.setLimits(vendorLicenseFacade.listLimits(vlmId, vlmVersion,
83 licenseKeyGroupEntity.getId()));
84 if (Objects.nonNull(manufacturerReferenceNumber) && !manufacturerReferenceNumber.trim().isEmpty()) {
85 licenseKeyGroupEntity.setManufacturerReferenceNumber(manufacturerReferenceNumber);
89 featureGroupModel.setEntitlementPools(entitlementPoolEntities.stream().map(
90 entitlementPoolEntity -> (EntitlementPoolEntity) healingService
91 .heal(entitlementPoolEntity)).collect(Collectors.toSet()));
92 featureGroupModel.setLicenseKeyGroups(licenseKeyGroupEntities.stream().map(
93 licenseKeyGroupEntity -> (LicenseKeyGroupEntity) healingService
94 .heal(licenseKeyGroupEntity)).collect(Collectors.toSet()));
95 artifact.getFeatureGroups().add(featureGroupModel);
98 return artifact.toXml().getBytes();
101 private static byte[] createVendorLicenseArtifact(String vlmId, String vendorName) {
102 VendorLicenseArtifact vendorLicenseArtifact = new VendorLicenseArtifact();
103 vendorLicenseArtifact.setVendorName(vendorName);
104 Set<EntitlementPoolEntity> entitlementPoolEntities = new HashSet<>();
105 Set<LicenseKeyGroupEntity> licenseKeyGroupEntities = new HashSet<>();
107 List<Version> finalVersions = getFinalVersionsForVlm(vlmId);
108 for (Version finalVersion : finalVersions) {
109 Collection<EntitlementPoolEntity> eps =
110 vendorLicenseFacade.listEntitlementPools(vlmId, finalVersion);
111 eps.forEach(entitlementPoolEntity -> {
112 entitlementPoolEntity.setLimits(vendorLicenseFacade.listLimits(vlmId, finalVersion,
113 entitlementPoolEntity.getId()));
114 Optional<String> manufacturerReferenceNumber = getFeatureGroupManufactureRefNumber
115 (entitlementPoolEntity.getReferencingFeatureGroups(), vlmId, finalVersion);
116 manufacturerReferenceNumber
117 .ifPresent(entitlementPoolEntity::setManufacturerReferenceNumber);
119 entitlementPoolEntities.addAll(eps);
121 Collection<LicenseKeyGroupEntity> lkgs =
122 vendorLicenseFacade.listLicenseKeyGroups(vlmId, finalVersion);
124 lkgs.forEach(licenseKeyGroupEntity -> {
125 licenseKeyGroupEntity.setLimits(vendorLicenseFacade.listLimits(vlmId, finalVersion,
126 licenseKeyGroupEntity.getId()));
127 Optional<String> manufacturerReferenceNumber = getFeatureGroupManufactureRefNumber
128 (licenseKeyGroupEntity.getReferencingFeatureGroups(), vlmId, finalVersion);
129 manufacturerReferenceNumber
130 .ifPresent(licenseKeyGroupEntity::setManufacturerReferenceNumber);
132 licenseKeyGroupEntities.addAll(lkgs);
136 entitlementPoolEntities =
137 healEPs(filterChangedEntities(prepareForFiltering(entitlementPoolEntities, true)));
138 licenseKeyGroupEntities =
139 healLkgs(filterChangedEntities(prepareForFiltering(licenseKeyGroupEntities, false)));
140 vendorLicenseArtifact.setEntitlementPoolEntities(entitlementPoolEntities);
141 vendorLicenseArtifact.setLicenseKeyGroupEntities(licenseKeyGroupEntities);
142 return vendorLicenseArtifact.toXml().getBytes();
145 private static Optional<String> getFeatureGroupManufactureRefNumber(Set<String> featureGroupIds,
147 Version finalVersion) {
148 String manufactureReferenceNumber = null;
149 if (CollectionUtils.isNotEmpty(featureGroupIds)) {
150 Object[] featureGroupIdsList = featureGroupIds.toArray();
151 if (featureGroupIdsList.length > 0) {
152 FeatureGroupEntity featureGroup =
153 vendorLicenseFacade.getFeatureGroup(new FeatureGroupEntity(vlmId, finalVersion,
154 featureGroupIdsList[0].toString()));
155 manufactureReferenceNumber = featureGroup != null ? featureGroup
156 .getManufacturerReferenceNumber() : null;
159 return StringUtils.isNotEmpty(manufactureReferenceNumber) ? Optional.of(manufactureReferenceNumber) :
165 * Create License Artifacts.
169 * @param vlmVersion vlmVersion
170 * @param featureGroups featureGroups
171 * @return FileContentHandler
174 public FileContentHandler createLicenseArtifacts(String vspId, String vlmId, Version vlmVersion,
175 List<String> featureGroups) {
176 FileContentHandler artifacts = new FileContentHandler();
177 String vendorName = getVendorName(vlmId);
179 artifacts.addFile(VNF_ARTIFACT_NAME_WITH_PATH,
180 createVnfArtifact(vspId, vlmId, vlmVersion, vendorName, featureGroups));
181 artifacts.addFile(VENDOR_LICENSE_MODEL_ARTIFACT_NAME_WITH_PATH,
182 createVendorLicenseArtifact(vlmId, vendorName));