Adding interfaces in documentation
[aai/sparky-be.git] / sparkybe-onap-service / src / main / java / org / onap / aai / sparky / security / portal / PortalRestAPIServiceImpl.java
1 /**
2  * ============LICENSE_START===================================================
3  * SPARKY (AAI UI service)
4  * ============================================================================
5  * Copyright © 2017 AT&T Intellectual Property.
6  * Copyright © 2017 Amdocs
7  * All rights reserved.
8  * ============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file 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  * ============LICENSE_END=====================================================
21  *
22  * ECOMP and OpenECOMP are trademarks
23  * and service marks of AT&T Intellectual Property.
24  */
25 package org.onap.aai.sparky.security.portal;
26
27 import java.io.File;
28 import java.io.IOException;
29 import java.text.MessageFormat;
30 import java.util.LinkedHashSet;
31 import java.util.List;
32
33 import javax.servlet.http.HttpServletRequest;
34
35 import org.onap.aai.sparky.security.EcompSso;
36 import org.onap.aai.sparky.security.portal.config.PortalAuthenticationConfig;
37 import org.onap.aai.sparky.viewandinspect.config.SparkyConstants;
38 import org.openecomp.portalsdk.core.onboarding.crossapi.IPortalRestAPIService;
39 import org.openecomp.portalsdk.core.onboarding.exception.PortalAPIException;
40 import org.openecomp.portalsdk.core.restful.domain.EcompRole;
41 import org.openecomp.portalsdk.core.restful.domain.EcompUser;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45 /**
46  * Responds to ECOMP Portal's REST queries for user and role information and management.
47  */
48 public class PortalRestAPIServiceImpl implements IPortalRestAPIService {
49
50   private static final Logger LOG = LoggerFactory.getLogger(PortalRestAPIServiceImpl.class);
51   private static final String ERROR_MESSAGE = "Failed to {0} user [loginId:{1}]";
52
53   private UserManager userManager;
54
55   /**
56    * Initialise user manager.
57    */
58   public PortalRestAPIServiceImpl() {
59     userManager = new UserManager(new File(SparkyConstants.USERS_FILE_LOCATION));
60   }
61
62   /////////////////////////////////////////////////////////////////////////////
63   // User interface
64   /////////////////////////////////////////////////////////////////////////////
65
66   /*
67    * (non-Javadoc)
68    *
69    */
70   @Override
71   public void pushUser(EcompUser user) throws PortalAPIException {
72     LOG.debug("Push user [loginId:" + user.getLoginId() + "]");
73
74     if (userManager.getUser(user.getLoginId()).isPresent()) {
75       String message = getMessage(ERROR_MESSAGE, "push", user.getLoginId())
76           + ", user is already stored";
77       LOG.error(message);
78       throw new PortalAPIException(message);
79     }
80
81     try {
82       userManager.pushUser(user);
83     } catch (IOException e) {
84       String message = getMessage(ERROR_MESSAGE, "push", user.getLoginId());
85       LOG.error(message, e);
86       throw new PortalAPIException(message, e);
87     }
88   }
89
90   /*
91    * (non-Javadoc)
92    *
93    */
94   @Override
95   public void editUser(String loginId, EcompUser user) throws PortalAPIException {
96     LOG.debug("Edit user [loginId:" + loginId + "]");
97
98     userManager.getUser(loginId).orElseThrow(() -> {
99       String message = getMessage(ERROR_MESSAGE, "edit", loginId) + ", unknown user";
100       LOG.error(message);
101       return new PortalAPIException(message);
102     });
103
104     try {
105       userManager.editUser(loginId, user);
106     } catch (IOException e) {
107       String message = getMessage(ERROR_MESSAGE, "edit", loginId);
108       LOG.error(message, e);
109       throw new PortalAPIException(message, e);
110     }
111   }
112
113   /*
114    * (non-Javadoc)
115    *
116    */
117   @Override
118   public EcompUser getUser(String loginId) throws PortalAPIException {
119     LOG.debug("Get user [loginId:" + loginId + "]");
120     return userManager.getUser(loginId).orElseThrow(() -> {
121       String message = getMessage(ERROR_MESSAGE, "get", loginId) + ", unknown user";
122       LOG.error(message);
123       return new PortalAPIException(message);
124     });
125   }
126
127   /*
128    * (non-Javadoc)
129    *
130    */
131   @Override
132   public List<EcompUser> getUsers() throws PortalAPIException {
133     LOG.debug("Get users");
134     return userManager.getUsers();
135   }
136
137   @Override
138   public String getUserId(HttpServletRequest request) throws PortalAPIException {
139     return EcompSso.validateEcompSso(request);
140   }
141
142   /////////////////////////////////////////////////////////////////////////////
143   // Role interface
144   /////////////////////////////////////////////////////////////////////////////
145
146   public List<EcompRole> getAvailableRoles() throws PortalAPIException {
147     LOG.debug("Get available roles");
148     return UserManager.getRoles();
149   }
150
151   /*
152    * (non-Javadoc)
153    *
154    * @see
155    */
156   @Override
157   public List<EcompRole> getUserRoles(String loginId) throws PortalAPIException {
158     LOG.debug("Get user roles");
159     return userManager.getUserRoles(loginId);
160   }
161
162   /*
163    * (non-Javadoc)
164    *
165    */
166   @Override
167   public void pushUserRole(String loginId, List<EcompRole> roles) throws PortalAPIException {
168     LOG.debug("Push user role [loginId:" + loginId + "]");
169     try {
170       EcompUser user = getUser(loginId);
171       if (roles != null) {
172         user.setRoles(new LinkedHashSet<EcompRole>(roles));
173       } else {
174         user.setRoles(new LinkedHashSet<EcompRole>());
175       }
176       editUser(loginId, user);
177     } catch (PortalAPIException e) {
178       String message = getMessage(ERROR_MESSAGE, "push role", loginId);
179       LOG.error(message);
180       throw new PortalAPIException(message, e);
181     }
182   }
183
184   /////////////////////////////////////////////////////////////////////////////
185   // Security interface
186   /////////////////////////////////////////////////////////////////////////////
187
188   /*
189    * (non-Javadoc)
190    *
191    */
192   @Override
193   public boolean isAppAuthenticated(HttpServletRequest request) throws PortalAPIException {
194     LOG.debug("Authentication request");
195     PortalAuthenticationConfig config = PortalAuthenticationConfig.getInstance();
196     String restUsername = request.getHeader(PortalAuthenticationConfig.PROP_USERNAME);
197     String restPassword = request.getHeader(PortalAuthenticationConfig.PROP_PASSWORD);
198     return restUsername != null && restPassword != null && restUsername.equals(config.getUsername())
199         && restPassword.equals(config.getPassword());
200   }
201
202   private String getMessage(String message, Object... args) {
203     MessageFormat formatter = new MessageFormat("");
204     formatter.applyPattern(message);
205     return formatter.format(args);
206   }
207
208   public List<EcompRole> getAvailableRoles(String requestedLoginId) throws PortalAPIException {
209     LOG.debug("Get available roles");
210     return UserManager.getRoles();
211   }
212
213 }