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