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.io.IOException;
41 import java.util.HashSet;
42 import java.util.Iterator;
43 import java.util.List;
45 import java.util.TreeSet;
47 import org.onap.portalsdk.core.domain.Role;
48 import org.onap.portalsdk.core.domain.RoleFunction;
49 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
50 import org.springframework.beans.factory.annotation.Autowired;
51 import org.springframework.transaction.annotation.Transactional;
53 import com.fasterxml.jackson.databind.ObjectMapper;
54 import com.fasterxml.jackson.databind.type.TypeFactory;
57 public class RoleServiceCentralizedAccess implements RoleService {
59 private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(RoleServiceCentralizedAccess.class);
62 private RestApiRequestBuilder restApiRequestBuilder;
65 public List<RoleFunction> getRoleFunctions(String loginId) throws IOException {
66 String roleFunctionString = restApiRequestBuilder.getViaREST("/functions", true, loginId);
67 ObjectMapper mapper = new ObjectMapper();
68 List<RoleFunction> roleFunctionList = mapper.readValue(roleFunctionString,
69 TypeFactory.defaultInstance().constructCollectionType(List.class, RoleFunction.class));
70 return roleFunctionList;
74 public List<Role> getAvailableChildRoles(String loginId, Long roleId) throws IOException {
75 List<Role> availableChildRoles = getAvailableRoles(loginId);
76 if (roleId == null || roleId == 0) {
77 return availableChildRoles;
80 Role currentRole = getRole(loginId, roleId);
81 Set<Role> allParentRoles = new TreeSet<>();
82 allParentRoles = getAllParentRolesAsList(loginId, currentRole, allParentRoles);
84 Iterator<Role> availableChildRolesIterator = availableChildRoles.iterator();
85 while (availableChildRolesIterator.hasNext()) {
86 Role role = availableChildRolesIterator.next();
87 if (!role.getActive() || allParentRoles.contains(role) || role.getId().equals(roleId)) {
88 availableChildRolesIterator.remove();
91 return availableChildRoles;
94 @SuppressWarnings("unchecked")
95 private Set<Role> getAllParentRolesAsList(String loginId, Role role, Set<Role> allParentRoles) {
96 Set<Role> parentRoles = role.getParentRoles();
97 allParentRoles.addAll(parentRoles);
98 Iterator<Role> parentRolesIterator = parentRoles.iterator();
99 while (parentRolesIterator.hasNext()) {
100 getAllParentRolesAsList(loginId, parentRolesIterator.next(), allParentRoles);
102 return allParentRoles;
106 public Role getRole(String loginId, Long id) throws IOException {
107 ObjectMapper mapper = new ObjectMapper();
108 String roleString = restApiRequestBuilder.getViaREST("/role/" + id, true, loginId);
109 Role role = mapper.readValue(roleString, Role.class);
110 if (role.getRoleFunctions() != null) {
111 @SuppressWarnings("unchecked")
112 Set<RoleFunction> roleFunctionList = role.getRoleFunctions();
113 Set<RoleFunction> roleFunctionListNew = new HashSet<>();
114 Iterator<RoleFunction> itetaror = roleFunctionList.iterator();
115 while (itetaror.hasNext()) {
116 Object nextValue = itetaror.next();
117 RoleFunction roleFun = mapper.convertValue(nextValue, RoleFunction.class);
118 roleFunctionListNew.add(roleFun);
121 role.setRoleFunctions(roleFunctionListNew);
123 logger.info(EELFLoggerDelegate.applicationLogger, "role_id" + role.getId());
129 public void saveRole(String loginId, Role domainRole) throws IOException {
130 ObjectMapper mapper = new ObjectMapper();
131 String role = mapper.writeValueAsString(domainRole);
132 restApiRequestBuilder.postViaREST("/role", true, role, loginId);
136 public void deleteRole(String loginId, Role domainRole) throws IOException {
137 Long roleId = domainRole.getId();
138 restApiRequestBuilder.deleteViaRest("v2/deleteRole/" + roleId, true, null, loginId);
142 public List<Role> getAvailableRoles(String requestedLoginId) throws IOException {
143 ObjectMapper mapper = new ObjectMapper();
144 String roleList = restApiRequestBuilder.getViaREST("/roles", true, requestedLoginId);
145 List<Role> roles = mapper.readValue(roleList,
146 TypeFactory.defaultInstance().constructCollectionType(List.class, Role.class));
151 public List<Role> getActiveRoles(String requestedLoginId) throws IOException {
152 ObjectMapper mapper = new ObjectMapper();
153 String roleString = restApiRequestBuilder.getViaREST("/activeRoles", true, requestedLoginId);
154 List<Role> roles = mapper.readValue(roleString,
155 TypeFactory.defaultInstance().constructCollectionType(List.class, Role.class));
160 public RoleFunction getRoleFunction(String requestedLoginId, String code) throws IOException {
161 ObjectMapper mapper = new ObjectMapper();
162 String responseString = restApiRequestBuilder.getViaREST("/function/" + code, true, requestedLoginId);
163 RoleFunction roleFunction = new RoleFunction();
164 if (!responseString.isEmpty()) {
165 roleFunction = mapper.readValue(responseString, RoleFunction.class);
171 public void saveRoleFunction(String requestedLoginId, RoleFunction domainRoleFunction) throws IOException {
172 ObjectMapper mapper = new ObjectMapper();
173 String roleFunction = mapper.writeValueAsString(domainRoleFunction);
174 restApiRequestBuilder.postViaREST("/roleFunction", true, roleFunction, requestedLoginId);
178 public void deleteRoleFunction(String requestedLoginId, RoleFunction domainRoleFunction) throws IOException {
179 String code = domainRoleFunction.getCode();
180 restApiRequestBuilder.deleteViaRest("/roleFunction/" + code, true, null, requestedLoginId);
184 public void deleteDependcyRoleRecord(String requestedLoginId, Long id) throws IOException {
185 restApiRequestBuilder.deleteViaRest("/deleteDependcyRoleRecord/" + id, true, null, requestedLoginId);