@Override public EndpointProfileDto getEndpointProfileByKeyHash(String endpointProfileKeyHash) throws ControlServiceException { return endpointService.findEndpointProfileByKeyHash( Base64.decodeBase64(endpointProfileKeyHash) ); }
@Override public EndpointProfileDto findEndpointProfileByEndpointKeyHash(String endpointKeyHash) { byte[] hash = Base64.decodeBase64(endpointKeyHash); EndpointProfileDto endpointProfile = endpointService.findEndpointProfileByKeyHash(hash); return endpointProfile; }
@Override public EndpointProfileDto getProfile(EndpointObjectHash endpointKey) { return endpointService.findEndpointProfileByKeyHash(endpointKey.getData()); }
private EndpointProfileDto getEndpointProfileDto(byte[] endpointKeyHash) { validateNotNull(endpointKeyHash, "Endpoint key hash is required"); return endpointService.findEndpointProfileByKeyHash(endpointKeyHash); }
@Override public PublicKey compute(EndpointObjectHash key) { LOG.debug("Fetching result for getEndpointKey"); PublicKey result = null; EndpointProfileDto endpointProfile = endpointService.findEndpointProfileByKeyHash( key.getData()); if (endpointProfile != null) { try { X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec( endpointProfile.getEndpointKey()); KeyFactory keyFact = KeyFactory.getInstance(ALGORITHM); result = keyFact.generatePublic(x509KeySpec); } catch (NoSuchAlgorithmException | InvalidKeySpecException ex) { LOG.error("failed to decode key", ex); } } else { LOG.error("failed to find key by hash {}", key); } return result; } });
private EndpointProfileDto updateProfile( EndpointProfileDto update, BiFunction<EndpointProfileDto, EndpointProfileDto, EndpointProfileDto> mergeFunction, int retryCount) { LOG.debug("Updating profile {} ", update); try { return endpointService.saveEndpointProfile(update); } catch (KaaOptimisticLockingFailureException ex) { LOG.warn("Failed to update profile {} ", update, ex); if (retryCount > 0) { EndpointProfileDto stored = endpointService.findEndpointProfileByKeyHash( update.getEndpointKeyHash()); LOG.warn("Going to merge it with stored profile {}", stored); EndpointProfileDto merged = mergeFunction.apply(stored, update); LOG.warn("Merge result: {}", merged); return updateProfile(merged, mergeFunction, retryCount - 1); } else { throw ex; } } }
@Test public void testShouldNotFindActiveConfigurationByEndpointKeyHashWhenProfileNotFound() { when(endpointServiceMock.findEndpointProfileByKeyHash(KEY)).thenReturn(null); Assert.assertFalse(SERVICE.findActiveConfigurationByEndpointKeyHash(KEY).isPresent()); verify(endpointServiceMock).findEndpointProfileByKeyHash(KEY); }
@Test public void testShouldNotDeleteActiveConfigurationByEndpointKeyHashWhenProfileNotFound() { when(endpointServiceMock.findEndpointProfileByKeyHash(KEY)).thenReturn(null); Assert.assertFalse(SERVICE.deleteActiveConfigurationByEndpointKeyHash(KEY).isPresent()); verify(endpointServiceMock).findEndpointProfileByKeyHash(KEY); }
@Test public void testShouldDeleteActiveConfigurationByEndpointKeyHash() { when(endpointServiceMock.findEndpointProfileByKeyHash(KEY)).thenReturn(generateProfile()); when(daoMock.findByEndpointKeyHashAndConfigurationVersion(KEY, CONF_VERSION)).thenReturn(configuration); when(configuration.toDto()).thenReturn(configurationDto); when(configurationDto.getConfigurationSchemaVersion()).thenReturn(CONF_VERSION); Assert.assertTrue(SERVICE.deleteActiveConfigurationByEndpointKeyHash(KEY).isPresent()); verify(endpointServiceMock).findEndpointProfileByKeyHash(KEY); verify(daoMock).findByEndpointKeyHashAndConfigurationVersion(KEY, CONF_VERSION); verify(daoMock).removeByEndpointKeyHashAndConfigurationVersion(KEY, CONF_VERSION); }
EndpointProfileDto checkEndpointProfile(byte[] endpointKeyHash) throws KaaAdminServiceException { try { validateNotNull(endpointKeyHash, "Missing endpoint key hash"); EndpointProfileDto endpointProfile = endpointService.findEndpointProfileByKeyHash(endpointKeyHash); checkNotNull(endpointProfile); checkApplicationId(endpointProfile.getApplicationId()); return endpointProfile; } catch (Exception ex) { throw Utils.handleException(ex); } }
@Test public void testShouldFindActiveConfigurationByEndpointKeyHash() { EndpointSpecificConfigurationDto dto = new EndpointSpecificConfigurationDto(); dto.setConfigurationSchemaVersion(CONF_VERSION); when(endpointServiceMock.findEndpointProfileByKeyHash(KEY)).thenReturn(generateProfile()); when(daoMock.findByEndpointKeyHashAndConfigurationVersion(KEY, CONF_VERSION)).thenReturn(configuration); when(configuration.toDto()).thenReturn(dto); Assert.assertTrue(SERVICE.findActiveConfigurationByEndpointKeyHash(KEY).isPresent()); verify(endpointServiceMock).findEndpointProfileByKeyHash(KEY); verify(daoMock).findByEndpointKeyHashAndConfigurationVersion(KEY, CONF_VERSION); }
@Override public EndpointProfileDto updateProfile(ClientSyncMetaData metaData, EndpointObjectHash keyHash, boolean useConfigurationRawSchema) { LOG.debug("Updating Profile for {}", keyHash); EndpointProfileDto dto = endpointService.findEndpointProfileByKeyHash(keyHash.getData()); AppSeqNumber appSeqNumber = cacheService.getAppSeqNumber(metaData.getApplicationToken()); SdkProfileDto sdkProfile = cacheService.getSdkProfileBySdkToken(metaData.getSdkToken()); Function<EndpointProfileDto, EndpointProfileDto> updateFunction = profile -> { populateVersionStates(appSeqNumber.getTenantId(), profile, sdkProfile); profile.setGroupState(new ArrayList<>()); profile.setUseConfigurationRawSchema(useConfigurationRawSchema); profile.setSequenceNumber(0); return profile; }; return updateProfile(updateFunction.apply(dto), (storedProfile, newProfile) -> { return updateFunction.apply(storedProfile); }); }
@Test public void testShouldDeleteByEndpointKeyHashAndConfSchemaVersion() { EndpointProfileDto profile = generateProfile(); profile.setConfigurationVersion(0); when(endpointServiceMock.findEndpointProfileByKeyHash(KEY)).thenReturn(profile); when(daoMock.findByEndpointKeyHashAndConfigurationVersion(KEY, CONF_VERSION)).thenReturn(configuration); when(configuration.toDto()).thenReturn(configurationDto); when(configurationServiceMock.findConfSchemaByAppIdAndVersion(APP_ID, CONF_VERSION)).thenReturn(new ConfigurationSchemaDto()); when(configurationDto.getConfigurationSchemaVersion()).thenReturn(CONF_VERSION); Assert.assertTrue(SERVICE.deleteByEndpointKeyHashAndConfSchemaVersion(KEY, CONF_VERSION).isPresent()); verify(endpointServiceMock).findEndpointProfileByKeyHash(KEY); verify(daoMock).findByEndpointKeyHashAndConfigurationVersion(KEY, CONF_VERSION); verify(daoMock).removeByEndpointKeyHashAndConfigurationVersion(KEY, CONF_VERSION); verify(configurationServiceMock).findConfSchemaByAppIdAndVersion(APP_ID, CONF_VERSION); }
@Test public void testShouldSaveWithActiveConfigSchemaVersion() { EndpointSpecificConfigurationDto dto = new EndpointSpecificConfigurationDto(); dto.setConfigurationSchemaVersion(null); dto.setEndpointKeyHash(KEY); dto.setConfiguration(CONFIG_BODY); when(endpointServiceMock.findEndpointProfileByKeyHash(KEY)).thenReturn(generateProfile()); when(configurationServiceMock.normalizeAccordingToOverrideConfigurationSchema(APP_ID, CONF_VERSION, CONFIG_BODY)).thenReturn("valid body"); when(daoMock.save(dto)).thenReturn(configuration); when(configuration.toDto()).thenReturn(new EndpointSpecificConfigurationDto()); Assert.assertTrue(SERVICE.save(dto) != null); verify(configurationServiceMock).normalizeAccordingToOverrideConfigurationSchema(APP_ID, CONF_VERSION, CONFIG_BODY); verify(endpointServiceMock).findEndpointProfileByKeyHash(KEY); verify(daoMock).save(dto); }
@Test public void testShouldSaveWithProvidedConfigSchemaVersion() { EndpointSpecificConfigurationDto dto = new EndpointSpecificConfigurationDto(); dto.setConfigurationSchemaVersion(7); dto.setEndpointKeyHash(KEY); dto.setConfiguration(CONFIG_BODY); when(endpointServiceMock.findEndpointProfileByKeyHash(KEY)).thenReturn(generateProfile()); when(configurationServiceMock.normalizeAccordingToOverrideConfigurationSchema(APP_ID, 7, CONFIG_BODY)).thenReturn("valid body"); when(daoMock.save(dto)).thenReturn(configuration); when(configuration.toDto()).thenReturn(new EndpointSpecificConfigurationDto()); Assert.assertTrue(SERVICE.save(dto) != null); verify(configurationServiceMock).normalizeAccordingToOverrideConfigurationSchema(APP_ID, 7, CONFIG_BODY); verify(endpointServiceMock).findEndpointProfileByKeyHash(KEY); verify(daoMock).save(dto); }
private void sendEndpointConfigurationRefreshMessage(EndpointSpecificConfigurationDto configuration) { byte[] endpointKeyHashBytes = configuration.getEndpointKeyHash(); EndpointProfileDto endpointProfile = endpointService.findEndpointProfileByKeyHash(endpointKeyHashBytes); if (!configuration.getConfigurationSchemaVersion().equals(endpointProfile.getConfigurationVersion())) { return; } checkNeighbors(); String endpointKeyHash = Base64Util.encode(configuration.getEndpointKeyHash()); ApplicationDto appDto = applicationService.findAppById(endpointProfile.getApplicationId()); OperationsNodeInfo server = resolve(endpointKeyHash); if (server != null) { ThriftEndpointConfigurationRefreshMessage msg = new ThriftEndpointConfigurationRefreshMessage(); msg.setAddress(new ThriftEntityAddress(appDto.getTenantId(), appDto.getApplicationToken(), ThriftClusterEntityType.ENDPOINT, ByteBuffer.wrap(endpointKeyHashBytes))); msg.setActorClassifier(ThriftActorClassifier.GLOBAL); if (LOG.isTraceEnabled()) { LOG.trace("Sending message {} to [{}]", msg, Neighbors.getServerId(server.getConnectionInfo())); } neighbors.sendMessage(server.getConnectionInfo(), OperationsServiceMsg.fromEndpointConfigurationRefresh(msg)); } else { LOG.warn("Can't find server for endpoint [{}]", endpointKeyHash); } }
@Test public void findEndpointProfileByKeyHashTest() { String endpointGroupId = "124"; EndpointProfileDto savedEndpointProfileDto = generateEndpointProfileWithGroupIdDto(endpointGroupId); EndpointProfileDto endpointProfileDto = endpointService.findEndpointProfileByKeyHash(savedEndpointProfileDto.getEndpointKeyHash()); Assert.assertEquals(savedEndpointProfileDto, endpointProfileDto); }
response.setResult(SyncStatus.SUCCESS); } else { EndpointProfileDto detachEndpoint = endpointService.findEndpointProfileByKeyHash( endpointKeyHash); if (detachEndpoint != null) {
@Override public EndpointProfileDto updateProfile(UpdateProfileRequest request) { LOG.debug("Updating Profile for {}", request.getEndpointKeyHash()); EndpointProfileDto dto = endpointService.findEndpointProfileByKeyHash( request.getEndpointKeyHash().getData()); AppSeqNumber appSeqNumber = cacheService.getAppSeqNumber(request.getApplicationToken()); SdkProfileDto sdkProfile = cacheService.getSdkProfileBySdkToken(request.getSdkToken()); String profileJson = decodeProfile( request.getProfile(), appSeqNumber.getAppToken(), sdkProfile.getProfileSchemaVersion()); Function<EndpointProfileDto, EndpointProfileDto> updateFunction = profile -> { if (request.getAccessToken() != null) { profile.setAccessToken(request.getAccessToken()); } profile.setClientProfileBody(profileJson); profile.setProfileHash(EndpointObjectHash.fromSha1(request.getProfile()).getData()); populateVersionStates(appSeqNumber.getTenantId(), profile, sdkProfile); profile.setGroupState(new ArrayList<>()); profile.setSequenceNumber(0); return profile; }; return updateProfile(updateFunction.apply(dto), (storedProfile, newProfile) -> { return updateFunction.apply(storedProfile); }); }
@Test @Ignore("generateEndpointProfileDtoWithSchemaVersion() updates profile," + " but it shouldn't have existed by the moment, so save must have have been invoked") public void testFindServerProfileSchemaByKeyHash() { ServerProfileSchemaDto schemaDto = generateServerProfileSchema(null, null); EndpointProfileDto ep = generateEndpointProfileDtoWithSchemaVersion(schemaDto.getApplicationId(), schemaDto.getVersion(), null); EndpointProfileDto found = endpointService.findEndpointProfileByKeyHash(ep.getEndpointKeyHash()); ServerProfileSchemaDto foundSchema = serverProfileService.findServerProfileSchemaByAppIdAndVersion(found.getApplicationId(), found.getServerProfileVersion()); Assert.assertEquals(schemaDto, foundSchema); }