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
/*
* Copyright (c) 2020 InterDigital Communications, 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 sbi
import (
"encoding/json"
dataModel "github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-data-model"
log "github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-logger"
mod "github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-model"
mq "github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-mq"
postgis "github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-postgis"
)
const moduleName string = "meep-wais-sbi"
const geModuleName string = "meep-gis-engine"
const postgisUser string = "postgres"
const postgisPwd string = "pwd"
type SbiCfg struct {
SandboxName string
RedisAddr string
PostgisHost string
PostgisPort string
UeDataCb func(string, string, string)
ApInfoCb func(string, string, *float32, *float32, []string)
ScenarioNameCb func(string)
CleanUpCb func()
}
type WaisSbi struct {
sandboxName string
mqLocal *mq.MsgQueue
handlerId int
activeModel *mod.Model
pc *postgis.Connector
updateUeDataCB func(string, string, string)
updateAccessPointInfoCB func(string, string, *float32, *float32, []string)
updateScenarioNameCB func(string)
cleanUpCB func()
}
var sbi *WaisSbi
// Init - WAI Service SBI initialization
func Init(cfg SbiCfg) (err error) {
// Create new SBI instance
if sbi != nil {
sbi = nil
}
sbi = new(WaisSbi)
sbi.sandboxName = cfg.SandboxName
sbi.updateUeDataCB = cfg.UeDataCb
sbi.updateAccessPointInfoCB = cfg.ApInfoCb
sbi.updateScenarioNameCB = cfg.ScenarioNameCb
sbi.cleanUpCB = cfg.CleanUpCb
// Create message queue
sbi.mqLocal, err = mq.NewMsgQueue(mq.GetLocalName(sbi.sandboxName), moduleName, sbi.sandboxName, cfg.RedisAddr)
if err != nil {
log.Error("Failed to create Message Queue with error: ", err)
return err
}
log.Info("Message Queue created")
// Create new active scenario model
modelCfg := mod.ModelCfg{
Name: "activeScenario",
Namespace: sbi.sandboxName,
Module: moduleName,
UpdateCb: nil,
DbAddr: cfg.RedisAddr,
}
sbi.activeModel, err = mod.NewModel(modelCfg)
if err != nil {
log.Error("Failed to create model: ", err.Error())
return err
}
// Connect to Postgis DB
sbi.pc, err = postgis.NewConnector(geModuleName, sbi.sandboxName, postgisUser, postgisPwd, cfg.PostgisHost, cfg.PostgisPort)
if err != nil {
log.Error("Failed to create postgis connector with error: ", err.Error())
return err
}
log.Info("Postgis Connector created")
// Initialize service
processActiveScenarioUpdate()
return nil
}
// Run - MEEP WAIS execution
func Run() (err error) {
// Register Message Queue handler
handler := mq.MsgHandler{Handler: msgHandler, UserData: nil}
sbi.handlerId, err = sbi.mqLocal.RegisterHandler(handler)
if err != nil {
log.Error("Failed to register message queue handler: ", err.Error())
return err
}
return nil
}
func Stop() (err error) {
sbi.mqLocal.UnregisterHandler(sbi.handlerId)
return nil
}
// Message Queue handler
func msgHandler(msg *mq.Msg, userData interface{}) {
switch msg.Message {
case mq.MsgScenarioActivate:
log.Debug("RX MSG: ", mq.PrintMsg(msg))
processActiveScenarioUpdate()
case mq.MsgScenarioUpdate:
log.Debug("RX MSG: ", mq.PrintMsg(msg))
processActiveScenarioUpdate()
case mq.MsgScenarioTerminate:
log.Debug("RX MSG: ", mq.PrintMsg(msg))
processActiveScenarioTerminate()
default:
log.Trace("Ignoring unsupported message: ", mq.PrintMsg(msg))
}
}
func processActiveScenarioTerminate() {
log.Debug("processActiveScenarioTerminate")
// Sync with active scenario store
sbi.activeModel.UpdateScenario()
sbi.cleanUpCB()
}
func processActiveScenarioUpdate() {
log.Debug("processActiveScenarioUpdate")
formerUeNameList := sbi.activeModel.GetNodeNames("UE")
sbi.activeModel.UpdateScenario()
scenarioName := sbi.activeModel.GetScenarioName()
sbi.updateScenarioNameCB(scenarioName)
// Get all UE & POA positions
//ueMap, _ := sbi.pc.GetAllUe()
poaMap, _ := sbi.pc.GetAllPoa()
// Update UE info
ueNameList := sbi.activeModel.GetNodeNames("UE")
for _, name := range ueNameList {
ueParent := sbi.activeModel.GetNodeParent(name)
if poa, ok := ueParent.(*dataModel.NetworkLocation); ok {
apMacId := ""
switch poa.Type_ {
case mod.NodeTypePoaWifi:
apMacId = poa.PoaWifiConfig.MacId
}
ue := (sbi.activeModel.GetNode(name)).(*dataModel.PhysicalLocation)
sbi.updateUeDataCB(name, ue.MacId, apMacId)
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
}
}
//only find UEs that were removed, check that former UEs are in new UE list
for _, oldUe := range formerUeNameList {
found := false
for _, newUe := range ueNameList {
if newUe == oldUe {
found = true
break
}
}
if !found {
sbi.updateUeDataCB(oldUe, oldUe, "")
log.Info("Ue removed : ", oldUe)
}
}
// Update POA Wifi info
poaNameList := sbi.activeModel.GetNodeNames(mod.NodeTypePoaWifi)
for _, name := range poaNameList {
poa := (sbi.activeModel.GetNode(name)).(*dataModel.NetworkLocation)
if poa == nil {
log.Error("Can't find poa named " + name)
continue
}
var longitude *float32
var latitude *float32
if myPoa, found := poaMap[name]; found {
longitude, latitude = parsePosition(myPoa.Position)
}
//list of Ues MacIds
var ueMacIdList []string
for _, pl := range poa.PhysicalLocations {
ueMacIdList = append(ueMacIdList, pl.MacId)
}
sbi.updateAccessPointInfoCB(name, poa.PoaWifiConfig.MacId, longitude, latitude, ueMacIdList)
}
}
func parsePosition(position string) (longitude *float32, latitude *float32) {
var point dataModel.Point
err := json.Unmarshal([]byte(position), &point)
if err != nil {
return nil, nil
}
return &point.Coordinates[0], &point.Coordinates[1]
}