09a4ca8ebe998bdf152216cb4abc285a3edc5fcb
[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.types.LoggerServiceName;
6 import org.openecomp.sdc.vendorlicense.VendorLicenseManager;
7 import org.openecomp.sdc.vendorlicense.VendorLicenseManagerFactory;
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.springframework.context.annotation.Scope;
20 import org.springframework.stereotype.Service;
21
22 import javax.inject.Named;
23 import javax.ws.rs.core.Response;
24 import java.util.Collection;
25
26 @Named
27 @Service("entitlementPoolLimits")
28 @Scope(value = "prototype")
29 public class EntitlementPoolLimitsImpl implements EntitlementPoolLimits {
30   private VendorLicenseManager vendorLicenseManager =
31       VendorLicenseManagerFactory.getInstance().createInterface();
32
33   public static final String parent = "EntitlementPool";
34
35   @Override
36   public Response createLimit(LimitRequestDto request,
37                               String vlmId,
38                               String versionId,
39                               String entitlementPoolId,
40                               String user) {
41     MdcUtil.initMdc(LoggerServiceName.Create_LIMIT.toString());
42     Version version = new Version(versionId);
43     vendorLicenseManager
44         .getEntitlementPool(new EntitlementPoolEntity(vlmId, version, entitlementPoolId));
45
46     LimitEntity limitEntity =
47         new MapLimitRequestDtoToLimitEntity().applyMapping(request, LimitEntity.class);
48     limitEntity.setVendorLicenseModelId(vlmId);
49     limitEntity.setVersion(version);
50     limitEntity.setEpLkgId(entitlementPoolId);
51     limitEntity.setParent(parent);
52
53     LimitEntity createdLimit = vendorLicenseManager.createLimit(limitEntity);
54     MapLimitEntityToLimitCreationDto mapper = new MapLimitEntityToLimitCreationDto();
55     LimitCreationDto createdLimitDto = mapper.applyMapping(createdLimit, LimitCreationDto.class);
56
57     /*StringWrapperResponse result =
58         createdLimit != null ? new StringWrapperResponse(createdLimit.getId())
59             : null;*/
60     //return Response.ok(result).build();
61     return Response.ok(createdLimitDto != null ? createdLimitDto : null).build();
62   }
63
64   @Override
65   public Response listLimits(String vlmId, String versionId, String entitlementPoolId, String
66       user) {
67     MdcUtil.initMdc(LoggerServiceName.List_EP.toString());
68     Version version = new Version(versionId);
69     vendorLicenseManager
70         .getEntitlementPool(new EntitlementPoolEntity(vlmId, version, entitlementPoolId));
71
72     Collection<LimitEntity> limits =
73         vendorLicenseManager.listLimits(vlmId, version, entitlementPoolId);
74
75     GenericCollectionWrapper<LimitEntityDto> result = new GenericCollectionWrapper<>();
76     MapLimitEntityToLimitDto outputMapper =
77         new MapLimitEntityToLimitDto();
78     for (LimitEntity limit : limits) {
79       result.add(outputMapper.applyMapping(limit, LimitEntityDto.class));
80     }
81     return Response.ok(result).build();
82   }
83
84   @Override
85   public Response getLimit(String vlmId, String versionId, String entitlementPoolId,
86                            String limitId, String user) {
87     MdcUtil.initMdc(LoggerServiceName.Get_LIMIT.toString());
88
89     Version version = new Version(versionId);
90     vendorLicenseManager
91         .getEntitlementPool(new EntitlementPoolEntity(vlmId, version, entitlementPoolId));
92     LimitEntity limitInput = new LimitEntity();
93     limitInput.setVendorLicenseModelId(vlmId);
94     limitInput.setVersion(version);
95     limitInput.setEpLkgId(entitlementPoolId);
96     limitInput.setId(limitId);
97     LimitEntity limit = vendorLicenseManager.getLimit(limitInput);
98
99     LimitEntityDto entitlementPoolEntityDto = limit == null ? null :
100         new MapLimitEntityToLimitDto().applyMapping(limit, LimitEntityDto.class);
101     return Response.ok(entitlementPoolEntityDto).build();
102   }
103
104   @Override
105   public Response updateLimit(LimitRequestDto request,
106                               String vlmId,
107                               String versionId,
108                               String entitlementPoolId,
109                               String limitId,
110                               String user) {
111     MdcUtil.initMdc(LoggerServiceName.Update_LIMIT.toString());
112
113     Version version = new Version(versionId);
114     vendorLicenseManager
115         .getEntitlementPool(new EntitlementPoolEntity(vlmId, version, entitlementPoolId));
116
117     LimitEntity limitEntity =
118         new MapLimitRequestDtoToLimitEntity().applyMapping(request, LimitEntity.class);
119     limitEntity.setVendorLicenseModelId(vlmId);
120     limitEntity.setVersion(version);
121     limitEntity.setEpLkgId(entitlementPoolId);
122     limitEntity.setId(limitId);
123     limitEntity.setParent(parent);
124
125     vendorLicenseManager.updateLimit(limitEntity);
126     return Response.ok().build();
127   }
128
129   /**
130    * Delete entitlement pool.
131    *
132    * @param vlmId             the vlm id
133    * @param entitlementPoolId the entitlement pool id
134    * @param limitId           the limitId
135    * @param user              the user
136    * @return the response
137    */
138   public Response deleteLimit(String vlmId, String versionId, String entitlementPoolId,
139                               String limitId, String user) {
140     MdcUtil.initMdc(LoggerServiceName.Delete_LIMIT.toString());
141
142     Version version = new Version(versionId);
143     vendorLicenseManager.getEntitlementPool(
144         new EntitlementPoolEntity(vlmId, version, entitlementPoolId));
145
146     LimitEntity limitInput = new LimitEntity();
147     limitInput.setVendorLicenseModelId(vlmId);
148     limitInput.setVersion(version);
149     limitInput.setEpLkgId(entitlementPoolId);
150     limitInput.setId(limitId);
151     limitInput.setParent(parent);
152
153     vendorLicenseManager.deleteLimit(limitInput);
154     return Response.ok().build();
155   }
156 }