Skip to content
Snippets Groups Projects
Commit 19a8499c authored by Manuel Kieweg's avatar Manuel Kieweg
Browse files

save last pnd and sbi uuid to cli config

parent 5d46189e
No related branches found
No related tags found
1 merge request!90Develop
Pipeline #67239 passed with warnings
...@@ -3,6 +3,7 @@ package cli ...@@ -3,6 +3,7 @@ package cli
import ( import (
"fmt" "fmt"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"strings" "strings"
...@@ -33,7 +34,15 @@ func HttpGet(apiEndpoint, f string, args ...string) error { ...@@ -33,7 +34,15 @@ func HttpGet(apiEndpoint, f string, args ...string) error {
if err != nil { if err != nil {
return err return err
} }
fmt.Println(string(bytes)) if f == "init" {
pnd := string(bytes[:36])
sbi := string(bytes[36:])
viper.Set("CLI_PND", pnd)
viper.Set("CLI_SBI", sbi)
return viper.WriteConfig()
} else {
fmt.Println(string(bytes))
}
case http.StatusCreated: case http.StatusCreated:
defer resp.Body.Close() defer resp.Body.Close()
bytes, err := ioutil.ReadAll(resp.Body) bytes, err := ioutil.ReadAll(resp.Body)
......
...@@ -47,6 +47,8 @@ var addDeviceCmd = &cobra.Command{ ...@@ -47,6 +47,8 @@ var addDeviceCmd = &cobra.Command{
"address="+address, "address="+address,
"password="+password, "password="+password,
"username="+username, "username="+username,
"sbi="+cliSbi,
"pnd="+cliPnd,
) )
}, },
} }
......
...@@ -31,7 +31,7 @@ POSSIBILITY OF SUCH DAMAGE. ...@@ -31,7 +31,7 @@ POSSIBILITY OF SUCH DAMAGE.
package cmd package cmd
import ( import (
"errors" "code.fbi.h-da.de/cocsn/gosdn/cli"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
...@@ -44,7 +44,7 @@ var cliCmd = &cobra.Command{ ...@@ -44,7 +44,7 @@ var cliCmd = &cobra.Command{
Short: "", Short: "",
Long: ``, Long: ``,
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
return errors.New("no subcommand provided") return cli.HttpGet(apiEndpoint, "init")
}, },
} }
...@@ -53,4 +53,5 @@ func init() { ...@@ -53,4 +53,5 @@ func init() {
cliCmd.PersistentFlags().StringVar(&uuid, "uuid", "", "uuid of the requested device") cliCmd.PersistentFlags().StringVar(&uuid, "uuid", "", "uuid of the requested device")
cliCmd.PersistentFlags().StringVar(&apiEndpoint, "api-endpoint", "http://localhost:8080", "address of the target") cliCmd.PersistentFlags().StringVar(&apiEndpoint, "api-endpoint", "http://localhost:8080", "address of the target")
} }
...@@ -45,6 +45,8 @@ var cliSetCmd = &cobra.Command{ ...@@ -45,6 +45,8 @@ var cliSetCmd = &cobra.Command{
apiEndpoint, apiEndpoint,
"set", "set",
"uuid="+uuid, "uuid="+uuid,
"cliSbi="+cliSbi,
"cliPnd="+cliPnd,
"path="+args[0], "path="+args[0],
"address="+address, "address="+address,
"value="+args[1], "value="+args[1],
......
...@@ -41,7 +41,13 @@ var getDeviceCmd = &cobra.Command{ ...@@ -41,7 +41,13 @@ var getDeviceCmd = &cobra.Command{
Short: "gets device information from the controller", Short: "gets device information from the controller",
Long: ``, Long: ``,
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
return cli.HttpGet(apiEndpoint, "getDevice", "uuid="+uuid) return cli.HttpGet(
apiEndpoint,
"getDevice",
"uuid="+uuid,
"sbi="+cliSbi,
"pnd="+cliPnd,
)
}, },
} }
......
/*
Copyright © 2021 da/net research group <danet.fbi.h-da.de>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
package cmd
import (
"code.fbi.h-da.de/cocsn/gosdn/cli"
"github.com/spf13/cobra"
)
// initCmd represents the init command
var initCmd = &cobra.Command{
Use: "init",
Short: "initialise SBI and PND",
Long: ``,
RunE: func(cmd *cobra.Command, args []string) error {
return cli.HttpGet(apiEndpoint, "init" )
},
}
func init() {
cliCmd.AddCommand(initCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// initCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// initCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
...@@ -41,7 +41,14 @@ var requestCmd = &cobra.Command{ ...@@ -41,7 +41,14 @@ var requestCmd = &cobra.Command{
Short: "requests a path from a specified device on the controller", Short: "requests a path from a specified device on the controller",
Long: ``, Long: ``,
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
return cli.HttpGet(apiEndpoint, "request", "uuid="+uuid, "path="+args[0]) return cli.HttpGet(
apiEndpoint,
"request",
"uuid="+uuid,
"sbi="+cliSbi,
"pnd="+cliPnd,
"path="+args[0],
)
}, },
} }
......
...@@ -41,7 +41,13 @@ var requestAllCmd = &cobra.Command{ ...@@ -41,7 +41,13 @@ var requestAllCmd = &cobra.Command{
Short: "requests specified path from all devices on the controller", Short: "requests specified path from all devices on the controller",
Long: ``, Long: ``,
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
return cli.HttpGet(apiEndpoint, "requestAll", "path="+args[0]) return cli.HttpGet(
apiEndpoint,
"requestAll",
"sbi="+cliSbi,
"pnd="+cliPnd,
"path="+args[0],
)
}, },
} }
......
...@@ -46,12 +46,14 @@ var password string ...@@ -46,12 +46,14 @@ var password string
var address string var address string
var loglevel string var loglevel string
var grpcPort string var grpcPort string
var cliPnd string
var cliSbi string
// rootCmd represents the base command when called without any subcommands // rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{ var rootCmd = &cobra.Command{
Use: "gosdn", Use: "gosdn",
Short: "starts the gosdn controller", Short: "starts the gosdn controller",
Long: `Set GOSDN_DEBUG environment variable to enalbe debug logging.`, Long: `Set GOSDN_DEBUG environment variable to enable debug logging.`,
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
...@@ -104,6 +106,8 @@ func initConfig() { ...@@ -104,6 +106,8 @@ func initConfig() {
} }
viper.SetDefault("socket", ":"+grpcPort) viper.SetDefault("socket", ":"+grpcPort)
cliPnd = viper.GetString("CLI_PND")
cliSbi = viper.GetString("CLI_SBI")
loglevel = viper.GetString("GOSDN_LOG") loglevel = viper.GetString("GOSDN_LOG")
log.SetReportCaller(true) log.SetReportCaller(true)
......
...@@ -15,6 +15,8 @@ const basePath = "/api" ...@@ -15,6 +15,8 @@ const basePath = "/api"
// deprecated // deprecated
func httpApi() (err error) { func httpApi() (err error) {
http.HandleFunc(basePath, httpHandler) http.HandleFunc(basePath, httpHandler)
http.HandleFunc("/livez", healthCheck)
http.HandleFunc("/readyz", healthCheck)
go func() { go func() {
err = http.ListenAndServe(":8080", nil) err = http.ListenAndServe(":8080", nil)
...@@ -25,6 +27,10 @@ func httpApi() (err error) { ...@@ -25,6 +27,10 @@ func httpApi() (err error) {
return nil return nil
} }
func healthCheck(writer http.ResponseWriter, request *http.Request) {
writer.WriteHeader(http.StatusOK)
}
func httpHandler(writer http.ResponseWriter, request *http.Request) { func httpHandler(writer http.ResponseWriter, request *http.Request) {
log.WithFields(log.Fields{ log.WithFields(log.Fields{
"request": request, "request": request,
...@@ -46,9 +52,21 @@ func httpHandler(writer http.ResponseWriter, request *http.Request) { ...@@ -46,9 +52,21 @@ func httpHandler(writer http.ResponseWriter, request *http.Request) {
log.Error(err) log.Error(err)
} }
pnd, err := c.pndc.get(pid) sid, err := uuid.Parse(query.Get("sbi"))
if err != nil {
log.Error(err)
}
sbi := pnd.GetSBIs() var pnd PrincipalNetworkDomain
var sbi SouthboundInterface
if query.Get("q") != "init" && query.Get("q") != "getIDs" {
pnd, err = c.pndc.get(pid)
if err != nil {
log.Error(err)
}
sbic := pnd.GetSBIs()
sbi, err = sbic.(*sbiStore).get(sid)
}
switch query.Get("q") { switch query.Get("q") {
case "addDevice": case "addDevice":
...@@ -113,10 +131,25 @@ func httpHandler(writer http.ResponseWriter, request *http.Request) { ...@@ -113,10 +131,25 @@ func httpHandler(writer http.ResponseWriter, request *http.Request) {
writer.Header().Set("Content-Type", "application/json") writer.Header().Set("Content-Type", "application/json")
fmt.Fprintf(writer, "%v", device) fmt.Fprintf(writer, "%v", device)
case "getIDs": case "getIDs":
ids := pnd.(*pndImplementation).devices.UUIDs() writeIDs := func(typ string, ids []uuid.UUID) {
for i, id := range ids { fmt.Fprintf(writer, "%v:\n", typ)
fmt.Fprintf(writer, "%v: %v\n", i+1, id) for i, id := range ids {
fmt.Fprintf(writer, "%v: %v\n", i+1, id)
}
}
writeIDs("PNDs", c.pndc.UUIDs())
writeIDs("SBIs", c.sbic.UUIDs())
if pnd != nil {
writeIDs("Devices", pnd.(*pndImplementation).devices.UUIDs())
}
case "init":
writeIDs := func(typ string, ids []uuid.UUID) {
for _, id := range ids {
fmt.Fprintf(writer, "%v", id)
}
} }
writeIDs("PNDs", c.pndc.UUIDs())
writeIDs("SBIs", c.sbic.UUIDs())
case "set": case "set":
resp, err := pnd.(*pndImplementation).Set(id, query.Get("path"), query.Get("value")) resp, err := pnd.(*pndImplementation).Set(id, query.Get("path"), query.Get("value"))
if err != nil { if err != nil {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment