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-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"
Simon Pastor
committed
clientNotifOMA "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
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
}
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
}
Simon Pastor
committed
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()
Simon Pastor
committed
zoneStatusReInit()
_ = 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")
}
}
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
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
func deregisterZoneStatus(subsIdStr string) {
subsId, _ := strconv.Atoi(subsIdStr)
zonalSubscriptionMap[subsId] = ""
}
func registerZoneStatus(zoneId string, nbOfUsersZoneThreshold uint32, nbOfUsersAPThreshold uint32, opStatus []OperationStatus, subsIdStr string) {
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] = ""
Loading
Loading full blame…