/
EndpointProfileCassandraDao.java
600 lines (548 loc) · 33.5 KB
/
EndpointProfileCassandraDao.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
/*
* Copyright 2014-2016 CyberVision, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.kaaproject.kaa.server.common.nosql.cassandra.dao;
import static com.datastax.driver.core.querybuilder.QueryBuilder.delete;
import static com.datastax.driver.core.querybuilder.QueryBuilder.eq;
import static com.datastax.driver.core.querybuilder.QueryBuilder.in;
import static com.datastax.driver.core.querybuilder.QueryBuilder.select;
import static com.datastax.driver.core.querybuilder.QueryBuilder.set;
import static org.kaaproject.kaa.server.common.dao.impl.DaoUtil.getDto;
import static org.kaaproject.kaa.server.common.nosql.cassandra.dao.CassandraDaoUtil.convertKeyHashToString;
import static org.kaaproject.kaa.server.common.nosql.cassandra.dao.CassandraDaoUtil.convertStringToKeyHash;
import static org.kaaproject.kaa.server.common.nosql.cassandra.dao.CassandraDaoUtil.getByteBuffer;
import static org.kaaproject.kaa.server.common.nosql.cassandra.dao.CassandraDaoUtil.getBytes;
import static org.kaaproject.kaa.server.common.nosql.cassandra.dao.model.CassandraModelConstants.ENDPOINT_GROUP_STATE_CONFIGURATION_ID_PROPERTY;
import static org.kaaproject.kaa.server.common.nosql.cassandra.dao.model.CassandraModelConstants.ENDPOINT_GROUP_STATE_ENDPOINT_GROUP_ID_PROPERTY;
import static org.kaaproject.kaa.server.common.nosql.cassandra.dao.model.CassandraModelConstants.ENDPOINT_GROUP_STATE_PROFILE_FILTER_ID_PROPERTY;
import static org.kaaproject.kaa.server.common.nosql.cassandra.dao.model.CassandraModelConstants.ENDPOINT_GROUP_STATE_USER_TYPE_NAME;
import static org.kaaproject.kaa.server.common.nosql.cassandra.dao.model.CassandraModelConstants.EP_ACCESS_TOKEN_PROPERTY;
import static org.kaaproject.kaa.server.common.nosql.cassandra.dao.model.CassandraModelConstants.EP_APP_ID_PROPERTY;
import static org.kaaproject.kaa.server.common.nosql.cassandra.dao.model.CassandraModelConstants.EP_BY_APP_ID_APPLICATION_ID_PROPERTY;
import static org.kaaproject.kaa.server.common.nosql.cassandra.dao.model.CassandraModelConstants.EP_BY_APP_ID_COLUMN_FAMILY_NAME;
import static org.kaaproject.kaa.server.common.nosql.cassandra.dao.model.CassandraModelConstants.EP_BY_APP_ID_ENDPOINT_KEY_HASH_PROPERTY;
import static org.kaaproject.kaa.server.common.nosql.cassandra.dao.model.CassandraModelConstants.EP_BY_ENDPOINT_GROUP_ID_COLUMN_FAMILY_NAME;
import static org.kaaproject.kaa.server.common.nosql.cassandra.dao.model.CassandraModelConstants.EP_BY_ENDPOINT_GROUP_ID_ENDPOINT_GROUP_ID_PROPERTY;
import static org.kaaproject.kaa.server.common.nosql.cassandra.dao.model.CassandraModelConstants.EP_BY_ENDPOINT_GROUP_ID_ENDPOINT_KEY_HASH_PROPERTY;
import static org.kaaproject.kaa.server.common.nosql.cassandra.dao.model.CassandraModelConstants.EP_BY_SDK_TOKEN_COLUMN_FAMILY_NAME;
import static org.kaaproject.kaa.server.common.nosql.cassandra.dao.model.CassandraModelConstants.EP_BY_SDK_TOKEN_SDK_TOKEN_PROPERTY;
import static org.kaaproject.kaa.server.common.nosql.cassandra.dao.model.CassandraModelConstants.EP_COLUMN_FAMILY_NAME;
import static org.kaaproject.kaa.server.common.nosql.cassandra.dao.model.CassandraModelConstants.EP_CONFIGURATION_VERSION_PROPERTY;
import static org.kaaproject.kaa.server.common.nosql.cassandra.dao.model.CassandraModelConstants.EP_CONFIG_HASH_PROPERTY;
import static org.kaaproject.kaa.server.common.nosql.cassandra.dao.model.CassandraModelConstants.EP_ECF_VERSION_STATE_PROPERTY;
import static org.kaaproject.kaa.server.common.nosql.cassandra.dao.model.CassandraModelConstants.EP_ENDPOINT_ID_PROPERTY;
import static org.kaaproject.kaa.server.common.nosql.cassandra.dao.model.CassandraModelConstants.EP_EP_KEY_HASH_PROPERTY;
import static org.kaaproject.kaa.server.common.nosql.cassandra.dao.model.CassandraModelConstants.EP_EP_KEY_PROPERTY;
import static org.kaaproject.kaa.server.common.nosql.cassandra.dao.model.CassandraModelConstants.EP_GROUP_STATE_PROPERTY;
import static org.kaaproject.kaa.server.common.nosql.cassandra.dao.model.CassandraModelConstants.EP_LOG_SCHEMA_VERSION_PROPERTY;
import static org.kaaproject.kaa.server.common.nosql.cassandra.dao.model.CassandraModelConstants.EP_NOTIFICATION_VERSION_PROPERTY;
import static org.kaaproject.kaa.server.common.nosql.cassandra.dao.model.CassandraModelConstants.EP_PROFILE_HASH_PROPERTY;
import static org.kaaproject.kaa.server.common.nosql.cassandra.dao.model.CassandraModelConstants.EP_PROFILE_PROPERTY;
import static org.kaaproject.kaa.server.common.nosql.cassandra.dao.model.CassandraModelConstants.EP_PROFILE_VERSION_PROPERTY;
import static org.kaaproject.kaa.server.common.nosql.cassandra.dao.model.CassandraModelConstants.EP_SDK_TOKEN_PROPERTY;
import static org.kaaproject.kaa.server.common.nosql.cassandra.dao.model.CassandraModelConstants.EP_SEQUENCE_NUMBER_PROPERTY;
import static org.kaaproject.kaa.server.common.nosql.cassandra.dao.model.CassandraModelConstants.EP_SERVER_HASH_PROPERTY;
import static org.kaaproject.kaa.server.common.nosql.cassandra.dao.model.CassandraModelConstants.EP_SERVER_PROFILE_PROPERTY;
import static org.kaaproject.kaa.server.common.nosql.cassandra.dao.model.CassandraModelConstants.EP_SERVER_PROFILE_VERSION_PROPERTY;
import static org.kaaproject.kaa.server.common.nosql.cassandra.dao.model.CassandraModelConstants.EP_SIMPLE_TOPIC_HASH_PROPERTY;
import static org.kaaproject.kaa.server.common.nosql.cassandra.dao.model.CassandraModelConstants.EP_SUBSCRIPTIONS_PROPERTY;
import static org.kaaproject.kaa.server.common.nosql.cassandra.dao.model.CassandraModelConstants.EP_SYSTEM_NOTIFICATION_VERSION_PROPERTY;
import static org.kaaproject.kaa.server.common.nosql.cassandra.dao.model.CassandraModelConstants.EP_TOPIC_HASH_PROPERTY;
import static org.kaaproject.kaa.server.common.nosql.cassandra.dao.model.CassandraModelConstants.EP_USER_CONFIG_HASH_PROPERTY;
import static org.kaaproject.kaa.server.common.nosql.cassandra.dao.model.CassandraModelConstants.EP_USER_ID_PROPERTY;
import static org.kaaproject.kaa.server.common.nosql.cassandra.dao.model.CassandraModelConstants.EP_USER_NOTIFICATION_VERSION_PROPERTY;
import static org.kaaproject.kaa.server.common.nosql.cassandra.dao.model.CassandraModelConstants.EVENT_CLASS_FAMILY_VERSION_STATE_ECF_ID_PROPERTY;
import static org.kaaproject.kaa.server.common.nosql.cassandra.dao.model.CassandraModelConstants.EVENT_CLASS_FAMILY_VERSION_STATE_ECF_VERSION_PROPERTY;
import static org.kaaproject.kaa.server.common.nosql.cassandra.dao.model.CassandraModelConstants.EVENT_CLASS_FAMILY_VERSION_STATE_USER_TYPE_NAME;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import org.apache.commons.codec.binary.Base64;
import org.kaaproject.kaa.common.dto.EndpointProfileBodyDto;
import org.kaaproject.kaa.common.dto.EndpointProfileDto;
import org.kaaproject.kaa.common.dto.EndpointProfilesBodyDto;
import org.kaaproject.kaa.common.dto.EndpointProfilesPageDto;
import org.kaaproject.kaa.common.dto.PageLinkDto;
import org.kaaproject.kaa.server.common.dao.DaoConstants;
import org.kaaproject.kaa.server.common.dao.exception.DatabaseProcessingException;
import org.kaaproject.kaa.server.common.dao.exception.KaaOptimisticLockingFailureException;
import org.kaaproject.kaa.server.common.dao.impl.EndpointProfileDao;
import org.kaaproject.kaa.server.common.nosql.cassandra.dao.filter.CassandraEPByAccessTokenDao;
import org.kaaproject.kaa.server.common.nosql.cassandra.dao.filter.CassandraEPByAppIdDao;
import org.kaaproject.kaa.server.common.nosql.cassandra.dao.filter.CassandraEPByEndpointGroupIdDao;
import org.kaaproject.kaa.server.common.nosql.cassandra.dao.filter.CassandraEPBySdkTokenDao;
import org.kaaproject.kaa.server.common.nosql.cassandra.dao.model.CassandraEPByAccessToken;
import org.kaaproject.kaa.server.common.nosql.cassandra.dao.model.CassandraEPByAppId;
import org.kaaproject.kaa.server.common.nosql.cassandra.dao.model.CassandraEPByEndpointGroupId;
import org.kaaproject.kaa.server.common.nosql.cassandra.dao.model.CassandraEPBySdkToken;
import org.kaaproject.kaa.server.common.nosql.cassandra.dao.model.CassandraEndpointProfile;
import org.kaaproject.kaa.server.common.nosql.cassandra.dao.model.CassandraEndpointUser;
import org.kaaproject.kaa.server.common.nosql.cassandra.dao.model.type.CassandraEndpointGroupState;
import org.kaaproject.kaa.server.common.nosql.cassandra.dao.model.type.CassandraEventClassFamilyVersionState;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.datastax.driver.core.ConsistencyLevel;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Row;
import com.datastax.driver.core.Statement;
import com.datastax.driver.core.UDTValue;
import com.datastax.driver.core.UserType;
import com.datastax.driver.core.querybuilder.Assignment;
import com.datastax.driver.core.querybuilder.QueryBuilder;
import com.google.common.base.Predicates;
import com.google.common.collect.Sets;
@Repository(value = "endpointProfileDao")
public class EndpointProfileCassandraDao extends AbstractVersionableCassandraDao<CassandraEndpointProfile, ByteBuffer> implements EndpointProfileDao<CassandraEndpointProfile> {
private static final Logger LOG = LoggerFactory.getLogger(EndpointProfileCassandraDao.class);
@Autowired
private CassandraEPByAppIdDao cassandraEPByAppIdDao;
@Autowired
private CassandraEPByAccessTokenDao cassandraEPByAccessTokenDao;
@Autowired
private CassandraEPBySdkTokenDao cassandraEPBySdkTokenDao;
@Autowired
private CassandraEPByEndpointGroupIdDao cassandraEPByEndpointGroupIdDao;
private EndpointUserCassandraDao endpointUserDao;
@Override
protected Class<CassandraEndpointProfile> getColumnFamilyClass() {
return CassandraEndpointProfile.class;
}
@Override
protected String getColumnFamilyName() {
return EP_COLUMN_FAMILY_NAME;
}
@Override
public CassandraEndpointProfile save(EndpointProfileDto dto) {
CassandraEndpointProfile endpointProfile = new CassandraEndpointProfile(dto);
return save(endpointProfile);
}
@Override
public CassandraEndpointProfile save(CassandraEndpointProfile endpointProfile) {
if (endpointProfile.getVersion() == null) {
return saveProfile(endpointProfile);
} else {
return updateProfile(endpointProfile);
}
}
@Override
protected CassandraEndpointProfile updateLocked(
CassandraEndpointProfile entity) {
return updateLockedImpl(
entity.getVersion(),
new Assignment[] {set(EP_ECF_VERSION_STATE_PROPERTY, convertEventStateToUDT(entity.getEcfVersionStates())),
set(EP_CONFIG_HASH_PROPERTY, entity.getConfigurationHash()),
set(EP_GROUP_STATE_PROPERTY, convertGroupStateToUDT(entity.getGroupStates())),
set(EP_SEQUENCE_NUMBER_PROPERTY, entity.getSequenceNumber()),
set(EP_SERVER_PROFILE_VERSION_PROPERTY, entity.getServerProfileVersion()),
set(EP_SYSTEM_NOTIFICATION_VERSION_PROPERTY, entity.getSystemNfVersion()),
set(EP_NOTIFICATION_VERSION_PROPERTY, entity.getNotificationVersion()),
set(EP_PROFILE_VERSION_PROPERTY, entity.getProfileVersion()),
set(EP_USER_CONFIG_HASH_PROPERTY, entity.getUserConfigurationHash()),
set(EP_SERVER_HASH_PROPERTY, entity.getServerHash()),
set(EP_USER_ID_PROPERTY, entity.getEndpointUserId()),
set(EP_USER_NOTIFICATION_VERSION_PROPERTY, entity.getUserNfVersion()),
set(EP_CONFIGURATION_VERSION_PROPERTY, entity.getConfigurationVersion()),
set(EP_ACCESS_TOKEN_PROPERTY, entity.getAccessToken()),
set(EP_PROFILE_PROPERTY, entity.getProfile()),
set(EP_SERVER_PROFILE_PROPERTY, entity.getServerProfile()),
set(EP_SUBSCRIPTIONS_PROPERTY, entity.getSubscriptions()),
set(EP_LOG_SCHEMA_VERSION_PROPERTY, entity.getLogSchemaVersion()),
set(EP_APP_ID_PROPERTY, entity.getApplicationId()),
set(EP_EP_KEY_PROPERTY, entity.getEndpointProfileKey()),
set(EP_PROFILE_HASH_PROPERTY, entity.getProfileHash()),
set(EP_TOPIC_HASH_PROPERTY, entity.getTopicHash()),
set(EP_SIMPLE_TOPIC_HASH_PROPERTY, entity.getSimpleTopicHash()),
set(EP_SDK_TOKEN_PROPERTY, entity.getSdkToken()),
set(EP_ENDPOINT_ID_PROPERTY, entity.getId())},
eq(EP_EP_KEY_HASH_PROPERTY, entity.getEndpointKeyHash())
);
}
private CassandraEndpointProfile saveProfile(CassandraEndpointProfile profile) {
CassandraEndpointProfile storedProfile = findByKeyHash(getBytes(profile.getEndpointKeyHash()));
profile.setId(convertKeyHashToString(profile.getEndpointKeyHash()));
if (storedProfile != null) {
LOG.error("[{}] Profile is already exists. Can't save endpoint profile.", profile.getId());
throw new DatabaseProcessingException("Profile is already exists. Can't save endpoint profile.");
}
profile.setVersion(0l);
LOG.debug("Saving endpoint profile with id {}", profile.getId());
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 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);
if (storedProfile != null) {
profile = super.save(profile);
List<Statement> statementList = new ArrayList<>();
Set<String> oldEndpointGroupIds = getEndpointProfilesGroupIdSet(storedProfile);
Set<String> newEndpointGroupIds = getEndpointProfilesGroupIdSet(profile);
Set<String> removeEndpointGroupIds = Sets.filter(oldEndpointGroupIds, Predicates.not(Predicates.in(newEndpointGroupIds)));
Set<String> addEndpointGroupIds = Sets.filter(newEndpointGroupIds, Predicates.not(Predicates.in(oldEndpointGroupIds)));
if (addEndpointGroupIds != null) {
for (String id : addEndpointGroupIds) {
statementList.add(cassandraEPByEndpointGroupIdDao.getSaveQuery(new CassandraEPByEndpointGroupId(id, epKeyHash)));
}
if (removeEndpointGroupIds != null) {
for (String id : removeEndpointGroupIds) {
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, epKeyHash)));
}
}
executeBatch(statementList.toArray(new Statement[statementList.size()]));
} else {
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());
}
if (accessToken != null) {
statementList.add(cassandraEPByAccessTokenDao.getSaveQuery(new CassandraEPByAccessToken(accessToken, epKeyHash)));
}
executeBatch(statementList.toArray(new Statement[statementList.size()]));
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.");
}
return profile;
}
@Override
public CassandraEndpointProfile findByKeyHash(byte[] endpointKeyHash) {
LOG.debug("Try to find endpoint profile by key hash [{}]", endpointKeyHash);
CassandraEndpointProfile endpointProfile = (CassandraEndpointProfile) getMapper().get(getByteBuffer(endpointKeyHash));
LOG.debug("[{}] Found endpoint profile {}", endpointKeyHash, endpointProfile);
return endpointProfile;
}
@Override
public EndpointProfileBodyDto findBodyByKeyHash(byte[] endpointKeyHash) {
LOG.debug("Try to find endpoint profile body by key hash [{}]", endpointKeyHash);
String profile = null;
String appId = null;
ResultSet resultSet = execute(select(EP_PROFILE_PROPERTY, EP_APP_ID_PROPERTY).from(getColumnFamilyName())
.where(eq(EP_EP_KEY_HASH_PROPERTY, getByteBuffer(endpointKeyHash))));
Row row = resultSet.one();
if (row != null) {
profile = row.getString(EP_PROFILE_PROPERTY);
appId = row.getString(EP_APP_ID_PROPERTY);
}
LOG.debug("[{}] Found endpoint profile body {}", endpointKeyHash, profile);
return new EndpointProfileBodyDto(endpointKeyHash, profile, appId);
}
@Override
public CassandraEndpointProfile findEndpointIdByKeyHash(byte[] endpointKeyHash) {
LOG.debug("Try to check if endpoint profile exists with key hash [{}]", 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 [{}]", endpointKeyHash, profile);
return profile;
}
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);
}
@Override
public void removeByKeyHash(byte[] endpointKeyHash) {
LOG.debug("Remove endpoint profile by key hash [{}]", 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));
}
@Override
public void removeByAppId(String appId) {
LOG.debug("Remove endpoint profile by application id [{}]", appId);
Statement deleteEps = delete().from(getColumnFamilyName()).where(in(EP_EP_KEY_HASH_PROPERTY, cassandraEPByAppIdDao.getEPIdsListByAppId(appId)));
ByteBuffer[] epKeyHashList = cassandraEPByAppIdDao.getEPIdsListByAppId(appId);
if (epKeyHashList != null) {
for (ByteBuffer epKeyHash : epKeyHashList) {
removeByKeyHashFromEpByEndpointGroupId(getBytes(epKeyHash));
}
}
Statement deleteEpsByAppId = delete().from(EP_BY_APP_ID_COLUMN_FAMILY_NAME).where(eq(EP_BY_APP_ID_APPLICATION_ID_PROPERTY, appId));
executeBatch(deleteEps, deleteEpsByAppId);
LOG.trace("Execute statements {}, {} like batch", deleteEps, deleteEpsByAppId);
}
@Override
public CassandraEndpointProfile findByAccessToken(String endpointAccessToken) {
LOG.debug("Try to find endpoint profile by access token id [{}]", endpointAccessToken);
CassandraEndpointProfile endpointProfile = null;
ByteBuffer epKeyHash = cassandraEPByAccessTokenDao.findEPIdByAccessToken(endpointAccessToken);
if (epKeyHash != null) {
endpointProfile = (CassandraEndpointProfile) getMapper().get(epKeyHash);
}
LOG.trace("Found endpoint profile {} by access token [{}]", endpointProfile, endpointAccessToken);
return endpointProfile;
}
private List<EndpointProfileDto> findEndpointProfilesList(ByteBuffer[] keyHashList, String endpointGroupId) {
List<EndpointProfileDto> cassandraEndpointProfileList = new ArrayList<>();
LOG.debug("Found {} endpoint profiles by group id {}", keyHashList != null ? keyHashList.length : 0, endpointGroupId);
for (ByteBuffer keyHash : keyHashList) {
CassandraEndpointProfile profile = findByKeyHash(getBytes(keyHash));
if (profile != null) {
cassandraEndpointProfileList.add(getDto(profile));
} else {
LOG.debug("Can't find endpoint profile by id {}", keyHash);
}
}
return cassandraEndpointProfileList;
}
private List<EndpointProfileBodyDto> findEndpointProfilesBodyList(ByteBuffer[] keyHashList, String endpointGroupId) {
List<EndpointProfileBodyDto> endpointProfilesBodyDto = new ArrayList<>();
LOG.debug("Found {} endpoint profiles body by group id {}", keyHashList != null ? keyHashList.length : 0, endpointGroupId);
for (ByteBuffer keyHash : keyHashList) {
EndpointProfileBodyDto endpointProfileBodyDto = findBodyByKeyHash(getBytes(keyHash));
if (endpointProfileBodyDto != null) {
endpointProfilesBodyDto.add(endpointProfileBodyDto);
} else {
LOG.debug("Can't find endpoint profile by id {}", keyHash);
}
}
return endpointProfilesBodyDto;
}
@Override
public EndpointProfilesPageDto findByEndpointGroupId(PageLinkDto pageLink) {
LOG.debug("Try to find endpoint profile by endpoint group id [{}]", pageLink.getEndpointGroupId());
EndpointProfilesPageDto endpointProfilesPageDto;
List<EndpointProfileDto> cassandraEndpointProfileList;
ByteBuffer[] keyHashList;
if (pageLink.getApplicationId() != null) {
keyHashList = cassandraEPByAppIdDao.findEPByAppId(pageLink, pageLink.getApplicationId());
} else {
keyHashList = cassandraEPByEndpointGroupIdDao.findEPByEndpointGroupId(pageLink);
}
cassandraEndpointProfileList = findEndpointProfilesList(keyHashList, pageLink.getEndpointGroupId());
endpointProfilesPageDto = createNextPage(cassandraEndpointProfileList, pageLink.getEndpointGroupId(), pageLink.getLimit());
return endpointProfilesPageDto;
}
@Override
public EndpointProfilesBodyDto findBodyByEndpointGroupId(PageLinkDto pageLink) {
LOG.debug("Try to find endpoint profile body by endpoint group id [{}]", pageLink.getEndpointGroupId());
EndpointProfilesBodyDto endpointProfilesBodyDto;
List<EndpointProfileBodyDto> profilesBodyDto;
ByteBuffer[] keyHashList;
if (pageLink.getApplicationId() != null) {
keyHashList = cassandraEPByAppIdDao.findEPByAppId(pageLink, pageLink.getApplicationId());
} else {
keyHashList = cassandraEPByEndpointGroupIdDao.findEPByEndpointGroupId(pageLink);
}
profilesBodyDto = findEndpointProfilesBodyList(keyHashList, pageLink.getEndpointGroupId());
endpointProfilesBodyDto = createNextBodyPage(profilesBodyDto, pageLink.getEndpointGroupId(), pageLink.getLimit());
return endpointProfilesBodyDto;
}
@Override
public List<CassandraEndpointProfile> findByEndpointUserId(String endpointUserId) {
LOG.debug("Try to find endpoint profiles by endpoint user id [{}]", endpointUserId);
List<CassandraEndpointProfile> profileList = Collections.emptyList();
CassandraEndpointUser endpointUser = endpointUserDao.findById(endpointUserId);
if (endpointUser != null) {
List<String> ids = endpointUser.getEndpointIds();
if (ids != null && !ids.isEmpty()) {
Statement select = select().from(getColumnFamilyName()).where(in(EP_EP_KEY_HASH_PROPERTY, convertStringIds(ids)));
LOG.trace("Execute statements {}", select);
profileList = findListByStatement(select);
}
}
if (LOG.isTraceEnabled()) {
LOG.trace("Found endpoint profiles {}", Arrays.toString(profileList.toArray()));
}
return profileList;
}
@Override
public CassandraEndpointProfile findById(ByteBuffer key) {
LOG.debug("Try to find endpoint profiles by key [{}]", key);
CassandraEndpointProfile profile = null;
if (key != null) {
profile = findByKeyHash(key.array());
}
LOG.trace("Found endpoint profiles {}", profile);
return profile;
}
@Override
public void removeById(ByteBuffer key) {
LOG.debug("Remove endpoint profiles by key [{}]", key);
if (key != null) {
removeByKeyHash(key.array());
}
}
@Override
public boolean checkSdkToken(String sdkToken) {
LOG.debug("Checking for endpoint profiles with SDK token {}", sdkToken);
Statement query = select().from(EP_BY_SDK_TOKEN_COLUMN_FAMILY_NAME)
.where(eq(EP_BY_SDK_TOKEN_SDK_TOKEN_PROPERTY, sdkToken));
return execute(query).one() != null;
}
@Override
public CassandraEndpointProfile updateServerProfile(byte[] keyHash, int version, String serverProfile) {
LOG.debug("Updating server profile for endpoint profile with key hash [{}] with schema version [{}]", keyHash, version);
ByteBuffer key = ByteBuffer.wrap(keyHash);
Statement update = QueryBuilder.update(EP_COLUMN_FAMILY_NAME)
.with(set(EP_SERVER_PROFILE_PROPERTY, serverProfile))
.and(set(EP_SERVER_PROFILE_VERSION_PROPERTY, version))
.where(eq(EP_EP_KEY_HASH_PROPERTY, key));
execute(update, ConsistencyLevel.ALL);
return findById(key);
}
private List<UDTValue> convertGroupStateToUDT(List<CassandraEndpointGroupState> cfGroupState) {
List<UDTValue> utList = null;
if (cfGroupState != null && !cfGroupState.isEmpty()) {
UserType ut = getUserType(ENDPOINT_GROUP_STATE_USER_TYPE_NAME);
utList = new ArrayList<>();
for (CassandraEndpointGroupState state : cfGroupState) {
UDTValue udtValue = ut.newValue().setString(ENDPOINT_GROUP_STATE_ENDPOINT_GROUP_ID_PROPERTY, state.getEndpointGroupId())
.setString(ENDPOINT_GROUP_STATE_PROFILE_FILTER_ID_PROPERTY, state.getProfileFilterId())
.setString(ENDPOINT_GROUP_STATE_CONFIGURATION_ID_PROPERTY, state.getConfigurationId());
utList.add(udtValue);
}
}
return utList;
}
private List<UDTValue> convertEventStateToUDT(List<CassandraEventClassFamilyVersionState> eventStates) {
List<UDTValue> utList = null;
if (eventStates != null && !eventStates.isEmpty()) {
UserType ut = getUserType(EVENT_CLASS_FAMILY_VERSION_STATE_USER_TYPE_NAME);
utList = new ArrayList<>();
for (CassandraEventClassFamilyVersionState state : eventStates) {
UDTValue udtValue = ut.newValue().setString(EVENT_CLASS_FAMILY_VERSION_STATE_ECF_ID_PROPERTY, state.getEcfId())
.setInt(EVENT_CLASS_FAMILY_VERSION_STATE_ECF_VERSION_PROPERTY, state.getVersion());
utList.add(udtValue);
}
}
return utList;
}
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 ByteBuffer[] convertStringIds(List<String> ids) {
ByteBuffer[] keyHashArray = new ByteBuffer[ids.size()];
for (int i = 0; i < ids.size(); i++) {
keyHashArray[i] = convertStringToKeyHash(ids.get(i));
}
return keyHashArray;
}
private EndpointProfilesPageDto createNextPage(List<EndpointProfileDto> cassandraEndpointProfileList, String endpointGroupId, String limit) {
EndpointProfilesPageDto endpointProfilesPageDto = new EndpointProfilesPageDto();
PageLinkDto pageLinkDto = new PageLinkDto();
String next;
int lim = Integer.valueOf(limit);
if (cassandraEndpointProfileList.size() == (lim + 1)) {
pageLinkDto.setEndpointGroupId(endpointGroupId);
pageLinkDto.setLimit(limit);
pageLinkDto.setOffset(Base64.encodeBase64URLSafeString(cassandraEndpointProfileList.get(lim).getEndpointKeyHash()));
cassandraEndpointProfileList.remove(lim);
next = null;
} else {
next = DaoConstants.LAST_PAGE_MESSAGE;
}
pageLinkDto.setNext(next);
endpointProfilesPageDto.setPageLinkDto(pageLinkDto);
endpointProfilesPageDto.setEndpointProfiles(cassandraEndpointProfileList);
return endpointProfilesPageDto;
}
private EndpointProfilesBodyDto createNextBodyPage(List<EndpointProfileBodyDto> profilesBodyDto, String endpointGroupId, String limit) {
EndpointProfilesBodyDto endpointProfilesBodyDto = new EndpointProfilesBodyDto();
PageLinkDto pageLinkDto = new PageLinkDto();
String next;
int lim = Integer.valueOf(limit);
if (profilesBodyDto.size() == (lim + 1)) {
pageLinkDto.setEndpointGroupId(endpointGroupId);
pageLinkDto.setLimit(limit);
pageLinkDto.setOffset(Base64.encodeBase64URLSafeString(profilesBodyDto.get(lim).getEndpointKeyHash()));
profilesBodyDto.remove(lim);
next = null;
} else {
next = DaoConstants.LAST_PAGE_MESSAGE;
}
pageLinkDto.setNext(next);
endpointProfilesBodyDto.setPageLinkDto(pageLinkDto);
endpointProfilesBodyDto.setEndpointProfilesBody(profilesBodyDto);
return endpointProfilesBodyDto;
}
public EndpointUserCassandraDao getEndpointUserDao() {
return endpointUserDao;
}
public void setEndpointUserDao(EndpointUserCassandraDao endpointUserDao) {
this.endpointUserDao = endpointUserDao;
}
}