Search in sources :

Example 16 with RequestValidationException

use of org.usermanagement.exception.RequestValidationException in project open-kilda by telstra.

the class FlowService method getFlowById.

/**
 * Flow by flow id.
 *
 * @param flowId
 *            the flow id
 * @return the flow by id
 * @throws AccessDeniedException the access denied exception
 */
public FlowInfo getFlowById(String flowId, boolean controller) throws AccessDeniedException {
    FlowInfo flowInfo = new FlowInfo();
    FlowV2 flow = null;
    try {
        flow = flowsIntegrationService.getFlowById(flowId);
    } catch (InvalidResponseException ex) {
        LOGGER.error("Error occurred while retrieving flows from controller", ex);
        if (controller) {
            throw new InvalidResponseException(ex.getCode(), ex.getResponse());
        }
    }
    Map<String, String> csNames = switchIntegrationService.getSwitchNames();
    if (flow != null) {
        flowInfo = flowConverter.toFlowV2Info(flow, csNames);
    }
    UserInfo userInfo = userService.getLoggedInUserInfo();
    try {
        if (!controller && userInfo.getPermissions().contains(IConstants.Permission.FW_FLOW_INVENTORY)) {
            if (storeService.getLinkStoreConfig().getUrls().size() > 0) {
                InventoryFlow inventoryFlow = flowStoreService.getFlowById(flowId);
                if (flow == null && inventoryFlow.getId() == null) {
                    throw new RequestValidationException("Can not get flow: Flow " + flowId + " not found");
                } else if (flow != null && inventoryFlow.getId() != null) {
                    flowInfo.setState(inventoryFlow.getState());
                    FlowDiscrepancy discrepancy = new FlowDiscrepancy();
                    discrepancy.setControllerDiscrepancy(false);
                    if (flowInfo.getMaximumBandwidth() != (inventoryFlow.getMaximumBandwidth() == null ? 0 : inventoryFlow.getMaximumBandwidth())) {
                        discrepancy.setBandwidth(true);
                        FlowBandwidth flowBandwidth = new FlowBandwidth();
                        flowBandwidth.setControllerBandwidth(flow.getMaximumBandwidth());
                        flowBandwidth.setInventoryBandwidth(inventoryFlow.getMaximumBandwidth());
                        discrepancy.setBandwidthValue(flowBandwidth);
                    }
                    if (("UP".equalsIgnoreCase(flowInfo.getStatus()) && !"ACTIVE".equalsIgnoreCase(inventoryFlow.getState())) || ("DOWN".equalsIgnoreCase(flowInfo.getStatus()) && "ACTIVE".equalsIgnoreCase(inventoryFlow.getState()))) {
                        discrepancy.setStatus(true);
                        FlowState flowState = new FlowState();
                        flowState.setControllerState(flow.getStatus());
                        flowState.setInventoryState(inventoryFlow.getState());
                        discrepancy.setStatusValue(flowState);
                    }
                    flowInfo.setInventoryFlow(true);
                    flowInfo.setDiscrepancy(discrepancy);
                } else if (inventoryFlow.getId() == null && flow != null) {
                    FlowDiscrepancy discrepancy = new FlowDiscrepancy();
                    discrepancy.setInventoryDiscrepancy(true);
                    discrepancy.setControllerDiscrepancy(false);
                    discrepancy.setStatus(true);
                    discrepancy.setBandwidth(true);
                    FlowBandwidth flowBandwidth = new FlowBandwidth();
                    flowBandwidth.setControllerBandwidth(flow.getMaximumBandwidth());
                    flowBandwidth.setInventoryBandwidth(0);
                    discrepancy.setBandwidthValue(flowBandwidth);
                    FlowState flowState = new FlowState();
                    flowState.setControllerState(flow.getStatus());
                    flowState.setInventoryState(null);
                    discrepancy.setStatusValue(flowState);
                    flowInfo.setDiscrepancy(discrepancy);
                } else {
                    flowConverter.toFlowInfo(flowInfo, inventoryFlow, csNames);
                }
            }
        }
        if (flow == null) {
            throw new RequestValidationException("Can not get flow: Flow " + flowId + " not found");
        }
    } catch (Exception ex) {
        LOGGER.error("Error occurred while retrieving flows from store", ex);
        throw new RequestValidationException(ex.getMessage());
    }
    return flowInfo;
}
Also used : FlowInfo(org.openkilda.model.FlowInfo) FlowDiscrepancy(org.openkilda.model.FlowDiscrepancy) FlowState(org.openkilda.model.FlowState) FlowV2(org.openkilda.integration.model.FlowV2) InventoryFlow(org.openkilda.integration.source.store.dto.InventoryFlow) UserInfo(org.usermanagement.model.UserInfo) FlowBandwidth(org.openkilda.model.FlowBandwidth) InvalidResponseException(org.openkilda.integration.exception.InvalidResponseException) RequestValidationException(org.usermanagement.exception.RequestValidationException) InvalidResponseException(org.openkilda.integration.exception.InvalidResponseException) IntegrationException(org.openkilda.integration.exception.IntegrationException) RequestValidationException(org.usermanagement.exception.RequestValidationException) AccessDeniedException(java.nio.file.AccessDeniedException)

