27e902216e75c7cb6977fc423a404fc87710391b
[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.onboarding.crossapi;
39
40 import java.io.IOException;
41 import java.util.ArrayList;
42 import java.util.HashMap;
43 import java.util.List;
44 import java.util.Map;
45 import java.util.Set;
46 import java.util.stream.Collectors;
47
48 import javax.servlet.ServletException;
49 import javax.servlet.http.HttpServletRequest;
50
51 import org.apache.commons.logging.Log;
52 import org.apache.commons.logging.LogFactory;
53 import org.onap.portalsdk.core.onboarding.exception.PortalAPIException;
54 import org.onap.portalsdk.core.onboarding.rest.RestWebServiceClient;
55 import org.onap.portalsdk.core.onboarding.util.AuthUtil;
56 import org.onap.portalsdk.core.onboarding.util.PortalApiConstants;
57 import org.onap.portalsdk.core.onboarding.util.PortalApiProperties;
58 import org.onap.portalsdk.core.restful.domain.EcompRole;
59 import org.onap.portalsdk.core.restful.domain.EcompUser;
60
61 import com.fasterxml.jackson.databind.ObjectMapper;
62 import com.fasterxml.jackson.databind.type.TypeFactory;
63
64 public class PortalRestAPICentralServiceImpl implements IPortalRestAPIService {
65
66         private static final Log logger = LogFactory.getLog(PortalRestAPICentralServiceImpl.class);
67         private String username;
68         private String password;
69         private String appName;
70         IPortalRestCentralService portalRestCentralService;
71         public static final String API_VERSION = "/v4";
72         private static String portalApiVersion = "/v3";
73         private static final String nameSpace = PortalApiProperties
74                         .getProperty(PortalApiConstants.AUTH_NAMESPACE);
75
76         public PortalRestAPICentralServiceImpl() throws ServletException {
77                 String centralClassName = PortalApiProperties.getProperty(PortalApiConstants.PORTAL_API_IMPL_CLASS);
78                 if (centralClassName == null)
79                         throw new ServletException(
80                                         "init: Failed to find class name property " + PortalApiConstants.PORTAL_API_IMPL_CLASS);
81                 try {
82                         Class<?> centralImplClass = Class.forName(centralClassName);
83                         portalRestCentralService = (IPortalRestCentralService) (centralImplClass.getConstructor().newInstance());
84                         username = portalRestCentralService.getAppCredentials().get("username");
85                         password = portalRestCentralService.getAppCredentials().get("password");
86                         appName = portalRestCentralService.getAppCredentials().get("appName");
87                 } catch (Exception e) {
88                         throw new ClassCastException("Failed to find or instantiate class ");
89                 }
90         }
91
92         private final ObjectMapper mapper = new ObjectMapper();
93
94         @Override
95         public void pushUser(EcompUser user) throws PortalAPIException {
96                 portalRestCentralService.pushUser(user);
97         }
98
99         @Override
100         public void editUser(String loginId, EcompUser user) throws PortalAPIException {
101                 portalRestCentralService.editUser(loginId, user);
102         }
103
104         @Override
105         public EcompUser getUser(String loginId) throws PortalAPIException {
106                 EcompUser user = new EcompUser();
107                 String responseString = null;
108                 try {
109                         responseString = RestWebServiceClient.getInstance().getPortalContent(API_VERSION + "/user/" + loginId, null,
110                                         appName, null, username, password, true);
111                         logger.debug("responseString is: " + responseString);
112                         user = mapper.readValue(responseString, EcompUser.class);
113
114                 } catch (IOException e) {
115                         String response = "Failed to get user from portal";
116                         logger.error(response, e);
117                         throw new PortalAPIException(response, e);
118                 }
119                 return user;
120         }
121
122         @Override
123         public List<EcompUser> getUsers() throws PortalAPIException {
124                 List<EcompUser> usersList = new ArrayList<>();
125                 String responseString = null;
126                 try {
127                         responseString = RestWebServiceClient.getInstance().getPortalContent(portalApiVersion + "/users", null, appName, null,
128                                         username, password, true);
129                         logger.debug("responseString is: " + responseString);
130                         usersList = mapper.readValue(responseString,
131                                         TypeFactory.defaultInstance().constructCollectionType(List.class, EcompUser.class));
132
133                 } catch (IOException e) {
134                         String response = "Failed to get the users from portal";
135                         logger.error(response, e);
136                         throw new PortalAPIException(response, e);
137                 }
138                 return usersList;
139         }
140
141         @Override
142         public List<EcompRole> getAvailableRoles(String requestedLoginId) throws PortalAPIException {
143                 List<EcompRole> rolesList = new ArrayList<>();
144                 String responseString = null;
145                 try {
146                         responseString = RestWebServiceClient.getInstance().getPortalContent(API_VERSION + "/roles", requestedLoginId,
147                                         appName, null, username, password, true);
148                         logger.debug("responseString is: " + responseString);
149                         rolesList = mapper.readValue(responseString,
150                                         TypeFactory.defaultInstance().constructCollectionType(List.class, EcompRole.class));
151
152                 } catch (IOException e) {
153                         String response = "Failed to get Roles from portal";
154                         logger.error(response, e);
155                         throw new PortalAPIException(response, e);
156                 }
157                 return rolesList;
158         }
159
160         @Override
161         public void pushUserRole(String loginId, List<EcompRole> roles) throws PortalAPIException {
162                 throw new PortalAPIException("Please use Portal for Role Management");
163
164         }
165
166         @SuppressWarnings("unchecked")
167         @Override
168         public List<EcompRole> getUserRoles(String loginId) throws PortalAPIException {
169                 List<EcompRole> userRoles = new ArrayList<>();
170                 EcompUser user = new EcompUser();
171                 String responseString = null;
172                 try {
173                         responseString = RestWebServiceClient.getInstance().getPortalContent(API_VERSION + "/user/" + loginId, null,
174                                         appName, null, username, password, true);
175                         logger.debug("responseString is: " + responseString);
176                         user = mapper.readValue(responseString, EcompUser.class);
177                         Set roles = user.getRoles();
178                         userRoles = (List<EcompRole>) roles.stream().collect(Collectors.toList());
179
180                 } catch (IOException e) {
181                         String response = "Failed to get user roles from portal";
182                         logger.error(response, e);
183                         throw new PortalAPIException(response, e);
184                 }
185                 return userRoles;
186         }
187
188         @Override
189         public boolean isAppAuthenticated(HttpServletRequest request, Map<String,String> appCredentials) throws PortalAPIException {
190                 boolean accessAllowed = false;
191                 try {
192                         accessAllowed = AuthUtil.isAccessAllowed(request, nameSpace, appCredentials);
193                 } catch (Exception e) {
194                         logger.error(e);
195                 }
196                 return accessAllowed;
197         }
198
199         @Override
200         public String getUserId(HttpServletRequest request) throws PortalAPIException {
201                 return portalRestCentralService.getUserId(request);
202         }
203         
204         @Override
205         public Map<String, String> getCredentials() throws PortalAPIException{
206                 Map<String, String> credentialsMap = new HashMap<>();
207
208                 credentialsMap.put("username", username);
209                 credentialsMap.put("password", password);
210                 credentialsMap.put("appName", appName);
211                 return credentialsMap;
212         }
213
214 }