VOL-1947 Update source-router to use protodescriptor from method router

Change-Id: Ic1748d881d0cd6ea4d7b66a3f10d0b14a82631f3
diff --git a/afrouter/afrouter/source-router.go b/afrouter/afrouter/source-router.go
index c9f94ef..7841554 100644
--- a/afrouter/afrouter/source-router.go
+++ b/afrouter/afrouter/source-router.go
@@ -36,10 +36,8 @@
 	"errors"
 	"fmt"
 	"github.com/golang/protobuf/proto"
-	pb "github.com/golang/protobuf/protoc-gen-go/descriptor"
 	"github.com/opencord/voltha-go/common/log"
 	"google.golang.org/grpc"
-	"io/ioutil"
 	"regexp"
 	"strconv"
 )
@@ -47,11 +45,10 @@
 type SourceRouter struct {
 	name string
 	//association     associationType
-	routingField    string
-	grpcService     string
-	protoDescriptor *pb.FileDescriptorSet
-	methodMap       map[string]byte
-	cluster         *cluster
+	routingField string
+	grpcService  string
+	methodMap    map[string]byte
+	cluster      *cluster
 }
 
 func newSourceRouter(rconf *RouterConfig, config *RouteConfig) (Router, error) {
@@ -91,37 +88,23 @@
 		methodMap:   make(map[string]byte),
 	}
 
-	// Load the protobuf descriptor file
-	dr.protoDescriptor = &pb.FileDescriptorSet{}
-	fb, err := ioutil.ReadFile(rconf.ProtoFile)
-	if err != nil {
-		log.Errorf("Could not open proto file '%s'", rconf.ProtoFile)
-		rtrn_err = true
-	}
-	err = proto.Unmarshal(fb, dr.protoDescriptor)
-	if err != nil {
-		log.Errorf("Could not unmarshal %s, %v", "proto.pb", err)
-		rtrn_err = true
-	}
-
 	// Build the routing structure based on the loaded protobuf
 	// descriptor file and the config information.
 	type key struct {
 		method string
 		field  string
 	}
