Add collaboration feature
[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.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;
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   private VendorLicenseManager vendorLicenseManager =
51       VendorLicenseManagerFactory.getInstance().createInterface();
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, new Version(versionId));
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     entitlementPoolEntity.setVersion(new Version(versionId));
99
100     EntitlementPoolEntity createdEntitlementPool =
101         vendorLicenseManager.createEntitlementPool(entitlementPoolEntity);
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.setVersion(new Version(versionId));
131     entitlementPoolEntity.setId(entitlementPoolId);
132
133     vendorLicenseManager.updateEntitlementPool(entitlementPoolEntity);
134
135     mdcDataDebugMessage.debugExitMessage("VLM id, EP id", vlmId, entitlementPoolId);
136
137     return Response.ok().build();
138   }
139
140   /**
141    * Gets entitlement pool.
142    *
143    * @param vlmId             the vlm id
144    * @param versionId           the version id
145    * @param entitlementPoolId the entitlement pool id
146    * @param user              the user
147    * @return the entitlement pool
148    */
149   public Response getEntitlementPool(String vlmId, String versionId, String entitlementPoolId,
150                                      String user) {
151
152     mdcDataDebugMessage.debugEntryMessage("VLM id, EP id", vlmId, entitlementPoolId);
153
154     MdcUtil.initMdc(LoggerServiceName.Get_EP.toString());
155     EntitlementPoolEntity epInput = new EntitlementPoolEntity();
156     epInput.setVendorLicenseModelId(vlmId);
157     epInput.setVersion(new Version(versionId));
158     epInput.setId(entitlementPoolId);
159     EntitlementPoolEntity entitlementPool = vendorLicenseManager.getEntitlementPool(epInput);
160
161     EntitlementPoolEntityDto entitlementPoolEntityDto = entitlementPool == null ? null :
162         new MapEntitlementPoolEntityToEntitlementPoolEntityDto()
163             .applyMapping(entitlementPool, EntitlementPoolEntityDto.class);
164
165     mdcDataDebugMessage.debugExitMessage("VLM id, EP id", vlmId, entitlementPoolId);
166
167     return Response.ok(entitlementPoolEntityDto).build();
168   }
169
170   /**
171    * Delete entitlement pool response.
172    *
173    * @param vlmId             the vlm id
174    * @param entitlementPoolId the entitlement pool id
175    * @param user              the user
176    * @return the response
177    */
178   public Response deleteEntitlementPool(String vlmId, String versionId, String entitlementPoolId,
179                                         String user) {
180
181     mdcDataDebugMessage.debugEntryMessage("VLM id, EP id", vlmId, entitlementPoolId);
182
183     MdcUtil.initMdc(LoggerServiceName.Delete_EP.toString());
184     EntitlementPoolEntity epInput = new EntitlementPoolEntity();
185     epInput.setVendorLicenseModelId(vlmId);
186     epInput.setId(entitlementPoolId);
187     epInput.setVersion(new Version(versionId));
188     vendorLicenseManager.deleteEntitlementPool(epInput);
189
190     mdcDataDebugMessage.debugExitMessage("VLM id, EP id", vlmId, entitlementPoolId);
191
192     return Response.ok().build();
193   }
194 }