83519198b7549b579dbf1c94f56f2791fb1a86b7
[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 java.util.Comparator;
24 import java.util.stream.Collectors;
25 import javax.inject.Named;
26 import javax.ws.rs.core.Response;
27 import org.openecomp.sdc.vendorlicense.VendorLicenseManager;
28 import org.openecomp.sdc.vendorlicense.VendorLicenseManagerFactory;
29 import org.openecomp.sdc.vendorlicense.dao.types.EntitlementPoolEntity;
30 import org.openecomp.sdc.versioning.dao.types.Version;
31 import org.openecomp.sdcrests.vendorlicense.rest.EntitlementPools;
32 import org.openecomp.sdcrests.vendorlicense.rest.mapping.MapEntitlementPoolEntityToEntitlementPoolEntityDto;
33 import org.openecomp.sdcrests.vendorlicense.rest.mapping.MapEntitlementPoolRequestDtoToEntitlementPoolEntity;
34 import org.openecomp.sdcrests.vendorlicense.types.EntitlementPoolEntityDto;
35 import org.openecomp.sdcrests.vendorlicense.types.EntitlementPoolRequestDto;
36 import org.openecomp.sdcrests.wrappers.GenericCollectionWrapper;
37 import org.openecomp.sdcrests.wrappers.StringWrapperResponse;
38 import org.springframework.context.annotation.Scope;
39 import org.springframework.stereotype.Service;
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
58         MapEntitlementPoolEntityToEntitlementPoolEntityDto outputMapper =
59                 new MapEntitlementPoolEntityToEntitlementPoolEntityDto();
60
61         GenericCollectionWrapper<EntitlementPoolEntityDto> result = new GenericCollectionWrapper<>(
62                 vendorLicenseManager.listEntitlementPools(vlmId, new Version(versionId)).stream()
63                                     .sorted(Comparator.comparing(EntitlementPoolEntity::getName))
64                                     .map(item -> outputMapper.applyMapping(item, EntitlementPoolEntityDto.class))
65                                     .collect(Collectors.toList()));
66
67         return Response.ok(result).build();
68     }
69
70   /**
71    * Create entitlement pool response.
72    *
73    * @param request the request
74    * @param vlmId   the vlm id
75    * @param user    the user
76    * @return the response
77    */
78   public Response createEntitlementPool(EntitlementPoolRequestDto request, String vlmId,
79                                         String versionId, String user) {
80     EntitlementPoolEntity entitlementPoolEntity =
81         new MapEntitlementPoolRequestDtoToEntitlementPoolEntity()
82             .applyMapping(request, EntitlementPoolEntity.class);
83     entitlementPoolEntity.setVendorLicenseModelId(vlmId);
84     entitlementPoolEntity.setVersion(new Version(versionId));
85
86     EntitlementPoolEntity createdEntitlementPool =
87         vendorLicenseManager.createEntitlementPool(entitlementPoolEntity);
88     StringWrapperResponse result =
89         createdEntitlementPool != null ? new StringWrapperResponse(createdEntitlementPool.getId())
90             : null;
91     return Response.ok(result).build();
92   }
93
94   /**
95    * Update entitlement pool response.
96    *
97    * @param request           the request
98    * @param vlmId             the vlm id
99    * @param entitlementPoolId the entitlement pool id
100    * @param user              the user
101    * @return the response
102    */
103   public Response updateEntitlementPool(EntitlementPoolRequestDto request, String vlmId,
104                                         String versionId, String entitlementPoolId, String user) {
105     EntitlementPoolEntity entitlementPoolEntity =
106         new MapEntitlementPoolRequestDtoToEntitlementPoolEntity()
107             .applyMapping(request, EntitlementPoolEntity.class);
108     entitlementPoolEntity.setVendorLicenseModelId(vlmId);
109     entitlementPoolEntity.setVersion(new Version(versionId));
110     entitlementPoolEntity.setId(entitlementPoolId);
111
112     vendorLicenseManager.updateEntitlementPool(entitlementPoolEntity);
113     return Response.ok().build();
114   }
115
116   /**
117    * Gets entitlement pool.
118    *
119    * @param vlmId             the vlm id
120    * @param versionId           the version id
121    * @param entitlementPoolId the entitlement pool id
122    * @param user              the user
123    * @return the entitlement pool
124    */
125   public Response getEntitlementPool(String vlmId, String versionId, String entitlementPoolId,
126                                      String user) {
127     EntitlementPoolEntity epInput = new EntitlementPoolEntity();
128     epInput.setVendorLicenseModelId(vlmId);
129     epInput.setVersion(new Version(versionId));
130     epInput.setId(entitlementPoolId);
131     EntitlementPoolEntity entitlementPool = vendorLicenseManager.getEntitlementPool(epInput);
132
133     EntitlementPoolEntityDto entitlementPoolEntityDto = entitlementPool == null ? null :
134         new MapEntitlementPoolEntityToEntitlementPoolEntityDto()
135             .applyMapping(entitlementPool, EntitlementPoolEntityDto.class);
136     return Response.ok(entitlementPoolEntityDto).build();
137   }
138
139   /**
140    * Delete entitlement pool response.
141    *
142    * @param vlmId             the vlm id
143    * @param entitlementPoolId the entitlement pool id
144    * @param user              the user
145    * @return the response
146    */
147   public Response deleteEntitlementPool(String vlmId, String versionId, String entitlementPoolId,
148                                         String user) {
149     EntitlementPoolEntity epInput = new EntitlementPoolEntity();
150     epInput.setVendorLicenseModelId(vlmId);
151     epInput.setId(entitlementPoolId);
152     epInput.setVersion(new Version(versionId));
153     vendorLicenseManager.deleteEntitlementPool(epInput);
154     return Response.ok().build();
155   }
156 }