Commit 2ae9a246 authored by Yann Garcia's avatar Yann Garcia
Browse files

Add MEC-011 support in onboarded-demo app - App Swagger API support in demo4-ue

parent 7519a414
Loading
Loading
Loading
Loading
+7 −3
Original line number Diff line number Diff line
@@ -3,10 +3,12 @@ module github.com/AdvantEDGE/examples/demo4-ue/src/demo-server/backend
go 1.13

require (
	github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-ams-client v0.0.0-20211214133749-f203f7ab4f1c
	github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-app-support-client v0.0.0-20211214133749-f203f7ab4f1c
	github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-dai-client v0.0.0
	github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-data-key-mgr v0.0.0
	github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-http-logger v0.0.0
	github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-logger v0.0.0
	github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-metrics v0.0.0
	github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-mq v0.0.0
	github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-redis v0.0.0
	github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-sandbox-ctrl-client v0.0.0-20211214133749-f203f7ab4f1c
@@ -14,15 +16,17 @@ require (
	github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-swagger-api-mgr v0.0.0
	github.com/google/uuid v1.1.2
	github.com/gorilla/handlers v1.5.1
	github.com/gorilla/mux v1.7.3
	github.com/gorilla/mux v1.8.0
	github.com/spf13/viper v1.12.0
)

replace (
	github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-ams-client => ../../../../../go-packages/meep-ams-client
	github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-app-support-client => ../../../../../go-packages/meep-app-support-client
	github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-dai-client => ../../../../../go-packages/meep-dai-client
	github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-data-key-mgr => ../../../../../go-packages/meep-data-key-mgr
	github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-http-logger => ../../../../../go-packages/meep-http-logger
	github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-logger => ../../../../../go-packages/meep-logger
	github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-metrics => ../../../../../go-packages/meep-metrics
	github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-mq => ../../../../../go-packages/meep-mq
	github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-redis => ../../../../../go-packages/meep-redis
	github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-sandbox-ctrl-client => ../../../../../go-packages/meep-sandbox-ctrl-client
+193 −0

File changed.

Preview size limit exceeded, changes collapsed.

+1 −1
Original line number Diff line number Diff line
@@ -38,7 +38,7 @@ import (

// Initalize customized logger
func init() {
	log.MeepTextLogInit("Demo-4")
	log.MeepTextLogInit("demo4-ue")
}

func main() {
+14 −208
Original line number Diff line number Diff line
@@ -30,7 +30,6 @@ import (
	//"time"

	"github.com/AdvantEDGE/examples/demo4-ue/src/demo-server/backend/util"
	ams "github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-ams-client"
	asc "github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-app-support-client"
	dai "github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-dai-client"
	log "github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-logger"
@@ -49,7 +48,6 @@ const (

var mutex sync.Mutex

//var intervalTicker *time.Ticker
var done chan bool

// App-enablement client
@@ -58,9 +56,6 @@ var srvMgmtClientPath string
var appSupportClient *asc.APIClient
var appSupportClientPath string

//var registrationTicker *time.Ticker
//var appInstanceId string

// Sandbox controller client
var sandBoxClient *sbx.APIClient
var sbxCtrlUrl string = "http://meep-sandbox-ctrl"
@@ -69,12 +64,6 @@ var sbxCtrlUrl string = "http://meep-sandbox-ctrl"
var daiClient *dai.APIClient
var daiServiceName string

// Ams client & context transfer payload
var amsClient *ams.APIClient
var amsResourceId string
var amsTargetId string
var amsServiceName string

var svcSubscriptionSent bool
var appTerminationSent bool
var serviceRegistered bool
@@ -126,21 +115,6 @@ type AppContextState struct {

var appContexts = make(map[string]AppContextState)

// Initiaze ticker to increment terminal device state every second
/*func startTicker() {
	intervalTicker = time.NewTicker(1 * time.Second)
	go func() {
		for range intervalTicker.C {
			// Increment terminal device state by 1
			for _, device := range trackDevices {
				terminalDeviceState[device] += 1
				stateAsString := strconv.Itoa(terminalDeviceState[device])
				terminalDevices[device] = device + " using this instance" + "(state=" + stateAsString + ")"
			}
		}
	}()
}*/

// Init - Config & Client Package initialization
func Init(envPath string, envName string) (port string, err error) {

@@ -268,10 +242,9 @@ func Init(envPath string, envName string) (port string, err error) {
	log.Info("Message Queue created")

	// Create Swagger API Manager
	os.Setenv("MEEP_HOST_URL", "http://"+config.Localurl)
	os.Setenv("MEEP_HOST_URL", callBackUrl)
	os.Setenv("MEEP_SVC_PATH", "/"+moduleName)
	log.Info("am.NewSwaggerApiMgr: ", moduleName, " - ", config.SandboxUrl, " - ", mepName)
	apiMgr, err = sam.NewSwaggerApiMgr(moduleName, config.SandboxUrl, mepName, mqLocal)
	apiMgr, err = sam.NewSwaggerApiMgr_1(moduleName, mepName, mqLocal, callBackUrl, "/"+moduleName)
	if err != nil {
		log.Error("Failed to create Swagger API Manager. Error: ", err)
		return "", err
@@ -291,16 +264,16 @@ func Init(envPath string, envName string) (port string, err error) {

func setApplicationInfo(appInfo sbx.ApplicationInfo) (appInfoRes sbx.ApplicationInfo, err error) {
	log.Info(">>> setApplicationInfo: ", appInfo)
	appInfoRes, _, err = sandBoxClient.ApplicationsApi.ApplicationsPOST(context.TODO(), appInfo)
	appInfoRes, resp, err := sandBoxClient.ApplicationsApi.ApplicationsPOST(context.TODO(), appInfo)
	if err != nil {
		log.Error(err.Error())
		return appInfoRes, err
	}
	log.Debug("setApplicationInfo: Receive confirmation acknowlegement ", resp.Status)

	return appInfoRes, nil
}

// REST API - Demo4 confirm acknowledgement, create ams resource & subscriptions & mec service
func demo4Register() (err error) {
	log.Info(">>> demo4Register: ", appEnablementEnabled)

@@ -327,7 +300,7 @@ func demo4Register() (err error) {
	handler := mq.MsgHandler{Handler: msgHandler, UserData: nil}
	handlerId, err = mqLocal.RegisterHandler(handler)

	// Start app registeration ticker counter
	// Start app registeration
	if !appEnablementEnabled {

		// If app is restarted, clean app activity, AMS terminal devices, discovered services
@@ -432,42 +405,6 @@ func demo4Register() (err error) {
		log.Debug("daiClient instance: ", daiClient)
		appActivityLogs = append(appActivityLogs, "daiClient instance created")

		/*if environment == "advantedge" {
			amsServiceName = "meep-ams"
		} else {
			amsServiceName = "mec021-1"
		}
		var amsUrl = mecServicesMap[amsServiceName]
		var amsSubscription ApplicationInstanceAmsLinkListSubscription

		amsClientcfg := ams.NewConfiguration()
		amsClientcfg.BasePath = amsUrl
		amsClient = ams.NewAPIClient(amsClientcfg)
		if amsClient == nil {
			appActivityLogs = append(appActivityLogs, "Failed to get amsClient instance")
			err := errors.New("Failed to get amsClient instance")
			return err
		}

		amsId, err := amsSendService(instanceName, "")
		if err != nil {
			appActivityLogs = append(appActivityLogs, "Failed to subscribe to AMS service resource")
			err := errors.New("Failed to subscribe to AMS service resource")
			return err
		} else {
			amsServiceCreated = true
			// Store ams resource
			demoAppInfo.AmsResource = true
			// Create ams subscription
			subscriptionId, _ := amsSendSubscription(instanceName, "", callBackUrl)
			// Store ams resource id & ams subcription id
			amsResourceId = amsId
			amsSubscriptionId = subscriptionId
			amsSubscription.SubId = subscriptionId
			amsSubscriptionSent = true
		}

		subscriptions.AmsLinkListSubscription = &amsSubscription*/
		demoAppInfo.Subscriptions = &subscriptions
		appEnablementEnabled = true
		//startTicker()
@@ -515,12 +452,6 @@ func Terminate() {
			appActivityLogs = append(appActivityLogs, "=== De-register Demo4 MEC Application [200]")
		}

		// empty ams state
		// terminalDeviceState = make(map[string]int)
		// trackDevices = []string{}
		// orderedAmsAdded = []string{}
		// terminalDevices = make(map[string]string)

		if appTerminationSent {
			//Delete app subscriptions
			if demoAppInfo.Subscriptions != nil {
@@ -555,30 +486,6 @@ func Terminate() {
				}
			}

			// Delete ams service
			// if amsServiceCreated {
			// 	err := delAmsService(amsResourceId)
			// 	if err == nil {

			// 		log.Info("Deleted AMS resource")
			// 		amsServiceCreated = false
			// 		demoAppInfo.AmsResource = false
			// 	}

			// }

			// Delete ams subscriptions
			// if amsSubscriptionSent {
			// 	err := deleteAmsSubscription(demoAppInfo.Subscriptions.AmsLinkListSubscription.SubId)
			// 	if err == nil {

			// 		log.Info("Deleted AMS subscription")
			// 		demoAppInfo.Subscriptions.AmsLinkListSubscription.SubId = ""
			// 		amsSubscriptionSent = false
			// 	}

			// }

			//Send Confirm Terminate if received notification
			if terminateNotification {
				confirmTerminate(instanceName)
@@ -842,81 +749,6 @@ func subscribeAvailability(appInstanceId string, callbackReference string) (stri
	return subscriptionId, nil
}

/*func amsSendService(appInstanceId string, device string) (string, error) {
	log.Debug("Sending request to mec platform create ams resource api")
	var bodyRegisterationInfo ams.RegistrationInfo
	bodyRegisterationInfo.ServiceConsumerId = &ams.RegistrationInfoServiceConsumerId{
		AppInstanceId: appInstanceId,
	}

	registerationInfo, _, err := amsClient.AmsiApi.AppMobilityServicePOST(context.TODO(), bodyRegisterationInfo)

	if err != nil {
		log.Error(err)
		return "", err
	}

	// Store ams service id
	amsResourceId = registerationInfo.AppMobilityServiceId

	log.Info("Created app mobility service resource on user app instance ", instanceName[0:6], "...", " tracking ", associateId.Value)

	return registerationInfo.AppMobilityServiceId, nil
}*/

// CLient request to create an ams subscription
// Return ams subscription id to update ams
/*func amsSendSubscription(appInstanceId string, device string, callBackUrl string) (string, error) {
	log.Debug("Sending request to mec platform adding ams subscription api")

	var mobilityProcedureSubscription ams.MobilityProcedureSubscription

	mobilityProcedureSubscription.CallbackReference = callBackUrl + "/services/callback/amsevent"
	mobilityProcedureSubscription.SubscriptionType = "MobilityProcedureSubscription"

	// Default tracking ue set to 10.100.0.3
	var associateId ams.AssociateId
	var t_ ams.ModelType = ams.UE_I_PV4_ADDRESS
	associateId.Type_ = &t_
	associateId.Value = device

	// Filter criteria
	var mobilityFiler ams.MobilityProcedureSubscriptionFilterCriteria
	mobilityFiler.AppInstanceId = appInstanceId
	mobilityFiler.AssociateId = append(mobilityFiler.AssociateId, associateId)

	mobilityProcedureSubscription.FilterCriteria = &mobilityFiler

	inlineSubscription := ams.ConvertMobilityProcedureSubscriptionToInlineSubscription(&mobilityProcedureSubscription)

	mobilitySubscription, resp, err := amsClient.AmsiApi.SubPOST(context.TODO(), *inlineSubscription)
	if err != nil {
		log.Error(err.Error())
		return "", err
	}
	hRefLink := mobilitySubscription.Links.Self.Href

	// Find subscription id from response
	idPosition := strings.LastIndex(hRefLink, "/")
	if idPosition == -1 {
		log.Error("Error parsing subscription id for subscription")
		return "", err
	}
	status := strconv.Itoa(resp.StatusCode)
	amsId := hRefLink[idPosition+1:]

	if err != nil {
		log.Error(resp.Status)
		appActivityLogs = append(appActivityLogs, "Subscribe to AMS notifications ["+status+"]")
		return "", err
	}

	amsSubscriptionSent = true

	appActivityLogs = append(appActivityLogs, "Subscribe to AMS notifications ["+status+"]")
	return amsId, nil
}*/

// Client request to delete a mec-service resource
func unregisterService(appInstanceId string, serviceId string) error {
	resp, err := srvMgmtClient.MecServiceMgmtApi.AppServicesServiceIdDELETE(context.TODO(), appInstanceId, serviceId)
@@ -964,32 +796,6 @@ func delsubscribeAvailability(appInstanceId string, subscriptionId string) error
	return nil
}

// Client request to delete ams subscription
func deleteAmsSubscription(subscriptionId string) error {
	resp, err := amsClient.AmsiApi.SubByIdDELETE(context.TODO(), subscriptionId)
	status := strconv.Itoa(resp.StatusCode)
	if err != nil {
		appActivityLogs = append(appActivityLogs, "Delete AMS subscription ["+status+"]")
		log.Error("Failed to clear ams subcription ", resp.Status)
		return err
	}
	appActivityLogs = append(appActivityLogs, "Delete AMS subscription ["+status+"]")
	return nil
}

// Client request to delete ams service
func delAmsService(serviceId string) error {
	resp, err := amsClient.AmsiApi.AppMobilityServiceByIdDELETE(context.TODO(), serviceId)
	status := strconv.Itoa(resp.StatusCode)
	if err != nil {
		appActivityLogs = append(appActivityLogs, "Delete AMS resource ["+status+"]")
		log.Error("Failed to cleared ams service ", resp.Status)
		return err
	}
	appActivityLogs = append(appActivityLogs, "Delete AMS resource ["+status+"]")
	return nil
}

// REST API retrieve app instance info
func demo4GetPlatformInfo(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/json; charset=UTF-8")
+20 −2
Original line number Diff line number Diff line
@@ -27,6 +27,9 @@ import (
	"net/http"
	"strings"

	httpLog "github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-http-logger"
	met "github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-metrics"

	"github.com/gorilla/mux"
)

@@ -40,10 +43,13 @@ type Route struct {
type Routes []Route

func NewRouter() *mux.Router {
	var handler http.Handler
	router := mux.NewRouter().StrictSlash(true)
	for _, route := range routes {
		var handler http.Handler = route.HandlerFunc
		handler = route.HandlerFunc
		handler = Logger(handler, route.Name)
		handler = met.MetricsHandler(handler, "", moduleName)
		handler = httpLog.LogRx(handler, "")

		router.
			Methods(route.Method).
@@ -52,7 +58,19 @@ func NewRouter() *mux.Router {
			Handler(handler)
	}

	router.PathPrefix("/").Handler(http.StripPrefix("/", http.FileServer(http.Dir("./static/"))))
	// Path prefix router order is important
	// Service Api files
	handler = http.StripPrefix("/demo4-ue/api/", http.FileServer(http.Dir("./api/")))
	router.
		PathPrefix("/demo4-ue/api/").
		Name("Api").
		Handler(handler)
	// User supplied service API files
	handler = http.StripPrefix("/demo4-ue/user-api/", http.FileServer(http.Dir("./user-api/")))
	router.
		PathPrefix("/demo4-ue/user-api/").
		Name("UserApi").
		Handler(handler)

	return router
}
Loading