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