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