Unverified Commit 521551ac authored by Kevin Di Lallo's avatar Kevin Di Lallo Committed by GitHub
Browse files

Merge pull request #336 from supermikii/my_sp47_prcomment

Demo3 Minor Fix
parents e599f736 9591b1d6
Loading
Loading
Loading
Loading
+3 −4
Original line number Diff line number Diff line
@@ -42,7 +42,7 @@ The following steps need to be done prior to running demo 3

| Operation: | Notes: |
| ---------  | ------ |
| 0a. Build demo3 server and frontend by invoking `Advantedge/example/demo3/build-demo3.sh` |  |
| 0a. Build demo3 server and frontend by invoking `Advantedge/examples/demo3/build-demo3.sh` |  |
| 0b. Create work directories of your choice on the system of your choosing; we'll use `~/demo3-mep1` and `~/demo3-mep2` for this example and create a folder named `static` inside each one of the folders. The structure should look like this <br>
     ├── demo3-mep1
          ├── static
@@ -52,7 +52,7 @@ The following steps need to be done prior to running demo 3

|  |  |
| ---------  | ------ |
| 0c. For each application instance, copy Demo3 server (`/AdvantEDGE/example/demo3/bin/demo-server/demo-server`) in the work directories and copy Demo3 frontend bundle `/AdvantEDGE/example/demo3/bin/demo-frontend/*` in the static folder <br> The resulting should look like this <br>
| 0c. For each application instance, copy Demo3 server (`/AdvantEDGE/examples/demo3/bin/demo-server/demo-server`) in the work directories and copy Demo3 frontend bundle `/AdvantEDGE/example/demo3/bin/demo-frontend/*` in the static folder <br> The resulting should look like this <br>
     ├── demo3-mep1
          ├── demo-server
          ├── static
