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