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.collections4.MultiValuedMap;
24 import org.apache.commons.collections4.multimap.ArrayListValuedHashMap;
25 import org.openecomp.core.utilities.file.FileContentHandler;
26 import org.openecomp.sdc.vendorlicense.HealingServiceFactory;
27 import org.openecomp.sdc.vendorlicense.VendorLicenseConstants;
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.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.versioning.dao.types.Version;
39 import org.openecomp.sdc.versioning.dao.types.VersionableEntity;
40 import org.openecomp.sdc.versioning.types.VersionInfo;
41 import org.openecomp.sdc.versioning.types.VersionableEntityAction;
43 import java.util.ArrayList;
44 import java.util.Collection;
45 import java.util.HashMap;
46 import java.util.HashSet;
47 import java.util.List;
50 import java.util.stream.Collectors;
52 public class VendorLicenseArtifactsServiceImpl implements VendorLicenseArtifactsService {
54 private static final VendorLicenseFacade vendorLicenseFacade = VendorLicenseFacadeFactory
55 .getInstance().createInterface();
56 private static final HealingService healingService = HealingServiceFactory
57 .getInstance().createInterface();
60 * Create License Artifacts.
62 * @param vspId the vsp id
63 * @param vlmId the vlm id
64 * @param vlmVersion the vlm version
65 * @param featureGroups the feature groups
66 * @param user the user
67 * @return FileContentHandler
69 public FileContentHandler createLicenseArtifacts(String vspId, String vlmId,
71 List<String> featureGroups, String user) {
72 FileContentHandler artifacts = new FileContentHandler();
73 String vendorName = getVendorName(vlmId, user);
75 artifacts.addFile(VendorLicenseConstants.VNF_ARTIFACT_NAME_WITH_PATH,
76 createVnfArtifact(vspId, vlmId, vlmVersion, vendorName, featureGroups, user));
77 artifacts.addFile(VendorLicenseConstants.VENDOR_LICENSE_MODEL_ARTIFACT_NAME_WITH_PATH,
78 createVendorLicenseArtifact(vlmId, vendorName, user));
83 static byte[] createVnfArtifact(
84 String vspId, String vlmId, Version vlmVersion,
85 String vendorName, List<String> featureGroups,
87 VnfLicenseArtifact artifact = new VnfLicenseArtifact();
89 artifact.setVspId(vspId);
90 artifact.setVendorName(vendorName);
91 for (String featureGroupId : featureGroups) {
92 FeatureGroupModel featureGroupModel =
93 vendorLicenseFacade.getFeatureGroupModel(new FeatureGroupEntity(
94 vlmId, vlmVersion, featureGroupId), user);
95 Set<EntitlementPoolEntity> entitlementPoolEntities = featureGroupModel.getEntitlementPools();
96 Set<LicenseKeyGroupEntity> licenseKeyGroupEntities = featureGroupModel.getLicenseKeyGroups();
98 featureGroupModel.setEntitlementPools(
99 entitlementPoolEntities.stream()
100 .map(entitlementPoolEntity -> (EntitlementPoolEntity) healingService
101 .heal(entitlementPoolEntity, user))
102 .collect(Collectors.toSet()));
103 featureGroupModel.setLicenseKeyGroups(
104 licenseKeyGroupEntities.stream()
105 .map(licenseKeyGroupEntity -> (LicenseKeyGroupEntity) healingService
106 .heal(licenseKeyGroupEntity, user))
107 .collect(Collectors.toSet()));
108 artifact.getFeatureGroups().add(featureGroupModel);
111 return artifact.toXml().getBytes();
115 static byte[] createVendorLicenseArtifact(String vlmId, String vendorName, String user) {
116 VendorLicenseArtifact vendorLicenseArtifact = new VendorLicenseArtifact();
117 vendorLicenseArtifact.setVendorName(vendorName);
118 Set<EntitlementPoolEntity> entitlementPoolEntities = new HashSet<>();
119 Set<LicenseKeyGroupEntity> licenseKeyGroupEntities = new HashSet<>();
121 List<Version> finalVersions = getFinalVersionsForVlm(vlmId);
122 for (Version finalVersion : finalVersions) {
123 entitlementPoolEntities.addAll(
124 vendorLicenseFacade.listEntitlementPools(vlmId, finalVersion, user));
125 licenseKeyGroupEntities.addAll(
126 vendorLicenseFacade.listLicenseKeyGroups(vlmId, finalVersion, user));
130 entitlementPoolEntities = healEPs(user, filterChangedEntities(entitlementPoolEntities));
131 licenseKeyGroupEntities = healLkgs(user, filterChangedEntities(licenseKeyGroupEntities));
133 vendorLicenseArtifact.setEntitlementPoolEntities(entitlementPoolEntities);
134 vendorLicenseArtifact.setLicenseKeyGroupEntities(licenseKeyGroupEntities);
135 return vendorLicenseArtifact.toXml().getBytes();
138 private static List<VersionableEntity> filterChangedEntities(
139 Collection<? extends VersionableEntity> versionableEntities) {
140 MultiValuedMap<String, VersionableEntity> entitiesById = mapById(versionableEntities);
141 Map<String, VersionableEntity> entitiesByVersionUuId = new HashMap<>();
142 List<VersionableEntity> changedOnly = new ArrayList<>();
144 for (String epId : entitiesById.keySet()) {
145 Collection<VersionableEntity> versionableEntitiesForId = entitiesById.get(epId);
146 for (VersionableEntity ep : versionableEntitiesForId) {
147 entitiesByVersionUuId.put(ep.getVersionUuId(), ep);
151 changedOnly.addAll(entitiesByVersionUuId.values());
156 private static MultiValuedMap<String, VersionableEntity> mapById(
157 Collection<? extends VersionableEntity> versionableEntities) {
158 MultiValuedMap<String, VersionableEntity> mappedById = new ArrayListValuedHashMap<>();
159 for (VersionableEntity ve : versionableEntities) {
160 mappedById.put(ve.getId(), ve);
166 private static Set<LicenseKeyGroupEntity> healLkgs(
167 String user, Collection<? extends VersionableEntity> licenseKeyGroupEntities) {
168 Set<LicenseKeyGroupEntity> healed = new HashSet<>();
169 for (VersionableEntity licenseKeyGroupEntity : licenseKeyGroupEntities) {
170 healed.add((LicenseKeyGroupEntity) healingService.heal(licenseKeyGroupEntity, user));
176 private static Set<EntitlementPoolEntity> healEPs(
177 String user, Collection<? extends VersionableEntity> entitlementPoolEntities) {
178 Set<EntitlementPoolEntity> healed = new HashSet<>();
179 for (VersionableEntity entitlementPoolEntity : entitlementPoolEntities) {
180 healed.add((EntitlementPoolEntity) healingService.heal(entitlementPoolEntity, user));
186 private static List<Version> getFinalVersionsForVlm(String vlmId) {
187 VersionInfo versionInfo = vendorLicenseFacade
188 .getVersionInfo(vlmId, VersionableEntityAction.Read, "");
189 return versionInfo.getFinalVersions();
194 private static String getVendorName(String vendorLicenseModelId, String user) {
195 return vendorLicenseFacade
196 .getVendorLicenseModel(vendorLicenseModelId, null, user)
197 .getVendorLicenseModel().getVendorName();