Docker changes and Music Integration
[portal.git] / ecomp-portal-BE-common / src / main / java / org / onap / portalapp / portal / controller / ExternalAccessRolesController.java
index 0be015e..f846253 100644 (file)
@@ -39,6 +39,7 @@ package org.onap.portalapp.portal.controller;
 
 
 import java.io.IOException;
+import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -69,10 +70,15 @@ import org.onap.portalapp.portal.utils.PortalConstants;
 import org.onap.portalsdk.core.domain.AuditLog;
 import org.onap.portalsdk.core.domain.Role;
 import org.onap.portalsdk.core.domain.RoleFunction;
+import org.onap.portalsdk.core.domain.User;
 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
+import org.onap.portalsdk.core.restful.domain.EcompRole;
 import org.onap.portalsdk.core.restful.domain.EcompUser;
 import org.onap.portalsdk.core.service.AuditService;
+import org.onap.portalsdk.core.service.UserService;
+import org.onap.portalsdk.core.service.UserServiceCentalizedImpl;
 import org.onap.portalsdk.core.util.SystemProperties;
+import org.onap.portalsdk.core.web.support.UserUtils;
 import org.slf4j.MDC;
 import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -87,6 +93,7 @@ import org.springframework.web.client.RestTemplate;
 
 import com.fasterxml.jackson.databind.DeserializationFeature;
 import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.type.TypeFactory;
 
 import io.swagger.annotations.ApiOperation;
 
@@ -117,6 +124,9 @@ public class ExternalAccessRolesController implements BasicAuthenticationControl
        @Autowired
        private ExternalAccessRolesService externalAccessRolesService;
 
+       @Autowired
+       private UserService userservice =  new UserServiceCentalizedImpl();
+
        @ApiOperation(value = "Gets user role for an application.", response = CentralUser.class, responseContainer="List")
        @RequestMapping(value = {
                        "/user/{loginId}" }, method = RequestMethod.GET, produces = "application/json")
