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 org.apache.commons.collections.CollectionUtils;
24 import org.openecomp.core.utilities.file.FileContentHandler;
25 import org.openecomp.sdc.logging.context.impl.MdcDataDebugMessage;
26 import org.openecomp.sdc.vendorlicense.HealingServiceFactory;
27 import org.openecomp.sdc.vendorlicense.dao.types.EntitlementPoolEntity;
28 import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity;
29 import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupModel;
30 import org.openecomp.sdc.vendorlicense.dao.types.LicenseKeyGroupEntity;
31 import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacade;
32 import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacadeFactory;
33 import org.openecomp.sdc.vendorlicense.healing.HealingService;
34 import org.openecomp.sdc.vendorlicense.licenseartifacts.VendorLicenseArtifactsService;
35 import org.openecomp.sdc.vendorlicense.licenseartifacts.impl.types.VendorLicenseArtifact;
36 import org.openecomp.sdc.vendorlicense.licenseartifacts.impl.types.VnfLicenseArtifact;
37 import org.openecomp.sdc.versioning.dao.types.Version;
39 import java.util.Collection;
40 import java.util.HashSet;
41 import java.util.List;
42 import java.util.Optional;
44 import java.util.stream.Collectors;
46 import static org.openecomp.sdc.vendorlicense.VendorLicenseConstants.VENDOR_LICENSE_MODEL_ARTIFACT_NAME_WITH_PATH;
47 import static org.openecomp.sdc.vendorlicense.VendorLicenseConstants.VNF_ARTIFACT_NAME_WITH_PATH;
48 import static org.openecomp.sdc.vendorlicense.licenseartifacts.impl.util.VendorLicenseArtifactsServiceUtils.filterChangedEntities;
49 import static org.openecomp.sdc.vendorlicense.licenseartifacts.impl.util.VendorLicenseArtifactsServiceUtils.getFinalVersionsForVlm;
50 import static org.openecomp.sdc.vendorlicense.licenseartifacts.impl.util.VendorLicenseArtifactsServiceUtils.getVendorName;
51 import static org.openecomp.sdc.vendorlicense.licenseartifacts.impl.util.VendorLicenseArtifactsServiceUtils.healEPs;
52 import static org.openecomp.sdc.vendorlicense.licenseartifacts.impl.util.VendorLicenseArtifactsServiceUtils.healLkgs;
53 import static org.openecomp.sdc.vendorlicense.licenseartifacts.impl.util.VendorLicenseArtifactsServiceUtils.prepareForFiltering;
55 public class VendorLicenseArtifactsServiceImpl implements VendorLicenseArtifactsService {
57 public static final VendorLicenseFacade vendorLicenseFacade =
58 VendorLicenseFacadeFactory.getInstance().createInterface();
59 public static final HealingService healingService =
60 HealingServiceFactory.getInstance().createInterface();
61 private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
64 private static byte[] createVnfArtifact(String vspId, String vlmId, Version vlmVersion,
66 List<String> featureGroups, String user) {
69 mdcDataDebugMessage.debugEntryMessage("VLM name", vendorName);
71 VnfLicenseArtifact artifact = new VnfLicenseArtifact();
73 artifact.setVspId(vspId);
74 artifact.setVendorName(vendorName);
75 if (featureGroups != null) {
76 for (String featureGroupId : featureGroups) {
77 FeatureGroupModel featureGroupModel = vendorLicenseFacade
78 .getFeatureGroupModel(new FeatureGroupEntity(vlmId, vlmVersion, featureGroupId), user);
79 Set<EntitlementPoolEntity> entitlementPoolEntities =
80 featureGroupModel.getEntitlementPools();
81 for (EntitlementPoolEntity entitlementPoolEntity : entitlementPoolEntities) {
82 entitlementPoolEntity.setLimits(vendorLicenseFacade.listLimits(vlmId, vlmVersion,
83 entitlementPoolEntity.getId(), user));
84 entitlementPoolEntity.setManufacturerReferenceNumber(featureGroupModel.
85 getEntityManufacturerReferenceNumber());
87 Set<LicenseKeyGroupEntity> licenseKeyGroupEntities =
88 featureGroupModel.getLicenseKeyGroups();
89 for (LicenseKeyGroupEntity licenseKeyGroupEntity : licenseKeyGroupEntities) {
90 licenseKeyGroupEntity.setLimits(vendorLicenseFacade.listLimits(vlmId, vlmVersion,
91 licenseKeyGroupEntity.getId(), user));
92 licenseKeyGroupEntity.setManufacturerReferenceNumber(featureGroupModel.
93 getEntityManufacturerReferenceNumber());
96 featureGroupModel.setEntitlementPools(entitlementPoolEntities.stream().map(
97 entitlementPoolEntity -> (EntitlementPoolEntity) healingService
98 .heal(entitlementPoolEntity, user)).collect(Collectors.toSet()));
99 featureGroupModel.setLicenseKeyGroups(licenseKeyGroupEntities.stream().map(
100 licenseKeyGroupEntity -> (LicenseKeyGroupEntity) healingService
101 .heal(licenseKeyGroupEntity, user)).collect(Collectors.toSet()));
102 artifact.getFeatureGroups().add(featureGroupModel);
106 mdcDataDebugMessage.debugExitMessage("VLM name", vendorName);
107 return artifact.toXml().getBytes();
110 private static byte[] createVendorLicenseArtifact(String vlmId, String vendorName, String user) {
113 mdcDataDebugMessage.debugEntryMessage("VLM name", vendorName);
115 VendorLicenseArtifact vendorLicenseArtifact = new VendorLicenseArtifact();
116 vendorLicenseArtifact.setVendorName(vendorName);
117 Set<EntitlementPoolEntity> entitlementPoolEntities = new HashSet<>();
118 Set<LicenseKeyGroupEntity> licenseKeyGroupEntities = new HashSet<>();
120 List<Version> finalVersions = getFinalVersionsForVlm(vlmId);
121 for (Version finalVersion : finalVersions) {
122 Collection<EntitlementPoolEntity> coll = vendorLicenseFacade.listEntitlementPools(vlmId,
124 coll.stream().forEach(entitlementPoolEntity -> {
125 entitlementPoolEntity.setLimits(vendorLicenseFacade.listLimits(vlmId, finalVersion,
126 entitlementPoolEntity.getId(), user));
127 Optional<String> manufacturerReferenceNumber = getFeatureGroupManufactureRefNumber
128 (entitlementPoolEntity.getReferencingFeatureGroups(), vlmId, finalVersion, user);
129 manufacturerReferenceNumber
130 .ifPresent(entitlementPoolEntity::setManufacturerReferenceNumber);
133 entitlementPoolEntities.addAll(coll);
135 Collection<LicenseKeyGroupEntity> coll2 = vendorLicenseFacade.listLicenseKeyGroups(vlmId,
138 coll2.stream().forEach(licenseKeyGroupEntity -> {
139 licenseKeyGroupEntity.setLimits(vendorLicenseFacade.listLimits(vlmId, finalVersion,
140 licenseKeyGroupEntity.getId(), user));
141 Optional<String> manufacturerReferenceNumber = getFeatureGroupManufactureRefNumber
142 (licenseKeyGroupEntity.getReferencingFeatureGroups(), vlmId, finalVersion, user);
143 manufacturerReferenceNumber
144 .ifPresent(licenseKeyGroupEntity::setManufacturerReferenceNumber);
147 licenseKeyGroupEntities.addAll(coll2);
150 entitlementPoolEntities =
151 healEPs(user, filterChangedEntities(prepareForFiltering(entitlementPoolEntities, user,
153 licenseKeyGroupEntities =
154 healLkgs(user, filterChangedEntities(prepareForFiltering(licenseKeyGroupEntities, user,
156 vendorLicenseArtifact.setEntitlementPoolEntities(entitlementPoolEntities);
157 vendorLicenseArtifact.setLicenseKeyGroupEntities(licenseKeyGroupEntities);
159 mdcDataDebugMessage.debugExitMessage("VLM name", vendorName);
160 return vendorLicenseArtifact.toXml().getBytes();
163 private static Optional<String> getFeatureGroupManufactureRefNumber(Set<String> featureGroupIds,
165 Version finalVersion,
167 String manufactureReferenceNumber = null;
168 if (CollectionUtils.isNotEmpty(featureGroupIds)) {
169 Object[] featureGroupIdsList = featureGroupIds.toArray();
170 if (featureGroupIdsList.length > 0) {
171 FeatureGroupEntity featureGroup =
172 vendorLicenseFacade.getFeatureGroup(new FeatureGroupEntity(vlmId, finalVersion,
173 featureGroupIdsList[0].toString()), user);
174 manufactureReferenceNumber = featureGroup != null ? featureGroup
175 .getManufacturerReferenceNumber() : null;
178 return manufactureReferenceNumber != null ? Optional.of(manufactureReferenceNumber) :
184 * Create License Artifacts.
188 * @param vlmVersion vlmVersion
189 * @param featureGroups featureGroups
191 * @return FileContentHandler
193 public FileContentHandler createLicenseArtifacts(String vspId, String vlmId, Version vlmVersion,
194 List<String> featureGroups, String user) {
197 mdcDataDebugMessage.debugEntryMessage("VSP Id", vspId);
199 FileContentHandler artifacts = new FileContentHandler();
200 String vendorName = getVendorName(vlmId, user);
202 artifacts.addFile(VNF_ARTIFACT_NAME_WITH_PATH,
203 createVnfArtifact(vspId, vlmId, vlmVersion, vendorName, featureGroups, user));
204 artifacts.addFile(VENDOR_LICENSE_MODEL_ARTIFACT_NAME_WITH_PATH,
205 createVendorLicenseArtifact(vlmId, vendorName, user));
207 mdcDataDebugMessage.debugExitMessage("VSP Id", vspId);