580fc6c0e5242caebdd124ccbb76e4faf7ea1cf5
[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.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.vendorlicense.licenseartifacts.impl.util.VendorLicenseArtifactsServiceUtils;
38 import org.openecomp.sdc.versioning.dao.types.Version;
39
40 import java.util.HashSet;
41 import java.util.List;
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
48 public class VendorLicenseArtifactsServiceImpl implements VendorLicenseArtifactsService {
49
50   public static final VendorLicenseFacade vendorLicenseFacade =
51       VendorLicenseFacadeFactory.getInstance().createInterface();
52   public static final HealingService healingService =
53       HealingServiceFactory.getInstance().createInterface();
54   private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
55
56
57   static byte[] createVnfArtifact(String vspId, String vlmId, Version vlmVersion, String vendorName,
58                                   List<String> featureGroups, String user) {
59
60
61     mdcDataDebugMessage.debugEntryMessage("VLM name", vendorName);
62
63     VnfLicenseArtifact artifact = new VnfLicenseArtifact();
64
65     artifact.setVspId(vspId);
66     artifact.setVendorName(vendorName);
67     for (String featureGroupId : featureGroups) {
68       FeatureGroupModel featureGroupModel = vendorLicenseFacade
69           .getFeatureGroupModel(new FeatureGroupEntity(vlmId, vlmVersion, featureGroupId), user);
70       Set<EntitlementPoolEntity> entitlementPoolEntities = featureGroupModel.getEntitlementPools();
71       Set<LicenseKeyGroupEntity> licenseKeyGroupEntities = featureGroupModel.getLicenseKeyGroups();
72
73       featureGroupModel.setEntitlementPools(entitlementPoolEntities.stream().map(
74           entitlementPoolEntity -> (EntitlementPoolEntity) healingService
75               .heal(entitlementPoolEntity, user)).collect(Collectors.toSet()));
76       featureGroupModel.setLicenseKeyGroups(licenseKeyGroupEntities.stream().map(
77           licenseKeyGroupEntity -> (LicenseKeyGroupEntity) healingService
78               .heal(licenseKeyGroupEntity, user)).collect(Collectors.toSet()));
79       artifact.getFeatureGroups().add(featureGroupModel);
80     }
81
82     mdcDataDebugMessage.debugExitMessage("VLM name", vendorName);
83     return artifact.toXml().getBytes();
84   }
85
86   static byte[] createVendorLicenseArtifact(String vlmId, String vendorName, String user) {
87
88
89     mdcDataDebugMessage.debugEntryMessage("VLM name", vendorName);
90
91     VendorLicenseArtifact vendorLicenseArtifact = new VendorLicenseArtifact();
92     vendorLicenseArtifact.setVendorName(vendorName);
93     Set<EntitlementPoolEntity> entitlementPoolEntities = new HashSet<>();
94     Set<LicenseKeyGroupEntity> licenseKeyGroupEntities = new HashSet<>();
95
96     List<Version> finalVersions = VendorLicenseArtifactsServiceUtils.getFinalVersionsForVlm(vlmId);
97     for (Version finalVersion : finalVersions) {
98       entitlementPoolEntities
99           .addAll(vendorLicenseFacade.listEntitlementPools(vlmId, finalVersion, user));
100       licenseKeyGroupEntities
101           .addAll(vendorLicenseFacade.listLicenseKeyGroups(vlmId, finalVersion, user));
102     }
103
104
105     entitlementPoolEntities = VendorLicenseArtifactsServiceUtils
106         .healEPs(user,
107             VendorLicenseArtifactsServiceUtils.filterChangedEntities(entitlementPoolEntities));
108     licenseKeyGroupEntities = VendorLicenseArtifactsServiceUtils
109         .healLkgs(user,
110             VendorLicenseArtifactsServiceUtils.filterChangedEntities(licenseKeyGroupEntities));
111
112     vendorLicenseArtifact.setEntitlementPoolEntities(entitlementPoolEntities);
113     vendorLicenseArtifact.setLicenseKeyGroupEntities(licenseKeyGroupEntities);
114
115     mdcDataDebugMessage.debugExitMessage("VLM name", vendorName);
116     return vendorLicenseArtifact.toXml().getBytes();
117   }
118
119
120   /**
121    * Create License Artifacts.
122    * @param vspId vspId
123    * @param vlmId vlmId
124    * @param vlmVersion vlmVersion
125    * @param featureGroups featureGroups
126    * @param user user
127    * @return FileContentHandler
128    */
129   public FileContentHandler createLicenseArtifacts(String vspId, String vlmId, Version vlmVersion,
130                                                    List<String> featureGroups, String user) {
131
132
133     mdcDataDebugMessage.debugEntryMessage("VSP Id", vspId);
134
135     FileContentHandler artifacts = new FileContentHandler();
136     String vendorName = VendorLicenseArtifactsServiceUtils.getVendorName(vlmId, user);
137
138     artifacts.addFile(VNF_ARTIFACT_NAME_WITH_PATH,
139         createVnfArtifact(vspId, vlmId, vlmVersion, vendorName, featureGroups, user));
140     artifacts.addFile(VENDOR_LICENSE_MODEL_ARTIFACT_NAME_WITH_PATH,
141         createVendorLicenseArtifact(vlmId, vendorName, user));
142
143     mdcDataDebugMessage.debugExitMessage("VSP Id", vspId);
144
145     return artifacts;
146   }
147
148 }