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