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