Example 17 with RequestValidationException

use of org.usermanagement.exception.RequestValidationException in project open-kilda by telstra.

the class SwitchService method saveOrUpdateSwitchName.

/**
 * Save or update switch name.
 *
 * @param switchId the switch id
 * @param switchName the switch name
 * @return the SwitchInfo
 */
public SwitchInfo saveOrUpdateSwitchName(String switchId, String switchName) {
    String storageType = applicationSettingService.getApplicationSetting(ApplicationSetting.SWITCH_NAME_STORAGE_TYPE);
    if (storageType.equals(IConstants.StorageType.DATABASE_STORAGE.name())) {
        SwitchNameEntity switchNameEntity = switchNameRepository.findBySwitchDpid(switchId);
        if (switchNameEntity == null) {
            switchNameEntity = new SwitchNameEntity();
        }
        switchNameEntity.setSwitchDpid(switchId);
        switchNameEntity.setSwitchName(switchName);
        switchNameEntity.setUpdatedDate(new Date());
        switchNameRepository.save(switchNameEntity);
        SwitchInfo switchInfo = new SwitchInfo();
        switchInfo.setSwitchId(switchId);
        switchInfo.setName(switchName);
        return switchInfo;
    } else {
        throw new RequestValidationException("Storage-Type in Application Settings is not Database, " + "so switch name can not be updated");
    }
}
Also used : SwitchNameEntity(org.openkilda.dao.entity.SwitchNameEntity) SwitchInfo(org.openkilda.model.SwitchInfo) RequestValidationException(org.usermanagement.exception.RequestValidationException) Date(java.util.Date)

Example 18 with RequestValidationException

use of org.usermanagement.exception.RequestValidationException in project open-kilda by telstra.

the class RoleService method deleteRoleById.

/**
 * Delete role by id.
 *
 * @param roleId the role id
 */
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public void deleteRoleById(final Long roleId) {
    RoleEntity roleEntity = roleRepository.findByRoleId(roleId);
    if (ValidatorUtil.isNull(roleEntity)) {
        LOGGER.warn("Role with role id '" + roleId + "' not found. Error: " + messageUtil.getAttributeInvalid("role_id", roleId + ""));
        throw new RequestValidationException(messageUtil.getAttributeInvalid("role_id", roleId + ""));
    }
    Set<UserEntity> userEntityList = userRepository.findByRoles_roleId(roleId);
    if (userEntityList.size() > 0) {
        String users = "";
        for (UserEntity userEntity : userEntityList) {
            users += !"".equals(users) ? "," + userEntity.getName() : userEntity.getName();
        }
        LOGGER.warn("Role with role id '" + roleId + "' not allowed to delete. Error: " + messageUtil.getAttributeDeletionNotAllowed(roleEntity.getName(), users));
        throw new RequestValidationException(messageUtil.getAttributeDeletionNotAllowed(roleEntity.getName(), users));
    }
    roleRepository.delete(roleEntity);
    activityLogger.log(ActivityType.DELETE_ROLE, roleEntity.getName());
    LOGGER.info("Role(roleId: " + roleId + ") deleted successfully.");
}
Also used : RoleEntity(org.usermanagement.dao.entity.RoleEntity) RequestValidationException(org.usermanagement.exception.RequestValidationException) UserEntity(org.usermanagement.dao.entity.UserEntity) Transactional(org.springframework.transaction.annotation.Transactional)

