/** * All-args constructor. */ public static List<ProfileFilterRecordDto> convertToProfileFilterRecords( Collection<ProfileFilterDto> profileFilters) { Map<ProfileVersionPairDto, ProfileFilterRecordDto> profileFiltterRecordsMap = new HashMap<>(); for (ProfileFilterDto profileFilter : profileFilters) { ProfileVersionPairDto versionPair = new ProfileVersionPairDto( profileFilter.getEndpointProfileSchemaId(), profileFilter.getEndpointProfileSchemaVersion(), profileFilter.getServerProfileSchemaId(), profileFilter.getServerProfileSchemaVersion()); ProfileFilterRecordDto profileFilterRecord = profileFiltterRecordsMap.get(versionPair); if (profileFilterRecord == null) { profileFilterRecord = new ProfileFilterRecordDto(); profileFiltterRecordsMap.put(versionPair, profileFilterRecord); } if (profileFilter.getStatus() == UpdateStatus.ACTIVE) { profileFilterRecord.setActiveStructureDto(profileFilter); } else if (profileFilter.getStatus() == UpdateStatus.INACTIVE) { profileFilterRecord.setInactiveStructureDto(profileFilter); } } return new ArrayList<>(profileFiltterRecordsMap.values()); }
@Override protected ProfileFilterDto createDto() { return new ProfileFilterDto(); }
@Override public ProfileFilterDto editProfileFilter(ProfileFilterDto profileFilter) throws KaaAdminServiceException { checkAuthority(KaaAuthorityDto.TENANT_DEVELOPER, KaaAuthorityDto.TENANT_USER); try { String username = getCurrentUser().getUsername(); if (isEmpty(profileFilter.getId())) { profileFilter.setCreatedUsername(username); checkEndpointGroupId(profileFilter.getEndpointGroupId()); } else { profileFilter.setModifiedUsername(username); ProfileFilterDto storedProfileFilter = controlService.getProfileFilter( profileFilter.getId()); Utils.checkNotNull(storedProfileFilter); checkEndpointGroupId(storedProfileFilter.getEndpointGroupId()); } validateProfileFilterBody(profileFilter.getEndpointProfileSchemaId(), profileFilter.getServerProfileSchemaId(), profileFilter.getBody()); return controlService.editProfileFilter(profileFilter); } catch (Exception ex) { throw Utils.handleException(ex); } }
private boolean supports(ProfileFilterDto profileFilter, Integer endpointProfileSchemaVersion, Integer serverProfileSchemaVersion) { return (profileFilter.getEndpointProfileSchemaVersion() == null || profileFilter.getEndpointProfileSchemaVersion() == endpointProfileSchemaVersion) && (profileFilter.getServerProfileSchemaVersion() == null || profileFilter.getServerProfileSchemaVersion() == serverProfileSchemaVersion); } });
private boolean checkFilter(FilterEvaluator filterEvaluator, ProfileFilterDto filter) { try { if (filterEvaluator.matches(filter)) { LOG.trace("profile body matched"); return true; } } catch (EvaluationException ex) { LOG.warn("Failed to process filter [{}]: {} due to evaluate exception. " + "Please check your filter body", filter.getId(), filter.getBody(), ex); } catch (Exception ex) { LOG.error("Failed to process filter [{}]: {} due to exception", filter.getId(), filter.getBody(), ex); } return false; }
private void validateFilter(ProfileFilterDto dto) { if (dto == null) { throw new IncorrectParameterException("Can't save profile filter. Incorrect object."); } if (dto.getEndpointProfileSchemaId() == null && dto.getServerProfileSchemaId() == null) { throw new IncorrectParameterException( "Profile Filter object invalid. Both schemas are empty."); } if (StringUtils.isBlank(dto.getEndpointGroupId())) { throw new IncorrectParameterException( "Profile Filter object invalid. Endpoint Group id invalid:" + dto.getEndpointGroupId()); } } }
@Override public ProfileFilterDto toDto() { ProfileFilterDto filterDto = super.toDto(); filterDto.setBody(body); if (endpointProfileSchema != null) { filterDto.setEndpointProfileSchemaId(endpointProfileSchema.getStringId()); filterDto.setEndpointProfileSchemaVersion(endpointProfileSchema.getVersion()); } if (serverProfileSchema != null) { filterDto.setServerProfileSchemaId(serverProfileSchema.getStringId()); filterDto.setServerProfileSchemaVersion(serverProfileSchema.getVersion()); } return filterDto; }
@Override public ProfileFilterDto saveProfileFilter(ProfileFilterDto profileFilterDto) { validateFilter(profileFilterDto); String id = profileFilterDto.getId(); if (isNotBlank(id)) { ProfileFilterDto oldProfileFilter = findProfileFilterById(id); if (oldProfileFilter != null && oldProfileFilter.getStatus() != INACTIVE) { throw new UpdateStatusConflictException("Can't update profile filter, " + "invalid old profile filter with id " + id); String endProfSchemaId = profileFilterDto.getEndpointProfileSchemaId(); String srvProfSchemaId = profileFilterDto.getServerProfileSchemaId(); String groupId = profileFilterDto.getEndpointGroupId(); EndpointGroup group = endpointGroupDao.findById(groupId); if (group.getWeight() == 0) { srvProfSchemaId, groupId); if (inactiveFilter != null) { profileFilterDto.setId(inactiveFilter.getId().toString()); profileFilterDto.setSequenceNumber(inactiveFilter.getSequenceNumber()); } else if (latestFilter != null) { profileFilterDto.setSequenceNumber(latestFilter.getSequenceNumber()); profileFilterDto.setApplicationId(endpointProfileSchemaDto.getApplicationId()); } else { profileFilterDto.setApplicationId(serverProfileSchemaDto.getApplicationId()); profileFilterDto.setCreatedTime(System.currentTimeMillis()); } else { throw new IncorrectParameterException("Can't update profile filter, "
ProfileFilterDto dto = new ProfileFilterDto(); dto.setId(null); dto.setStatus(null); if (isBlank(groupId)) { groupId = generateEndpointGroupDto(schemaDto.getApplicationId()).getId(); dto.setEndpointGroupId(groupId); dto.setEndpointProfileSchemaId(schemaDto.getId()); dto.setEndpointProfileSchemaVersion(schemaDto.getVersion()); dto.setServerProfileSchemaId(serverProfileSchemaDto.getId()); dto.setServerProfileSchemaVersion(serverProfileSchemaDto.getVersion()); dto.setApplicationId(schemaDto.getApplicationId()); ProfileFilterDto saved = profileService.saveProfileFilter(dto); Assert.assertNotNull(saved); if (activate) { ChangeProfileFilterNotification notification = profileService.activateProfileFilter(saved.getId(), schemaDto.getCreatedUsername()); saved = notification.getProfileFilterDto();
@Test(expected = IncorrectParameterException.class) public void saveProfileFilterWithIncorrectSchemaIdTest() { ApplicationDto app = generateApplicationDto(null); String appId = app.getId(); ProfileFilterDto filter = new ProfileFilterDto(); filter.setId(null); filter.setApplicationId(appId); filter.setBody("true"); filter.setEndpointProfileSchemaId(null); filter.setServerProfileSchemaId(null); profileService.saveProfileFilter(filter); }
@Test public void deactivateProfileFilterTest() { List<ProfileFilterDto> filterList = generateFilterDto(null, null, null, 1, true); ProfileFilterDto pf = filterList.get(0); String appId = pf.getApplicationId(); List<ProfileFilterDto> filters = profileService.findProfileFiltersByAppIdAndVersionsCombination(pf.getApplicationId(), pf.getEndpointProfileSchemaVersion(), pf.getServerProfileSchemaVersion()); Assert.assertNotNull(filters); ProfileFilterDto filter = filters.get(0); filter.setId(null); EndpointGroupDto group = generateEndpointGroupDto(appId); filter.setEndpointGroupId(group.getId()); ProfileFilterDto dto = profileService.saveProfileFilter(filter); ChangeProfileFilterNotification change = profileService.activateProfileFilter(dto.getId(), null); Assert.assertNotNull(change); Assert.assertEquals(UpdateStatus.ACTIVE, change.getProfileFilterDto().getStatus()); change = profileService.deactivateProfileFilter(dto.getId(), null); Assert.assertNotNull(change); Assert.assertEquals(UpdateStatus.DEPRECATED, change.getProfileFilterDto().getStatus()); }
@Test(expected = UpdateStatusConflictException.class) public void updateDefaultProfileFilter() { ApplicationDto app = generateApplicationDto(null); EndpointProfileSchemaDto schema = generateProfSchemaDto(app.getTenantId(), app.getId(), 1).get(0); ServerProfileSchemaDto serverSchema = generateServerProfileSchema(app.getId(), app.getTenantId()); EndpointGroupDto defaultGroup = endpointService.findDefaultGroup(app.getId()); ProfileFilterDto filter = new ProfileFilterDto(); filter.setApplicationId(app.getId()); filter.setBody("true"); filter.setEndpointProfileSchemaId(schema.getId()); filter.setServerProfileSchemaId(serverSchema.getId()); filter.setEndpointGroupId(defaultGroup.getId()); filter = profileService.saveProfileFilter(filter); profileService.activateProfileFilter(filter.getId(), "test"); }
@Test public void saveFiltersWithDiferentGroupsTest() { ApplicationDto application = generateApplicationDto(null); EndpointGroupDto groupDto = generateEndpointGroupDto(application.getId()); EndpointGroupDto groupDto2 = generateEndpointGroupDto(application.getId(), "OTHER_GROUP"); EndpointProfileSchemaDto schema = generateProfSchemaDto(application.getTenantId(), application.getId(), 1).get(0); ServerProfileSchemaDto serverSchema = generateServerProfileSchema(application.getId(), application.getTenantId()); ProfileFilterDto filterDto = generateFilterDto(schema.getId(), serverSchema.getId(), groupDto.getId(), 1, false).get(0); Assert.assertNotNull(filterDto); filterDto.setId(null); filterDto.setEndpointGroupId(groupDto.getId()); ProfileFilterDto savedOne = profileService.saveProfileFilter(filterDto); Assert.assertNotNull(savedOne); ChangeProfileFilterNotification activeOne = profileService.activateProfileFilter(savedOne.getId(), null); Assert.assertNotNull(activeOne); filterDto.setId(null); filterDto.setEndpointGroupId(groupDto2.getId()); ProfileFilterDto savedTwo = profileService.saveProfileFilter(filterDto); Assert.assertNotNull(savedTwo); ChangeProfileFilterNotification activeTwo = profileService.activateProfileFilter(savedTwo.getId(), null); Assert.assertNotNull(activeTwo); List<ProfileFilterDto> activeList = profileService.findProfileFiltersByAppIdAndVersionsCombination(application.getId(), schema.getVersion(), serverSchema.getVersion()); Assert.assertFalse(activeList.isEmpty()); Set<String> groupIds = new HashSet<>(); for (ProfileFilterDto dto : activeList) { Assert.assertEquals(dto.getStatus(), UpdateStatus.ACTIVE); groupIds.add(dto.getEndpointGroupId()); } Assert.assertEquals(2, groupIds.size()); }
@Test public void saveMoreFiltersTest() { List<EndpointProfileSchemaDto> schemas = generateProfSchemaDto(null, null, 1); EndpointProfileSchemaDto schema = schemas.get(0); ApplicationDto app = applicationService.findAppById(schema.getApplicationId()); ServerProfileSchemaDto serverSchema = generateServerProfileSchema(app.getId(), app.getTenantId(), 1); generateFilterDto(schema.getId(), serverSchema.getId(), null, 10, true); List<ProfileFilterDto> filters = profileService.findProfileFiltersByAppIdAndVersionsCombination(schema.getApplicationId(), schema.getVersion(), serverSchema.getVersion()); Assert.assertFalse(filters.isEmpty()); Assert.assertEquals(filters.get(0).getStatus(), UpdateStatus.ACTIVE); Assert.assertEquals(schema.getId(), filters.get(0).getEndpointProfileSchemaId()); Assert.assertEquals(serverSchema.getId(), filters.get(0).getServerProfileSchemaId()); }
@Test public void saveFilterObjectWithoutInactiveFilterTest() { ProfileFilterDto filterDto = generateFilterDto(null, null, null, 1, true).get(0); filterDto.setId(null); ProfileFilterDto saved = profileService.saveProfileFilter(filterDto); Assert.assertNotNull(saved); Assert.assertEquals(saved.getStatus(), UpdateStatus.INACTIVE); Assert.assertEquals(saved.getSequenceNumber(), filterDto.getSequenceNumber()); Assert.assertNotEquals(saved.getId(), filterDto.getId()); }
/** * Create new instance of <code>ProfileFilter</code>. * @param dto data transfer object contain body, * endpoint profile schema id, server profile * schema id. Assign these values on corresponding * fields of new instance */ public ProfileFilter(ProfileFilterDto dto) { super(dto); this.body = dto.getBody(); String endpointSchemaId = dto.getEndpointProfileSchemaId(); if (isNotBlank(endpointSchemaId)) { this.endpointProfileSchema = new EndpointProfileSchema(getLongId(endpointSchemaId)); } String serverSchemaId = dto.getServerProfileSchemaId(); if (isNotBlank(serverSchemaId)) { this.serverProfileSchema = new ServerProfileSchema(getLongId(serverSchemaId)); } }
@Test public void saveFilterObjectWithoutIdTest() { List<ProfileFilterDto> filters = generateFilterDto(null, null, null, 1, false); ProfileFilterDto filterDto = filters.get(0); String inactiveId = filterDto.getId(); filterDto.setId(null); ProfileFilterDto saved = profileService.saveProfileFilter(filterDto); Assert.assertNotNull(saved); Assert.assertEquals(saved.getStatus(), UpdateStatus.INACTIVE); Assert.assertEquals(inactiveId, saved.getId()); }
@Test public void activateProfileFilterTest() { List<ProfileFilterDto> filters = generateFilterDto(null, null, null, 1, false); ProfileFilterDto filterDto = filters.get(0); Assert.assertNotNull(filterDto); Assert.assertEquals(UpdateStatus.INACTIVE, filterDto.getStatus()); ChangeProfileFilterNotification activated = profileService.activateProfileFilter(filterDto.getId(), null); Assert.assertNotNull(activated); ProfileFilterDto profileFilterDto = activated.getProfileFilterDto(); Assert.assertNotNull(profileFilterDto); Assert.assertEquals(UpdateStatus.ACTIVE, profileFilterDto.getStatus()); Assert.assertNotEquals(filterDto.getSequenceNumber(), profileFilterDto.getSequenceNumber()); }
private HistoryDto addHistory(ProfileFilterDto dto, ChangeType type) { LOG.debug("Add history information about profile filter update with change type {} ", type); HistoryDto history = new HistoryDto(); history.setApplicationId(dto.getApplicationId()); ChangeDto change = new ChangeDto(); change.setProfileFilterId(dto.getId()); change.setProfileFilterId(dto.getId()); change.setEndpointGroupId(dto.getEndpointGroupId()); change.setType(type); history.setChange(change); return historyService.saveHistory(history); }
@Override public boolean matches(ProfileFilterDto filter) { final Expression expression = new SpelExpressionParser().parseExpression(filter.getBody()); StandardEvaluationContext evaluationContext; if (filter.getEndpointProfileSchemaVersion() != null) { evaluationContext = new StandardEvaluationContext(clientProfileGenericRecord); evaluationContext.setVariable(CLIENT_PROFILE_VARIABLE_NAME, clientProfileGenericRecord); } else { evaluationContext = new StandardEvaluationContext(); } evaluationContext.addPropertyAccessor(new GenericRecordPropertyAccessor()); evaluationContext.setVariable(EP_KEYHASH_VARIABLE_NAME, epKey); if (filter.getServerProfileSchemaVersion() != null) { evaluationContext.setVariable(SERVER_PROFILE_VARIABLE_NAME, serverProfileGenericRecord); } return expression.getValue(evaluationContext, Boolean.class); }