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