13da750c0005c5e8569fc4138493c11a6b090236
[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.util.HashSet;
41 import java.util.Iterator;
42 import java.util.List;
43 import java.util.Set;
44 import java.util.TreeSet;
45
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;
51
52 import com.fasterxml.jackson.databind.ObjectMapper;
53 import com.fasterxml.jackson.databind.type.TypeFactory;
54
55 @Transactional
56 public class RoleServiceCentralizedAccess implements RoleService {
57
58         private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(RoleServiceCentralizedAccess.class);
59
60         @Autowired
61         private RestApiRequestBuilder restApiRequestBuilder;
62
63         @Override
64         public List<RoleFunction> getRoleFunctions(String loginId) throws Exception {
65
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;
73         }
74
75         @Override
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;
80                 }
81
82                 Role currentRole = getRole(loginId, roleId);
83                 Set<Role> allParentRoles = new TreeSet<Role>();
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 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);
122                         }
123
124                         role.setRoleFunctions(roleFunctionListNew);
125                 }
126                 logger.info(EELFLoggerDelegate.applicationLogger, "role_id" + role.getId());
127                 return role;
128
129         }
130
131         @Override
132         public void saveRole(String loginId, Role domainRole) throws Exception {
133                 ObjectMapper mapper = new ObjectMapper();
134                 String role = mapper.writeValueAsString(domainRole);
135                 try {
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());
140                 }
141         }
142
143         @Override
144         public void deleteRole(String loginId, Role domainRole) throws Exception {
145                 String roleName = domainRole.getName().replaceAll(" ", "%20");
146                 try {
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());
151                 }
152         }
153         @Override
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));
160                 return roles;
161         }
162
163         @Override
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));
170                 return roles;
171
172         }
173
174         @Override
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);
181                 }
182                 return roleFunction;
183         }
184
185         @Override
186         public void saveRoleFunction(String requestedLoginId, RoleFunction domainRoleFunction) throws Exception {
187                 ObjectMapper mapper = new ObjectMapper();
188                 String roleFunction = mapper.writeValueAsString(domainRoleFunction);
189                 try{
190                 restApiRequestBuilder.postViaREST("/roleFunction", true, roleFunction, requestedLoginId);
191                 }catch(Exception e){
192                         logger.error(EELFLoggerDelegate.errorLogger, "saveRoleFunction Failed", e);
193                         throw new Exception(e.getMessage());
194                 }
195         }
196
197         @Override
198         public void deleteRoleFunction(String requestedLoginId, RoleFunction domainRoleFunction) throws Exception {
199                 String code = domainRoleFunction.getCode();
200                 try {
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());
205                 }
206         }
207
208         @Override
209         public void deleteDependcyRoleRecord(String requestedLoginId, Long id) {
210                 try {
211                         restApiRequestBuilder.deleteViaRest("/deleteDependcyRoleRecord/" + id, true, null, requestedLoginId);
212                 } catch (Exception e) {
213                         logger.error(EELFLoggerDelegate.errorLogger, "deleteDependcyRoleRecord Failed", e);
214                 }
215         }
216
217 }