-	var msgs = make(map[key]byte)
-	for _, f := range dr.protoDescriptor.File {
+	var fieldNumberLookup = make(map[key]byte)
+	for _, f := range rconf.protoDescriptor.File {
 		// Build a temporary map of message types by name.
 		for _, m := range f.MessageType {
 			for _, fld := range m.Field {
 				log.Debugf("Processing message '%s', field '%s'", *m.Name, *fld.Name)
-				msgs[key{*m.Name, *fld.Name}] = byte(*fld.Number)
+				fieldNumberLookup[key{*m.Name, *fld.Name}] = byte(*fld.Number)
 			}
 		}
 	}
-	log.Debugf("The map contains: %v", msgs)
-	for _, f := range dr.protoDescriptor.File {
+	for _, f := range rconf.protoDescriptor.File {
 		if *f.Package == rconf.ProtoPackage {
 			for _, s := range f.Service {
 				if *s.Name == rconf.ProtoService {
@@ -144,7 +127,7 @@
 							// The input type has the package name prepended to it. Remove it.
 							//in := (*m.InputType)[len(rconf.ProtoPackage)+2:]
 							in := pkg_methd[PKG_MTHD_MTHD]
-							dr.methodMap[*m.Name], ok = msgs[key{in, config.RouteField}]
+							dr.methodMap[*m.Name], ok = fieldNumberLookup[key{in, config.RouteField}]
 							if !ok {
 								log.Errorf("Method '%s' has no field named '%s' in it's parameter message '%s'",
 									*m.Name, config.RouteField, in)
diff --git a/afrouter/afrouter/source-router_test.go b/afrouter/afrouter/source-router_test.go
index 742c238..b911dcd 100644
--- a/afrouter/afrouter/source-router_test.go
+++ b/afrouter/afrouter/source-router_test.go
@@ -33,7 +33,7 @@
 	log.AddPackage(log.JSON, log.WarnLevel, nil)
 }
 
-func MakeSourceRouterTestConfig() (*ConnectionConfig, *BackendConfig, *BackendClusterConfig, *RouteConfig, *RouterConfig) {
+func MakeSourceRouterTestConfig() (*RouteConfig, *RouterConfig) {
 	connectionConfig := ConnectionConfig{
 		Name: "ro_vcore01",
 		Addr: "foo",
@@ -67,70 +67,74 @@
 		Routes:       []RouteConfig{routeConfig},
 		ProtoFile:    SOURCE_ROUTER_PROTOFILE,
 	}
-	return &connectionConfig, &backendConfig, &backendClusterConfig, &routeConfig, &routerConfig
+	return &routeConfig, &routerConfig
 }
 
 func TestSourceRouterInit(t *testing.T) {
-	_, _, _, routeConfig, routerConfig := MakeSourceRouterTestConfig()
+	routeConfig, routerConfig := MakeSourceRouterTestConfig()
 
 	router, err := newSourceRouter(routerConfig, routeConfig)
 
-	assert.NotEqual(t, router, nil)
-	assert.Equal(t, err, nil)
+	assert.NotNil(t, router)
+	assert.Nil(t, err)
 
 	assert.Equal(t, router.Service(), "VolthaService")
 	assert.Equal(t, router.Name(), "logger")
 
 	cluster, err := router.BackendCluster("foo", "bar")
 	assert.Equal(t, cluster, clusters["ro_vcore"])
-	assert.Equal(t, err, nil)
+	assert.Nil(t, err)
 
 	assert.Equal(t, router.FindBackendCluster("ro_vcore"), clusters["ro_vcore"])
-	assert.Equal(t, router.ReplyHandler("foo"), nil)
+	assert.Nil(t, router.ReplyHandler("foo"))
 }
 
 func TestSourceRouterDecodeProtoField(t *testing.T) {
-	_, _, _, routeConfig, routerConfig := MakeSourceRouterTestConfig()
+	_, routerConfig := MakeSourceRouterTestConfig()
+	_, err := newRouter(routerConfig)
+	assert.Nil(t, err)
 
-	router, err := newSourceRouter(routerConfig, routeConfig)
-	assert.Equal(t, err, nil)
+	// Get the created AffinityRouter so we can inspect its state
+	sourceRouter := allRouters["vcorelogger"].(SourceRouter)
 
 	loggingMessage := &common_pb.Logging{Level: 1,
 		PackageName:   "default",
 		ComponentName: "ro_vcore0.ro_vcore01"}
 
 	loggingData, err := proto.Marshal(loggingMessage)
-	assert.Equal(t, err, nil)
+	assert.Nil(t, err)
 
-	s, err := router.(SourceRouter).decodeProtoField(loggingData, 2) // field 2 is package_name
+	s, err := sourceRouter.decodeProtoField(loggingData, 2) // field 2 is package_name
 	assert.Equal(t, s, "default")
 
-	s, err = router.(SourceRouter).decodeProtoField(loggingData, 3) // field 2 is component_name
+	s, err = sourceRouter.decodeProtoField(loggingData, 3) // field 2 is component_name
 	assert.Equal(t, s, "ro_vcore0.ro_vcore01")
 }
 
 func TestSourceRouterRoute(t *testing.T) {
-	_, _, _, routeConfig, routerConfig := MakeSourceRouterTestConfig()
+	_, routerConfig := MakeSourceRouterTestConfig()
+	_, err := newRouter(routerConfig)
+	assert.Nil(t, err)
 
-	router, err := newSourceRouter(routerConfig, routeConfig)
-	assert.Equal(t, err, nil)
+	// Get the created AffinityRouter so we can inspect its state
+	sourceRouter := allRouters["vcorelogger"].(SourceRouter)
 
 	loggingMessage := &common_pb.Logging{Level: 1,
 		PackageName:   "default",
 		ComponentName: "ro_vcore0.ro_vcore01"}
 
 	loggingData, err := proto.Marshal(loggingMessage)
-	assert.Equal(t, err, nil)
+	assert.Nil(t, err)
 
 	sel := &requestFrame{payload: loggingData,
 		err:        nil,
 		methodInfo: newMethodDetails("/voltha.VolthaService/UpdateLogLevel")}
 
-	backend, connection := router.Route(sel)
+	backend, connection := sourceRouter.Route(sel)
 
-	assert.Equal(t, sel.err, nil)
-	assert.NotEqual(t, backend, nil)
+	assert.Nil(t, sel.err)
+	assert.NotNil(t, backend)
 	assert.Equal(t, backend.name, "ro_vcore0")
-	assert.NotEqual(t, connection, nil)
+	assert.NotNil(t, connection)
 	assert.Equal(t, connection.name, "ro_vcore01")
 }