From 19a8499cec39b61ca67fa1c79a41d37c9f06e546 Mon Sep 17 00:00:00 2001
From: Manuel Kieweg <manuel.kieweg@h-da.de>
Date: Thu, 25 Mar 2021 14:32:03 +0000
Subject: [PATCH] save last pnd and sbi uuid to cli config

---
 cli/http.go       | 11 ++++++++-
 cmd/addDevice.go  |  2 ++
 cmd/cli.go        |  5 ++--
 cmd/cliSet.go     |  2 ++
 cmd/getDevice.go  |  8 ++++++-
 cmd/init.go       | 60 +++++++++++++++++++++++++++++++++++++++++++++++
 cmd/request.go    |  9 ++++++-
 cmd/requestAll.go |  8 ++++++-
 cmd/root.go       |  6 ++++-
 nucleus/http.go   | 43 +++++++++++++++++++++++++++++----
 10 files changed, 142 insertions(+), 12 deletions(-)
 create mode 100644 cmd/init.go

diff --git a/cli/http.go b/cli/http.go
index 2554574f2..1dc35dd17 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 c534ec48d..e3e55636e 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 ebe9af89a..74658c51d 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 324915c72..a17dab0b5 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 ea4b7271a..3e5ae2730 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 000000000..17fb0ca83
--- /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 9cb4a00ae..d234bb24b 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 d3196783d..2b996b0d8 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 463bdcc35..08093cbcf 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 3c2980c9a..7693bcabd 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 {
-- 
GitLab