Fixed health check issue
[portal.git] / ecomp-portal-BE-common / src / main / java / org / onap / portalapp / portal / service / EPRoleServiceImpl.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 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.portalapp.portal.service;
39
40 import java.util.HashMap;
41 import java.util.Iterator;
42 import java.util.List;
43 import java.util.Map;
44 import java.util.Set;
45 import java.util.TreeSet;
46 import org.springframework.beans.factory.annotation.Autowired;
47 import org.springframework.context.annotation.EnableAspectJAutoProxy;
48 import org.springframework.stereotype.Service;
49 import org.springframework.transaction.annotation.Transactional;
50 import org.onap.portalapp.portal.domain.EPRole;
51 import org.onap.portalapp.portal.logging.aop.EPMetricsLog;
52 import org.onap.portalapp.portal.utils.PortalConstants;
53 import org.onap.portalsdk.core.domain.RoleFunction;
54 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
55 import org.onap.portalsdk.core.service.DataAccessService;
56
57 @Service("epRoleService")
58 @Transactional
59 @org.springframework.context.annotation.Configuration
60 @EnableAspectJAutoProxy
61 @EPMetricsLog
62 public class EPRoleServiceImpl implements EPRoleService {
63     EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(EPRoleServiceImpl.class);
64
65     private static final String GET_APP_ROLE_SQL_FORMAT =
66             "SELECT * FROM fn_role where APP_ID = %s AND APP_ROLE_ID = %s";
67
68     @Autowired
69     private DataAccessService dataAccessService;
70
71     @Autowired
72     ExternalAccessRolesService externalAccessRolesService;
73
74     @SuppressWarnings("unchecked")
75     public List<RoleFunction> getRoleFunctions() {
76         return getDataAccessService().getList(RoleFunction.class, null);
77     }
78
79     @SuppressWarnings("unchecked")
80     public List<EPRole> getAvailableChildRoles(Long roleId) {
81         List<EPRole> availableChildRoles =
82                 (List<EPRole>) getDataAccessService().getList(EPRole.class, null);
83         if (roleId == null || roleId == 0) {
84             return availableChildRoles;
85         }
86
87         EPRole currentRole =
88                 (EPRole) getDataAccessService().getDomainObject(EPRole.class, roleId, null);
89         Set<EPRole> allParentRoles = new TreeSet<>();
90         allParentRoles = getAllParentRolesAsList(currentRole, allParentRoles);
91
92         Iterator<EPRole> availableChildRolesIterator = availableChildRoles.iterator();
93         while (availableChildRolesIterator.hasNext()) {
94             EPRole role = availableChildRolesIterator.next();
95             if (!role.getActive() || allParentRoles.contains(role) || role.getId().equals(roleId)) {
96                 availableChildRolesIterator.remove();
97             }
98         }
99         return availableChildRoles;
100     }
101
102     private Set<EPRole> getAllParentRolesAsList(EPRole role, Set<EPRole> allParentRoles) {
103         Set<EPRole> parentRoles = role.getParentRoles();
104         allParentRoles.addAll(parentRoles);
105         Iterator<EPRole> parentRolesIterator = parentRoles.iterator();
106         while (parentRolesIterator.hasNext()) {
107             getAllParentRolesAsList(parentRolesIterator.next(), allParentRoles);
108         }
109         return allParentRoles;
110     }
111
112     public RoleFunction getRoleFunction(String code) {
113         return (RoleFunction) getDataAccessService().getDomainObject(RoleFunction.class, code,
114                 null);
115     }
116
117     public void saveRoleFunction(RoleFunction domainRoleFunction) {
118         getDataAccessService().saveDomainObject(domainRoleFunction, null);
119     }
120
121     public void deleteRoleFunction(RoleFunction domainRoleFunction) {
122         getDataAccessService().deleteDomainObject(domainRoleFunction, null);
123     }
124
125     public EPRole getRole(Long id) {
126         return (EPRole) getDataAccessService().getDomainObject(EPRole.class, id, null);
127     }
128
129     @SuppressWarnings("unchecked")
130     public EPRole getRole(Long appId, Long appRoleid) {
131         if (appId == null || appRoleid == null) {
132             logger.error(EELFLoggerDelegate.errorLogger, String.format(
133                     "getRole does not support null appId or roleId. appRoleid=%s, appRoleid=%s",
134                     appId, appRoleid));
135             return null;
136         }
137
138         String sql = String.format(GET_APP_ROLE_SQL_FORMAT, appId, appRoleid);
139
140         List<EPRole> roles =
141                 (List<EPRole>) dataAccessService.executeSQLQuery(sql, EPRole.class, null);
142         int resultsCount = roles.size();
143         if (resultsCount > 1) {
144             logger.error(EELFLoggerDelegate.errorLogger, String.format(
145                     "search by appId=%s, appRoleid=%s should have returned 0 or 1 results. Got %d. This is an internal server error.",
146                     appId, appRoleid, resultsCount));
147             logger.error(EELFLoggerDelegate.errorLogger,
148                     "Trying to recover from duplicates by returning the first search result. This issue should be treated, it is probably not critical because duplicate roles should be similar.");
149             return roles.get(0);
150         } else if (resultsCount == 1) {
151             return roles.get(0);
152         }
153         return null;
154     }
155
156     @SuppressWarnings("unchecked")
157     public EPRole getAppRole(String roleName, Long appId) {
158
159         final Map<String, String> params = new HashMap<>();
160         final Map<String, String> portalParams = new HashMap<>();
161         List<EPRole> roles = null;
162         params.put("appId", appId.toString());
163         params.put("roleName", roleName);
164         portalParams.put("appRoleName", roleName);
165
166         List<EPRole> roleList = externalAccessRolesService
167                 .getPortalAppRoleInfo(PortalConstants.ACCOUNT_ADMIN_ROLE_ID);
168         EPRole role = new EPRole();
169         if (!roleList.isEmpty()) {
170             role = roleList.get(0);
171         }
172         logger.debug(EELFLoggerDelegate.debugLogger, "Requested RoleName is  " + role.getName());
173
174         if (appId == 1 || roleName.equals(role.getName())) {
175             roles = (List<EPRole>) dataAccessService.executeNamedQuery("getPortalAppRoles",
176                     portalParams, null);
177         } else if (appId != 1 && !roleName.equals(role.getName())) {
178             roles = (List<EPRole>) dataAccessService.executeNamedQuery("getAppRoles", params, null);
179         }
180         int resultsCount = (roles == null ? 0 : roles.size());
181         if (resultsCount > 1) {
182             logger.error(EELFLoggerDelegate.errorLogger,
183                     "Trying to recover from duplicates by returning the first search result. This issue should be treated, it is probably not critical because duplicate roles should be similar.");
184             if (roles != null) {
185                 return roles.get(0);
186             }
187         } else if (roles!=null && resultsCount == 1) {
188             return roles.get(0);
189         }
190         return null;
191     }
192
193     public void saveRole(EPRole domainRole) {
194         getDataAccessService().saveDomainObject(domainRole, null);
195     }
196
197     public void deleteRole(EPRole domainRole) {
198         getDataAccessService().deleteDomainObject(domainRole, null);
199     }
200
201     @SuppressWarnings("unchecked")
202     public List<EPRole> getAvailableRoles() {
203         return getDataAccessService().getList(EPRole.class, null);
204     }
205
206     public DataAccessService getDataAccessService() {
207         return dataAccessService;
208     }
209
210     public void setDataAccessService(DataAccessService dataAccessService) {
211         this.dataAccessService = dataAccessService;
212     }
213 }