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