2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
8 * Unless otherwise specified, all software contained herein is licensed
9 * under the Apache License, Version 2.0 (the “License”);
10 * you may not use this software except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * Unless otherwise specified, all documentation contained herein is licensed
22 * under the Creative Commons License, Attribution 4.0 Intl. (the “License”);
23 * you may not use this documentation except in compliance with the License.
24 * You may obtain a copy of the License at
26 * https://creativecommons.org/licenses/by/4.0/
28 * Unless required by applicable law or agreed to in writing, documentation
29 * distributed under the License is distributed on an "AS IS" BASIS,
30 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31 * See the License for the specific language governing permissions and
32 * limitations under the License.
34 * ============LICENSE_END============================================
36 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
38 package org.onap.portalsdk.core.service;
40 import java.util.HashSet;
41 import java.util.Iterator;
42 import java.util.List;
44 import java.util.TreeSet;
46 import org.onap.portalsdk.core.domain.Role;
47 import org.onap.portalsdk.core.domain.RoleFunction;
48 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
49 import org.springframework.beans.factory.annotation.Autowired;
50 import org.springframework.transaction.annotation.Transactional;
52 import com.fasterxml.jackson.databind.ObjectMapper;
53 import com.fasterxml.jackson.databind.type.TypeFactory;
56 public class RoleServiceCentralizedAccess implements RoleService {
58 private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(RoleServiceCentralizedAccess.class);
61 private RestApiRequestBuilder restApiRequestBuilder;
64 public List<RoleFunction> getRoleFunctions(String loginId) throws Exception {
66 List<RoleFunction> roleFunctionList = null;
67 String role_function_list = "";
68 role_function_list = restApiRequestBuilder.getViaREST("/functions", true, loginId);
69 ObjectMapper mapper = new ObjectMapper();
70 roleFunctionList = mapper.readValue(role_function_list,
71 TypeFactory.defaultInstance().constructCollectionType(List.class, RoleFunction.class));
72 return roleFunctionList;
76 public List<Role> getAvailableChildRoles(String loginId, Long roleId) throws Exception {
77 List<Role> availableChildRoles = getAvailableRoles(loginId);
78 if (roleId == null || roleId == 0) {
79 return availableChildRoles;
82 Role currentRole = getRole(loginId, roleId);
83 Set<Role> allParentRoles = new TreeSet<Role>();
84 allParentRoles = getAllParentRolesAsList(loginId, currentRole, allParentRoles);
86 Iterator<Role> availableChildRolesIterator = availableChildRoles.iterator();
87 while (availableChildRolesIterator.hasNext()) {
88 Role role = availableChildRolesIterator.next();
89 if (!role.getActive() || allParentRoles.contains(role) || role.getId().equals(roleId)) {
90 availableChildRolesIterator.remove();
93 return availableChildRoles;
96 @SuppressWarnings("unchecked")
97 private Set<Role> getAllParentRolesAsList(String loginId, Role role, Set<Role> allParentRoles) {
98 Set<Role> parentRoles = role.getParentRoles();
99 allParentRoles.addAll(parentRoles);
100 Iterator<Role> parentRolesIterator = parentRoles.iterator();
101 while (parentRolesIterator.hasNext()) {
102 getAllParentRolesAsList(loginId, parentRolesIterator.next(), allParentRoles);
104 return allParentRoles;
108 public Role getRole(String loginId, Long id) throws Exception {
109 ObjectMapper mapper = new ObjectMapper();
110 String roleString = restApiRequestBuilder.getViaREST("/role/" + id, true, loginId);
111 Role role = new Role();
112 role = mapper.readValue(roleString, Role.class);
113 if (role.getRoleFunctions() != null) {
114 @SuppressWarnings("unchecked")
115 Set<RoleFunction> roleFunctionList = role.getRoleFunctions();
116 Set<RoleFunction> roleFunctionListNew = new HashSet<>();
117 Iterator<RoleFunction> itetaror = roleFunctionList.iterator();
118 while (itetaror.hasNext()) {
119 Object nextValue = itetaror.next();
120 RoleFunction roleFun = mapper.convertValue(nextValue, RoleFunction.class);
121 roleFunctionListNew.add(roleFun);
124 role.setRoleFunctions(roleFunctionListNew);
126 logger.info(EELFLoggerDelegate.applicationLogger, "role_id" + role.getId());
132 public void saveRole(String loginId, Role domainRole) throws Exception {
133 ObjectMapper mapper = new ObjectMapper();
134 String role = mapper.writeValueAsString(domainRole);
136 restApiRequestBuilder.postViaREST("/role", true, role, loginId);
137 } catch (Exception e) {
138 logger.error(EELFLoggerDelegate.errorLogger, "saveRole Failed", e);
139 throw new Exception(e.getMessage());
144 public void deleteRole(String loginId, Role domainRole) throws Exception {
145 String roleName = domainRole.getName().replaceAll(" ", "%20");
147 restApiRequestBuilder.deleteViaRest("/deleteRole/"+ roleName, true, null, loginId);
148 } catch (Exception e) {
149 logger.error(EELFLoggerDelegate.errorLogger, "deleteRole Failed", e);
150 throw new Exception(e.getMessage());
154 public List<Role> getAvailableRoles(String requestedLoginId) throws Exception {
155 ObjectMapper mapper = new ObjectMapper();
156 String roleList = restApiRequestBuilder.getViaREST("/roles", true, requestedLoginId);
157 List<Role> roles = null;
158 roles = mapper.readValue(roleList,
159 TypeFactory.defaultInstance().constructCollectionType(List.class, Role.class));
164 public List<Role> getActiveRoles(String requestedLoginId) throws Exception {
165 ObjectMapper mapper = new ObjectMapper();
166 String roleString = restApiRequestBuilder.getViaREST("/activeRoles", true, requestedLoginId);
167 List<Role> roles = null;
168 roles = mapper.readValue(roleString,
169 TypeFactory.defaultInstance().constructCollectionType(List.class, Role.class));
175 public RoleFunction getRoleFunction(String requestedLoginId, String code) throws Exception {
176 ObjectMapper mapper = new ObjectMapper();
177 String responseString = restApiRequestBuilder.getViaREST("/function/" + code, true, requestedLoginId);
178 RoleFunction roleFunction = new RoleFunction();
179 if (!responseString.isEmpty()) {
180 roleFunction = mapper.readValue(responseString, RoleFunction.class);
186 public void saveRoleFunction(String requestedLoginId, RoleFunction domainRoleFunction) throws Exception {
187 ObjectMapper mapper = new ObjectMapper();
188 String roleFunction = mapper.writeValueAsString(domainRoleFunction);
190 restApiRequestBuilder.postViaREST("/roleFunction", true, roleFunction, requestedLoginId);
192 logger.error(EELFLoggerDelegate.errorLogger, "saveRoleFunction Failed", e);
193 throw new Exception(e.getMessage());
198 public void deleteRoleFunction(String requestedLoginId, RoleFunction domainRoleFunction) throws Exception {
199 String code = domainRoleFunction.getCode();
201 restApiRequestBuilder.deleteViaRest("/roleFunction/"+ code, true, null, requestedLoginId);
202 } catch (Exception e) {
203 logger.error(EELFLoggerDelegate.errorLogger, "deleteRoleFunction Failed ", e);
204 throw new Exception(e.getMessage());
209 public void deleteDependcyRoleRecord(String requestedLoginId, Long id) {
211 restApiRequestBuilder.deleteViaRest("/deleteDependcyRoleRecord/" + id, true, null, requestedLoginId);
212 } catch (Exception e) {
213 logger.error(EELFLoggerDelegate.errorLogger, "deleteDependcyRoleRecord Failed", e);