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