2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.vendorlicense.licenseartifacts.impl;
23 import static org.openecomp.sdc.vendorlicense.VendorLicenseConstants.VENDOR_LICENSE_MODEL_ARTIFACT_NAME_WITH_PATH;
24 import static org.openecomp.sdc.vendorlicense.VendorLicenseConstants.VNF_ARTIFACT_NAME_WITH_PATH;
26 import org.apache.commons.collections.CollectionUtils;
27 import org.openecomp.core.utilities.file.FileContentHandler;
28 import org.openecomp.sdc.logging.context.impl.MdcDataDebugMessage;
29 import org.openecomp.sdc.vendorlicense.HealingServiceFactory;
30 import org.openecomp.sdc.vendorlicense.dao.types.EntitlementPoolEntity;
31 import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity;
32 import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupModel;
33 import org.openecomp.sdc.vendorlicense.dao.types.LicenseKeyGroupEntity;
34 import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacade;
35 import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacadeFactory;
36 import org.openecomp.sdc.vendorlicense.healing.HealingService;
37 import org.openecomp.sdc.vendorlicense.licenseartifacts.VendorLicenseArtifactsService;
38 import org.openecomp.sdc.vendorlicense.licenseartifacts.impl.types.VendorLicenseArtifact;
39 import org.openecomp.sdc.vendorlicense.licenseartifacts.impl.types.VnfLicenseArtifact;
40 import org.openecomp.sdc.versioning.dao.types.Version;
42 import java.util.Collection;
43 import java.util.HashSet;
44 import java.util.List;
45 import java.util.Optional;
47 import java.util.stream.Collectors;
49 import static org.openecomp.sdc.vendorlicense.VendorLicenseConstants.VENDOR_LICENSE_MODEL_ARTIFACT_NAME_WITH_PATH;
50 import static org.openecomp.sdc.vendorlicense.VendorLicenseConstants.VNF_ARTIFACT_NAME_WITH_PATH;
51 import static org.openecomp.sdc.vendorlicense.licenseartifacts.impl.util.VendorLicenseArtifactsServiceUtils.filterChangedEntities;
52 import static org.openecomp.sdc.vendorlicense.licenseartifacts.impl.util.VendorLicenseArtifactsServiceUtils.getFinalVersionsForVlm;
53 import static org.openecomp.sdc.vendorlicense.licenseartifacts.impl.util.VendorLicenseArtifactsServiceUtils.getVendorName;
54 import static org.openecomp.sdc.vendorlicense.licenseartifacts.impl.util.VendorLicenseArtifactsServiceUtils.healEPs;
55 import static org.openecomp.sdc.vendorlicense.licenseartifacts.impl.util.VendorLicenseArtifactsServiceUtils.healLkgs;
56 import static org.openecomp.sdc.vendorlicense.licenseartifacts.impl.util.VendorLicenseArtifactsServiceUtils.prepareForFiltering;
58 public class VendorLicenseArtifactsServiceImpl implements VendorLicenseArtifactsService {
60 public static final VendorLicenseFacade vendorLicenseFacade =
61 VendorLicenseFacadeFactory.getInstance().createInterface();
62 public static final HealingService healingService =
63 HealingServiceFactory.getInstance().createInterface();
64 private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
67 private static byte[] createVnfArtifact(String vspId, String vlmId, Version vlmVersion,
69 List<String> featureGroups, String user) {
72 mdcDataDebugMessage.debugEntryMessage("VLM name", vendorName);
74 VnfLicenseArtifact artifact = new VnfLicenseArtifact();
76 artifact.setVspId(vspId);
77 artifact.setVendorName(vendorName);
78 if (featureGroups != null) {
79 for (String featureGroupId : featureGroups) {
80 FeatureGroupModel featureGroupModel = vendorLicenseFacade
81 .getFeatureGroupModel(new FeatureGroupEntity(vlmId, vlmVersion, featureGroupId), user);
82 Set<EntitlementPoolEntity> entitlementPoolEntities =
83 featureGroupModel.getEntitlementPools();
84 for (EntitlementPoolEntity entitlementPoolEntity : entitlementPoolEntities) {
85 entitlementPoolEntity.setLimits(vendorLicenseFacade.listLimits(vlmId, vlmVersion,
86 entitlementPoolEntity.getId(), user));
87 entitlementPoolEntity.setManufacturerReferenceNumber(featureGroupModel.
88 getEntityManufacturerReferenceNumber());
90 Set<LicenseKeyGroupEntity> licenseKeyGroupEntities =
91 featureGroupModel.getLicenseKeyGroups();
92 for (LicenseKeyGroupEntity licenseKeyGroupEntity : licenseKeyGroupEntities) {
93 licenseKeyGroupEntity.setLimits(vendorLicenseFacade.listLimits(vlmId, vlmVersion,
94 licenseKeyGroupEntity.getId(), user));
95 licenseKeyGroupEntity.setManufacturerReferenceNumber(featureGroupModel.
96 getEntityManufacturerReferenceNumber());
99 featureGroupModel.setEntitlementPools(entitlementPoolEntities.stream().map(
100 entitlementPoolEntity -> (EntitlementPoolEntity) healingService
101 .heal(entitlementPoolEntity, user)).collect(Collectors.toSet()));
102 featureGroupModel.setLicenseKeyGroups(licenseKeyGroupEntities.stream().map(
103 licenseKeyGroupEntity -> (LicenseKeyGroupEntity) healingService
104 .heal(licenseKeyGroupEntity, user)).collect(Collectors.toSet()));
105 artifact.getFeatureGroups().add(featureGroupModel);
109 mdcDataDebugMessage.debugExitMessage("VLM name", vendorName);
110 return artifact.toXml().getBytes();
113 private static byte[] createVendorLicenseArtifact(String vlmId, String vendorName, String user) {
116 mdcDataDebugMessage.debugEntryMessage("VLM name", vendorName);
118 VendorLicenseArtifact vendorLicenseArtifact = new VendorLicenseArtifact();
119 vendorLicenseArtifact.setVendorName(vendorName);
120 Set<EntitlementPoolEntity> entitlementPoolEntities = new HashSet<>();
121 Set<LicenseKeyGroupEntity> licenseKeyGroupEntities = new HashSet<>();
123 List<Version> finalVersions = getFinalVersionsForVlm(vlmId);
124 for (Version finalVersion : finalVersions) {
125 Collection<EntitlementPoolEntity> coll = vendorLicenseFacade.listEntitlementPools(vlmId,
127 coll.stream().forEach(entitlementPoolEntity -> {
128 entitlementPoolEntity.setLimits(vendorLicenseFacade.listLimits(vlmId, finalVersion,
129 entitlementPoolEntity.getId(), user));
130 Optional<String> manufacturerReferenceNumber = getFeatureGroupManufactureRefNumber
131 (entitlementPoolEntity.getReferencingFeatureGroups(), vlmId, finalVersion, user);
132 manufacturerReferenceNumber
133 .ifPresent(entitlementPoolEntity::setManufacturerReferenceNumber);
136 entitlementPoolEntities.addAll(coll);
138 Collection<LicenseKeyGroupEntity> coll2 = vendorLicenseFacade.listLicenseKeyGroups(vlmId,
141 coll2.stream().forEach(licenseKeyGroupEntity -> {
142 licenseKeyGroupEntity.setLimits(vendorLicenseFacade.listLimits(vlmId, finalVersion,
143 licenseKeyGroupEntity.getId(), user));
144 Optional<String> manufacturerReferenceNumber = getFeatureGroupManufactureRefNumber
145 (licenseKeyGroupEntity.getReferencingFeatureGroups(), vlmId, finalVersion, user);
146 manufacturerReferenceNumber
147 .ifPresent(licenseKeyGroupEntity::setManufacturerReferenceNumber);
150 licenseKeyGroupEntities.addAll(coll2);
153 entitlementPoolEntities =
154 healEPs(user, filterChangedEntities(prepareForFiltering(entitlementPoolEntities, user,
156 licenseKeyGroupEntities =
157 healLkgs(user, filterChangedEntities(prepareForFiltering(licenseKeyGroupEntities, user,
159 vendorLicenseArtifact.setEntitlementPoolEntities(entitlementPoolEntities);
160 vendorLicenseArtifact.setLicenseKeyGroupEntities(licenseKeyGroupEntities);
162 mdcDataDebugMessage.debugExitMessage("VLM name", vendorName);
163 return vendorLicenseArtifact.toXml().getBytes();
166 private static Optional<String> getFeatureGroupManufactureRefNumber(Set<String> featureGroupIds,
168 Version finalVersion,
170 String manufactureReferenceNumber = null;
171 if (CollectionUtils.isNotEmpty(featureGroupIds)) {
172 Object[] featureGroupIdsList = featureGroupIds.toArray();
173 if (featureGroupIdsList.length > 0) {
174 FeatureGroupEntity featureGroup =
175 vendorLicenseFacade.getFeatureGroup(new FeatureGroupEntity(vlmId, finalVersion,
176 featureGroupIdsList[0].toString()), user);
177 manufactureReferenceNumber = featureGroup != null ? featureGroup
178 .getManufacturerReferenceNumber() : null;
181 return manufactureReferenceNumber != null ? Optional.of(manufactureReferenceNumber) :
187 * Create License Artifacts.
191 * @param vlmVersion vlmVersion
192 * @param featureGroups featureGroups
194 * @return FileContentHandler
196 public FileContentHandler createLicenseArtifacts(String vspId, String vlmId, Version vlmVersion,
197 List<String> featureGroups, String user) {
200 mdcDataDebugMessage.debugEntryMessage("VSP Id", vspId);
202 FileContentHandler artifacts = new FileContentHandler();
203 String vendorName = getVendorName(vlmId, user);
205 artifacts.addFile(VNF_ARTIFACT_NAME_WITH_PATH,
206 createVnfArtifact(vspId, vlmId, vlmVersion, vendorName, featureGroups, user));
207 artifacts.addFile(VENDOR_LICENSE_MODEL_ARTIFACT_NAME_WITH_PATH,
208 createVendorLicenseArtifact(vlmId, vendorName, user));
210 mdcDataDebugMessage.debugExitMessage("VSP Id", vspId);