[SDC] Onboarding 1710 rebase.
[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.openecomp.core.utilities.file.FileContentHandler;
24 import org.openecomp.sdc.common.utils.CommonUtil;
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.dao.types.LimitEntity;
32 import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacade;
33 import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacadeFactory;
34 import org.openecomp.sdc.vendorlicense.healing.HealingService;
35 import org.openecomp.sdc.vendorlicense.licenseartifacts.VendorLicenseArtifactsService;
36 import org.openecomp.sdc.vendorlicense.licenseartifacts.impl.types.VendorLicenseArtifact;
37 import org.openecomp.sdc.vendorlicense.licenseartifacts.impl.types.VnfLicenseArtifact;
38 import org.openecomp.sdc.vendorlicense.licenseartifacts.impl.util.VendorLicenseArtifactsServiceUtils;
39 import org.openecomp.sdc.versioning.dao.types.Version;
40
41 import java.util.Collection;
42 import java.util.HashSet;
43 import java.util.List;
44 import java.util.Set;
45 import java.util.stream.Collectors;
46
47 import static org.openecomp.sdc.vendorlicense.VendorLicenseConstants.VENDOR_LICENSE_MODEL_ARTIFACT_NAME_WITH_PATH;
48 import static org.openecomp.sdc.vendorlicense.VendorLicenseConstants.VNF_ARTIFACT_NAME_WITH_PATH;
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 MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
57
58
59   static byte[] createVnfArtifact(String vspId, String vlmId, Version vlmVersion, String vendorName,
60                                   List<String> featureGroups, String user) {
61
62
63     mdcDataDebugMessage.debugEntryMessage("VLM name", vendorName);
64
65     VnfLicenseArtifact artifact = new VnfLicenseArtifact();
66
67     artifact.setVspId(vspId);
68     artifact.setVendorName(vendorName);
69     if(featureGroups != null) {
70       for (String featureGroupId : featureGroups) {
71         FeatureGroupModel featureGroupModel = vendorLicenseFacade
72             .getFeatureGroupModel(new FeatureGroupEntity(vlmId, vlmVersion, featureGroupId), user);
73         Set<EntitlementPoolEntity> entitlementPoolEntities = featureGroupModel.getEntitlementPools();
74         for(EntitlementPoolEntity entitlementPoolEntity : entitlementPoolEntities){
75           entitlementPoolEntity.setLimits(vendorLicenseFacade.listLimits(vlmId, vlmVersion,
76               entitlementPoolEntity.getId(), user));
77         }
78         Set<LicenseKeyGroupEntity> licenseKeyGroupEntities = featureGroupModel.getLicenseKeyGroups();
79         for(LicenseKeyGroupEntity licenseKeyGroupEntity : licenseKeyGroupEntities){
80           licenseKeyGroupEntity.setLimits(vendorLicenseFacade.listLimits(vlmId, vlmVersion,
81               licenseKeyGroupEntity.getId(), user));
82         }
83
84         featureGroupModel.setEntitlementPools(entitlementPoolEntities.stream().map(
85             entitlementPoolEntity -> (EntitlementPoolEntity) healingService
86                 .heal(entitlementPoolEntity, user)).collect(Collectors.toSet()));
87         featureGroupModel.setLicenseKeyGroups(licenseKeyGroupEntities.stream().map(
88             licenseKeyGroupEntity -> (LicenseKeyGroupEntity) healingService
89                 .heal(licenseKeyGroupEntity, user)).collect(Collectors.toSet()));
90         artifact.getFeatureGroups().add(featureGroupModel);
91       }
92     }
93
94     mdcDataDebugMessage.debugExitMessage("VLM name", vendorName);
95     return artifact.toXml().getBytes();
96   }
97
98   static byte[] createVendorLicenseArtifact(String vlmId, String vendorName, String user) {
99
100
101     mdcDataDebugMessage.debugEntryMessage("VLM name", vendorName);
102
103     VendorLicenseArtifact vendorLicenseArtifact = new VendorLicenseArtifact();
104     vendorLicenseArtifact.setVendorName(vendorName);
105     Set<EntitlementPoolEntity> entitlementPoolEntities = new HashSet<>();
106     Set<LicenseKeyGroupEntity> licenseKeyGroupEntities = new HashSet<>();
107
108     List<Version> finalVersions = VendorLicenseArtifactsServiceUtils.getFinalVersionsForVlm(vlmId);
109     for (Version finalVersion : finalVersions) {
110       Collection<EntitlementPoolEntity> coll = vendorLicenseFacade.listEntitlementPools(vlmId,
111           finalVersion, user);
112       for(EntitlementPoolEntity entitlementPoolEntity : coll){
113         entitlementPoolEntity.setLimits(vendorLicenseFacade.listLimits(vlmId, finalVersion,
114             entitlementPoolEntity.getId(), user));
115       }
116       entitlementPoolEntities.addAll(coll);
117
118       Collection<LicenseKeyGroupEntity> coll2 = vendorLicenseFacade.listLicenseKeyGroups(vlmId,
119           finalVersion, user);
120       for(LicenseKeyGroupEntity licenseKeyGroupEntity : coll2){
121         licenseKeyGroupEntity.setLimits(vendorLicenseFacade.listLimits(vlmId, finalVersion,
122             licenseKeyGroupEntity.getId(), user));
123       }
124       licenseKeyGroupEntities.addAll(coll2);
125     }
126
127     entitlementPoolEntities = VendorLicenseArtifactsServiceUtils
128         .healEPs(user,
129             VendorLicenseArtifactsServiceUtils.filterChangedEntities(entitlementPoolEntities));
130     licenseKeyGroupEntities = VendorLicenseArtifactsServiceUtils
131         .healLkgs(user,
132             VendorLicenseArtifactsServiceUtils.filterChangedEntities(licenseKeyGroupEntities));
133
134     vendorLicenseArtifact.setEntitlementPoolEntities(entitlementPoolEntities);
135     vendorLicenseArtifact.setLicenseKeyGroupEntities(licenseKeyGroupEntities);
136
137     mdcDataDebugMessage.debugExitMessage("VLM name", vendorName);
138     return vendorLicenseArtifact.toXml().getBytes();
139   }
140
141
142   /**
143    * Create License Artifacts.
144    * @param vspId vspId
145    * @param vlmId vlmId
146    * @param vlmVersion vlmVersion
147    * @param featureGroups featureGroups
148    * @param user user
149    * @return FileContentHandler
150    */
151   public FileContentHandler createLicenseArtifacts(String vspId, String vlmId, Version vlmVersion,
152                                                    List<String> featureGroups, String user) {
153
154
155     mdcDataDebugMessage.debugEntryMessage("VSP Id", vspId);
156
157     FileContentHandler artifacts = new FileContentHandler();
158     String vendorName = VendorLicenseArtifactsServiceUtils.getVendorName(vlmId, user);
159
160     artifacts.addFile(VNF_ARTIFACT_NAME_WITH_PATH,
161         createVnfArtifact(vspId, vlmId, vlmVersion, vendorName, featureGroups, user));
162     artifacts.addFile(VENDOR_LICENSE_MODEL_ARTIFACT_NAME_WITH_PATH,
163         createVendorLicenseArtifact(vlmId, vendorName, user));
164
165     mdcDataDebugMessage.debugExitMessage("VSP Id", vspId);
166
167     return artifacts;
168   }
169
170 }