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