push addional code
[sdc.git] / openecomp-be / lib / openecomp-sdc-vendor-license-lib / openecomp-sdc-vendor-license-core / src / main / java / org / openecomp / sdc / vendorlicense / licenseartifacts / impl / VendorLicenseArtifactsServiceImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.vendorlicense.licenseartifacts.impl;
22
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;
42
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;
48 import java.util.Map;
49 import java.util.Set;
50 import java.util.stream.Collectors;
51
52 public class VendorLicenseArtifactsServiceImpl implements VendorLicenseArtifactsService {
53
54   private static final VendorLicenseFacade vendorLicenseFacade = VendorLicenseFacadeFactory
55           .getInstance().createInterface();
56   private static final HealingService healingService = HealingServiceFactory
57           .getInstance().createInterface();
58
59   /**
60    * Create License Artifacts.
61    *
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
68    */
69   public FileContentHandler createLicenseArtifacts(String vspId, String vlmId,
70                                                    Version vlmVersion,
71                                                    List<String> featureGroups, String user) {
72     FileContentHandler artifacts = new FileContentHandler();
73     String vendorName = getVendorName(vlmId, user);
74
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));
79
80     return artifacts;
81   }
82
83   static byte[] createVnfArtifact(
84       String vspId, String vlmId, Version vlmVersion,
85       String vendorName, List<String> featureGroups,
86       String user) {
87     VnfLicenseArtifact artifact = new VnfLicenseArtifact();
88
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();
97
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);
109     }
110
111     return artifact.toXml().getBytes();
112   }
113
114
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<>();
120
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));
127     }
128
129
130     entitlementPoolEntities = healEPs(user, filterChangedEntities(entitlementPoolEntities));
131     licenseKeyGroupEntities = healLkgs(user, filterChangedEntities(licenseKeyGroupEntities));
132
133     vendorLicenseArtifact.setEntitlementPoolEntities(entitlementPoolEntities);
134     vendorLicenseArtifact.setLicenseKeyGroupEntities(licenseKeyGroupEntities);
135     return vendorLicenseArtifact.toXml().getBytes();
136   }
137
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<>();
143
144     for (String epId : entitiesById.keySet()) {
145       Collection<VersionableEntity> versionableEntitiesForId = entitiesById.get(epId);
146       for (VersionableEntity ep : versionableEntitiesForId) {
147         entitiesByVersionUuId.put(ep.getVersionUuId(), ep);
148       }
149     }
150
151     changedOnly.addAll(entitiesByVersionUuId.values());
152
153     return changedOnly;
154   }
155
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);
161     }
162     return mappedById;
163   }
164
165
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));
171     }
172
173     return healed;
174   }
175
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));
181     }
182
183     return healed;
184   }
185
186   private static List<Version> getFinalVersionsForVlm(String vlmId) {
187     VersionInfo versionInfo = vendorLicenseFacade
188         .getVersionInfo(vlmId, VersionableEntityAction.Read, "");
189     return versionInfo.getFinalVersions();
190
191   }
192
193
194   private static String getVendorName(String vendorLicenseModelId, String user) {
195     return vendorLicenseFacade
196         .getVendorLicenseModel(vendorLicenseModelId, null, user)
197         .getVendorLicenseModel().getVendorName();
198   }
199
200 }