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