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