Repository: scionproto/scion
Pull request #3688 (2020-03-13 09:56:13)

SIG: Move sig.json parsing into a shared library


<!-- Reviewable:start -->
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/scionproto/scion/3688)
<!-- Reviewable:end -->



Patch:
diff --git a/go/sig/config/BUILD.bazel b/go/lib/sigjson/BUILD.bazel
similarity index 91%
rename from go/sig/config/BUILD.bazel
rename to go/lib/sigjson/BUILD.bazel
index 2adb0dd3b..cb677d9d4 100644
--- a/go/sig/config/BUILD.bazel
+++ b/go/lib/sigjson/BUILD.bazel
@@ -6,7 +6,7 @@ go_library(
         "config.go",
         "ipnet.go",
     ],
-    importpath = "github.com/scionproto/scion/go/sig/config",
+    importpath = "github.com/scionproto/scion/go/lib/sigjson",
     visibility = ["//visibility:public"],
     deps = [
         "//go/lib/addr:go_default_library",
diff --git a/go/sig/config/config.go b/go/lib/sigjson/config.go
similarity index 93%
rename from go/sig/config/config.go
rename to go/lib/sigjson/config.go
index fb30cf8cf..ed5a30c8c 100644
--- a/go/sig/config/config.go
+++ b/go/lib/sigjson/config.go
@@ -12,9 +12,10 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-// Package config is responsible for parsing the SIG json config file into a
+// Package sigjson is responsible for parsing the SIG json config file into a
 // set of simple intermediate data-structures.
-package config
+
+package sigjson
 
 import (
 	"encoding/json"
diff --git a/go/sig/config/ipnet.go b/go/lib/sigjson/ipnet.go
similarity index 91%
rename from go/sig/config/ipnet.go
rename to go/lib/sigjson/ipnet.go
index 6211a6443..a10e8315f 100644
--- a/go/sig/config/ipnet.go
+++ b/go/lib/sigjson/ipnet.go
@@ -12,9 +12,7 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-// Package config is responsible for parsing the SIG json config file into a
-// set of simple intermediate data-structures.
-package config
+package sigjson
 
 import (
 	"encoding/json"
diff --git a/go/sig/config/testdata/01-loadfromfile.json b/go/lib/sigjson/testdata/01-loadfromfile.json
similarity index 100%
rename from go/sig/config/testdata/01-loadfromfile.json
rename to go/lib/sigjson/testdata/01-loadfromfile.json
diff --git a/go/sig/config/testdata/README.md b/go/lib/sigjson/testdata/README.md
similarity index 100%
rename from go/sig/config/testdata/README.md
rename to go/lib/sigjson/testdata/README.md
diff --git a/go/sig/BUILD.bazel b/go/sig/BUILD.bazel
index b69616d46..2acafbc28 100644
--- a/go/sig/BUILD.bazel
+++ b/go/sig/BUILD.bazel
@@ -14,7 +14,7 @@ go_library(
         "//go/lib/prom:go_default_library",
         "//go/lib/serrors:go_default_library",
         "//go/lib/sigdisp:go_default_library",
-        "//go/sig/config:go_default_library",
+        "//go/lib/sigjson:go_default_library",
         "//go/sig/egress:go_default_library",
         "//go/sig/internal/base:go_default_library",
         "//go/sig/internal/ingress:go_default_library",
diff --git a/go/sig/egress/BUILD.bazel b/go/sig/egress/BUILD.bazel
index 04e5fb0ef..ac1927037 100644
--- a/go/sig/egress/BUILD.bazel
+++ b/go/sig/egress/BUILD.bazel
@@ -8,7 +8,7 @@ go_library(
     deps = [
         "//go/lib/fatal:go_default_library",
         "//go/lib/log:go_default_library",
-        "//go/sig/config:go_default_library",
+        "//go/lib/sigjson:go_default_library",
         "//go/sig/egress/asmap:go_default_library",
         "//go/sig/egress/iface:go_default_library",
         "//go/sig/egress/reader:go_default_library",
diff --git a/go/sig/egress/api.go b/go/sig/egress/api.go
index ebbbca377..7b773af26 100644
--- a/go/sig/egress/api.go
+++ b/go/sig/egress/api.go
@@ -20,7 +20,7 @@ import (
 
 	"github.com/scionproto/scion/go/lib/fatal"
 	"github.com/scionproto/scion/go/lib/log"
-	"github.com/scionproto/scion/go/sig/config"
+	"github.com/scionproto/scion/go/lib/sigjson"
 	"github.com/scionproto/scion/go/sig/egress/asmap"
 	"github.com/scionproto/scion/go/sig/egress/iface"
 	"github.com/scionproto/scion/go/sig/egress/reader"
@@ -36,7 +36,7 @@ func Init(tunIO io.ReadWriteCloser) {
 	}()
 }
 
-func ReloadConfig(cfg *config.Cfg) bool {
+func ReloadConfig(cfg *sigjson.Cfg) bool {
 	res := asmap.Map.ReloadConfig(cfg)
 	log.Info("Config reloaded")
 	return res
diff --git a/go/sig/egress/asmap/BUILD.bazel b/go/sig/egress/asmap/BUILD.bazel
index 649b91a0f..e41bd6947 100644
--- a/go/sig/egress/asmap/BUILD.bazel
+++ b/go/sig/egress/asmap/BUILD.bazel
@@ -13,7 +13,7 @@ go_library(
         "//go/lib/common:go_default_library",
         "//go/lib/log:go_default_library",
         "//go/lib/ringbuf:go_default_library",
-        "//go/sig/config:go_default_library",
+        "//go/lib/sigjson:go_default_library",
         "//go/sig/egress/dispatcher:go_default_library",
         "//go/sig/egress/iface:go_default_library",
         "//go/sig/egress/router:go_default_library",
diff --git a/go/sig/egress/asmap/as.go b/go/sig/egress/asmap/as.go
index a45a9f1e9..670bc6031 100644
--- a/go/sig/egress/asmap/as.go
+++ b/go/sig/egress/asmap/as.go
@@ -25,7 +25,7 @@ import (
 	"github.com/scionproto/scion/go/lib/common"
 	"github.com/scionproto/scion/go/lib/log"
 	"github.com/scionproto/scion/go/lib/ringbuf"
-	"github.com/scionproto/scion/go/sig/config"
+	"github.com/scionproto/scion/go/lib/sigjson"
 	"github.com/scionproto/scion/go/sig/egress/dispatcher"
 	"github.com/scionproto/scion/go/sig/egress/iface"
 	"github.com/scionproto/scion/go/sig/egress/router"
@@ -72,7 +72,7 @@ func newASEntry(ia addr.IA) (*ASEntry, error) {
 	return ae, nil
 }
 
-func (ae *ASEntry) ReloadConfig(cfg *config.Cfg, cfgEntry *config.ASEntry) bool {
+func (ae *ASEntry) ReloadConfig(cfg *sigjson.Cfg, cfgEntry *sigjson.ASEntry) bool {
 	ae.Lock()
 	defer ae.Unlock()
 	// Method calls first to prevent skips due to logical short-circuit
@@ -81,7 +81,7 @@ func (ae *ASEntry) ReloadConfig(cfg *config.Cfg, cfgEntry *config.ASEntry) bool
 }
 
 // addNewNets adds the networks in ipnets that are not currently configured.
-func (ae *ASEntry) addNewNets(ipnets []*config.IPNet) bool {
+func (ae *ASEntry) addNewNets(ipnets []*sigjson.IPNet) bool {
 	s := true
 	for _, ipnet := range ipnets {
 		err := ae.addNet(ipnet.IPNet())
@@ -94,7 +94,7 @@ func (ae *ASEntry) addNewNets(ipnets []*config.IPNet) bool {
 }
 
 // delOldNets deletes currently configured networks that are not in ipnets.
-func (ae *ASEntry) delOldNets(ipnets []*config.IPNet) bool {
+func (ae *ASEntry) delOldNets(ipnets []*sigjson.IPNet) bool {
 	s := true
 Top:
 	for k, v := range ae.Nets {
diff --git a/go/sig/egress/asmap/map.go b/go/sig/egress/asmap/map.go
index a2b7f8ad6..1f1240d60 100644
--- a/go/sig/egress/asmap/map.go
+++ b/go/sig/egress/asmap/map.go
@@ -22,7 +22,7 @@ import (
 	"github.com/scionproto/scion/go/lib/addr"
 	"github.com/scionproto/scion/go/lib/common"
 	"github.com/scionproto/scion/go/lib/log"
-	"github.com/scionproto/scion/go/sig/config"
+	"github.com/scionproto/scion/go/lib/sigjson"
 )
 
 var Map = &ASMap{}
@@ -60,14 +60,14 @@ func (am *ASMap) Range(f func(key addr.IAInt, value *ASEntry) bool) {
 	})
 }
 
-func (am *ASMap) ReloadConfig(cfg *config.Cfg) bool {
+func (am *ASMap) ReloadConfig(cfg *sigjson.Cfg) bool {
 	// Method calls first to prevent skips due to logical short-circuit
 	s := am.addNewIAs(cfg)
 	return am.delOldIAs(cfg) && s
 }
 
 // addNewIAs adds the ASes in cfg that are not currently configured.
-func (am *ASMap) addNewIAs(cfg *config.Cfg) bool {
+func (am *ASMap) addNewIAs(cfg *sigjson.Cfg) bool {
 	s := true
 	for ia, cfgEntry := range cfg.ASes {
 		log.Info("ReloadConfig: Adding AS...", "ia", ia)
@@ -83,7 +83,7 @@ func (am *ASMap) addNewIAs(cfg *config.Cfg) bool {
 	return s
 }
 
-func (am *ASMap) delOldIAs(cfg *config.Cfg) bool {
+func (am *ASMap) delOldIAs(cfg *sigjson.Cfg) bool {
 	s := true
 	// Delete all ASes that currently exist but are not in cfg
 	am.Range(func(iaInt addr.IAInt, as *ASEntry) bool {
diff --git a/go/sig/main.go b/go/sig/main.go
index d14ea6e2a..420aec573 100644
--- a/go/sig/main.go
+++ b/go/sig/main.go
@@ -36,7 +36,7 @@ import (
 	"github.com/scionproto/scion/go/lib/prom"
 	"github.com/scionproto/scion/go/lib/serrors"
 	"github.com/scionproto/scion/go/lib/sigdisp"
-	"github.com/scionproto/scion/go/sig/config"
+	"github.com/scionproto/scion/go/lib/sigjson"
 	"github.com/scionproto/scion/go/sig/egress"
 	"github.com/scionproto/scion/go/sig/internal/base"
 	"github.com/scionproto/scion/go/sig/internal/ingress"
@@ -195,7 +195,7 @@ func checkPerms() error {
 }
 
 func loadConfig(path string) bool {
-	cfg, err := config.LoadFromFile(path)
+	cfg, err := sigjson.LoadFromFile(path)
 	if err != nil {
 		log.Error("loadConfig: Failed", "err", err)
 		return false


Test patch:
diff --git a/go/sig/config/config_test.go b/go/lib/sigjson/config_test.go
similarity index 99%
rename from go/sig/config/config_test.go
rename to go/lib/sigjson/config_test.go
index b52fe0b35..717f042cc 100644
--- a/go/sig/config/config_test.go
+++ b/go/lib/sigjson/config_test.go
@@ -12,7 +12,7 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-package config
+package sigjson
 
 import (
 	"flag"