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