14491b0e6af2b85229d89c68260281e07097eaed
[sdc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdcrests.vendorlicense.rest.services;
22
23 import org.openecomp.sdc.vendorlicense.VendorLicenseManager;
24 import org.openecomp.sdc.vendorlicense.VendorLicenseManagerFactory;
25 import org.openecomp.sdc.vendorlicense.dao.types.LicenseKeyGroupEntity;
26 import org.openecomp.sdc.vendorlicense.dao.types.LimitEntity;
27 import org.openecomp.sdc.versioning.dao.types.Version;
28 import org.openecomp.sdcrests.vendorlicense.rest.LicenseKeyGroupLimits;
29 import org.openecomp.sdcrests.vendorlicense.rest.mapping.LimitCreationDto;
30 import org.openecomp.sdcrests.vendorlicense.rest.mapping.MapLimitEntityToLimitCreationDto;
31 import org.openecomp.sdcrests.vendorlicense.rest.mapping.MapLimitEntityToLimitDto;
32 import org.openecomp.sdcrests.vendorlicense.rest.mapping.MapLimitRequestDtoToLimitEntity;
33 import org.openecomp.sdcrests.vendorlicense.types.LimitEntityDto;
34 import org.openecomp.sdcrests.vendorlicense.types.LimitRequestDto;
35 import org.openecomp.sdcrests.wrappers.GenericCollectionWrapper;
36 import org.springframework.context.annotation.Scope;
37 import org.springframework.stereotype.Service;
38
39 import javax.inject.Named;
40 import javax.ws.rs.core.Response;
41 import java.util.Collection;
42
43 @Named
44 @Service("licenseKeyGroupLimits")
45 @Scope(value = "prototype")
46 public class LicenseKeyGroupLimitsImpl implements LicenseKeyGroupLimits {
47   private VendorLicenseManager vendorLicenseManager =
48       VendorLicenseManagerFactory.getInstance().createInterface();
49
50   private static final String PARENT = "LicenseKeyGroup";
51
52
53   @Override
54   public Response createLimit(LimitRequestDto request,
55                               String vlmId,
56                               String versionId,
57                               String licenseKeyGroupId,
58                               String user) {
59     Version version = new Version(versionId);
60     vendorLicenseManager.getLicenseKeyGroup(
61         new LicenseKeyGroupEntity(vlmId, version, licenseKeyGroupId));
62
63     LimitEntity limitEntity =
64         new MapLimitRequestDtoToLimitEntity().applyMapping(request, LimitEntity.class);
65     limitEntity.setVendorLicenseModelId(vlmId);
66     limitEntity.setVersion(version);
67     limitEntity.setEpLkgId(licenseKeyGroupId);
68     limitEntity.setParent(PARENT);
69
70     LimitEntity createdLimit = vendorLicenseManager.createLimit(limitEntity);
71     MapLimitEntityToLimitCreationDto mapper = new MapLimitEntityToLimitCreationDto();
72     LimitCreationDto createdLimitDto = mapper.applyMapping(createdLimit, LimitCreationDto.class);
73     return Response.ok(createdLimitDto != null ? createdLimitDto : null).build();
74   }
75
76   @Override
77   public Response listLimits(String vlmId, String versionId, String licenseKeyGroupId,
78                              String user) {
79     Version version = new Version(versionId);
80     vendorLicenseManager
81         .getLicenseKeyGroup(new LicenseKeyGroupEntity(vlmId, version, licenseKeyGroupId));
82
83     Collection<LimitEntity> limits =
84         vendorLicenseManager.listLimits(vlmId, version, licenseKeyGroupId);
85
86     GenericCollectionWrapper<LimitEntityDto> result = new GenericCollectionWrapper<>();
87     MapLimitEntityToLimitDto outputMapper =
88         new MapLimitEntityToLimitDto();
89     for (LimitEntity limit : limits) {
90       result.add(outputMapper.applyMapping(limit, LimitEntityDto.class));
91     }
92     return Response.ok(result).build();
93   }
94
95   @Override
96   public Response updateLimit(LimitRequestDto request,
97                               String vlmId,
98                               String versionId,
99                               String licenseKeyGroupId,
100                               String limitId,
101                               String user) {
102     Version version = new Version(versionId);
103     vendorLicenseManager
104         .getLicenseKeyGroup(new LicenseKeyGroupEntity(vlmId, version, licenseKeyGroupId));
105
106     LimitEntity limitEntity =
107         new MapLimitRequestDtoToLimitEntity().applyMapping(request, LimitEntity.class);
108     limitEntity.setVendorLicenseModelId(vlmId);
109     limitEntity.setVersion(version);
110     limitEntity.setEpLkgId(licenseKeyGroupId);
111     limitEntity.setId(limitId);
112     limitEntity.setParent(PARENT);
113
114     vendorLicenseManager.updateLimit(limitEntity);
115     return Response.ok().build();
116   }
117
118   /**
119    * Delete License Key Group.
120    *
121    * @param vlmId             the vlm id
122    * @param licenseKeyGroupId the license Key Group id
123    * @param limitId           the limitId
124    * @param user              the user
125    * @return the response
126    */
127   public Response deleteLimit(String vlmId, String versionId, String licenseKeyGroupId,
128                               String limitId, String user) {
129     Version version = new Version(versionId);
130     vendorLicenseManager
131         .getLicenseKeyGroup(new LicenseKeyGroupEntity(vlmId, version, licenseKeyGroupId));
132
133     LimitEntity limitInput = new LimitEntity();
134     limitInput.setVendorLicenseModelId(vlmId);
135     limitInput.setVersion(version);
136     limitInput.setEpLkgId(licenseKeyGroupId);
137     limitInput.setId(limitId);
138     limitInput.setParent(PARENT);
139
140     vendorLicenseManager.deleteLimit(limitInput);
141     return Response.ok().build();
142   }
143
144   @Override
145   public Response getLimit(String vlmId, String versionId, String licenseKeyGroupId,
146                            String limitId, String user) {
147     Version version = new Version(versionId);
148     vendorLicenseManager
149         .getLicenseKeyGroup(new LicenseKeyGroupEntity(vlmId, version, licenseKeyGroupId));
150     LimitEntity limitInput = new LimitEntity();
151     limitInput.setVendorLicenseModelId(vlmId);
152     limitInput.setVersion(version);
153     limitInput.setEpLkgId(licenseKeyGroupId);
154     limitInput.setId(limitId);
155     LimitEntity limit = vendorLicenseManager.getLimit(limitInput);
156
157     LimitEntityDto entitlementPoolEntityDto = limit == null ? null
158         : new MapLimitEntityToLimitDto().applyMapping(limit, LimitEntityDto.class);
159     return Response.ok(entitlementPoolEntityDto).build();
160   }
161 }