2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdcrests.vendorlicense.rest.services;
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;
46 import javax.inject.Named;
47 import javax.ws.rs.core.Response;
48 import java.util.Collection;
49 import java.util.HashSet;
52 @Service("licenseAgreements")
53 @Scope(value = "prototype")
54 public class LicenseAgreementsImpl implements LicenseAgreements {
55 private VendorLicenseManager vendorLicenseManager =
56 VendorLicenseManagerFactory.getInstance().createInterface();
59 * List license agreements response.
61 * @param vlmId the vlm id
62 * @param versionId the version
63 * @param user the user
64 * @return the response
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));
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);
81 return Response.ok(results).build();
85 * Create license agreement response.
87 * @param request the request
88 * @param vlmId the vlm id
89 * @param user the user
90 * @return the response
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());
102 LicenseAgreementEntity createdLicenseAgreement =
103 vendorLicenseManager.createLicenseAgreement(licenseAgreementEntity);
104 StringWrapperResponse result =
105 createdLicenseAgreement != null ? new StringWrapperResponse(createdLicenseAgreement.getId())
107 return Response.ok(result).build();
111 * Update license agreement response.
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
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);
130 .updateLicenseAgreement(licenseAgreementEntity, request.getAddedFeatureGroupsIds(),
131 request.getRemovedFeatureGroupsIds());
132 return Response.ok().build();
136 * Gets license agreement.
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
144 public Response getLicenseAgreement(String vlmId, String versionId, String licenseAgreementId,
146 MdcUtil.initMdc(LoggerServiceName.Get_LA.toString());
147 LicenseAgreementModel licenseAgreementModel = vendorLicenseManager
148 .getLicenseAgreementModel(vlmId, new Version(versionId), licenseAgreementId);
150 if (licenseAgreementModel == null) {
151 return Response.ok().build();
154 LicenseAgreementModelDto lamDto = new LicenseAgreementModelDto();
155 lamDto.setId(licenseAgreementModel.getLicenseAgreement().getId());
156 new MapLicenseAgreementEntityToLicenseAgreementDescriptorDto()
157 .doMapping(licenseAgreementModel.getLicenseAgreement(), lamDto);
159 if (!CommonMethods.isEmpty(licenseAgreementModel.getFeatureGroups())) {
160 lamDto.setFeatureGroups(new HashSet<>());
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);
171 lamDto.getFeatureGroups().add(fgeDto);
174 return Response.ok(lamDto).build();
178 * Delete license agreement response.
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
186 public Response deleteLicenseAgreement(String vlmId, String versionId, String licenseAgreementId,
188 MdcUtil.initMdc(LoggerServiceName.Delete_LA.toString());
189 vendorLicenseManager.deleteLicenseAgreement(vlmId, new Version(versionId), licenseAgreementId);
190 return Response.ok().build();