Removed reference to MDC for logging
[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.vendorlicense.VendorLicenseManager;
24 import org.openecomp.sdc.vendorlicense.VendorLicenseManagerFactory;
25 import org.openecomp.sdc.vendorlicense.dao.types.LicenseKeyGroupEntity;
26 import org.openecomp.sdc.versioning.dao.types.Version;
27 import org.openecomp.sdcrests.vendorlicense.rest.LicenseKeyGroups;
28 import org.openecomp.sdcrests.vendorlicense.rest.mapping.MapLicenseKeyGroupEntityToLicenseKeyGroupEntityDto;
29 import org.openecomp.sdcrests.vendorlicense.rest.mapping.MapLicenseKeyGroupRequestDtoToLicenseKeyGroupEntity;
30 import org.openecomp.sdcrests.vendorlicense.types.LicenseKeyGroupEntityDto;
31 import org.openecomp.sdcrests.vendorlicense.types.LicenseKeyGroupRequestDto;
32 import org.openecomp.sdcrests.wrappers.GenericCollectionWrapper;
33 import org.openecomp.sdcrests.wrappers.StringWrapperResponse;
34 import org.springframework.context.annotation.Scope;
35 import org.springframework.stereotype.Service;
36 import org.springframework.validation.annotation.Validated;
37
38 import javax.inject.Named;
39 import javax.ws.rs.core.Response;
40 import java.util.Collection;
41
42 @Named
43 @Service("licenseKeyGroups")
44 @Scope(value = "prototype")
45 @Validated
46 public class LicenseKeyGroupsImpl implements LicenseKeyGroups {
47   private VendorLicenseManager vendorLicenseManager =
48       VendorLicenseManagerFactory.getInstance().createInterface();
49
50   /**
51    * List license key groups response.
52    *
53    * @param vlmId     the vlm id
54    * @param versionId the version
55    * @param user      the user
56    * @return the response
57    */
58   public Response listLicenseKeyGroups(String vlmId, String versionId, String user) {
59     Collection<LicenseKeyGroupEntity> licenseKeyGroups =
60         vendorLicenseManager.listLicenseKeyGroups(vlmId, new Version(versionId));
61
62     GenericCollectionWrapper<LicenseKeyGroupEntityDto> result = new GenericCollectionWrapper<>();
63     MapLicenseKeyGroupEntityToLicenseKeyGroupEntityDto outputMapper =
64         new MapLicenseKeyGroupEntityToLicenseKeyGroupEntityDto();
65     for (LicenseKeyGroupEntity ep : licenseKeyGroups) {
66       result.add(outputMapper.applyMapping(ep, LicenseKeyGroupEntityDto.class));
67     }
68     return Response.ok(result).build();
69   }
70
71   /**
72    * Create license key group response.
73    *
74    * @param request the request
75    * @param vlmId   the vlm id
76    * @param user    the user
77    * @return the response
78    */
79   public Response createLicenseKeyGroup(LicenseKeyGroupRequestDto request, String vlmId,
80                                         String versionId, String user) {
81     LicenseKeyGroupEntity licenseKeyGroupEntity =
82         new MapLicenseKeyGroupRequestDtoToLicenseKeyGroupEntity()
83             .applyMapping(request, LicenseKeyGroupEntity.class);
84     licenseKeyGroupEntity.setVendorLicenseModelId(vlmId);
85     licenseKeyGroupEntity.setVersion(new Version(versionId));
86
87     LicenseKeyGroupEntity createdLicenseKeyGroup =
88         vendorLicenseManager.createLicenseKeyGroup(licenseKeyGroupEntity);
89     StringWrapperResponse result =
90         createdLicenseKeyGroup != null ? new StringWrapperResponse(createdLicenseKeyGroup.getId())
91             : null;
92     return Response.ok(result).build();
93   }
94
95   /**
96    * Update license key group response.
97    *
98    * @param request           the request
99    * @param vlmId             the vlm id
100    * @param licenseKeyGroupId the license key group id
101    * @param user              the user
102    * @return the response
103    */
104   public Response updateLicenseKeyGroup(LicenseKeyGroupRequestDto request, String vlmId,
105                                         String versionId,
106                                         String licenseKeyGroupId, String user) {
107     LicenseKeyGroupEntity licenseKeyGroupEntity =
108         new MapLicenseKeyGroupRequestDtoToLicenseKeyGroupEntity()
109             .applyMapping(request, LicenseKeyGroupEntity.class);
110     licenseKeyGroupEntity.setVendorLicenseModelId(vlmId);
111     licenseKeyGroupEntity.setVersion(new Version(versionId));
112     licenseKeyGroupEntity.setId(licenseKeyGroupId);
113
114     vendorLicenseManager.updateLicenseKeyGroup(licenseKeyGroupEntity);
115     return Response.ok().build();
116   }
117
118   /**
119    * Gets license key group.
120    *
121    * @param vlmId             the vlm id
122    * @param versionId         the version
123    * @param licenseKeyGroupId the license key group id
124    * @param user              the user
125    * @return the license key group
126    */
127   public Response getLicenseKeyGroup(String vlmId, String versionId, String licenseKeyGroupId,
128                                      String user) {
129     LicenseKeyGroupEntity lkgInput = new LicenseKeyGroupEntity();
130     lkgInput.setVendorLicenseModelId(vlmId);
131     lkgInput.setVersion(new Version(versionId));
132     lkgInput.setId(licenseKeyGroupId);
133     LicenseKeyGroupEntity licenseKeyGroup = vendorLicenseManager.getLicenseKeyGroup(lkgInput);
134
135     LicenseKeyGroupEntityDto licenseKeyGroupEntityDto = licenseKeyGroup == null ? null :
136         new MapLicenseKeyGroupEntityToLicenseKeyGroupEntityDto()
137             .applyMapping(licenseKeyGroup, LicenseKeyGroupEntityDto.class);
138     return Response.ok(licenseKeyGroupEntityDto).build();
139   }
140
141   /**
142    * Delete license key group response.
143    *
144    * @param vlmId             the vlm id
145    * @param licenseKeyGroupId the license key group id
146    * @param user              the user
147    * @return the response
148    */
149   public Response deleteLicenseKeyGroup(String vlmId, String versionId, String licenseKeyGroupId,
150                                         String user) {
151     LicenseKeyGroupEntity lkgInput = new LicenseKeyGroupEntity();
152     lkgInput.setVendorLicenseModelId(vlmId);
153     lkgInput.setVersion(new Version(versionId));
154     lkgInput.setId(licenseKeyGroupId);
155     vendorLicenseManager.deleteLicenseKeyGroup(lkgInput);
156     return Response.ok().build();
157   }
158 }