@@ -118,7 +118,7 @@ The following steps need to be done prior to using this scenario
#### Configure demo frontend and obtain binaries
| Operation | Notes |   
| ---------  | ------ |
| 0. Build and dockerize demo3 by invoking `AdvantEDGE/example/demo3/build-demo3.sh` and `AdvantEDGE/example/demo3/dockerize.sh`|  |
| 0. Build and dockerize demo3 by invoking `AdvantEDGE/examples/demo3/build-demo3.sh` and `AdvantEDGE/examples/demo3/dockerize.sh`|  |
| 1. Import provided `demo3-scenario.yaml` in AdvantEDGE and save it | |
| 2. Deploy `demo3-scenario` from the frontend | this scenario uses geo-localization, therefore it is necessary to provision a map as described [here](https://interdigitalinc.github.io/AdvantEDGE/docs/overview/features/gis/#map-provisioning) |

@@ -137,4 +137,3 @@ The scenario is composed of the following components:
  - Zone1 has 1 Edge node
  - Zone2&3 has 1 Edge node
- 3 UEs with pre-defined route will move interchangeably from mep 1 to mep2
+178 −398

File changed.

Preview size limit exceeded, changes collapsed.

+105 −110
Original line number Diff line number Diff line
@@ -107,7 +107,6 @@ func startTicker() {
			// Increment terminal device state by 1
			for _, device := range trackDevices {
				terminalDeviceState[device] += 1
				log.Info(terminalDeviceState[device])
				stateAsString := strconv.Itoa(terminalDeviceState[device])
				terminalDevices[device] = device + " using this instance" + "(state=" + stateAsString + ")"
			}
@@ -766,6 +765,7 @@ func amsNotificationCallback(w http.ResponseWriter, r *http.Request) {
	decoder := json.NewDecoder(r.Body)
	err := decoder.Decode(&amsNotification)
	if err != nil {
		log.Error(err)
		w.WriteHeader(http.StatusInternalServerError)
		return
	}
@@ -774,39 +774,37 @@ func amsNotificationCallback(w http.ResponseWriter, r *http.Request) {
	targetDevice := amsNotification.AssociateId[0].Value
	var notifyUrl string

	// Find the target svc id should be same svc name as demo3
	var targetSvcId string
	for _, v := range demoAppInfo.DiscoveredServices {
		if v.SerName == serviceCategory && v.SerInstanceId != demoAppInfo.OfferedService.Id {
			log.Info("Target service id:", v.SerInstanceId)
			targetSvcId = v.SerInstanceId
			break
		}
	}

	// Retrieve all mec service on target ams application
	serviceInfo, _, err := srvMgmtClient.MecServiceMgmtApi.ServicesServiceIdGET(context.TODO(), targetSvcId)
	// Retrieve service on target ams application
	serviceInfo, _, err := srvMgmtClient.MecServiceMgmtApi.AppServicesGET(context.TODO(), amsTargetId, nil)
	if err != nil {
		log.Error("Failed to get target app mec service resource on mec platform", err.Error())
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	// Check if svc info struct is empty
	if (smc.ServiceInfo{} == serviceInfo) {
		log.Error("Cannot find target service")
	// Check if service is empty
	if len(serviceInfo) == 0 {
		log.Error("Cannot find target service for AMS")
		// Does not perform the transfer
		w.WriteHeader(http.StatusOK)
		return
	} else {
		notifyUrl = serviceInfo.TransportInfo.Endpoint.Uris[0]
		notifyUrl = serviceInfo[0].TransportInfo.Endpoint.Uris[0]
	}

	// If demo3 service does not exists return
	if notifyUrl == "" {
		w.WriteHeader(http.StatusOK)
	if notifyUrl != "" {
		log.Info("AMS event received for ", amsNotification.AssociateId[0].Value, " moved to app ", amsTargetId)

		// Sent context transfer with ams state object
		err = sendContextTransfer(notifyUrl, amsNotification.AssociateId[0].Value, amsNotification.TargetAppInfo.AppInstanceId)
		if err != nil {
			appActivityLogs = append(appActivityLogs, "AMS event: transfer "+targetDevice+" context to "+
				amsTargetId+" [500]")
			log.Error("Failed to transfer context")
			return
		}

		// Remove device from terminal devices using this instances so it no longer increments state
	// triggered when receives ams mobility notification
		for i, v := range trackDevices {
			if v == targetDevice {
				if i < len(trackDevices)-1 {
@@ -816,31 +814,12 @@ func amsNotificationCallback(w http.ResponseWriter, r *http.Request) {
					trackDevices = trackDevices[:len(trackDevices)-1]
				}
			}

		}

		counter := strconv.Itoa(terminalDeviceState[targetDevice])

	// Transfer only if ams target service is found
	// Update Activity Logs
	log.Info("AMS event received for ", amsNotification.AssociateId[0].Value, " moved to app ", amsTargetId)

	if notifyUrl != "" {

		// Update ams pane
		terminalDevices[targetDevice] = amsNotification.AssociateId[0].Value + " transferred to " + amsTargetId + " (state=" + counter + ")"

		// Sent context transfer with ams state object
		contextErr := sendContextTransfer(notifyUrl, amsNotification.AssociateId[0].Value, amsNotification.TargetAppInfo.AppInstanceId)
		if contextErr != nil {
			appActivityLogs = append(appActivityLogs, "AMS event: transfer "+targetDevice+" context to "+
				amsTargetId+"[500]")
			log.Error("Failed to transfer context")
			return
		}

		targetDevice := amsNotification.AssociateId[0].Value

		// Retrieve AMS Resource Information
		amsResource, _, err := amsClient.AmsiApi.AppMobilityServiceByIdGET(context.TODO(), amsResourceId)
		if err != nil {
@@ -856,71 +835,88 @@ func amsNotificationCallback(w http.ResponseWriter, r *http.Request) {
			fmt.Fprintf(w, "Could not update ams")
			return
		}

	}

	appActivityLogs = append(appActivityLogs, "AMS event: transfer "+targetDevice+" context to "+
		amsTargetId+" [200]")

	w.WriteHeader(http.StatusOK)
}

	w.WriteHeader(http.StatusOK)
// Add to tracking device if device not exists then return false
// Otherwise true
func addToTrackingDevices(device string) bool {
	for _, v := range trackDevices {
		if device == v {
			return true
		}
	}

	trackDevices = append(trackDevices, device)

	return false
}

func addToAmsKey(device string) {
	for _, v := range orderedAmsAdded {
		if device == v {
			return
		}
	}
	orderedAmsAdded = append(orderedAmsAdded, device)
}

// Rest API handle context state transfer
// Start incrementing terminal device state
func stateTransferPOST(w http.ResponseWriter, r *http.Request) {
	log.Info("Recevied AMS context transfer")
	mutex.Lock()
	defer mutex.Unlock()
	log.Info("Received AMS context transfer")

	var targetContextState ApplicationContextState

	decoder := json.NewDecoder(r.Body)
	err := decoder.Decode(&targetContextState)
	counter := strconv.Itoa(targetContextState.Counter)
	if err != nil {
		log.Error(err.Error())
		appActivityLogs = append(appActivityLogs, "=== Receive device "+targetContextState.Device+" context (state="+counter+") [500]")
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	// Add terminal device if not added
	if _, ok := terminalDevices[targetContextState.Device]; !ok {
	// Retrieve AMS Resource
	amsResourceBody, _, err := amsClient.AmsiApi.AppMobilityServiceByIdGET(context.TODO(), amsResourceId)
	if err != nil {
			w.WriteHeader(http.StatusInternalServerError)
			fmt.Fprintf(w, "Could not retrieve ams resource")
		log.Error(err.Error())
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	// Update AMS Resource
		_, amsUpdateError := amsAddDevice(amsResourceId, amsResourceBody, targetContextState.Device)
		if amsUpdateError != nil {
			w.WriteHeader(http.StatusInternalServerError)
			fmt.Fprintf(w, "Could not add ams device")
	_, err = amsAddDevice(amsResourceId, amsResourceBody, targetContextState.Device)
	if err != nil {
		log.Error(err.Error())
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

		// Add terminal device into an ordered array
		orderedAmsAdded = append(orderedAmsAdded, targetContextState.Device)

	// Update ams subscription
	amsSubscription, _, err := amsClient.AmsiApi.SubByIdGET(context.TODO(), demoAppInfo.Subscriptions.AmsLinkListSubscription.SubId)
	if err != nil {
			w.WriteHeader(http.StatusInternalServerError)
			fmt.Fprintf(w, "Could not retrieve ams subscription")
			appActivityLogs = append(appActivityLogs, "Add "+targetContextState.Device+" to AMS resource [500]")
		log.Error(err.Error())
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

		_, updateAmsError := updateAmsSubscription(demoAppInfo.Subscriptions.AmsLinkListSubscription.SubId, targetContextState.Device, amsSubscription)
		if updateAmsError != nil {
			w.WriteHeader(http.StatusInternalServerError)
			fmt.Fprintf(w, "Could not add ams subscription")
			appActivityLogs = append(appActivityLogs, "Add "+targetContextState.Device+" to AMS resource [500]")
	_, err = updateAmsSubscription(demoAppInfo.Subscriptions.AmsLinkListSubscription.SubId, targetContextState.Device, amsSubscription)
	if err != nil {
		log.Error(err.Error())
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

		// Update ams pan// Default device status & state set to 0
		trackDevices = append(trackDevices, targetContextState.Device)

	}
	addToAmsKey(targetContextState.Device)
	addToTrackingDevices(targetContextState.Device)

	terminalDeviceState[targetContextState.Device] = targetContextState.Counter

@@ -972,15 +968,14 @@ func sendContextTransfer(notifyUrl string, device string, targetId string) error
		log.Error("Failed to marshal context state ", err.Error())
		return err
	}
	resp, contextErr := http.Post(notifyUrl, "application/json", bytes.NewBuffer(jsonCounter))
	status := strconv.Itoa(resp.StatusCode)
	counter := strconv.Itoa(contextState.Counter)

	if contextErr != nil {
		log.Error(resp.Status, contextErr.Error())
		appActivityLogs = append(appActivityLogs, "=== Send device "+device+" context (state="+counter+") ["+status+"]")
	resp, err := http.Post(notifyUrl, "application/json", bytes.NewBuffer(jsonCounter))
	if err != nil {
		log.Error(err.Error())
		appActivityLogs = append(appActivityLogs, "=== Send device "+device+" context (state="+counter+") [500]")
		return err
	}
	status := strconv.Itoa(resp.StatusCode)

	appActivityLogs = append(appActivityLogs, "=== Send device "+device+" context (state="+counter+") ["+status+"]")