Commit 02116676 authored by Muhammad Umair Zafar's avatar Muhammad Umair Zafar
Browse files

modify login endpoint and add namespace endpoint in the demo

parent ae1e7796
Loading
Loading
Loading
Loading
+100 −28
Original line number Diff line number Diff line
@@ -43,6 +43,8 @@ import (
	"strings"
	"syscall"
	"time"
	"github.com/gorilla/mux"
	// "crypto/tls"

	client "github.com/InterDigitalInc/AdvantEDGE/example/demo6/client"

@@ -95,13 +97,15 @@ type Links struct {
var (
	dir            	string
	fileName       	string
	provider       string = "gitlab"
	provider       	string = "github"
	run            	bool   = true
	done           	chan bool
	cfg            	*client.Configuration = nil
	cl             	*client.APIClient     = nil
	reader         	*bufio.Reader         = nil
	sandboxName    	string                = ""
	verificationUri	string			 = ""
	userCode	   	string			 = ""
	scenarios      	[]client.SandboxNetworkScenario
	scenario       	[]client.Scenario
	scenarioId     	int
@@ -115,6 +119,7 @@ var (
// Display menu and read selection
const (
	LOGIN                 = "l"
	NAMESPACE			  = "n"
	LOGOUT                = "L"
	LIST_SC               = "s"
	SC                    = "S"
@@ -133,6 +138,49 @@ const (
	QUIT                  = "q"
)

type Route struct {
	Name        string
	Method      string
	Pattern     string
	HandlerFunc http.HandlerFunc
}

type Routes []Route

func NewRouter() *mux.Router {
	var handler http.Handler
	router := mux.NewRouter().StrictSlash(true)
	for _, route := range routes {
		handler = route.HandlerFunc
		router.
			Methods(route.Method).
			Path(route.Pattern).
			Name(route.Name).
			Handler(handler)
	}

	return router
}

func Index(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "Hello World!")
}

var routes = Routes{
	Route{
		"Index",
		"GET",
		"/",
		Index,
	},
	Route{
		"Index",
		"GET",
		"/demo6/v1/",
		Index,
	},
}

func clearScreen() {
	fmt.Println("\033[2J")
}
@@ -141,7 +189,7 @@ func menu(message string) []string {
	clearScreen()
	fmt.Printf(
		"Mandatory commands:\n"+
			"\t%s: Login, %s: Logout, %s: Get scenarios list\n"+
			"\t%s: Login, %s: Get Namespace, %s: Logout, %s: Get scenarios list\n"+
			"\t%s <index>: Activate a scenario, %s: Terminate a scenario\n"+
			"Optional commands:\n"+
			"\t%s <index>: Get scenario description\n"+
@@ -154,7 +202,7 @@ func menu(message string) []string {
			"MEC 030:\n"+
			"\t%s: Get V2X UU unicast setting\n"+
			"%s: Quit\n",
		LOGIN, LOGOUT, LIST_SC, ACTIVATE, DEACTIVATE, SC, LIST_SERVICES, LIST_APP, CREATE_APP, DELETE_APP, MEC011_CONFIRM_READY, MEC011_REGISTRATION, MEC011_DEREGISTRATION, MEC011_CREATE_SVC, MEC011_DELETE_SVC, MEC030_UU_SETTINGS, QUIT)
		LOGIN, NAMESPACE, LOGOUT, LIST_SC, ACTIVATE, DEACTIVATE, SC, LIST_SERVICES, LIST_APP, CREATE_APP, DELETE_APP, MEC011_CONFIRM_READY, MEC011_REGISTRATION, MEC011_DEREGISTRATION, MEC011_CREATE_SVC, MEC011_DELETE_SVC, MEC030_UU_SETTINGS, QUIT)
	if message != "" {
		fmt.Println("Last message: ", message)
	}
@@ -166,12 +214,12 @@ func menu(message string) []string {
	return strings.Split(choice, " ")
}

func login() (string, error) {
func login() (string, string, error) {
	fmt.Println(">>> login")

	// Sanity checks
	if sandboxName != "" {
		return "", errors.New("Please, logout first")
		return "", "", errors.New("Please, logout first")
	}

	// Initialize g;lobal variables
@@ -181,11 +229,24 @@ func login() (string, error) {

	sandbox, _, err := cl.AuthorizationApi.Login(context.TODO(), provider)
	if err != nil {
		return "", err
		return "", "", err
	}
	fmt.Println("login: sandbox: ", sandbox)

	return sandbox.Name, nil
	return sandbox.User_code, sandbox.Verification_uri,  nil
}

func getNamespace() (string, error) {
	fmt.Println(">>> Get Namespace")

	response, _, err := cl.AuthorizationApi.GetNamespace(context.TODO(), userCode)
	if err != nil {
		return "", err
	}
	fmt.Println("login: Namespace is: ", response)
	sandboxName = response.Sandbox_name

	return sandboxName,  nil
}

func logout() error {
@@ -589,6 +650,11 @@ func send_mec_service_request(method string, path string, body io.Reader, vars u

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

	// tr := &http.Transport{
	// 	TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
	// }
	// cfg.HTTPClient = &http.Client{Transport: tr}

	res, err = cfg.HTTPClient.Do(localVarRequest)
	if err != nil {
		return nil, nil, err
@@ -750,14 +816,20 @@ func process_choice(choice []string) string {

	var message string
	if strings.Compare(choice[0], LOGIN) == 0 {
		sandboxName, _ = login()
		message = fmt.Sprintf("Sandbox Id: %s", sandboxName)
		userCode, verificationUri, _ = login()
		message = fmt.Sprintf("User Code: %s and Verification URI is %s: ", userCode, verificationUri)
		} else if strings.Compare(choice[0], LOGOUT) == 0 {
			err := logout()
			if err != nil {
				return err.Error()
			}
			message = "Sandbox terminated"
	} else if strings.Compare(choice[0], NAMESPACE) == 0 {
		sandbox, err := getNamespace()
		if err != nil {
			return err.Error()
		}
		message = fmt.Sprintf("Sandbox ID is: %s", sandbox)
	} else if strings.Compare(choice[0], LIST_SC) == 0 {
		scenarios, _ = getListOfScenarios()
		message = fmt.Sprintf("scenarios: %s", fmt.Sprint(scenarios))
+66 −66
Original line number Diff line number Diff line
/*
 * Copyright (c) 2022  The AdvantEDGE Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * AdvantEDGE Application Mobility API
 *
 * Application Mobility Service is AdvantEDGE's implementation of [ETSI MEC ISG MEC021 Application Mobility API](http://www.etsi.org/deliver/etsi_gs/MEC/001_099/021/02.02.01_60/gs_MEC021v020201p.pdf) <p>[Copyright (c) ETSI 2017](https://forge.etsi.org/etsi-forge-copyright-notice.txt) <p>**Micro-service**<br>[meep-ams](https://github.com/InterDigitalInc/AdvantEDGE/tree/master/go-apps/meep-ams) <p>**Type & Usage**<br>Edge Service used by edge applications that want to get information about application mobility in the network <p>**Note**<br>AdvantEDGE supports a selected subset of Application Mobility API endpoints (see below).
 *
 * API version: 2.2.1
 * Contact: AdvantEDGE@InterDigital.com
 * Generated by: Swagger Codegen (https://github.com/swagger-api/swagger-codegen.git)
 */
package main
// /*
//  * Copyright (c) 2022  The AdvantEDGE Authors
//  *
//  * Licensed under the Apache License, Version 2.0 (the "License");
//  * you may not use this file except in compliance with the License.
//  * You may obtain a copy of the License at
//  *
//  *    http://www.apache.org/licenses/LICENSE-2.0
//  *
//  * Unless required by applicable law or agreed to in writing, software
//  * distributed under the License is distributed on an "AS IS" BASIS,
//  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  * See the License for the specific language governing permissions and
//  * limitations under the License.
//  *
//  * AdvantEDGE Application Mobility API
//  *
//  * Application Mobility Service is AdvantEDGE's implementation of [ETSI MEC ISG MEC021 Application Mobility API](http://www.etsi.org/deliver/etsi_gs/MEC/001_099/021/02.02.01_60/gs_MEC021v020201p.pdf) <p>[Copyright (c) ETSI 2017](https://forge.etsi.org/etsi-forge-copyright-notice.txt) <p>**Micro-service**<br>[meep-ams](https://github.com/InterDigitalInc/AdvantEDGE/tree/master/go-apps/meep-ams) <p>**Type & Usage**<br>Edge Service used by edge applications that want to get information about application mobility in the network <p>**Note**<br>AdvantEDGE supports a selected subset of Application Mobility API endpoints (see below).
//  *
//  * API version: 2.2.1
//  * Contact: AdvantEDGE@InterDigital.com
//  * Generated by: Swagger Codegen (https://github.com/swagger-api/swagger-codegen.git)
//  */
// package main

import (
	"fmt"
	"net/http"
// import (
// 	"fmt"
// 	"net/http"

	"github.com/gorilla/mux"
)
// 	"github.com/gorilla/mux"
// )

type Route struct {
	Name        string
	Method      string
	Pattern     string
	HandlerFunc http.HandlerFunc
}
// type Route struct {
// 	Name        string
// 	Method      string
// 	Pattern     string
// 	HandlerFunc http.HandlerFunc
// }

type Routes []Route
// type Routes []Route

func NewRouter() *mux.Router {
	var handler http.Handler
	router := mux.NewRouter().StrictSlash(true)
	for _, route := range routes {
		handler = route.HandlerFunc
		router.
			Methods(route.Method).
			Path(route.Pattern).
			Name(route.Name).
			Handler(handler)
	}
// func NewRouter() *mux.Router {
// 	var handler http.Handler
// 	router := mux.NewRouter().StrictSlash(true)
// 	for _, route := range routes {
// 		handler = route.HandlerFunc
// 		router.
// 			Methods(route.Method).
// 			Path(route.Pattern).
// 			Name(route.Name).
// 			Handler(handler)
// 	}

	return router
}
// 	return router
// }

func Index(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "Hello World!")
}
// func Index(w http.ResponseWriter, r *http.Request) {
// 	fmt.Fprintf(w, "Hello World!")
// }

var routes = Routes{
	Route{
		"Index",
		"GET",
		"/",
		Index,
	},
	Route{
		"Index",
		"GET",
		"/demo6/v1/",
		Index,
	},
}
// var routes = Routes{
// 	Route{
// 		"Index",
// 		"GET",
// 		"/",
// 		Index,
// 	},
// 	Route{
// 		"Index",
// 		"GET",
// 		"/demo6/v1/",
// 		Index,
// 	},
// }