Newer
Older
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
jsonRespDB, _ := rc.JSONGetEntry(baseKey+"subscriptions:"+subIdParamStr, ".")
if jsonRespDB == "" {
w.WriteHeader(http.StatusNotFound)
return
}
var subscriptionCommon SubscriptionCommon
err := json.Unmarshal([]byte(jsonRespDB), &subscriptionCommon)
if err != nil {
log.Error(err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
var jsonResponse []byte
switch subscriptionCommon.SubscriptionType {
case ASSOC_STA_SUBSCRIPTION:
var subscription AssocStaSubscription
err = json.Unmarshal([]byte(jsonRespDB), &subscription)
if err != nil {
log.Error(err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
Simon Pastor
committed
jsonResponse, err = json.Marshal(subscription)
case STA_DATA_RATE_SUBSCRIPTION:
var subscription StaDataRateSubscription
err = json.Unmarshal([]byte(jsonRespDB), &subscription)
if err != nil {
log.Error(err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
case MEASUREMENT_REPORT_SUBSCRIPTION:
w.WriteHeader(http.StatusNotImplemented)
return
default:
log.Error("Unknown subscription type")
w.WriteHeader(http.StatusBadRequest)
return
}
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 isSubscriptionIdRegisteredAssocSta(subsIdStr string) bool {
if assocStaSubscriptionInfoMap[subsId] != nil {
return true
} else {
return false
}
}
Simon Pastor
committed
func isSubscriptionIdRegisteredStaDataRate(subsIdStr string) bool {
subsId, _ := strconv.Atoi(subsIdStr)
mutex.Lock()
defer mutex.Unlock()
if staDataRateSubscriptionInfoMap[subsId] != nil {
Simon Pastor
committed
return true
} else {
return false
}
}
func registerAssocSta(subscription *AssocStaSubscription, subsIdStr string) {
mutex.Lock()
defer mutex.Unlock()
//immediate trigger of the subscription
assocStaSubscriptionInfo := AssocStaSubscriptionInfo{0 /*subscription.NotificationPeriod*/, false, subscription, false}
assocStaSubscriptionInfoMap[subsId] = &assocStaSubscriptionInfo
if subscription.ExpiryDeadline != nil {
//get current list of subscription meant to expire at this time
intList := subscriptionExpiryMap[int(subscription.ExpiryDeadline.Seconds)]
intList = append(intList, subsId)
subscriptionExpiryMap[int(subscription.ExpiryDeadline.Seconds)] = intList
}
log.Info("New registration: ", subsId, " type: ", subscription.SubscriptionType)
Simon Pastor
committed
if subscription.RequestTestNotification {
sendTestNotification(subscription.CallbackReference, subscription.Links.Self)
}
}
func registerStaDataRate(subscription *StaDataRateSubscription, subsIdStr string) {
subsId, _ := strconv.Atoi(subsIdStr)
mutex.Lock()
defer mutex.Unlock()
//immediate trigger of the subscription
staDataRateSubscriptionInfo := StaDataRateSubscriptionInfo{0 /*subscription.NotificationPeriod*/, false, subscription, false}
staDataRateSubscriptionInfoMap[subsId] = &staDataRateSubscriptionInfo
Simon Pastor
committed
if subscription.ExpiryDeadline != nil {
//get current list of subscription meant to expire at this time
intList := subscriptionExpiryMap[int(subscription.ExpiryDeadline.Seconds)]
intList = append(intList, subsId)
subscriptionExpiryMap[int(subscription.ExpiryDeadline.Seconds)] = intList
}
log.Info("New registration: ", subsId, " type: ", subscription.SubscriptionType)
if subscription.RequestTestNotification {
sendTestNotification(subscription.CallbackReference, subscription.Links.Self)
}
func deregisterAssocSta(subsIdStr string, mutexTaken bool) {
if !mutexTaken {
mutex.Lock()
defer mutex.Unlock()
}
assocStaSubscriptionInfoMap[subsId] = nil
log.Info("Deregistration: ", subsId, " type: ", assocStaSubscriptionType)
}
Simon Pastor
committed
func deregisterStaDataRate(subsIdStr string, mutexTaken bool) {
subsId, _ := strconv.Atoi(subsIdStr)
if !mutexTaken {
mutex.Lock()
defer mutex.Unlock()
}
staDataRateSubscriptionInfoMap[subsId] = nil
Simon Pastor
committed
log.Info("Deregistration: ", subsId, " type: ", staDataRateSubscriptionType)
}
func subscriptionsPOST(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
var subscriptionCommon SubscriptionCommon
bodyBytes, _ := ioutil.ReadAll(r.Body)
err := json.Unmarshal(bodyBytes, &subscriptionCommon)
if err != nil {
log.Error(err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
//extract common body part
subscriptionType := subscriptionCommon.SubscriptionType
//mandatory parameter
if subscriptionCommon.CallbackReference == "" {
log.Error("Mandatory CallbackReference parameter not present")
http.Error(w, "Mandatory CallbackReference parameter not present", http.StatusBadRequest)
return
}
newSubsId := nextSubscriptionIdAvailable
nextSubscriptionIdAvailable++
subsIdStr := strconv.Itoa(newSubsId)
link := new(AssocStaSubscriptionLinks)
self := new(LinkType)
self.Href = hostUrl.String() + basePath + "subscriptions/" + subsIdStr
link.Self = self
var jsonResponse []byte
switch subscriptionType {
case ASSOC_STA_SUBSCRIPTION:
var subscription AssocStaSubscription
err = json.Unmarshal(bodyBytes, &subscription)
if err != nil {
log.Error(err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
subscription.Links = link
//make sure subscription is valid for mandatory parameters
//make sure subscription is valid for mandatory parameters
if subscription.NotificationPeriod == 0 && subscription.NotificationEvent == nil {
log.Error("Either or Both NotificationPeriod or NotificationEvent shall be present")
http.Error(w, "Either or Both NotificationPeriod or NotificationEvent shall be present", http.StatusBadRequest)
return
}
if subscription.NotificationEvent != nil {
if subscription.NotificationEvent.Trigger <= 0 && subscription.NotificationEvent.Trigger > 2 {
log.Error("Mandatory Notification Event Trigger not valid")
http.Error(w, "Mandatory Notification Event Trigger not valid", http.StatusBadRequest)
return
}
}
if subscription.ApId == nil {
log.Error("Mandatory ApId missing")
http.Error(w, "Mandatory ApId missing", http.StatusBadRequest)
return
}
//registration
_ = rc.JSONSetEntry(baseKey+"subscriptions:"+subsIdStr, ".", convertAssocStaSubscriptionToJson(&subscription))
registerAssocSta(&subscription, subsIdStr)
Simon Pastor
committed
jsonResponse, err = json.Marshal(subscription)
case STA_DATA_RATE_SUBSCRIPTION:
var subscription StaDataRateSubscription
err = json.Unmarshal(bodyBytes, &subscription)
if err != nil {
log.Error(err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
subscription.Links = link
//make sure subscription is valid for mandatory parameters
//make sure subscription is valid for mandatory parameters
if subscription.NotificationPeriod == 0 && subscription.NotificationEvent == nil {
log.Error("Either or Both NotificationPeriod or NotificationEvent shall be present")
http.Error(w, "Either or Both NotificationPeriod or NotificationEvent shall be present", http.StatusBadRequest)
return
}
if subscription.NotificationEvent != nil {
if subscription.NotificationEvent.Trigger <= 0 && subscription.NotificationEvent.Trigger > 8 {
log.Error("Mandatory Notification Event Trigger not valid")
http.Error(w, "Mandatory Notification Event Trigger not valid", http.StatusBadRequest)
return
}
}
if subscription.StaId == nil {
log.Error("Mandatory StaId missing")
http.Error(w, "Mandatory StaId missing", http.StatusBadRequest)
return
}
Simon Pastor
committed
//registration
_ = rc.JSONSetEntry(baseKey+"subscriptions:"+subsIdStr, ".", convertStaDataRateSubscriptionToJson(&subscription))
registerStaDataRate(&subscription, subsIdStr)
case MEASUREMENT_REPORT_SUBSCRIPTION:
nextSubscriptionIdAvailable--
w.WriteHeader(http.StatusNotImplemented)
return
default:
nextSubscriptionIdAvailable--
w.WriteHeader(http.StatusBadRequest)
return
}
if err != nil {
log.Error(err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusCreated)
fmt.Fprintf(w, string(jsonResponse))
}
func subscriptionsPUT(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
vars := mux.Vars(r)
subIdParamStr := vars["subscriptionId"]
var subscriptionCommon SubscriptionCommon
bodyBytes, _ := ioutil.ReadAll(r.Body)
err := json.Unmarshal(bodyBytes, &subscriptionCommon)
if err != nil {
log.Error(err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
//extract common body part
subscriptionType := subscriptionCommon.SubscriptionType
//mandatory parameter
if subscriptionCommon.CallbackReference == "" {
log.Error("Mandatory CallbackReference parameter not present")
http.Error(w, "Mandatory CallbackReference parameter not present", http.StatusBadRequest)
return
}
link := subscriptionCommon.Links
if link == nil || link.Self == nil {
log.Error("Mandatory Link parameter not present")
http.Error(w, "Mandatory Link parameter not present", http.StatusBadRequest)
return
}
selfUrl := strings.Split(link.Self.Href, "/")
subsIdStr := selfUrl[len(selfUrl)-1]
if subsIdStr != subIdParamStr {
log.Error("SubscriptionId in endpoint and in body not matching")
http.Error(w, "SubscriptionId in endpoint and in body not matching", http.StatusBadRequest)
return
}
alreadyRegistered := false
var jsonResponse []byte
switch subscriptionType {
case ASSOC_STA_SUBSCRIPTION:
var subscription AssocStaSubscription
err = json.Unmarshal(bodyBytes, &subscription)
if err != nil {
log.Error(err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if isSubscriptionIdRegisteredAssocSta(subsIdStr) {
registerAssocSta(&subscription, subsIdStr)
_ = rc.JSONSetEntry(baseKey+"subscriptions:"+subsIdStr, ".", convertAssocStaSubscriptionToJson(&subscription))
alreadyRegistered = true
jsonResponse, err = json.Marshal(subscription)
}
Simon Pastor
committed
case STA_DATA_RATE_SUBSCRIPTION:
var subscription StaDataRateSubscription
err = json.Unmarshal(bodyBytes, &subscription)
if err != nil {
log.Error(err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
//only support one subscription
if isSubscriptionIdRegisteredStaDataRate(subsIdStr) {
registerStaDataRate(&subscription, subsIdStr)
_ = rc.JSONSetEntry(baseKey+"subscriptions:"+subsIdStr, ".", convertStaDataRateSubscriptionToJson(&subscription))
alreadyRegistered = true
jsonResponse, err = json.Marshal(subscription)
}
case MEASUREMENT_REPORT_SUBSCRIPTION:
w.WriteHeader(http.StatusNotImplemented)
return
default:
w.WriteHeader(http.StatusBadRequest)
return
}
if alreadyRegistered {
if err != nil {
log.Error(err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, string(jsonResponse))
} else {
w.WriteHeader(http.StatusNotFound)
}
func delSubscription(keyPrefix string, subsId string, mutexTaken bool) error {
err := rc.JSONDelEntry(keyPrefix+":"+subsId, ".")
Simon Pastor
committed
deregisterStaDataRate(subsId, mutexTaken)
return err
}
func subscriptionsDELETE(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
vars := mux.Vars(r)
subIdParamStr := vars["subscriptionId"]
jsonRespDB, _ := rc.JSONGetEntry(baseKey+"subscriptions:"+subIdParamStr, ".")
if jsonRespDB == "" {
w.WriteHeader(http.StatusNotFound)
return
}
err := delSubscription(baseKey+"subscriptions", subIdParamStr, false)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
func populateApInfo(key string, jsonInfo string, response interface{}) error {
if resp == nil {
return errors.New("Response not defined")
}
// Retrieve user info from DB
var apInfoComplete ApInfoComplete
err := json.Unmarshal([]byte(jsonInfo), &apInfoComplete)
if err != nil {
return err
}
//timeStamp is optional, commenting the code
//seconds := time.Now().Unix()
//var timeStamp TimeStamp
//timeStamp.Seconds = int32(seconds)
var bssLoad BssLoad
bssLoad.StaCount = int32(len(apInfoComplete.StaMacIds))
bssLoad.ChannelUtilization = 0
bssLoad.AvailAdmCap = 0
apInfo.BssLoad = &bssLoad
var apLocation ApLocation
var geoLocation GeoLocation
if apInfoComplete.ApLocation.Geolocation != nil {
geoLocation.Lat = apInfoComplete.ApLocation.Geolocation.Lat
geoLocation.Long = apInfoComplete.ApLocation.Geolocation.Long
func populateApInfoCompleteList(key string, jsonInfo string, response interface{}) error {
resp := response.(*ApInfoCompleteResp)
// Retrieve ap info from DB
var apInfoComplete ApInfoComplete
err := json.Unmarshal([]byte(jsonInfo), &apInfoComplete)
if err != nil {
return err
}
resp.ApInfoCompleteList = append(resp.ApInfoCompleteList, apInfoComplete)
return nil
}
func apInfoGET(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
//initialise array to make sure Marshal processes it properly if it is empty
response.ApInfoList = make([]ApInfo, 0)
//loop through each AP
keyName := baseKey + "AP:*"
err := rc.ForEachJSONEntry(keyName, populateApInfo, &response)
if err != nil {
log.Error(err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if err != nil {
log.Error(err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, string(jsonResponse))
}
Simon Pastor
committed
func populateStaData(key string, jsonInfo string, response interface{}) error {
if resp == nil {
return errors.New("Response not defined")
}
// Add STA info to reponse (ignore if not associated to a wifi AP)
Simon Pastor
committed
staData := convertJsonToStaData(jsonInfo)
if staData.StaInfo.ApAssociated != nil {
//timeStamp is optional, commenting the code
//seconds := time.Now().Unix()
//var timeStamp TimeStamp
//timeStamp.Seconds = int32(seconds)
//staInfo.TimeStamp = &timeStamp
Simon Pastor
committed
resp.StaInfoList = append(resp.StaInfoList, *staData.StaInfo)
/*
func populateStaDataList(key string, jsonInfo string, response interface{}) error {
resp := response.(*StaDataList)
var staData StaData
err := json.Unmarshal([]byte(jsonInfo), &staData)
if err != nil {
return err
}
resp.StaDataList = append(resp.StaDataList, staData)
return nil
}
*/
func staInfoGET(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
//initialise array to make sure Marshal processes it properly if it is empty
response.StaInfoList = make([]StaInfo, 0)
// Loop through each STA
Simon Pastor
committed
err := rc.ForEachJSONEntry(keyName, populateStaData, &response)
if err != nil {
log.Error(err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
jsonResponse, err := json.Marshal(response.StaInfoList)
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 createSubscriptionLinkList(subType string) *SubscriptionLinkList {
subscriptionLinkList := new(SubscriptionLinkList)
link := new(SubscriptionLinkListLinks)
self := new(LinkType)
self.Href = hostUrl.String() + basePath + "subscriptions"
subscriptionLinkList.Links = link
//loop through all different types of subscription
mutex.Lock()
defer mutex.Unlock()
if subType == "" || subType == assocStaSubscriptionType {
//loop through assocSta map
for _, assocStaSubscriptionInfo := range assocStaSubscriptionInfoMap {
if assocStaSubscriptionInfo != nil {
Simon Pastor
committed
var subscription SubscriptionLinkListSubscription
subscription.Href = assocStaSubscriptionInfo.Subscription.Links.Self.Href
Simon Pastor
committed
subscription.SubscriptionType = ASSOC_STA_SUBSCRIPTION
subscriptionLinkList.Subscription = append(subscriptionLinkList.Subscription, subscription)
}
}
}
if subType == "" || subType == staDataRateSubscriptionType {
//loop through assocSta map
for _, staDataRateSubscriptionInfo := range staDataRateSubscriptionInfoMap {
if staDataRateSubscriptionInfo != nil {
Simon Pastor
committed
var subscription SubscriptionLinkListSubscription
subscription.Href = staDataRateSubscriptionInfo.Subscription.Links.Self.Href
Simon Pastor
committed
subscription.SubscriptionType = STA_DATA_RATE_SUBSCRIPTION
subscriptionLinkList.Subscription = append(subscriptionLinkList.Subscription, subscription)
Simon Pastor
committed
//no other maps to go through
return subscriptionLinkList
}
func subscriptionLinkListSubscriptionsGET(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
u, _ := url.Parse(r.URL.String())
log.Info("url: ", u.RequestURI())
q := u.Query()
subType := q.Get("subscription_type")
validQueryParams := []string{"subscription_type"}
validQueryParamValues := []string{"assoc_sta", "sta_data_rate", "measure_report"}
//look for all query parameters to reject if any invalid ones
found := false
for queryParam, values := range q {
found = false
for _, validQueryParam := range validQueryParams {
if queryParam == validQueryParam {
found = true
break
}
}
if !found {
log.Error("Query param not valid: ", queryParam)
w.WriteHeader(http.StatusBadRequest)
return
}
for _, validQueryParamValue := range validQueryParamValues {
found = false
for _, value := range values {
if value == validQueryParamValue {
found = true
break
}
}
if found {
break
}
}
if !found {
log.Error("Query param not valid: ", queryParam)
w.WriteHeader(http.StatusBadRequest)
return
}
}
response := createSubscriptionLinkList(subType)
jsonResponse, err := json.Marshal(response)
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 cleanUp() {
log.Info("Terminate all")
rc.DBFlush(baseKey)
nextSubscriptionIdAvailable = 1
mutex.Lock()
defer mutex.Unlock()
assocStaSubscriptionInfoMap = map[int]*AssocStaSubscriptionInfo{}
staDataRateSubscriptionInfoMap = map[int]*StaDataRateSubscriptionInfo{}
func updateStoreName(storeName string) {
_ = httpLog.ReInit(logModuleWAIS, sandboxName, storeName, redisAddr, influxAddr)