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.common.utils.CommonUtil;
26 import org.openecomp.sdc.logging.context.impl.MdcDataDebugMessage;
27 import org.openecomp.sdc.vendorlicense.HealingServiceFactory;
28 import org.openecomp.sdc.vendorlicense.dao.types.EntitlementPoolEntity;
29 import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity;
30 import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupModel;
31 import org.openecomp.sdc.vendorlicense.dao.types.LicenseKeyGroupEntity;
32 import org.openecomp.sdc.vendorlicense.dao.types.LimitEntity;
33 import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacade;
34 import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacadeFactory;
35 import org.openecomp.sdc.vendorlicense.healing.HealingService;
36 import org.openecomp.sdc.vendorlicense.licenseartifacts.VendorLicenseArtifactsService;
37 import org.openecomp.sdc.vendorlicense.licenseartifacts.impl.types.VendorLicenseArtifact;
38 import org.openecomp.sdc.vendorlicense.licenseartifacts.impl.types.VnfLicenseArtifact;
39 import org.openecomp.sdc.vendorlicense.licenseartifacts.impl.util.VendorLicenseArtifactsServiceUtils;
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;
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 MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
61 static byte[] createVnfArtifact(String vspId, String vlmId, Version vlmVersion, String vendorName,
62 List<String> featureGroups, String user) {
65 mdcDataDebugMessage.debugEntryMessage("VLM name", vendorName);
67 VnfLicenseArtifact artifact = new VnfLicenseArtifact();
69 artifact.setVspId(vspId);
70 artifact.setVendorName(vendorName);
71 if(featureGroups != null) {
72 for (String featureGroupId : featureGroups) {
73 FeatureGroupModel featureGroupModel = vendorLicenseFacade
74 .getFeatureGroupModel(new FeatureGroupEntity(vlmId, vlmVersion, featureGroupId), user);
75 Set<EntitlementPoolEntity> entitlementPoolEntities = featureGroupModel.getEntitlementPools();
76 for(EntitlementPoolEntity entitlementPoolEntity : entitlementPoolEntities){
77 entitlementPoolEntity.setLimits(vendorLicenseFacade.listLimits(vlmId, vlmVersion,
78 entitlementPoolEntity.getId(), user));
79 entitlementPoolEntity.setManufacturerReferenceNumber(featureGroupModel.
80 getEntityManufacturerReferenceNumber());
82 Set<LicenseKeyGroupEntity> licenseKeyGroupEntities = featureGroupModel.getLicenseKeyGroups();
83 for(LicenseKeyGroupEntity licenseKeyGroupEntity : licenseKeyGroupEntities){
84 licenseKeyGroupEntity.setLimits(vendorLicenseFacade.listLimits(vlmId, vlmVersion,
85 licenseKeyGroupEntity.getId(), user));
86 licenseKeyGroupEntity.setManufacturerReferenceNumber(featureGroupModel.
87 getEntityManufacturerReferenceNumber());
90 featureGroupModel.setEntitlementPools(entitlementPoolEntities.stream().map(
91 entitlementPoolEntity -> (EntitlementPoolEntity) healingService
92 .heal(entitlementPoolEntity, user)).collect(Collectors.toSet()));
93 featureGroupModel.setLicenseKeyGroups(licenseKeyGroupEntities.stream().map(
94 licenseKeyGroupEntity -> (LicenseKeyGroupEntity) healingService
95 .heal(licenseKeyGroupEntity, user)).collect(Collectors.toSet()));
96 artifact.getFeatureGroups().add(featureGroupModel);
100 mdcDataDebugMessage.debugExitMessage("VLM name", vendorName);
101 return artifact.toXml().getBytes();
104 static byte[] createVendorLicenseArtifact(String vlmId, String vendorName, String user) {
107 mdcDataDebugMessage.debugEntryMessage("VLM name", vendorName);
109 VendorLicenseArtifact vendorLicenseArtifact = new VendorLicenseArtifact();
110 vendorLicenseArtifact.setVendorName(vendorName);
111 Set<EntitlementPoolEntity> entitlementPoolEntities = new HashSet<>();
112 Set<LicenseKeyGroupEntity> licenseKeyGroupEntities = new HashSet<>();
114 List<Version> finalVersions = VendorLicenseArtifactsServiceUtils.getFinalVersionsForVlm(vlmId);
115 for (Version finalVersion : finalVersions) {
116 Collection<EntitlementPoolEntity> coll = vendorLicenseFacade.listEntitlementPools(vlmId,
118 coll.stream().forEach( entitlementPoolEntity -> {
119 entitlementPoolEntity.setLimits(vendorLicenseFacade.listLimits(vlmId, finalVersion,
120 entitlementPoolEntity.getId(), user));
121 Optional<String> manufacturerReferenceNumber = getFeatureGroupManufactureRefNumber
122 (entitlementPoolEntity.getReferencingFeatureGroups(), vlmId, finalVersion, user);
123 manufacturerReferenceNumber.ifPresent(mrn -> entitlementPoolEntity
124 .setManufacturerReferenceNumber(mrn));
127 entitlementPoolEntities.addAll(coll);
129 Collection<LicenseKeyGroupEntity> coll2 = vendorLicenseFacade.listLicenseKeyGroups(vlmId,
132 coll2.stream().forEach( licenseKeyGroupEntity -> {
133 licenseKeyGroupEntity.setLimits(vendorLicenseFacade.listLimits(vlmId, finalVersion,
134 licenseKeyGroupEntity.getId(), user));
135 Optional<String> manufacturerReferenceNumber = getFeatureGroupManufactureRefNumber
136 (licenseKeyGroupEntity.getReferencingFeatureGroups(), vlmId, finalVersion, user);
137 manufacturerReferenceNumber.ifPresent(mrn -> licenseKeyGroupEntity
138 .setManufacturerReferenceNumber(mrn));
141 licenseKeyGroupEntities.addAll(coll2);
144 entitlementPoolEntities = VendorLicenseArtifactsServiceUtils
146 VendorLicenseArtifactsServiceUtils.filterChangedEntities(entitlementPoolEntities));
147 licenseKeyGroupEntities = VendorLicenseArtifactsServiceUtils
149 VendorLicenseArtifactsServiceUtils.filterChangedEntities(licenseKeyGroupEntities));
151 vendorLicenseArtifact.setEntitlementPoolEntities(entitlementPoolEntities);
152 vendorLicenseArtifact.setLicenseKeyGroupEntities(licenseKeyGroupEntities);
154 mdcDataDebugMessage.debugExitMessage("VLM name", vendorName);
155 return vendorLicenseArtifact.toXml().getBytes();
158 private static Optional<String> getFeatureGroupManufactureRefNumber(Set<String> featureGroupIds,
159 String vlmId, Version finalVersion, String user) {
160 String manufactureReferenceNumber = null;
161 if (CollectionUtils.isNotEmpty(featureGroupIds)) {
162 Object[] featureGroupIdsList = featureGroupIds.toArray();
163 if (featureGroupIdsList != null && featureGroupIdsList.length > 0) {
164 FeatureGroupEntity featureGroup =
165 vendorLicenseFacade.getFeatureGroup(new FeatureGroupEntity(vlmId, finalVersion,
166 featureGroupIdsList[0].toString()), user);
167 manufactureReferenceNumber = featureGroup != null ? featureGroup
168 .getManufacturerReferenceNumber() : null;
171 return manufactureReferenceNumber != null ? Optional.of(manufactureReferenceNumber) :
177 * Create License Artifacts.
180 * @param vlmVersion vlmVersion
181 * @param featureGroups featureGroups
183 * @return FileContentHandler
185 public FileContentHandler createLicenseArtifacts(String vspId, String vlmId, Version vlmVersion,
186 List<String> featureGroups, String user) {
189 mdcDataDebugMessage.debugEntryMessage("VSP Id", vspId);
191 FileContentHandler artifacts = new FileContentHandler();
192 String vendorName = VendorLicenseArtifactsServiceUtils.getVendorName(vlmId, user);
194 artifacts.addFile(VNF_ARTIFACT_NAME_WITH_PATH,
195 createVnfArtifact(vspId, vlmId, vlmVersion, vendorName, featureGroups, user));
196 artifacts.addFile(VENDOR_LICENSE_MODEL_ARTIFACT_NAME_WITH_PATH,
197 createVendorLicenseArtifact(vlmId, vendorName, user));
199 mdcDataDebugMessage.debugExitMessage("VSP Id", vspId);