2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdcrests.vendorlicense.rest.services;
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;
39 import javax.inject.Named;
40 import javax.ws.rs.core.Response;
41 import java.util.Collection;
44 @Service("entitlementPools")
45 @Scope(value = "prototype")
46 public class EntitlementPoolsImpl implements EntitlementPools {
47 private VendorLicenseManager vendorLicenseManager =
48 VendorLicenseManagerFactory.getInstance().createInterface();
51 * List entitlement pools response.
53 * @param vlmId the vlm id
54 * @param versionId the version
55 * @param user the user
56 * @return the response
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));
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));
69 return Response.ok(result).build();
73 * Create entitlement pool response.
75 * @param request the request
76 * @param vlmId the vlm id
77 * @param user the user
78 * @return the response
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));
89 EntitlementPoolEntity createdEntitlementPool =
90 vendorLicenseManager.createEntitlementPool(entitlementPoolEntity);
91 StringWrapperResponse result =
92 createdEntitlementPool != null ? new StringWrapperResponse(createdEntitlementPool.getId())
94 return Response.ok(result).build();
98 * Update entitlement pool response.
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
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);
116 vendorLicenseManager.updateEntitlementPool(entitlementPoolEntity);
117 return Response.ok().build();
121 * Gets entitlement pool.
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
129 public Response getEntitlementPool(String vlmId, String versionId, String entitlementPoolId,
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);
138 EntitlementPoolEntityDto entitlementPoolEntityDto = entitlementPool == null ? null :
139 new MapEntitlementPoolEntityToEntitlementPoolEntityDto()
140 .applyMapping(entitlementPool, EntitlementPoolEntityDto.class);
141 return Response.ok(entitlementPoolEntityDto).build();
145 * Delete entitlement pool response.
147 * @param vlmId the vlm id
148 * @param entitlementPoolId the entitlement pool id
149 * @param user the user
150 * @return the response
152 public Response deleteEntitlementPool(String vlmId, String versionId, String entitlementPoolId,
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();