Newer
Older
/*
* 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-packages/meep-logger"
redis "github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-redis"
sbi "github.com/InterDigitalInc/AdvantEDGE/go-apps/meep-loc-serv/sbi"
Simon Pastor
committed
clientNotifOMA "github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-loc-serv-notification-client"
"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 USER_TRACKING_AND_ZONAL_TRAFFIC = 1
const ZONE_STATUS = 2
var nextZonalSubscriptionIdAvailable int
var nextUserSubscriptionIdAvailable int
Simon Pastor
committed
var nextZoneStatusSubscriptionIdAvailable int
var zonalSubscriptionEnteringMap = map[int]string{}
var zonalSubscriptionLeavingMap = map[int]string{}
var zonalSubscriptionTransferringMap = map[int]string{}
var zonalSubscriptionMap = map[int]string{}
var userSubscriptionEnteringMap = map[int]string{}
var userSubscriptionLeavingMap = map[int]string{}
var userSubscriptionTransferringMap = map[int]string{}
var userSubscriptionMap = map[int]string{}
Simon Pastor
committed
var zoneStatusSubscriptionMap = map[int]*ZoneStatusCheck{}
type ZoneStatusCheck struct {
ZoneId string
Serviceable bool
Unserviceable bool
Unknown bool
NbUsersInZoneThreshold int
NbUsersInAPThreshold int
}
Simon Pastor
committed
// Init - Location Service initialization
func Init() (err error) {
userTrackingReInit()
zonalTrafficReInit()
Simon Pastor
committed
zoneStatusReInit()
//sbi is the sole responsible of updating the userInfo, zoneInfo and apInfo structures
_ = sbi.Init(updateUserInfo, updateZoneInfo, updateAccessPointInfo, cleanUp)
Simon Pastor
committed
func createClient(notifyPath string) (*clientNotifOMA.APIClient, error) {
// Create & store client for App REST API
Simon Pastor
committed
subsAppClientCfg := clientNotifOMA.NewConfiguration()
subsAppClientCfg.BasePath = notifyPath
Simon Pastor
committed
subsAppClient := clientNotifOMA.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
}
Simon Pastor
committed
func deregisterZoneStatus(subsIdStr string) {
subsId, _ := strconv.Atoi(subsIdStr)
zonalSubscriptionMap[subsId] = ""
}
func registerZoneStatus(zoneId string, nbOfUsersZoneThreshold int32, nbOfUsersAPThreshold int32, opStatus []OperationStatus, subsIdStr string) {
Simon Pastor
committed
subsId, _ := strconv.Atoi(subsIdStr)
var zoneStatus ZoneStatusCheck
if opStatus != nil {
for i := 0; i < len(opStatus); i++ {
switch opStatus[i] {
case SERVICEABLE:
zoneStatus.Serviceable = true
case UNSERVICEABLE:
zoneStatus.Unserviceable = true
case OPSTATUS_UNKNOWN:
zoneStatus.Unknown = true
default:
}
}
}
zoneStatus.NbUsersInZoneThreshold = (int)(nbOfUsersZoneThreshold)
zoneStatus.NbUsersInAPThreshold = (int)(nbOfUsersAPThreshold)
zoneStatus.ZoneId = zoneId
zoneStatusSubscriptionMap[subsId] = &zoneStatus
}
func deregisterZonal(subsIdStr string) {
subsId, _ := strconv.Atoi(subsIdStr)
zonalSubscriptionMap[subsId] = ""
zonalSubscriptionEnteringMap[subsId] = ""
zonalSubscriptionLeavingMap[subsId] = ""
zonalSubscriptionTransferringMap[subsId] = ""
func registerZonal(zoneId string, event []UserEventType, subsIdStr string) {
subsId, _ := strconv.Atoi(subsIdStr)
if event != nil {
for i := 0; i < len(event); i++ {
switch event[i] {
case ENTERING:
zonalSubscriptionEnteringMap[subsId] = zoneId
zonalSubscriptionLeavingMap[subsId] = zoneId
zonalSubscriptionTransferringMap[subsId] = zoneId
default:
}
}
} else {
zonalSubscriptionEnteringMap[subsId] = zoneId
zonalSubscriptionLeavingMap[subsId] = zoneId
zonalSubscriptionTransferringMap[subsId] = zoneId
}
zonalSubscriptionMap[subsId] = zoneId
}
func deregisterUser(subsIdStr string) {
subsId, _ := strconv.Atoi(subsIdStr)
userSubscriptionMap[subsId] = ""
userSubscriptionEnteringMap[subsId] = ""
userSubscriptionLeavingMap[subsId] = ""
userSubscriptionTransferringMap[subsId] = ""
func registerUser(userAddress string, event []UserEventType, subsIdStr string) {
subsId, _ := strconv.Atoi(subsIdStr)
if event != nil {
for i := 0; i < len(event); i++ {
switch event[i] {
case ENTERING:
userSubscriptionEnteringMap[subsId] = userAddress
userSubscriptionLeavingMap[subsId] = userAddress
userSubscriptionTransferringMap[subsId] = userAddress
default:
}
}
} else {
userSubscriptionEnteringMap[subsId] = userAddress
userSubscriptionLeavingMap[subsId] = userAddress
userSubscriptionTransferringMap[subsId] = userAddress
}
userSubscriptionMap[subsId] = userAddress
}
func checkNotificationRegistrations(checkType int, param1 string, param2 string, param3 string, param4 string, param5 string) {
switch checkType {
case USER_TRACKING_AND_ZONAL_TRAFFIC:
//params are the following => newZoneId:oldZoneId:newAccessPointId:oldAccessPointId:userAddress
checkNotificationRegisteredUsers(param1, param2, param3, param4, param5)
checkNotificationRegisteredZones(param1, param2, param3, param4, param5)
case ZONE_STATUS:
//params are the following => zoneId:accessPointId:nbUsersInAP:nbUsersInZone
checkNotificationRegisteredZoneStatus(param1, param2, param3, param4)
default:
Simon Pastor
committed
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
}
}
func checkNotificationRegisteredZoneStatus(zoneId string, apId string, nbUsersInAPStr string, nbUsersInZoneStr string) {
//check all that applies
for subsId, zoneStatus := range zoneStatusSubscriptionMap {
if zoneStatus.ZoneId == zoneId {
nbUsersInZone := 0
nbUsersInAP := -1
zoneWarning := false
apWarning := false
if nbUsersInZoneStr != "" {
nbUsersInZone, _ = strconv.Atoi(nbUsersInZoneStr)
if nbUsersInZone >= zoneStatus.NbUsersInZoneThreshold {
zoneWarning = true
}
}
if nbUsersInAPStr != "" {
nbUsersInAP, _ = strconv.Atoi(nbUsersInAPStr)
if nbUsersInAP >= zoneStatus.NbUsersInAPThreshold {
apWarning = true
}
}
if zoneWarning || apWarning {
subsIdStr := strconv.Itoa(subsId)
jsonInfo, _ := rc.JSONGetEntry(moduleLocServ+":"+typeZoneStatusSubscription+":"+subsIdStr, ".")
Simon Pastor
committed
if jsonInfo == "" {
return
}
subscription := convertJsonToZoneStatusSubscription(jsonInfo)
var zoneStatusNotif clientNotifOMA.ZoneStatusNotification
zoneStatusNotif.ZoneId = zoneId
if apWarning {
zoneStatusNotif.AccessPointId = apId
zoneStatusNotif.NumberOfUsersInAP = (int32)(nbUsersInAP)
Simon Pastor
committed
}
if zoneWarning {
zoneStatusNotif.NumberOfUsersInZone = (int32)(nbUsersInZone)
Simon Pastor
committed
}
zoneStatusNotif.Timestamp = time.Now().String()
go sendStatusNotification(subscription.CallbackReference.NotifyURL, context.TODO(), subsIdStr, zoneStatusNotif)
if apWarning {
log.Info("Zone Status Notification" + "(" + subsIdStr + "): " + "For event in zone " + zoneId + " which has " + nbUsersInAPStr + " users in AP " + apId)
} else {
log.Info("Zone Status Notification" + "(" + subsIdStr + "): " + "For event in zone " + zoneId + " which has " + nbUsersInZoneStr + " users in total")
}
}
}
}
}
func checkNotificationRegisteredUsers(oldZoneId string, newZoneId string, oldApId string, newApId string, userId string) {
//check all that applies
for subsId, value := range userSubscriptionMap {
subsIdStr := strconv.Itoa(subsId)
jsonInfo, _ := rc.JSONGetEntry(moduleLocServ+":"+typeUserSubscription+":"+subsIdStr, ".")
if jsonInfo == "" {
return
}
subscription := convertJsonToUserSubscription(jsonInfo)
Simon Pastor
committed
var zonal clientNotifOMA.TrackingNotification
zonal.Address = userId
zonal.Timestamp = time.Now().String()
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
zonal.CallbackData = subscription.ClientCorrelator
if newZoneId != oldZoneId {
if userSubscriptionEnteringMap[subsId] != "" {
zonal.ZoneId = newZoneId
zonal.CurrentAccessPointId = newApId
zonal.UserEventType = "ENTERING"
go sendNotification(subscription.CallbackReference.NotifyURL, context.TODO(), subsIdStr, zonal)
log.Info("User Notification" + "(" + subsIdStr + "): " + "Entering event in zone " + newZoneId + " for user " + userId)
}
if oldZoneId != "" {
if userSubscriptionLeavingMap[subsId] != "" {
zonal.ZoneId = oldZoneId
zonal.CurrentAccessPointId = oldApId
zonal.UserEventType = "LEAVING"
go sendNotification(subscription.CallbackReference.NotifyURL, context.TODO(), subsIdStr, zonal)
log.Info("User Notification" + "(" + subsIdStr + "): " + "Leaving event in zone " + oldZoneId + " for user " + userId)
}
}
} else {
if newApId != oldApId {
if userSubscriptionTransferringMap[subsId] != "" {
zonal.ZoneId = newZoneId
zonal.CurrentAccessPointId = newApId
zonal.PreviousAccessPointId = oldApId
zonal.UserEventType = "TRANSFERRING"
go sendNotification(subscription.CallbackReference.NotifyURL, context.TODO(), subsIdStr, zonal)
log.Info("User Notification" + "(" + subsIdStr + "): " + " Transferring event within zone " + newZoneId + " for user " + userId + " from Ap " + oldApId + " to " + newApId)
}
}
Simon Pastor
committed
func sendNotification(notifyUrl string, ctx context.Context, subscriptionId string, notification clientNotifOMA.TrackingNotification) {
client, err := createClient(notifyUrl)
if err != nil {
log.Error(err)
return
}
_, _ = client.NotificationsApi.TrackingNotification(ctx, subscriptionId, notification)
}
Simon Pastor
committed
func sendStatusNotification(notifyUrl string, ctx context.Context, subscriptionId string, notification clientNotifOMA.ZoneStatusNotification) {
client, err := createClient(notifyUrl)
if err != nil {
log.Error(err)
return
}
_, _ = client.NotificationsApi.ZoneStatusNotification(ctx, subscriptionId, notification)
}
func checkNotificationRegisteredZones(oldZoneId string, newZoneId string, oldApId string, newApId string, userId string) {
//check all that applies
for subsId, value := range zonalSubscriptionMap {
if value == newZoneId {
if newZoneId != oldZoneId {
if zonalSubscriptionEnteringMap[subsId] != "" {
subsIdStr := strconv.Itoa(subsId)
jsonInfo, _ := rc.JSONGetEntry(moduleLocServ+":"+typeZonalSubscription+":"+subsIdStr, ".")
if jsonInfo != "" {
subscription := convertJsonToZonalSubscription(jsonInfo)
Simon Pastor
committed
var zonal clientNotifOMA.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(), subsIdStr, zonal)
log.Info("Zonal Notify Entering event in zone " + newZoneId + " for user " + userId)
}
}
} else {
if newApId != oldApId {
if zonalSubscriptionTransferringMap[subsId] != "" {
subsIdStr := strconv.Itoa(subsId)
jsonInfo, _ := rc.JSONGetEntry(moduleLocServ+":"+typeZonalSubscription+":"+subsIdStr, ".")
if jsonInfo != "" {
subscription := convertJsonToZonalSubscription(jsonInfo)
Simon Pastor
committed
var zonal clientNotifOMA.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(), subsIdStr, zonal)
log.Info("Zonal Notify Transferring event in zone " + newZoneId + " for user " + userId + " from Ap " + oldApId + " to " + newApId)
}
}
}
} else {
if value == oldZoneId {
if zonalSubscriptionLeavingMap[subsId] != "" {
subsIdStr := strconv.Itoa(subsId)
jsonInfo, _ := rc.JSONGetEntry(moduleLocServ+":"+typeZonalSubscription+":"+subsIdStr, ".")
if jsonInfo != "" {
subscription := convertJsonToZonalSubscription(jsonInfo)
Simon Pastor
committed
var zonal clientNotifOMA.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(), subsIdStr, zonal)
log.Info("Zonal Notify Leaving event in zone " + oldZoneId + " for user " + userId)
}
}
}
}
}
}
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
_ = rc.JSONGetList(zoneIdVar, accessPointIdVar, moduleLocServ+":"+typeUser+":", populateUserList, &userList)
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
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)
jsonInfo, _ := rc.JSONGetEntry(moduleLocServ+":"+typeUser+":"+vars["userId"], ".")
} 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)
_ = rc.JSONGetList(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, _ := rc.JSONGetEntry(moduleLocServ+":"+typeZone+":"+vars["zoneId"]+":"+typeAccessPoint+":"+vars["accessPointId"], ".")
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
_ = rc.JSONGetList("", "", 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, _ := rc.JSONGetEntry(moduleLocServ+":"+typeZone+":"+vars["zoneId"], ".")
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
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 := rc.JSONDelEntry(moduleLocServ+":"+typeUserSubscription+":"+vars["subscriptionId"], ".")
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
_ = rc.JSONGetList("", "", 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)
jsonInfo, _ := rc.JSONGetEntry(moduleLocServ+":"+typeUserSubscription+":"+vars["subscriptionId"], ".")
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
} 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
_ = rc.JSONSetEntry(moduleLocServ+":"+typeUserSubscription+":"+subsIdStr, ".", convertUserSubscriptionToJson(subs))
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
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
_ = rc.JSONSetEntry(moduleLocServ+":"+typeUserSubscription+":"+subsIdStr, ".", convertUserSubscriptionToJson(subs))
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
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 := rc.JSONDelEntry(moduleLocServ+":"+typeZonalSubscription+":"+vars["subscriptionId"], ".")
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
_ = rc.JSONGetList("", "", 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)
jsonInfo, _ := rc.JSONGetEntry(moduleLocServ+":"+typeZonalSubscription+":"+vars["subscriptionId"], ".")
Simon Pastor
committed
if jsonInfo != "" {
fmt.Fprintf(w, jsonInfo)
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
} 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
_ = rc.JSONSetEntry(moduleLocServ+":"+typeZonalSubscription+":"+subsIdStr, ".", convertZonalSubscriptionToJson(subs))
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
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
_ = rc.JSONSetEntry(moduleLocServ+":"+typeZonalSubscription+":"+subsIdStr, ".", convertZonalSubscriptionToJson(subs))
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")
Simon Pastor
committed
vars := mux.Vars(r)
err := rc.JSONDelEntry(moduleLocServ+":"+typeZoneStatusSubscription+":"+vars["subscriptionId"], ".")
Simon Pastor
committed
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
deregisterZoneStatus(vars["subscriptionId"])
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
_ = rc.JSONGetList("", "", 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")
Simon Pastor
committed
vars := mux.Vars(r)
jsonInfo, _ := rc.JSONGetEntry(moduleLocServ+":"+typeZoneStatusSubscription+":"+vars["subscriptionId"], ".")
Simon Pastor
committed
if jsonInfo != "" {
fmt.Fprintf(w, jsonInfo)
} else {
w.WriteHeader(http.StatusNotFound)
return
}
w.WriteHeader(http.StatusOK)
}
func zoneStatusPost(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
Simon Pastor
committed
subs := new(ZoneStatusSubscription)
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 := nextZoneStatusSubscriptionIdAvailable
nextZoneStatusSubscriptionIdAvailable++
subsIdStr := strconv.Itoa(newSubsId)
subs.ResourceURL = basepathURL + "subscriptions/zoneStatus/" + subsIdStr
_ = rc.JSONSetEntry(moduleLocServ+":"+typeZoneStatusSubscription+":"+subsIdStr, ".", convertZoneStatusSubscriptionToJson(subs))
Simon Pastor
committed
registerZoneStatus(subs.ZoneId, subs.NumberOfUsersZoneThreshold, subs.NumberOfUsersAPThreshold, subs.OperationStatus, 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)
Simon Pastor
committed
fmt.Fprintf(w, string(jsonResponse))
}
func zoneStatusPutById(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
Simon Pastor
committed
vars := mux.Vars(r)
subs := new(ZoneStatusSubscription)
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/zoneStatus/" + subsIdStr
_ = rc.JSONSetEntry(moduleLocServ+":"+typeZoneStatusSubscription+":"+subsIdStr, ".", convertZoneStatusSubscriptionToJson(subs))
Simon Pastor
committed
deregisterZoneStatus(subsIdStr)
registerZoneStatus(subs.ZoneId, subs.NumberOfUsersZoneThreshold, subs.NumberOfUsersAPThreshold, subs.OperationStatus, subsIdStr)
w.WriteHeader(http.StatusOK)
}
func populateZoneStatusList(key string, jsonInfo string, dummy1 string, dummy2 string, userData interface{}) error {
zoneList := userData.(*InlineResponse2002NotificationSubscriptionList)