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.stream.Collectors;
25 import java.util.stream.Stream;
26 import javax.inject.Named;
27 import javax.ws.rs.core.Response;
28 import org.openecomp.sdc.vendorlicense.VendorLicenseManager;
29 import org.openecomp.sdc.vendorlicense.VendorLicenseManagerFactory;
30 import org.openecomp.sdc.vendorlicense.dao.types.LicenseKeyGroupEntity;
31 import org.openecomp.sdc.versioning.dao.types.Version;
32 import org.openecomp.sdcrests.vendorlicense.rest.LicenseKeyGroups;
33 import org.openecomp.sdcrests.vendorlicense.rest.mapping.MapLicenseKeyGroupEntityToLicenseKeyGroupEntityDto;
34 import org.openecomp.sdcrests.vendorlicense.rest.mapping.MapLicenseKeyGroupRequestDtoToLicenseKeyGroupEntity;
35 import org.openecomp.sdcrests.vendorlicense.types.LicenseKeyGroupEntityDto;
36 import org.openecomp.sdcrests.vendorlicense.types.LicenseKeyGroupRequestDto;
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;
41 import org.springframework.validation.annotation.Validated;
44 @Service("licenseKeyGroups")
45 @Scope(value = "prototype")
47 public class LicenseKeyGroupsImpl implements LicenseKeyGroups {
48 private VendorLicenseManager vendorLicenseManager =
49 VendorLicenseManagerFactory.getInstance().createInterface();
52 * List license key groups response.
54 * @param vlmId the vlm id
55 * @param versionId the version
56 * @param user the user
57 * @return the response
59 public Response listLicenseKeyGroups(String vlmId, String versionId, String user) {
61 MapLicenseKeyGroupEntityToLicenseKeyGroupEntityDto outputMapper =
62 new MapLicenseKeyGroupEntityToLicenseKeyGroupEntityDto();
64 GenericCollectionWrapper<LicenseKeyGroupEntityDto> result = new GenericCollectionWrapper<>(
65 vendorLicenseManager.listLicenseKeyGroups(vlmId, new Version(versionId)).stream()
66 .sorted(Comparator.comparing(LicenseKeyGroupEntity::getName))
67 .map(item -> outputMapper.applyMapping(item, LicenseKeyGroupEntityDto.class))
68 .collect(Collectors.toList()));
70 return Response.ok(result).build();
74 * Create license key group response.
76 * @param request the request
77 * @param vlmId the vlm id
78 * @param user the user
79 * @return the response
81 public Response createLicenseKeyGroup(LicenseKeyGroupRequestDto request, String vlmId,
82 String versionId, String user) {
83 LicenseKeyGroupEntity licenseKeyGroupEntity =
84 new MapLicenseKeyGroupRequestDtoToLicenseKeyGroupEntity()
85 .applyMapping(request, LicenseKeyGroupEntity.class);
86 licenseKeyGroupEntity.setVendorLicenseModelId(vlmId);
87 licenseKeyGroupEntity.setVersion(new Version(versionId));
89 LicenseKeyGroupEntity createdLicenseKeyGroup =
90 vendorLicenseManager.createLicenseKeyGroup(licenseKeyGroupEntity);
91 StringWrapperResponse result =
92 createdLicenseKeyGroup != null ? new StringWrapperResponse(createdLicenseKeyGroup.getId())
94 return Response.ok(result).build();
98 * Update license key group response.
100 * @param request the request
101 * @param vlmId the vlm id
102 * @param licenseKeyGroupId the license key group id
103 * @param user the user
104 * @return the response
106 public Response updateLicenseKeyGroup(LicenseKeyGroupRequestDto request, String vlmId,
108 String licenseKeyGroupId, String user) {
109 LicenseKeyGroupEntity licenseKeyGroupEntity =
110 new MapLicenseKeyGroupRequestDtoToLicenseKeyGroupEntity()
111 .applyMapping(request, LicenseKeyGroupEntity.class);
112 licenseKeyGroupEntity.setVendorLicenseModelId(vlmId);
113 licenseKeyGroupEntity.setVersion(new Version(versionId));
114 licenseKeyGroupEntity.setId(licenseKeyGroupId);
116 vendorLicenseManager.updateLicenseKeyGroup(licenseKeyGroupEntity);
117 return Response.ok().build();
121 * Gets license key group.
123 * @param vlmId the vlm id
124 * @param versionId the version
125 * @param licenseKeyGroupId the license key group id
126 * @param user the user
127 * @return the license key group
129 public Response getLicenseKeyGroup(String vlmId, String versionId, String licenseKeyGroupId,
131 LicenseKeyGroupEntity lkgInput = new LicenseKeyGroupEntity();
132 lkgInput.setVendorLicenseModelId(vlmId);
133 lkgInput.setVersion(new Version(versionId));
134 lkgInput.setId(licenseKeyGroupId);
135 LicenseKeyGroupEntity licenseKeyGroup = vendorLicenseManager.getLicenseKeyGroup(lkgInput);
137 LicenseKeyGroupEntityDto licenseKeyGroupEntityDto = licenseKeyGroup == null ? null :
138 new MapLicenseKeyGroupEntityToLicenseKeyGroupEntityDto()
139 .applyMapping(licenseKeyGroup, LicenseKeyGroupEntityDto.class);
140 return Response.ok(licenseKeyGroupEntityDto).build();
144 * Delete license key group response.
146 * @param vlmId the vlm id
147 * @param licenseKeyGroupId the license key group id
148 * @param user the user
149 * @return the response
151 public Response deleteLicenseKeyGroup(String vlmId, String versionId, String licenseKeyGroupId,
153 LicenseKeyGroupEntity lkgInput = new LicenseKeyGroupEntity();
154 lkgInput.setVendorLicenseModelId(vlmId);
155 lkgInput.setVersion(new Version(versionId));
156 lkgInput.setId(licenseKeyGroupId);
157 vendorLicenseManager.deleteLicenseKeyGroup(lkgInput);
158 return Response.ok().build();