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