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