b9fd403db1bed30f9edf1380bdeceb22bde9aa74
[sdc.git] /
1 /*
2  * Copyright © 2016-2017 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.openecomp.sdc.vendorlicense.licenseartifacts.impl;
18
19 import org.apache.commons.collections.CollectionUtils;
20 import org.openecomp.core.utilities.file.FileContentHandler;
21 import org.openecomp.sdc.common.togglz.ToggleableFeature;
22 import org.openecomp.sdc.vendorlicense.HealingServiceFactory;
23 import org.openecomp.sdc.vendorlicense.dao.types.EntitlementPoolEntity;
24 import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity;
25 import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupModel;
26 import org.openecomp.sdc.vendorlicense.dao.types.LicenseKeyGroupEntity;
27 import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacade;
28 import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacadeFactory;
29 import org.openecomp.sdc.vendorlicense.healing.HealingService;
30 import org.openecomp.sdc.vendorlicense.licenseartifacts.VendorLicenseArtifactsService;
31 import org.openecomp.sdc.vendorlicense.licenseartifacts.impl.types.VendorLicenseArtifact;
32 import org.openecomp.sdc.vendorlicense.licenseartifacts.impl.types.VnfLicenseArtifact;
33 import org.openecomp.sdc.versioning.dao.types.Version;
34
35 import java.util.Collection;
36 import java.util.HashSet;
37 import java.util.List;
38 import java.util.Objects;
39 import java.util.Optional;
40 import java.util.Set;
41 import java.util.stream.Collectors;
42
43 import static org.openecomp.sdc.vendorlicense.VendorLicenseConstants.VENDOR_LICENSE_MODEL_ARTIFACT_NAME_WITH_PATH;
44 import static org.openecomp.sdc.vendorlicense.VendorLicenseConstants.VNF_ARTIFACT_NAME_WITH_PATH;
45 import static org.openecomp.sdc.vendorlicense.licenseartifacts.impl.util.VendorLicenseArtifactsServiceUtils.filterChangedEntities;
46 import static org.openecomp.sdc.vendorlicense.licenseartifacts.impl.util.VendorLicenseArtifactsServiceUtils.getFinalVersionsForVlm;
47 import static org.openecomp.sdc.vendorlicense.licenseartifacts.impl.util.VendorLicenseArtifactsServiceUtils.getVendorName;
48 import static org.openecomp.sdc.vendorlicense.licenseartifacts.impl.util.VendorLicenseArtifactsServiceUtils.healEPs;
49 import static org.openecomp.sdc.vendorlicense.licenseartifacts.impl.util.VendorLicenseArtifactsServiceUtils.healLkgs;
50 import static org.openecomp.sdc.vendorlicense.licenseartifacts.impl.util.VendorLicenseArtifactsServiceUtils.prepareForFiltering;
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 byte[] createVnfArtifact(String vspId, String vlmId, Version vlmVersion, String vendorName,
59                                   List<String> featureGroups) {
60     VnfLicenseArtifact artifact = new VnfLicenseArtifact();
61
62     artifact.setVspId(vspId);
63     artifact.setVendorName(vendorName);
64     if (featureGroups != null) {
65       for (String featureGroupId : featureGroups) {
66         FeatureGroupModel featureGroupModel = vendorLicenseFacade
67             .getFeatureGroupModel(new FeatureGroupEntity(vlmId, vlmVersion, featureGroupId));
68
69         updateEntitlementPools(vlmId, vlmVersion, featureGroupModel);
70         updateLicenseKeyGroupEntities(vlmId, vlmVersion, featureGroupModel);
71
72         artifact.getFeatureGroups().add(featureGroupModel);
73       }
74     }
75     return artifact.toXml().getBytes();
76   }
77
78   private static void updateEntitlementPools(String vlmId, Version vlmVersion, FeatureGroupModel featureGroupModel) {
79
80     Set<EntitlementPoolEntity> entitlementPoolEntities =
81         featureGroupModel.getEntitlementPools();
82
83     for (EntitlementPoolEntity entitlementPoolEntity : entitlementPoolEntities) {
84       entitlementPoolEntity.setLimits(vendorLicenseFacade.listLimits(vlmId, vlmVersion,
85           entitlementPoolEntity.getId()));
86       String manuFacturerReferenceNumber = featureGroupModel.getEntityManufacturerReferenceNumber();
87       if (ToggleableFeature.MRN.isActive()) {
88         if (Objects.nonNull(manuFacturerReferenceNumber)
89             && !manuFacturerReferenceNumber.trim().isEmpty()) {
90           entitlementPoolEntity.setManufacturerReferenceNumber(manuFacturerReferenceNumber);
91         }
92       } else {
93         entitlementPoolEntity.setManufacturerReferenceNumber(manuFacturerReferenceNumber);
94       }
95     }
96     featureGroupModel.setEntitlementPools(entitlementPoolEntities.stream().map(
97         entitlementPoolEntity -> (EntitlementPoolEntity) healingService
98             .heal(entitlementPoolEntity)).collect(Collectors.toSet()));
99   }
100
101   private static void updateLicenseKeyGroupEntities(String vlmId, Version vlmVersion, FeatureGroupModel featureGroupModel) {
102
103     Set<LicenseKeyGroupEntity> licenseKeyGroupEntities =
104         featureGroupModel.getLicenseKeyGroups();
105     for (LicenseKeyGroupEntity licenseKeyGroupEntity : licenseKeyGroupEntities) {
106       licenseKeyGroupEntity.setLimits(vendorLicenseFacade.listLimits(vlmId, vlmVersion,
107           licenseKeyGroupEntity.getId()));
108       String manuFacturerReferenceNumber = featureGroupModel.getEntityManufacturerReferenceNumber();
109       if (ToggleableFeature.MRN.isActive()) {
110         if (Objects.nonNull(manuFacturerReferenceNumber)
111             && !manuFacturerReferenceNumber.trim().isEmpty()) {
112           licenseKeyGroupEntity.setManufacturerReferenceNumber(manuFacturerReferenceNumber);
113         }
114       } else {
115         licenseKeyGroupEntity.setManufacturerReferenceNumber(manuFacturerReferenceNumber);
116       }
117     }
118
119     featureGroupModel.setLicenseKeyGroups(licenseKeyGroupEntities.stream().map(
120         licenseKeyGroupEntity -> (LicenseKeyGroupEntity) healingService
121             .heal(licenseKeyGroupEntity)).collect(Collectors.toSet()));
122
123   }
124
125   private static byte[] createVendorLicenseArtifact(String vlmId, String vendorName) {
126     VendorLicenseArtifact vendorLicenseArtifact = new VendorLicenseArtifact();
127     vendorLicenseArtifact.setVendorName(vendorName);
128     Set<EntitlementPoolEntity> entitlementPoolEntities = new HashSet<>();
129     Set<LicenseKeyGroupEntity> licenseKeyGroupEntities = new HashSet<>();
130
131     List<Version> finalVersions = getFinalVersionsForVlm(vlmId);
132     for (Version finalVersion : finalVersions) {
133       Collection<EntitlementPoolEntity> eps =
134           vendorLicenseFacade.listEntitlementPools(vlmId, finalVersion);
135       eps.forEach(entitlementPoolEntity -> {
136         entitlementPoolEntity.setLimits(vendorLicenseFacade.listLimits(vlmId, finalVersion,
137             entitlementPoolEntity.getId()));
138         Optional<String> manufacturerReferenceNumber = getFeatureGroupManufactureRefNumber
139             (entitlementPoolEntity.getReferencingFeatureGroups(), vlmId, finalVersion);
140         manufacturerReferenceNumber
141             .ifPresent(entitlementPoolEntity::setManufacturerReferenceNumber);
142       });
143       entitlementPoolEntities.addAll(eps);
144
145       Collection<LicenseKeyGroupEntity> lkgs =
146           vendorLicenseFacade.listLicenseKeyGroups(vlmId, finalVersion);
147
148       lkgs.forEach(licenseKeyGroupEntity -> {
149         licenseKeyGroupEntity.setLimits(vendorLicenseFacade.listLimits(vlmId, finalVersion,
150             licenseKeyGroupEntity.getId()));
151         Optional<String> manufacturerReferenceNumber = getFeatureGroupManufactureRefNumber
152             (licenseKeyGroupEntity.getReferencingFeatureGroups(), vlmId, finalVersion);
153         manufacturerReferenceNumber
154             .ifPresent(licenseKeyGroupEntity::setManufacturerReferenceNumber);
155       });
156       licenseKeyGroupEntities.addAll(lkgs);
157     }
158
159
160     entitlementPoolEntities =
161         healEPs(filterChangedEntities(prepareForFiltering(entitlementPoolEntities, true)));
162     licenseKeyGroupEntities =
163         healLkgs(filterChangedEntities(prepareForFiltering(licenseKeyGroupEntities, false)));
164     vendorLicenseArtifact.setEntitlementPoolEntities(entitlementPoolEntities);
165     vendorLicenseArtifact.setLicenseKeyGroupEntities(licenseKeyGroupEntities);
166     return vendorLicenseArtifact.toXml().getBytes();
167   }
168
169   private static Optional<String> getFeatureGroupManufactureRefNumber(Set<String> featureGroupIds,
170                                                                       String vlmId,
171                                                                       Version finalVersion) {
172     String manufactureReferenceNumber = null;
173     if (CollectionUtils.isNotEmpty(featureGroupIds)) {
174       Object[] featureGroupIdsList = featureGroupIds.toArray();
175       if (featureGroupIdsList.length > 0) {
176         FeatureGroupEntity featureGroup =
177             vendorLicenseFacade.getFeatureGroup(new FeatureGroupEntity(vlmId, finalVersion,
178                 featureGroupIdsList[0].toString()));
179         manufactureReferenceNumber = featureGroup != null ? featureGroup
180             .getManufacturerReferenceNumber() : null;
181       }
182     }
183     return manufactureReferenceNumber != null ? Optional.of(manufactureReferenceNumber) :
184         Optional.empty();
185   }
186
187
188   /**
189    * Create License Artifacts.
190    *
191    * @param vspId         vspId
192    * @param vlmId         vlmId
193    * @param vlmVersion    vlmVersion
194    * @param featureGroups featureGroups
195    * @return FileContentHandler
196    */
197   @Override
198   public FileContentHandler createLicenseArtifacts(String vspId, String vlmId, Version vlmVersion,
199                                                    List<String> featureGroups) {
200     FileContentHandler artifacts = new FileContentHandler();
201     String vendorName = getVendorName(vlmId);
202
203     artifacts.addFile(VNF_ARTIFACT_NAME_WITH_PATH,
204         createVnfArtifact(vspId, vlmId, vlmVersion, vendorName, featureGroups));
205     artifacts.addFile(VENDOR_LICENSE_MODEL_ARTIFACT_NAME_WITH_PATH,
206         createVendorLicenseArtifact(vlmId, vendorName));
207     return artifacts;
208   }
209
210 }