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, String vendorName,
65 List<String> featureGroups) {
68 mdcDataDebugMessage.debugEntryMessage("VLM name", vendorName);
70 VnfLicenseArtifact artifact = new VnfLicenseArtifact();
72 artifact.setVspId(vspId);
73 artifact.setVendorName(vendorName);
74 if (featureGroups != null) {
75 for (String featureGroupId : featureGroups) {
76 FeatureGroupModel featureGroupModel = vendorLicenseFacade
77 .getFeatureGroupModel(new FeatureGroupEntity(vlmId, vlmVersion, featureGroupId));
78 Set<EntitlementPoolEntity> entitlementPoolEntities =
79 featureGroupModel.getEntitlementPools();
80 for (EntitlementPoolEntity entitlementPoolEntity : entitlementPoolEntities) {
81 entitlementPoolEntity.setLimits(vendorLicenseFacade.listLimits(vlmId, vlmVersion,
82 entitlementPoolEntity.getId()));
83 entitlementPoolEntity.setManufacturerReferenceNumber(featureGroupModel.
84 getEntityManufacturerReferenceNumber());
87 Set<LicenseKeyGroupEntity> licenseKeyGroupEntities =
88 featureGroupModel.getLicenseKeyGroups();
89 for (LicenseKeyGroupEntity licenseKeyGroupEntity : licenseKeyGroupEntities) {
90 licenseKeyGroupEntity.setLimits(vendorLicenseFacade.listLimits(vlmId, vlmVersion,
91 licenseKeyGroupEntity.getId()));
92 licenseKeyGroupEntity.setManufacturerReferenceNumber(featureGroupModel.
93 getEntityManufacturerReferenceNumber());
96 featureGroupModel.setEntitlementPools(entitlementPoolEntities.stream().map(
97 entitlementPoolEntity -> (EntitlementPoolEntity) healingService
98 .heal(entitlementPoolEntity)).collect(Collectors.toSet()));
99 featureGroupModel.setLicenseKeyGroups(licenseKeyGroupEntities.stream().map(
100 licenseKeyGroupEntity -> (LicenseKeyGroupEntity) healingService
101 .heal(licenseKeyGroupEntity)).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) {
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> eps =
123 vendorLicenseFacade.listEntitlementPools(vlmId, finalVersion);
124 eps.forEach(entitlementPoolEntity -> {
125 entitlementPoolEntity.setLimits(vendorLicenseFacade.listLimits(vlmId, finalVersion,
126 entitlementPoolEntity.getId()));
127 Optional<String> manufacturerReferenceNumber = getFeatureGroupManufactureRefNumber
128 (entitlementPoolEntity.getReferencingFeatureGroups(), vlmId, finalVersion);
129 manufacturerReferenceNumber
130 .ifPresent(entitlementPoolEntity::setManufacturerReferenceNumber);
132 entitlementPoolEntities.addAll(eps);
134 Collection<LicenseKeyGroupEntity> lkgs =
135 vendorLicenseFacade.listLicenseKeyGroups(vlmId, finalVersion);
137 lkgs.forEach(licenseKeyGroupEntity -> {
138 licenseKeyGroupEntity.setLimits(vendorLicenseFacade.listLimits(vlmId, finalVersion,
139 licenseKeyGroupEntity.getId()));
140 Optional<String> manufacturerReferenceNumber = getFeatureGroupManufactureRefNumber
141 (licenseKeyGroupEntity.getReferencingFeatureGroups(), vlmId, finalVersion);
142 manufacturerReferenceNumber
143 .ifPresent(licenseKeyGroupEntity::setManufacturerReferenceNumber);
145 licenseKeyGroupEntities.addAll(lkgs);
149 entitlementPoolEntities =
150 healEPs(filterChangedEntities(prepareForFiltering(entitlementPoolEntities, true)));
151 licenseKeyGroupEntities =
152 healLkgs(filterChangedEntities(prepareForFiltering(licenseKeyGroupEntities, false)));
153 vendorLicenseArtifact.setEntitlementPoolEntities(entitlementPoolEntities);
154 vendorLicenseArtifact.setLicenseKeyGroupEntities(licenseKeyGroupEntities);
156 mdcDataDebugMessage.debugExitMessage("VLM name", vendorName);
157 return vendorLicenseArtifact.toXml().getBytes();
160 private static Optional<String> getFeatureGroupManufactureRefNumber(Set<String> featureGroupIds,
162 Version finalVersion) {
163 String manufactureReferenceNumber = null;
164 if (CollectionUtils.isNotEmpty(featureGroupIds)) {
165 Object[] featureGroupIdsList = featureGroupIds.toArray();
166 if (featureGroupIdsList.length > 0) {
167 FeatureGroupEntity featureGroup =
168 vendorLicenseFacade.getFeatureGroup(new FeatureGroupEntity(vlmId, finalVersion,
169 featureGroupIdsList[0].toString()));
170 manufactureReferenceNumber = featureGroup != null ? featureGroup
171 .getManufacturerReferenceNumber() : null;
174 return manufactureReferenceNumber != null ? Optional.of(manufactureReferenceNumber) :
180 * Create License Artifacts.
184 * @param vlmVersion vlmVersion
185 * @param featureGroups featureGroups
186 * @return FileContentHandler
188 public FileContentHandler createLicenseArtifacts(String vspId, String vlmId, Version vlmVersion,
189 List<String> featureGroups) {
192 mdcDataDebugMessage.debugEntryMessage("VSP Id", vspId);
194 FileContentHandler artifacts = new FileContentHandler();
195 String vendorName = getVendorName(vlmId);
197 artifacts.addFile(VNF_ARTIFACT_NAME_WITH_PATH,
198 createVnfArtifact(vspId, vlmId, vlmVersion, vendorName, featureGroups));
199 artifacts.addFile(VENDOR_LICENSE_MODEL_ARTIFACT_NAME_WITH_PATH,
200 createVendorLicenseArtifact(vlmId, vendorName));
202 mdcDataDebugMessage.debugExitMessage("VSP Id", vspId);