1c967ffa088b0d13ac999cba09004ea377323b8a
[sdc.git] /
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.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.EntitlementPoolEntityDto;
40 import org.openecomp.sdcrests.vendorlicense.types.FeatureGroupEntityDto;
41 import org.openecomp.sdcrests.vendorlicense.types.FeatureGroupModelDto;
42 import org.openecomp.sdcrests.vendorlicense.types.FeatureGroupRequestDto;
43 import org.openecomp.sdcrests.vendorlicense.types.FeatureGroupUpdateRequestDto;
44 import org.openecomp.sdcrests.vendorlicense.types.LicenseKeyGroupEntityDto;
45 import org.openecomp.sdcrests.wrappers.GenericCollectionWrapper;
46 import org.openecomp.sdcrests.wrappers.StringWrapperResponse;
47 import org.springframework.context.annotation.Scope;
48 import org.springframework.stereotype.Service;
49
50 import javax.inject.Named;
51 import javax.ws.rs.core.Response;
52 import java.util.Collection;
53 import java.util.HashSet;
54 import java.util.Objects;
55
56 @Named
57 @Service("featureGroups")
58 @Scope(value = "prototype")
59 public class FeatureGroupsImpl implements FeatureGroups {
60   private VendorLicenseManager vendorLicenseManager =
61       VendorLicenseManagerFactory.getInstance().createInterface();
62
63   @Override
64   public Response listFeatureGroups(String vlmId, String versionId, String user) {
65     Collection<FeatureGroupEntity> featureGroupEntities =
66         vendorLicenseManager.listFeatureGroups(vlmId, new Version(versionId));
67
68     MapFeatureGroupEntityToFeatureGroupDescriptorDto outputMapper =
69         new MapFeatureGroupEntityToFeatureGroupDescriptorDto();
70     GenericCollectionWrapper<FeatureGroupEntityDto> results = new GenericCollectionWrapper<>();
71
72     for (FeatureGroupEntity fg : featureGroupEntities) {
73       FeatureGroupEntityDto fgDto = new FeatureGroupEntityDto();
74       fgDto.setId(fg.getId());
75       fgDto.setLicenseKeyGroupsIds(fg.getLicenseKeyGroupIds());
76       fgDto.setEntitlementPoolsIds(fg.getEntitlementPoolIds());
77       fgDto.setReferencingLicenseAgreements(fg.getReferencingLicenseAgreements());
78       outputMapper.doMapping(fg, fgDto);
79       results.add(fgDto);
80     }
81     return Response.ok(results).build();
82   }
83
84   @Override
85   public Response createFeatureGroup(FeatureGroupRequestDto request, String vlmId, String versionId,
86                                      String user) {
87     //Below if block is introduced to have feature toggleable. This if block needs to remove when
88     // we remove toggle.
89     if (!ToggleableFeature.MRN.isActive()) {
90       if (Objects.isNull(request.getManufacturerReferenceNumber())) {
91         throw new CoreException((new ErrorCode.ErrorCodeBuilder().withMessage("Field does not " +
92             "conform to predefined criteria : manufacturerReferenceNumber : is mandatory and should not be empty").build()));
93       }
94     }
95
96     FeatureGroupEntity featureGroupEntity = new MapFeatureGroupDescriptorDtoToFeatureGroupEntity()
97         .applyMapping(request, FeatureGroupEntity.class);
98     featureGroupEntity.setVendorLicenseModelId(vlmId);
99     featureGroupEntity.setVersion(new Version(versionId));
100     featureGroupEntity.setLicenseKeyGroupIds(request.getAddedLicenseKeyGroupsIds());
101     featureGroupEntity.setEntitlementPoolIds(request.getAddedEntitlementPoolsIds());
102
103     FeatureGroupEntity createdFeatureGroup =
104         vendorLicenseManager.createFeatureGroup(featureGroupEntity);
105
106     StringWrapperResponse result =
107         createdFeatureGroup != null ? new StringWrapperResponse(createdFeatureGroup.getId()) : null;
108     return Response.ok(result).build();
109   }
110
111   @Override
112   public Response updateFeatureGroup(FeatureGroupUpdateRequestDto request, String vlmId,
113                                      String versionId, String featureGroupId, String user) {
114     FeatureGroupEntity featureGroupEntity = new MapFeatureGroupDescriptorDtoToFeatureGroupEntity()
115         .applyMapping(request, FeatureGroupEntity.class);
116     featureGroupEntity.setVendorLicenseModelId(vlmId);
117     featureGroupEntity.setVersion(new Version(versionId));
118     featureGroupEntity.setId(featureGroupId);
119
120     vendorLicenseManager
121         .updateFeatureGroup(featureGroupEntity, request.getAddedLicenseKeyGroupsIds(),
122             request.getRemovedLicenseKeyGroupsIds(), request.getAddedEntitlementPoolsIds(),
123             request.getRemovedEntitlementPoolsIds());
124     return Response.ok().build();
125   }
126
127   @Override
128   public Response getFeatureGroup(String vlmId, String versionId, String featureGroupId,
129                                   String user) {
130     FeatureGroupEntity fgInput = new FeatureGroupEntity();
131     fgInput.setVendorLicenseModelId(vlmId);
132     fgInput.setVersion(new Version(versionId));
133     fgInput.setId(featureGroupId);
134     FeatureGroupModel featureGroupModel = vendorLicenseManager.getFeatureGroupModel(fgInput);
135
136     if (featureGroupModel == null) {
137       return Response.ok().build();
138     }
139
140     FeatureGroupModelDto fgmDto = new FeatureGroupModelDto();
141     fgmDto.setId(featureGroupModel.getFeatureGroup().getId());
142     fgmDto.setReferencingLicenseAgreements(
143         featureGroupModel.getFeatureGroup().getReferencingLicenseAgreements());
144     new MapFeatureGroupEntityToFeatureGroupDescriptorDto()
145         .doMapping(featureGroupModel.getFeatureGroup(), fgmDto);
146
147     if (!CommonMethods.isEmpty(featureGroupModel.getLicenseKeyGroups())) {
148       fgmDto.setLicenseKeyGroups(new HashSet<>());
149
150       MapLicenseKeyGroupEntityToLicenseKeyGroupEntityDto lkgMapper =
151           new MapLicenseKeyGroupEntityToLicenseKeyGroupEntityDto();
152       for (LicenseKeyGroupEntity lkg : featureGroupModel.getLicenseKeyGroups()) {
153         fgmDto.getLicenseKeyGroups()
154             .add(lkgMapper.applyMapping(lkg, LicenseKeyGroupEntityDto.class));
155       }
156     }
157
158     if (!CommonMethods.isEmpty(featureGroupModel.getEntitlementPools())) {
159       fgmDto.setEntitlementPools(new HashSet<>());
160
161       MapEntitlementPoolEntityToEntitlementPoolEntityDto epMapper =
162           new MapEntitlementPoolEntityToEntitlementPoolEntityDto();
163       for (EntitlementPoolEntity ep : featureGroupModel.getEntitlementPools()) {
164         fgmDto.getEntitlementPools().add(epMapper.applyMapping(ep, EntitlementPoolEntityDto.class));
165
166       }
167     }
168     return Response.ok(fgmDto).build();
169   }
170
171   @Override
172   public Response deleteFeatureGroup(String vlmId, String versionId, String featureGroupId,
173                                      String user) {
174     FeatureGroupEntity fgInput = new FeatureGroupEntity();
175     fgInput.setVendorLicenseModelId(vlmId);
176     fgInput.setVersion(new Version(versionId));
177     fgInput.setId(featureGroupId);
178     vendorLicenseManager.deleteFeatureGroup(fgInput);
179     return Response.ok().build();
180   }
181
182 }