[SDC] Onboarding 1710 rebase.
[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 / EntitlementPoolLimitsImpl.java
1 package org.openecomp.sdcrests.vendorlicense.rest.services;
2
3
4 import org.openecomp.sdc.logging.context.MdcUtil;
5 import org.openecomp.sdc.logging.context.impl.MdcDataDebugMessage;
6 import org.openecomp.sdc.logging.types.LoggerServiceName;
7 import org.openecomp.sdc.vendorlicense.VendorLicenseManager;
8 import org.openecomp.sdc.vendorlicense.dao.types.EntitlementPoolEntity;
9 import org.openecomp.sdc.vendorlicense.dao.types.LimitEntity;
10 import org.openecomp.sdc.versioning.dao.types.Version;
11 import org.openecomp.sdcrests.vendorlicense.rest.EntitlementPoolLimits;
12 import org.openecomp.sdcrests.vendorlicense.rest.mapping.LimitCreationDto;
13 import org.openecomp.sdcrests.vendorlicense.rest.mapping.MapLimitEntityToLimitCreationDto;
14 import org.openecomp.sdcrests.vendorlicense.rest.mapping.MapLimitEntityToLimitDto;
15 import org.openecomp.sdcrests.vendorlicense.rest.mapping.MapLimitRequestDtoToLimitEntity;
16 import org.openecomp.sdcrests.vendorlicense.types.LimitEntityDto;
17 import org.openecomp.sdcrests.vendorlicense.types.LimitRequestDto;
18 import org.openecomp.sdcrests.wrappers.GenericCollectionWrapper;
19 import org.openecomp.sdcrests.wrappers.StringWrapperResponse;
20 import org.springframework.beans.factory.annotation.Autowired;
21 import org.springframework.context.annotation.Scope;
22 import org.springframework.stereotype.Service;
23
24 import java.util.Collection;
25 import javax.inject.Named;
26 import javax.ws.rs.core.Response;
27
28 @Named
29 @Service("entitlementPoolLimits")
30 @Scope(value = "prototype")
31 public class EntitlementPoolLimitsImpl implements EntitlementPoolLimits {
32   private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
33   @Autowired
34   private VendorLicenseManager vendorLicenseManager;
35
36   @Override
37   public Response createLimit(LimitRequestDto request,
38                               String vlmId,
39                               String versionId,
40                               String entitlementPoolId,
41                               String user) {
42     mdcDataDebugMessage.debugEntryMessage("VLM id", vlmId, "EP id", entitlementPoolId);
43
44     MdcUtil.initMdc(LoggerServiceName.Create_LIMIT.toString());
45     vendorLicenseManager.getEntitlementPool(new EntitlementPoolEntity(vlmId, Version.valueOf
46         (versionId), entitlementPoolId), user);
47
48     LimitEntity limitEntity =
49         new MapLimitRequestDtoToLimitEntity()
50             .applyMapping(request, LimitEntity.class);
51     limitEntity.setEpLkgId(entitlementPoolId);
52     limitEntity.setVendorLicenseModelId(vlmId);
53
54     LimitEntity createdLimit = vendorLicenseManager.createLimit(limitEntity, user);
55     MapLimitEntityToLimitCreationDto mapper = new MapLimitEntityToLimitCreationDto();
56     LimitCreationDto createdLimitDto = mapper.applyMapping(createdLimit, LimitCreationDto
57         .class);
58
59     /*StringWrapperResponse result =
60         createdLimit != null ? new StringWrapperResponse(createdLimit.getId())
61             : null;*/
62
63     mdcDataDebugMessage.debugExitMessage("VLM id", vlmId, "EP id", entitlementPoolId);
64
65     //return Response.ok(result).build();
66     return Response.ok(createdLimitDto != null ? createdLimitDto : null)
67         .build();
68   }
69
70   @Override
71   public Response listLimits(String vlmId, String versionId, String entitlementPoolId, String
72       user) {
73     mdcDataDebugMessage.debugEntryMessage("VLM id", vlmId, "EP id", entitlementPoolId);
74
75     MdcUtil.initMdc(LoggerServiceName.List_EP.toString());
76     vendorLicenseManager.getEntitlementPool(new EntitlementPoolEntity(vlmId, Version.valueOf
77         (versionId), entitlementPoolId), user);
78
79     Collection<LimitEntity> limits =
80         vendorLicenseManager.listLimits(vlmId, Version.valueOf(versionId), entitlementPoolId, user);
81
82     GenericCollectionWrapper<LimitEntityDto> result = new GenericCollectionWrapper<>();
83     MapLimitEntityToLimitDto outputMapper =
84         new MapLimitEntityToLimitDto();
85     for (LimitEntity limit : limits) {
86       result.add(outputMapper.applyMapping(limit, LimitEntityDto.class));
87     }
88
89     mdcDataDebugMessage.debugExitMessage("VLM id", vlmId, "EP id", entitlementPoolId);
90
91     return Response.ok(result).build();
92   }
93
94   @Override
95   public Response getLimit( String vlmId, String versionId, String entitlementPoolId,
96                             String limitId, String user) {
97     mdcDataDebugMessage.debugEntryMessage("VLM id, EP id, Limit Id", vlmId, entitlementPoolId,
98         limitId);
99
100     MdcUtil.initMdc(LoggerServiceName.Get_LIMIT.toString());
101
102     vendorLicenseManager.getEntitlementPool(new EntitlementPoolEntity(vlmId, Version.valueOf
103         (versionId), entitlementPoolId), user);
104     LimitEntity epInput = new LimitEntity();
105     epInput.setVendorLicenseModelId(vlmId);
106     epInput.setVersion(Version.valueOf(versionId));
107     epInput.setEpLkgId(entitlementPoolId);
108     epInput.setId(limitId);
109     LimitEntity limit = vendorLicenseManager.getLimit(epInput, user);
110
111     LimitEntityDto entitlementPoolEntityDto = limit == null ? null :
112         new MapLimitEntityToLimitDto()
113             .applyMapping(limit, LimitEntityDto.class);
114
115     mdcDataDebugMessage.debugExitMessage("VLM id, EP id, Limit Id", vlmId, entitlementPoolId,
116         limitId);
117
118     return Response.ok(entitlementPoolEntityDto).build();
119   }
120
121   @Override
122   public Response updateLimit(LimitRequestDto request,
123                               String vlmId,
124                               String versionId,
125                               String entitlementPoolId,
126                               String limitId,
127                               String user) {
128     mdcDataDebugMessage.debugEntryMessage("VLM id", vlmId, "EP id", entitlementPoolId, "limit Id",
129         limitId);
130
131     MdcUtil.initMdc(LoggerServiceName.Update_LIMIT.toString());
132
133     vendorLicenseManager.getEntitlementPool(new EntitlementPoolEntity(vlmId, Version.valueOf
134         (versionId), entitlementPoolId), user);
135
136     LimitEntity limitEntity =
137         new MapLimitRequestDtoToLimitEntity()
138             .applyMapping(request, LimitEntity.class);
139     limitEntity.setEpLkgId(entitlementPoolId);
140     limitEntity.setVendorLicenseModelId(vlmId);
141     limitEntity.setId(limitId);
142
143     vendorLicenseManager.updateLimit(limitEntity, user);
144
145     mdcDataDebugMessage.debugExitMessage("VLM id", vlmId, "EP id", entitlementPoolId, "limit Id",
146         limitId);
147
148     return Response.ok().build();
149   }
150
151   /**
152      * Delete entitlement pool.
153      *
154      * @param vlmId               the vlm id
155      * @param entitlementPoolId   the entitlement pool id
156      * @param limitId             the limitId
157      * @param user                the user
158      * @return the response
159    */
160   public Response deleteLimit(String vlmId, String versionId, String entitlementPoolId,
161                                String limitId, String user) {
162     mdcDataDebugMessage.debugEntryMessage("VLM id, Verison Id, EP id, Limit Id", vlmId, versionId, entitlementPoolId, limitId);
163
164     MdcUtil.initMdc(LoggerServiceName.Delete_LIMIT.toString());
165     vendorLicenseManager.getEntitlementPool(new EntitlementPoolEntity(vlmId, Version.valueOf
166             (versionId), entitlementPoolId), user);
167
168     LimitEntity limitInput = new LimitEntity();
169     limitInput.setVendorLicenseModelId(vlmId);
170     limitInput.setEpLkgId(entitlementPoolId);
171     limitInput.setId(limitId);
172     vendorLicenseManager.deleteLimit(limitInput, user);
173
174     mdcDataDebugMessage.debugExitMessage("VLM id, Verison Id, EP id, Limit Id", vlmId, versionId, entitlementPoolId, limitId);
175
176     return Response.ok().build();
177   }
178 }