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 java.util.Comparator;
24 import java.util.HashSet;
25 import java.util.stream.Collectors;
26 import javax.inject.Named;
27 import javax.ws.rs.core.Response;
28 import org.apache.commons.collections4.CollectionUtils;
29 import org.openecomp.sdc.vendorlicense.VendorLicenseManager;
30 import org.openecomp.sdc.vendorlicense.VendorLicenseManagerFactory;
31 import org.openecomp.sdc.vendorlicense.dao.types.EntitlementPoolEntity;
32 import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity;
33 import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupModel;
34 import org.openecomp.sdc.vendorlicense.dao.types.LicenseKeyGroupEntity;
35 import org.openecomp.sdc.versioning.dao.types.Version;
36 import org.openecomp.sdcrests.vendorlicense.rest.FeatureGroups;
37 import org.openecomp.sdcrests.vendorlicense.rest.mapping.MapEntitlementPoolEntityToEntitlementPoolEntityDto;
38 import org.openecomp.sdcrests.vendorlicense.rest.mapping.MapFeatureGroupDescriptorDtoToFeatureGroupEntity;
39 import org.openecomp.sdcrests.vendorlicense.rest.mapping.MapFeatureGroupEntityToFeatureGroupDescriptorDto;
40 import org.openecomp.sdcrests.vendorlicense.rest.mapping.MapLicenseKeyGroupEntityToLicenseKeyGroupEntityDto;
41 import org.openecomp.sdcrests.vendorlicense.types.EntitlementPoolEntityDto;
42 import org.openecomp.sdcrests.vendorlicense.types.FeatureGroupEntityDto;
43 import org.openecomp.sdcrests.vendorlicense.types.FeatureGroupModelDto;
44 import org.openecomp.sdcrests.vendorlicense.types.FeatureGroupRequestDto;
45 import org.openecomp.sdcrests.vendorlicense.types.FeatureGroupUpdateRequestDto;
46 import org.openecomp.sdcrests.vendorlicense.types.LicenseKeyGroupEntityDto;
47 import org.openecomp.sdcrests.wrappers.GenericCollectionWrapper;
48 import org.openecomp.sdcrests.wrappers.StringWrapperResponse;
49 import org.springframework.context.annotation.Scope;
50 import org.springframework.stereotype.Service;
53 @Service("featureGroups")
54 @Scope(value = "prototype")
55 public class FeatureGroupsImpl implements FeatureGroups {
57 private VendorLicenseManager vendorLicenseManager =
58 VendorLicenseManagerFactory.getInstance().createInterface();
61 public Response listFeatureGroups(String vlmId, String versionId, String user) {
63 MapFeatureGroupEntityToFeatureGroupDescriptorDto outputMapper =
64 new MapFeatureGroupEntityToFeatureGroupDescriptorDto();
66 GenericCollectionWrapper<FeatureGroupEntityDto> results = new GenericCollectionWrapper<>(
67 vendorLicenseManager.listFeatureGroups(vlmId, new Version(versionId)).stream()
68 .sorted(Comparator.comparing(FeatureGroupEntity::getName))
69 .map(fg -> getFeatureGroupEntityDto(outputMapper,fg)).collect(Collectors.toList()));
71 return Response.ok(results).build();
75 public Response createFeatureGroup(FeatureGroupRequestDto request, String vlmId, String versionId,
77 FeatureGroupEntity featureGroupEntity = new MapFeatureGroupDescriptorDtoToFeatureGroupEntity()
78 .applyMapping(request, FeatureGroupEntity.class);
79 featureGroupEntity.setVendorLicenseModelId(vlmId);
80 featureGroupEntity.setVersion(new Version(versionId));
81 featureGroupEntity.setLicenseKeyGroupIds(request.getAddedLicenseKeyGroupsIds());
82 featureGroupEntity.setEntitlementPoolIds(request.getAddedEntitlementPoolsIds());
84 FeatureGroupEntity createdFeatureGroup =
85 vendorLicenseManager.createFeatureGroup(featureGroupEntity);
87 StringWrapperResponse result =
88 createdFeatureGroup != null ? new StringWrapperResponse(createdFeatureGroup.getId()) : null;
89 return Response.ok(result).build();
93 public Response updateFeatureGroup(FeatureGroupUpdateRequestDto request, String vlmId,
94 String versionId, String featureGroupId, String user) {
95 FeatureGroupEntity featureGroupEntity = new MapFeatureGroupDescriptorDtoToFeatureGroupEntity()
96 .applyMapping(request, FeatureGroupEntity.class);
97 featureGroupEntity.setVendorLicenseModelId(vlmId);
98 featureGroupEntity.setVersion(new Version(versionId));
99 featureGroupEntity.setId(featureGroupId);
102 .updateFeatureGroup(featureGroupEntity, request.getAddedLicenseKeyGroupsIds(),
103 request.getRemovedLicenseKeyGroupsIds(), request.getAddedEntitlementPoolsIds(),
104 request.getRemovedEntitlementPoolsIds());
105 return Response.ok().build();
109 public Response getFeatureGroup(String vlmId, String versionId, String featureGroupId,
111 FeatureGroupEntity fgInput = new FeatureGroupEntity();
112 fgInput.setVendorLicenseModelId(vlmId);
113 fgInput.setVersion(new Version(versionId));
114 fgInput.setId(featureGroupId);
115 FeatureGroupModel featureGroupModel = vendorLicenseManager.getFeatureGroupModel(fgInput);
117 if (featureGroupModel == null) {
118 return Response.ok().build();
121 FeatureGroupModelDto fgmDto = new FeatureGroupModelDto();
122 fgmDto.setId(featureGroupModel.getFeatureGroup().getId());
123 fgmDto.setReferencingLicenseAgreements(
124 featureGroupModel.getFeatureGroup().getReferencingLicenseAgreements());
125 new MapFeatureGroupEntityToFeatureGroupDescriptorDto()
126 .doMapping(featureGroupModel.getFeatureGroup(), fgmDto);
128 if (!CollectionUtils.isEmpty(featureGroupModel.getLicenseKeyGroups())) {
129 fgmDto.setLicenseKeyGroups(new HashSet<>());
131 MapLicenseKeyGroupEntityToLicenseKeyGroupEntityDto lkgMapper =
132 new MapLicenseKeyGroupEntityToLicenseKeyGroupEntityDto();
133 for (LicenseKeyGroupEntity lkg : featureGroupModel.getLicenseKeyGroups()) {
134 fgmDto.getLicenseKeyGroups()
135 .add(lkgMapper.applyMapping(lkg, LicenseKeyGroupEntityDto.class));
139 if (!CollectionUtils.isEmpty(featureGroupModel.getEntitlementPools())) {
140 fgmDto.setEntitlementPools(new HashSet<>());
142 MapEntitlementPoolEntityToEntitlementPoolEntityDto epMapper =
143 new MapEntitlementPoolEntityToEntitlementPoolEntityDto();
144 for (EntitlementPoolEntity ep : featureGroupModel.getEntitlementPools()) {
145 fgmDto.getEntitlementPools().add(epMapper.applyMapping(ep, EntitlementPoolEntityDto.class));
149 return Response.ok(fgmDto).build();
153 public Response deleteFeatureGroup(String vlmId, String versionId, String featureGroupId,
155 FeatureGroupEntity fgInput = new FeatureGroupEntity();
156 fgInput.setVendorLicenseModelId(vlmId);
157 fgInput.setVersion(new Version(versionId));
158 fgInput.setId(featureGroupId);
159 vendorLicenseManager.deleteFeatureGroup(fgInput);
160 return Response.ok().build();
163 private FeatureGroupEntityDto getFeatureGroupEntityDto(MapFeatureGroupEntityToFeatureGroupDescriptorDto mapper,FeatureGroupEntity fg) {
164 FeatureGroupEntityDto fgDto = new FeatureGroupEntityDto();
165 fgDto.setId(fg.getId());
166 fgDto.setLicenseKeyGroupsIds(fg.getLicenseKeyGroupIds());
167 fgDto.setEntitlementPoolsIds(fg.getEntitlementPoolIds());
168 fgDto.setReferencingLicenseAgreements(fg.getReferencingLicenseAgreements());
169 mapper.doMapping(fg, fgDto);