@Override public void run() { try { CassandraEndpointProfile ep = new CassandraEndpointProfile(endpointProfile); ep.setEndpointUserId("Ololo " + id); endpointProfileDao.save(ep.toDto()); } catch (KaaOptimisticLockingFailureException ex) { LOG.error("Catch optimistic exception."); throw ex; } } }));
private CassandraEndpointProfile saveProfile(CassandraEndpointProfile profile) { profile.setId(convertKeyHashToString(profile.getEndpointKeyHash())); LOG.debug("Saving endpoint profile with id {}", profile.getId()); profile = super.save(profile); ByteBuffer epKeyHash = profile.getEndpointKeyHash(); List<Statement> statementList = new ArrayList<>(); statementList.add(cassandraEpByAppIdDao.getSaveQuery( new CassandraEpByAppId(profile.getApplicationId(), epKeyHash))); String accessToken = profile.getAccessToken(); if (accessToken != null) { statementList.add( cassandraEpByAccessTokenDao.getSaveQuery( new CassandraEpByAccessToken(accessToken, epKeyHash))); } statementList.add(getSaveQuery(profile)); Statement saveBySdkTokenId = cassandraEpBySdkTokenDao.getSaveQuery( new CassandraEpBySdkToken(profile.getSdkToken(), epKeyHash)); statementList.add(saveBySdkTokenId); Set<String> groupIdSet = getEndpointProfilesGroupIdSet(profile); for (String groupId : groupIdSet) { statementList.add( cassandraEpByEndpointGroupIdDao.getSaveQuery( new CassandraEpByEndpointGroupId(groupId, epKeyHash))); } executeBatch(statementList.toArray(new Statement[statementList.size()])); LOG.debug("[{}] Endpoint profile saved", profile.getId()); return profile; }
private Set<String> getEndpointProfilesGroupIdSet(CassandraEndpointProfile profile) { Set<String> groupIdSet = new HashSet<>(); List<CassandraEndpointGroupState> groupStateSet = new LinkedList<>(); if (profile != null) { List<CassandraEndpointGroupState> cfGroupState = profile.getGroupStates(); if (cfGroupState != null && !cfGroupState.isEmpty()) { groupStateSet.addAll(cfGroupState); } for (CassandraEndpointGroupState cf : groupStateSet) { groupIdSet.add(cf.getEndpointGroupId()); } } return groupIdSet; }
private CassandraEndpointProfile updateProfile(CassandraEndpointProfile profile) { LOG.debug("Updating endpoint profile with id {}", profile.getId()); ByteBuffer epKeyHash = profile.getEndpointKeyHash(); byte[] keyHash = getBytes(epKeyHash); CassandraEndpointProfile storedProfile = findByKeyHash(keyHash); LOG.error("[{}] Can't update endpoint profile with version {}. " + "Endpoint profile already changed!", profile.getId(), profile.getVersion()); throw new KaaOptimisticLockingFailureException("Can't update endpoint profile with" + " version . Endpoint profile already changed!"); String accessToken = profile.getAccessToken(); if (storedProfile.getAccessToken() != null && !storedProfile.getAccessToken().equals(accessToken)) { cassandraEpByAccessTokenDao.removeById(storedProfile.getAccessToken()); LOG.debug("[{}] Endpoint profile updated", profile.getId()); } else { LOG.error("[{}] Stored profile is null. Can't update endpoint profile.", profile.getId()); throw new DatabaseProcessingException("Stored profile is null. " + "Can't update endpoint profile.");
protected EndpointProfileDto generateEndpointProfileWithEndpointGroupId(String appId) { byte[] keyHash = generateBytes(); if (appId == null) { appId = generateStringId(); } EndpointProfileDto profileDto = new EndpointProfileDto(); profileDto.setApplicationId(appId); profileDto.setEndpointKeyHash(keyHash); profileDto.setAccessToken(generateStringId()); profileDto.setClientProfileBody("test profile"); profileDto.setServerProfileBody("test server-side profile"); profileDto.setEpsConfigurationHash(EPS_CONFIG_HASH); List<EndpointGroupStateDto> groupState = new ArrayList<>(); groupState.add(new EndpointGroupStateDto(TEST_ENDPOINT_GROUP_ID, null, null)); profileDto.setGroupState(groupState); profileDto.setSdkToken(UUID.randomUUID().toString()); return endpointProfileDao.save(new CassandraEndpointProfile(profileDto)).toDto(); }
@Override public CassandraEndpointProfile findEndpointIdByKeyHash(byte[] endpointKeyHash) { LOG.debug("Try to check if endpoint profile exists with key hash [{}]", Utils.encodeHexString(endpointKeyHash)); CassandraEndpointProfile profile = null; ResultSet resultSet = execute(select(EP_ENDPOINT_ID_PROPERTY).from(getColumnFamilyName()) .where(eq(EP_EP_KEY_HASH_PROPERTY, getByteBuffer(endpointKeyHash)))); Row row = resultSet.one(); if (row != null) { profile = new CassandraEndpointProfile(); profile.setId(row.getString(EP_ENDPOINT_ID_PROPERTY)); } LOG.debug("{} endpoint profile exists with key hash [{}]", Utils.encodeHexString(endpointKeyHash), profile); return profile; }
@Override public CassandraEndpointProfile save(EndpointProfileDto dto) { CassandraEndpointProfile endpointProfile = new CassandraEndpointProfile(dto); return save(endpointProfile); }
@Override public CassandraEndpointProfile save(CassandraEndpointProfile object) { if (object.getVersion() == null) { return saveProfile(object); } else { return updateProfile(object); } }
@Override public void removeByKeyHash(byte[] endpointKeyHash) { LOG.debug("Remove endpoint profile by key hash [{}]", Utils.encodeHexString(endpointKeyHash)); CassandraEndpointProfile storedProfile = findByKeyHash(endpointKeyHash); removeByKeyHashFromEpByEndpointGroupId(endpointKeyHash); String appId = storedProfile.getApplicationId(); if (!appId.isEmpty()) { Statement deleteEp = delete() .from(EP_BY_APP_ID_COLUMN_FAMILY_NAME) .where(eq(EP_BY_APP_ID_APPLICATION_ID_PROPERTY, appId)) .and(eq(EP_BY_APP_ID_ENDPOINT_KEY_HASH_PROPERTY, getByteBuffer(endpointKeyHash))); executeBatch(deleteEp); } getMapper().delete(getByteBuffer(endpointKeyHash)); }
protected EndpointProfileDto generateEndpointProfile(String appId, String sdkToken, String accessToken, List<String> topicIds, CtlDataDto ctlDataDto) { byte[] keyHash = generateBytes(); if (appId == null) { appId = generateStringId(); } if (sdkToken == null) { sdkToken = generateStringId(); } if (accessToken == null) { accessToken = generateStringId(); } EndpointProfileDto profileDto = new EndpointProfileDto(); profileDto.setApplicationId(appId); profileDto.setSdkToken(sdkToken); profileDto.setSubscriptions(topicIds); profileDto.setEndpointKeyHash(keyHash); profileDto.setAccessToken(accessToken); profileDto.setEpsConfigurationHash(EPS_CONFIG_HASH); if (ctlDataDto != null) { profileDto.setServerProfileBody(ctlDataDto.getBody()); profileDto.setServerProfileVersion(ctlDataDto.getServerProfileVersion()); } return endpointProfileDao.save(new CassandraEndpointProfile(profileDto)).toDto(); }
private void removeByKeyHashFromEpByEndpointGroupId(byte[] endpointKeyHash) { CassandraEndpointProfile storedProfile = findByKeyHash(endpointKeyHash); List<CassandraEndpointGroupState> cfGroupState = new ArrayList<>(); List<String> endpointGroupIds = new ArrayList<>(); List<Statement> statementList = new ArrayList<>(); if (storedProfile.getGroupStates() != null) { cfGroupState.addAll(storedProfile.getGroupStates()); } if (cfGroupState != null) { for (CassandraEndpointGroupState cf : cfGroupState) { endpointGroupIds.add(cf.getEndpointGroupId()); } } if (endpointGroupIds != null) { for (String id : endpointGroupIds) { statementList.add(delete() .from(EP_BY_ENDPOINT_GROUP_ID_COLUMN_FAMILY_NAME) .where(eq(EP_BY_ENDPOINT_GROUP_ID_ENDPOINT_GROUP_ID_PROPERTY, id)) .and(eq( EP_BY_ENDPOINT_GROUP_ID_ENDPOINT_KEY_HASH_PROPERTY, getByteBuffer(endpointKeyHash)))); } } Statement[] st = new Statement[statementList.size()]; statementList.toArray(st); executeBatch(st); }