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.openecomp.core.utilities.file.FileContentHandler;
21 import org.openecomp.sdc.vendorlicense.HealingServiceFactory;
22 import org.openecomp.sdc.vendorlicense.dao.types.EntitlementPoolEntity;
23 import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity;
24 import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupModel;
25 import org.openecomp.sdc.vendorlicense.dao.types.LicenseKeyGroupEntity;
26 import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacade;
27 import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacadeFactory;
28 import org.openecomp.sdc.vendorlicense.healing.HealingService;
29 import org.openecomp.sdc.vendorlicense.licenseartifacts.VendorLicenseArtifactsService;
30 import org.openecomp.sdc.vendorlicense.licenseartifacts.impl.types.VendorLicenseArtifact;
31 import org.openecomp.sdc.vendorlicense.licenseartifacts.impl.types.VnfLicenseArtifact;
32 import org.openecomp.sdc.versioning.dao.types.Version;
34 import java.util.Collection;
35 import java.util.HashSet;
36 import java.util.List;
37 import java.util.Optional;
39 import java.util.stream.Collectors;
41 import static org.openecomp.sdc.vendorlicense.VendorLicenseConstants.VENDOR_LICENSE_MODEL_ARTIFACT_NAME_WITH_PATH;
42 import static org.openecomp.sdc.vendorlicense.VendorLicenseConstants.VNF_ARTIFACT_NAME_WITH_PATH;
43 import static org.openecomp.sdc.vendorlicense.licenseartifacts.impl.util.VendorLicenseArtifactsServiceUtils.filterChangedEntities;
44 import static org.openecomp.sdc.vendorlicense.licenseartifacts.impl.util.VendorLicenseArtifactsServiceUtils.getFinalVersionsForVlm;
45 import static org.openecomp.sdc.vendorlicense.licenseartifacts.impl.util.VendorLicenseArtifactsServiceUtils.getVendorName;
46 import static org.openecomp.sdc.vendorlicense.licenseartifacts.impl.util.VendorLicenseArtifactsServiceUtils.healEPs;
47 import static org.openecomp.sdc.vendorlicense.licenseartifacts.impl.util.VendorLicenseArtifactsServiceUtils.healLkgs;
48 import static org.openecomp.sdc.vendorlicense.licenseartifacts.impl.util.VendorLicenseArtifactsServiceUtils.prepareForFiltering;
50 public class VendorLicenseArtifactsServiceImpl implements VendorLicenseArtifactsService {
52 public static final VendorLicenseFacade vendorLicenseFacade =
53 VendorLicenseFacadeFactory.getInstance().createInterface();
54 public static final HealingService healingService =
55 HealingServiceFactory.getInstance().createInterface();
56 private static byte[] createVnfArtifact(String vspId, String vlmId, Version vlmVersion, String vendorName,
57 List<String> featureGroups) {
58 VnfLicenseArtifact artifact = new VnfLicenseArtifact();
60 artifact.setVspId(vspId);
61 artifact.setVendorName(vendorName);
62 if (featureGroups != null) {
63 for (String featureGroupId : featureGroups) {
64 FeatureGroupModel featureGroupModel = vendorLicenseFacade
65 .getFeatureGroupModel(new FeatureGroupEntity(vlmId, vlmVersion, featureGroupId));
66 Set<EntitlementPoolEntity> entitlementPoolEntities =
67 featureGroupModel.getEntitlementPools();
68 for (EntitlementPoolEntity entitlementPoolEntity : entitlementPoolEntities) {
69 entitlementPoolEntity.setLimits(vendorLicenseFacade.listLimits(vlmId, vlmVersion,
70 entitlementPoolEntity.getId()));
71 entitlementPoolEntity.setManufacturerReferenceNumber(featureGroupModel.
72 getEntityManufacturerReferenceNumber());
75 Set<LicenseKeyGroupEntity> licenseKeyGroupEntities =
76 featureGroupModel.getLicenseKeyGroups();
77 for (LicenseKeyGroupEntity licenseKeyGroupEntity : licenseKeyGroupEntities) {
78 licenseKeyGroupEntity.setLimits(vendorLicenseFacade.listLimits(vlmId, vlmVersion,
79 licenseKeyGroupEntity.getId()));
80 licenseKeyGroupEntity.setManufacturerReferenceNumber(featureGroupModel.
81 getEntityManufacturerReferenceNumber());
84 featureGroupModel.setEntitlementPools(entitlementPoolEntities.stream().map(
85 entitlementPoolEntity -> (EntitlementPoolEntity) healingService
86 .heal(entitlementPoolEntity)).collect(Collectors.toSet()));
87 featureGroupModel.setLicenseKeyGroups(licenseKeyGroupEntities.stream().map(
88 licenseKeyGroupEntity -> (LicenseKeyGroupEntity) healingService
89 .heal(licenseKeyGroupEntity)).collect(Collectors.toSet()));
90 artifact.getFeatureGroups().add(featureGroupModel);
93 return artifact.toXml().getBytes();
96 private static byte[] createVendorLicenseArtifact(String vlmId, String vendorName) {
97 VendorLicenseArtifact vendorLicenseArtifact = new VendorLicenseArtifact();
98 vendorLicenseArtifact.setVendorName(vendorName);
99 Set<EntitlementPoolEntity> entitlementPoolEntities = new HashSet<>();
100 Set<LicenseKeyGroupEntity> licenseKeyGroupEntities = new HashSet<>();
102 List<Version> finalVersions = getFinalVersionsForVlm(vlmId);
103 for (Version finalVersion : finalVersions) {
104 Collection<EntitlementPoolEntity> eps =
105 vendorLicenseFacade.listEntitlementPools(vlmId, finalVersion);
106 eps.forEach(entitlementPoolEntity -> {
107 entitlementPoolEntity.setLimits(vendorLicenseFacade.listLimits(vlmId, finalVersion,
108 entitlementPoolEntity.getId()));
109 Optional<String> manufacturerReferenceNumber = getFeatureGroupManufactureRefNumber
110 (entitlementPoolEntity.getReferencingFeatureGroups(), vlmId, finalVersion);
111 manufacturerReferenceNumber
112 .ifPresent(entitlementPoolEntity::setManufacturerReferenceNumber);
114 entitlementPoolEntities.addAll(eps);
116 Collection<LicenseKeyGroupEntity> lkgs =
117 vendorLicenseFacade.listLicenseKeyGroups(vlmId, finalVersion);
119 lkgs.forEach(licenseKeyGroupEntity -> {
120 licenseKeyGroupEntity.setLimits(vendorLicenseFacade.listLimits(vlmId, finalVersion,
121 licenseKeyGroupEntity.getId()));
122 Optional<String> manufacturerReferenceNumber = getFeatureGroupManufactureRefNumber
123 (licenseKeyGroupEntity.getReferencingFeatureGroups(), vlmId, finalVersion);
124 manufacturerReferenceNumber
125 .ifPresent(licenseKeyGroupEntity::setManufacturerReferenceNumber);
127 licenseKeyGroupEntities.addAll(lkgs);
131 entitlementPoolEntities =
132 healEPs(filterChangedEntities(prepareForFiltering(entitlementPoolEntities, true)));
133 licenseKeyGroupEntities =
134 healLkgs(filterChangedEntities(prepareForFiltering(licenseKeyGroupEntities, false)));
135 vendorLicenseArtifact.setEntitlementPoolEntities(entitlementPoolEntities);
136 vendorLicenseArtifact.setLicenseKeyGroupEntities(licenseKeyGroupEntities);
137 return vendorLicenseArtifact.toXml().getBytes();
140 private static Optional<String> getFeatureGroupManufactureRefNumber(Set<String> featureGroupIds,
142 Version finalVersion) {
143 String manufactureReferenceNumber = null;
144 if (CollectionUtils.isNotEmpty(featureGroupIds)) {
145 Object[] featureGroupIdsList = featureGroupIds.toArray();
146 if (featureGroupIdsList.length > 0) {
147 FeatureGroupEntity featureGroup =
148 vendorLicenseFacade.getFeatureGroup(new FeatureGroupEntity(vlmId, finalVersion,
149 featureGroupIdsList[0].toString()));
150 manufactureReferenceNumber = featureGroup != null ? featureGroup
151 .getManufacturerReferenceNumber() : null;
154 return manufactureReferenceNumber != null ? Optional.of(manufactureReferenceNumber) :
160 * Create License Artifacts.
164 * @param vlmVersion vlmVersion
165 * @param featureGroups featureGroups
166 * @return FileContentHandler
169 public FileContentHandler createLicenseArtifacts(String vspId, String vlmId, Version vlmVersion,
170 List<String> featureGroups) {
171 FileContentHandler artifacts = new FileContentHandler();
172 String vendorName = getVendorName(vlmId);
174 artifacts.addFile(VNF_ARTIFACT_NAME_WITH_PATH,
175 createVnfArtifact(vspId, vlmId, vlmVersion, vendorName, featureGroups));
176 artifacts.addFile(VENDOR_LICENSE_MODEL_ARTIFACT_NAME_WITH_PATH,
177 createVendorLicenseArtifact(vlmId, vendorName));