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