@@ -136,7 +146,7 @@ public class ExternalAccessRolesController implements BasicAuthenticationControl
        
        @ApiOperation(value = "Gets user roles for an application which is upgraded to newer version.", response = String.class, responseContainer="List")
        @RequestMapping(value = {
-                       "/v2/user/{loginId}" }, method = RequestMethod.GET, produces = "application/json")
+                       "/v1/user/{loginId}" }, method = RequestMethod.GET, produces = "application/json")
        public String getV2UserList(HttpServletRequest request, HttpServletResponse response,
                        @PathVariable("loginId") String loginId) throws Exception {
                String answer = null;
@@ -176,7 +186,7 @@ public class ExternalAccessRolesController implements BasicAuthenticationControl
        
        @ApiOperation(value = "Gets roles for an application which is upgraded to newer version.", response = CentralV2Role.class, responseContainer="Json")
        @RequestMapping(value = {
-                       "/v2/roles" }, method = RequestMethod.GET, produces = "application/json")
+                       "/v1/roles" }, method = RequestMethod.GET, produces = "application/json")
        public List<CentralV2Role> getV2RolesForApp(HttpServletRequest request, HttpServletResponse response) throws Exception {
                logger.debug(EELFLoggerDelegate.debugLogger, "Request received for getV2RolesForApp");
                List<CentralV2Role> answer = null;
@@ -199,10 +209,10 @@ public class ExternalAccessRolesController implements BasicAuthenticationControl
        @ApiOperation(value = "Gets all role functions for an application for older version.", response = CentralRoleFunction.class, responseContainer="Json")
        @RequestMapping(value = {
                        "/functions" }, method = RequestMethod.GET, produces = "application/json")
-       public List<RoleFunction> getRoleFunctionsList(HttpServletRequest request, HttpServletResponse response)
+       public List<CentralRoleFunction> getRoleFunctionsList(HttpServletRequest request, HttpServletResponse response)
                        throws Exception {
                List<CentralV2RoleFunction> answer = null;
-               List<RoleFunction> roleFuncList = null;
+               List<CentralRoleFunction> roleFuncList = null;
                logger.debug(EELFLoggerDelegate.debugLogger, "Request received for getRoleFunctionsList");
                try {
                        fieldsValidation(request);
@@ -223,7 +233,7 @@ public class ExternalAccessRolesController implements BasicAuthenticationControl
        
        @ApiOperation(value = "Gets all role functions for an application which is upgraded to newer version.", response = CentralV2RoleFunction.class, responseContainer="Json")
        @RequestMapping(value = {
-                       "/v2/functions" }, method = RequestMethod.GET, produces = "application/json")
+                       "/v1/functions" }, method = RequestMethod.GET, produces = "application/json")
        public List<CentralV2RoleFunction> getV2RoleFunctionsList(HttpServletRequest request, HttpServletResponse response)
                        throws Exception {
                List<CentralV2RoleFunction> cenRoleFuncList = null;
@@ -243,6 +253,7 @@ public class ExternalAccessRolesController implements BasicAuthenticationControl
                logger.debug(EELFLoggerDelegate.debugLogger, "Request completed for getV2RoleFunctionsList");
                return cenRoleFuncList;
        }       
+       
 
        @ApiOperation(value = "Gets role information for an application.", response = CentralRole.class, responseContainer="Json")
        @RequestMapping(value = {
@@ -266,7 +277,7 @@ public class ExternalAccessRolesController implements BasicAuthenticationControl
        
        @ApiOperation(value = "Gets v2 role information for an application which is upgraded to newer version.", response = CentralV2Role.class, responseContainer="Json")
        @RequestMapping(value = {
-                       "/v2/role/{role_id}" }, method = RequestMethod.GET, produces = "application/json")
+                       "/v1/role/{role_id}" }, method = RequestMethod.GET, produces = "application/json")
        public CentralV2Role getV2RoleInfo(HttpServletRequest request, HttpServletResponse response,
                        @PathVariable("role_id") Long roleId) throws Exception {
                CentralV2Role answer = null;
@@ -302,7 +313,7 @@ public class ExternalAccessRolesController implements BasicAuthenticationControl
        }
        
        @ApiOperation(value = "Gets role information for an application provided by function code.", response = CentralV2RoleFunction.class, responseContainer = "Json")
-       @RequestMapping(value = { "/v2/function/{code}" }, method = RequestMethod.GET, produces = "application/json")
+       @RequestMapping(value = { "/v1/function/{code}" }, method = RequestMethod.GET, produces = "application/json")
        public CentralV2RoleFunction getV2RoleFunction(HttpServletRequest request, HttpServletResponse response,
                        @PathVariable("code") String code) throws Exception {
                CentralV2RoleFunction centralV2RoleFunction = null;
@@ -331,10 +342,20 @@ public class ExternalAccessRolesController implements BasicAuthenticationControl
                        EPApp requestedApp = applicationList.get(0);
                        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
                        CentralV2RoleFunction availableRoleFunction = mapper.readValue(data, CentralV2RoleFunction.class);
-                       CentralV2RoleFunction domainRoleFunction = externalAccessRolesService.getRoleFunction(availableRoleFunction.getCode(),
-                                       requestedApp.getUebKey());
+                       CentralV2RoleFunction domainRoleFunction = null;
+                       if(availableRoleFunction.getType()!=null && availableRoleFunction.getAction()!= null) {
+                               String code = availableRoleFunction.getType()+"|"+availableRoleFunction.getCode()+"|"+availableRoleFunction.getAction();
+                               domainRoleFunction = externalAccessRolesService.getRoleFunction(code,
+                                               requestedApp.getUebKey());
+                       } else {
+                               domainRoleFunction = externalAccessRolesService.getRoleFunction(availableRoleFunction.getCode(),
+                                               requestedApp.getUebKey());
+                       }
+       
                        boolean saveOrUpdateResponse = false;
-                       if (domainRoleFunction != null) {
+                       if (domainRoleFunction != null && domainRoleFunction.getCode().equals(availableRoleFunction.getCode())
+                                       && domainRoleFunction.getType().equals(availableRoleFunction.getType())
+                                       && domainRoleFunction.getAction().equals(availableRoleFunction.getAction())) {
                                domainRoleFunction.setName(availableRoleFunction.getName());
                                saveOrUpdateResponse = externalAccessRolesService.saveCentralRoleFunction(domainRoleFunction,
                                                requestedApp);
@@ -552,16 +573,32 @@ public class ExternalAccessRolesController implements BasicAuthenticationControl
                return new PortalRestResponse<String>(PortalRestStatusEnum.OK, SUCCESSFULLY_DELETED, "Success");
        }
        
-       @ApiOperation(value = "Gets active roles for an application.", response = CentralV2Role.class, responseContainer = "Json")
+       @ApiOperation(value = "Gets active roles for an application.", response = CentralRole.class, responseContainer = "Json")
        @RequestMapping(value = { "/activeRoles" }, method = RequestMethod.GET, produces = "application/json")
-       public  List<CentralV2Role> getActiveRoles(HttpServletRequest request, HttpServletResponse response) throws Exception {
+       public  List<CentralRole> getActiveRoles(HttpServletRequest request, HttpServletResponse response) throws Exception {
+               List<CentralRole> roles = null;
+               try {
+                       fieldsValidation(request);
+                       List<CentralV2Role> cenRoles= externalAccessRolesService.getActiveRoles(request.getHeader(UEBKEY));
+                       roles = externalAccessRolesService.convertV2CentralRoleListToOldVerisonCentralRoleList(cenRoles);
+               } catch (Exception e) {
+                       sendErrorResponse(response, e);         
+                       logger.error(EELFLoggerDelegate.errorLogger, "getActiveRoles failed", e);
+               }
+               return roles;
+               
+       }
+       
+       @ApiOperation(value = "Gets active roles for an application.", response = CentralV2Role.class, responseContainer = "Json")
+       @RequestMapping(value = { "/v1/activeRoles" }, method = RequestMethod.GET, produces = "application/json")
+       public  List<CentralV2Role> getV2ActiveRoles(HttpServletRequest request, HttpServletResponse response) throws Exception {
                List<CentralV2Role> cenRole = null;
                try {
                        fieldsValidation(request);
                        cenRole = externalAccessRolesService.getActiveRoles(request.getHeader(UEBKEY));
                } catch (Exception e) {
                        sendErrorResponse(response, e);         
-                       logger.error(EELFLoggerDelegate.errorLogger, "getActiveRoles failed", e);
+                       logger.error(EELFLoggerDelegate.errorLogger, "getV2ActiveRoles failed", e);
                }
                return cenRole;
                
@@ -750,11 +787,11 @@ public class ExternalAccessRolesController implements BasicAuthenticationControl
        public  List<EcompUser> getUsersOfApplication(HttpServletRequest request, HttpServletResponse response) throws Exception {
                List<EcompUser> users = null;
                try {
+                       fieldsValidation(request);
                        users = externalAccessRolesService.getAllAppUsers(request.getHeader(UEBKEY));
                } catch (Exception e) {         
-                       response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
+                       sendErrorResponse(response, e);
                        logger.error(EELFLoggerDelegate.errorLogger, "getUsersOfApplication failed", e);
-                       throw new Exception(e.getMessage());
                }
                return users;
        }
@@ -804,4 +841,60 @@ public class ExternalAccessRolesController implements BasicAuthenticationControl
                        response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
                }
        }
+       
+       @ApiOperation(value = "Gets ecompUser of an application.", response = CentralUser.class, responseContainer = "List")
+       @RequestMapping(value = { "/v2/user/{loginId}" }, method = RequestMethod.GET, produces = "application/json")
+       public String getEcompUser(HttpServletRequest request, HttpServletResponse response,
+                       @PathVariable("loginId") String loginId) throws Exception {
+               EcompUser user = null;
+               ObjectMapper mapper = new ObjectMapper();
+               String answer = null;
+               try {
+                       fieldsValidation(request);
+                       answer = externalAccessRolesService.getV2UserWithRoles(loginId, request.getHeader(UEBKEY));
+                       if (answer != null) {
+                User ecompUser = userservice.userMapper(answer);
+                               user = UserUtils.convertToEcompUser(ecompUser);
+                       }
+               } catch (Exception e) {
+                       sendErrorResponse(response, e); 
+                       logger.error(EELFLoggerDelegate.errorLogger, "getEcompUser failed", e);
+               }
+               if (user != null) {
+                       return mapper.writeValueAsString(user);
+               }
+               return null;
+       }
+
+       @ApiOperation(value = "Gets user ecomp role for an application.", response = CentralUser.class, responseContainer = "List")
+       @RequestMapping(value = { "/v2/roles" }, method = RequestMethod.GET, produces = "application/json")
+       public List<EcompRole> getEcompRolesOfApplication(HttpServletRequest request, HttpServletResponse response)
+                       throws Exception {
+               List<EcompRole> ecompRoles = null;
+               ObjectMapper mapper = new ObjectMapper();
+               List<CentralV2Role> cenRole = null;
+               try {
+                       fieldsValidation(request);
+                       EPApp app = externalAccessRolesService.getApp(request.getHeader(UEBKEY)).get(0);
+                       // Sync all roles from external system into Ecomp portal DB
+                       logger.debug(EELFLoggerDelegate.debugLogger,
+                                       "getRolesForApp: Entering into syncApplicationRolesWithEcompDB");
+                       externalAccessRolesService.syncApplicationRolesWithEcompDB(app);
+                       logger.debug(EELFLoggerDelegate.debugLogger, "getRolesForApp: Finished syncApplicationRolesWithEcompDB");
+                       cenRole = externalAccessRolesService.getActiveRoles(request.getHeader(UEBKEY));
+               } catch (Exception e) {
+                       sendErrorResponse(response, e);
+                       logger.error(EELFLoggerDelegate.errorLogger, "getActiveRoles failed", e);
+               }
+               if (cenRole != null) {
+                       String res = mapper.writeValueAsString(cenRole);
+                       ecompRoles = new ArrayList<>();
+                       List<Role> roles = mapper.readValue(res,
+                                       TypeFactory.defaultInstance().constructCollectionType(List.class, Role.class));
+                       for (Role role : roles)
+                               ecompRoles.add(UserUtils.convertToEcompRole(role));
+                       logger.debug(EELFLoggerDelegate.debugLogger, "Request completed for getEcompRolesOfApplication");
+               }
+               return ecompRoles;
+       }
 }