nexus site path corrected
[portal.git] / ecomp-portal-BE / src / main / java / org / openecomp / portalapp / portal / controller / AppsController.java
1 /*-
2  * ================================================================================
3  * eCOMP Portal
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ================================================================================
19  */
20 package org.openecomp.portalapp.portal.controller;
21
22 import java.io.IOException;
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Map;
26
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
29
30 import org.springframework.beans.factory.annotation.Autowired;
31 import org.springframework.context.annotation.EnableAspectJAutoProxy;
32 import org.springframework.web.bind.annotation.PathVariable;
33 import org.springframework.web.bind.annotation.RequestBody;
34 import org.springframework.web.bind.annotation.RequestMapping;
35 import org.springframework.web.bind.annotation.RequestMethod;
36 import org.springframework.web.bind.annotation.RestController;
37
38 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
39 import org.json.JSONObject;
40 import org.openecomp.portalapp.controller.EPRestrictedBaseController;
41 import org.openecomp.portalapp.portal.domain.AdminUserApplications;
42 import org.openecomp.portalapp.portal.domain.AppIdAndNameTransportModel;
43 import org.openecomp.portalapp.portal.domain.AppsResponse;
44 import org.openecomp.portalapp.portal.domain.EPApp;
45 import org.openecomp.portalapp.portal.domain.EPUser;
46 import org.openecomp.portalapp.portal.domain.EcompApp;
47 import org.openecomp.portalapp.portal.domain.UserRole;
48 import org.openecomp.portalapp.portal.domain.UserRoles;
49 import org.openecomp.portalapp.portal.ecomp.model.PortalRestResponse;
50 import org.openecomp.portalapp.portal.ecomp.model.PortalRestStatusEnum;
51 import org.openecomp.portalapp.portal.logging.aop.EPAuditLog;
52 import org.openecomp.portalapp.portal.service.AdminRolesService;
53 import org.openecomp.portalapp.portal.service.EPAppService;
54 import org.openecomp.portalapp.portal.service.PersUserAppService;
55 import org.openecomp.portalapp.portal.service.UserService;
56 import org.openecomp.portalapp.portal.transport.FieldsValidator;
57 import org.openecomp.portalapp.portal.transport.LocalRole;
58 import org.openecomp.portalapp.portal.transport.OnboardingApp;
59 import org.openecomp.portalapp.portal.utils.EcompPortalUtils;
60 import org.openecomp.portalapp.util.EPUserUtils;
61
62 @RestController
63 @org.springframework.context.annotation.Configuration
64 @EnableAspectJAutoProxy
65 @EPAuditLog
66 public class AppsController extends EPRestrictedBaseController {
67         
68         static final String FAILURE = "failure";
69         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(AppsController.class);
70
71         @Autowired
72         AdminRolesService adminRolesService;
73         @Autowired
74         EPAppService appService;
75         @Autowired
76         PersUserAppService persUserAppService;
77         @Autowired
78         UserService userService;
79
80         /**
81          * RESTful service method to fetch all Applications available to watch for
82          * current user
83          * 
84          * @return
85          */
86         @RequestMapping(value = { "/portalApi/userApps" }, method = RequestMethod.GET, produces = "application/json")
87         public List<EcompApp> getUserApps(HttpServletRequest request, HttpServletResponse response) {
88                 EPUser user = EPUserUtils.getUserSession(request);
89                 List<EcompApp> ecompApps = null;
90
91                 try {
92                         if (user == null) {
93                                 EcompPortalUtils.setBadPermissions(user, response, "getUserApps");
94                         } else {
95                                 ecompApps = appService.transformAppsToEcompApps(appService.getUserApps(user));
96                                 EcompPortalUtils.logAndSerializeObject("/portalApi/userApps", "GET result =", ecompApps);
97                         }
98                 } catch (Exception e) {
99                         logger.error(EELFLoggerDelegate.errorLogger,
100                                         "Exception occurred while performing getUserApps operation, Details: "
101                                                         + EcompPortalUtils.getStackTrace(e));
102                 }
103
104                 return ecompApps;
105         }
106         
107         /**
108          * Create new application's contact us details.
109          * 
110          * @param contactUs
111          * @return
112          */
113         @RequestMapping(value = "/portalApi/saveNewUser", method = RequestMethod.POST, produces = "application/json")
114         public PortalRestResponse<String> saveNewUser(HttpServletRequest request,@RequestBody EPUser newUser) {
115                 EPUser user = EPUserUtils.getUserSession(request);
116                 if (newUser == null)
117                         return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, FAILURE,
118                                         "New User cannot be null or empty");
119                 
120                 if (!(adminRolesService.isSuperAdmin(user) || adminRolesService.isAccountAdmin(user))){
121                         if(!user.getLoginId().equalsIgnoreCase(newUser.getLoginId()))
122                                 return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, FAILURE,
123                                                 "UnAuthorized");
124                 }
125                         
126         String checkDuplicate = request.getParameter("isCheck");
127                 String saveNewUser = FAILURE;
128                 try {
129                         saveNewUser = userService.saveNewUser(newUser,checkDuplicate);
130                 } catch (Exception e) {
131                         return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, saveNewUser, e.getMessage());
132                 }
133                 return new PortalRestResponse<String>(PortalRestStatusEnum.OK, saveNewUser, "");
134         }
135
136         /**
137          * RESTful service method to fetch all applications accessible to the
138          * current user, with personalizations.
139          * 
140          * @return
141          */
142         @RequestMapping(value = { "/portalApi/persUserApps" }, method = RequestMethod.GET, produces = "application/json")
143         public List<EcompApp> getPersUserApps(HttpServletRequest request, HttpServletResponse response) throws IOException {
144                 EPUser user = EPUserUtils.getUserSession(request);
145                 List<EcompApp> ecompApps = null;
146                 try {
147                         if (user == null) {
148                                 EcompPortalUtils.setBadPermissions(user, response, "getPersUserApps");
149                         } else {
150                                 List<EPApp> apps = null;
151                                 if (adminRolesService.isSuperAdmin(user))
152                                         apps = appService.getPersAdminApps(user);
153                                 else
154                                         apps = appService.getPersUserApps(user);
155                                 ecompApps = appService.transformAppsToEcompApps(apps);
156                                 EcompPortalUtils.logAndSerializeObject("/portalApi/userPersApps", "GET result =", ecompApps);
157                         }
158                 } catch (Exception e) {
159                         logger.error(EELFLoggerDelegate.errorLogger, "Failed in getPersUserApps", e);
160                         response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.toString());
161                 }
162                 return ecompApps;
163         }
164
165         /**
166          * RESTful service method to fetch applications for which the current user
167          * is an Administrator
168          * 
169          * @return
170          */
171         @RequestMapping(value = { "/portalApi/adminApps" }, method = RequestMethod.GET, produces = "application/json")
172         public List<AppIdAndNameTransportModel> getAdminApps(HttpServletRequest request, HttpServletResponse response) {
173                 EPUser user = EPUserUtils.getUserSession(request);
174                 List<AppIdAndNameTransportModel> adminApps = null;
175
176                 try {
177                         if (!adminRolesService.isAccountAdmin(user)) {
178                                 EcompPortalUtils.setBadPermissions(user, response, "getAdminApps");
179                         } else {
180                                 adminApps = appService.getAdminApps(user);
181                                 EcompPortalUtils.logAndSerializeObject("/portalApi/adminApps", "GET result =", adminApps);
182                         }
183                 } catch (Exception e) {
184                         logger.error(EELFLoggerDelegate.errorLogger,
185                                         "Exception occurred while performing getAdminApps operation, Details: "
186                                                         + EcompPortalUtils.getStackTrace(e));
187                 }
188
189                 return adminApps;
190         }
191
192         /**
193          * RESTful service method to fetch Applications in which the logged in user
194          * is an Administrator
195          * 
196          * @return
197          */
198         @RequestMapping(value = {
199                         "/portalApi/appsForSuperAdminAndAccountAdmin" }, method = RequestMethod.GET, produces = "application/json")
200         public List<AppIdAndNameTransportModel> getAppsForSuperAdminAndAccountAdmin(HttpServletRequest request,
201                         HttpServletResponse response) {
202                 EPUser user = EPUserUtils.getUserSession(request);
203                 List<AppIdAndNameTransportModel> adminApps = null;
204
205                 try {
206                         if (!adminRolesService.isSuperAdmin(user) && !adminRolesService.isAccountAdmin(user)) {
207                                 EcompPortalUtils.setBadPermissions(user, response, "getAdminApps");
208                         } else {
209                                 adminApps = appService.getAppsForSuperAdminAndAccountAdmin(user);
210                                 EcompPortalUtils.logAndSerializeObject("/portalApi/appsForSuperAdminAndAccountAdmin", "GET result =",
211                                                 adminApps);
212                         }
213                 } catch (Exception e) {
214                         logger.error(EELFLoggerDelegate.errorLogger,
215                                         "Exception occurred while performing getAppsForSuperAdminAndAccountAdmin operation, Details: "
216                                                         + EcompPortalUtils.getStackTrace(e));
217                 }
218
219                 return adminApps;
220         }
221
222         /**
223          * RESTful service method to fetch Application Administrators to Super
224          * Administrator user. Attention: Users which have Super Administrator roles
225          * only are not included!
226          * 
227          * @return
228          */
229         @RequestMapping(value = { "/portalApi/accountAdmins" }, method = RequestMethod.GET, produces = "application/json")
230         public List<AdminUserApplications> getAppsAdministrators(HttpServletRequest request, HttpServletResponse response) {
231                 EPUser user = EPUserUtils.getUserSession(request);
232                 List<AdminUserApplications> admins = null;
233
234                 try {
235                         if (!adminRolesService.isSuperAdmin(user)) {
236                                 EcompPortalUtils.setBadPermissions(user, response, "getAppsAdministrators");
237                         } else {
238                                 admins = appService.getAppsAdmins();
239                                 EcompPortalUtils.logAndSerializeObject("/portalApi/accountAdmins", "GET result =", admins);
240                         }
241                 } catch (Exception e) {
242                         logger.error(EELFLoggerDelegate.errorLogger,
243                                         "Exception occurred while performing getAppsAdministrators operation, Details: "
244                                                         + EcompPortalUtils.getStackTrace(e));
245                 }
246
247                 return admins;
248         }
249
250         @RequestMapping(value = { "/portalApi/availableApps" }, method = RequestMethod.GET, produces = "application/json")
251         public List<AppsResponse> getApps(HttpServletRequest request, HttpServletResponse response) {
252                 EPUser user = EPUserUtils.getUserSession(request);
253                 List<AppsResponse> apps = null;
254                 try {
255                         if (!adminRolesService.isSuperAdmin(user)) {
256                                 EcompPortalUtils.setBadPermissions(user, response, "getApps");
257                         } else {
258                                 apps = appService.getAllApps(false);
259                                 EcompPortalUtils.logAndSerializeObject("/portalApi/availableApps", "GET result =", apps);
260                         }
261                 } catch (Exception e) {
262                         logger.error(EELFLoggerDelegate.errorLogger,
263                                         "Exception occurred while performing getApps operation, Details: "
264                                                         + EcompPortalUtils.getStackTrace(e));
265                 }
266
267                 return apps;
268         }
269
270         /**
271          * Gets all apps, both active and inactive; i.e., all on-boarded apps,
272          * regardless of enabled status.
273          * 
274          * @param request
275          * @param response
276          * @return List of applications
277          */
278         // This API returns
279         @RequestMapping(value = {
280                         "/portalApi/allAvailableApps" }, method = RequestMethod.GET, produces = "application/json")
281         public List<AppsResponse> getAllApps(HttpServletRequest request, HttpServletResponse response) {
282                 EPUser user = EPUserUtils.getUserSession(request);
283                 List<AppsResponse> apps = null;
284                 try {
285                         if (!adminRolesService.isSuperAdmin(user)) {
286                                 EcompPortalUtils.setBadPermissions(user, response, "getApps");
287                         } else {
288                                 apps = appService.getAllApps(true);
289                                 EcompPortalUtils.logAndSerializeObject("/portalApi/availableApps", "GET result =", apps);
290                         }
291                 } catch (Exception e) {
292                         logger.error(EELFLoggerDelegate.errorLogger, "Failed in getAllApps", e);
293                 }
294
295                 return apps;
296         }
297
298         @RequestMapping(value = { "/portalApi/appsFullList" }, method = RequestMethod.GET, produces = "application/json")
299         public List<EcompApp> getAppsFullList(HttpServletRequest request, HttpServletResponse response) {
300                 EPUser user = EPUserUtils.getUserSession(request);
301                 List<EcompApp> ecompApps = null;
302                 if (user == null) {
303                         EcompPortalUtils.setBadPermissions(user, response, "getAppsFullList");
304                 } else {
305                         ecompApps = appService.getEcompAppAppsFullList();
306                         EcompPortalUtils.logAndSerializeObject("/portalApi/appsFullList", "GET result =", ecompApps);
307                 }
308
309                 return ecompApps;
310         }
311
312         @RequestMapping(value = { "/portalApi/userProfile" }, method = RequestMethod.GET, produces = "application/json")
313         public UserRoles getUserProfile(HttpServletRequest request, HttpServletResponse response) {
314                 EPUser user = EPUserUtils.getUserSession(request);
315                 UserRoles userAndRoles = null;
316                 try {
317                         if (user == null) {
318                                 EcompPortalUtils.setBadPermissions(user, response, "getUserProfile");
319                         } else {
320                                 // Check database.
321                                 userAndRoles = appService.getUserProfile(user.getLoginId());
322                                 // If no roles are defined, treat this user as a guest.
323                                 if (user.isGuest() || userAndRoles == null) {
324                                         logger.debug(EELFLoggerDelegate.debugLogger, "getUserProfile: treating user {} as guest",
325                                                         user.getLoginId());
326                                         UserRole userRole = new UserRole();
327                                         userRole.setOrgUserId(user.getLoginId());
328                                         userRole.setFirstName(user.getFirstName());
329                                         userRole.setLastName(user.getLastName());
330                                         userRole.setRoleId(-1L);
331                                         userRole.setRoleName("Guest");
332                                         userRole.setUser_Id(-1L);
333                                         userAndRoles = new UserRoles(userRole);
334                                 }
335                         }
336                 } catch (Exception e) {
337                         logger.error(EELFLoggerDelegate.errorLogger, "Failed to get user names and roles", e);
338                 }
339
340                 EcompPortalUtils.logAndSerializeObject("/portalApi/userProfile", "getUserProfile result =", userAndRoles);
341                 return userAndRoles;
342         }
343         
344         @RequestMapping(value = { "/portalApi/currentUserProfile/{loginId}" }, method = RequestMethod.GET, produces = "application/json")
345         public String getCurrentUserProfile(HttpServletRequest request, @PathVariable("loginId") String loginId) {
346                 
347                 Map<String,String> map = new HashMap<String,String>();
348                 EPUser user = null;
349                 try {
350                          user = (EPUser) userService.getUserByUserId(loginId).get(0);
351                          map.put("firstName", user.getFirstName());
352                      map.put("lastName", user.getLastName());
353                      map.put("email", user.getEmail());
354                          map.put("loginId", user.getLoginId());
355                          map.put("loginPwd",user.getLoginPwd());
356                          map.put("middleInitial",user.getMiddleInitial());
357                 } catch (Exception e) {
358                         logger.error(EELFLoggerDelegate.errorLogger, "Failed to get user info", e);
359                 }
360
361                 JSONObject j = new JSONObject(map);;
362                 return j.toString();
363         }
364
365         @RequestMapping(value = { "/portalApi/appRoles/{appId}" }, method = {
366                         RequestMethod.GET }, produces = "application/json")
367         public List<LocalRole> getAppRoles(HttpServletRequest request, @PathVariable("appId") Long appId) {
368                 List<LocalRole> roleList = null;
369                 try {
370                         roleList = appService.getAppRoles(appId);
371                         EcompPortalUtils.logAndSerializeObject("/portalApi/appRoles/" + appId, "GET result =", roleList);
372                 } catch (Exception e) {
373                         logger.error(EELFLoggerDelegate.errorLogger,
374                                         "Exception occurred while performing getAppRoles operation, Details: "
375                                                         + EcompPortalUtils.getStackTrace(e));
376                 }
377
378                 return roleList;
379         }
380
381         @RequestMapping(value = { "/portalApi/onboardingApps" }, method = RequestMethod.GET, produces = "application/json")
382         public List<OnboardingApp> getOnboardingApps(HttpServletRequest request, HttpServletResponse response) {
383                 EPUser user = EPUserUtils.getUserSession(request);
384                 List<OnboardingApp> onboardingApps = null;
385                 try {
386                         if (!adminRolesService.isSuperAdmin(user)) {
387                                 EcompPortalUtils.setBadPermissions(user, response, "getOnboardingApps");
388                         } else {
389                                 onboardingApps = appService.getOnboardingApps();
390                                 EcompPortalUtils.logAndSerializeObject("/portalApi/onboardingApps", "GET result =",
391                                                 "onboardingApps of size " + onboardingApps.size());
392                         }
393                 } catch (Exception e) {
394                         logger.error(EELFLoggerDelegate.errorLogger,
395                                         "Exception occurred while performing getOnboardingApps operation, Details: "
396                                                         + EcompPortalUtils.getStackTrace(e));
397                 }
398
399                 return onboardingApps;
400         }
401
402         @RequestMapping(value = { "/portalApi/onboardingApps" }, method = RequestMethod.PUT, produces = "application/json")
403         public FieldsValidator putOnboardingApp(HttpServletRequest request,
404                         @RequestBody OnboardingApp modifiedOnboardingApp, HttpServletResponse response) {
405                 FieldsValidator fieldsValidator = null;
406                 try {
407                         EPUser user = EPUserUtils.getUserSession(request);
408                         if (!adminRolesService.isSuperAdmin(user)) {
409                                 EcompPortalUtils.setBadPermissions(user, response, "putOnboardingApp");
410                         } else {
411                                 modifiedOnboardingApp.normalize();
412                                 fieldsValidator = appService.modifyOnboardingApp(modifiedOnboardingApp, user);
413                                 response.setStatus(fieldsValidator.httpStatusCode.intValue());
414                         }
415                 } catch (Exception e) {
416                         logger.error(EELFLoggerDelegate.errorLogger,
417                                         "Exception occurred while performing putOnboardingApps operation, Details: "
418                                                         + EcompPortalUtils.getStackTrace(e));
419                 }
420
421                 EcompPortalUtils.logAndSerializeObject("/portalApi/onboardingApps", "PUT result =", response.getStatus());
422                 return fieldsValidator;
423         }
424
425         @RequestMapping(value = { "/portalApi/onboardingApps" }, method = RequestMethod.POST, produces = "application/json")
426         public FieldsValidator postOnboardingApp(HttpServletRequest request, @RequestBody OnboardingApp newOnboardingApp,
427                         HttpServletResponse response) {
428                 FieldsValidator fieldsValidator = null;
429                 try {
430                         EPUser user = EPUserUtils.getUserSession(request);
431                         if (!adminRolesService.isSuperAdmin(user)) {
432                                 EcompPortalUtils.setBadPermissions(user, response, "postOnboardingApps");
433                         } else {
434                                 newOnboardingApp.normalize();
435                                 fieldsValidator = appService.addOnboardingApp(newOnboardingApp, user);
436                                 response.setStatus(fieldsValidator.httpStatusCode.intValue());
437                         }
438                 } catch (Exception e) {
439                         logger.error(EELFLoggerDelegate.errorLogger,
440                                         "Exception occurred while performing postOnboardingApps operation, Details: "
441                                                         + EcompPortalUtils.getStackTrace(e));
442                 }
443
444                 EcompPortalUtils.logAndSerializeObject("/portalApi/onboardingApps", "POST result =", response.getStatus());
445                 return fieldsValidator;
446         }
447
448         @RequestMapping(value = { "/portalApi/onboardingApps/{appId}" }, method = {
449                         RequestMethod.DELETE }, produces = "application/json")
450         public FieldsValidator deleteOnboardingApp(HttpServletRequest request, @PathVariable("appId") Long appId,
451                         HttpServletResponse response) {
452                 FieldsValidator fieldsValidator = null;
453                 ;
454                 try {
455                         EPUser user = EPUserUtils.getUserSession(request);
456                         if (!adminRolesService.isSuperAdmin(user)) {
457                                 EcompPortalUtils.setBadPermissions(user, response, "deleteOnboardingApps");
458                         } else {
459                                 fieldsValidator = appService.deleteOnboardingApp(user, appId);
460                                 response.setStatus(fieldsValidator.httpStatusCode.intValue());
461                         }
462                 } catch (Exception e) {
463                         logger.error(EELFLoggerDelegate.errorLogger, EcompPortalUtils.getStackTrace(e));
464                 }
465
466                 EcompPortalUtils.logAndSerializeObject("/portalApi/onboardingApps" + appId, "DELETE result =",
467                                 response.getStatus());
468                 return fieldsValidator;
469         }
470 }