2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdcrests.vendorlicense.rest.services;
23 import org.openecomp.core.utilities.CommonMethods;
24 import org.openecomp.sdc.logging.context.MdcUtil;
25 import org.openecomp.sdc.logging.types.LoggerServiceName;
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.vendorlicense.dao.types.FeatureGroupEntity;
30 import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupModel;
31 import org.openecomp.sdc.vendorlicense.dao.types.LicenseKeyGroupEntity;
32 import org.openecomp.sdc.versioning.dao.types.Version;
33 import org.openecomp.sdcrests.vendorlicense.rest.FeatureGroups;
34 import org.openecomp.sdcrests.vendorlicense.rest.mapping.MapEntitlementPoolEntityToEntitlementPoolEntityDto;
35 import org.openecomp.sdcrests.vendorlicense.rest.mapping.MapFeatureGroupDescriptorDtoToFeatureGroupEntity;
36 import org.openecomp.sdcrests.vendorlicense.rest.mapping.MapFeatureGroupEntityToFeatureGroupDescriptorDto;
37 import org.openecomp.sdcrests.vendorlicense.rest.mapping.MapLicenseKeyGroupEntityToLicenseKeyGroupEntityDto;
38 import org.openecomp.sdcrests.vendorlicense.types.EntitlementPoolEntityDto;
39 import org.openecomp.sdcrests.vendorlicense.types.FeatureGroupEntityDto;
40 import org.openecomp.sdcrests.vendorlicense.types.FeatureGroupModelDto;
41 import org.openecomp.sdcrests.vendorlicense.types.FeatureGroupRequestDto;
42 import org.openecomp.sdcrests.vendorlicense.types.FeatureGroupUpdateRequestDto;
43 import org.openecomp.sdcrests.vendorlicense.types.LicenseKeyGroupEntityDto;
44 import org.openecomp.sdcrests.wrappers.GenericCollectionWrapper;
45 import org.openecomp.sdcrests.wrappers.StringWrapperResponse;
46 import org.springframework.context.annotation.Scope;
47 import org.springframework.stereotype.Service;
49 import javax.inject.Named;
50 import javax.ws.rs.core.Response;
51 import java.util.Collection;
52 import java.util.HashSet;
55 @Service("featureGroups")
56 @Scope(value = "prototype")
57 public class FeatureGroupsImpl implements FeatureGroups {
58 private VendorLicenseManager vendorLicenseManager =
59 VendorLicenseManagerFactory.getInstance().createInterface();
62 public Response listFeatureGroups(String vlmId, String versionId, String user) {
63 MdcUtil.initMdc(LoggerServiceName.List_FG.toString());
64 Collection<FeatureGroupEntity> featureGroupEntities =
65 vendorLicenseManager.listFeatureGroups(vlmId, new Version(versionId));
67 MapFeatureGroupEntityToFeatureGroupDescriptorDto outputMapper =
68 new MapFeatureGroupEntityToFeatureGroupDescriptorDto();
69 GenericCollectionWrapper<FeatureGroupEntityDto> results = new GenericCollectionWrapper<>();
71 for (FeatureGroupEntity fg : featureGroupEntities) {
72 FeatureGroupEntityDto fgDto = new FeatureGroupEntityDto();
73 fgDto.setId(fg.getId());
74 fgDto.setLicenseKeyGroupsIds(fg.getLicenseKeyGroupIds());
75 fgDto.setEntitlementPoolsIds(fg.getEntitlementPoolIds());
76 fgDto.setReferencingLicenseAgreements(fg.getReferencingLicenseAgreements());
77 fgDto.setManufacturerReferenceNumber(fg.getManufacturerReferenceNumber());
78 outputMapper.doMapping(fg, fgDto);
81 return Response.ok(results).build();
85 public Response createFeatureGroup(FeatureGroupRequestDto request, String vlmId, String versionId,
87 MdcUtil.initMdc(LoggerServiceName.Create_FG.toString());
88 FeatureGroupEntity featureGroupEntity = new MapFeatureGroupDescriptorDtoToFeatureGroupEntity()
89 .applyMapping(request, FeatureGroupEntity.class);
90 featureGroupEntity.setVendorLicenseModelId(vlmId);
91 featureGroupEntity.setVersion(new Version(versionId));
92 featureGroupEntity.setLicenseKeyGroupIds(request.getAddedLicenseKeyGroupsIds());
93 featureGroupEntity.setEntitlementPoolIds(request.getAddedEntitlementPoolsIds());
95 FeatureGroupEntity createdFeatureGroup =
96 vendorLicenseManager.createFeatureGroup(featureGroupEntity);
98 StringWrapperResponse result =
99 createdFeatureGroup != null ? new StringWrapperResponse(createdFeatureGroup.getId()) : null;
100 return Response.ok(result).build();
104 public Response updateFeatureGroup(FeatureGroupUpdateRequestDto request, String vlmId,
105 String versionId, String featureGroupId, String user) {
106 MdcUtil.initMdc(LoggerServiceName.Update_FG.toString());
107 FeatureGroupEntity featureGroupEntity = new MapFeatureGroupDescriptorDtoToFeatureGroupEntity()
108 .applyMapping(request, FeatureGroupEntity.class);
109 featureGroupEntity.setVendorLicenseModelId(vlmId);
110 featureGroupEntity.setVersion(new Version(versionId));
111 featureGroupEntity.setId(featureGroupId);
114 .updateFeatureGroup(featureGroupEntity, request.getAddedLicenseKeyGroupsIds(),
115 request.getRemovedLicenseKeyGroupsIds(), request.getAddedEntitlementPoolsIds(),
116 request.getRemovedEntitlementPoolsIds());
117 return Response.ok().build();
121 public Response getFeatureGroup(String vlmId, String versionId, String featureGroupId,
123 MdcUtil.initMdc(LoggerServiceName.Get_FG.toString());
124 FeatureGroupEntity fgInput = new FeatureGroupEntity();
125 fgInput.setVendorLicenseModelId(vlmId);
126 fgInput.setVersion(new Version(versionId));
127 fgInput.setId(featureGroupId);
128 FeatureGroupModel featureGroupModel = vendorLicenseManager.getFeatureGroupModel(fgInput);
130 if (featureGroupModel == null) {
131 return Response.ok().build();
134 FeatureGroupModelDto fgmDto = new FeatureGroupModelDto();
135 fgmDto.setId(featureGroupModel.getFeatureGroup().getId());
136 fgmDto.setReferencingLicenseAgreements(
137 featureGroupModel.getFeatureGroup().getReferencingLicenseAgreements());
138 new MapFeatureGroupEntityToFeatureGroupDescriptorDto()
139 .doMapping(featureGroupModel.getFeatureGroup(), fgmDto);
141 if (!CommonMethods.isEmpty(featureGroupModel.getLicenseKeyGroups())) {
142 fgmDto.setLicenseKeyGroups(new HashSet<>());
144 MapLicenseKeyGroupEntityToLicenseKeyGroupEntityDto lkgMapper =
145 new MapLicenseKeyGroupEntityToLicenseKeyGroupEntityDto();
146 for (LicenseKeyGroupEntity lkg : featureGroupModel.getLicenseKeyGroups()) {
147 fgmDto.getLicenseKeyGroups()
148 .add(lkgMapper.applyMapping(lkg, LicenseKeyGroupEntityDto.class));
152 if (!CommonMethods.isEmpty(featureGroupModel.getEntitlementPools())) {
153 fgmDto.setEntitlementPools(new HashSet<>());
155 MapEntitlementPoolEntityToEntitlementPoolEntityDto epMapper =
156 new MapEntitlementPoolEntityToEntitlementPoolEntityDto();
157 for (EntitlementPoolEntity ep : featureGroupModel.getEntitlementPools()) {
158 fgmDto.getEntitlementPools().add(epMapper.applyMapping(ep, EntitlementPoolEntityDto.class));
162 return Response.ok(fgmDto).build();
166 public Response deleteFeatureGroup(String vlmId, String versionId, String featureGroupId,
168 MdcUtil.initMdc(LoggerServiceName.Delete_FG.toString());
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();