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 / 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.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.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   private VendorLicenseManager vendorLicenseManager =
53       VendorLicenseManagerFactory.getInstance().createInterface();
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, new Version(versionId));
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     licenseKeyGroupEntity.setVersion(new Version(versionId));
102
103     LicenseKeyGroupEntity createdLicenseKeyGroup =
104         vendorLicenseManager.createLicenseKeyGroup(licenseKeyGroupEntity);
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.setVersion(new Version(versionId));
135     licenseKeyGroupEntity.setId(licenseKeyGroupId);
136
137     vendorLicenseManager.updateLicenseKeyGroup(licenseKeyGroupEntity);
138
139     mdcDataDebugMessage.debugExitMessage("VLM id, LKG id", vlmId, licenseKeyGroupId);
140
141     return Response.ok().build();
142   }
143
144   /**
145    * Gets license key group.
146    *
147    * @param vlmId             the vlm id
148    * @param versionId         the version
149    * @param licenseKeyGroupId the license key group id
150    * @param user              the user
151    * @return the license key group
152    */
153   public Response getLicenseKeyGroup(String vlmId, String versionId, String licenseKeyGroupId,
154                                      String user) {
155
156     mdcDataDebugMessage.debugEntryMessage("VLM id, LKG id", vlmId, licenseKeyGroupId);
157
158     MdcUtil.initMdc(LoggerServiceName.Get_LKG.toString());
159     LicenseKeyGroupEntity lkgInput = new LicenseKeyGroupEntity();
160     lkgInput.setVendorLicenseModelId(vlmId);
161     lkgInput.setVersion(new Version(versionId));
162     lkgInput.setId(licenseKeyGroupId);
163     LicenseKeyGroupEntity licenseKeyGroup = vendorLicenseManager.getLicenseKeyGroup(lkgInput);
164
165     LicenseKeyGroupEntityDto licenseKeyGroupEntityDto = licenseKeyGroup == null ? null :
166         new MapLicenseKeyGroupEntityToLicenseKeyGroupEntityDto()
167             .applyMapping(licenseKeyGroup, LicenseKeyGroupEntityDto.class);
168
169     mdcDataDebugMessage.debugExitMessage("VLM id, LKG id", vlmId, licenseKeyGroupId);
170
171     return Response.ok(licenseKeyGroupEntityDto).build();
172   }
173
174   /**
175    * Delete license key group response.
176    *
177    * @param vlmId             the vlm id
178    * @param licenseKeyGroupId the license key group id
179    * @param user              the user
180    * @return the response
181    */
182   public Response deleteLicenseKeyGroup(String vlmId, String versionId, String licenseKeyGroupId,
183                                         String user) {
184
185     mdcDataDebugMessage.debugEntryMessage("VLM id, LKG id", vlmId, licenseKeyGroupId);
186
187     MdcUtil.initMdc(LoggerServiceName.Delete_LKG.toString());
188     LicenseKeyGroupEntity lkgInput = new LicenseKeyGroupEntity();
189     lkgInput.setVendorLicenseModelId(vlmId);
190     lkgInput.setVersion(new Version(versionId));
191     lkgInput.setId(licenseKeyGroupId);
192     vendorLicenseManager.deleteLicenseKeyGroup(lkgInput);
193
194     mdcDataDebugMessage.debugExitMessage("VLM id, LKG id", vlmId, licenseKeyGroupId);
195
196     return Response.ok().build();
197   }
198 }