Commit 5ec345ec authored by Yann Garcia's avatar Yann Garcia
Browse files

Add MEC service add/delete features

parent f528a931
Loading
Loading
Loading
Loading
+104 −34
Original line number Diff line number Diff line
@@ -76,11 +76,41 @@ type AppInfo struct {
type ServiceInfo struct {
	SerInstanceId string        `json:"serInstanceId,omitempty"`
	SerName       string        `json:"serName"`
	SerCategory   string `json:"serCategory,omitempty"`
	Version       string        `json:"version"` // Service version
	State         string        `json:"state"`
	TransportInfo TransportInfo `json:"transportInfo"`
	Serializer    string        `json:"serializer"`
	Links         Links  `json:"_links"`
	Links         *Links        `json:"_links,omitempty"`
}
type TransportInfo struct {
	Id       string                      `json:"id"`
	Name     string                      `json:"name"`
	Type_    *string                     `json:"type"`
	Protocol string                      `json:"protocol"`
	Version  string                      `json:"version"`
	Endpoint *OneOfTransportInfoEndpoint `json:"endpoint"`
}
type OneOfTransportInfoEndpoint struct {
	EndPointInfoUris
	EndPointInfoFqdn
	EndPointInfoAddresses
	EndPointInfoAlternative
}
type EndPointInfoUris struct {
	Uris []string `json:"uris"`
}
type EndPointInfoFqdn struct {
	Fqdn []string `json:"fqdn"`
}
type EndPointInfoAddress struct {
	Host string `json:"host"`
	Port int32  `json:"port"`
}
type EndPointInfoAddresses struct {
	Addresses []EndPointInfoAddress `json:"addresses"`
}
type EndPointInfoAlternative struct {
	Alternative *interface{} `json:"alternative"`
}

// MEC 011 Termination subscription
@@ -129,7 +159,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
@@ -224,6 +254,7 @@ func login() (string, string, error) {
	// Initialize g;lobal variables
	scenarioId = -1
	appsInfo.Id = ""
	terminationSubscriptionID = ""
	appServiceInfo.SerInstanceId = ""

	sandbox, _, err := cl.AuthorizationApi.Login(context.TODO(), provider)
@@ -262,6 +293,11 @@ func logout() error {
		delete_termination_subscription()
	}

	// Delete MEC service if any
	if appServiceInfo.SerInstanceId != "" {
		mec011_delete_service()
	}

	// Delete registration if any
	if isRegistered {
		mec011_send_deregistration()
@@ -549,17 +585,28 @@ func mec011_send_subscribe_termination() (subId string, response *http.Response,
	fmt.Println("mec011_send_subscribe_termination: response: " + response.Status)
	fmt.Println("mec011_send_subscribe_termination: Location: " + response.Header["Location"][0])

	re := regexp.MustCompile(url + "/(?P<sub_id>.+)")
	subId, err = extract_subscription_id(url, response.Header["Location"][0])
	if err != nil {
		return "", nil, err
	}

	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 "", nil, errors.New("Regexp creation failure")
		return "", errors.New("Regexp creation failure")
	}
	matches := re.FindStringSubmatch(response.Header["Location"][0])
	matches := re.FindStringSubmatch(subscription_url)
	if matches == nil {
		return "", nil, errors.New("Matching failure")
		return "", errors.New("Matching failure")
	}
	subId = matches[re.SubexpIndex("sub_id")]

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

func delete_termination_subscription() (err error) {
@@ -656,47 +703,68 @@ func mec011_send_deregistration() (response *http.Response, err error) {
	return response, nil
}

func mec011_create_service() (body []byte, response *http.Response, err error) {
func mec011_create_service() (resId string, response *http.Response, err error) {
	fmt.Println(">>> mec011_create_service")

	// Sanity checks
	if sandboxName == "" {
		return nil, nil, errors.New("No sandbox available")
		return "", nil, errors.New("No sandbox available")
	} else if appsInfo.Id == "" {
		return nil, nil, errors.New("No App instance available")
		return "", nil, errors.New("No App instance available")
	} else if appServiceInfo.SerInstanceId != "" {
		return nil, nil, errors.New("A MEC service is already created")
		return "", nil, errors.New("A MEC service is already created")
	}

	// Set URL
	url := mecUrl + "/" + sandboxName + "/" + mecPlateform + "/mec_service_mgmt/v1/applications/" + appsInfo.Id + "/services"
	fmt.Println("mec011_create_service: url: " + url)
	// Build message body
	transportType := "REST_HTTP"
	appServiceInfo = ServiceInfo{
		SerInstanceId: uuid.New().String(),
		SerName: "demo6 MEC Service",
		SerCategory:   "Game",
		Version: "1.0.0",
		State:         "demo",
		Serializer:    "JSON",
		Links: Links{
			Self: &LinkType{
				Href: "http://yanngarcia.ddns.net/location/v3/notif/1",
			},
		State:   "ACTIVE",
		TransportInfo: TransportInfo{
			Id:       "transport",
			Name:     "REST",
			Type_:    &transportType,
			Protocol: "HTTP",
			Version:  "2.0",
			Endpoint: &OneOfTransportInfoEndpoint{},
		},
		Serializer: "JSON",
	}
	appServiceInfo.TransportInfo.Endpoint.Uris = append(appServiceInfo.TransportInfo.Endpoint.Uris, callbackUrl+"/demo6svc/v1")
	json_body, err := json.Marshal(appServiceInfo)
	if err != nil {
		return nil, nil, err
		return "", nil, err
	}
	fmt.Println("mec011_create_service: json_body: " + string(json_body))
	io_body := bytes.NewReader(json_body)
	fmt.Println("mec011_create_service: json_body: ", io_body)
	// Send request and await response
	body, response, err = send_mec_service_request(http.MethodPost, url, io_body, nil, nil, nil)
	body, response, err := send_mec_service_request(http.MethodPost, url, io_body, nil, nil, nil)
	if err != nil {
		return nil, nil, err
		return "", nil, err
	}
	fmt.Println("mec011_create_service: body: " + string(body))
	if response.StatusCode != 201 {
		return "", nil, errors.New("Invalid Status: " + response.Status)
	}
	// Unmarshal the response body
	fmt.Println("mec011_create_service: body: " + string(body))
	err = json.Unmarshal([]byte(body), &appServiceInfo)
	if err != nil {
		return "", nil, err
	}

	return body, response, nil
	fmt.Println("mec011_create_service: Location: " + response.Header["Location"][0])
	resId, err = extract_subscription_id(url, response.Header["Location"][0])
	if err != nil {
		return "", nil, err
	}

	return resId, response, nil
}

func mec011_delete_service() (err error) {
@@ -720,6 +788,8 @@ func mec011_delete_service() (err error) {
		return err
	}

	appServiceInfo.SerInstanceId = ""

	return nil
}

@@ -1156,11 +1226,11 @@ func process_choice(choice []string) string {
		message = fmt.Sprintf("response body:  %s", response.Status)
	} else if strings.Compare(choice[0], MEC011_CREATE_SVC) == 0 {
		var err error
		body, _, err := mec011_create_service()
		resId, _, err := mec011_create_service()
		if err != nil {
			return err.Error()
		}
		message = fmt.Sprintf("response body:  %s", string(body))
		message = fmt.Sprintf("response body: resource id: %s", resId)
	} else if strings.Compare(choice[0], MEC011_DELETE_SVC) == 0 {
		err := mec011_delete_service()
		if err != nil {