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