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.openecomp.core.utilities.file.FileContentHandler;
24 import org.openecomp.sdc.common.utils.CommonUtil;
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.dao.types.LimitEntity;
32 import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacade;
33 import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacadeFactory;
34 import org.openecomp.sdc.vendorlicense.healing.HealingService;
35 import org.openecomp.sdc.vendorlicense.licenseartifacts.VendorLicenseArtifactsService;
36 import org.openecomp.sdc.vendorlicense.licenseartifacts.impl.types.VendorLicenseArtifact;
37 import org.openecomp.sdc.vendorlicense.licenseartifacts.impl.types.VnfLicenseArtifact;
38 import org.openecomp.sdc.vendorlicense.licenseartifacts.impl.util.VendorLicenseArtifactsServiceUtils;
39 import org.openecomp.sdc.versioning.dao.types.Version;
41 import java.util.Collection;
42 import java.util.HashSet;
43 import java.util.List;
45 import java.util.stream.Collectors;
47 import static org.openecomp.sdc.vendorlicense.VendorLicenseConstants.VENDOR_LICENSE_MODEL_ARTIFACT_NAME_WITH_PATH;
48 import static org.openecomp.sdc.vendorlicense.VendorLicenseConstants.VNF_ARTIFACT_NAME_WITH_PATH;
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 MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
59 static byte[] createVnfArtifact(String vspId, String vlmId, Version vlmVersion, String vendorName,
60 List<String> featureGroups, String user) {
63 mdcDataDebugMessage.debugEntryMessage("VLM name", vendorName);
65 VnfLicenseArtifact artifact = new VnfLicenseArtifact();
67 artifact.setVspId(vspId);
68 artifact.setVendorName(vendorName);
69 if(featureGroups != null) {
70 for (String featureGroupId : featureGroups) {
71 FeatureGroupModel featureGroupModel = vendorLicenseFacade
72 .getFeatureGroupModel(new FeatureGroupEntity(vlmId, vlmVersion, featureGroupId), user);
73 Set<EntitlementPoolEntity> entitlementPoolEntities = featureGroupModel.getEntitlementPools();
74 for(EntitlementPoolEntity entitlementPoolEntity : entitlementPoolEntities){
75 entitlementPoolEntity.setLimits(vendorLicenseFacade.listLimits(vlmId, vlmVersion,
76 entitlementPoolEntity.getId(), user));
78 Set<LicenseKeyGroupEntity> licenseKeyGroupEntities = featureGroupModel.getLicenseKeyGroups();
79 for(LicenseKeyGroupEntity licenseKeyGroupEntity : licenseKeyGroupEntities){
80 licenseKeyGroupEntity.setLimits(vendorLicenseFacade.listLimits(vlmId, vlmVersion,
81 licenseKeyGroupEntity.getId(), user));
84 featureGroupModel.setEntitlementPools(entitlementPoolEntities.stream().map(
85 entitlementPoolEntity -> (EntitlementPoolEntity) healingService
86 .heal(entitlementPoolEntity, user)).collect(Collectors.toSet()));
87 featureGroupModel.setLicenseKeyGroups(licenseKeyGroupEntities.stream().map(
88 licenseKeyGroupEntity -> (LicenseKeyGroupEntity) healingService
89 .heal(licenseKeyGroupEntity, user)).collect(Collectors.toSet()));
90 artifact.getFeatureGroups().add(featureGroupModel);
94 mdcDataDebugMessage.debugExitMessage("VLM name", vendorName);
95 return artifact.toXml().getBytes();
98 static byte[] createVendorLicenseArtifact(String vlmId, String vendorName, String user) {
101 mdcDataDebugMessage.debugEntryMessage("VLM name", vendorName);
103 VendorLicenseArtifact vendorLicenseArtifact = new VendorLicenseArtifact();
104 vendorLicenseArtifact.setVendorName(vendorName);
105 Set<EntitlementPoolEntity> entitlementPoolEntities = new HashSet<>();
106 Set<LicenseKeyGroupEntity> licenseKeyGroupEntities = new HashSet<>();
108 List<Version> finalVersions = VendorLicenseArtifactsServiceUtils.getFinalVersionsForVlm(vlmId);
109 for (Version finalVersion : finalVersions) {
110 Collection<EntitlementPoolEntity> coll = vendorLicenseFacade.listEntitlementPools(vlmId,
112 for(EntitlementPoolEntity entitlementPoolEntity : coll){
113 entitlementPoolEntity.setLimits(vendorLicenseFacade.listLimits(vlmId, finalVersion,
114 entitlementPoolEntity.getId(), user));
116 entitlementPoolEntities.addAll(coll);
118 Collection<LicenseKeyGroupEntity> coll2 = vendorLicenseFacade.listLicenseKeyGroups(vlmId,
120 for(LicenseKeyGroupEntity licenseKeyGroupEntity : coll2){
121 licenseKeyGroupEntity.setLimits(vendorLicenseFacade.listLimits(vlmId, finalVersion,
122 licenseKeyGroupEntity.getId(), user));
124 licenseKeyGroupEntities.addAll(coll2);
127 entitlementPoolEntities = VendorLicenseArtifactsServiceUtils
129 VendorLicenseArtifactsServiceUtils.filterChangedEntities(entitlementPoolEntities));
130 licenseKeyGroupEntities = VendorLicenseArtifactsServiceUtils
132 VendorLicenseArtifactsServiceUtils.filterChangedEntities(licenseKeyGroupEntities));
134 vendorLicenseArtifact.setEntitlementPoolEntities(entitlementPoolEntities);
135 vendorLicenseArtifact.setLicenseKeyGroupEntities(licenseKeyGroupEntities);
137 mdcDataDebugMessage.debugExitMessage("VLM name", vendorName);
138 return vendorLicenseArtifact.toXml().getBytes();
143 * Create License Artifacts.
146 * @param vlmVersion vlmVersion
147 * @param featureGroups featureGroups
149 * @return FileContentHandler
151 public FileContentHandler createLicenseArtifacts(String vspId, String vlmId, Version vlmVersion,
152 List<String> featureGroups, String user) {
155 mdcDataDebugMessage.debugEntryMessage("VSP Id", vspId);
157 FileContentHandler artifacts = new FileContentHandler();
158 String vendorName = VendorLicenseArtifactsServiceUtils.getVendorName(vlmId, user);
160 artifacts.addFile(VNF_ARTIFACT_NAME_WITH_PATH,
161 createVnfArtifact(vspId, vlmId, vlmVersion, vendorName, featureGroups, user));
162 artifacts.addFile(VENDOR_LICENSE_MODEL_ARTIFACT_NAME_WITH_PATH,
163 createVendorLicenseArtifact(vlmId, vendorName, user));
165 mdcDataDebugMessage.debugExitMessage("VSP Id", vspId);