VOL-2927 convert to static protos

Change-Id: If08aec0b1fb84fc54f7f62d5e4ede8ad4a9db80f
diff --git a/internal/pkg/commands/devices.go b/internal/pkg/commands/devices.go
index 6579690..b6864f6 100644
--- a/internal/pkg/commands/devices.go
+++ b/internal/pkg/commands/devices.go
@@ -17,17 +17,16 @@
 
 import (
 	"context"
-	"errors"
 	"fmt"
 	"os"
 	"strconv"
 	"strings"
 
-	"github.com/fullstorydev/grpcurl"
+	"github.com/golang/protobuf/ptypes/empty"
 	flags "github.com/jessevdk/go-flags"
-	"github.com/jhump/protoreflect/dynamic"
 	"github.com/opencord/voltctl/pkg/format"
-	"github.com/opencord/voltctl/pkg/model"
+	"github.com/opencord/voltha-protos/v3/go/common"
+	"github.com/opencord/voltha-protos/v3/go/voltha"
 )
 
 const (
@@ -161,10 +160,7 @@
 	}
 	defer conn.Close()
 
-	descriptor, method, err := GetMethod("device-ports")
-	if err != nil {
-		return nil
-	}
+	client := voltha.NewVolthaServiceClient(conn)
 
 	/*
 	 * The command line args when completing for PortNum will be a DeviceId
@@ -195,34 +191,19 @@
 		return nil
 	}
 
-	h := &RpcEventHandler{
-		Fields: map[string]map[string]interface{}{ParamNames[GlobalConfig.ApiVersion]["ID"]: {"id": deviceId}},
-	}
 	ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
 	defer cancel()
-	err = grpcurl.InvokeRPC(ctx, descriptor, conn, method, []string{}, h, h.GetParams)
-	if err != nil {
-		return nil
-	}
 
-	if h.Status != nil && h.Status.Err() != nil {
-		return nil
-	}
+	id := voltha.ID{Id: string(deviceId)}
 
-	d, err := dynamic.AsDynamicMessage(h.Response)
-	if err != nil {
-		return nil
-	}
-
-	items, err := d.TryGetFieldByName("items")
+	ports, err := client.ListDevicePorts(ctx, &id)
 	if err != nil {
 		return nil
 	}
 
 	list := make([]flags.Completion, 0)
-	for _, item := range items.([]interface{}) {
-		val := item.(*dynamic.Message)
-		pn := strconv.FormatUint(uint64(val.GetFieldByName("port_no").(uint32)), 10)
+	for _, item := range ports.Items {
+		pn := strconv.FormatUint(uint64(item.PortNo), 10)
 		if strings.HasPrefix(pn, match) {
 			list = append(list, flags.Completion{Item: pn})
 		}
@@ -238,40 +219,20 @@
 	}
 	defer conn.Close()
 
-	descriptor, method, err := GetMethod("device-list")
-	if err != nil {
-		return nil
-	}
+	client := voltha.NewVolthaServiceClient(conn)
 
 	ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
 	defer cancel()
 
-	h := &RpcEventHandler{}
-	err = grpcurl.InvokeRPC(ctx, descriptor, conn, method, []string{}, h, h.GetParams)
-	if err != nil {
-		return nil
-	}
-
-	if h.Status != nil && h.Status.Err() != nil {
-		return nil
-	}
-
-	d, err := dynamic.AsDynamicMessage(h.Response)
-	if err != nil {
-		return nil
-	}
-
-	items, err := d.TryGetFieldByName("items")
+	devices, err := client.ListDevices(ctx, &empty.Empty{})
 	if err != nil {
 		return nil
 	}
 
 	list := make([]flags.Completion, 0)
-	for _, item := range items.([]interface{}) {
-		val := item.(*dynamic.Message)
-		id := val.GetFieldByName("id").(string)
-		if strings.HasPrefix(id, match) {
-			list = append(list, flags.Completion{Item: id})
+	for _, item := range devices.Items {
+		if strings.HasPrefix(item.Id, match) {
+			list = append(list, flags.Completion{Item: item.Id})
 		}
 	}
 
@@ -286,30 +247,12 @@
 	}
 	defer conn.Close()
 
-	descriptor, method, err := GetMethod("device-list")
-	if err != nil {
-		return err
-	}
+	client := voltha.NewVolthaServiceClient(conn)
 
 	ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
 	defer cancel()
 
-	h := &RpcEventHandler{}
-	err = grpcurl.InvokeRPC(ctx, descriptor, conn, method, []string{}, h, h.GetParams)
-	if err != nil {
-		return err
-	}
-
-	if h.Status != nil && h.Status.Err() != nil {
-		return h.Status.Err()
-	}
-
-	d, err := dynamic.AsDynamicMessage(h.Response)
-	if err != nil {
-		return err
-	}
-
-	items, err := d.TryGetFieldByName("items")
+	devices, err := client.ListDevices(ctx, &empty.Empty{})
 	if err != nil {
 		return err
 	}
@@ -327,10 +270,9 @@
 		orderBy = GetCommandOptionWithDefault("device-list", "order", "")
 	}
 
-	data := make([]model.Device, len(items.([]interface{})))
-	for i, item := range items.([]interface{}) {
-		val := item.(*dynamic.Message)
-		data[i].PopulateFrom(val)
+	// Make sure json output prints an empty list, not "null"
+	if devices.Items == nil {
+		devices.Items = make([]*voltha.Device, 0)
 	}
 
 	result := CommandResult{
@@ -339,7 +281,7 @@
 		OrderBy:   orderBy,
 		OutputAs:  toOutputType(options.OutputAs),
 		NameLimit: options.NameLimit,
-		Data:      data,
+		Data:      devices.Items,
 	}
 
 	GenerateOutput(&result)
@@ -348,17 +290,17 @@
 
 func (options *DeviceCreate) Execute(args []string) error {
 
-	dm := make(map[string]interface{})
+	device := voltha.Device{}
 	if options.HostAndPort != "" {
-		dm["host_and_port"] = options.HostAndPort
+		device.Address = &voltha.Device_HostAndPort{HostAndPort: options.HostAndPort}
 	} else if options.IPAddress != "" {
-		dm["ipv4_address"] = options.IPAddress
+		device.Address = &voltha.Device_Ipv4Address{Ipv4Address: options.IPAddress}
 	}
 	if options.MACAddress != "" {
-		dm["mac_address"] = strings.ToLower(options.MACAddress)
+		device.MacAddress = strings.ToLower(options.MACAddress)
 	}
 	if options.DeviceType != "" {
-		dm["type"] = options.DeviceType
+		device.Type = options.DeviceType
 	}
 
 	conn, err := NewConnection()
@@ -367,29 +309,17 @@
 	}
 	defer conn.Close()
 
-	descriptor, method, err := GetMethod("device-create")
-	if err != nil {
-		return err
-	}
+	client := voltha.NewVolthaServiceClient(conn)
 
 	ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
 	defer cancel()
 
-	h := &RpcEventHandler{
-		Fields: map[string]map[string]interface{}{"voltha.Device": dm},
-	}
-	err = grpcurl.InvokeRPC(ctx, descriptor, conn, method, []string{}, h, h.GetParams)
+	createdDevice, err := client.CreateDevice(ctx, &device)
 	if err != nil {
 		return err
-	} else if h.Status != nil && h.Status.Err() != nil {
-		return h.Status.Err()
 	}
 
-	resp, err := dynamic.AsDynamicMessage(h.Response)
-	if err != nil {
-		return err
-	}
-	fmt.Printf("%s\n", resp.GetFieldByName("id").(string))
+	fmt.Printf("%s\n", createdDevice.Id)
 
 	return nil
 }
@@ -402,29 +332,20 @@
 	}
 	defer conn.Close()
 
-	descriptor, method, err := GetMethod("device-delete")
-	if err != nil {
-		return err
-	}
+	client := voltha.NewVolthaServiceClient(conn)
 
 	var lastErr error
 	for _, i := range options.Args.Ids {
-
-		h := &RpcEventHandler{
-			Fields: map[string]map[string]interface{}{ParamNames[GlobalConfig.ApiVersion]["ID"]: {"id": i}},
-		}
 		ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
 		defer cancel()
 
-		err = grpcurl.InvokeRPC(ctx, descriptor, conn, method, []string{}, h, h.GetParams)
+		id := voltha.ID{Id: string(i)}
+
+		_, err := client.DeleteDevice(ctx, &id)
 		if err != nil {
 			Error.Printf("Error while deleting '%s': %s\n", i, err)
 			lastErr = err
 			continue
-		} else if h.Status != nil && h.Status.Err() != nil {
-			Error.Printf("Error while deleting '%s': %s\n", i, ErrorToString(h.Status.Err()))
-			lastErr = h.Status.Err()
-			continue
 		}
 		fmt.Printf("%s\n", i)
 	}
@@ -442,28 +363,20 @@
 	}
 	defer conn.Close()
 
-	descriptor, method, err := GetMethod("device-enable")
-	if err != nil {
-		return err
-	}
+	client := voltha.NewVolthaServiceClient(conn)
 
 	var lastErr error
 	for _, i := range options.Args.Ids {
-		h := &RpcEventHandler{
-			Fields: map[string]map[string]interface{}{ParamNames[GlobalConfig.ApiVersion]["ID"]: {"id": i}},
-		}
 		ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
 		defer cancel()
 
-		err = grpcurl.InvokeRPC(ctx, descriptor, conn, method, []string{}, h, h.GetParams)
+		id := voltha.ID{Id: string(i)}
+
+		_, err := client.EnableDevice(ctx, &id)
 		if err != nil {
 			Error.Printf("Error while enabling '%s': %s\n", i, err)
 			lastErr = err
 			continue
-		} else if h.Status != nil && h.Status.Err() != nil {
-			Error.Printf("Error while enabling '%s': %s\n", i, ErrorToString(h.Status.Err()))
-			lastErr = h.Status.Err()
-			continue
 		}
 		fmt.Printf("%s\n", i)
 	}
@@ -481,28 +394,20 @@
 	}
 	defer conn.Close()
 
-	descriptor, method, err := GetMethod("device-disable")
-	if err != nil {
-		return err
-	}
+	client := voltha.NewVolthaServiceClient(conn)
 
 	var lastErr error
 	for _, i := range options.Args.Ids {
-		h := &RpcEventHandler{
-			Fields: map[string]map[string]interface{}{ParamNames[GlobalConfig.ApiVersion]["ID"]: {"id": i}},
-		}
 		ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
 		defer cancel()
 
-		err = grpcurl.InvokeRPC(ctx, descriptor, conn, method, []string{}, h, h.GetParams)
+		id := voltha.ID{Id: string(i)}
+
+		_, err := client.DisableDevice(ctx, &id)
 		if err != nil {
 			Error.Printf("Error while disabling '%s': %s\n", i, err)
 			lastErr = err
 			continue
-		} else if h.Status != nil && h.Status.Err() != nil {
-			Error.Printf("Error while disabling '%s': %s\n", i, ErrorToString(h.Status.Err()))
-			lastErr = h.Status.Err()
-			continue
 		}
 		fmt.Printf("%s\n", i)
 	}
@@ -520,28 +425,20 @@
 	}
 	defer conn.Close()
 
-	descriptor, method, err := GetMethod("device-reboot")
-	if err != nil {
-		return err
-	}
+	client := voltha.NewVolthaServiceClient(conn)
 
 	var lastErr error
 	for _, i := range options.Args.Ids {
-		h := &RpcEventHandler{
-			Fields: map[string]map[string]interface{}{ParamNames[GlobalConfig.ApiVersion]["ID"]: {"id": i}},
-		}
 		ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
 		defer cancel()
 
-		err = grpcurl.InvokeRPC(ctx, descriptor, conn, method, []string{}, h, h.GetParams)
+		id := voltha.ID{Id: string(i)}
+
+		_, err := client.RebootDevice(ctx, &id)
 		if err != nil {
 			Error.Printf("Error while rebooting '%s': %s\n", i, err)
 			lastErr = err
 			continue
-		} else if h.Status != nil && h.Status.Err() != nil {
-			Error.Printf("Error while rebooting '%s': %s\n", i, ErrorToString(h.Status.Err()))
-			lastErr = h.Status.Err()
-			continue
 		}
 		fmt.Printf("%s\n", i)
 	}
@@ -560,32 +457,14 @@
 	}
 	defer conn.Close()
 
-	descriptor, method, err := GetMethod("device-ports")
-	if err != nil {
-		return err
-	}
+	client := voltha.NewVolthaServiceClient(conn)
 
 	ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
 	defer cancel()
 
-	h := &RpcEventHandler{
-		Fields: map[string]map[string]interface{}{ParamNames[GlobalConfig.ApiVersion]["ID"]: {"id": options.Args.Id}},
-	}
-	err = grpcurl.InvokeRPC(ctx, descriptor, conn, method, []string{}, h, h.GetParams)
-	if err != nil {
-		return err
-	}
+	id := voltha.ID{Id: string(options.Args.Id)}
 
-	if h.Status != nil && h.Status.Err() != nil {
-		return h.Status.Err()
-	}
-
-	d, err := dynamic.AsDynamicMessage(h.Response)
-	if err != nil {
-		return err
-	}
-
-	items, err := d.TryGetFieldByName("items")
+	ports, err := client.ListDevicePorts(ctx, &id)
 	if err != nil {
 		return err
 	}
@@ -603,18 +482,13 @@
 		orderBy = GetCommandOptionWithDefault("device-ports", "order", "")
 	}
 
-	data := make([]model.DevicePort, len(items.([]interface{})))
-	for i, item := range items.([]interface{}) {
-		data[i].PopulateFrom(item.(*dynamic.Message))
-	}
-
 	result := CommandResult{
 		Format:    format.Format(outputFormat),
 		Filter:    options.Filter,
 		OrderBy:   orderBy,
 		OutputAs:  toOutputType(options.OutputAs),
 		NameLimit: options.NameLimit,
-		Data:      data,
+		Data:      ports.Items,
 	}
 
 	GenerateOutput(&result)
@@ -640,32 +514,18 @@
 	}
 	defer conn.Close()
 
-	descriptor, method, err := GetMethod("device-inspect")
-	if err != nil {
-		return err
-	}
+	client := voltha.NewVolthaServiceClient(conn)
 
 	ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
 	defer cancel()
 
-	h := &RpcEventHandler{
-		Fields: map[string]map[string]interface{}{ParamNames[GlobalConfig.ApiVersion]["ID"]: {"id": options.Args.Id}},
-	}
-	err = grpcurl.InvokeRPC(ctx, descriptor, conn, method, []string{"Get-Depth: 2"}, h, h.GetParams)
-	if err != nil {
-		return err
-	} else if h.Status != nil && h.Status.Err() != nil {
-		return h.Status.Err()
-	}
+	id := voltha.ID{Id: string(options.Args.Id)}
 
-	d, err := dynamic.AsDynamicMessage(h.Response)
+	device, err := client.GetDevice(ctx, &id)
 	if err != nil {
 		return err
 	}
 
-	device := &model.Device{}
-	device.PopulateFrom(d)
-
 	outputFormat := CharReplacer.Replace(options.Format)
 	if outputFormat == "" {
 		outputFormat = GetCommandOptionWithDefault("device-inspect", "format", DEFAULT_DEVICE_INSPECT_FORMAT)
@@ -692,30 +552,23 @@
 	}
 	defer conn.Close()
 
-	descriptor, method, err := GetMethod("device-port-enable")
-	if err != nil {
-		return err
-	}
+	client := voltha.NewVolthaServiceClient(conn)
 
-	h := &RpcEventHandler{
-		Fields: map[string]map[string]interface{}{ParamNames[GlobalConfig.ApiVersion]["port"]: {"device_id": options.Args.Id, "port_no": options.Args.PortId}},
-	}
 	ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
 	defer cancel()
 
-	err = grpcurl.InvokeRPC(ctx, descriptor, conn, method, []string{}, h, h.GetParams)
+	port := voltha.Port{DeviceId: string(options.Args.Id), PortNo: uint32(options.Args.PortId)}
+
+	_, err = client.EnablePort(ctx, &port)
 	if err != nil {
 		Error.Printf("Error enabling port number %v on device Id %s,err=%s\n", options.Args.PortId, options.Args.Id, ErrorToString(err))
 		return err
-	} else if h.Status != nil && h.Status.Err() != nil {
-		Error.Printf("Error enabling port number %v on device Id %s,err=%s\n", options.Args.PortId, options.Args.Id, ErrorToString(h.Status.Err()))
-		return h.Status.Err()
 	}
 
 	return nil
 }
 
-/*Device Port Disable */
+/*Device  Port Disable */
 func (options *DevicePortDisable) Execute(args []string) error {
 	conn, err := NewConnection()
 	if err != nil {
@@ -723,27 +576,27 @@
 	}
 	defer conn.Close()
 
-	descriptor, method, err := GetMethod("device-port-disable")
-	if err != nil {
-		return err
-	}
-	h := &RpcEventHandler{
-		Fields: map[string]map[string]interface{}{ParamNames[GlobalConfig.ApiVersion]["port"]: {"device_id": options.Args.Id, "port_no": options.Args.PortId}},
-	}
+	client := voltha.NewVolthaServiceClient(conn)
+
 	ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
 	defer cancel()
 
-	err = grpcurl.InvokeRPC(ctx, descriptor, conn, method, []string{}, h, h.GetParams)
+	port := voltha.Port{DeviceId: string(options.Args.Id), PortNo: uint32(options.Args.PortId)}
+
+	_, err = client.DisablePort(ctx, &port)
 	if err != nil {
-		Error.Printf("Error disabling port number %v on device Id %s,err=%s\n", options.Args.PortId, options.Args.Id, ErrorToString(err))
+		Error.Printf("Error enabling port number %v on device Id %s,err=%s\n", options.Args.PortId, options.Args.Id, ErrorToString(err))
 		return err
-	} else if h.Status != nil && h.Status.Err() != nil {
-		Error.Printf("Error disabling port number %v on device Id %s,err=%s\n", options.Args.PortId, options.Args.Id, ErrorToString(h.Status.Err()))
-		return h.Status.Err()
 	}
+
 	return nil
 }
 
