282b4e6743abfa92399514fa252d16fc23f96641
[sdc.git] /
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.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;
38
39 import java.util.Collection;
40 import java.util.HashSet;
41 import java.util.List;
42 import java.util.Optional;
43 import java.util.Set;
44 import java.util.stream.Collectors;
45
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;
54
55 public class VendorLicenseArtifactsServiceImpl implements VendorLicenseArtifactsService {
56
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();
62
63
64   private static byte[] createVnfArtifact(String vspId, String vlmId, Version vlmVersion,
65                                           String vendorName,
66                                           List<String> featureGroups, String user) {
67
68
69     mdcDataDebugMessage.debugEntryMessage("VLM name", vendorName);
70
71     VnfLicenseArtifact artifact = new VnfLicenseArtifact();
72
73     artifact.setVspId(vspId);
74     artifact.setVendorName(vendorName);
75     if (featureGroups != null) {
76       for (String featureGroupId : featureGroups) {
77         FeatureGroupModel featureGroupModel = vendorLicenseFacade
78                 .getFeatureGroupModel(new FeatureGroupEntity(vlmId, vlmVersion, featureGroupId), user);
79         Set<EntitlementPoolEntity> entitlementPoolEntities =
80                 featureGroupModel.getEntitlementPools();
81         for (EntitlementPoolEntity entitlementPoolEntity : entitlementPoolEntities) {
82           entitlementPoolEntity.setLimits(vendorLicenseFacade.listLimits(vlmId, vlmVersion,
83                   entitlementPoolEntity.getId(), user));
84           entitlementPoolEntity.setManufacturerReferenceNumber(featureGroupModel.
85                   getEntityManufacturerReferenceNumber());
86         }
87         Set<LicenseKeyGroupEntity> licenseKeyGroupEntities =
88                 featureGroupModel.getLicenseKeyGroups();
89         for (LicenseKeyGroupEntity licenseKeyGroupEntity : licenseKeyGroupEntities) {
90           licenseKeyGroupEntity.setLimits(vendorLicenseFacade.listLimits(vlmId, vlmVersion,
91                   licenseKeyGroupEntity.getId(), user));
92           licenseKeyGroupEntity.setManufacturerReferenceNumber(featureGroupModel.
93                   getEntityManufacturerReferenceNumber());
94         }
95
96         featureGroupModel.setEntitlementPools(entitlementPoolEntities.stream().map(
97                 entitlementPoolEntity -> (EntitlementPoolEntity) healingService
98                         .heal(entitlementPoolEntity, user)).collect(Collectors.toSet()));
99         featureGroupModel.setLicenseKeyGroups(licenseKeyGroupEntities.stream().map(
100                 licenseKeyGroupEntity -> (LicenseKeyGroupEntity) healingService
101                         .heal(licenseKeyGroupEntity, user)).collect(Collectors.toSet()));
102         artifact.getFeatureGroups().add(featureGroupModel);
103       }
104     }
105
106     mdcDataDebugMessage.debugExitMessage("VLM name", vendorName);
107     return artifact.toXml().getBytes();
108   }
109
110   private static byte[] createVendorLicenseArtifact(String vlmId, String vendorName, String user) {
111
112
113     mdcDataDebugMessage.debugEntryMessage("VLM name", vendorName);
114
115     VendorLicenseArtifact vendorLicenseArtifact = new VendorLicenseArtifact();
116     vendorLicenseArtifact.setVendorName(vendorName);
117     Set<EntitlementPoolEntity> entitlementPoolEntities = new HashSet<>();
118     Set<LicenseKeyGroupEntity> licenseKeyGroupEntities = new HashSet<>();
119
120     List<Version> finalVersions = getFinalVersionsForVlm(vlmId);
121     for (Version finalVersion : finalVersions) {
122       Collection<EntitlementPoolEntity> coll = vendorLicenseFacade.listEntitlementPools(vlmId,
123               finalVersion, user);
124       coll.stream().forEach(entitlementPoolEntity -> {
125         entitlementPoolEntity.setLimits(vendorLicenseFacade.listLimits(vlmId, finalVersion,
126                 entitlementPoolEntity.getId(), user));
127         Optional<String> manufacturerReferenceNumber = getFeatureGroupManufactureRefNumber
128                 (entitlementPoolEntity.getReferencingFeatureGroups(), vlmId, finalVersion, user);
129         manufacturerReferenceNumber
130                 .ifPresent(entitlementPoolEntity::setManufacturerReferenceNumber);
131       });
132
133       entitlementPoolEntities.addAll(coll);
134
135       Collection<LicenseKeyGroupEntity> coll2 = vendorLicenseFacade.listLicenseKeyGroups(vlmId,
136               finalVersion, user);
137
138       coll2.stream().forEach(licenseKeyGroupEntity -> {
139         licenseKeyGroupEntity.setLimits(vendorLicenseFacade.listLimits(vlmId, finalVersion,
140                 licenseKeyGroupEntity.getId(), user));
141         Optional<String> manufacturerReferenceNumber = getFeatureGroupManufactureRefNumber
142                 (licenseKeyGroupEntity.getReferencingFeatureGroups(), vlmId, finalVersion, user);
143         manufacturerReferenceNumber
144                 .ifPresent(licenseKeyGroupEntity::setManufacturerReferenceNumber);
145       });
146
147       licenseKeyGroupEntities.addAll(coll2);
148     }
149
150     entitlementPoolEntities =
151             healEPs(user, filterChangedEntities(prepareForFiltering(entitlementPoolEntities, user,
152                     true)));
153     licenseKeyGroupEntities =
154             healLkgs(user, filterChangedEntities(prepareForFiltering(licenseKeyGroupEntities, user,
155                     false)));
156     vendorLicenseArtifact.setEntitlementPoolEntities(entitlementPoolEntities);
157     vendorLicenseArtifact.setLicenseKeyGroupEntities(licenseKeyGroupEntities);
158
159     mdcDataDebugMessage.debugExitMessage("VLM name", vendorName);
160     return vendorLicenseArtifact.toXml().getBytes();
161   }
162
163   private static Optional<String> getFeatureGroupManufactureRefNumber(Set<String> featureGroupIds,
164                                                                       String vlmId,
165                                                                       Version finalVersion,
166                                                                       String user) {
167     String manufactureReferenceNumber = null;
168     if (CollectionUtils.isNotEmpty(featureGroupIds)) {
169       Object[] featureGroupIdsList = featureGroupIds.toArray();
170       if (featureGroupIdsList.length > 0) {
171         FeatureGroupEntity featureGroup =
172                 vendorLicenseFacade.getFeatureGroup(new FeatureGroupEntity(vlmId, finalVersion,
173                         featureGroupIdsList[0].toString()), user);
174         manufactureReferenceNumber = featureGroup != null ? featureGroup
175                 .getManufacturerReferenceNumber() : null;
176       }
177     }
178     return manufactureReferenceNumber != null ? Optional.of(manufactureReferenceNumber) :
179             Optional.empty();
180   }
181
182
183   /**
184    * Create License Artifacts.
185    *
186    * @param vspId         vspId
187    * @param vlmId         vlmId
188    * @param vlmVersion    vlmVersion
189    * @param featureGroups featureGroups
190    * @param user          user
191    * @return FileContentHandler
192    */
193   public FileContentHandler createLicenseArtifacts(String vspId, String vlmId, Version vlmVersion,
194                                                    List<String> featureGroups, String user) {
195
196
197     mdcDataDebugMessage.debugEntryMessage("VSP Id", vspId);
198
199     FileContentHandler artifacts = new FileContentHandler();
200     String vendorName = getVendorName(vlmId, user);
201
202     artifacts.addFile(VNF_ARTIFACT_NAME_WITH_PATH,
203             createVnfArtifact(vspId, vlmId, vlmVersion, vendorName, featureGroups, user));
204     artifacts.addFile(VENDOR_LICENSE_MODEL_ARTIFACT_NAME_WITH_PATH,
205             createVendorLicenseArtifact(vlmId, vendorName, user));
206
207     mdcDataDebugMessage.debugExitMessage("VSP Id", vspId);
208
209     return artifacts;
210   }
211
212 }