5be30d861bdd93c8a3129d376c0830f1782c347b
[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.sdcrests.vendorlicense.rest.services;
22
23 import org.openecomp.core.utilities.CommonMethods;
24 import org.openecomp.sdc.logging.context.MdcUtil;
25 import org.openecomp.sdc.logging.types.LoggerServiceName;
26 import org.openecomp.sdc.vendorlicense.VendorLicenseManager;
27 import org.openecomp.sdc.vendorlicense.VendorLicenseManagerFactory;
28 import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity;
29 import org.openecomp.sdc.vendorlicense.dao.types.LicenseAgreementEntity;
30 import org.openecomp.sdc.vendorlicense.dao.types.LicenseAgreementModel;
31 import org.openecomp.sdc.versioning.dao.types.Version;
32 import org.openecomp.sdcrests.vendorlicense.rest.LicenseAgreements;
33 import org.openecomp.sdcrests.vendorlicense.rest.mapping.MapFeatureGroupEntityToFeatureGroupDescriptorDto;
34 import org.openecomp.sdcrests.vendorlicense.rest.mapping.MapLicenseAgreementDescriptorDtoToLicenseAgreementEntity;
35 import org.openecomp.sdcrests.vendorlicense.rest.mapping.MapLicenseAgreementEntityToLicenseAgreementDescriptorDto;
36 import org.openecomp.sdcrests.vendorlicense.types.FeatureGroupEntityDto;
37 import org.openecomp.sdcrests.vendorlicense.types.LicenseAgreementEntityDto;
38 import org.openecomp.sdcrests.vendorlicense.types.LicenseAgreementModelDto;
39 import org.openecomp.sdcrests.vendorlicense.types.LicenseAgreementRequestDto;
40 import org.openecomp.sdcrests.vendorlicense.types.LicenseAgreementUpdateRequestDto;
41 import org.openecomp.sdcrests.wrappers.GenericCollectionWrapper;
42 import org.openecomp.sdcrests.wrappers.StringWrapperResponse;
43 import org.springframework.context.annotation.Scope;
44 import org.springframework.stereotype.Service;
45
46 import javax.inject.Named;
47 import javax.ws.rs.core.Response;
48 import java.util.Collection;
49 import java.util.HashSet;
50
51 @Named
52 @Service("licenseAgreements")
53 @Scope(value = "prototype")
54 public class LicenseAgreementsImpl implements LicenseAgreements {
55   private VendorLicenseManager vendorLicenseManager =
56       VendorLicenseManagerFactory.getInstance().createInterface();
57
58   /**
59    * List license agreements response.
60    *
61    * @param vlmId     the vlm id
62    * @param versionId the version
63    * @param user      the user
64    * @return the response
65    */
66   public Response listLicenseAgreements(String vlmId, String versionId, String user) {
67     MdcUtil.initMdc(LoggerServiceName.List_LA.toString());
68     Collection<LicenseAgreementEntity> licenseAgreements =
69         vendorLicenseManager.listLicenseAgreements(vlmId, new Version(versionId));
70
71     GenericCollectionWrapper<LicenseAgreementEntityDto> results = new GenericCollectionWrapper<>();
72     MapLicenseAgreementEntityToLicenseAgreementDescriptorDto outputMapper =
73         new MapLicenseAgreementEntityToLicenseAgreementDescriptorDto();
74     for (LicenseAgreementEntity lae : licenseAgreements) {
75       LicenseAgreementEntityDto laeDto = new LicenseAgreementEntityDto();
76       laeDto.setId(lae.getId());
77       laeDto.setFeatureGroupsIds(lae.getFeatureGroupIds());
78       outputMapper.doMapping(lae, laeDto);
79       results.add(laeDto);
80     }
81     return Response.ok(results).build();
82   }
83
84   /**
85    * Create license agreement response.
86    *
87    * @param request the request
88    * @param vlmId   the vlm id
89    * @param user    the user
90    * @return the response
91    */
92   public Response createLicenseAgreement(LicenseAgreementRequestDto request, String vlmId,
93                                          String versionId, String user) {
94     MdcUtil.initMdc(LoggerServiceName.Create_LA.toString());
95     LicenseAgreementEntity licenseAgreementEntity =
96         new MapLicenseAgreementDescriptorDtoToLicenseAgreementEntity()
97             .applyMapping(request, LicenseAgreementEntity.class);
98     licenseAgreementEntity.setVendorLicenseModelId(vlmId);
99     licenseAgreementEntity.setVersion(new Version(versionId));
100     licenseAgreementEntity.setFeatureGroupIds(request.getAddedFeatureGroupsIds());
101
102     LicenseAgreementEntity createdLicenseAgreement =
103         vendorLicenseManager.createLicenseAgreement(licenseAgreementEntity);
104     StringWrapperResponse result =
105         createdLicenseAgreement != null ? new StringWrapperResponse(createdLicenseAgreement.getId())
106             : null;
107     return Response.ok(result).build();
108   }
109
110   /**
111    * Update license agreement response.
112    *
113    * @param request            the request
114    * @param vlmId              the vlm id
115    * @param licenseAgreementId the license agreement id
116    * @param user               the user
117    * @return the response
118    */
119   public Response updateLicenseAgreement(LicenseAgreementUpdateRequestDto request, String vlmId,
120                                          String versionId, String licenseAgreementId, String user) {
121     MdcUtil.initMdc(LoggerServiceName.Update_LA.toString());
122     LicenseAgreementEntity licenseAgreementEntity =
123         new MapLicenseAgreementDescriptorDtoToLicenseAgreementEntity()
124             .applyMapping(request, LicenseAgreementEntity.class);
125     licenseAgreementEntity.setVendorLicenseModelId(vlmId);
126     licenseAgreementEntity.setVersion(new Version(versionId));
127     licenseAgreementEntity.setId(licenseAgreementId);
128
129     vendorLicenseManager
130         .updateLicenseAgreement(licenseAgreementEntity, request.getAddedFeatureGroupsIds(),
131             request.getRemovedFeatureGroupsIds());
132     return Response.ok().build();
133   }
134
135   /**
136    * Gets license agreement.
137    *
138    * @param vlmId              the vlm id
139    * @param versionId          the version
140    * @param licenseAgreementId the license agreement id
141    * @param user               the user
142    * @return the license agreement
143    */
144   public Response getLicenseAgreement(String vlmId, String versionId, String licenseAgreementId,
145                                       String user) {
146     MdcUtil.initMdc(LoggerServiceName.Get_LA.toString());
147     LicenseAgreementModel licenseAgreementModel = vendorLicenseManager
148         .getLicenseAgreementModel(vlmId, new Version(versionId), licenseAgreementId);
149
150     if (licenseAgreementModel == null) {
151       return Response.ok().build();
152     }
153
154     LicenseAgreementModelDto lamDto = new LicenseAgreementModelDto();
155     lamDto.setId(licenseAgreementModel.getLicenseAgreement().getId());
156     new MapLicenseAgreementEntityToLicenseAgreementDescriptorDto()
157         .doMapping(licenseAgreementModel.getLicenseAgreement(), lamDto);
158
159     if (!CommonMethods.isEmpty(licenseAgreementModel.getFeatureGroups())) {
160       lamDto.setFeatureGroups(new HashSet<>());
161
162       MapFeatureGroupEntityToFeatureGroupDescriptorDto fgMapper =
163           new MapFeatureGroupEntityToFeatureGroupDescriptorDto();
164       for (FeatureGroupEntity fg : licenseAgreementModel.getFeatureGroups()) {
165         FeatureGroupEntityDto fgeDto = new FeatureGroupEntityDto();
166         fgeDto.setId(fg.getId());
167         fgeDto.setEntitlementPoolsIds(fg.getEntitlementPoolIds());
168         fgeDto.setLicenseKeyGroupsIds(fg.getLicenseKeyGroupIds());
169         fgMapper.doMapping(fg, fgeDto);
170
171         lamDto.getFeatureGroups().add(fgeDto);
172       }
173     }
174     return Response.ok(lamDto).build();
175   }
176
177   /**
178    * Delete license agreement response.
179    *
180    * @param vlmId              the vlm id
181    * @param versionId          the version id
182    * @param licenseAgreementId the license agreement id
183    * @param user               the user
184    * @return the response
185    */
186   public Response deleteLicenseAgreement(String vlmId, String versionId, String licenseAgreementId,
187                                          String user) {
188     MdcUtil.initMdc(LoggerServiceName.Delete_LA.toString());
189     vendorLicenseManager.deleteLicenseAgreement(vlmId, new Version(versionId), licenseAgreementId);
190     return Response.ok().build();
191   }
192 }