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