4c4bf134fd7f043a357f95881ea77baee41b937d
[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.sdc.logging.context.MdcUtil;
24 import org.openecomp.sdc.logging.context.impl.MdcDataDebugMessage;
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.LicenseKeyGroupEntity;
29 import org.openecomp.sdc.versioning.dao.types.Version;
30 import org.openecomp.sdcrests.vendorlicense.rest.LicenseKeyGroups;
31 import org.openecomp.sdcrests.vendorlicense.rest.mapping.MapLicenseKeyGroupEntityToLicenseKeyGroupEntityDto;
32 import org.openecomp.sdcrests.vendorlicense.rest.mapping.MapLicenseKeyGroupRequestDtoToLicenseKeyGroupEntity;
33 import org.openecomp.sdcrests.vendorlicense.types.LicenseKeyGroupEntityDto;
34 import org.openecomp.sdcrests.vendorlicense.types.LicenseKeyGroupRequestDto;
35 import org.openecomp.sdcrests.wrappers.GenericCollectionWrapper;
36 import org.openecomp.sdcrests.wrappers.StringWrapperResponse;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.context.annotation.Scope;
39 import org.springframework.stereotype.Service;
40 import org.springframework.validation.annotation.Validated;
41
42 import javax.inject.Named;
43 import javax.ws.rs.core.Response;
44 import java.util.Collection;
45
46 @Named
47 @Service("licenseKeyGroups")
48 @Scope(value = "prototype")
49 @Validated
50 public class LicenseKeyGroupsImpl implements LicenseKeyGroups {
51
52   private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
53   private VendorLicenseManager vendorLicenseManager =
54       VendorLicenseManagerFactory.getInstance().createInterface();
55
56   /**
57    * List license key groups response.
58    *
59    * @param vlmId     the vlm id
60    * @param versionId the version
61    * @param user      the user
62    * @return the response
63    */
64   public Response listLicenseKeyGroups(String vlmId, String versionId, String user) {
65
66     mdcDataDebugMessage.debugEntryMessage("VLM id", vlmId);
67
68     MdcUtil.initMdc(LoggerServiceName.List_LKG.toString());
69     Collection<LicenseKeyGroupEntity> licenseKeyGroups =
70         vendorLicenseManager.listLicenseKeyGroups(vlmId, Version.valueOf(versionId), user);
71
72     GenericCollectionWrapper<LicenseKeyGroupEntityDto> result = new GenericCollectionWrapper<>();
73     MapLicenseKeyGroupEntityToLicenseKeyGroupEntityDto outputMapper =
74         new MapLicenseKeyGroupEntityToLicenseKeyGroupEntityDto();
75     for (LicenseKeyGroupEntity ep : licenseKeyGroups) {
76       result.add(outputMapper.applyMapping(ep, LicenseKeyGroupEntityDto.class));
77     }
78
79     mdcDataDebugMessage.debugExitMessage("VLM id", vlmId);
80
81     return Response.ok(result).build();
82   }
83
84   /**
85    * Create license key group response.
86    *
87    * @param request the request
88    * @param vlmId   the vlm id
89    * @param user    the user
90    * @return the response
91    */
92   public Response createLicenseKeyGroup(LicenseKeyGroupRequestDto request, String vlmId,
93                                         String versionId, String user) {
94
95     mdcDataDebugMessage.debugEntryMessage("VLM id", vlmId);
96
97     MdcUtil.initMdc(LoggerServiceName.Create_LKG.toString());
98     LicenseKeyGroupEntity licenseKeyGroupEntity =
99         new MapLicenseKeyGroupRequestDtoToLicenseKeyGroupEntity()
100             .applyMapping(request, LicenseKeyGroupEntity.class);
101     licenseKeyGroupEntity.setVendorLicenseModelId(vlmId);
102
103     LicenseKeyGroupEntity createdLicenseKeyGroup =
104         vendorLicenseManager.createLicenseKeyGroup(licenseKeyGroupEntity, user);
105     StringWrapperResponse result =
106         createdLicenseKeyGroup != null ? new StringWrapperResponse(createdLicenseKeyGroup.getId())
107             : null;
108
109     mdcDataDebugMessage.debugExitMessage("VLM id", vlmId);
110
111     return Response.ok(result).build();
112   }
113
114   /**
115    * Update license key group response.
116    *
117    * @param request           the request
118    * @param vlmId             the vlm id
119    * @param licenseKeyGroupId the license key group id
120    * @param user              the user
121    * @return the response
122    */
123   public Response updateLicenseKeyGroup(LicenseKeyGroupRequestDto request, String vlmId,
124                                         String versionId,
125                                         String licenseKeyGroupId, String user) {
126
127     mdcDataDebugMessage.debugEntryMessage("VLM id, LKG id", vlmId, licenseKeyGroupId);
128
129     MdcUtil.initMdc(LoggerServiceName.Update_LKG.toString());
130     LicenseKeyGroupEntity licenseKeyGroupEntity =
131         new MapLicenseKeyGroupRequestDtoToLicenseKeyGroupEntity()
132             .applyMapping(request, LicenseKeyGroupEntity.class);
133     licenseKeyGroupEntity.setVendorLicenseModelId(vlmId);
134     licenseKeyGroupEntity.setId(licenseKeyGroupId);
135
136     vendorLicenseManager.updateLicenseKeyGroup(licenseKeyGroupEntity, user);
137
138     mdcDataDebugMessage.debugExitMessage("VLM id, LKG id", vlmId, licenseKeyGroupId);
139
140     return Response.ok().build();
141   }
142
143   /**
144    * Gets license key group.
145    *
146    * @param vlmId             the vlm id
147    * @param versionId         the version
148    * @param licenseKeyGroupId the license key group id
149    * @param user              the user
150    * @return the license key group
151    */
152   public Response getLicenseKeyGroup(String vlmId, String versionId, String licenseKeyGroupId,
153                                      String user) {
154
155     mdcDataDebugMessage.debugEntryMessage("VLM id, LKG id", vlmId, licenseKeyGroupId);
156
157     MdcUtil.initMdc(LoggerServiceName.Get_LKG.toString());
158     LicenseKeyGroupEntity lkgInput = new LicenseKeyGroupEntity();
159     lkgInput.setVendorLicenseModelId(vlmId);
160     lkgInput.setVersion(Version.valueOf(versionId));
161     lkgInput.setId(licenseKeyGroupId);
162     LicenseKeyGroupEntity licenseKeyGroup = vendorLicenseManager.getLicenseKeyGroup(lkgInput, user);
163
164     LicenseKeyGroupEntityDto licenseKeyGroupEntityDto = licenseKeyGroup == null ? null :
165         new MapLicenseKeyGroupEntityToLicenseKeyGroupEntityDto()
166             .applyMapping(licenseKeyGroup, LicenseKeyGroupEntityDto.class);
167
168     mdcDataDebugMessage.debugExitMessage("VLM id, LKG id", vlmId, licenseKeyGroupId);
169
170     return Response.ok(licenseKeyGroupEntityDto).build();
171   }
172
173   /**
174    * Delete license key group response.
175    *
176    * @param vlmId             the vlm id
177    * @param licenseKeyGroupId the license key group id
178    * @param user              the user
179    * @return the response
180    */
181   public Response deleteLicenseKeyGroup(String vlmId, String versionId, String licenseKeyGroupId,
182                                         String user) {
183
184     mdcDataDebugMessage.debugEntryMessage("VLM id, LKG id", vlmId, licenseKeyGroupId);
185
186     MdcUtil.initMdc(LoggerServiceName.Delete_LKG.toString());
187     LicenseKeyGroupEntity lkgInput = new LicenseKeyGroupEntity();
188     lkgInput.setVendorLicenseModelId(vlmId);
189     lkgInput.setId(licenseKeyGroupId);
190     vendorLicenseManager.deleteLicenseKeyGroup(lkgInput, user);
191
192     mdcDataDebugMessage.debugExitMessage("VLM id, LKG id", vlmId, licenseKeyGroupId);
193
194     return Response.ok().build();
195   }
196 }