4ea35d321dc25690385c7ae74c5152c597d7ff68
[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 java.util.Comparator;
24 import java.util.HashSet;
25 import java.util.stream.Collectors;
26 import javax.inject.Named;
27 import javax.ws.rs.core.Response;
28 import org.apache.commons.collections4.CollectionUtils;
29 import org.openecomp.sdc.vendorlicense.VendorLicenseManager;
30 import org.openecomp.sdc.vendorlicense.VendorLicenseManagerFactory;
31 import org.openecomp.sdc.vendorlicense.dao.types.EntitlementPoolEntity;
32 import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity;
33 import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupModel;
34 import org.openecomp.sdc.vendorlicense.dao.types.LicenseKeyGroupEntity;
35 import org.openecomp.sdc.versioning.dao.types.Version;
36 import org.openecomp.sdcrests.vendorlicense.rest.FeatureGroups;
37 import org.openecomp.sdcrests.vendorlicense.rest.mapping.MapEntitlementPoolEntityToEntitlementPoolEntityDto;
38 import org.openecomp.sdcrests.vendorlicense.rest.mapping.MapFeatureGroupDescriptorDtoToFeatureGroupEntity;
39 import org.openecomp.sdcrests.vendorlicense.rest.mapping.MapFeatureGroupEntityToFeatureGroupDescriptorDto;
40 import org.openecomp.sdcrests.vendorlicense.rest.mapping.MapLicenseKeyGroupEntityToLicenseKeyGroupEntityDto;
41 import org.openecomp.sdcrests.vendorlicense.types.EntitlementPoolEntityDto;
42 import org.openecomp.sdcrests.vendorlicense.types.FeatureGroupEntityDto;
43 import org.openecomp.sdcrests.vendorlicense.types.FeatureGroupModelDto;
44 import org.openecomp.sdcrests.vendorlicense.types.FeatureGroupRequestDto;
45 import org.openecomp.sdcrests.vendorlicense.types.FeatureGroupUpdateRequestDto;
46 import org.openecomp.sdcrests.vendorlicense.types.LicenseKeyGroupEntityDto;
47 import org.openecomp.sdcrests.wrappers.GenericCollectionWrapper;
48 import org.openecomp.sdcrests.wrappers.StringWrapperResponse;
49 import org.springframework.context.annotation.Scope;
50 import org.springframework.stereotype.Service;
51
52 @Named
53 @Service("featureGroups")
54 @Scope(value = "prototype")
55 public class FeatureGroupsImpl implements FeatureGroups {
56
57     private VendorLicenseManager vendorLicenseManager =
58             VendorLicenseManagerFactory.getInstance().createInterface();
59
60     @Override
61     public Response listFeatureGroups(String vlmId, String versionId, String user) {
62
63         MapFeatureGroupEntityToFeatureGroupDescriptorDto outputMapper =
64                 new MapFeatureGroupEntityToFeatureGroupDescriptorDto();
65
66         GenericCollectionWrapper<FeatureGroupEntityDto> results = new GenericCollectionWrapper<>(
67                 vendorLicenseManager.listFeatureGroups(vlmId, new Version(versionId)).stream()
68                                     .sorted(Comparator.comparing(FeatureGroupEntity::getName))
69                                     .map(fg -> getFeatureGroupEntityDto(outputMapper,fg)).collect(Collectors.toList()));
70
71         return Response.ok(results).build();
72     }
73
74     @Override
75     public Response createFeatureGroup(FeatureGroupRequestDto request, String vlmId, String versionId,
76                                        String user) {
77         FeatureGroupEntity featureGroupEntity = new MapFeatureGroupDescriptorDtoToFeatureGroupEntity()
78                 .applyMapping(request, FeatureGroupEntity.class);
79         featureGroupEntity.setVendorLicenseModelId(vlmId);
80         featureGroupEntity.setVersion(new Version(versionId));
81         featureGroupEntity.setLicenseKeyGroupIds(request.getAddedLicenseKeyGroupsIds());
82         featureGroupEntity.setEntitlementPoolIds(request.getAddedEntitlementPoolsIds());
83
84         FeatureGroupEntity createdFeatureGroup =
85                 vendorLicenseManager.createFeatureGroup(featureGroupEntity);
86
87         StringWrapperResponse result =
88                 createdFeatureGroup != null ? new StringWrapperResponse(createdFeatureGroup.getId()) : null;
89         return Response.ok(result).build();
90     }
91
92     @Override
93     public Response updateFeatureGroup(FeatureGroupUpdateRequestDto request, String vlmId,
94                                        String versionId, String featureGroupId, String user) {
95         FeatureGroupEntity featureGroupEntity = new MapFeatureGroupDescriptorDtoToFeatureGroupEntity()
96                 .applyMapping(request, FeatureGroupEntity.class);
97         featureGroupEntity.setVendorLicenseModelId(vlmId);
98         featureGroupEntity.setVersion(new Version(versionId));
99         featureGroupEntity.setId(featureGroupId);
100
101         vendorLicenseManager
102                 .updateFeatureGroup(featureGroupEntity, request.getAddedLicenseKeyGroupsIds(),
103                         request.getRemovedLicenseKeyGroupsIds(), request.getAddedEntitlementPoolsIds(),
104                         request.getRemovedEntitlementPoolsIds());
105         return Response.ok().build();
106     }
107
108     @Override
109     public Response getFeatureGroup(String vlmId, String versionId, String featureGroupId,
110                                     String user) {
111         FeatureGroupEntity fgInput = new FeatureGroupEntity();
112         fgInput.setVendorLicenseModelId(vlmId);
113         fgInput.setVersion(new Version(versionId));
114         fgInput.setId(featureGroupId);
115         FeatureGroupModel featureGroupModel = vendorLicenseManager.getFeatureGroupModel(fgInput);
116
117         if (featureGroupModel == null) {
118             return Response.ok().build();
119         }
120
121         FeatureGroupModelDto fgmDto = new FeatureGroupModelDto();
122         fgmDto.setId(featureGroupModel.getFeatureGroup().getId());
123         fgmDto.setReferencingLicenseAgreements(
124                 featureGroupModel.getFeatureGroup().getReferencingLicenseAgreements());
125         new MapFeatureGroupEntityToFeatureGroupDescriptorDto()
126                 .doMapping(featureGroupModel.getFeatureGroup(), fgmDto);
127
128         if (!CollectionUtils.isEmpty(featureGroupModel.getLicenseKeyGroups())) {
129             fgmDto.setLicenseKeyGroups(new HashSet<>());
130
131             MapLicenseKeyGroupEntityToLicenseKeyGroupEntityDto lkgMapper =
132                     new MapLicenseKeyGroupEntityToLicenseKeyGroupEntityDto();
133             for (LicenseKeyGroupEntity lkg : featureGroupModel.getLicenseKeyGroups()) {
134                 fgmDto.getLicenseKeyGroups()
135                         .add(lkgMapper.applyMapping(lkg, LicenseKeyGroupEntityDto.class));
136             }
137         }
138
139         if (!CollectionUtils.isEmpty(featureGroupModel.getEntitlementPools())) {
140             fgmDto.setEntitlementPools(new HashSet<>());
141
142             MapEntitlementPoolEntityToEntitlementPoolEntityDto epMapper =
143                     new MapEntitlementPoolEntityToEntitlementPoolEntityDto();
144             for (EntitlementPoolEntity ep : featureGroupModel.getEntitlementPools()) {
145                 fgmDto.getEntitlementPools().add(epMapper.applyMapping(ep, EntitlementPoolEntityDto.class));
146
147             }
148         }
149         return Response.ok(fgmDto).build();
150     }
151
152     @Override
153     public Response deleteFeatureGroup(String vlmId, String versionId, String featureGroupId,
154                                        String user) {
155         FeatureGroupEntity fgInput = new FeatureGroupEntity();
156         fgInput.setVendorLicenseModelId(vlmId);
157         fgInput.setVersion(new Version(versionId));
158         fgInput.setId(featureGroupId);
159         vendorLicenseManager.deleteFeatureGroup(fgInput);
160         return Response.ok().build();
161     }
162
163     private FeatureGroupEntityDto getFeatureGroupEntityDto(MapFeatureGroupEntityToFeatureGroupDescriptorDto mapper,FeatureGroupEntity fg) {
164         FeatureGroupEntityDto fgDto = new FeatureGroupEntityDto();
165         fgDto.setId(fg.getId());
166         fgDto.setLicenseKeyGroupsIds(fg.getLicenseKeyGroupIds());
167         fgDto.setEntitlementPoolsIds(fg.getEntitlementPoolIds());
168         fgDto.setReferencingLicenseAgreements(fg.getReferencingLicenseAgreements());
169         mapper.doMapping(fg, fgDto);
170         return fgDto;
171     }
172
173 }