Add collaboration feature
[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.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.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   private VendorLicenseManager vendorLicenseManager =
62       VendorLicenseManagerFactory.getInstance().createInterface();
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, new Version(versionId));
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       fgDto.setManufacturerReferenceNumber(fg.getManufacturerReferenceNumber());
84       outputMapper.doMapping(fg, fgDto);
85       results.add(fgDto);
86     }
87
88     mdcDataDebugMessage.debugExitMessage("VLM id", vlmId);
89
90     return Response.ok(results).build();
91   }
92
93   @Override
94   public Response createFeatureGroup(FeatureGroupRequestDto request, String vlmId, String versionId,
95                                      String user) {
96
97     mdcDataDebugMessage.debugEntryMessage("VLM id", vlmId);
98
99     MdcUtil.initMdc(LoggerServiceName.Create_FG.toString());
100     FeatureGroupEntity featureGroupEntity = new MapFeatureGroupDescriptorDtoToFeatureGroupEntity()
101         .applyMapping(request, FeatureGroupEntity.class);
102     featureGroupEntity.setVendorLicenseModelId(vlmId);
103     featureGroupEntity.setVersion(new Version(versionId));
104     featureGroupEntity.setLicenseKeyGroupIds(request.getAddedLicenseKeyGroupsIds());
105     featureGroupEntity.setEntitlementPoolIds(request.getAddedEntitlementPoolsIds());
106
107     FeatureGroupEntity createdFeatureGroup =
108         vendorLicenseManager.createFeatureGroup(featureGroupEntity);
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.setVersion(new Version(versionId));
129     featureGroupEntity.setId(featureGroupId);
130
131     vendorLicenseManager
132         .updateFeatureGroup(featureGroupEntity, request.getAddedLicenseKeyGroupsIds(),
133             request.getRemovedLicenseKeyGroupsIds(), request.getAddedEntitlementPoolsIds(),
134             request.getRemovedEntitlementPoolsIds());
135
136     mdcDataDebugMessage.debugExitMessage("VLM id, FG id", vlmId, featureGroupId);
137
138     return Response.ok().build();
139   }
140
141   @Override
142   public Response getFeatureGroup(String vlmId, String versionId, String featureGroupId,
143                                   String user) {
144
145     mdcDataDebugMessage.debugEntryMessage("VLM id, FG id", vlmId, featureGroupId);
146
147     MdcUtil.initMdc(LoggerServiceName.Get_FG.toString());
148     FeatureGroupEntity fgInput = new FeatureGroupEntity();
149     fgInput.setVendorLicenseModelId(vlmId);
150     fgInput.setVersion(new Version(versionId));
151     fgInput.setId(featureGroupId);
152     FeatureGroupModel featureGroupModel = vendorLicenseManager.getFeatureGroupModel(fgInput);
153
154     if (featureGroupModel == null) {
155       return Response.ok().build();
156     }
157
158     FeatureGroupModelDto fgmDto = new FeatureGroupModelDto();
159     fgmDto.setId(featureGroupModel.getFeatureGroup().getId());
160     fgmDto.setReferencingLicenseAgreements(
161         featureGroupModel.getFeatureGroup().getReferencingLicenseAgreements());
162     new MapFeatureGroupEntityToFeatureGroupDescriptorDto()
163         .doMapping(featureGroupModel.getFeatureGroup(), fgmDto);
164
165     if (!CommonMethods.isEmpty(featureGroupModel.getLicenseKeyGroups())) {
166       fgmDto.setLicenseKeyGroups(new HashSet<>());
167
168       MapLicenseKeyGroupEntityToLicenseKeyGroupEntityDto lkgMapper =
169           new MapLicenseKeyGroupEntityToLicenseKeyGroupEntityDto();
170       for (LicenseKeyGroupEntity lkg : featureGroupModel.getLicenseKeyGroups()) {
171         fgmDto.getLicenseKeyGroups()
172             .add(lkgMapper.applyMapping(lkg, LicenseKeyGroupEntityDto.class));
173       }
174     }
175
176     if (!CommonMethods.isEmpty(featureGroupModel.getEntitlementPools())) {
177       fgmDto.setEntitlementPools(new HashSet<>());
178
179       MapEntitlementPoolEntityToEntitlementPoolEntityDto epMapper =
180           new MapEntitlementPoolEntityToEntitlementPoolEntityDto();
181       for (EntitlementPoolEntity ep : featureGroupModel.getEntitlementPools()) {
182         fgmDto.getEntitlementPools().add(epMapper.applyMapping(ep, EntitlementPoolEntityDto.class));
183
184       }
185     }
186
187     mdcDataDebugMessage.debugExitMessage("VLM id, FG id", vlmId, featureGroupId);
188
189     return Response.ok(fgmDto).build();
190   }
191
192   @Override
193   public Response deleteFeatureGroup(String vlmId, String versionId, String featureGroupId,
194                                      String user) {
195
196     mdcDataDebugMessage.debugEntryMessage("VLM id, FG id", vlmId, featureGroupId);
197
198     MdcUtil.initMdc(LoggerServiceName.Delete_FG.toString());
199     FeatureGroupEntity fgInput = new FeatureGroupEntity();
200     fgInput.setVendorLicenseModelId(vlmId);
201     fgInput.setVersion(new Version(versionId));
202     fgInput.setId(featureGroupId);
203     vendorLicenseManager.deleteFeatureGroup(fgInput);
204
205     mdcDataDebugMessage.debugExitMessage("VLM id, FG id", vlmId, featureGroupId);
206
207     return Response.ok().build();
208   }
209
210 }