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.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;
41 import org.springframework.beans.factory.annotation.Autowired;
42 import org.springframework.context.annotation.Scope;
43 import org.springframework.stereotype.Service;
45 import java.util.Collection;
46 import java.util.HashSet;
47 import javax.inject.Named;
48 import javax.ws.rs.core.Response;
51 @Service("licenseAgreements")
52 @Scope(value = "prototype")
53 public class LicenseAgreementsImpl implements LicenseAgreements {
56 private VendorLicenseManager vendorLicenseManager;
59 * List license agreements response.
61 * @param vlmId the vlm id
62 * @param version the version
63 * @param user the user
64 * @return the response
66 public Response listLicenseAgreements(String vlmId, String version, String user) {
67 Collection<LicenseAgreementEntity> licenseAgreements =
68 vendorLicenseManager.listLicenseAgreements(vlmId, Version.valueOf(version), user);
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);
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,
94 LicenseAgreementEntity licenseAgreementEntity =
95 new MapLicenseAgreementDescriptorDtoToLicenseAgreementEntity()
96 .applyMapping(request, LicenseAgreementEntity.class);
97 licenseAgreementEntity.setVendorLicenseModelId(vlmId);
98 licenseAgreementEntity.setFeatureGroupIds(request.getAddedFeatureGroupsIds());
100 LicenseAgreementEntity createdLicenseAgreement =
101 vendorLicenseManager.createLicenseAgreement(licenseAgreementEntity, user);
102 StringWrapperResponse result =
103 createdLicenseAgreement != null ? new StringWrapperResponse(createdLicenseAgreement.getId())
106 return Response.ok(result).build();
110 * Update license agreement response.
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
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);
127 .updateLicenseAgreement(licenseAgreementEntity, request.getAddedFeatureGroupsIds(),
128 request.getRemovedFeatureGroupsIds(), user);
129 return Response.ok().build();
133 * Gets license agreement.
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
141 public Response getLicenseAgreement(String vlmId, String version, String licenseAgreementId,
143 LicenseAgreementModel licenseAgreementModel = vendorLicenseManager
144 .getLicenseAgreementModel(vlmId, Version.valueOf(version), licenseAgreementId, user);
146 if (licenseAgreementModel == null) {
147 return Response.ok().build();
150 LicenseAgreementModelDto lamDto = new LicenseAgreementModelDto();
151 lamDto.setId(licenseAgreementModel.getLicenseAgreement().getId());
152 new MapLicenseAgreementEntityToLicenseAgreementDescriptorDto()
153 .doMapping(licenseAgreementModel.getLicenseAgreement(), lamDto);
155 if (!CommonMethods.isEmpty(licenseAgreementModel.getFeatureGroups())) {
156 lamDto.setFeatureGroups(new HashSet<>());
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);
167 lamDto.getFeatureGroups().add(fgeDto);
171 return Response.ok(lamDto).build();
175 * Delete license agreement response.
177 * @param vlmId the vlm id
178 * @param licenseAgreementId the license agreement id
179 * @param user the user
180 * @return the response
182 public Response deleteLicenseAgreement(String vlmId, String licenseAgreementId, String user) {
183 vendorLicenseManager.deleteLicenseAgreement(vlmId, licenseAgreementId, user);
184 return Response.ok().build();