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