re base code
[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 / FeatureGroupsImpl.java
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.core.utilities.CommonMethods;
24 import org.openecomp.sdc.common.errors.CoreException;
25 import org.openecomp.sdc.common.errors.ErrorCode;
26 import org.openecomp.sdc.common.togglz.ToggleableFeature;
27 import org.openecomp.sdc.vendorlicense.VendorLicenseManager;
28 import org.openecomp.sdc.vendorlicense.VendorLicenseManagerFactory;
29 import org.openecomp.sdc.vendorlicense.dao.types.EntitlementPoolEntity;
30 import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity;
31 import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupModel;
32 import org.openecomp.sdc.vendorlicense.dao.types.LicenseKeyGroupEntity;
33 import org.openecomp.sdc.versioning.dao.types.Version;
34 import org.openecomp.sdcrests.vendorlicense.rest.FeatureGroups;
35 import org.openecomp.sdcrests.vendorlicense.rest.mapping.MapEntitlementPoolEntityToEntitlementPoolEntityDto;
36 import org.openecomp.sdcrests.vendorlicense.rest.mapping.MapFeatureGroupDescriptorDtoToFeatureGroupEntity;
37 import org.openecomp.sdcrests.vendorlicense.rest.mapping.MapFeatureGroupEntityToFeatureGroupDescriptorDto;
38 import org.openecomp.sdcrests.vendorlicense.rest.mapping.MapLicenseKeyGroupEntityToLicenseKeyGroupEntityDto;
39 import org.openecomp.sdcrests.vendorlicense.types.*;
40 import org.openecomp.sdcrests.wrappers.GenericCollectionWrapper;
41 import org.openecomp.sdcrests.wrappers.StringWrapperResponse;
42 import org.springframework.context.annotation.Scope;
43 import org.springframework.stereotype.Service;
44
45 import javax.inject.Named;
46 import javax.ws.rs.core.Response;
47 import java.util.Collection;
48 import java.util.HashSet;
49 import java.util.Objects;
50
51 @Named
52 @Service("featureGroups")
53 @Scope(value = "prototype")
54 public class FeatureGroupsImpl implements FeatureGroups {
55   private VendorLicenseManager vendorLicenseManager =
56       VendorLicenseManagerFactory.getInstance().createInterface();
57
58   @Override
59   public Response listFeatureGroups(String vlmId, String versionId, String user) {
60     Collection<FeatureGroupEntity> featureGroupEntities =
61         vendorLicenseManager.listFeatureGroups(vlmId, new Version(versionId));
62
63     MapFeatureGroupEntityToFeatureGroupDescriptorDto outputMapper =
64         new MapFeatureGroupEntityToFeatureGroupDescriptorDto();
65     GenericCollectionWrapper<FeatureGroupEntityDto> results = new GenericCollectionWrapper<>();
66
67     for (FeatureGroupEntity fg : featureGroupEntities) {
68       FeatureGroupEntityDto fgDto = new FeatureGroupEntityDto();
69       fgDto.setId(fg.getId());
70       fgDto.setLicenseKeyGroupsIds(fg.getLicenseKeyGroupIds());
71       fgDto.setEntitlementPoolsIds(fg.getEntitlementPoolIds());
72       fgDto.setReferencingLicenseAgreements(fg.getReferencingLicenseAgreements());
73       outputMapper.doMapping(fg, fgDto);
74       results.add(fgDto);
75     }
76     return Response.ok(results).build();
77   }
78
79   @Override
80   public Response createFeatureGroup(FeatureGroupRequestDto request, String vlmId, String versionId,
81                                      String user) {
82     //Below if block is introduced to have feature toggleable. This if block needs to remove when
83     // we remove toggle.
84     if (!ToggleableFeature.MRN.isActive()) {
85       if (Objects.isNull(request.getManufacturerReferenceNumber())) {
86         throw new CoreException((new ErrorCode.ErrorCodeBuilder().withMessage("Field does not " +
87             "conform to predefined criteria : manufacturerReferenceNumber : is mandatory and should not be empty").build()));
88       }
89     }
90
91     FeatureGroupEntity featureGroupEntity = new MapFeatureGroupDescriptorDtoToFeatureGroupEntity()
92         .applyMapping(request, FeatureGroupEntity.class);
93     featureGroupEntity.setVendorLicenseModelId(vlmId);
94     featureGroupEntity.setVersion(new Version(versionId));
95     featureGroupEntity.setLicenseKeyGroupIds(request.getAddedLicenseKeyGroupsIds());
96     featureGroupEntity.setEntitlementPoolIds(request.getAddedEntitlementPoolsIds());
97
98     FeatureGroupEntity createdFeatureGroup =
99         vendorLicenseManager.createFeatureGroup(featureGroupEntity);
100
101     StringWrapperResponse result =
102         createdFeatureGroup != null ? new StringWrapperResponse(createdFeatureGroup.getId()) : null;
103     return Response.ok(result).build();
104   }
105
106   @Override
107   public Response updateFeatureGroup(FeatureGroupUpdateRequestDto request, String vlmId,
108                                      String versionId, String featureGroupId, String user) {
109     FeatureGroupEntity featureGroupEntity = new MapFeatureGroupDescriptorDtoToFeatureGroupEntity()
110         .applyMapping(request, FeatureGroupEntity.class);
111     featureGroupEntity.setVendorLicenseModelId(vlmId);
112     featureGroupEntity.setVersion(new Version(versionId));
113     featureGroupEntity.setId(featureGroupId);
114
115     vendorLicenseManager
116         .updateFeatureGroup(featureGroupEntity, request.getAddedLicenseKeyGroupsIds(),
117             request.getRemovedLicenseKeyGroupsIds(), request.getAddedEntitlementPoolsIds(),
118             request.getRemovedEntitlementPoolsIds());
119     return Response.ok().build();
120   }
121
122   @Override
123   public Response getFeatureGroup(String vlmId, String versionId, String featureGroupId,
124                                   String user) {
125     FeatureGroupEntity fgInput = new FeatureGroupEntity();
126     fgInput.setVendorLicenseModelId(vlmId);
127     fgInput.setVersion(new Version(versionId));
128     fgInput.setId(featureGroupId);
129     FeatureGroupModel featureGroupModel = vendorLicenseManager.getFeatureGroupModel(fgInput);
130
131     if (featureGroupModel == null) {
132       return Response.ok().build();
133     }
134
135     FeatureGroupModelDto fgmDto = new FeatureGroupModelDto();
136     fgmDto.setId(featureGroupModel.getFeatureGroup().getId());
137     fgmDto.setReferencingLicenseAgreements(
138         featureGroupModel.getFeatureGroup().getReferencingLicenseAgreements());
139     new MapFeatureGroupEntityToFeatureGroupDescriptorDto()
140         .doMapping(featureGroupModel.getFeatureGroup(), fgmDto);
141
142     if (!CommonMethods.isEmpty(featureGroupModel.getLicenseKeyGroups())) {
143       fgmDto.setLicenseKeyGroups(new HashSet<>());
144
145       MapLicenseKeyGroupEntityToLicenseKeyGroupEntityDto lkgMapper =
146           new MapLicenseKeyGroupEntityToLicenseKeyGroupEntityDto();
147       for (LicenseKeyGroupEntity lkg : featureGroupModel.getLicenseKeyGroups()) {
148         fgmDto.getLicenseKeyGroups()
149             .add(lkgMapper.applyMapping(lkg, LicenseKeyGroupEntityDto.class));
150       }
151     }
152
153     if (!CommonMethods.isEmpty(featureGroupModel.getEntitlementPools())) {
154       fgmDto.setEntitlementPools(new HashSet<>());
155
156       MapEntitlementPoolEntityToEntitlementPoolEntityDto epMapper =
157           new MapEntitlementPoolEntityToEntitlementPoolEntityDto();
158       for (EntitlementPoolEntity ep : featureGroupModel.getEntitlementPools()) {
159         fgmDto.getEntitlementPools().add(epMapper.applyMapping(ep, EntitlementPoolEntityDto.class));
160
161       }
162     }
163     return Response.ok(fgmDto).build();
164   }
165
166   @Override
167   public Response deleteFeatureGroup(String vlmId, String versionId, String featureGroupId,
168                                      String user) {
169     FeatureGroupEntity fgInput = new FeatureGroupEntity();
170     fgInput.setVendorLicenseModelId(vlmId);
171     fgInput.setVersion(new Version(versionId));
172     fgInput.setId(featureGroupId);
173     vendorLicenseManager.deleteFeatureGroup(fgInput);
174     return Response.ok().build();
175   }
176
177 }