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.common.errors.CoreException;
24 import org.openecomp.sdc.common.errors.ErrorCode;
25 import org.openecomp.sdc.common.togglz.ToggleableFeature;
26 import org.openecomp.sdc.vendorlicense.VendorLicenseManager;
27 import org.openecomp.sdc.vendorlicense.VendorLicenseManagerFactory;
28 import org.openecomp.sdc.vendorlicense.dao.types.EntitlementPoolEntity;
29 import org.openecomp.sdc.versioning.dao.types.Version;
30 import org.openecomp.sdcrests.vendorlicense.rest.EntitlementPools;
31 import org.openecomp.sdcrests.vendorlicense.rest.mapping.MapEntitlementPoolEntityToEntitlementPoolEntityDto;
32 import org.openecomp.sdcrests.vendorlicense.rest.mapping.MapEntitlementPoolRequestDtoToEntitlementPoolEntity;
33 import org.openecomp.sdcrests.vendorlicense.types.EntitlementPoolEntityDto;
34 import org.openecomp.sdcrests.vendorlicense.types.EntitlementPoolRequestDto;
35 import org.openecomp.sdcrests.wrappers.GenericCollectionWrapper;
36 import org.openecomp.sdcrests.wrappers.StringWrapperResponse;
37 import org.springframework.context.annotation.Scope;
38 import org.springframework.stereotype.Service;
40 import javax.inject.Named;
41 import javax.ws.rs.core.Response;
42 import java.util.Collection;
43 import java.util.Objects;
46 @Service("entitlementPools")
47 @Scope(value = "prototype")
48 public class EntitlementPoolsImpl implements EntitlementPools {
49 private VendorLicenseManager vendorLicenseManager =
50 VendorLicenseManagerFactory.getInstance().createInterface();
53 * List entitlement pools response.
55 * @param vlmId the vlm id
56 * @param versionId the version
57 * @param user the user
58 * @return the response
60 public Response listEntitlementPools(String vlmId, String versionId, String user) {
61 Collection<EntitlementPoolEntity> entitlementPools =
62 vendorLicenseManager.listEntitlementPools(vlmId, new Version(versionId));
64 GenericCollectionWrapper<EntitlementPoolEntityDto> result = new GenericCollectionWrapper<>();
65 MapEntitlementPoolEntityToEntitlementPoolEntityDto outputMapper =
66 new MapEntitlementPoolEntityToEntitlementPoolEntityDto();
67 for (EntitlementPoolEntity ep : entitlementPools) {
68 result.add(outputMapper.applyMapping(ep, EntitlementPoolEntityDto.class));
70 return Response.ok(result).build();
74 * Create entitlement pool response.
76 * @param request the request
77 * @param vlmId the vlm id
78 * @param user the user
79 * @return the response
81 public Response createEntitlementPool(EntitlementPoolRequestDto request, String vlmId,
82 String versionId, String user) {
83 if (ToggleableFeature.MRN.isActive()) {
84 if (Objects.isNull(request.getManufacturerReferenceNumber())) {
85 throw new CoreException((new ErrorCode.ErrorCodeBuilder().withMessage("Field does not conform to "
86 + "predefined criteria : manufacturerReferenceNumber : is mandatory and should not be empty").build()));
89 EntitlementPoolEntity entitlementPoolEntity =
90 new MapEntitlementPoolRequestDtoToEntitlementPoolEntity()
91 .applyMapping(request, EntitlementPoolEntity.class);
92 entitlementPoolEntity.setVendorLicenseModelId(vlmId);
93 entitlementPoolEntity.setVersion(new Version(versionId));
95 EntitlementPoolEntity createdEntitlementPool =
96 vendorLicenseManager.createEntitlementPool(entitlementPoolEntity);
97 StringWrapperResponse result =
98 createdEntitlementPool != null ? new StringWrapperResponse(createdEntitlementPool.getId())
100 return Response.ok(result).build();
104 * Update entitlement pool response.
106 * @param request the request
107 * @param vlmId the vlm id
108 * @param entitlementPoolId the entitlement pool id
109 * @param user the user
110 * @return the response
112 public Response updateEntitlementPool(EntitlementPoolRequestDto request, String vlmId,
113 String versionId, String entitlementPoolId, String user) {
114 EntitlementPoolEntity entitlementPoolEntity =
115 new MapEntitlementPoolRequestDtoToEntitlementPoolEntity()
116 .applyMapping(request, EntitlementPoolEntity.class);
117 entitlementPoolEntity.setVendorLicenseModelId(vlmId);
118 entitlementPoolEntity.setVersion(new Version(versionId));
119 entitlementPoolEntity.setId(entitlementPoolId);
121 vendorLicenseManager.updateEntitlementPool(entitlementPoolEntity);
122 return Response.ok().build();
126 * Gets entitlement pool.
128 * @param vlmId the vlm id
129 * @param versionId the version id
130 * @param entitlementPoolId the entitlement pool id
131 * @param user the user
132 * @return the entitlement pool
134 public Response getEntitlementPool(String vlmId, String versionId, String entitlementPoolId,
136 EntitlementPoolEntity epInput = new EntitlementPoolEntity();
137 epInput.setVendorLicenseModelId(vlmId);
138 epInput.setVersion(new Version(versionId));
139 epInput.setId(entitlementPoolId);
140 EntitlementPoolEntity entitlementPool = vendorLicenseManager.getEntitlementPool(epInput);
142 EntitlementPoolEntityDto entitlementPoolEntityDto = entitlementPool == null ? null :
143 new MapEntitlementPoolEntityToEntitlementPoolEntityDto()
144 .applyMapping(entitlementPool, EntitlementPoolEntityDto.class);
145 return Response.ok(entitlementPoolEntityDto).build();
149 * Delete entitlement pool response.
151 * @param vlmId the vlm id
152 * @param entitlementPoolId the entitlement pool id
153 * @param user the user
154 * @return the response
156 public Response deleteEntitlementPool(String vlmId, String versionId, String entitlementPoolId,
158 EntitlementPoolEntity epInput = new EntitlementPoolEntity();
159 epInput.setVendorLicenseModelId(vlmId);
160 epInput.setId(entitlementPoolId);
161 epInput.setVersion(new Version(versionId));
162 vendorLicenseManager.deleteEntitlementPool(epInput);
163 return Response.ok().build();