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 / EntitlementPoolsImpl.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.EntitlementPoolEntity;
25 import org.openecomp.sdc.versioning.dao.types.Version;
26
27 import org.openecomp.sdcrests.vendorlicense.rest.EntitlementPools;
28 import org.openecomp.sdcrests.vendorlicense.rest.mapping.MapEntitlementPoolEntityToEntitlementPoolEntityDto;
29 import org.openecomp.sdcrests.vendorlicense.rest.mapping.MapEntitlementPoolRequestDtoToEntitlementPoolEntity;
30 import org.openecomp.sdcrests.vendorlicense.types.EntitlementPoolEntityDto;
31 import org.openecomp.sdcrests.vendorlicense.types.EntitlementPoolRequestDto;
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
39 import java.util.Collection;
40 import javax.inject.Named;
41 import javax.ws.rs.core.Response;
42
43 @Named
44 @Service("entitlementPools")
45 @Scope(value = "prototype")
46 public class EntitlementPoolsImpl implements EntitlementPools {
47
48   @Autowired
49   private VendorLicenseManager vendorLicenseManager;
50
51   /**
52    * List entitlement pools response.
53    *
54    * @param vlmId   the vlm id
55    * @param version the version
56    * @param user    the user
57    * @return the response
58    */
59   public Response listEntitlementPools(String vlmId, String version, String user) {
60     Collection<EntitlementPoolEntity> entitlementPools =
61         vendorLicenseManager.listEntitlementPools(vlmId, Version.valueOf(version), user);
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
70     return Response.ok(result).build();
71   }
72
73   /**
74    * Create entitlement pool 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 createEntitlementPool(EntitlementPoolRequestDto request, String vlmId,
82                                         String user) {
83     EntitlementPoolEntity entitlementPoolEntity =
84         new MapEntitlementPoolRequestDtoToEntitlementPoolEntity()
85             .applyMapping(request, EntitlementPoolEntity.class);
86     entitlementPoolEntity.setVendorLicenseModelId(vlmId);
87
88     EntitlementPoolEntity createdEntitlementPool =
89         vendorLicenseManager.createEntitlementPool(entitlementPoolEntity, user);
90     StringWrapperResponse result =
91         createdEntitlementPool != null ? new StringWrapperResponse(createdEntitlementPool.getId())
92             : null;
93
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 entitlementPoolId, String user) {
108     EntitlementPoolEntity entitlementPoolEntity =
109         new MapEntitlementPoolRequestDtoToEntitlementPoolEntity()
110             .applyMapping(request, EntitlementPoolEntity.class);
111
112     entitlementPoolEntity.setVendorLicenseModelId(vlmId);
113     entitlementPoolEntity.setId(entitlementPoolId);
114
115     vendorLicenseManager.updateEntitlementPool(entitlementPoolEntity, user);
116     return Response.ok().build();
117   }
118
119   /**
120    * Gets entitlement pool.
121    *
122    * @param vlmId             the vlm id
123    * @param version           the version
124    * @param entitlementPoolId the entitlement pool id
125    * @param user              the user
126    * @return the entitlement pool
127    */
128   public Response getEntitlementPool(String vlmId, String version, String entitlementPoolId,
129                                      String user) {
130     EntitlementPoolEntity epInput = new EntitlementPoolEntity();
131     epInput.setVendorLicenseModelId(vlmId);
132     epInput.setVersion(Version.valueOf(version));
133     epInput.setId(entitlementPoolId);
134     EntitlementPoolEntity entitlementPool = vendorLicenseManager.getEntitlementPool(epInput, user);
135
136     EntitlementPoolEntityDto entitlementPoolEntityDto = entitlementPool == null ? null :
137         new MapEntitlementPoolEntityToEntitlementPoolEntityDto()
138             .applyMapping(entitlementPool, EntitlementPoolEntityDto.class);
139     return Response.ok(entitlementPoolEntityDto).build();
140   }
141
142   /**
143    * Delete entitlement pool response.
144    *
145    * @param vlmId             the vlm id
146    * @param entitlementPoolId the entitlement pool id
147    * @param user              the user
148    * @return the response
149    */
150   public Response deleteEntitlementPool(String vlmId, String entitlementPoolId, String user) {
151     EntitlementPoolEntity epInput = new EntitlementPoolEntity();
152     epInput.setVendorLicenseModelId(vlmId);
153     epInput.setId(entitlementPoolId);
154     vendorLicenseManager.deleteEntitlementPool(epInput, user);
155     return Response.ok().build();
156   }
157 }