4c24d59b63abb693ef3e2aebcc4eebffaac218ef
[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 org.openecomp.sdc.logging.context.MdcUtil;
24 import org.openecomp.sdc.logging.types.LoggerServiceName;
25 import org.openecomp.sdc.vendorlicense.VendorLicenseManager;
26 import org.openecomp.sdc.vendorlicense.VendorLicenseManagerFactory;
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.context.annotation.Scope;
37 import org.springframework.stereotype.Service;
38 import org.springframework.validation.annotation.Validated;
39
40 import javax.inject.Named;
41 import javax.ws.rs.core.Response;
42 import java.util.Collection;
43
44 @Named
45 @Service("licenseKeyGroups")
46 @Scope(value = "prototype")
47 @Validated
48 public class LicenseKeyGroupsImpl implements LicenseKeyGroups {
49   private VendorLicenseManager vendorLicenseManager =
50       VendorLicenseManagerFactory.getInstance().createInterface();
51
52   /**
53    * List license key groups response.
54    *
55    * @param vlmId     the vlm id
56    * @param versionId the version
57    * @param user      the user
58    * @return the response
59    */
60   public Response listLicenseKeyGroups(String vlmId, String versionId, String user) {
61     MdcUtil.initMdc(LoggerServiceName.List_LKG.toString());
62     Collection<LicenseKeyGroupEntity> licenseKeyGroups =
63         vendorLicenseManager.listLicenseKeyGroups(vlmId, new Version(versionId));
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     return Response.ok(result).build();
72   }
73
74   /**
75    * Create license key group response.
76    *
77    * @param request the request
78    * @param vlmId   the vlm id
79    * @param user    the user
80    * @return the response
81    */
82   public Response createLicenseKeyGroup(LicenseKeyGroupRequestDto request, String vlmId,
83                                         String versionId, String user) {
84     MdcUtil.initMdc(LoggerServiceName.Create_LKG.toString());
85     LicenseKeyGroupEntity licenseKeyGroupEntity =
86         new MapLicenseKeyGroupRequestDtoToLicenseKeyGroupEntity()
87             .applyMapping(request, LicenseKeyGroupEntity.class);
88     licenseKeyGroupEntity.setVendorLicenseModelId(vlmId);
89     licenseKeyGroupEntity.setVersion(new Version(versionId));
90
91     LicenseKeyGroupEntity createdLicenseKeyGroup =
92         vendorLicenseManager.createLicenseKeyGroup(licenseKeyGroupEntity);
93     StringWrapperResponse result =
94         createdLicenseKeyGroup != null ? new StringWrapperResponse(createdLicenseKeyGroup.getId())
95             : null;
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 versionId,
110                                         String licenseKeyGroupId, String user) {
111     MdcUtil.initMdc(LoggerServiceName.Update_LKG.toString());
112     LicenseKeyGroupEntity licenseKeyGroupEntity =
113         new MapLicenseKeyGroupRequestDtoToLicenseKeyGroupEntity()
114             .applyMapping(request, LicenseKeyGroupEntity.class);
115     licenseKeyGroupEntity.setVendorLicenseModelId(vlmId);
116     licenseKeyGroupEntity.setVersion(new Version(versionId));
117     licenseKeyGroupEntity.setId(licenseKeyGroupId);
118
119     vendorLicenseManager.updateLicenseKeyGroup(licenseKeyGroupEntity);
120     return Response.ok().build();
121   }
122
123   /**
124    * Gets license key group.
125    *
126    * @param vlmId             the vlm id
127    * @param versionId         the version
128    * @param licenseKeyGroupId the license key group id
129    * @param user              the user
130    * @return the license key group
131    */
132   public Response getLicenseKeyGroup(String vlmId, String versionId, String licenseKeyGroupId,
133                                      String user) {
134     MdcUtil.initMdc(LoggerServiceName.Get_LKG.toString());
135     LicenseKeyGroupEntity lkgInput = new LicenseKeyGroupEntity();
136     lkgInput.setVendorLicenseModelId(vlmId);
137     lkgInput.setVersion(new Version(versionId));
138     lkgInput.setId(licenseKeyGroupId);
139     LicenseKeyGroupEntity licenseKeyGroup = vendorLicenseManager.getLicenseKeyGroup(lkgInput);
140
141     LicenseKeyGroupEntityDto licenseKeyGroupEntityDto = licenseKeyGroup == null ? null :
142         new MapLicenseKeyGroupEntityToLicenseKeyGroupEntityDto()
143             .applyMapping(licenseKeyGroup, LicenseKeyGroupEntityDto.class);
144     return Response.ok(licenseKeyGroupEntityDto).build();
145   }
146
147   /**
148    * Delete license key group response.
149    *
150    * @param vlmId             the vlm id
151    * @param licenseKeyGroupId the license key group id
152    * @param user              the user
153    * @return the response
154    */
155   public Response deleteLicenseKeyGroup(String vlmId, String versionId, String licenseKeyGroupId,
156                                         String user) {
157     MdcUtil.initMdc(LoggerServiceName.Delete_LKG.toString());
158     LicenseKeyGroupEntity lkgInput = new LicenseKeyGroupEntity();
159     lkgInput.setVendorLicenseModelId(vlmId);
160     lkgInput.setVersion(new Version(versionId));
161     lkgInput.setId(licenseKeyGroupId);
162     vendorLicenseManager.deleteLicenseKeyGroup(lkgInput);
163     return Response.ok().build();
164   }
165 }