Skip to content
wais.go 60 KiB
Newer Older
Simon Pastor's avatar
Simon Pastor committed
/*
 * 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 server

import (
Simon Pastor's avatar
Simon Pastor committed
	"bytes"
Simon Pastor's avatar
Simon Pastor committed
	"context"
Simon Pastor's avatar
Simon Pastor committed
	"encoding/json"
	"errors"
	"fmt"
Simon Pastor's avatar
Simon Pastor committed
	"io/ioutil"
Simon Pastor's avatar
Simon Pastor committed
	"net/http"
	"net/url"
	"os"
	"reflect"
	"strconv"
	"strings"
	"sync"
Simon Pastor's avatar
Simon Pastor committed
	"time"

	sbi "github.com/InterDigitalInc/AdvantEDGE/go-apps/meep-wais/sbi"
	asc "github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-app-support-client"
Simon Pastor's avatar
Simon Pastor committed
	dkm "github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-data-key-mgr"
	httpLog "github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-http-logger"
	log "github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-logger"
	met "github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-metrics"
Simon Pastor's avatar
Simon Pastor committed
	redis "github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-redis"
	scc "github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-sandbox-ctrl-client"
	smc "github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-service-mgmt-client"
Simon Pastor's avatar
Simon Pastor committed

	"github.com/gorilla/mux"
)

Kevin Di Lallo's avatar
Kevin Di Lallo committed
const moduleName = "meep-wais"
const waisBasePath = "wai/v2/"
const waisKey = "wais"
const defaultMepName = "global"
const defaultScopeOfLocality = "MEC_SYSTEM"
const defaultConsumedLocalOnly = true
const appTerminationPath = "notifications/mec011/appTermination"
	notifAssocSta    = "AssocStaNotification"
	notifStaDataRate = "StaDataRateNotification"
	notifExpiry      = "ExpiryNotification"
	notifTest        = "TestNotification"
Simon Pastor's avatar
Simon Pastor committed

var redisAddr string = "meep-redis-master.default.svc.cluster.local:6379"
var influxAddr string = "http://meep-influxdb.default.svc.cluster.local:8086"
var sbxCtrlUrl string = "http://meep-sandbox-ctrl"
Simon Pastor's avatar
Simon Pastor committed
var currentStoreName = ""
Simon Pastor's avatar
Simon Pastor committed

const assocStaSubscriptionType = "AssocStaSubscription"
const staDataRateSubscriptionType = "StaDataRateSubscription"
Simon Pastor's avatar
Simon Pastor committed
const ASSOC_STA_SUBSCRIPTION = "AssocStaSubscription"
const STA_DATA_RATE_SUBSCRIPTION = "StaDataRateSubscription"
Simon Pastor's avatar
Simon Pastor committed
const ASSOC_STA_NOTIFICATION = "AssocStaNotification"
const STA_DATA_RATE_NOTIFICATION = "StaDataRateNotification"
Simon Pastor's avatar
Simon Pastor committed
const MEASUREMENT_REPORT_SUBSCRIPTION = "MeasurementReportSubscription"
const TEST_NOTIFICATION = "TestNotification"
var assocStaSubscriptionInfoMap = map[int]*AssocStaSubscriptionInfo{}
var staDataRateSubscriptionInfoMap = map[int]*StaDataRateSubscriptionInfo{}
Simon Pastor's avatar
Simon Pastor committed
var subscriptionExpiryMap = map[int][]int{}

var WAIS_DB = 0
Simon Pastor's avatar
Simon Pastor committed

var rc *redis.Connector
var hostUrl *url.URL
var instanceId string
var instanceName string
Simon Pastor's avatar
Simon Pastor committed
var sandboxName string
var mepName string = defaultMepName
var scopeOfLocality string = defaultScopeOfLocality
var consumedLocalOnly bool = defaultConsumedLocalOnly
var locality []string
Simon Pastor's avatar
Simon Pastor committed
var basePath string
var baseKey string
var mutex sync.Mutex
Simon Pastor's avatar
Simon Pastor committed

var expiryTicker *time.Ticker

var nextSubscriptionIdAvailable int

type ApInfoComplete struct {
	ApId       ApIdentity
	ApLocation ApLocation
	StaMacIds  []string
}

type ApInfoCompleteResp struct {
	ApInfoCompleteList []ApInfoComplete
}

type StaDataRateSubscriptionInfo struct {
Simon Pastor's avatar
Simon Pastor committed
	NextTts                int32 //next time to send, derived from notificationPeriod
	NotificationCheckReady bool
	Subscription           *StaDataRateSubscription
	Triggered              bool
}

type AssocStaSubscriptionInfo struct {
Simon Pastor's avatar
Simon Pastor committed
	NextTts                int32 //next time to send, derived from notificationPeriod
	NotificationCheckReady bool
	Subscription           *AssocStaSubscription
	Triggered              bool
	StaInfo *StaInfo `json:"staInfo"`
Simon Pastor's avatar
Simon Pastor committed
type StaInfoResp struct {
	StaInfoList []StaInfo
}

type ApInfoResp struct {
	ApInfoList []ApInfo
}

Simon Pastor's avatar
Simon Pastor committed
const serviceAppVersion = "2.1.1"

var serviceAppInstanceId string

var appEnablementUrl string
Kevin Di Lallo's avatar
Kevin Di Lallo committed
var appEnablementEnabled bool
Simon Pastor's avatar
Simon Pastor committed
var sendAppTerminationWhenDone bool = false
var appEnablementServiceId string
var appSupportClient *asc.APIClient
var svcMgmtClient *smc.APIClient
var sbxCtrlClient *scc.APIClient
Simon Pastor's avatar
Simon Pastor committed

var registrationTicker *time.Ticker
Simon Pastor's avatar
Simon Pastor committed

func notImplemented(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/json; charset=UTF-8")
	w.WriteHeader(http.StatusNotImplemented)
}
Simon Pastor's avatar
Simon Pastor committed
func Init() (err error) {

	// Retrieve Instance ID from environment variable if present
	instanceIdEnv := strings.TrimSpace(os.Getenv("MEEP_INSTANCE_ID"))
	if instanceIdEnv != "" {
		instanceId = instanceIdEnv
	}
	log.Info("MEEP_INSTANCE_ID: ", instanceId)

	// Retrieve Instance Name from environment variable
Kevin Di Lallo's avatar
Kevin Di Lallo committed
	instanceName = moduleName
	instanceNameEnv := strings.TrimSpace(os.Getenv("MEEP_POD_NAME"))
	if instanceNameEnv != "" {
		instanceName = instanceNameEnv
	}
	log.Info("MEEP_POD_NAME: ", instanceName)

Simon Pastor's avatar
Simon Pastor committed
	// Retrieve Sandbox name from environment variable
	sandboxNameEnv := strings.TrimSpace(os.Getenv("MEEP_SANDBOX_NAME"))
	if sandboxNameEnv != "" {
		sandboxName = sandboxNameEnv
	}
	if sandboxName == "" {
		err = errors.New("MEEP_SANDBOX_NAME env variable not set")
		log.Error(err.Error())
		return err
	}
	log.Info("MEEP_SANDBOX_NAME: ", sandboxName)

	// hostUrl is the url of the node serving the resourceURL
	// Retrieve public url address where service is reachable, if not present, use Host URL environment variable
	hostUrl, err = url.Parse(strings.TrimSpace(os.Getenv("MEEP_PUBLIC_URL")))
	if err != nil || hostUrl == nil || hostUrl.String() == "" {
		hostUrl, err = url.Parse(strings.TrimSpace(os.Getenv("MEEP_HOST_URL")))
		if err != nil {
			hostUrl = new(url.URL)
		}
	}
	log.Info("MEEP_HOST_URL: ", hostUrl)

	// Get MEP name
	mepNameEnv := strings.TrimSpace(os.Getenv("MEEP_MEP_NAME"))
	if mepNameEnv != "" {
Loading
Loading full blame…