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