3ef7cb8bec7c8e68a9ea16891d0b65b1c8765d80
[portal/sdk.git] /
1 /*
2  * ============LICENSE_START==========================================
3  * ONAP Portal SDK
4  * ===================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
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
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
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.
20  *
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
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
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.
33  *
34  * ============LICENSE_END============================================
35  *
36  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
37  */
38 package org.onap.portalsdk.core.service;
39
40 import java.io.IOException;
41 import java.util.HashSet;
42 import java.util.Iterator;
43 import java.util.List;
44 import java.util.Set;
45 import java.util.TreeSet;
46
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;
52
53 import com.fasterxml.jackson.databind.ObjectMapper;
54 import com.fasterxml.jackson.databind.type.TypeFactory;
55
56 @Transactional
57 public class RoleServiceCentralizedAccess implements RoleService {
58
59         private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(RoleServiceCentralizedAccess.class);
60
61         @Autowired
62         private RestApiRequestBuilder restApiRequestBuilder;
63
64         @Override
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;
71         }
72
73         @Override
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;
78                 }
79
80                 Role currentRole = getRole(loginId, roleId);
81                 Set<Role> allParentRoles = new TreeSet<>();
82                 allParentRoles = getAllParentRolesAsList(loginId, currentRole, allParentRoles);
83
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();
89                         }
90                 }
91                 return availableChildRoles;
92         }
93
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);
101                 }
102                 return allParentRoles;
103         }
104
105         @Override
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);
119                         }
120
121                         role.setRoleFunctions(roleFunctionListNew);
122                 }
123                 logger.info(EELFLoggerDelegate.applicationLogger, "role_id" + role.getId());
124                 return role;
125
126         }
127
128         @Override
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);
133         }
134
135         @Override
136         public void deleteRole(String loginId, Role domainRole) throws IOException {
137                 String roleName = domainRole.getName().replaceAll(" ", "%20");
138                 restApiRequestBuilder.deleteViaRest("/deleteRole/" + roleName, true, null, loginId);
139         }
140
141         @Override
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));
147                 return roles;
148         }
149
150         @Override
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));
156                 return roles;
157         }
158
159         @Override
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);
166                 }
167                 return roleFunction;
168         }
169
170         @Override
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);
175         }
176
177         @Override
178         public void deleteRoleFunction(String requestedLoginId, RoleFunction domainRoleFunction) throws IOException {
179                 String code = domainRoleFunction.getCode();
180                 restApiRequestBuilder.deleteViaRest("/roleFunction/" + code, true, null, requestedLoginId);
181         }
182
183         @Override
184         public void deleteDependcyRoleRecord(String requestedLoginId, Long id) throws IOException {
185                 restApiRequestBuilder.deleteViaRest("/deleteDependcyRoleRecord/" + id, true, null, requestedLoginId);
186         }
187
188 }