Commit 84415a1f authored by supermikii's avatar supermikii
Browse files

modify demo service to use global mec service

parent c2e2e8ba
Loading
Loading
Loading
Loading
+6 −6
Original line number Original line Diff line number Diff line
@@ -4,7 +4,7 @@ set -e
echo "mode: advantedge" >app_instance.yaml
echo "mode: advantedge" >app_instance.yaml
echo "sandbox:" >>app_instance.yaml
echo "sandbox:" >>app_instance.yaml
echo "mecplatform: ${MEEP_MEP_NAME}" >>app_instance.yaml
echo "mecplatform: ${MEEP_MEP_NAME}" >>app_instance.yaml
echo "appid:" >> app_instance.yaml
echo "appid:" ${MEEP_INSTANCE_ID} >>app_instance.yaml
echo "localurl: ${MEEP_POD_NAME}" >>app_instance.yaml
echo "localurl: ${MEEP_POD_NAME}" >>app_instance.yaml
echo "port:" >>app_instance.yaml
echo "port:" >>app_instance.yaml


+56 −67
Original line number Original line Diff line number Diff line
@@ -23,7 +23,6 @@ import (
	"fmt"
	"fmt"
	"net/http"
	"net/http"
	"net/url"
	"net/url"
	"os"
	"strconv"
	"strconv"
	"strings"
	"strings"
	"sync"
	"sync"
@@ -57,6 +56,7 @@ var amsClient *ams.APIClient
var amsResourceId string
var amsResourceId string
var amsTargetId string
var amsTargetId string
var orderedAmsAdded = []string{}
var orderedAmsAdded = []string{}
var amsServiceName string


var svcSubscriptionSent bool
var svcSubscriptionSent bool
var appTerminationSent bool
var appTerminationSent bool
@@ -172,41 +172,39 @@ func Init(envPath string, envName string) (port string, err error) {


	// Retrieve mec platform name
	// Retrieve mec platform name
	mep = config.MecPlatform
	mep = config.MecPlatform
	instanceName = config.AppInstanceId

	// If demo3 starts on advantedge then get resource node name from sbx controller
	if environment == "advantedge" {
		sandBoxClientCfg := sbx.NewConfiguration()
		sandBoxClientCfg.BasePath = sbxCtrlUrl + "/sandbox-ctrl/v1"
		sandBoxClient = sbx.NewAPIClient(sandBoxClientCfg)
		if sandBoxClient == nil {
			return "", errors.New("Failed to create Sandbox Controller REST API client")
		}
		appInfo, err := getApplicationInfo(instanceName)
		if err != nil {
			return "", errors.New("Failed to retrieve mec application resource")
		}
		mep = appInfo.NodeName
	}


	// Setup application support client & service management client
	// Setup application support client & service management client
	appSupportClientCfg := asc.NewConfiguration()
	appSupportClientCfg := asc.NewConfiguration()
	srvMgmtClientCfg := smc.NewConfiguration()
	srvMgmtClientCfg := smc.NewConfiguration()
	if environment == "advantedge" {
	if environment == "advantedge" {
		if mep != "" {
		if config.MecPlatform != "" {
			appSupportClientCfg.BasePath = "http://" + mep + "-meep-app-enablement" + "/mec_app_support/v1"
			appSupportClientCfg.BasePath = "http://" + mep + "-meep-app-enablement" + "/mec_app_support/v1"
			srvMgmtClientCfg.BasePath = "http://" + mep + "-meep-app-enablement" + "/mec_service_mgmt/v1"
			srvMgmtClientCfg.BasePath = "http://" + mep + "-meep-app-enablement" + "/mec_service_mgmt/v1"
		} else {
		} else {
			appSupportClientCfg.BasePath = "http://meep-app-enablement/mec_app_support/v1"
			appSupportClientCfg.BasePath = "http://meep-app-enablement/mec_app_support/v1"
			srvMgmtClientCfg.BasePath = "http://meep-app-enablement/mec_service_mgmt/v1"
			srvMgmtClientCfg.BasePath = "http://meep-app-enablement/mec_service_mgmt/v1"
			mep = os.Getenv("MEEP_MEP_NAME")
		}
		}
	} else {
	} else {
		appSupportClientCfg.BasePath = mecUrl + "/mec_app_support/v1"
		appSupportClientCfg.BasePath = mecUrl + "/mec_app_support/v1"
		srvMgmtClientCfg.BasePath = mecUrl + "/mec_service_mgmt/v1"
		srvMgmtClientCfg.BasePath = mecUrl + "/mec_service_mgmt/v1"
	}
	}


	// If demo3 starts on advantedge then create a mec application resource
	if environment == "advantedge" {
		sandBoxClientCfg := sbx.NewConfiguration()
		sandBoxClientCfg.BasePath = sbxCtrlUrl + "/sandbox-ctrl/v1"
		sandBoxClient = sbx.NewAPIClient(sandBoxClientCfg)
		if sandBoxClient == nil {
			return "", errors.New("Failed to create Sandbox Controller REST API client")
		}
		instanceId, err := getAppInstanceId()
		if err != nil {
			return "", errors.New("Failed to register mec application resource")
		}
		instanceName = instanceId
	} else {
		instanceName = config.AppInstanceId
	}

	// Create app enablement client
	// Create app enablement client
	appSupportClient = asc.NewAPIClient(appSupportClientCfg)
	appSupportClient = asc.NewAPIClient(appSupportClientCfg)
	appSupportClientPath = appSupportClientCfg.BasePath
	appSupportClientPath = appSupportClientCfg.BasePath
@@ -236,25 +234,14 @@ func Init(envPath string, envName string) (port string, err error) {
	return localPort, nil
	return localPort, nil
}
}


// Create a mec resource on platform then return app id
func getApplicationInfo(appId string) (appInfo sbx.ApplicationInfo, err error) {
func getAppInstanceId() (id string, err error) {
	appInfo, _, err = sandBoxClient.ApplicationsApi.ApplicationsAppInstanceIdGET(context.TODO(), appId)
	var appInfo sbx.ApplicationInfo
	appInfo.Name = serviceCategory
	appInfo.MepName = mep
	appInfo.Version = serviceAppVersion
	appType := sbx.USER_ApplicationType
	appInfo.Type_ = &appType
	state := sbx.INITIALIZED_ApplicationState
	appInfo.State = &state
	response, _, err := sandBoxClient.ApplicationsApi.ApplicationsPOST(context.TODO(), appInfo)
	if err != nil {
	if err != nil {
		demoRegisteratonStatus = "500"
		log.Info("Failed to retrieve mec application resource ", err)
		log.Error("Failed to get App Instance ID with error: ", err)
		return appInfo, err
		return "", err
	}
	}
	// Store app activity log demo3 application successfully registered


	return response.Id, nil
	return appInfo, nil
}
}


// REST API - Demo3 confirm acknowledgement, create ams resource & subscriptions & mec service
// REST API - Demo3 confirm acknowledgement, create ams resource & subscriptions & mec service
@@ -272,15 +259,15 @@ func demo3Register(w http.ResponseWriter, r *http.Request) {
		terminalDevices = make(map[string]string)
		terminalDevices = make(map[string]string)
		demoAppInfo.DiscoveredServices = []ApplicationInstanceDiscoveredServices{}
		demoAppInfo.DiscoveredServices = []ApplicationInstanceDiscoveredServices{}


		appActivityLogs = append(appActivityLogs, "=== Register Demo3 MEC Application ["+demoRegisteratonStatus+"]")

		// Send confirm ready
		// Send confirm ready
		err := sendReadyConfirmation(instanceName)
		err := sendReadyConfirmation(instanceName)
		if err != nil {
		if err != nil {
			// Add to activity log for error indicator
			// Add to activity log for error indicator
			appActivityLogs = append(appActivityLogs, "=== Register Demo3 MEC Application [200]")
			http.Error(w, err.Error(), http.StatusInternalServerError)
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
			return
		}
		}
		appActivityLogs = append(appActivityLogs, "=== Register Demo3 MEC Application [200]")
		demoAppInfo.MecReady = true
		demoAppInfo.MecReady = true


		// Retrieve mec services
		// Retrieve mec services
@@ -350,12 +337,14 @@ func demo3Register(w http.ResponseWriter, r *http.Request) {
			ConsumedLocalOnly: true,
			ConsumedLocalOnly: true,
		}
		}


		// Check if ams service is available after polling
		if environment == "advantedge" {
		var amsUrl = mecServicesMap["mec021-1"]
			amsServiceName = "meep-ams"
		} else {
			amsServiceName = "mec021-1"
		}
		var amsUrl = mecServicesMap[amsServiceName]
		var amsSubscription ApplicationInstanceAmsLinkListSubscription
		var amsSubscription ApplicationInstanceAmsLinkListSubscription


		// Add AMS if exists
		if amsUrl != "" {
		amsClientcfg := ams.NewConfiguration()
		amsClientcfg := ams.NewConfiguration()
		amsClientcfg.BasePath = amsUrl
		amsClientcfg.BasePath = amsUrl
		amsClient = ams.NewAPIClient(amsClientcfg)
		amsClient = ams.NewAPIClient(amsClientcfg)
@@ -383,7 +372,7 @@ func demo3Register(w http.ResponseWriter, r *http.Request) {


			amsSubscriptionSent = true
			amsSubscriptionSent = true
		}
		}
		}

		subscriptions.AmsLinkListSubscription = &amsSubscription
		subscriptions.AmsLinkListSubscription = &amsSubscription


		demoAppInfo.Subscriptions = &subscriptions
		demoAppInfo.Subscriptions = &subscriptions
@@ -474,7 +463,7 @@ func demo3UpdateAmsDevices(w http.ResponseWriter, r *http.Request) {
		device := vars["device"]
		device := vars["device"]


		// Check if ams is available by checking discovered services
		// Check if ams is available by checking discovered services
		amsUrl := mecServicesMap["mec021-1"]
		amsUrl := mecServicesMap[amsServiceName]
		if amsUrl == "" {
		if amsUrl == "" {
			log.Info("Could not find ams services from available services ")
			log.Info("Could not find ams services from available services ")
			appActivityLogs = append(appActivityLogs, "Could not find AMS service")
			appActivityLogs = append(appActivityLogs, "Could not find AMS service")