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