From 93f48c816aeb1e6ed5265cea0b0f9b8f990a8232 Mon Sep 17 00:00:00 2001
From: Fabian Seidl <fabian.b.seidl@stud.h-da.de>
Date: Tue, 2 Aug 2022 13:39:50 +0000
Subject: [PATCH] Resolve "Running the controller via VSCode uses different
 paths for configs/stores/etc. which leads to some problems"

See merge request danet/gosdn!360
---
 controller/config/config.go                      |  6 ++++++
 controller/config/gnmiSubscriptionConfig.go      | 16 +++++++++++-----
 .../configs/containerlab-gosdn.toml.example      |  2 ++
 .../configs/development-gosdn.toml.example       |  2 +-
 controller/controller.go                         |  2 --
 5 files changed, 20 insertions(+), 8 deletions(-)

diff --git a/controller/config/config.go b/controller/config/config.go
index dded01cad..55161e10c 100644
--- a/controller/config/config.go
+++ b/controller/config/config.go
@@ -21,6 +21,7 @@ const (
 	jwtDurationKey                  = "defaultJWTDuration"
 	defaultJWTDuration              = time.Hour * 24
 	jwtSecretKey                    = "jwtSecret"
+	gNMISubscriptionsFilePathKey    = "gNMISubscriptionsPath"
 
 	// RabbitMQ Broker
 	amqpPrefixKey   = "amqpPrefix"
@@ -72,6 +73,9 @@ var AMQPHost string
 // AMQPPort is the amqp port
 var AMQPPort string
 
+// GNMISubscriptionsFilePath is the path to the file used for automated subscriptions
+var GNMISubscriptionsFilePath string
+
 // Init gets called on module import
 func Init() {
 	err := InitializeConfig()
@@ -125,6 +129,8 @@ func InitializeConfig() error {
 
 	JWTSecret = viper.GetString(jwtSecretKey)
 
+	GNMISubscriptionsFilePath = getStringFromViper(gNMISubscriptionsFilePathKey)
+
 	loadAMQPConfig()
 
 	if err := viper.WriteConfig(); err != nil {
diff --git a/controller/config/gnmiSubscriptionConfig.go b/controller/config/gnmiSubscriptionConfig.go
index fa433e3d8..53e1b3257 100644
--- a/controller/config/gnmiSubscriptionConfig.go
+++ b/controller/config/gnmiSubscriptionConfig.go
@@ -16,12 +16,18 @@ var gnmiSubscriptionPaths [][]string
 
 // ReadGnmiSubscriptionPaths reads the paths the watcher should subscribe to provided in a file.
 func ReadGnmiSubscriptionPaths() error {
-	currentDir, err := os.Getwd()
-	if err != nil {
-		return err
-	}
+	var filePath string
 
-	filePath := filepath.Join(currentDir, defaultGnmiSubscriptionFilePath)
+	if GNMISubscriptionsFilePath != "" {
+		filePath = GNMISubscriptionsFilePath
+	} else {
+		currentDir, err := os.Getwd()
+		if err != nil {
+			return err
+		}
+
+		filePath = filepath.Join(currentDir, defaultGnmiSubscriptionFilePath)
+	}
 
 	f, err := os.Open(filePath)
 	if err != nil {
diff --git a/controller/configs/containerlab-gosdn.toml.example b/controller/configs/containerlab-gosdn.toml.example
index 6f52b353c..abe7167bd 100644
--- a/controller/configs/containerlab-gosdn.toml.example
+++ b/controller/configs/containerlab-gosdn.toml.example
@@ -11,6 +11,8 @@ pnduuid = "bf8160d4-4659-4a1b-98fd-f409a04111ec"
 socket = ":55055"
 databaseConnection = "mongodb://root:example@clab-gosdn_csbi_arista_base-mongodb:27017"
 filesystemPathToStores = "stores"
+gNMISubscriptionsPath = "controller/configs/gNMISubscriptions.txt"
+
 
 amqpPrefix = "amqp://"
 amqpUser = "guest"
diff --git a/controller/configs/development-gosdn.toml.example b/controller/configs/development-gosdn.toml.example
index b1cf52c3a..820c76f13 100644
--- a/controller/configs/development-gosdn.toml.example
+++ b/controller/configs/development-gosdn.toml.example
@@ -11,7 +11,7 @@ pnduuid = "bf8160d4-4659-4a1b-98fd-f409a04111ec"
 socket = ":55055"
 databaseConnection = "mongodb://root:example@localhost:27017"
 filesystemPathToStores = "stores"
-
+gNMISubscriptionsPath = "controller/configs/gNMISubscriptions.txt"
 
 amqpPrefix = "amqp://"
 amqpUser = "guest"
diff --git a/controller/controller.go b/controller/controller.go
index d2f9f454c..148b68b3b 100644
--- a/controller/controller.go
+++ b/controller/controller.go
@@ -112,8 +112,6 @@ func initialize() error {
 	}
 
 	c.deviceWatcher = nucleus.NewDeviceWatcher(c.pndStore)
-	// TODO: udpate with actual paths to subscribe when using template/config
-	fmt.Println(config.GetGnmiSubscriptionPaths())
 	c.deviceWatcher.SubToDevices(config.GetGnmiSubscriptionPaths(), nil)
 
 	err = ensureDefaultRoleExists()
-- 
GitLab