[SDC-29] Amdocs OnBoard 1707 initial commit.
[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.logging.context.MdcUtil;
24 import org.openecomp.sdc.logging.context.impl.MdcDataDebugMessage;
25 import org.openecomp.sdc.logging.types.LoggerServiceName;
26 import org.openecomp.sdc.vendorlicense.VendorLicenseManager;
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.beans.factory.annotation.Autowired;
37 import org.springframework.context.annotation.Scope;
38 import org.springframework.stereotype.Service;
39
40 import javax.inject.Named;
41 import javax.ws.rs.core.Response;
42 import java.util.Collection;
43
44 @Named
45 @Service("entitlementPools")
46 @Scope(value = "prototype")
47 public class EntitlementPoolsImpl implements EntitlementPools {
48
49   private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
50   @Autowired
51   private VendorLicenseManager vendorLicenseManager;
52
53   /**
54    * List entitlement pools response.
55    *
56    * @param vlmId     the vlm id
57    * @param versionId the version
58    * @param user      the user
59    * @return the response
60    */
61   public Response listEntitlementPools(String vlmId, String versionId, String user) {
62     mdcDataDebugMessage.debugEntryMessage("VLM id", vlmId);
63
64     MdcUtil.initMdc(LoggerServiceName.List_EP.toString());
65     Collection<EntitlementPoolEntity> entitlementPools =
66         vendorLicenseManager.listEntitlementPools(vlmId, Version.valueOf(versionId), user);
67
68     GenericCollectionWrapper<EntitlementPoolEntityDto> result = new GenericCollectionWrapper<>();
69     MapEntitlementPoolEntityToEntitlementPoolEntityDto outputMapper =
70         new MapEntitlementPoolEntityToEntitlementPoolEntityDto();
71     for (EntitlementPoolEntity ep : entitlementPools) {
72       result.add(outputMapper.applyMapping(ep, EntitlementPoolEntityDto.class));
73     }
74
75     mdcDataDebugMessage.debugExitMessage("VLM id", vlmId);
76
77     return Response.ok(result).build();
78   }
79
80   /**
81    * Create entitlement pool response.
82    *
83    * @param request the request
84    * @param vlmId   the vlm id
85    * @param user    the user
86    * @return the response
87    */
88   public Response createEntitlementPool(EntitlementPoolRequestDto request, String vlmId,
89                                         String versionId, String user) {
90
91     mdcDataDebugMessage.debugEntryMessage("VLM id", vlmId);
92
93     MdcUtil.initMdc(LoggerServiceName.Create_EP.toString());
94     EntitlementPoolEntity entitlementPoolEntity =
95         new MapEntitlementPoolRequestDtoToEntitlementPoolEntity()
96             .applyMapping(request, EntitlementPoolEntity.class);
97     entitlementPoolEntity.setVendorLicenseModelId(vlmId);
98
99     EntitlementPoolEntity createdEntitlementPool =
100         vendorLicenseManager.createEntitlementPool(entitlementPoolEntity, user);
101     StringWrapperResponse result =
102         createdEntitlementPool != null ? new StringWrapperResponse(createdEntitlementPool.getId())
103             : null;
104
105     mdcDataDebugMessage.debugExitMessage("VLM id", vlmId);
106
107     return Response.ok(result).build();
108   }
109
110   /**
111    * Update entitlement pool response.
112    *
113    * @param request           the request
114    * @param vlmId             the vlm id
115    * @param entitlementPoolId the entitlement pool id
116    * @param user              the user
117    * @return the response
118    */
119   public Response updateEntitlementPool(EntitlementPoolRequestDto request, String vlmId,
120                                         String versionId, String entitlementPoolId, String user) {
121
122     mdcDataDebugMessage.debugEntryMessage("VLM id, EP id", vlmId, entitlementPoolId);
123
124     MdcUtil.initMdc(LoggerServiceName.Update_EP.toString());
125     EntitlementPoolEntity entitlementPoolEntity =
126         new MapEntitlementPoolRequestDtoToEntitlementPoolEntity()
127             .applyMapping(request, EntitlementPoolEntity.class);
128     entitlementPoolEntity.setVendorLicenseModelId(vlmId);
129     entitlementPoolEntity.setId(entitlementPoolId);
130
131     vendorLicenseManager.updateEntitlementPool(entitlementPoolEntity, user);
132
133     mdcDataDebugMessage.debugExitMessage("VLM id, EP id", vlmId, entitlementPoolId);
134
135     return Response.ok().build();
136   }
137
138   /**
139    * Gets entitlement pool.
140    *
141    * @param vlmId             the vlm id
142    * @param version           the version
143    * @param entitlementPoolId the entitlement pool id
144    * @param user              the user
145    * @return the entitlement pool
146    */
147   public Response getEntitlementPool(String vlmId, String version, String entitlementPoolId,
148                                      String user) {
149
150     mdcDataDebugMessage.debugEntryMessage("VLM id, EP id", vlmId, entitlementPoolId);
151
152     MdcUtil.initMdc(LoggerServiceName.Get_EP.toString());
153     EntitlementPoolEntity epInput = new EntitlementPoolEntity();
154     epInput.setVendorLicenseModelId(vlmId);
155     epInput.setVersion(Version.valueOf(version));
156     epInput.setId(entitlementPoolId);
157     EntitlementPoolEntity entitlementPool = vendorLicenseManager.getEntitlementPool(epInput, user);
158
159     EntitlementPoolEntityDto entitlementPoolEntityDto = entitlementPool == null ? null :
160         new MapEntitlementPoolEntityToEntitlementPoolEntityDto()
161             .applyMapping(entitlementPool, EntitlementPoolEntityDto.class);
162
163     mdcDataDebugMessage.debugExitMessage("VLM id, EP id", vlmId, entitlementPoolId);
164
165     return Response.ok(entitlementPoolEntityDto).build();
166   }
167
168   /**
169    * Delete entitlement pool response.
170    *
171    * @param vlmId             the vlm id
172    * @param entitlementPoolId the entitlement pool id
173    * @param user              the user
174    * @return the response
175    */
176   public Response deleteEntitlementPool(String vlmId, String versionId, String entitlementPoolId,
177                                         String user) {
178
179     mdcDataDebugMessage.debugEntryMessage("VLM id, EP id", vlmId, entitlementPoolId);
180
181     MdcUtil.initMdc(LoggerServiceName.Delete_EP.toString());
182     EntitlementPoolEntity epInput = new EntitlementPoolEntity();
183     epInput.setVendorLicenseModelId(vlmId);
184     epInput.setId(entitlementPoolId);
185     vendorLicenseManager.deleteEntitlementPool(epInput, user);
186
187     mdcDataDebugMessage.debugExitMessage("VLM id, EP id", vlmId, entitlementPoolId);
188
189     return Response.ok().build();
190   }
191 }