292b4a093781580f20d41d84e2c6f62122f11d36
[sdc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 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.common.errors.CoreException;
24 import org.openecomp.sdc.common.errors.ErrorCode;
25 import org.openecomp.sdc.common.togglz.ToggleableFeature;
26 import org.openecomp.sdc.vendorlicense.VendorLicenseManager;
27 import org.openecomp.sdc.vendorlicense.VendorLicenseManagerFactory;
28 import org.openecomp.sdc.vendorlicense.dao.types.EntitlementPoolEntity;
29 import org.openecomp.sdc.versioning.dao.types.Version;
30 import org.openecomp.sdcrests.vendorlicense.rest.EntitlementPools;
31 import org.openecomp.sdcrests.vendorlicense.rest.mapping.MapEntitlementPoolEntityToEntitlementPoolEntityDto;
32 import org.openecomp.sdcrests.vendorlicense.rest.mapping.MapEntitlementPoolRequestDtoToEntitlementPoolEntity;
33 import org.openecomp.sdcrests.vendorlicense.types.EntitlementPoolEntityDto;
34 import org.openecomp.sdcrests.vendorlicense.types.EntitlementPoolRequestDto;
35 import org.openecomp.sdcrests.wrappers.GenericCollectionWrapper;
36 import org.openecomp.sdcrests.wrappers.StringWrapperResponse;
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 import java.util.Objects;
44
45 @Named
46 @Service("entitlementPools")
47 @Scope(value = "prototype")
48 public class EntitlementPoolsImpl implements EntitlementPools {
49   private VendorLicenseManager vendorLicenseManager =
50       VendorLicenseManagerFactory.getInstance().createInterface();
51
52   /**
53    * List entitlement pools response.
54    *
55    * @param vlmId     the vlm id
56    * @param versionId the version
57    * @param user      the user
58    * @return the response
59    */
60   public Response listEntitlementPools(String vlmId, String versionId, String user) {
61     Collection<EntitlementPoolEntity> entitlementPools =
62         vendorLicenseManager.listEntitlementPools(vlmId, new Version(versionId));
63
64     GenericCollectionWrapper<EntitlementPoolEntityDto> result = new GenericCollectionWrapper<>();
65     MapEntitlementPoolEntityToEntitlementPoolEntityDto outputMapper =
66         new MapEntitlementPoolEntityToEntitlementPoolEntityDto();
67     for (EntitlementPoolEntity ep : entitlementPools) {
68       result.add(outputMapper.applyMapping(ep, EntitlementPoolEntityDto.class));
69     }
70     return Response.ok(result).build();
71   }
72
73   /**
74    * Create entitlement pool response.
75    *
76    * @param request the request
77    * @param vlmId   the vlm id
78    * @param user    the user
79    * @return the response
80    */
81   public Response createEntitlementPool(EntitlementPoolRequestDto request, String vlmId,
82                                         String versionId, String user) {
83     if (ToggleableFeature.MRN.isActive()) {
84       if (Objects.isNull(request.getManufacturerReferenceNumber())) {
85         throw new CoreException((new ErrorCode.ErrorCodeBuilder().withMessage("Field does not conform to "
86             + "predefined criteria : manufacturerReferenceNumber : is mandatory and should not be empty").build()));
87       }
88     }
89     EntitlementPoolEntity entitlementPoolEntity =
90         new MapEntitlementPoolRequestDtoToEntitlementPoolEntity()
91             .applyMapping(request, EntitlementPoolEntity.class);
92     entitlementPoolEntity.setVendorLicenseModelId(vlmId);
93     entitlementPoolEntity.setVersion(new Version(versionId));
94
95     EntitlementPoolEntity createdEntitlementPool =
96         vendorLicenseManager.createEntitlementPool(entitlementPoolEntity);
97     StringWrapperResponse result =
98         createdEntitlementPool != null ? new StringWrapperResponse(createdEntitlementPool.getId())
99             : null;
100     return Response.ok(result).build();
101   }
102
103   /**
104    * Update entitlement pool response.
105    *
106    * @param request           the request
107    * @param vlmId             the vlm id
108    * @param entitlementPoolId the entitlement pool id
109    * @param user              the user
110    * @return the response
111    */
112   public Response updateEntitlementPool(EntitlementPoolRequestDto request, String vlmId,
113                                         String versionId, String entitlementPoolId, String user) {
114     EntitlementPoolEntity entitlementPoolEntity =
115         new MapEntitlementPoolRequestDtoToEntitlementPoolEntity()
116             .applyMapping(request, EntitlementPoolEntity.class);
117     entitlementPoolEntity.setVendorLicenseModelId(vlmId);
118     entitlementPoolEntity.setVersion(new Version(versionId));
119     entitlementPoolEntity.setId(entitlementPoolId);
120
121     vendorLicenseManager.updateEntitlementPool(entitlementPoolEntity);
122     return Response.ok().build();
123   }
124
125   /**
126    * Gets entitlement pool.
127    *
128    * @param vlmId             the vlm id
129    * @param versionId           the version id
130    * @param entitlementPoolId the entitlement pool id
131    * @param user              the user
132    * @return the entitlement pool
133    */
134   public Response getEntitlementPool(String vlmId, String versionId, String entitlementPoolId,
135                                      String user) {
136     EntitlementPoolEntity epInput = new EntitlementPoolEntity();
137     epInput.setVendorLicenseModelId(vlmId);
138     epInput.setVersion(new Version(versionId));
139     epInput.setId(entitlementPoolId);
140     EntitlementPoolEntity entitlementPool = vendorLicenseManager.getEntitlementPool(epInput);
141
142     EntitlementPoolEntityDto entitlementPoolEntityDto = entitlementPool == null ? null :
143         new MapEntitlementPoolEntityToEntitlementPoolEntityDto()
144             .applyMapping(entitlementPool, EntitlementPoolEntityDto.class);
145     return Response.ok(entitlementPoolEntityDto).build();
146   }
147
148   /**
149    * Delete entitlement pool response.
150    *
151    * @param vlmId             the vlm id
152    * @param entitlementPoolId the entitlement pool id
153    * @param user              the user
154    * @return the response
155    */
156   public Response deleteEntitlementPool(String vlmId, String versionId, String entitlementPoolId,
157                                         String user) {
158     EntitlementPoolEntity epInput = new EntitlementPoolEntity();
159     epInput.setVendorLicenseModelId(vlmId);
160     epInput.setId(entitlementPoolId);
161     epInput.setVersion(new Version(versionId));
162     vendorLicenseManager.deleteEntitlementPool(epInput);
163     return Response.ok().build();
164   }
165 }