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