Newer
Older
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 (c) 2019
* InterDigital Communications, Inc.
* All rights reserved.
*
* The information provided herein is the proprietary and confidential
* information of InterDigital Communications, Inc.
*/
package server
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"strconv"
"strings"
"time"
log "github.com/InterDigitalInc/AdvantEDGE/go-apps/meep-loc-serv/log"
db "github.com/InterDigitalInc/AdvantEDGE/go-apps/meep-loc-serv/redis"
sbi "github.com/InterDigitalInc/AdvantEDGE/go-apps/meep-loc-serv/sbi"
subs "github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-loc-serv-notification-client"
"github.com/KromDaniel/rejonson"
"github.com/go-redis/redis"
"github.com/gorilla/mux"
)
const basepathURL = "http://meep-loc-serv/etsi-013/location/v1/"
const moduleLocServ string = "loc-serv"
const typeZone = "zone"
const typeAccessPoint = "accessPoint"
const typeUser = "user"
const typeZonalSubscription = "zonalsubs"
const typeUserSubscription = "usersubs"
const typeZoneStatusSubscription = "zonestatus"
const locServChannel string = moduleLocServ
var nextZonalSubscriptionIdAvailable int
var nextUserSubscriptionIdAvailable int
//var nextZoneStatusSubscriptionIdAvailable = 1
var zonalSubscriptionEnteringMap = map[string]string{}
var zonalSubscriptionLeavingMap = map[string]string{}
var zonalSubscriptionTransferringMap = map[string]string{}
var zonalSubscriptionMap = map[string]string{}
var userSubscriptionEnteringMap = map[string]string{}
var userSubscriptionLeavingMap = map[string]string{}
var userSubscriptionTransferringMap = map[string]string{}
var userSubscriptionMap = map[string]string{}
var pubsub *redis.PubSub
var LOC_SERV_DB = 5
var dbClient *rejonson.Client
// Init - Location Service initialization
func Init() (err error) {
// Connect to Redis DB
dbClient, err = db.RedisDBConnect(LOC_SERV_DB)
if err != nil {
log.Error("Failed connection to Active DB for server. Error: ", err)
return err
}
log.Info("Connected to Active location service DB")
// Subscribe to Pub-Sub events for MEEP Controller
// NOTE: Current implementation is RedisDB Pub-Sub
pubsub, err = db.Subscribe(dbClient, locServChannel)
if err != nil {
log.Error("Failed to subscribe to Pub/Sub events. Error: ", err)
return err
}
userTrackingReInit()
zonalTrafficReInit()
_ = sbi.Init(updateUserInfo, updateZoneInfo, updateAccessPointInfo)
return nil
}
// Run - MEEP Location Service execution
func Run() {
// Listen for subscribed events. Provide event handler method.
_ = db.Listen(pubsub, eventHandler)
}
func eventHandler(channel string, payload string) {
// Handle Message according to Rx Channel
switch channel {
// MEEP Ctrl Engine active scenario update Channel
case locServChannel:
log.Debug("Event received on location service channel in server : ", payload)
go checkNotificationRegistrations(payload)
default:
log.Warn("Unsupported channel")
}
}
func createClient(notifyPath string) (*subs.APIClient, error) {
// Create & store client for App REST API
subsAppClientCfg := subs.NewConfiguration()
subsAppClientCfg.BasePath = notifyPath
subsAppClient := subs.NewAPIClient(subsAppClientCfg)
if subsAppClient == nil {
log.Error("Failed to create Subscription App REST API client: ", subsAppClientCfg.BasePath)
err := errors.New("Failed to create Subscription App REST API client")
return nil, err
}
return subsAppClient, nil
}
func deregisterZonal(subsId string) {
zoneId := zonalSubscriptionMap[subsId]
zonalSubscriptionMap[subsId] = ""
zonalSubscriptionEnteringMap[zoneId] = ""
zonalSubscriptionLeavingMap[zoneId] = ""
zonalSubscriptionTransferringMap[zoneId] = ""
}
func registerZonal(zoneId string, event []UserEventType, subsId string) {
if event != nil {
for i := 0; i < len(event); i++ {
switch event[i] {
case ENTERING:
zonalSubscriptionEnteringMap[zoneId] = subsId
case LEAVING:
zonalSubscriptionLeavingMap[zoneId] = subsId
case TRANSFERRING:
zonalSubscriptionTransferringMap[zoneId] = subsId
default:
}
}
} else {
zonalSubscriptionEnteringMap[zoneId] = subsId
zonalSubscriptionLeavingMap[zoneId] = subsId
zonalSubscriptionTransferringMap[zoneId] = subsId
}
zonalSubscriptionMap[subsId] = zoneId
}
func deregisterUser(subsId string) {
userAddress := userSubscriptionMap[subsId]
userSubscriptionMap[subsId] = ""
userSubscriptionEnteringMap[userAddress] = ""
userSubscriptionLeavingMap[userAddress] = ""
userSubscriptionTransferringMap[userAddress] = ""
}
func registerUser(userAddress string, event []UserEventType, subsId string) {
if event != nil {
for i := 0; i < len(event); i++ {
switch event[i] {
case ENTERING:
userSubscriptionEnteringMap[userAddress] = subsId
case LEAVING:
userSubscriptionLeavingMap[userAddress] = subsId
case TRANSFERRING:
userSubscriptionTransferringMap[userAddress] = subsId
default:
}
}
} else {
userSubscriptionEnteringMap[userAddress] = subsId
userSubscriptionLeavingMap[userAddress] = subsId
userSubscriptionTransferringMap[userAddress] = subsId
}
userSubscriptionMap[subsId] = userAddress
}
func checkNotificationRegistrations(payload string) {
values := strings.Split(payload, ":")
if len(values) == 5 {
//value is split in 5 newZoneId:oldZoneId:newAccessPointId:oldAccessPointId:userAddress
checkNotificationRegisteredUsers(values[0], values[1], values[2], values[3], values[4])
checkNotificationRegisteredZones(values[0], values[1], values[2], values[3], values[4])
}
}
func checkNotificationRegisteredUsers(oldZoneId string, newZoneId string, oldApId string, newApId string, userId string) {
//user is the same so we just need to get it once
subsId := userSubscriptionEnteringMap[userId]
if subsId == "" {
subsId = userSubscriptionLeavingMap[userId]
}
if subsId == "" {
subsId = userSubscriptionTransferringMap[userId]
}
if subsId == "" {
return
}
jsonInfo := db.DbJsonGet(dbClient, subsId, moduleLocServ+":"+typeUserSubscription)
if jsonInfo == "" {
return
}
subscription := convertJsonToUserSubscription(jsonInfo)
var zonal subs.TrackingNotification
zonal.Address = userId
zonal.Timestamp = time.Now().String()
zonal.CallbackData = subscription.ClientCorrelator
if newZoneId != oldZoneId {
if userSubscriptionEnteringMap[userId] != "" {
zonal.ZoneId = newZoneId
zonal.CurrentAccessPointId = newApId
zonal.UserEventType = "ENTERING"
go sendNotification(subscription.CallbackReference.NotifyURL, context.TODO(), subsId, zonal)
log.Info("User Notify Entering event in zone " + newZoneId + " for user " + userId)
}
if oldZoneId != "" {
if userSubscriptionLeavingMap[userId] != "" {
zonal.ZoneId = oldZoneId
zonal.CurrentAccessPointId = oldApId
zonal.UserEventType = "LEAVING"
go sendNotification(subscription.CallbackReference.NotifyURL, context.TODO(), subsId, zonal)
log.Info("User Notify Leaving event in zone " + oldZoneId + " for user " + userId)
}
}
} else {
if newApId != oldApId {
if userSubscriptionTransferringMap[userId] != "" {
zonal.ZoneId = newZoneId
zonal.CurrentAccessPointId = newApId
zonal.PreviousAccessPointId = oldApId
zonal.UserEventType = "TRANSFERRING"
go sendNotification(subscription.CallbackReference.NotifyURL, context.TODO(), subsId, zonal)
log.Info("User Notify Transferring event within zone " + newZoneId + " for user " + userId + " from Ap " + oldApId + " to " + newApId)
}
}
}
}
func sendNotification(notifyUrl string, ctx context.Context, subscriptionId string, notification subs.TrackingNotification) {
client, err := createClient(notifyUrl)
if err != nil {
log.Error(err)
return
}
_, _ = client.NotificationsApi.TrackingNotification(ctx, subscriptionId, notification)
}
func checkNotificationRegisteredZones(oldZoneId string, newZoneId string, oldApId string, newApId string, userId string) {
if newZoneId != oldZoneId {
if zonalSubscriptionEnteringMap[newZoneId] != "" {
subsId := zonalSubscriptionEnteringMap[newZoneId]
jsonInfo := db.DbJsonGet(dbClient, subsId, moduleLocServ+":"+typeZonalSubscription)
if jsonInfo != "" {
subscription := convertJsonToZonalSubscription(jsonInfo)
var zonal subs.TrackingNotification
zonal.ZoneId = newZoneId
zonal.CurrentAccessPointId = newApId
zonal.Address = userId
zonal.UserEventType = "ENTERING"
zonal.Timestamp = time.Now().String()
zonal.CallbackData = subscription.ClientCorrelator
go sendNotification(subscription.CallbackReference.NotifyURL, context.TODO(), subsId, zonal)
log.Info("Zonal Notify Entering event in zone " + newZoneId + " for user " + userId)
}
}
if zonalSubscriptionLeavingMap[oldZoneId] != "" {
subsId := zonalSubscriptionLeavingMap[oldZoneId]
jsonInfo := db.DbJsonGet(dbClient, subsId, moduleLocServ+":"+typeZonalSubscription)
if jsonInfo != "" {
subscription := convertJsonToZonalSubscription(jsonInfo)
var zonal subs.TrackingNotification
zonal.ZoneId = oldZoneId
zonal.CurrentAccessPointId = oldApId
zonal.Address = userId
zonal.UserEventType = "LEAVING"
zonal.Timestamp = time.Now().String()
zonal.CallbackData = subscription.ClientCorrelator
go sendNotification(subscription.CallbackReference.NotifyURL, context.TODO(), subsId, zonal)
log.Info("Zonal Notify Leaving event in zone " + oldZoneId + " for user " + userId)
}
}
} else {
if newApId != oldApId {
if zonalSubscriptionTransferringMap[newZoneId] != "" {
subsId := zonalSubscriptionTransferringMap[newZoneId]
jsonInfo := db.DbJsonGet(dbClient, subsId, moduleLocServ+":"+typeZonalSubscription)
if jsonInfo != "" {
subscription := convertJsonToZonalSubscription(jsonInfo)
var zonal subs.TrackingNotification
zonal.ZoneId = newZoneId
zonal.CurrentAccessPointId = newApId
zonal.PreviousAccessPointId = oldApId
zonal.Address = userId
zonal.UserEventType = "TRANSFERRING"
zonal.Timestamp = time.Now().String()
zonal.CallbackData = subscription.ClientCorrelator
go sendNotification(subscription.CallbackReference.NotifyURL, context.TODO(), subsId, zonal)
// go client.NotificationsApi.TrackingNotification(context.TODO(), subsId, zonal)
log.Info("Zonal Notify Transferring event in zone " + newZoneId + " for user " + userId + " from Ap " + oldApId + " to " + newApId)
}
}
}
}
}
func usersGet(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
u, _ := url.Parse(r.URL.String())
log.Info("url: ", u.RequestURI())
q := u.Query()
zoneIdVar := q.Get("zoneId")
accessPointIdVar := q.Get("accessPointId")
var userList UserList
_ = db.DbJsonGetList(dbClient, zoneIdVar, accessPointIdVar, moduleLocServ+":"+typeUser, populateUserList, &userList)
userList.ResourceURL = basepathURL + "users"
jsonResponse, err := json.Marshal(userList)
if err != nil {
log.Error(err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, string(jsonResponse))
}
func populateUserList(key string, jsonInfo string, zoneId string, apId string, userData interface{}) error {
userList := userData.(*UserList)
var userInfo UserInfo
// Format response
err := json.Unmarshal([]byte(jsonInfo), &userInfo)
if err != nil {
return err
}
found1 := false
found2 := false
if zoneId != "" {
if userInfo.ZoneId == zoneId {
found1 = true
}
} else {
found1 = true
}
if apId != "" {
if userInfo.AccessPointId == apId {
found2 = true
}
} else {
found2 = true
}
if found1 && found2 {
userList.User = append(userList.User, userInfo)
}
return nil
}
func usersGetById(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
vars := mux.Vars(r)
jsonUserInfo := db.DbJsonGet(dbClient, vars["userId"], moduleLocServ+":"+typeUser)
if jsonUserInfo != "" {
fmt.Fprintf(w, jsonUserInfo)
} else {
w.WriteHeader(http.StatusNotFound)
return
}
w.WriteHeader(http.StatusOK)
}
func zonesByIdGetAps(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
u, _ := url.Parse(r.URL.String())
log.Info("url: ", u.RequestURI())
q := u.Query()
interestRealm := q.Get("interestRealm")
var apList AccessPointList
vars := mux.Vars(r)
_ = db.DbJsonGetList(dbClient, interestRealm, "", moduleLocServ+":"+typeZone+":"+vars["zoneId"], populateApList, &apList)
apList.ZoneId = vars["zoneId"]
apList.ResourceURL = basepathURL + "zones/" + vars["zoneId"] + "/accessPoints"
jsonResponse, err := json.Marshal(apList)
if err != nil {
log.Error(err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, string(jsonResponse))
}
func zonesByIdGetApsById(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
vars := mux.Vars(r)
jsonInfo := db.DbJsonGet(dbClient, vars["accessPointId"], moduleLocServ+":"+typeZone+":"+vars["zoneId"]+":"+typeAccessPoint)
if jsonInfo != "" {
fmt.Fprintf(w, jsonInfo)
} else {
w.WriteHeader(http.StatusNotFound)
return
}
w.WriteHeader(http.StatusOK)
}
func zonesGet(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
var zoneList ZoneList
_ = db.DbJsonGetList(dbClient, "", "", moduleLocServ+":"+typeZone, populateZoneList, &zoneList)
zoneList.ResourceURL = basepathURL + "zones"
jsonResponse, err := json.Marshal(zoneList)
if err != nil {
log.Error(err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, string(jsonResponse))
}
func zonesGetById(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
vars := mux.Vars(r)
jsonInfo := db.DbJsonGet(dbClient, vars["zoneId"], moduleLocServ+":"+typeZone)
if jsonInfo != "" {
fmt.Fprintf(w, jsonInfo)
} else {
w.WriteHeader(http.StatusNotFound)
return
}
w.WriteHeader(http.StatusOK)
}
func populateZoneList(key string, jsonInfo string, dummy1 string, dummy2 string, userData interface{}) error {
zoneList := userData.(*ZoneList)
var zoneInfo ZoneInfo
// Format response
err := json.Unmarshal([]byte(jsonInfo), &zoneInfo)
if err != nil {
return err
}
if zoneInfo.ZoneId != "" {
zoneList.Zone = append(zoneList.Zone, zoneInfo)
}
return nil
}
func populateApList(key string, jsonInfo string, interestRealm string, dummy string, userData interface{}) error {
apList := userData.(*AccessPointList)
var apInfo AccessPointInfo
// Format response
err := json.Unmarshal([]byte(jsonInfo), &apInfo)
if err != nil {
return err
}
if apInfo.AccessPointId != "" {
found := false
if interestRealm != "" {
if apInfo.InterestRealm == interestRealm {
found = true
}
} else {
found = true
}
if found {
apList.AccessPoint = append(apList.AccessPoint, apInfo)
}
}
return nil
}
func userTrackingSubDelById(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
vars := mux.Vars(r)
err := db.DbJsonDelete(dbClient, vars["subscriptionId"], moduleLocServ+":"+typeUserSubscription)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
deregisterUser(vars["subscriptionId"])
w.WriteHeader(http.StatusOK)
}
func userTrackingSubGet(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
var userList InlineResponse2001NotificationSubscriptionList
_ = db.DbJsonGetList(dbClient, "", "", moduleLocServ+":"+typeUserSubscription, populateUserTrackingList, &userList)
userList.ResourceURL = basepathURL + "subscriptions/userTracking"
jsonResponse, err := json.Marshal(userList)
if err != nil {
log.Error(err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, string(jsonResponse))
}
func userTrackingSubGetById(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
vars := mux.Vars(r)
jsonUserInfo := db.DbJsonGet(dbClient, vars["subscriptionId"], moduleLocServ+":"+typeUserSubscription)
if jsonUserInfo != "" {
fmt.Fprintf(w, jsonUserInfo)
} else {
w.WriteHeader(http.StatusNotFound)
return
}
w.WriteHeader(http.StatusOK)
}
func userTrackingSubPost(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
subs := new(UserTrackingSubscription)
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&subs)
if err != nil {
log.Error(err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
newSubsId := nextUserSubscriptionIdAvailable
nextUserSubscriptionIdAvailable++
subsIdStr := strconv.Itoa(newSubsId)
registerUser(subs.Address, subs.UserEventCriteria, subsIdStr)
subs.ResourceURL = basepathURL + "subscriptions/userTracking/" + subsIdStr
_ = db.DbJsonSet(dbClient, subsIdStr, convertUserSubscriptionToJson(subs), moduleLocServ+":"+typeUserSubscription)
jsonResponse, err := json.Marshal(subs)
if err != nil {
log.Error(err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, string(jsonResponse))
}
func userTrackingSubPutById(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
vars := mux.Vars(r)
subs := new(UserTrackingSubscription)
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&subs)
if err != nil {
log.Error(err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
}
subsIdStr := vars["subscriptionId"]
subs.ResourceURL = basepathURL + "subscriptions/userTracking/" + subsIdStr
_ = db.DbJsonSet(dbClient, subsIdStr, convertUserSubscriptionToJson(subs), moduleLocServ+":"+typeUserSubscription)
deregisterUser(subsIdStr)
registerUser(subs.Address, subs.UserEventCriteria, subsIdStr)
w.WriteHeader(http.StatusOK)
}
func populateUserTrackingList(key string, jsonInfo string, dummy1 string, dummy2 string, userData interface{}) error {
userList := userData.(*InlineResponse2001NotificationSubscriptionList)
var userInfo UserTrackingSubscription
// Format response
err := json.Unmarshal([]byte(jsonInfo), &userInfo)
if err != nil {
return err
}
userList.UserTrackingSubscription = append(userList.UserTrackingSubscription, userInfo)
return nil
}
func zonalTrafficSubDelById(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
vars := mux.Vars(r)
err := db.DbJsonDelete(dbClient, vars["subscriptionId"], moduleLocServ+":"+typeZonalSubscription)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
deregisterZonal(vars["subscriptionId"])
w.WriteHeader(http.StatusOK)
}
func zonalTrafficSubGet(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
var zoneList InlineResponse200NotificationSubscriptionList
_ = db.DbJsonGetList(dbClient, "", "", moduleLocServ+":"+typeZonalSubscription, populateZonalTrafficList, &zoneList)
zoneList.ResourceURL = basepathURL + "subcription/zonalTraffic"
jsonResponse, err := json.Marshal(zoneList)
if err != nil {
log.Error(err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, string(jsonResponse))
}
func zonalTrafficSubGetById(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
vars := mux.Vars(r)
jsonUserInfo := db.DbJsonGet(dbClient, vars["subscriptionId"], moduleLocServ+":"+typeZonalSubscription)
if jsonUserInfo != "" {
fmt.Fprintf(w, jsonUserInfo)
} else {
w.WriteHeader(http.StatusNotFound)
return
}
w.WriteHeader(http.StatusOK)
}
func zonalTrafficSubPost(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
subs := new(ZonalTrafficSubscription)
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&subs)
if err != nil {
log.Error(err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
newSubsId := nextZonalSubscriptionIdAvailable
nextZonalSubscriptionIdAvailable++
subsIdStr := strconv.Itoa(newSubsId)
/*
if subs.Duration > 0 {
//TODO start a timer mecanism and expire subscription
}
//else, lasts forever or until subscription is deleted
*/
if subs.Duration != "" && subs.Duration != "0" {
//TODO start a timer mecanism and expire subscription
log.Info("Non zero duration")
}
//else, lasts forever or until subscription is deleted
subs.ResourceURL = basepathURL + "subscriptions/zonalTraffic/" + subsIdStr
_ = db.DbJsonSet(dbClient, subsIdStr, convertZonalSubscriptionToJson(subs), moduleLocServ+":"+typeZonalSubscription)
registerZonal(subs.ZoneId, subs.UserEventCriteria, subsIdStr)
jsonResponse, err := json.Marshal(subs)
if err != nil {
log.Error(err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, string(jsonResponse))
}
func zonalTrafficSubPutById(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
vars := mux.Vars(r)
subs := new(ZonalTrafficSubscription)
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&subs)
if err != nil {
log.Error(err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
subsIdStr := vars["subscriptionId"]
subs.ResourceURL = basepathURL + "subscriptions/zonalTraffic/" + subsIdStr
_ = db.DbJsonSet(dbClient, subsIdStr, convertZonalSubscriptionToJson(subs), moduleLocServ+":"+typeZonalSubscription)
deregisterZonal(subsIdStr)
registerZonal(subs.ZoneId, subs.UserEventCriteria, subsIdStr)
w.WriteHeader(http.StatusOK)
}
func populateZonalTrafficList(key string, jsonInfo string, dummy1 string, dummy2 string, userData interface{}) error {
zoneList := userData.(*InlineResponse200NotificationSubscriptionList)
var zoneInfo ZonalTrafficSubscription
// Format response
err := json.Unmarshal([]byte(jsonInfo), &zoneInfo)
if err != nil {
return err
}
zoneList.ZonalTrafficSubscription = append(zoneList.ZonalTrafficSubscription, zoneInfo)
return nil
}
func zoneStatusDelById(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
}
func zoneStatusGet(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
var zoneList InlineResponse2002NotificationSubscriptionList
_ = db.DbJsonGetList(dbClient, "", "", moduleLocServ+":"+typeZoneStatusSubscription, populateZoneStatusList, &zoneList)
zoneList.ResourceURL = basepathURL + "subscription/zoneStatus"
jsonResponse, err := json.Marshal(zoneList)
if err != nil {
log.Error(err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, string(jsonResponse))
}
func zoneStatusGetById(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
}
func zoneStatusPost(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
}
func zoneStatusPutById(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
}
func populateZoneStatusList(key string, jsonInfo string, dummy1 string, dummy2 string, userData interface{}) error {
zoneList := userData.(*InlineResponse2002NotificationSubscriptionList)
var zoneInfo ZoneStatusSubscription
// Format response
err := json.Unmarshal([]byte(jsonInfo), &zoneInfo)
if err != nil {
return err
}
zoneList.ZoneStatusSubscription = append(zoneList.ZoneStatusSubscription, zoneInfo)
return nil
}
func updateUserInfo(address string, zoneId string, accessPointId string, resourceName string) {
//get from DB
jsonUserInfo := db.DbJsonGet(dbClient, address, moduleLocServ+":"+typeUser)
userInfo := new(UserInfo)
if jsonUserInfo != "" {
userInfo = convertJsonToUserInfo(jsonUserInfo)
if zoneId != "" {
userInfo.ZoneId = zoneId
}
if accessPointId != "" {
userInfo.AccessPointId = accessPointId
}
//updateDB
_ = db.DbJsonSet(dbClient, address, convertUserInfoToJson(userInfo), moduleLocServ+":"+typeUser)
} else {
userInfo.Address = address
userInfo.ZoneId = zoneId
userInfo.AccessPointId = accessPointId
userInfo.ResourceURL = resourceName
//unsued optional attributes
//userInfo.LocationInfo.Latitude,
//userInfo.LocationInfo.Longitude,
//userInfo.LocationInfo.Altitude,
//userInfo.LocationInfo.Accuracy,
//userInfo.ContextLocationInfo,
//userInfo.AncillaryInfo)
_ = db.DbJsonSet(dbClient, address, convertUserInfoToJson(userInfo), moduleLocServ+":"+typeUser)
}
}
func updateZoneInfo(zoneId string, nbAccessPoints int, nbUnsrvAccessPoints int, nbUsers int, resourceName string) {
//get from DB
jsonZoneInfo := db.DbJsonGet(dbClient, zoneId, moduleLocServ+":"+typeZone)
zoneInfo := new(ZoneInfo)
if jsonZoneInfo != "" {
zoneInfo = convertJsonToZoneInfo(jsonZoneInfo)
if nbAccessPoints != -1 {
zoneInfo.NumberOfAccessPoints = uint32(nbAccessPoints)
}
if nbUnsrvAccessPoints != -1 {
zoneInfo.NumberOfUnservicableAccessPoints = uint32(nbUnsrvAccessPoints)
}
if nbUsers != -1 {
zoneInfo.NumberOfUsers = uint32(nbUsers)
}
//updateDB
_ = db.DbJsonSet(dbClient, zoneId, convertZoneInfoToJson(zoneInfo), moduleLocServ+":"+typeZone)
} else {
zoneInfo.ZoneId = zoneId
zoneInfo.ResourceURL = resourceName
zoneInfo.NumberOfAccessPoints = uint32(nbAccessPoints)
zoneInfo.NumberOfUnservicableAccessPoints = uint32(nbUnsrvAccessPoints)
zoneInfo.NumberOfUsers = uint32(nbUsers)
_ = db.DbJsonSet(dbClient, zoneId, convertZoneInfoToJson(zoneInfo), moduleLocServ+":"+typeZone)
}
}
func updateAccessPointInfo(zoneId string, apId string, conTypeStr string, opStatusStr string, nbUsers int, resourceName string) {
//get from DB
jsonApInfo := db.DbJsonGet(dbClient, apId, moduleLocServ+":"+typeZone+":"+zoneId+":"+typeAccessPoint)
if jsonApInfo != "" {
apInfo := convertJsonToAccessPointInfo(jsonApInfo)
if opStatusStr != "" {
opStatus := convertStringToOperationStatus(opStatusStr)
apInfo.OperationStatus = &opStatus
}
if nbUsers != -1 {
apInfo.NumberOfUsers = uint32(nbUsers)
}
//updateDB
_ = db.DbJsonSet(dbClient, apId, convertAccessPointInfoToJson(apInfo), moduleLocServ+":"+typeZone+":"+zoneId+":"+typeAccessPoint)
} else {
apInfo := new(AccessPointInfo)
apInfo.AccessPointId = apId
apInfo.ResourceURL = resourceName
conType := convertStringToConnectionType(conTypeStr)
apInfo.ConnectionType = &conType
opStatus := convertStringToOperationStatus(opStatusStr)
apInfo.OperationStatus = &opStatus
apInfo.NumberOfUsers = uint32(nbUsers)
//unsued optional attributes
//apInfo.LocationInfo.Latitude
//apInfo.LocationInfo.Longitude
//apInfo.LocationInfo.Altitude
//apInfo.LocationInfo.Accuracy
//apInfo.Timezone
//apInfo.InterestRealm
_ = db.DbJsonSet(dbClient, apId, convertAccessPointInfoToJson(apInfo), moduleLocServ+":"+typeZone+":"+zoneId+":"+typeAccessPoint)
}
}
func zonalTrafficReInit() {
//reusing the object response for the get multiple zonalSubscription
var zoneList InlineResponse200NotificationSubscriptionList
_ = db.DbJsonGetList(dbClient, "", "", moduleLocServ+":"+typeZonalSubscription, populateZonalTrafficList, &zoneList)
maxZonalSubscriptionId := 0
for _, zone := range zoneList.ZonalTrafficSubscription {
resourceUrl := strings.Split(zone.ResourceURL, "/")
subscriptionId, err := strconv.Atoi(resourceUrl[len(resourceUrl)-1])
if err != nil {
log.Error(err)
} else {
if subscriptionId > maxZonalSubscriptionId {
maxZonalSubscriptionId = subscriptionId
}
subscriptionIdStr := strconv.Itoa(subscriptionId)
for i := 0; i < len(zone.UserEventCriteria); i++ {
switch zone.UserEventCriteria[i] {
case ENTERING:
zonalSubscriptionEnteringMap[zone.ZoneId] = subscriptionIdStr
case LEAVING:
zonalSubscriptionLeavingMap[zone.ZoneId] = subscriptionIdStr
case TRANSFERRING:
zonalSubscriptionTransferringMap[zone.ZoneId] = subscriptionIdStr
default:
}
}
zonalSubscriptionMap[strconv.Itoa(subscriptionId)] = zone.ZoneId
}
}
nextZonalSubscriptionIdAvailable = maxZonalSubscriptionId + 1
}
func userTrackingReInit() {
//reusing the object response for the get multiple zonalSubscription
var userList InlineResponse2001NotificationSubscriptionList
_ = db.DbJsonGetList(dbClient, "", "", moduleLocServ+":"+typeUserSubscription, populateUserTrackingList, &userList)
maxUserSubscriptionId := 0
for _, user := range userList.UserTrackingSubscription {
resourceUrl := strings.Split(user.ResourceURL, "/")
subscriptionId, err := strconv.Atoi(resourceUrl[len(resourceUrl)-1])
if err != nil {
log.Error(err)
} else {