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