fixes for supporting non-gui application access provisioning
[portal.git] / ecomp-portal-BE-common / src / main / java / org / onap / portalapp / portal / service / ExternalAccessRolesServiceImpl.java
index 09afcf7..ad06dd9 100644 (file)
@@ -311,8 +311,8 @@ public class ExternalAccessRolesServiceImpl implements ExternalAccessRolesServic
                                        boolean isRoleNameChanged = false;
                                        if (!desc.equals(updateExtRole.getName())) {
                                                isRoleNameChanged = true;
+                                               addRole(updateExtRole, app.getUebKey()); //interchange 314 -315
                                                deleteRoleInExtSystem(mapper, name);
-                                               addRole(updateExtRole, app.getUebKey());
                                                // add partner functions to the global role in External
                                                // Auth System
                                                if (!list.isEmpty() && isGlobalRole) {
@@ -348,7 +348,7 @@ public class ExternalAccessRolesServiceImpl implements ExternalAccessRolesServic
                                                                roleFunc.getCode(), roleFunc.getAction());
                                                extAddRolePerms = new ExternalAccessRolePerms(extAddPerms,
                                                                app.getNameSpace() + "." + updateExtRole.getName().replaceAll(
-                                                                               EcompPortalUtils.EXTERNAL_CENTRAL_AUTH_ROLE_HANDLE_SPECIAL_CHARACTERS, "_"));
+                                                               EcompPortalUtils.EXTERNAL_CENTRAL_AUTH_ROLE_HANDLE_SPECIAL_CHARACTERS, "_"));
                                                response = addRoleFuncExtSysRestAPI(mapper, extAddRolePerms, headers);
                                        }
                                }
@@ -1179,14 +1179,19 @@ public class ExternalAccessRolesServiceImpl implements ExternalAccessRolesServic
                                                                        && (globalRole.toLowerCase().startsWith("global_")))) {
                                                CentralV2UserApp cua = new CentralV2UserApp();
                                                cua.setUserId(null);
-                                               CentralApp cenApp = new CentralApp(1L, epApp.getCreated(), epApp.getModified(),
-                                                               epApp.getCreatedId(), epApp.getModifiedId(), epApp.getRowNum(), epApp.getName(),
-                                                               epApp.getImageUrl(), epApp.getDescription(), epApp.getNotes(), epApp.getUrl(),
-                                                               epApp.getAlternateUrl(), epApp.getAppRestEndpoint(), epApp.getMlAppName(),
-                                                               epApp.getMlAppAdminId(), String.valueOf(epApp.getMotsId()), epApp.getAppPassword(),
-                                                               String.valueOf(epApp.getOpen()), String.valueOf(epApp.getEnabled()),
-                                                               epApp.getThumbnail(), epApp.getUsername(), epApp.getUebKey(), epApp.getUebSecret(),
-                                                               epApp.getUebTopicName());
+                        CentralApp cenApp = new CentralApp.CentralAppBuilder().setId(1L).setCreated(epApp.getCreated())
+                                .setModified(epApp.getModified()).setCreatedId(epApp.getCreatedId())
+                                .setModifiedId(epApp.getModifiedId()).setRowNum(epApp.getRowNum())
+                                .setName(epApp.getName()).setImageUrl(epApp.getImageUrl())
+                                .setDescription(epApp.getAppDescription()).setNotes(epApp.getAppNotes())
+                                .setUrl(epApp.getLandingPage()).setAlternateUrl(epApp.getLandingPage())
+                                .setRestEndpoint(epApp.getAppRestEndpoint()).setMlAppName(epApp.getMlAppName())
+                                .setMlAppAdminId(epApp.getMlAppAdminId()).setMotsId(String.valueOf(epApp.getMotsId()))
+                                .setAppPassword(epApp.getAppBasicAuthPassword()).setOpen(String.valueOf(epApp.getOpen()))
+                                .setEnabled(String.valueOf(epApp.getEnabled())).setThumbnail(epApp.getThumbnail())
+                                .setUsername(epApp.getAppBasicAuthUsername()).setUebKey(epApp.getUebKey())
+                                .setUebSecret(epApp.getUebSecret()).setUebTopicName(epApp.getUebTopicName())
+                                .createCentralApp();
                                                cenApp.setAppPassword(EPCommonSystemProperties.APP_DISPLAY_PASSWORD);
                                                cua.setApp(cenApp);
                                                Long appId = null;
@@ -1224,11 +1229,14 @@ public class ExternalAccessRolesServiceImpl implements ExternalAccessRolesServic
                                                } else {
                                                        userRoleId = userApp.getRole().getAppRoleId();
                                                }
