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.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;
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;
52 @Service("featureGroups")
53 @Scope(value = "prototype")
54 public class FeatureGroupsImpl implements FeatureGroups {
55 private VendorLicenseManager vendorLicenseManager =
56 VendorLicenseManagerFactory.getInstance().createInterface();
59 public Response listFeatureGroups(String vlmId, String versionId, String user) {
60 Collection<FeatureGroupEntity> featureGroupEntities =
61 vendorLicenseManager.listFeatureGroups(vlmId, new Version(versionId));
63 MapFeatureGroupEntityToFeatureGroupDescriptorDto outputMapper =
64 new MapFeatureGroupEntityToFeatureGroupDescriptorDto();
65 GenericCollectionWrapper<FeatureGroupEntityDto> results = new GenericCollectionWrapper<>();
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);
76 return Response.ok(results).build();
80 public Response createFeatureGroup(FeatureGroupRequestDto request, String vlmId, String versionId,
82 //Below if block is introduced to have feature toggleable. This if block needs to remove when
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()));
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());
98 FeatureGroupEntity createdFeatureGroup =
99 vendorLicenseManager.createFeatureGroup(featureGroupEntity);
101 StringWrapperResponse result =
102 createdFeatureGroup != null ? new StringWrapperResponse(createdFeatureGroup.getId()) : null;
103 return Response.ok(result).build();
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);
116 .updateFeatureGroup(featureGroupEntity, request.getAddedLicenseKeyGroupsIds(),
117 request.getRemovedLicenseKeyGroupsIds(), request.getAddedEntitlementPoolsIds(),
118 request.getRemovedEntitlementPoolsIds());
119 return Response.ok().build();
123 public Response getFeatureGroup(String vlmId, String versionId, String featureGroupId,
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);
131 if (featureGroupModel == null) {
132 return Response.ok().build();
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);
142 if (!CommonMethods.isEmpty(featureGroupModel.getLicenseKeyGroups())) {
143 fgmDto.setLicenseKeyGroups(new HashSet<>());
145 MapLicenseKeyGroupEntityToLicenseKeyGroupEntityDto lkgMapper =
146 new MapLicenseKeyGroupEntityToLicenseKeyGroupEntityDto();
147 for (LicenseKeyGroupEntity lkg : featureGroupModel.getLicenseKeyGroups()) {
148 fgmDto.getLicenseKeyGroups()
149 .add(lkgMapper.applyMapping(lkg, LicenseKeyGroupEntityDto.class));
153 if (!CommonMethods.isEmpty(featureGroupModel.getEntitlementPools())) {
154 fgmDto.setEntitlementPools(new HashSet<>());
156 MapEntitlementPoolEntityToEntitlementPoolEntityDto epMapper =
157 new MapEntitlementPoolEntityToEntitlementPoolEntityDto();
158 for (EntitlementPoolEntity ep : featureGroupModel.getEntitlementPools()) {
159 fgmDto.getEntitlementPools().add(epMapper.applyMapping(ep, EntitlementPoolEntityDto.class));
163 return Response.ok(fgmDto).build();
167 public Response deleteFeatureGroup(String vlmId, String versionId, String featureGroupId,
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();