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.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.*;
37 import org.openecomp.sdcrests.wrappers.GenericCollectionWrapper;
38 import org.openecomp.sdcrests.wrappers.StringWrapperResponse;
39 import org.springframework.context.annotation.Scope;
40 import org.springframework.stereotype.Service;
42 import javax.inject.Named;
43 import javax.ws.rs.core.Response;
44 import java.util.Collection;
45 import java.util.HashSet;
48 @Service("featureGroups")
49 @Scope(value = "prototype")
50 public class FeatureGroupsImpl implements FeatureGroups {
51 private VendorLicenseManager vendorLicenseManager =
52 VendorLicenseManagerFactory.getInstance().createInterface();
55 public Response listFeatureGroups(String vlmId, String versionId, String user) {
56 Collection<FeatureGroupEntity> featureGroupEntities =
57 vendorLicenseManager.listFeatureGroups(vlmId, new Version(versionId));
59 MapFeatureGroupEntityToFeatureGroupDescriptorDto outputMapper =
60 new MapFeatureGroupEntityToFeatureGroupDescriptorDto();
61 GenericCollectionWrapper<FeatureGroupEntityDto> results = new GenericCollectionWrapper<>();
63 for (FeatureGroupEntity fg : featureGroupEntities) {
64 FeatureGroupEntityDto fgDto = new FeatureGroupEntityDto();
65 fgDto.setId(fg.getId());
66 fgDto.setLicenseKeyGroupsIds(fg.getLicenseKeyGroupIds());
67 fgDto.setEntitlementPoolsIds(fg.getEntitlementPoolIds());
68 fgDto.setReferencingLicenseAgreements(fg.getReferencingLicenseAgreements());
69 outputMapper.doMapping(fg, fgDto);
72 return Response.ok(results).build();
76 public Response createFeatureGroup(FeatureGroupRequestDto request, String vlmId, String versionId,
78 FeatureGroupEntity featureGroupEntity = new MapFeatureGroupDescriptorDtoToFeatureGroupEntity()
79 .applyMapping(request, FeatureGroupEntity.class);
80 featureGroupEntity.setVendorLicenseModelId(vlmId);
81 featureGroupEntity.setVersion(new Version(versionId));
82 featureGroupEntity.setLicenseKeyGroupIds(request.getAddedLicenseKeyGroupsIds());
83 featureGroupEntity.setEntitlementPoolIds(request.getAddedEntitlementPoolsIds());
85 FeatureGroupEntity createdFeatureGroup =
86 vendorLicenseManager.createFeatureGroup(featureGroupEntity);
88 StringWrapperResponse result =
89 createdFeatureGroup != null ? new StringWrapperResponse(createdFeatureGroup.getId()) : null;
90 return Response.ok(result).build();
94 public Response updateFeatureGroup(FeatureGroupUpdateRequestDto request, String vlmId,
95 String versionId, String featureGroupId, String user) {
96 FeatureGroupEntity featureGroupEntity = new MapFeatureGroupDescriptorDtoToFeatureGroupEntity()
97 .applyMapping(request, FeatureGroupEntity.class);
98 featureGroupEntity.setVendorLicenseModelId(vlmId);
99 featureGroupEntity.setVersion(new Version(versionId));
100 featureGroupEntity.setId(featureGroupId);
103 .updateFeatureGroup(featureGroupEntity, request.getAddedLicenseKeyGroupsIds(),
104 request.getRemovedLicenseKeyGroupsIds(), request.getAddedEntitlementPoolsIds(),
105 request.getRemovedEntitlementPoolsIds());
106 return Response.ok().build();
110 public Response getFeatureGroup(String vlmId, String versionId, String featureGroupId,
112 FeatureGroupEntity fgInput = new FeatureGroupEntity();
113 fgInput.setVendorLicenseModelId(vlmId);
114 fgInput.setVersion(new Version(versionId));
115 fgInput.setId(featureGroupId);
116 FeatureGroupModel featureGroupModel = vendorLicenseManager.getFeatureGroupModel(fgInput);
118 if (featureGroupModel == null) {
119 return Response.ok().build();
122 FeatureGroupModelDto fgmDto = new FeatureGroupModelDto();
123 fgmDto.setId(featureGroupModel.getFeatureGroup().getId());
124 fgmDto.setReferencingLicenseAgreements(
125 featureGroupModel.getFeatureGroup().getReferencingLicenseAgreements());
126 new MapFeatureGroupEntityToFeatureGroupDescriptorDto()
127 .doMapping(featureGroupModel.getFeatureGroup(), fgmDto);
129 if (!CommonMethods.isEmpty(featureGroupModel.getLicenseKeyGroups())) {
130 fgmDto.setLicenseKeyGroups(new HashSet<>());
132 MapLicenseKeyGroupEntityToLicenseKeyGroupEntityDto lkgMapper =
133 new MapLicenseKeyGroupEntityToLicenseKeyGroupEntityDto();
134 for (LicenseKeyGroupEntity lkg : featureGroupModel.getLicenseKeyGroups()) {
135 fgmDto.getLicenseKeyGroups()
136 .add(lkgMapper.applyMapping(lkg, LicenseKeyGroupEntityDto.class));
140 if (!CommonMethods.isEmpty(featureGroupModel.getEntitlementPools())) {
141 fgmDto.setEntitlementPools(new HashSet<>());
143 MapEntitlementPoolEntityToEntitlementPoolEntityDto epMapper =
144 new MapEntitlementPoolEntityToEntitlementPoolEntityDto();
145 for (EntitlementPoolEntity ep : featureGroupModel.getEntitlementPools()) {
146 fgmDto.getEntitlementPools().add(epMapper.applyMapping(ep, EntitlementPoolEntityDto.class));
150 return Response.ok(fgmDto).build();
154 public Response deleteFeatureGroup(String vlmId, String versionId, String featureGroupId,
156 FeatureGroupEntity fgInput = new FeatureGroupEntity();
157 fgInput.setVendorLicenseModelId(vlmId);
158 fgInput.setVersion(new Version(versionId));
159 fgInput.setId(featureGroupId);
160 vendorLicenseManager.deleteFeatureGroup(fgInput);
161 return Response.ok().build();