Bulk upload changes and music health check apis
[portal.git] / ecomp-portal-BE-common / src / main / java / org / onap / portalapp / portal / controller / ExternalAccessRolesController.java
index 0be015e..e0bf7b0 100644 (file)
  *
  * ============LICENSE_END============================================
  *
- * ECOMP is a trademark and service mark of AT&T Intellectual Property.
+ * 
  */
 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;
@@ -68,11 +69,15 @@ import org.onap.portalapp.portal.utils.EcompPortalUtils;
 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 +92,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 +123,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 +145,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;
@@ -160,7 +169,7 @@ public class ExternalAccessRolesController implements BasicAuthenticationControl
                try {
                        fieldsValidation(request);      
                        EPApp app = externalAccessRolesService.getApp(request.getHeader(UEBKEY)).get(0);
-                       // Sync all roles from external system into Ecomp portal DB
+                       // Sync all roles from external system into ONAP portal DB
                        logger.debug(EELFLoggerDelegate.debugLogger, "getRolesForApp: Entering into syncApplicationRolesWithEcompDB");
                        externalAccessRolesService.syncApplicationRolesWithEcompDB(app);
                        logger.debug(EELFLoggerDelegate.debugLogger, "getRolesForApp: Finished syncApplicationRolesWithEcompDB");
@@ -176,14 +185,14 @@ 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;
                try {
                        fieldsValidation(request);
                        EPApp app = externalAccessRolesService.getApp(request.getHeader(UEBKEY)).get(0);
-                       // Sync all roles from external system into Ecomp portal DB
+                       // Sync all roles from external system into ONAP portal DB
                        logger.debug(EELFLoggerDelegate.debugLogger, "getV2RolesForApp: Entering into syncApplicationRolesWithEcompDB");
                        externalAccessRolesService.syncApplicationRolesWithEcompDB(app);
                        logger.debug(EELFLoggerDelegate.debugLogger, "getV2RolesForApp: Finished syncApplicationRolesWithEcompDB");
@@ -199,15 +208,15 @@ 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);
                        EPApp app = externalAccessRolesService.getApp(request.getHeader(UEBKEY)).get(0);
-                       // Sync all functions from external system into Ecomp portal DB
+                       // Sync all functions from external system into ONAP portal DB
                        logger.debug(EELFLoggerDelegate.debugLogger, "getRoleFunctionsList: Entering into syncRoleFunctionFromExternalAccessSystem");
                        externalAccessRolesService.syncRoleFunctionFromExternalAccessSystem(app);
                        logger.debug(EELFLoggerDelegate.debugLogger, "getRoleFunctionsList: Finished syncRoleFunctionFromExternalAccessSystem");
@@ -223,7 +232,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;
@@ -231,7 +240,7 @@ public class ExternalAccessRolesController implements BasicAuthenticationControl
                try {
                        fieldsValidation(request);
                        EPApp app = externalAccessRolesService.getApp(request.getHeader(UEBKEY)).get(0);
-                       // Sync all functions from external system into Ecomp portal DB
+                       // Sync all functions from external system into ONAP portal DB
                        logger.debug(EELFLoggerDelegate.debugLogger, "getV2RoleFunctionsList: Entering into syncRoleFunctionFromExternalAccessSystem");
                        externalAccessRolesService.syncRoleFunctionFromExternalAccessSystem(app);
                        logger.debug(EELFLoggerDelegate.debugLogger, "getV2RoleFunctionsList: Finished syncRoleFunctionFromExternalAccessSystem");
@@ -243,6 +252,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 +276,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 +312,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;
@@ -323,6 +333,7 @@ public class ExternalAccessRolesController implements BasicAuthenticationControl
        @RequestMapping(value = { "/roleFunction" }, method = RequestMethod.POST, produces = "application/json")
        public PortalRestResponse<String> saveRoleFunction(HttpServletRequest request, HttpServletResponse response,
                        @RequestBody String roleFunc) throws Exception {
+               String status = "Successfully saved!";
                try {
                        fieldsValidation(request);
                        String data = roleFunc;
@@ -331,10 +342,24 @@ 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;
+                       boolean isCentralV2Version = false;
+                       if(availableRoleFunction.getType()!=null && availableRoleFunction.getAction()!= null) {
+                               isCentralV2Version = true;
+                       }
+                       if(isCentralV2Version) {
+                               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 && isCentralV2Version && domainRoleFunction.getCode().equals(availableRoleFunction.getCode())
+                                       && domainRoleFunction.getType().equals(availableRoleFunction.getType())
+                                       && domainRoleFunction.getAction().equals(availableRoleFunction.getAction())) {
                                domainRoleFunction.setName(availableRoleFunction.getName());
                                saveOrUpdateResponse = externalAccessRolesService.saveCentralRoleFunction(domainRoleFunction,
                                                requestedApp);
@@ -342,13 +367,15 @@ public class ExternalAccessRolesController implements BasicAuthenticationControl
                                availableRoleFunction.setAppId(requestedApp.getId());
                                saveOrUpdateResponse = externalAccessRolesService.saveCentralRoleFunction(availableRoleFunction,
                                                requestedApp);
-                       }                       
+                       }               
+               
+                       if(domainRoleFunction != null) {
+                               status = "Successfully updated!";
+                       }
                        if (saveOrUpdateResponse) {
                                EPUser user = externalAccessRolesService.getUser(request.getHeader(LOGIN_ID)).get(0);
                                EPApp app = externalAccessRolesService.getApp(request.getHeader(UEBKEY)).get(0);
-                               CentralV2RoleFunction function = externalAccessRolesService
-                                               .getRoleFunction(availableRoleFunction.getCode(), request.getHeader(UEBKEY));
-                               String activityCode = (function.getCode() == null)
+                               String activityCode = (!status.equals("Successfully updated!"))
                                                ? EcompAuditLog.CD_ACTIVITY_EXTERNAL_AUTH_ADD_FUNCTION
                                                : EcompAuditLog.CD_ACTIVITY_EXTERNAL_AUTH_UPDATE_FUNCTION;
                                logger.info(EELFLoggerDelegate.applicationLogger, "saveRoleFunction: succeeded for app {}, function {}",
@@ -380,7 +407,7 @@ public class ExternalAccessRolesController implements BasicAuthenticationControl
                                                "Failed to saveRoleFunction for '" + availableRoleFunction.getCode() + "'", "Failed");
                        }
                } catch (Exception e) {
-                       if (e.getMessage().contains(INVALID_UEB_KEY)) {
+                       if (e.getMessage() == null ||e.getMessage().contains(INVALID_UEB_KEY)) {
                                response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
                        }else {
                                response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
@@ -388,7 +415,7 @@ public class ExternalAccessRolesController implements BasicAuthenticationControl
                        logger.error(EELFLoggerDelegate.errorLogger, "saveRoleFunction failed", e);
                        return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, e.getMessage(), "Failed");
                }
-               return new PortalRestResponse<String>(PortalRestStatusEnum.OK, "Successfully Saved", "Success");
+               return new PortalRestResponse<String>(PortalRestStatusEnum.OK, status, "Success");
        }
        
        @ApiOperation(value = "Deletes role function for an application.", response = PortalRestResponse.class, responseContainer = "Json")
@@ -552,16 +579,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;
                
@@ -694,15 +737,16 @@ public class ExternalAccessRolesController implements BasicAuthenticationControl
        
        @ApiOperation(value = "Bulk upload functions for an partner application.", response = PortalRestResponse.class, responseContainer = "Json")
        @RequestMapping(value = { "/upload/partner/functions" }, method = RequestMethod.POST, produces = "application/json")
-       public  PortalRestResponse<String> bulkUploadPartnerFunctions(HttpServletRequest request, HttpServletResponse response, @RequestBody List<RoleFunction> upload) throws Exception {
+       public  PortalRestResponse<String> bulkUploadPartnerFunctions(HttpServletRequest request, HttpServletResponse response) throws Exception {
+               Integer addedFunctions = 0;
                try {
-                       externalAccessRolesService.bulkUploadPartnerFunctions(request.getHeader(UEBKEY), upload);
+                       addedFunctions = externalAccessRolesService.bulkUploadPartnerFunctions(request.getHeader(UEBKEY));
                } catch (Exception e) {
                        response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
                        logger.error(EELFLoggerDelegate.errorLogger, "bulkUploadFunctions failed", e);
                        return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "Failed to bulkUploadFunctions", "Failed");
                }
-               return new PortalRestResponse<String>(PortalRestStatusEnum.OK, "Successfully added", "Success");
+               return new PortalRestResponse<String>(PortalRestStatusEnum.OK, "Successfully added: '"+addedFunctions+"' functions", "Success");
        }
        
        @ApiOperation(value = "Bulk upload roles for an partner application.", response = PortalRestResponse.class, responseContainer = "Json")
@@ -720,15 +764,16 @@ public class ExternalAccessRolesController implements BasicAuthenticationControl
        
        @ApiOperation(value = "Bulk upload role functions for an partner application.", response = PortalRestResponse.class, responseContainer = "Json")
        @RequestMapping(value = { "/upload/partner/roleFunctions" }, method = RequestMethod.POST, produces = "application/json")
-       public  PortalRestResponse<String> bulkUploadPartnerRoleFunctions(HttpServletRequest request, HttpServletResponse response, @RequestBody List<Role> upload) throws Exception {
+       public  PortalRestResponse<String> bulkUploadPartnerRoleFunctions(HttpServletRequest request, HttpServletResponse response) throws Exception {
+               Integer addedRoleFunctions = 0;
                try {
-                       externalAccessRolesService.bulkUploadPartnerRoleFunctions(request.getHeader(UEBKEY), upload);
+                       addedRoleFunctions = externalAccessRolesService.bulkUploadPartnerRoleFunctions(request.getHeader(UEBKEY));
                } catch (Exception e) {
                        response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
                        logger.error(EELFLoggerDelegate.errorLogger, "bulkUploadRoles failed", e);
                        return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "Failed to bulkUploadPartnerRoleFunctions", "Failed");
                }
-               return new PortalRestResponse<String>(PortalRestStatusEnum.OK, "Successfully added", "Success");
+               return new PortalRestResponse<String>(PortalRestStatusEnum.OK, "Successfully added: '"+addedRoleFunctions + "' role functions", "Success");
        }
        
        @ApiOperation(value = "Gets all functions along with global functions", response = List.class, responseContainer = "Json")
@@ -750,11 +795,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 +849,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 ONAP 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;
+       }
 }