07151d22323c9be3b74f7dc318998e6879b91a2e
[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.EntitlementPoolEntity;
28 import org.openecomp.sdc.versioning.dao.types.Version;
29 import org.openecomp.sdcrests.vendorlicense.rest.EntitlementPools;
30 import org.openecomp.sdcrests.vendorlicense.rest.mapping.MapEntitlementPoolEntityToEntitlementPoolEntityDto;
31 import org.openecomp.sdcrests.vendorlicense.rest.mapping.MapEntitlementPoolRequestDtoToEntitlementPoolEntity;
32 import org.openecomp.sdcrests.vendorlicense.types.EntitlementPoolEntityDto;
33 import org.openecomp.sdcrests.vendorlicense.types.EntitlementPoolRequestDto;
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
39 import javax.inject.Named;
40 import javax.ws.rs.core.Response;
41 import java.util.Collection;
42
43 @Named
44 @Service("entitlementPools")
45 @Scope(value = "prototype")
46 public class EntitlementPoolsImpl implements EntitlementPools {
47   private VendorLicenseManager vendorLicenseManager =
48       VendorLicenseManagerFactory.getInstance().createInterface();
49
50   /**
51    * List entitlement pools 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 listEntitlementPools(String vlmId, String versionId, String user) {
59     MdcUtil.initMdc(LoggerServiceName.List_EP.toString());
60     Collection<EntitlementPoolEntity> entitlementPools =
61         vendorLicenseManager.listEntitlementPools(vlmId, new Version(versionId));
62
63     GenericCollectionWrapper<EntitlementPoolEntityDto> result = new GenericCollectionWrapper<>();
64     MapEntitlementPoolEntityToEntitlementPoolEntityDto outputMapper =
65         new MapEntitlementPoolEntityToEntitlementPoolEntityDto();
66     for (EntitlementPoolEntity ep : entitlementPools) {
67       result.add(outputMapper.applyMapping(ep, EntitlementPoolEntityDto.class));
68     }
69     return Response.ok(result).build();
70   }
71
72   /**
73    * Create entitlement pool response.
74    *
75    * @param request the request
76    * @param vlmId   the vlm id
77    * @param user    the user
78    * @return the response
79    */
80   public Response createEntitlementPool(EntitlementPoolRequestDto request, String vlmId,
81                                         String versionId, String user) {
82     MdcUtil.initMdc(LoggerServiceName.Create_EP.toString());
83     EntitlementPoolEntity entitlementPoolEntity =
84         new MapEntitlementPoolRequestDtoToEntitlementPoolEntity()
85             .applyMapping(request, EntitlementPoolEntity.class);
86     entitlementPoolEntity.setVendorLicenseModelId(vlmId);
87     entitlementPoolEntity.setVersion(new Version(versionId));
88
89     EntitlementPoolEntity createdEntitlementPool =
90         vendorLicenseManager.createEntitlementPool(entitlementPoolEntity);
91     StringWrapperResponse result =
92         createdEntitlementPool != null ? new StringWrapperResponse(createdEntitlementPool.getId())
93             : null;
94     return Response.ok(result).build();
95   }
96
97   /**
98    * Update entitlement pool response.
99    *
100    * @param request           the request
101    * @param vlmId             the vlm id
102    * @param entitlementPoolId the entitlement pool id
103    * @param user              the user
104    * @return the response
105    */
106   public Response updateEntitlementPool(EntitlementPoolRequestDto request, String vlmId,
107                                         String versionId, String entitlementPoolId, String user) {
108     MdcUtil.initMdc(LoggerServiceName.Update_EP.toString());
109     EntitlementPoolEntity entitlementPoolEntity =
110         new MapEntitlementPoolRequestDtoToEntitlementPoolEntity()
111             .applyMapping(request, EntitlementPoolEntity.class);
112     entitlementPoolEntity.setVendorLicenseModelId(vlmId);
113     entitlementPoolEntity.setVersion(new Version(versionId));
114     entitlementPoolEntity.setId(entitlementPoolId);
115
116     vendorLicenseManager.updateEntitlementPool(entitlementPoolEntity);
117     return Response.ok().build();
118   }
119
120   /**
121    * Gets entitlement pool.
122    *
123    * @param vlmId             the vlm id
124    * @param versionId           the version id
125    * @param entitlementPoolId the entitlement pool id
126    * @param user              the user
127    * @return the entitlement pool
128    */
129   public Response getEntitlementPool(String vlmId, String versionId, String entitlementPoolId,
130                                      String user) {
131     MdcUtil.initMdc(LoggerServiceName.Get_EP.toString());
132     EntitlementPoolEntity epInput = new EntitlementPoolEntity();
133     epInput.setVendorLicenseModelId(vlmId);
134     epInput.setVersion(new Version(versionId));
135     epInput.setId(entitlementPoolId);
136     EntitlementPoolEntity entitlementPool = vendorLicenseManager.getEntitlementPool(epInput);
137
138     EntitlementPoolEntityDto entitlementPoolEntityDto = entitlementPool == null ? null :
139         new MapEntitlementPoolEntityToEntitlementPoolEntityDto()
140             .applyMapping(entitlementPool, EntitlementPoolEntityDto.class);
141     return Response.ok(entitlementPoolEntityDto).build();
142   }
143
144   /**
145    * Delete entitlement pool response.
146    *
147    * @param vlmId             the vlm id
148    * @param entitlementPoolId the entitlement pool id
149    * @param user              the user
150    * @return the response
151    */
152   public Response deleteEntitlementPool(String vlmId, String versionId, String entitlementPoolId,
153                                         String user) {
154     MdcUtil.initMdc(LoggerServiceName.Delete_EP.toString());
155     EntitlementPoolEntity epInput = new EntitlementPoolEntity();
156     epInput.setVendorLicenseModelId(vlmId);
157     epInput.setId(entitlementPoolId);
158     epInput.setVersion(new Version(versionId));
159     vendorLicenseManager.deleteEntitlementPool(epInput);
160     return Response.ok().build();
161   }
162 }