+type ReturnValueRow struct {
+	Name   string      `json:"name"`
+	Result interface{} `json:"result"`
+}
+
 /*Device  get Onu Distance */
 func (options *DeviceGetExtValue) Execute(args []string) error {
 	conn, err := NewConnection()
@@ -752,51 +605,50 @@
 	}
 	defer conn.Close()
 
-	descriptor, method, err := GetMethod("get-ext-value")
-	if err != nil {
-		return err
+	client := voltha.NewVolthaServiceClient(conn)
+
+	valueflag, okay := common.ValueType_Type_value[string(options.Args.Valueflag)]
+	if !okay {
+		Error.Printf("Unknown valueflag %s\n", options.Args.Valueflag)
 	}
 
-	// TODO: support multiple flags (VOL-2946)
-	// TODO: a more extensible way to validate than a long if statement...  (VOL-2946)
-	if (options.Args.Valueflag != "DISTANCE") && (options.Args.Valueflag != "EMPTY") {
-		return errors.New("ValueFlag must be either DISTANCE or EMPTY")
-	}
-
-	// value is an enum, so we need to map from an enum name to a value
-	valTypeDescriptor, err := descriptor.FindSymbol("common.ValueType")
-	if err != nil {
-		return err
-	}
-	valueTypeTypeDescriptor := valTypeDescriptor.GetFile().FindEnum("common.ValueType.Type")
-	eValue := valueTypeTypeDescriptor.FindValueByName(string(options.Args.Valueflag)).GetNumber()
-
-	h := &RpcEventHandler{
-		Fields: map[string]map[string]interface{}{ParamNames[GlobalConfig.ApiVersion]["ValueSpecifier"]: {"id": options.Args.Id, "value": eValue}},
-	}
+	val := voltha.ValueSpecifier{Id: string(options.Args.Id), Value: common.ValueType_Type(valueflag)}
 
 	ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
 	defer cancel()
 
-	err = grpcurl.InvokeRPC(ctx, descriptor, conn, method, []string{}, h, h.GetParams)
+	rv, err := client.GetExtValue(ctx, &val)
 	if err != nil {
 		Error.Printf("Error getting value on device Id %s,err=%s\n", options.Args.Id, ErrorToString(err))
 		return err
-	} else if h.Status != nil && h.Status.Err() != nil {
-		Error.Printf("Error Error getting distance on device Id %s,err=%s\n", options.Args.Id, ErrorToString(h.Status.Err()))
-		return h.Status.Err()
 	}
 
-	d, err := dynamic.AsDynamicMessage(h.Response)
-	if err != nil {
-		return err
+	var rows []ReturnValueRow
+	for name, num := range common.ValueType_Type_value {
+		if num == 0 {
+			// EMPTY is not a real value
+			continue
+		}
+		if (rv.Error & uint32(num)) != 0 {
+			row := ReturnValueRow{Name: name, Result: "Error"}
+			rows = append(rows, row)
+		}
+		if (rv.Unsupported & uint32(num)) != 0 {
+			row := ReturnValueRow{Name: name, Result: "Unsupported"}
+			rows = append(rows, row)
+		}
+		if (rv.Set & uint32(num)) != 0 {
+			switch name {
+			case "DISTANCE":
+				row := ReturnValueRow{Name: name, Result: rv.Distance}
+				rows = append(rows, row)
+			default:
+				row := ReturnValueRow{Name: name, Result: "Unimplemented-in-voltctl"}
+				rows = append(rows, row)
+			}
+		}
 	}
 
-	returnValues := &model.ReturnValues{}
-	returnValues.PopulateFrom(d)
-
-	returnRows := returnValues.GetKeyValuePairs(valueTypeTypeDescriptor.GetValues())
-
 	outputFormat := CharReplacer.Replace(options.Format)
 	if outputFormat == "" {
 		outputFormat = GetCommandOptionWithDefault("device-value-get", "format", DEFAULT_DEVICE_VALUE_GET_FORMAT)
@@ -806,7 +658,7 @@
 		Format:    format.Format(outputFormat),
 		OutputAs:  toOutputType(options.OutputAs),
 		NameLimit: options.NameLimit,
-		Data:      returnRows,
+		Data:      rows,
 	}
 	GenerateOutput(&result)
 	return nil