36e25e0f2e3796fc7c43477dc474398b33c23df8
[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   @Override
38   public Response createLimit(LimitRequestDto request,
39                               String vlmId,
40                               String versionId,
41                               String entitlementPoolId,
42                               String user) {
43     mdcDataDebugMessage.debugEntryMessage("VLM id", vlmId, "EP id", entitlementPoolId);
44
45     MdcUtil.initMdc(LoggerServiceName.Create_LIMIT.toString());
46     vendorLicenseManager.getEntitlementPool(new EntitlementPoolEntity(vlmId, Version.valueOf
47         (versionId), entitlementPoolId), user);
48
49     LimitEntity limitEntity =
50         new MapLimitRequestDtoToLimitEntity()
51             .applyMapping(request, LimitEntity.class);
52     limitEntity.setEpLkgId(entitlementPoolId);
53     limitEntity.setVendorLicenseModelId(vlmId);
54
55     LimitEntity createdLimit = vendorLicenseManager.createLimit(limitEntity, user);
56     MapLimitEntityToLimitCreationDto mapper = new MapLimitEntityToLimitCreationDto();
57     LimitCreationDto createdLimitDto = mapper.applyMapping(createdLimit, LimitCreationDto
58         .class);
59
60     /*StringWrapperResponse result =
61         createdLimit != null ? new StringWrapperResponse(createdLimit.getId())
62             : null;*/
63
64     mdcDataDebugMessage.debugExitMessage("VLM id", vlmId, "EP id", entitlementPoolId);
65
66     //return Response.ok(result).build();
67     return Response.ok(createdLimitDto != null ? createdLimitDto : null)
68         .build();
69   }
70
71   @Override
72   public Response listLimits(String vlmId, String versionId, String entitlementPoolId, String
73       user) {
74     mdcDataDebugMessage.debugEntryMessage("VLM id", vlmId, "EP id", entitlementPoolId);
75
76     MdcUtil.initMdc(LoggerServiceName.List_EP.toString());
77     vendorLicenseManager.getEntitlementPool(new EntitlementPoolEntity(vlmId, Version.valueOf
78         (versionId), entitlementPoolId), user);
79
80     Collection<LimitEntity> limits =
81         vendorLicenseManager.listLimits(vlmId, Version.valueOf(versionId), entitlementPoolId, user);
82
83     GenericCollectionWrapper<LimitEntityDto> result = new GenericCollectionWrapper<>();
84     MapLimitEntityToLimitDto outputMapper =
85         new MapLimitEntityToLimitDto();
86     for (LimitEntity limit : limits) {
87       result.add(outputMapper.applyMapping(limit, LimitEntityDto.class));
88     }
89
90     mdcDataDebugMessage.debugExitMessage("VLM id", vlmId, "EP id", entitlementPoolId);
91
92     return Response.ok(result).build();
93   }
94
95   @Override
96   public Response getLimit( String vlmId, String versionId, String entitlementPoolId,
97                             String limitId, String user) {
98     mdcDataDebugMessage.debugEntryMessage("VLM id, EP id, Limit Id", vlmId, entitlementPoolId,
99         limitId);
100
101     MdcUtil.initMdc(LoggerServiceName.Get_LIMIT.toString());
102
103     vendorLicenseManager.getEntitlementPool(new EntitlementPoolEntity(vlmId, Version.valueOf
104         (versionId), entitlementPoolId), user);
105     LimitEntity epInput = new LimitEntity();
106     epInput.setVendorLicenseModelId(vlmId);
107     epInput.setVersion(Version.valueOf(versionId));
108     epInput.setEpLkgId(entitlementPoolId);
109     epInput.setId(limitId);
110     LimitEntity limit = vendorLicenseManager.getLimit(epInput, user);
111
112     LimitEntityDto entitlementPoolEntityDto = limit == null ? null :
113         new MapLimitEntityToLimitDto()
114             .applyMapping(limit, LimitEntityDto.class);
115
116     mdcDataDebugMessage.debugExitMessage("VLM id, EP id, Limit Id", vlmId, entitlementPoolId,
117         limitId);
118
119     return Response.ok(entitlementPoolEntityDto).build();
120   }
121
122   @Override
123   public Response updateLimit(LimitRequestDto request,
124                               String vlmId,
125                               String versionId,
126                               String entitlementPoolId,
127                               String limitId,
128                               String user) {
129     mdcDataDebugMessage.debugEntryMessage("VLM id", vlmId, "EP id", entitlementPoolId, "limit Id",
130         limitId);
131
132     MdcUtil.initMdc(LoggerServiceName.Update_LIMIT.toString());
133
134     vendorLicenseManager.getEntitlementPool(new EntitlementPoolEntity(vlmId, Version.valueOf
135         (versionId), entitlementPoolId), user);
136
137     LimitEntity limitEntity =
138         new MapLimitRequestDtoToLimitEntity()
139             .applyMapping(request, LimitEntity.class);
140     limitEntity.setEpLkgId(entitlementPoolId);
141     limitEntity.setVendorLicenseModelId(vlmId);
142     limitEntity.setId(limitId);
143
144     vendorLicenseManager.updateLimit(limitEntity, user);
145
146     mdcDataDebugMessage.debugExitMessage("VLM id", vlmId, "EP id", entitlementPoolId, "limit Id",
147         limitId);
148
149     return Response.ok().build();
150   }
151
152   /**
153      * Delete entitlement pool.
154      *
155      * @param vlmId               the vlm id
156      * @param entitlementPoolId   the entitlement pool id
157      * @param limitId             the limitId
158      * @param user                the user
159      * @return the response
160    */
161   public Response deleteLimit(String vlmId, String versionId, String entitlementPoolId,
162                                String limitId, String user) {
163     mdcDataDebugMessage.debugEntryMessage("VLM id, Verison Id, EP id, Limit Id", vlmId, versionId, entitlementPoolId, limitId);
164
165     MdcUtil.initMdc(LoggerServiceName.Delete_LIMIT.toString());
166     vendorLicenseManager.getEntitlementPool(new EntitlementPoolEntity(vlmId, Version.valueOf
167             (versionId), entitlementPoolId), user);
168
169     LimitEntity limitInput = new LimitEntity();
170     limitInput.setVendorLicenseModelId(vlmId);
171     limitInput.setEpLkgId(entitlementPoolId);
172     limitInput.setId(limitId);
173     vendorLicenseManager.deleteLimit(limitInput, user);
174
175     mdcDataDebugMessage.debugExitMessage("VLM id, Verison Id, EP id, Limit Id", vlmId, versionId, entitlementPoolId, limitId);
176
177     return Response.ok().build();
178   }
179 }