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; }
@Override public List<ProfileFilterDto> getAllMatchingFilters(AppProfileVersionsKey key, EndpointProfileDto profile) { String endpointProfileSchemaBody = getEndpointProfileSchemaBody(key); String serverProfileSchemaBody = getServerProfileSchemaBody(key); List<ProfileFilterDto> filters = cacheService.getFilters(key); LOG.trace("Found {} filters by {}", filters.size(), key); List<ProfileFilterDto> matchingFilters = new LinkedList<ProfileFilterDto>(); FilterEvaluator filterEvaluator = null; for (ProfileFilterDto filter : filters) { if (filterEvaluator == null) { filterEvaluator = new DefaultFilterEvaluator(); filterEvaluator.init(profile, endpointProfileSchemaBody, serverProfileSchemaBody); } LOG.trace("matching profile body with filter [{}]: {}", filter.getId(), filter.getBody()); if (checkFilter(filterEvaluator, filter)) { matchingFilters.add(filter); } } return matchingFilters; }
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); }
@Test(expected = UpdateStatusConflictException.class) public void activateProfileFilterActiveStatusTest() { List<ProfileFilterDto> filters = generateFilterDto(null, null, null, 1, true); ProfileFilterDto filterDto = filters.get(0); ChangeProfileFilterNotification activated = profileService.activateProfileFilter(filterDto.getId(), null); }
@Test public void findProfileFilterByIdTest() { List<ProfileFilterDto> filters = generateFilterDto(null, null, null, 1, true); ProfileFilterDto filterDto = profileService.findProfileFilterById(filters.get(0).getId()); Assert.assertNotNull(filterDto); Assert.assertEquals(filters.get(0), filterDto); }
@Override public boolean matches(String appToken, String profileFilterId, EndpointProfileDto profile) { AppProfileVersionsKey key = new AppProfileVersionsKey( appToken, profile.getClientProfileVersion(), profile.getServerProfileVersion()); String endpointProfileSchemaBody = getEndpointProfileSchemaBody(key); String serverProfileSchemaBody = getServerProfileSchemaBody(key); FilterEvaluator filterEvaluator = new DefaultFilterEvaluator(); filterEvaluator.init(profile, endpointProfileSchemaBody, serverProfileSchemaBody); ProfileFilterDto filter = cacheService.getFilter(profileFilterId); LOG.trace("matching profile body with filter [{}]: {}", filter.getId(), filter.getBody()); return checkFilter(filterEvaluator, filter); }
@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()); }
@Override public HistoryDelta getDelta(EndpointProfileDto profile, String applicationToken, int curAppSeqNumber) { String endpointId = Base64Util.encode(profile); ConfigurationIdKey confIdKey = new ConfigurationIdKey( applicationToken, curAppSeqNumber, profile.getConfigurationVersion()); AppProfileVersionsKey appVersionsKey = new AppProfileVersionsKey( applicationToken, profile.getClientProfileVersion(), profile.getServerProfileVersion()); List<ProfileFilterDto> filters = filterService.getAllMatchingFilters(appVersionsKey, profile); LOG.debug("[{}] Found {} matching filters", endpointId, filters.size()); List<EndpointGroupStateDto> result = new ArrayList<>(1 + filters.size()); EndpointGroupDto groupDto = cacheService.getDefaultGroup(applicationToken); EndpointGroupStateDto groupAllState = new EndpointGroupStateDto(); groupAllState.setEndpointGroupId(groupDto.getId()); groupAllState.setConfigurationId(cacheService.getConfIdByKey( confIdKey.copyWithNewEgId(groupDto.getId()))); result.add(groupAllState); for (ProfileFilterDto filter : filters) { String confId = cacheService.getConfIdByKey( confIdKey.copyWithNewEgId(filter.getEndpointGroupId())); EndpointGroupStateDto endpointGroupState = new EndpointGroupStateDto(); endpointGroupState.setEndpointGroupId(filter.getEndpointGroupId()); endpointGroupState.setProfileFilterId(filter.getId()); endpointGroupState.setConfigurationId(confId); result.add(endpointGroupState); } return new HistoryDelta(result, true, true, true); }
@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()); }
@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); } }
@Test public void findAllProfileFilterRecordsByEndpointGroupIdTest() { ApplicationDto app = generateApplicationDto(null); String appId = app.getId(); EndpointGroupDto groupDto = generateEndpointGroupDto(appId); EndpointProfileSchemaDto schema = generateProfSchemaDto(app.getTenantId(), appId, 1).get(0); ServerProfileSchemaDto serverProfileSchema = generateServerProfileSchema(app.getId(), app.getTenantId()); ProfileFilterDto filter = generateFilterDto(schema.getId(), serverProfileSchema.getId(), groupDto.getId(), 3, true).get(2); Assert.assertEquals(UpdateStatus.ACTIVE, filter.getStatus()); profileService.deactivateProfileFilter(filter.getId(), null); List<ProfileFilterRecordDto> records = (List<ProfileFilterRecordDto>) profileService.findAllProfileFilterRecordsByEndpointGroupId(groupDto.getId(), true); Assert.assertNotNull(records); Assert.assertEquals(1, records.size()); Assert.assertEquals(UpdateStatus.DEPRECATED, records.get(0).getActiveStructureDto().getStatus()); }
@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 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()); }
@Override public ProfileFilterDto saveProfileFilter(ProfileFilterDto profileFilterDto) { validateFilter(profileFilterDto); String id = profileFilterDto.getId(); if (isNotBlank(id)) { ProfileFilterDto oldProfileFilter = findProfileFilterById(id);
/** * Notify endpoints. * * @param notification the notification * @param profileFilter the profile filter * @param configuration the configuration */ private void notifyEndpoints(ChangeNotificationDto notification, ProfileFilterDto profileFilter, ConfigurationDto configuration) { Notification thriftNotification = new Notification(); thriftNotification.setAppId(notification.getAppId()); thriftNotification.setAppSeqNumber(notification.getAppSeqNumber()); thriftNotification.setGroupId(notification.getGroupId()); thriftNotification.setGroupSeqNumber(notification.getGroupSeqNumber()); if (profileFilter != null) { thriftNotification.setProfileFilterId(profileFilter.getId()); thriftNotification.setProfileFilterSeqNumber(profileFilter.getSequenceNumber()); } if (configuration != null) { thriftNotification.setConfigurationId(configuration.getId()); thriftNotification.setConfigurationSeqNumber(configuration.getSequenceNumber()); } controlZkService.sendEndpointNotification(thriftNotification); }
Assert.assertNotNull(saved); if (activate) { ChangeProfileFilterNotification notification = profileService.activateProfileFilter(saved.getId(), schemaDto.getCreatedUsername()); saved = notification.getProfileFilterDto();
@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(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"); }