-                                               CentralV2Role cenRole = new CentralV2Role(userRoleId, userApp.getRole().getCreated(),
-                                                               userApp.getRole().getModified(), userApp.getRole().getCreatedId(),
-                                                               userApp.getRole().getModifiedId(), userApp.getRole().getRowNum(),
-                                                               userApp.getRole().getName(), userApp.getRole().getActive(),
-                                                               userApp.getRole().getPriority(), roleFunctionSet, null, null);
+                        CentralV2Role cenRole = new CentralV2Role.CentralV2RoleBuilder().setId(userRoleId)
+                                .setCreated(userApp.getRole().getCreated()).setModified(userApp.getRole().getModified())
+                                .setCreatedId(userApp.getRole().getCreatedId())
+                                .setModifiedId(userApp.getRole().getModifiedId())
+                                .setRowNum(userApp.getRole().getRowNum()).setName(userApp.getRole().getName())
+                                .setActive(userApp.getRole().getActive()).setPriority(userApp.getRole().getPriority())
+                                .setRoleFunctions(roleFunctionSet).setChildRoles(null).setParentRoles(null)
+                                .createCentralV2Role();
                                                cua.setRole(cenRole);
                                                userAppList.getUserApps().add(cua);
                                        }
@@ -1271,7 +1279,7 @@ public class ExternalAccessRolesServiceImpl implements ExternalAccessRolesServic
        public CentralV2Role getRoleInfo(Long roleId, String uebkey) throws Exception {
                final Map<String, Long> params = new HashMap<>();
                List<CentralV2Role> roleList = new ArrayList<>();
-               CentralV2Role cenRole = new CentralV2Role();
+        CentralV2Role cenRole = new CentralV2Role.CentralV2RoleBuilder().createCentralV2Role();
                List<EPRole> roleInfo = null;
                List<EPApp> app = null;
                try {
@@ -1363,15 +1371,19 @@ public class ExternalAccessRolesServiceImpl implements ExternalAccessRolesServic
                        SortedSet<CentralV2Role> childRoles = new TreeSet<>();
                        SortedSet<CentralV2Role> parentRoles = new TreeSet<>();
                        CentralV2Role cenRole = null;
-                       if (role.getAppRoleId() == null) {
-                               cenRole = new CentralV2Role(role.getId(), role.getCreated(), role.getModified(), role.getCreatedId(),
-                                               role.getModifiedId(), role.getRowNum(), role.getName(), role.getActive(), role.getPriority(),
-                                               roleFunctionSet, childRoles, parentRoles);
-                       } else {
-                               cenRole = new CentralV2Role(role.getAppRoleId(), role.getCreated(), role.getModified(),
-                                               role.getCreatedId(), role.getModifiedId(), role.getRowNum(), role.getName(), role.getActive(),
-                                               role.getPriority(), roleFunctionSet, childRoles, parentRoles);
-                       }
+            if (role.getAppRoleId() == null) {
+                cenRole = new CentralV2Role.CentralV2RoleBuilder().setId(role.getId()).setCreated(role.getCreated())
+                        .setModified(role.getModified()).setCreatedId(role.getCreatedId())
+                        .setModifiedId(role.getModifiedId()).setRowNum(role.getRowNum()).setName(role.getName())
+                        .setActive(role.getActive()).setPriority(role.getPriority()).setRoleFunctions(roleFunctionSet)
+                        .setChildRoles(childRoles).setParentRoles(parentRoles).createCentralV2Role();
+            } else {
+                cenRole = new CentralV2Role.CentralV2RoleBuilder().setId(role.getAppRoleId())
+                        .setCreated(role.getCreated()).setModified(role.getModified()).setCreatedId(role.getCreatedId())
+                        .setModifiedId(role.getModifiedId()).setRowNum(role.getRowNum()).setName(role.getName())
+                        .setActive(role.getActive()).setPriority(role.getPriority()).setRoleFunctions(roleFunctionSet)
+                        .setChildRoles(childRoles).setParentRoles(parentRoles).createCentralV2Role();
+            }
                        roleList.add(cenRole);
                }
                return roleList;
@@ -1961,14 +1973,29 @@ public class ExternalAccessRolesServiceImpl implements ExternalAccessRolesServic
                                                        .containsKey(extAuthrole.substring(app.getNameSpace().length() + 1))) {
                                                EPRole localAddFuntionRole = currentRolesInDB
                                                                .get(extAuthrole.substring(app.getNameSpace().length() + 1));
+                                               
+                                               String key = extAuthrole.substring(app.getNameSpace().length() + 1);
+                                               Boolean isaddAppRoleFuncRequired = true;
+                                               if(localAddFuntionRole == null && key.contains("-")) {
+                                                       key = key.replaceAll(EcompPortalUtils.EXTERNAL_CENTRAL_AUTH_ROLE_HANDLE_SPECIAL_CHARACTERS, "_");
+                                                       localAddFuntionRole = currentRolesInDB.get(key);
+                                                       if(localAddFuntionRole !=null) {
+                                                               isaddAppRoleFuncRequired = false;
+                                                       }
+                                               }
+                                               
+                                               logger.info("key >>>>>"+key);
+                                               logger.info("localAddFuntionRole >>>>>"+localAddFuntionRole);
                                                if (localAddFuntionRole == null) {
                                                        checkAndAddRoleInDB(app, currentRolesInDB, roleFunctionList, extAuthrole);
                                                } else {
-                                                       EPAppRoleFunction addAppRoleFunc = new EPAppRoleFunction();
-                                                       addAppRoleFunc.setAppId(app.getId());
-                                                       addAppRoleFunc.setCode(roleFunctionList.get(0).getCode());
-                                                       addAppRoleFunc.setRoleId(localAddFuntionRole.getId());
-                                                       dataAccessService.saveDomainObject(addAppRoleFunc, null);
+                                                       if(isaddAppRoleFuncRequired) {
+                                                               EPAppRoleFunction addAppRoleFunc = new EPAppRoleFunction();
+                                                               addAppRoleFunc.setAppId(app.getId());
+                                                               addAppRoleFunc.setCode(roleFunctionList.get(0).getCode());
+                                                               addAppRoleFunc.setRoleId(localAddFuntionRole.getId());
+                                                               dataAccessService.saveDomainObject(addAppRoleFunc, null);
+                                                       }
                                                }
                                        }
                                        // This block is to save global role function if exists
