Commit 730da5c3 authored by Yann Garcia's avatar Yann Garcia
Browse files

Enhance demo6 example

parent 7661bd95
Loading
Loading
Loading
Loading
+4 −0
Changes for examples/demo6/golang/app_instance.yaml: 4 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -12,3 +12,7 @@ mecplatform: 'mep1'
localurl: 'http://'
# Set host port number of demo-6. Example field: '8093'
port: '80'
# Callback base URL
callbackUrl: 'http://mec-platform2.etsi.org'
# Callback port for listener
callbackPort: '31111'
+63 −29
Changes for examples/demo6/golang/main.go: 63 added lines, 29 removed lines.
Original line number Diff line number Diff line
@@ -59,6 +59,8 @@ type Config struct {
	AppInstanceId string `mapstructure:"appid"`
	Localurl      string `mapstructure:"localurl"`
	Port          string `mapstructure:"port"`
	CallbackUrl   string `mapstructure:"callbackUrl"`
	CallbackPort  string `mapstructure:"callbackPort"`
}

// MEC 011 registration
@@ -179,7 +181,7 @@ type UeContext struct {
var (
	dir                       string
	fileName                  string
	provider                  string = "github" //"Jupyter2024"
	provider                  string = "Jupyter2024" //"github"
	run                       bool   = true
	done                      chan bool
	cfg                       *client.Configuration = nil
@@ -190,7 +192,8 @@ var (
	userCode                  string                = ""
	mecUrl                    string                = ""
	mecPlateform              string                = ""
	callbackUrl               string                = "http://yanngarcia.ddns.net"
	callbackUrl               string                = ""
	callbackPort              string                = ""
	terminationSubscriptionID string                = ""
	scenarios                 []client.SandboxNetworkScenario
	scenario                  []client.Scenario
@@ -324,7 +327,7 @@ func logout() error {
	// Delete subscriptions done
	if len(subscriptions) != 0 {
		for _, l := range subscriptions {
			delete_subscription(l)
			delete_subscription(l.Href)
		} // End of 'for' statement
	}

@@ -642,22 +645,6 @@ func mec011_send_subscribe_termination() (subId string, response *http.Response,
	return subId, response, nil
}

func extract_subscription_id(base_url string, subscription_url string) (string, error) {
	fmt.Println(">>> extract_subscription_id: base_url: " + base_url)
	fmt.Println(">>> extract_subscription_id: subscription_url: " + subscription_url)

	re := regexp.MustCompile(base_url + "/(?P<sub_id>.+)")
	if re == nil {
		return "", errors.New("Regexp creation failure")
	}
	matches := re.FindStringSubmatch(subscription_url)
	if matches == nil {
		return "", errors.New("Matching failure")
	}

	return matches[re.SubexpIndex("sub_id")], nil
}

func delete_termination_subscription() (err error) {
	fmt.Println(">>> delete_termination_subscription")

@@ -921,7 +908,7 @@ func send_mec_service_request(method string, path string, body io.Reader, vars u
	// Add the user agent to the request.
	localVarRequest.Header.Add("User-Agent", cfg.UserAgent)

	fmt.Println("send_mec_service_request: localVarRequest: ", localVarRequest)
	//fmt.Println("send_mec_service_request: localVarRequest: ", localVarRequest)

	// tr := &http.Transport{
	// 	TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
@@ -993,6 +980,12 @@ func mec030_subscribe_v2x_messages() (body []byte, response *http.Response, err
	}
	defer response.Body.Close()

	err = json.Unmarshal([]byte(body), &v2xMsgSubscription)
	if err != nil {
		return nil, nil, err
	}
	subscriptions = append(subscriptions, *v2xMsgSubscription.Links.Self)

	return body, response, nil
}

@@ -1011,7 +1004,7 @@ func mec030_delete_v2x_messages_subscription(choice string) (response *http.Resp
	url := mecUrl + "/" + sandboxName + "/" + mecPlateform + "/vis/v2/subscriptions/" + choice
	fmt.Println("mec030_delete_v2x_messages_subscription: url: " + url)
	// Send request and await response
	_, response, err = send_mec_service_request(http.MethodDelete, url, nil, nil, nil, nil)
	response, err = delete_subscription(url)
	if err != nil {
		return nil, err
	}
@@ -1119,6 +1112,12 @@ func mec40_create_subscription() (body []byte, response *http.Response, err erro
	}
	defer response.Body.Close()

	err = json.Unmarshal([]byte(body), &systemUpdateNotificationSubscription)
	if err != nil {
		return nil, nil, err
	}
	subscriptions = append(subscriptions, *systemUpdateNotificationSubscription.Links.Self)

	return body, response, nil
}

@@ -1144,7 +1143,6 @@ func mec40_get_subscriptions(choice []string) (body []byte, response *http.Respo
	defer response.Body.Close()

	return body, response, nil
	return nil, nil, errors.New("not implemented")
}

func mec40_delete_subscriptions(choice string) (response *http.Response, err error) {
@@ -1162,7 +1160,7 @@ func mec40_delete_subscriptions(choice string) (response *http.Response, err err
	url := mecUrl + "/" + sandboxName + "/" + mecPlateform + "/fed_enablement/v1/subscriptions/" + choice
	fmt.Println("mec40_delete_subscriptions: url: " + url)
	// Send request and await response
	_, response, err = send_mec_service_request(http.MethodDelete, url, nil, nil, nil, nil)
	response, err = delete_subscription(url)
	if err != nil {
		return nil, err
	}
@@ -1194,20 +1192,53 @@ func app_status() (resp string) {
	} else {
		resp += ", Demo6 app not registered"
	}
	if len(subscriptions) != 0 {
		resp += ", #" + strconv.Itoa(len(subscriptions)) + " subscriptions created"
	} else {
		resp += ", no subscriptions created"
	}

	return resp
}

func delete_subscription(link LinkType) (err error) {
	fmt.Println(">>> delete_subscription: ", link)
func extract_subscription_id(base_url string, subscription_url string) (string, error) {
	fmt.Println(">>> extract_subscription_id: base_url: " + base_url)
	fmt.Println(">>> extract_subscription_id: subscription_url: " + subscription_url)

	re := regexp.MustCompile(base_url + "/(?P<sub_id>.+)")
	if re == nil {
		return "", errors.New("Regexp creation failure")
	}
	matches := re.FindStringSubmatch(subscription_url)
	if matches == nil {
		return "", errors.New("Matching failure")
	}

	return matches[re.SubexpIndex("sub_id")], nil
}

func delete_subscription(url string) (response *http.Response, err error) {
	fmt.Println(">>> delete_subscription: ", url)

	// Send request and await response
	_, _, err = send_mec_service_request(http.MethodDelete, link.Href, nil, nil, nil, nil)
	_, response, err = send_mec_service_request(http.MethodDelete, url, nil, nil, nil, nil)
	if err != nil {
		return err
		return nil, err
	}

	return nil
	delete_subscription_from_list(url)

	return response, nil
}

func delete_subscription_from_list(url string) {
	fmt.Println(">>> delete_subscription: ", url)
	for s, l := range subscriptions {
		if l.Href == url {
			subscriptions = append(subscriptions[:s], subscriptions[s+1:]...)
			break
		}
	}
}

func main() {
@@ -1256,6 +1287,9 @@ func main() {
	fmt.Println("mecUrl: " + mecUrl)
	fmt.Println("mecPlateform: " + mecPlateform)

	callbackUrl = config.CallbackUrl + ":" + config.CallbackPort
	fmt.Println("callbackUrl: " + callbackUrl)

	cfg = client.NewConfiguration()
	if cfg == nil {
		fmt.Errorf("Failed to create client configuration!")
@@ -1305,7 +1339,7 @@ func main() {
		router := NewRouter()
		methods := handlers.AllowedMethods([]string{"OPTIONS", "DELETE", "GET", "HEAD", "POST", "PUT"})
		header := handlers.AllowedHeaders([]string{"content-type"})
		fmt.Println(http.ListenAndServe(":80", handlers.CORS(methods, header)(router)))
		fmt.Println(http.ListenAndServe(":"+config.CallbackPort, handlers.CORS(methods, header)(router)))
		run = false
		fmt.Println("<<< Stop REST API Server")
	}()