Example 19 with RequestValidationException

use of org.usermanagement.exception.RequestValidationException in project open-kilda by telstra.

the class RoleService method getRolesById.

/**
 * Gets the roles by id.
 *
 * @param roleIds the role ids
 * @return the roles by id
 */
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public Set<RoleEntity> getRolesById(final List<Long> roleIds) {
    Set<RoleEntity> roleEntities = new HashSet<>();
    List<RoleEntity> roleEntityList = roleRepository.findAll();
    for (Long roleId : roleIds) {
        RoleEntity roleEntity = roleEntityList.parallelStream().filter((entity) -> entity.getRoleId().equals(roleId)).findFirst().orElse(null);
        if (!ValidatorUtil.isNull(roleEntity)) {
            roleEntities.add(roleEntity);
        } else {
            LOGGER.warn("Role with role id '" + roleId + "' not found. Error: " + messageUtil.getAttributeNotFound("roles"));
            throw new RequestValidationException(messageUtil.getAttributeNotFound("roles"));
        }
    }
    return roleEntities;
}
Also used : RoleEntity(org.usermanagement.dao.entity.RoleEntity) RequestValidationException(org.usermanagement.exception.RequestValidationException) HashSet(java.util.HashSet) Transactional(org.springframework.transaction.annotation.Transactional)

Example 20 with RequestValidationException

use of org.usermanagement.exception.RequestValidationException in project open-kilda by telstra.

the class RoleService method updateRole.

/**
 * Update role.
 *
 * @param roleId the role id
 * @param role the role
 * @return the role
 */
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public Role updateRole(final Long roleId, final Role role) {
    roleValidator.validateUpdateRole(role, roleId);
    RoleEntity roleEntity = roleRepository.findByRoleId(roleId);
    if (ValidatorUtil.isNull(roleEntity)) {
        LOGGER.warn("Role with role id '" + roleId + "' not found. Error: " + messageUtil.getAttributeInvalid("role_id", roleId + ""));
        throw new RequestValidationException(messageUtil.getAttributeInvalid("role_id", roleId + ""));
    }
    if (role.getPermissionId() != null) {
        roleEntity.getPermissions().clear();
        for (Long permissionId : role.getPermissionId()) {
            PermissionEntity permissionEntity = permissionRepository.findByPermissionId(permissionId);
            if (permissionEntity != null) {
                roleEntity.getPermissions().add(permissionEntity);
            }
        }
    }
    roleEntity = RoleConversionUtil.toUpateRoleEntity(role, roleEntity);
    roleRepository.save(roleEntity);
    activityLogger.log(ActivityType.UPDATE_ROLE, roleEntity.getName());
    LOGGER.info("Role updated successfully (roleId: " + roleId + ")");
    return RoleConversionUtil.toRole(roleEntity, true, false);
}
Also used : RoleEntity(org.usermanagement.dao.entity.RoleEntity) RequestValidationException(org.usermanagement.exception.RequestValidationException) PermissionEntity(org.usermanagement.dao.entity.PermissionEntity) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

RequestValidationException (org.usermanagement.exception.RequestValidationException)25 Transactional (org.springframework.transaction.annotation.Transactional)16 RoleEntity (org.usermanagement.dao.entity.RoleEntity)9 UserEntity (org.usermanagement.dao.entity.UserEntity)7 PermissionEntity (org.usermanagement.dao.entity.PermissionEntity)6 AccessDeniedException (java.nio.file.AccessDeniedException)4 HashMap (java.util.HashMap)3 InvalidOtpException (org.openkilda.exception.InvalidOtpException)3 OtpRequiredException (org.openkilda.exception.OtpRequiredException)3 TwoFaKeyNotSetException (org.openkilda.exception.TwoFaKeyNotSetException)3 UsernameNotFoundException (org.springframework.security.core.userdetails.UsernameNotFoundException)3 UserInfo (org.usermanagement.model.UserInfo)3 Date (java.util.Date)2 HashSet (java.util.HashSet)2 SamlConfigEntity (org.openkilda.saml.dao.entity.SamlConfigEntity)2 OauthConfigEntity (org.openkilda.store.auth.dao.entity.OauthConfigEntity)2 UrlDto (org.openkilda.store.model.UrlDto)2 Role (org.usermanagement.model.Role)2 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1