@@ -2672,11 +2699,11 @@ public class ExternalAccessRolesServiceImpl implements ExternalAccessRolesServic
                        applicationRolesList = getAppRoles(app.getId());
                        List<String> applicationRoleIdList = new ArrayList<>();
                        for (EPRole applicationRole : applicationRolesList) {
-                               applicationRoleIdList.add(applicationRole.getName());
+                               applicationRoleIdList.add(applicationRole.getName().trim());
                        }
                        List<EPRole> roleListToBeAddInEcompDB = new ArrayList<>();
                        for (EPRole aafRole : finalRoleList) {
-                               if (!applicationRoleIdList.contains(aafRole.getName())) {
+                               if (!applicationRoleIdList.contains(aafRole.getName().trim())) {
                                        roleListToBeAddInEcompDB.add(aafRole);
                                }
                        }
@@ -2810,7 +2837,12 @@ public class ExternalAccessRolesServiceImpl implements ExternalAccessRolesServic
                        String name = extRole.getJSONObject(i).getString(ROLE_NAME);
                        String actualRoleName = name.substring(app.getNameSpace().length() + 1);
                        if (extRole.getJSONObject(i).has(EXTERNAL_AUTH_ROLE_DESCRIPTION)) {
-                               actualRoleName = extRole.getJSONObject(i).getString(EXTERNAL_AUTH_ROLE_DESCRIPTION);
+                               String description = extRole.getJSONObject(i).getString(EXTERNAL_AUTH_ROLE_DESCRIPTION);
+                                                               
+                               if(actualRoleName.replaceAll(EcompPortalUtils.EXTERNAL_CENTRAL_AUTH_ROLE_HANDLE_SPECIAL_CHARACTERS_EXCLUDE_HYPHEN,"_")
+                                       .equalsIgnoreCase(description.replaceAll(EcompPortalUtils.EXTERNAL_CENTRAL_AUTH_ROLE_HANDLE_SPECIAL_CHARACTERS_EXCLUDE_HYPHEN,"_"))){
+                                       actualRoleName = description;
+                               }
                        }
                        SortedSet<ExternalAccessPerms> externalAccessPermsOfRole = new TreeSet<>();
                        if (extRole.getJSONObject(i).has(EXTERNAL_AUTH_PERMS)) {
@@ -2881,7 +2913,7 @@ public class ExternalAccessRolesServiceImpl implements ExternalAccessRolesServic
                                                                        roleFunction = dataAccessService.executeNamedQuery("getAppFunctionOnCodeAndAppId",
                                                                                        appFuncsParams, null);
                                                                }
-                                                               if (!roleFunction.isEmpty()) {
+                                                               if (!roleFunction.isEmpty() && roleId != null) {
                                                                        EPAppRoleFunction apRoleFunction = new EPAppRoleFunction();
                                                                        apRoleFunction.setAppId(app.getId());
                                                                        apRoleFunction.setRoleId(roleId);
@@ -2973,7 +3005,7 @@ public class ExternalAccessRolesServiceImpl implements ExternalAccessRolesServic
                params.put("uebKey", app.getUebKey());
                List<BulkUploadUserRoles> userRolesList = null;
                Integer userRolesAdded = 0;
-               if (app.getCentralAuth()) {
+               if (app.getRolesInAAF()) {
                        userRolesList = dataAccessService.executeNamedQuery("getBulkUserRoles", params, null);
                        for (BulkUploadUserRoles userRolesUpload : userRolesList) {
                                if (!userRolesUpload.getOrgUserId().equals("su1234")) {
@@ -3284,7 +3316,7 @@ public class ExternalAccessRolesServiceImpl implements ExternalAccessRolesServic
                                }
                        }
                        if (!found) {
-                               CentralV2Role cenrole = new CentralV2Role();
+                               CentralV2Role cenrole = new CentralV2Role.CentralV2RoleBuilder().createCentralV2Role();
                                cenrole.setName(role.getRoleName());
                                cenrole.setId(role.getRoleId());
                                cenrole.setActive(role.isActive());
@@ -3331,9 +3363,11 @@ public class ExternalAccessRolesServiceImpl implements ExternalAccessRolesServic
        }
 
        private CentralV2Role convertRoleToCentralV2Role(EPRole role) {
-               return new CentralV2Role(role.getId(), role.getCreated(), role.getModified(), role.getCreatedId(),
-                               role.getModifiedId(), role.getRowNum(), role.getName(), role.getActive(), role.getPriority(),
-                               new TreeSet<>(), new TreeSet<>(), new TreeSet<>());
+        return new CentralV2Role.CentralV2RoleBuilder().setId(role.getId()).setCreated(role.getCreated())
+                .setModified(role.getModified()).setCreatedId(role.getCreatedId()).setModifiedId(role.getModifiedId())
+                .setRowNum(role.getRowNum()).setName(role.getName()).setActive(role.getActive())
+                .setPriority(role.getPriority()).setRoleFunctions(new TreeSet<>()).setChildRoles(new TreeSet<>())
+                .setParentRoles(new TreeSet<>()).createCentralV2Role();
        }
 
        @Override
@@ -3568,7 +3602,7 @@ public class ExternalAccessRolesServiceImpl implements ExternalAccessRolesServic
                params.put("roleId", String.valueOf(roleId));
                List<BulkUploadUserRoles> userRolesList = null;
                Integer userRolesAdded = 0;
-               if (app.getCentralAuth()) {
+               if (app.getRolesInAAF()) {
                        userRolesList = dataAccessService.executeNamedQuery("getBulkUsersForSingleRole", params, null);
                        for (BulkUploadUserRoles userRolesUpload : userRolesList) {
                                userRolesUpload.setRoleName(modifiedRoleName);