Skip to content
Snippets Groups Projects
start.go 7.1 KiB
Newer Older
  • Learn to ignore specific revisions
  • Copyright © 2023 da/net Research Group <danet@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 (
    
    Shrey Garg's avatar
    Shrey Garg committed
    
    
    	gnmitarget "code.fbi.h-da.de/danet/gnmi-target"
    	"code.fbi.h-da.de/danet/gnmi-target/examples/example01/etsiqkdnclient"
    
    	"code.fbi.h-da.de/danet/gnmi-target/examples/example01/handlers/danet"
    
    	"code.fbi.h-da.de/danet/gnmi-target/examples/example01/handlers/etsi"
    
    	"code.fbi.h-da.de/danet/gnmi-target/examples/example01/handlers/interfaces"
    	networkinstances "code.fbi.h-da.de/danet/gnmi-target/examples/example01/handlers/network-instances"
    	"code.fbi.h-da.de/danet/gnmi-target/examples/example01/handlers/system"
    	gnmitargetygot "code.fbi.h-da.de/danet/gnmi-target/examples/example01/model"
    	"code.fbi.h-da.de/danet/gnmi-target/handler"
    
    	"github.com/sirupsen/logrus"
    
    	"github.com/spf13/viper"
    
    	"gopkg.in/yaml.v3"
    
    	bindAddress string // IP/Port to bind to listen for the gnmi server
    	configFile  string // my config file to use
    	osclient    string // TODO unclear
    	logLevel    string // which loglevel to use
    	certFile    string // the location of the file containing the X.509 certificates for the gnmi server
    
    	caFile      string // the location of the file containing the ca certificate to verify client certificates
    
    	keyFile     string // the location of the file containing the key for the certificates for the gnmi server
    	insecure    *bool  // set to true if insecure operations is needed, i.e., do not use TLS
    	// Below is qkdn specific information
    
    	udpQL1AddrString string
    	ql1Name          string
    	udpQL2AddrString string
    	ql2Name          string
    
    	kmsConfig        string
    
    	// End of qkdn specific information
    
    // startCmd represents the start command
    var startCmd = &cobra.Command{
    	Use:   "start",
    
    	Short: "Start gnmi server",
    
    	Long:  `A gNMI target`,
    
    	Run: func(cmd *cobra.Command, args []string) {
    
    		fmt.Println("Startup of GNMI Target...")
    
    		lvl := viper.GetString("logLevel")
    
    		// parse string, this is built-in feature of logrus
    		ll, err := logrus.ParseLevel(lvl)
    		if err != nil {
    
    		}
    		// set global log level
    		logrus.SetLevel(ll)
    
    Shrey Garg's avatar
    Shrey Garg committed
    
    
    		kmsConfig := &etsiqkdnclient.Config{}
    
    		logrus.Println(viper.GetString("kms-config"))
    		file, err := os.ReadFile(viper.GetString("kms-config"))
    		if err != nil {
    			logrus.Fatal(err)
    		}
    
    		if err := yaml.Unmarshal(file, kmsConfig); err != nil {
    			logrus.Fatal(err)
    
    		qkdnClient := etsiqkdnclient.NewQkdnClient(kmsConfig)
    
    
    		schema, err := gnmitargetygot.Schema()
    		if err != nil {
    			logrus.Fatal(err)
    		}
    
    		// The registered path handlers sorted by priority. If specific
    		// handlers should be able to process their workload before others,
    		// then they should be placed in the front of the slice.
    		handlers := []handler.PathHandler{
    			//acl.NewACLYangHandler(),
    			interfaces.NewInterfacesHandler(),
    			networkinstances.NewNetworkInstanceHandler(),
    			//danet.NewModemYangHandler(),
    			system.NewHostnameHandler(),
    			system.NewMemoryHandler(),
    			system.NewMotdHandler(),
    			system.NewStateHandler(),
    			system.NewSystemHandler(),
    
    			etsi.NewETSI15Handler(qkdnClient),
    			danet.NewAssignForwardingHandler(qkdnClient),
    
    		gnmitTarget := gnmitarget.NewGnmiTarget(schema, &gnmitargetygot.Gnmitarget{}, gnmitargetygot.ΓModelData, gnmitargetygot.Unmarshal, gnmitargetygot.ΛEnum, handlers...)
    
    		if err := gnmitTarget.Start(viper.GetString("bindAddress"), viper.GetString("certFile"), viper.GetString("keyFile"), viper.GetString("caFile"), *insecure); err != nil {
    
    Shrey Garg's avatar
    Shrey Garg committed
    			logrus.Fatal(err)
    		}
    
    	},
    }
    
    func init() {
    	// Here you will define your flags and configuration settings.
    
    	startCmd.Flags().StringVarP(&configFile, "config", "c", "config/openconfig.json", "system configuration")
    	startCmd.Flags().StringVarP(&bindAddress, "bind_address", "a", ":7030", "address to bind to")
    
    	startCmd.Flags().StringVarP(&logLevel, "log", "l", "info", "loglevel")
    
    	insecure = startCmd.Flags().Bool("insecure", false, "If true do not use TLS")
    	startCmd.Flags().StringVarP(&certFile, "cert", "", "", "location of the cert file")
    	startCmd.Flags().StringVarP(&keyFile, "key", "", "", "location of the key file")
    
    	startCmd.Flags().StringVarP(&caFile, "ca_file", "", "", "location of the ca file")
    
    	startCmd.Flags().StringVarP(&osclient, "osclient", "o", "ubuntu", "os client to use by system")
    
    	startCmd.Flags().StringVarP(&udpQL1AddrString, "my_QLE_socket", "", "[::1]:50900", "local quantum element's address")
    	startCmd.Flags().StringVarP(&ql1Name, "my_name", "", "ekms-ql1", "The name of the local quantumlayer")
    	startCmd.Flags().StringVarP(&udpQL2AddrString, "remote_QLE_socket", "", "[::1]:50901", "remote quantum element's address")
    	startCmd.Flags().StringVarP(&ql2Name, "remote_name", "", "ekms-ql2", "The name of the remote quantumlayer")
    
    	startCmd.Flags().StringVarP(&kmsConfig, "kms_config", "", "/usr/bin/config/kms.yaml", "Path to the kms config file (yaml)")
    
    	viper.BindPFlag("bindAddress", startCmd.Flags().Lookup("bind_address"))
    	viper.BindPFlag("configFile", startCmd.Flags().Lookup("config"))
    
    	viper.BindPFlag("logLevel", startCmd.Flags().Lookup("log"))
    
    	viper.BindPFlag("insecure", startCmd.Flags().Lookup("insecure"))
    	viper.BindPFlag("certFile", startCmd.Flags().Lookup("cert"))
    	viper.BindPFlag("keyFile", startCmd.Flags().Lookup("key"))
    
    	viper.BindPFlag("caFile", startCmd.Flags().Lookup("ca_file"))
    
    	viper.BindPFlag("osclient", startCmd.Flags().Lookup("osclient"))
    
    	viper.BindPFlag("my_QLE_socket", startCmd.Flags().Lookup("my-address"))
    	viper.BindPFlag("my_name", startCmd.Flags().Lookup("my-name"))
    	viper.BindPFlag("remote_QLE_socket", startCmd.Flags().Lookup("remote-address"))
    	viper.BindPFlag("remote_name", startCmd.Flags().Lookup("remote-name"))
    
    	viper.BindPFlag("kms-config", startCmd.Flags().Lookup("kms_config"))
    
    	rootCmd.AddCommand(startCmd)