-
Notifications
You must be signed in to change notification settings - Fork 477
/
DefaultControlService.java
2349 lines (2115 loc) · 93.6 KB
/
DefaultControlService.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
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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.control.service;
import static org.apache.commons.lang.StringUtils.isNotBlank;
import static org.kaaproject.kaa.server.admin.shared.util.Utils.isEmpty;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import javax.annotation.PreDestroy;
import org.apache.avro.Schema;
import org.apache.commons.codec.binary.Base64;
import org.apache.thrift.TException;
import org.kaaproject.avro.ui.shared.Fqn;
import org.kaaproject.kaa.common.avro.GenericAvroConverter;
import org.kaaproject.kaa.common.dto.AbstractSchemaDto;
import org.kaaproject.kaa.common.dto.ApplicationDto;
import org.kaaproject.kaa.common.dto.ChangeConfigurationNotification;
import org.kaaproject.kaa.common.dto.ChangeNotificationDto;
import org.kaaproject.kaa.common.dto.ChangeProfileFilterNotification;
import org.kaaproject.kaa.common.dto.ChangeType;
import org.kaaproject.kaa.common.dto.ConfigurationDto;
import org.kaaproject.kaa.common.dto.ConfigurationRecordDto;
import org.kaaproject.kaa.common.dto.ConfigurationSchemaDto;
import org.kaaproject.kaa.common.dto.EndpointGroupDto;
import org.kaaproject.kaa.common.dto.EndpointNotificationDto;
import org.kaaproject.kaa.common.dto.EndpointProfileBodyDto;
import org.kaaproject.kaa.common.dto.EndpointProfileDto;
import org.kaaproject.kaa.common.dto.EndpointProfileSchemaDto;
import org.kaaproject.kaa.common.dto.EndpointProfilesBodyDto;
import org.kaaproject.kaa.common.dto.EndpointProfilesPageDto;
import org.kaaproject.kaa.common.dto.EndpointUserConfigurationDto;
import org.kaaproject.kaa.common.dto.EndpointUserDto;
import org.kaaproject.kaa.common.dto.HasId;
import org.kaaproject.kaa.common.dto.NotificationDto;
import org.kaaproject.kaa.common.dto.NotificationSchemaDto;
import org.kaaproject.kaa.common.dto.NotificationTypeDto;
import org.kaaproject.kaa.common.dto.PageLinkDto;
import org.kaaproject.kaa.common.dto.ProfileFilterDto;
import org.kaaproject.kaa.common.dto.ProfileFilterRecordDto;
import org.kaaproject.kaa.common.dto.ProfileVersionPairDto;
import org.kaaproject.kaa.common.dto.ServerProfileSchemaDto;
import org.kaaproject.kaa.common.dto.TenantAdminDto;
import org.kaaproject.kaa.common.dto.TenantDto;
import org.kaaproject.kaa.common.dto.TopicDto;
import org.kaaproject.kaa.common.dto.UpdateNotificationDto;
import org.kaaproject.kaa.common.dto.UserDto;
import org.kaaproject.kaa.common.dto.VersionDto;
import org.kaaproject.kaa.common.dto.admin.RecordKey;
import org.kaaproject.kaa.common.dto.admin.RecordKey.RecordFiles;
import org.kaaproject.kaa.common.dto.admin.SdkPlatform;
import org.kaaproject.kaa.common.dto.admin.SdkProfileDto;
import org.kaaproject.kaa.common.dto.credentials.CredentialsDto;
import org.kaaproject.kaa.common.dto.credentials.CredentialsStatus;
import org.kaaproject.kaa.common.dto.credentials.EndpointRegistrationDto;
import org.kaaproject.kaa.common.dto.ctl.CTLSchemaDto;
import org.kaaproject.kaa.common.dto.ctl.CTLSchemaMetaInfoDto;
import org.kaaproject.kaa.common.dto.event.AefMapInfoDto;
import org.kaaproject.kaa.common.dto.event.ApplicationEventFamilyMapDto;
import org.kaaproject.kaa.common.dto.event.EcfInfoDto;
import org.kaaproject.kaa.common.dto.event.EventClassDto;
import org.kaaproject.kaa.common.dto.event.EventClassFamilyDto;
import org.kaaproject.kaa.common.dto.event.EventClassType;
import org.kaaproject.kaa.common.dto.event.EventSchemaVersionDto;
import org.kaaproject.kaa.common.dto.file.FileData;
import org.kaaproject.kaa.common.dto.logs.LogAppenderDto;
import org.kaaproject.kaa.common.dto.logs.LogSchemaDto;
import org.kaaproject.kaa.common.dto.user.UserVerifierDto;
import org.kaaproject.kaa.common.hash.EndpointObjectHash;
import org.kaaproject.kaa.server.common.Base64Util;
import org.kaaproject.kaa.server.common.Version;
import org.kaaproject.kaa.server.common.core.schema.DataSchema;
import org.kaaproject.kaa.server.common.core.schema.ProtocolSchema;
import org.kaaproject.kaa.server.common.dao.ApplicationEventMapService;
import org.kaaproject.kaa.server.common.dao.ApplicationService;
import org.kaaproject.kaa.server.common.dao.CTLService;
import org.kaaproject.kaa.server.common.dao.ConfigurationService;
import org.kaaproject.kaa.server.common.dao.EndpointRegistrationService;
import org.kaaproject.kaa.server.common.dao.EndpointService;
import org.kaaproject.kaa.server.common.dao.EventClassService;
import org.kaaproject.kaa.server.common.dao.LogAppendersService;
import org.kaaproject.kaa.server.common.dao.LogSchemaService;
import org.kaaproject.kaa.server.common.dao.NotificationService;
import org.kaaproject.kaa.server.common.dao.ProfileService;
import org.kaaproject.kaa.server.common.dao.SdkProfileService;
import org.kaaproject.kaa.server.common.dao.ServerProfileService;
import org.kaaproject.kaa.server.common.dao.TopicService;
import org.kaaproject.kaa.server.common.dao.UserConfigurationService;
import org.kaaproject.kaa.server.common.dao.UserService;
import org.kaaproject.kaa.server.common.dao.UserVerifierService;
import org.kaaproject.kaa.server.common.dao.exception.CredentialsServiceException;
import org.kaaproject.kaa.server.common.dao.exception.EndpointRegistrationServiceException;
import org.kaaproject.kaa.server.common.dao.exception.IncorrectParameterException;
import org.kaaproject.kaa.server.common.dao.exception.NotFoundException;
import org.kaaproject.kaa.server.common.log.shared.RecordWrapperSchemaGenerator;
import org.kaaproject.kaa.server.common.thrift.KaaThriftService;
import org.kaaproject.kaa.server.common.thrift.gen.operations.Notification;
import org.kaaproject.kaa.server.common.thrift.gen.operations.Operation;
import org.kaaproject.kaa.server.common.thrift.gen.operations.OperationsThriftService.Iface;
import org.kaaproject.kaa.server.common.thrift.gen.operations.ThriftActorClassifier;
import org.kaaproject.kaa.server.common.thrift.gen.operations.ThriftClusterEntityType;
import org.kaaproject.kaa.server.common.thrift.gen.operations.ThriftEndpointDeregistrationMessage;
import org.kaaproject.kaa.server.common.thrift.gen.operations.ThriftEntityAddress;
import org.kaaproject.kaa.server.common.thrift.gen.operations.ThriftServerProfileUpdateMessage;
import org.kaaproject.kaa.server.common.thrift.gen.operations.ThriftUnicastNotificationMessage;
import org.kaaproject.kaa.server.common.thrift.gen.operations.UserConfigurationUpdate;
import org.kaaproject.kaa.server.common.zk.control.ControlNode;
import org.kaaproject.kaa.server.common.zk.gen.OperationsNodeInfo;
import org.kaaproject.kaa.server.common.zk.operations.OperationsNodeListener;
import org.kaaproject.kaa.server.control.service.exception.ControlServiceException;
import org.kaaproject.kaa.server.control.service.schema.SchemaLibraryGenerator;
import org.kaaproject.kaa.server.control.service.sdk.SdkGenerator;
import org.kaaproject.kaa.server.control.service.sdk.SdkGeneratorFactory;
import org.kaaproject.kaa.server.control.service.sdk.event.EventFamilyMetadata;
import org.kaaproject.kaa.server.control.service.zk.ControlZkService;
import org.kaaproject.kaa.server.hash.ConsistentHashResolver;
import org.kaaproject.kaa.server.node.service.credentials.CredentialsServiceLocator;
import org.kaaproject.kaa.server.node.service.credentials.CredentialsServiceRegistry;
import org.kaaproject.kaa.server.node.service.thrift.OperationsServiceMsg;
import org.kaaproject.kaa.server.resolve.OperationsServerResolver;
import org.kaaproject.kaa.server.thrift.NeighborTemplate;
import org.kaaproject.kaa.server.thrift.Neighbors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.helpers.MessageFormatter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.Base64Utils;
/**
* The Class DefaultControlService.
*/
@Service
public class DefaultControlService implements ControlService {
/** The Constant DEFAULT_NEIGHBOR_CONNECTIONS_SIZE. */
private static final int DEFAULT_NEIGHBOR_CONNECTIONS_SIZE = 10;
/** The Constant DEFAULT_USER_HASH_PARTITIONS_SIZE. */
private static final int DEFAULT_USER_HASH_PARTITIONS_SIZE = 10;
/** The Constant LOG. */
private static final Logger LOG = LoggerFactory.getLogger(DefaultControlService.class);
/** The Constant SCHEMA_NAME_PATTERN. */
private static final String SCHEMA_NAME_PATTERN = "kaa-record-schema-l{}.avsc";
/** The Constant DATA_NAME_PATTERN. */
private static final String DATA_NAME_PATTERN = "kaa-{}-schema-v{}.avsc";
/** The Constant LOG_SCHEMA_LIBRARY_NAME_PATTERN. */
private static final String LOG_SCHEMA_LIBRARY_NAME_PATTERN = "kaa-record-lib-l{}";
/**
* A template for naming exported CTL library schemas.
*
* @see #exportCTLSchemaFlatAsLibrary(CTLSchemaDto)
*/
private static final String CTL_LIBRARY_EXPORT_TEMPLATE = "{0}.v{1}";
/** The user service. */
@Autowired
private UserService userService;
/** The application service. */
@Autowired
private ApplicationService applicationService;
/** The configuration service. */
@Autowired
private ConfigurationService configurationService;
/** The user configuration service. */
@Autowired
private UserConfigurationService userConfigurationService;
/** The profile service. */
@Autowired
private ProfileService profileService;
/** The server profile service. */
@Autowired
private ServerProfileService serverProfileService;
/** The endpoint service. */
@Autowired
private EndpointService endpointService;
/** The notification service. */
@Autowired
private NotificationService notificationService;
/** The topic service. */
@Autowired
private TopicService topicService;
/** The event class service. */
@Autowired
private EventClassService eventClassService;
/** The application event map service. */
@Autowired
private ApplicationEventMapService applicationEventMapService;
/** The control zk service. */
@Autowired
private ControlZkService controlZKService;
/** The log schema service. */
@Autowired
private LogSchemaService logSchemaService;
/** The log appender service. */
@Autowired
private LogAppendersService logAppenderService;
/** The user verifier service. */
@Autowired
private UserVerifierService userVerifierService;
/** The sdk key service. */
@Autowired
private SdkProfileService sdkProfileService;
@Autowired
private CTLService ctlService;
@Autowired
@Qualifier("rootCredentialsServiceLocator")
private CredentialsServiceLocator credentialsServiceLocator;
@Autowired
private CredentialsServiceRegistry credentialsServiceRegistry;
@Autowired
private EndpointRegistrationService endpointRegistrationService;
/** The neighbor connections size. */
@Value("#{properties[max_number_neighbor_connections]}")
private int neighborConnectionsSize = DEFAULT_NEIGHBOR_CONNECTIONS_SIZE;
/** The user hash partitions. */
@Value("#{properties[user_hash_partitions]}")
private int userHashPartitions = DEFAULT_USER_HASH_PARTITIONS_SIZE;
/** The neighbors. */
private volatile Neighbors<NeighborTemplate<OperationsServiceMsg>, OperationsServiceMsg> neighbors;
/** The resolver. */
private volatile OperationsServerResolver resolver;
/** The zk lock. */
private Object zkLock = new Object();
/*
* (non-Javadoc)
*
* @see
* org.kaaproject.kaa.server.control.service.ControlService#getTenants()
*/
@Override
public List<TenantDto> getTenants() throws ControlServiceException {
return userService.findAllTenants();
}
/*
* (non-Javadoc)
*
* @see
* org.kaaproject.kaa.server.control.service.ControlService#getTenant(java
* .lang.String)
*/
@Override
public TenantDto getTenant(String tenantId) throws ControlServiceException {
return userService.findTenantById(tenantId);
}
/*
* (non-Javadoc)
*
* @see
* org.kaaproject.kaa.server.control.service.ControlService#editTenant(org
* .kaaproject.kaa.common.dto.TenantDto)
*/
@Override
public TenantDto editTenant(TenantDto tenant) throws ControlServiceException {
return userService.saveTenant(tenant);
}
/*
* (non-Javadoc)
*
* @see
* org.kaaproject.kaa.server.control.service.ControlService#deleteTenant
* (java.lang.String)
*/
@Override
public void deleteTenant(String tenantId) throws ControlServiceException {
userService.removeTenantById(tenantId);
}
/*
* (non-Javadoc)
*
* @see org.kaaproject.kaa.server.control.service.ControlService#getUsers()
*/
@Override
public List<UserDto> getUsers() throws ControlServiceException {
return userService.findAllUsers();
}
/*
* (non-Javadoc)
*
* @see
* org.kaaproject.kaa.server.control.service.ControlService#getTenantUsers
* (java.lang.String)
*/
@Override
public List<UserDto> getTenantUsers(String tenantId) throws ControlServiceException {
return userService.findAllTenantUsers(tenantId);
}
/*
* (non-Javadoc)
*
* @see
* org.kaaproject.kaa.server.control.service.ControlService#getUser(java
* .lang.String)
*/
@Override
public UserDto getUser(String userId) throws ControlServiceException {
return userService.findUserById(userId);
}
/*
* (non-Javadoc)
*
* @see org.kaaproject.kaa.server.control.service.ControlService#
* getUserByExternalUid (java.lang.String)
*/
@Override
public UserDto getUserByExternalUid(String uid) throws ControlServiceException {
return userService.findUserByExternalUid(uid);
}
/*
* (non-Javadoc)
*
* @see
* org.kaaproject.kaa.server.control.service.ControlService#editUser(org
* .kaaproject.kaa.common.dto.UserDto)
*/
@Override
public UserDto editUser(UserDto user) throws ControlServiceException {
return userService.saveUser(user);
}
/*
* (non-Javadoc)
*
* @see
* org.kaaproject.kaa.server.control.service.ControlService#deleteUser(java
* .lang.String)
*/
@Override
public void deleteUser(String userId) throws ControlServiceException {
userService.removeUserById(userId);
}
/*
* (non-Javadoc)
*
* @see
* org.kaaproject.kaa.server.control.service.ControlService#getTenantAdmins
* ()
*/
@Override
public List<TenantAdminDto> getTenantAdmins() throws ControlServiceException {
return userService.findAllTenantAdmins();
}
/*
* (non-Javadoc)
*
* @see
* org.kaaproject.kaa.server.control.service.ControlService#getTenantAdmin
* (java.lang.String)
*/
@Override
public TenantAdminDto getTenantAdmin(String tenantId) throws ControlServiceException {
return userService.findTenantAdminById(tenantId);
}
/*
* (non-Javadoc)
*
* @see
* org.kaaproject.kaa.server.control.service.ControlService#editTenantAdmin
* (org.kaaproject.kaa.common.dto.TenantAdminDto)
*/
@Override
public TenantAdminDto editTenantAdmin(TenantAdminDto tenantAdmin) throws ControlServiceException {
return userService.saveTenantAdmin(tenantAdmin);
}
/*
* (non-Javadoc)
*
* @see org.kaaproject.kaa.server.control.service.ControlService#
* deleteTenantAdmin (java.lang.String)
*/
@Override
public void deleteTenantAdmin(String tenantId) throws ControlServiceException {
userService.removeTenantAdminById(tenantId);
}
/*
* (non-Javadoc)
*
* @see
* org.kaaproject.kaa.server.control.service.ControlService#getApplication
* (java.lang.String)
*/
@Override
public ApplicationDto getApplication(String applicationId) throws ControlServiceException {
return applicationService.findAppById(applicationId);
}
/*
* (non-Javadoc)
*
* @see org.kaaproject.kaa.server.control.service.ControlService#
* getApplicationByApplicationToken(java.lang.String)
*/
@Override
public ApplicationDto getApplicationByApplicationToken(String applicationToken) throws ControlServiceException {
return applicationService.findAppByApplicationToken(applicationToken);
}
/*
* (non-Javadoc)
*
* @see org.kaaproject.kaa.server.control.service.ControlService#
* getApplicationsByTenantId(java.lang.String)
*/
@Override
public List<ApplicationDto> getApplicationsByTenantId(String tenantId) throws ControlServiceException {
return applicationService.findAppsByTenantId(tenantId);
}
/*
* (non-Javadoc)
*
* @see
* org.kaaproject.kaa.server.control.service.ControlService#editApplication
* (org.kaaproject.kaa.common.dto.ApplicationDto)
*/
@Override
public ApplicationDto editApplication(ApplicationDto application) throws ControlServiceException {
boolean update = !isEmpty(application.getId());
ApplicationDto appDto = applicationService.saveApp(application);
if (update) {
LOG.info("[{}] Broadcasting notification about application {} update.", application.getId(), application.getApplicationToken());
Notification thriftNotification = new Notification();
thriftNotification.setAppId(appDto.getId());
thriftNotification.setAppSeqNumber(appDto.getSequenceNumber());
thriftNotification.setOp(Operation.APP_UPDATE);
controlZKService.sendEndpointNotification(thriftNotification);
}
return appDto;
}
/*
* (non-Javadoc)
*
* @see org.kaaproject.kaa.server.control.service.ControlService#
* deleteApplication (java.lang.String)
*/
@Override
public void deleteApplication(String applicationId) throws ControlServiceException {
applicationService.removeAppById(applicationId);
}
/*
* (non-Javadoc)
*
* @see org.kaaproject.kaa.server.control.service.ControlService#
* getConfigurationSchemasByApplicationId(java.lang.String)
*/
@Override
public List<ConfigurationSchemaDto> getConfigurationSchemasByApplicationId(String applicationId) throws ControlServiceException {
return configurationService.findConfSchemasByAppId(applicationId);
}
/*
* (non-Javadoc)
*
* @see org.kaaproject.kaa.server.control.service.ControlService#
* getConfigurationSchema(java.lang.String)
*/
@Override
public ConfigurationSchemaDto getConfigurationSchema(String configurationSchemaId) throws ControlServiceException {
return configurationService.findConfSchemaById(configurationSchemaId);
}
/*
* (non-Javadoc)
*
* @see org.kaaproject.kaa.server.control.service.ControlService#
* saveConfigurationSchema
* (org.kaaproject.kaa.common.dto.ConfigurationSchemaDto)
*/
@Override
public ConfigurationSchemaDto editConfigurationSchema(ConfigurationSchemaDto configurationSchema) throws ControlServiceException {
ConfigurationSchemaDto confSchema = null;
try {
confSchema = configurationService.saveConfSchema(configurationSchema);
} catch (IncorrectParameterException e) {
LOG.error("Can't generate protocol schema. Can't save configuration schema.");
throw new ControlServiceException(e);
}
return confSchema;
}
/*
* (non-Javadoc)
*
* @see org.kaaproject.kaa.server.control.service.ControlService#
* getProfileSchemasByApplicationId(java.lang.String)
*/
@Override
public List<EndpointProfileSchemaDto> getProfileSchemasByApplicationId(String applicationId) throws ControlServiceException {
return profileService.findProfileSchemasByAppId(applicationId);
}
/*
* (non-Javadoc)
*
* @see
* org.kaaproject.kaa.server.control.service.ControlService#getProfileSchema
* (java.lang.String)
*/
@Override
public EndpointProfileSchemaDto getProfileSchema(String profileSchemaId) throws ControlServiceException {
return profileService.findProfileSchemaById(profileSchemaId);
}
/*
* (non-Javadoc)
*
* @see org.kaaproject.kaa.server.control.service.ControlService#
* getProfileSchemaByApplicationIdAndVersion (java.lang.String, int)
*/
@Override
public EndpointProfileSchemaDto getProfileSchemaByApplicationIdAndVersion(String applicationId, int version)
throws ControlServiceException {
return profileService.findProfileSchemaByAppIdAndVersion(applicationId, version);
}
/*
* (non-Javadoc)
*
* @see org.kaaproject.kaa.server.control.service.ControlService#
* editProfileSchema (org.kaaproject.kaa.common.dto.ProfileSchemaDto)
*/
@Override
public EndpointProfileSchemaDto editProfileSchema(EndpointProfileSchemaDto profileSchema) throws ControlServiceException {
return profileService.saveProfileSchema(profileSchema);
}
/*
* (non-Javadoc)
*
* @see org.kaaproject.kaa.server.control.service.ControlService#
* getServerProfileSchemasByApplicationId(java.lang.String)
*/
@Override
public List<ServerProfileSchemaDto> getServerProfileSchemasByApplicationId(String applicationId) throws ControlServiceException {
return serverProfileService.findServerProfileSchemasByAppId(applicationId);
}
/*
* (non-Javadoc)
*
* @see org.kaaproject.kaa.server.control.service.ControlService#
* getServerProfileSchema(java.lang.String)
*/
@Override
public ServerProfileSchemaDto getServerProfileSchema(String serverProfileSchemaId) throws ControlServiceException {
return serverProfileService.findServerProfileSchema(serverProfileSchemaId);
}
/*
* (non-Javadoc)
*
* @see org.kaaproject.kaa.server.control.service.ControlService#
* getServerProfileSchemaByApplicationIdAndVersion(java.lang.String, int)
*/
@Override
public ServerProfileSchemaDto getServerProfileSchemaByApplicationIdAndVersion(String applicationId, int version)
throws ControlServiceException {
return serverProfileService.findServerProfileSchemaByAppIdAndVersion(applicationId, version);
}
/*
* (non-Javadoc)
*
* @see org.kaaproject.kaa.server.control.service.ControlService#
* editServerProfileSchema
* (org.kaaproject.kaa.common.dto.ServerProfileSchemaDto)
*/
@Override
public ServerProfileSchemaDto saveServerProfileSchema(ServerProfileSchemaDto serverProfileSchema) throws ControlServiceException {
if (isNotBlank(serverProfileSchema.getCtlSchemaId())) {
return serverProfileService.saveServerProfileSchema(serverProfileSchema);
} else {
LOG.error("Server profile schema has no CTL schema ID");
throw new ControlServiceException("Server profile schema has no CTL schema ID");
}
}
/*
* (non-Javadoc)
*
* @see org.kaaproject.kaa.server.control.service.ControlService#
* findLatestServerProfileSchema(java.lang.String)
*/
@Override
public ServerProfileSchemaDto findLatestServerProfileSchema(String applicationId) throws ControlServiceException {
return serverProfileService.findLatestServerProfileSchema(applicationId);
}
/*
* (non-Javadoc)
*
* @see org.kaaproject.kaa.server.control.service.ControlService#
* getEndpointGroupsByApplicationId(java.lang.String)
*/
@Override
public List<EndpointGroupDto> getEndpointGroupsByApplicationId(String applicationId) throws ControlServiceException {
return endpointService.findEndpointGroupsByAppId(applicationId);
}
/*
* (non-Javadoc)
*
* @see
* org.kaaproject.kaa.server.control.service.ControlService#getEndpointGroup
* (java.lang.String)
*/
@Override
public EndpointGroupDto getEndpointGroup(String endpointGroupId) throws ControlServiceException {
return endpointService.findEndpointGroupById(endpointGroupId);
}
/*
* (non-Javadoc)
*
* @see org.kaaproject.kaa.server.control.service.ControlService#
* editEndpointGroup (org.kaaproject.kaa.common.dto.EndpointGroupDto)
*/
@Override
public EndpointGroupDto editEndpointGroup(EndpointGroupDto endpointGroup) throws ControlServiceException {
return endpointService.saveEndpointGroup(endpointGroup);
}
/*
* (non-Javadoc)
*
* @see org.kaaproject.kaa.server.control.service.ControlService#
* deleteEndpointGroup (java.lang.String)
*/
@Override
public void deleteEndpointGroup(String endpointGroupId) throws ControlServiceException {
ChangeNotificationDto notification = endpointService.removeEndpointGroupById(endpointGroupId);
if (notification != null) {
notifyEndpoints(notification, null, null);
}
}
/*
* (non-Javadoc)
*
* @see org.kaaproject.kaa.server.control.service.ControlService#
* removeTopicsFromEndpointGroup(java.lang.String, java.lang.String)
*/
@Override
public EndpointGroupDto removeTopicsFromEndpointGroup(String endpointGroupId, String topicId) throws ControlServiceException {
return notifyAndGetPayload(endpointService.removeTopicFromEndpointGroup(endpointGroupId, topicId));
}
/*
* (non-Javadoc)
*
* @see org.kaaproject.kaa.server.control.service.ControlService#
* addTopicsToEndpointGroup(java.lang.String, java.lang.String)
*/
@Override
public EndpointGroupDto addTopicsToEndpointGroup(String endpointGroupId, String topicId) throws ControlServiceException {
return notifyAndGetPayload(endpointService.addTopicToEndpointGroup(endpointGroupId, topicId));
}
/*
* (non-Javadoc)
*
* @see
* org.kaaproject.kaa.server.control.service.ControlService#getProfileFilter
* (java.lang.String)
*/
@Override
public ProfileFilterDto getProfileFilter(String profileFilterId) throws ControlServiceException {
return profileService.findProfileFilterById(profileFilterId);
}
/*
* (non-Javadoc)
*
* @see org.kaaproject.kaa.server.control.service.ControlService#
* getProfileFilterRecordsByEndpointGroupId(java.lang.String, boolean)
*/
@Override
public List<ProfileFilterRecordDto> getProfileFilterRecordsByEndpointGroupId(String endpointGroupId, boolean includeDeprecated)
throws ControlServiceException {
return new ArrayList<>(profileService.findAllProfileFilterRecordsByEndpointGroupId(endpointGroupId, includeDeprecated));
}
/*
* (non-Javadoc)
*
* @see org.kaaproject.kaa.server.control.service.ControlService#
* getProfileFilterRecord(java.lang.String, java.lang.String)
*/
@Override
public ProfileFilterRecordDto getProfileFilterRecord(String endpointProfileSchemaId, String serverProfileSchemaId,
String endpointGroupId) throws ControlServiceException {
return profileService.findProfileFilterRecordBySchemaIdAndEndpointGroupId(endpointProfileSchemaId, serverProfileSchemaId,
endpointGroupId);
}
/*
* (non-Javadoc)
*
* @see org.kaaproject.kaa.server.control.service.ControlService#
* getVacantProfileSchemasByEndpointGroupId(java.lang.String)
*/
@Override
public List<ProfileVersionPairDto> getVacantProfileSchemasByEndpointGroupId(String endpointGroupId) throws ControlServiceException {
return profileService.findVacantSchemasByEndpointGroupId(endpointGroupId);
}
/*
* (non-Javadoc)
*
* @see org.kaaproject.kaa.server.control.service.ControlService#
* editProfileFilter (org.kaaproject.kaa.common.dto.ProfileFilterDto)
*/
@Override
public ProfileFilterDto editProfileFilter(ProfileFilterDto profileFilter) throws ControlServiceException {
return profileService.saveProfileFilter(profileFilter);
}
/*
* (non-Javadoc)
*
* @see org.kaaproject.kaa.server.control.service.ControlService#
* getConfigurationRecordsByEndpointGroupId(java.lang.String, boolean)
*/
@Override
public List<ConfigurationRecordDto> getConfigurationRecordsByEndpointGroupId(String endpointGroupId, boolean includeDeprecated)
throws ControlServiceException {
return new ArrayList<>(configurationService.findAllConfigurationRecordsByEndpointGroupId(endpointGroupId, includeDeprecated));
}
/*
* (non-Javadoc)
*
* @see org.kaaproject.kaa.server.control.service.ControlService#
* getConfigurationRecord(java.lang.String, java.lang.String)
*/
@Override
public ConfigurationRecordDto getConfigurationRecord(String schemaId, String endpointGroupId) throws ControlServiceException {
return configurationService.findConfigurationRecordBySchemaIdAndEndpointGroupId(schemaId, endpointGroupId);
}
/*
* (non-Javadoc)
*
* @see org.kaaproject.kaa.server.control.service.ControlService#
* getVacantConfigurationSchemasByEndpointGroupId(java.lang.String)
*/
@Override
public List<VersionDto> getVacantConfigurationSchemasByEndpointGroupId(String endpointGroupId) throws ControlServiceException {
return configurationService.findVacantSchemasByEndpointGroupId(endpointGroupId);
}
/*
* (non-Javadoc)
*
* @see
* org.kaaproject.kaa.server.control.service.ControlService#getConfiguration
* (java.lang.String)
*/
@Override
public ConfigurationDto getConfiguration(String configurationId) throws ControlServiceException {
return configurationService.findConfigurationById(configurationId);
}
/*
* (non-Javadoc)
*
* @see org.kaaproject.kaa.server.control.service.ControlService#
* editConfiguration (org.kaaproject.kaa.common.dto.ConfigurationDto)
*/
@Override
public ConfigurationDto editConfiguration(ConfigurationDto configuration) throws ControlServiceException {
return configurationService.saveConfiguration(configuration);
}
/*
* (non-Javadoc)
*
* @see org.kaaproject.kaa.server.control.service.ControlService#
* editUserConfiguration
* (org.kaaproject.kaa.common.dto.EndpointUserConfigurationDto)
*/
@Override
public void editUserConfiguration(EndpointUserConfigurationDto configuration) throws ControlServiceException {
ApplicationDto appDto = applicationService.findAppByApplicationToken(configuration.getAppToken());
EndpointUserDto userDto = endpointService.findEndpointUserByExternalIdAndTenantId(configuration.getUserId(), appDto.getTenantId());
if(userDto == null){
throw new NotFoundException("Specified user not found!");
}
configuration.setUserId(userDto.getId());
configuration = userConfigurationService.saveUserConfiguration(configuration);
EndpointObjectHash hash = EndpointObjectHash.fromString(configuration.getBody());
checkNeighbors();
OperationsNodeInfo server = resolve(configuration.getUserId());
if (server != null) {
UserConfigurationUpdate msg = new UserConfigurationUpdate(appDto.getTenantId(), configuration.getUserId(),
configuration.getAppToken(), configuration.getSchemaVersion(), hash.getDataBuf());
if (LOG.isTraceEnabled()) {
LOG.trace("Sending message {} to [{}]", msg, Neighbors.getServerID(server.getConnectionInfo()));
}
neighbors.sendMessage(server.getConnectionInfo(), OperationsServiceMsg.fromUpdate(msg));
} else {
LOG.warn("Can't find server for user [{}]", configuration.getUserId());
}
}
/**
* Check neighbors.
*/
private void checkNeighbors() {
if (neighbors == null) {
synchronized (zkLock) {
if (neighbors == null) {
neighbors = new Neighbors<NeighborTemplate<OperationsServiceMsg>, OperationsServiceMsg>(
KaaThriftService.OPERATIONS_SERVICE, new NeighborTemplate<OperationsServiceMsg>() {
@Override
public void process(Iface client, List<OperationsServiceMsg> messages) throws TException {
OperationsServiceMsg.dispatch(client, messages);
}
@Override
public void onServerError(String serverId, Exception e) {
LOG.error("Can't send configuration update to {}", serverId, e);
}
}, neighborConnectionsSize);
ControlNode zkNode = controlZKService.getControlZKNode();
neighbors.setZkNode(KaaThriftService.KAA_NODE_SERVICE, zkNode.getControlServerInfo().getConnectionInfo(), zkNode);
}
}
}
}
/**
* Resolve.
*
* @param entityId
* the entity id
* @return the operations node info
*/
private OperationsNodeInfo resolve(String entityId) {
if (resolver == null) {
synchronized (zkLock) {
if (resolver == null) {
ControlNode zkNode = controlZKService.getControlZKNode();
resolver = new ConsistentHashResolver(zkNode.getCurrentOperationServerNodes(), userHashPartitions);
zkNode.addListener(new OperationsNodeListener() {
@Override
public void onNodeUpdated(OperationsNodeInfo node) {
LOG.info("Update of node {} is pushed to resolver {}", node, resolver);
resolver.onNodeUpdated(node);
}
@Override
public void onNodeRemoved(OperationsNodeInfo node) {
LOG.info("Remove of node {} is pushed to resolver {}", node, resolver);
resolver.onNodeRemoved(node);
}
@Override
public void onNodeAdded(OperationsNodeInfo node) {
LOG.info("Add of node {} is pushed to resolver {}", node, resolver);
resolver.onNodeAdded(node);
}
});
}
}
}
return resolver.getNode(entityId);
}
/*
* (non-Javadoc)
*
* @see org.kaaproject.kaa.server.control.service.ControlService#
* activateConfiguration(java.lang.String, java.lang.String)
*/
@Override
public ConfigurationDto activateConfiguration(String configurationId, String activatedUsername) throws ControlServiceException {
ChangeConfigurationNotification cfgNotification = configurationService.activateConfiguration(configurationId, activatedUsername);
ChangeNotificationDto notification = cfgNotification.getChangeNotificationDto();
if (notification != null) {
notifyEndpoints(notification, null, cfgNotification.getConfigurationDto());
}
return cfgNotification.getConfigurationDto();
}
/*
* (non-Javadoc)
*
* @see org.kaaproject.kaa.server.control.service.ControlService#
* deactivateConfiguration(java.lang.String, java.lang.String)
*/
@Override
public ConfigurationDto deactivateConfiguration(String configurationId, String deactivatedUsername) throws ControlServiceException {
ChangeConfigurationNotification cfgNotification = configurationService.deactivateConfiguration(configurationId,
deactivatedUsername);
ChangeNotificationDto notification = cfgNotification.getChangeNotificationDto();
if (notification != null) {
notifyEndpoints(notification, null, cfgNotification.getConfigurationDto());
}
return cfgNotification.getConfigurationDto();
}
/*
* (non-Javadoc)
*
* @see org.kaaproject.kaa.server.control.service.ControlService#
* deleteConfigurationRecord(java.lang.String, java.lang.String,
* java.lang.String)
*/
@Override
public void deleteConfigurationRecord(String schemaId, String endpointGroupId, String deactivatedUsername)
throws ControlServiceException {
ChangeConfigurationNotification cfgNotification = configurationService.deleteConfigurationRecord(schemaId, endpointGroupId,
deactivatedUsername);
if (cfgNotification != null) {
ChangeNotificationDto notification = cfgNotification.getChangeNotificationDto();
if (notification != null) {
notifyEndpoints(notification, null, cfgNotification.getConfigurationDto());
}
}
}