diff --git a/cli/http.go b/cli/http.go index 2554574f2657fdd9dd845f1d77cdd7546a202300..1dc35dd178006d65a8813a8e92bfb95d0d216f79 100644 --- a/cli/http.go +++ b/cli/http.go @@ -3,6 +3,7 @@ package cli import ( "fmt" log "github.com/sirupsen/logrus" + "github.com/spf13/viper" "io/ioutil" "net/http" "strings" @@ -33,7 +34,15 @@ func HttpGet(apiEndpoint, f string, args ...string) error { if err != nil { 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: defer resp.Body.Close() bytes, err := ioutil.ReadAll(resp.Body) diff --git a/cmd/addDevice.go b/cmd/addDevice.go index c534ec48dc91fd48651c0d4497708f35aae27847..e3e55636e6cec89492ad042424d70d370dbde872 100644 --- a/cmd/addDevice.go +++ b/cmd/addDevice.go @@ -47,6 +47,8 @@ var addDeviceCmd = &cobra.Command{ "address="+address, "password="+password, "username="+username, + "sbi="+cliSbi, + "pnd="+cliPnd, ) }, } diff --git a/cmd/cli.go b/cmd/cli.go index ebe9af89ac336e0d8b737bbc7c9c6e35d85769f2..74658c51d9be2c00a626b11bebd0169704951787 100644 --- a/cmd/cli.go +++ b/cmd/cli.go @@ -31,7 +31,7 @@ POSSIBILITY OF SUCH DAMAGE. package cmd import ( - "errors" + "code.fbi.h-da.de/cocsn/gosdn/cli" "github.com/spf13/cobra" ) @@ -44,7 +44,7 @@ var cliCmd = &cobra.Command{ Short: "", Long: ``, 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() { cliCmd.PersistentFlags().StringVar(&uuid, "uuid", "", "uuid of the requested device") cliCmd.PersistentFlags().StringVar(&apiEndpoint, "api-endpoint", "http://localhost:8080", "address of the target") + } diff --git a/cmd/cliSet.go b/cmd/cliSet.go index 324915c72f906de5bbfd6dea0ab53768d5c3d0e8..a17dab0b566a129d0d4cbe7ced712ea019001617 100644 --- a/cmd/cliSet.go +++ b/cmd/cliSet.go @@ -45,6 +45,8 @@ var cliSetCmd = &cobra.Command{ apiEndpoint, "set", "uuid="+uuid, + "cliSbi="+cliSbi, + "cliPnd="+cliPnd, "path="+args[0], "address="+address, "value="+args[1], diff --git a/cmd/getDevice.go b/cmd/getDevice.go index ea4b7271a93abef45649e7372836cb3c9389b16e..3e5ae2730e881737b0b153ec96b83a09ab346f56 100644 --- a/cmd/getDevice.go +++ b/cmd/getDevice.go @@ -41,7 +41,13 @@ var getDeviceCmd = &cobra.Command{ Short: "gets device information from the controller", Long: ``, 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, + ) }, } diff --git a/cmd/init.go b/cmd/init.go new file mode 100644 index 0000000000000000000000000000000000000000..17fb0ca83c6fbc2ca0c8f2e3e0de0823edebc437 --- /dev/null +++ b/cmd/init.go @@ -0,0 +1,60 @@ +/* +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") +} diff --git a/cmd/request.go b/cmd/request.go index 9cb4a00ae353c77062caaefaaf26364d8eb092d0..d234bb24b69f8491d1e5c707e0d02a58a68c4887 100644 --- a/cmd/request.go +++ b/cmd/request.go @@ -41,7 +41,14 @@ var requestCmd = &cobra.Command{ Short: "requests a path from a specified device on the controller", Long: ``, 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], + ) }, } diff --git a/cmd/requestAll.go b/cmd/requestAll.go index d3196783db09b47df0dc8fc51a01c3c5547fbb90..2b996b0d8e7014d5a98468254c008203fdbff47b 100644 --- a/cmd/requestAll.go +++ b/cmd/requestAll.go @@ -41,7 +41,13 @@ var requestAllCmd = &cobra.Command{ Short: "requests specified path from all devices on the controller", Long: ``, 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], + ) }, } diff --git a/cmd/root.go b/cmd/root.go index 463bdcc35bb1427196a3374add90b5fb0868234f..08093cbcfb5efb8d05cb3eeffd56f90a92393362 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -46,12 +46,14 @@ var password string var address string var loglevel string var grpcPort string +var cliPnd string +var cliSbi string // rootCmd represents the base command when called without any subcommands var rootCmd = &cobra.Command{ Use: "gosdn", 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 { ctx, cancel := context.WithCancel(context.Background()) defer cancel() @@ -104,6 +106,8 @@ func initConfig() { } viper.SetDefault("socket", ":"+grpcPort) + cliPnd = viper.GetString("CLI_PND") + cliSbi = viper.GetString("CLI_SBI") loglevel = viper.GetString("GOSDN_LOG") log.SetReportCaller(true) diff --git a/nucleus/http.go b/nucleus/http.go index 3c2980c9a031b785bed60b3a23eab13544547eb2..7693bcabd845652c9f0cb81cae78a0dbd1edfae6 100644 --- a/nucleus/http.go +++ b/nucleus/http.go @@ -15,6 +15,8 @@ const basePath = "/api" // deprecated func httpApi() (err error) { http.HandleFunc(basePath, httpHandler) + http.HandleFunc("/livez", healthCheck) + http.HandleFunc("/readyz", healthCheck) go func() { err = http.ListenAndServe(":8080", nil) @@ -25,6 +27,10 @@ func httpApi() (err error) { return nil } +func healthCheck(writer http.ResponseWriter, request *http.Request) { + writer.WriteHeader(http.StatusOK) +} + func httpHandler(writer http.ResponseWriter, request *http.Request) { log.WithFields(log.Fields{ "request": request, @@ -46,9 +52,21 @@ func httpHandler(writer http.ResponseWriter, request *http.Request) { 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") { case "addDevice": @@ -113,10 +131,25 @@ func httpHandler(writer http.ResponseWriter, request *http.Request) { writer.Header().Set("Content-Type", "application/json") fmt.Fprintf(writer, "%v", device) case "getIDs": - ids := pnd.(*pndImplementation).devices.UUIDs() - for i, id := range ids { - fmt.Fprintf(writer, "%v: %v\n", i+1, id) + writeIDs := func(typ string, ids []uuid.UUID) { + fmt.Fprintf(writer, "%v:\n", typ) + 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": resp, err := pnd.(*pndImplementation).Set(id, query.Get("path"), query.Get("value")) if err != nil {