88633abe0b81c71fc822bef18362f4ecad3fa828
[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  * 
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         private static String portalApiVersion = "/v1";
65
66         @Override
67         public List<RoleFunction> getRoleFunctions(String loginId) throws IOException {
68                 String roleFunctionString = restApiRequestBuilder.getViaREST(portalApiVersion+"/functions", true, loginId);
69                 ObjectMapper mapper = new ObjectMapper();
70                 List<RoleFunction> roleFunctionList = mapper.readValue(roleFunctionString,
71                                 TypeFactory.defaultInstance().constructCollectionType(List.class, RoleFunction.class));
72                 return roleFunctionList;
73         }
74
75         @Override
76         public List<Role> getAvailableChildRoles(String loginId, Long roleId) throws IOException {
77                 List<Role> availableChildRoles = getAvailableRoles(loginId);
78                 if (roleId == null || roleId == 0) {
79                         return availableChildRoles;
80                 }
81
82                 Role currentRole = getRole(loginId, roleId);
83                 Set<Role> allParentRoles = new TreeSet<>();
84                 allParentRoles = getAllParentRolesAsList(loginId, currentRole, allParentRoles);
85
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();
91                         }
92                 }
93                 return availableChildRoles;
94         }
95
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);
103                 }
104                 return allParentRoles;
105         }
106
107         @Override
108         public Role getRole(String loginId, Long id) throws IOException {
109                 ObjectMapper mapper = new ObjectMapper();
110                 String roleString = restApiRequestBuilder.getViaREST(portalApiVersion+"/role/" + id, true, loginId);
111                 Role role = mapper.readValue(roleString, Role.class);
112                 if (role.getRoleFunctions() != null) {
113                         @SuppressWarnings("unchecked")
114                         Set<RoleFunction> roleFunctionList = role.getRoleFunctions();
115                         Set<RoleFunction> roleFunctionListNew = new HashSet<>();
116                         Iterator<RoleFunction> itetaror = roleFunctionList.iterator();
117                         while (itetaror.hasNext()) {
118                                 Object nextValue = itetaror.next();
119                                 RoleFunction roleFun = mapper.convertValue(nextValue, RoleFunction.class);
120                                 roleFunctionListNew.add(roleFun);
121                         }
122
123                         role.setRoleFunctions(roleFunctionListNew);
124                 }
125                 logger.info(EELFLoggerDelegate.applicationLogger, "role_id" + role.getId());
126                 return role;
127
128         }
129
130         @Override
131         public void saveRole(String loginId, Role domainRole) throws IOException {
132                 ObjectMapper mapper = new ObjectMapper();
133                 String role = mapper.writeValueAsString(domainRole);
134                 restApiRequestBuilder.postViaREST("/role", true, role, loginId);
135         }
136
137         @Override
138         public void deleteRole(String loginId, Role domainRole) throws IOException {
139                 Long roleId = domainRole.getId();
140                 restApiRequestBuilder.deleteViaRest("v2/deleteRole/" + roleId, true, null, loginId);
141         }
142
143         @Override
144         public List<Role> getAvailableRoles(String requestedLoginId) throws IOException {
145                 ObjectMapper mapper = new ObjectMapper();
146                 String roleList = restApiRequestBuilder.getViaREST(portalApiVersion+"/roles", true, requestedLoginId);
147                 List<Role> roles = mapper.readValue(roleList,
148                                 TypeFactory.defaultInstance().constructCollectionType(List.class, Role.class));
149                 return roles;
150         }
151
152         @Override
153         public List<Role> getActiveRoles(String requestedLoginId) throws IOException {
154                 ObjectMapper mapper = new ObjectMapper();
155                 String roleString = restApiRequestBuilder.getViaREST(portalApiVersion+"/activeRoles", true, requestedLoginId);
156                 List<Role> roles = mapper.readValue(roleString,
157                                 TypeFactory.defaultInstance().constructCollectionType(List.class, Role.class));
158                 return roles;
159         }
160
161         @Override
162         public RoleFunction getRoleFunction(String requestedLoginId, String code) throws IOException {
163                 ObjectMapper mapper = new ObjectMapper();
164                 String responseString = restApiRequestBuilder.getViaREST(portalApiVersion+"/function/" + code, true, requestedLoginId);
165                 RoleFunction roleFunction = new RoleFunction();
166                 if (!responseString.isEmpty()) {
167                         roleFunction = mapper.readValue(responseString, RoleFunction.class);
168                 }
169                 return roleFunction;
170         }
171
172         @Override
173         public void saveRoleFunction(String requestedLoginId, RoleFunction domainRoleFunction) throws IOException {
174                 ObjectMapper mapper = new ObjectMapper();
175                 String roleFunction = mapper.writeValueAsString(domainRoleFunction);
176                 restApiRequestBuilder.postViaREST(portalApiVersion+"/roleFunction", true, roleFunction, requestedLoginId);
177         }
178
179         @Override
180         public void deleteRoleFunction(String requestedLoginId, RoleFunction domainRoleFunction) throws IOException {
181                 String code = domainRoleFunction.getCode();
182                 restApiRequestBuilder.deleteViaRest("/roleFunction/" + code, true, null, requestedLoginId);
183         }
184
185         @Override
186         public void deleteDependcyRoleRecord(String requestedLoginId, Long id) throws IOException {
187                 restApiRequestBuilder.deleteViaRest("/deleteDependcyRoleRecord/" + id, true, null, requestedLoginId);
188         }
189
190 }