Create uni command in BBSimctl
Clean up UNI command and add filtering to UNI services

Change-Id: I1a54d786ca71a602c0ca0113f030c6caa1bc9cc7
diff --git a/internal/bbr/devices/validate.go b/internal/bbr/devices/validate.go
index a5f1d85..e0e0f99 100644
--- a/internal/bbr/devices/validate.go
+++ b/internal/bbr/devices/validate.go
@@ -22,6 +22,7 @@
 	"time"
 
 	"github.com/opencord/bbsim/api/bbsim"
+	pb "github.com/opencord/bbsim/api/bbsim"
 	log "github.com/sirupsen/logrus"
 	"google.golang.org/grpc"
 )
@@ -35,7 +36,12 @@
 	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
 	defer cancel()
 
-	services, err := client.GetServices(ctx, &bbsim.Empty{})
+	req := pb.UNIRequest{
+		OnuSerialNumber: "",
+		UniID:           "",
+	}
+
+	services, err := client.GetServices(ctx, &req)
 
 	if err != nil {
 		log.WithFields(log.Fields{
diff --git a/internal/bbsim/api/onus_handler.go b/internal/bbsim/api/onus_handler.go
index 0a67105..4ea60e7 100644
--- a/internal/bbsim/api/onus_handler.go
+++ b/internal/bbsim/api/onus_handler.go
@@ -583,3 +583,19 @@
 
 	return res, nil
 }
+
+func (s BBSimServer) GetUnis(ctx context.Context, req *bbsim.Empty) (*bbsim.UNIs, error) {
+	onus, err := s.GetONUs(ctx, req)
+
+	if err != nil {
+		return nil, err
+	}
+	unis := []*bbsim.UNI{}
+	for _, onu := range onus.Items {
+		unis = append(unis, onu.Unis...)
+	}
+	unis_ret := bbsim.UNIs{
+		Items: unis,
+	}
+	return &unis_ret, nil
+}
diff --git a/internal/bbsim/api/services_handler.go b/internal/bbsim/api/services_handler.go
index cfb971f..f016919 100644
--- a/internal/bbsim/api/services_handler.go
+++ b/internal/bbsim/api/services_handler.go
@@ -20,6 +20,7 @@
 	"context"
 	"github.com/opencord/bbsim/api/bbsim"
 	"github.com/opencord/bbsim/internal/bbsim/devices"
+	"strconv"
 )
 
 func convertBBSimServiceToProtoService(s *devices.Service) *bbsim.Service {
@@ -50,7 +51,7 @@
 	return services
 }
 
-func (s BBSimServer) GetServices(ctx context.Context, req *bbsim.Empty) (*bbsim.Services, error) {
+func (s BBSimServer) GetServices(ctx context.Context, req *bbsim.UNIRequest) (*bbsim.Services, error) {
 
 	services := bbsim.Services{
 		Items: []*bbsim.Service{},
@@ -63,7 +64,16 @@
 			for _, u := range o.UniPorts {
 				uni := u.(*devices.UniPort)
 				s := convertBBsimServicesToProtoServices(uni.Services)
-				services.Items = append(services.Items, s...)
+				for _, service := range s {
+					intVar, err := strconv.Atoi(req.UniID)
+					if req.UniID == "" && req.OnuSerialNumber == "" {
+						services.Items = append(services.Items, s...)
+					} else if err == nil && service.UniId == uint32(intVar) && service.OnuSn == req.OnuSerialNumber {
+						services.Items = append(services.Items, s...)
+					} else if req.UniID == "" && service.OnuSn == req.OnuSerialNumber {
+						services.Items = append(services.Items, s...)
+					}
+				}
 			}
 		}
 	}
diff --git a/internal/bbsimctl/commands/onu.go b/internal/bbsimctl/commands/onu.go
index bef627e..4ae8841 100644
--- a/internal/bbsimctl/commands/onu.go
+++ b/internal/bbsimctl/commands/onu.go
@@ -35,8 +35,6 @@
 const (
 	DEFAULT_ONU_DEVICE_HEADER_FORMAT               = "table{{ .PonPortID }}\t{{ .ID }}\t{{ .SerialNumber }}\t{{ .OperState }}\t{{ .InternalState }}\t{{ .ImageSoftwareExpectedSections }}\t{{ .ImageSoftwareReceivedSections }}\t{{ .ActiveImageEntityId }}\t{{ .CommittedImageEntityId }}"
 	DEFAULT_ONU_DEVICE_HEADER_FORMAT_WITH_SERVICES = "table{{ .PonPortID }}\t{{ .ID }}\t{{ .SerialNumber }}\t{{ .OperState }}\t{{ .InternalState }}\t{{ .ImageSoftwareExpectedSections }}\t{{ .ImageSoftwareReceivedSections }}\t{{ .ActiveImageEntityId }}\t{{ .CommittedImageEntityId }}\t{{ .Unis }}"
-	DEFAULT_UNI_HEADER_FORMAT                      = "table{{ .OnuSn }}\t{{ .OnuID }}\t{{ .ID }}\t{{ .MeID }}\t{{ .PortNo }}\t{{ .OperState }}"
-	DEFAULT_UNI_HEADER_FORMAT_WITH_SERVICES        = "table{{ .OnuSn }}\t{{ .OnuID }}\t{{ .ID }}\t{{ .MeID }}\t{{ .PortNo }}\t{{ .OperState }}\t{{ .Services }}"
 )
 
 type OnuSnString string
@@ -111,10 +109,15 @@
 	} `positional-args:"yes" required:"yes"`
 }
 
+type ONUServices struct {
+	Args struct {
+		OnuSn OnuSnString
+	} `positional-args:"yes"`
+}
+
 type ONUOptions struct {
 	List              ONUList              `command:"list"`
 	Get               ONUGet               `command:"get"`
-	Unis              ONUUnis              `command:"unis"`
 	ShutDown          ONUShutDown          `command:"shutdown"`
 	PowerOn           ONUPowerOn           `command:"poweron"`
 	RestartEapol      ONUEapolRestart      `command:"auth_restart"`
@@ -123,6 +126,7 @@
 	TrafficSchedulers ONUTrafficSchedulers `command:"traffic_schedulers"`
 	Alarms            AlarmOptions         `command:"alarms"`
 	Flows             ONUFlows             `command:"flows"`
+	Services          ONUServices          `command:"services"`
 }
 
 func RegisterONUCommands(parser *flags.Parser) {
@@ -192,36 +196,6 @@
 	return nil
 }
 
-func (options *ONUUnis) Execute(args []string) error {
-
-	client, conn := connect()
-	defer conn.Close()
-
-	ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
-	defer cancel()
-	req := pb.ONURequest{
-		SerialNumber: string(options.Args.OnuSn),
-	}
-	res, err := client.GetOnuUnis(ctx, &req)
-
-	if err != nil {
-		log.Fatalf("Cannot not get unis for ONU %s: %v", options.Args.OnuSn, err)
-		return err
-	}
-
-	var tableFormat format.Format
-	if options.Verbose {
-		tableFormat = format.Format(DEFAULT_UNI_HEADER_FORMAT_WITH_SERVICES)
-	} else {
-		tableFormat = format.Format(DEFAULT_UNI_HEADER_FORMAT)
-	}
-	if err := tableFormat.Execute(os.Stdout, true, res.Items); err != nil {
-		log.Fatalf("Error while formatting Unis table: %s", err)
-	}
-
-	return nil
-}
-
 func (options *ONUShutDown) Execute(args []string) error {
 
 	client, conn := connect()
@@ -539,3 +513,20 @@
 
 	return nil
 }
+
+func (options *ONUServices) Execute(args []string) error {
+	services, err := getServices(string(options.Args.OnuSn), "")
+
+	if err != nil {
+		log.Errorf("Cannot get services for ONU %s: %v", options.Args.OnuSn, err)
+		return err
+	}
+
+	// print out
+	tableFormat := format.Format(DEFAULT_SERVICE_HEADER_FORMAT)
+	if err := tableFormat.Execute(os.Stdout, true, services.Items); err != nil {
+		log.Fatalf("Error while formatting ONUs table: %s", err)
+	}
+
+	return nil
+}
diff --git a/internal/bbsimctl/commands/services.go b/internal/bbsimctl/commands/services.go
index d3b6bdd..1f3216a 100644
--- a/internal/bbsimctl/commands/services.go
+++ b/internal/bbsimctl/commands/services.go
@@ -41,7 +41,7 @@
 	_, _ = parser.AddCommand("service", "Service Commands", "Commands to interact with ONU Services", &ServiceOptions{})
 }
 
-func getServices() *pb.Services {
+func getServices(OnuSn string, UniID string) (*pb.Services, error) {
 
 	client, conn := connect()
 	defer conn.Close()
@@ -50,17 +50,23 @@
 	ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
 	defer cancel()
 
-	services, err := client.GetServices(ctx, &pb.Empty{})
+	req := pb.UNIRequest{
+		OnuSerialNumber: OnuSn,
+		UniID:           UniID,
+	}
+
+	services, err := client.GetServices(ctx, &req)
+
+	return services, err
+}
+
+func (options *ServiceList) Execute(args []string) error {
+	services, err := getServices("", "")
+
 	if err != nil {
 		log.Fatalf("could not get OLT: %v", err)
 		return nil
 	}
-	return services
-}
-
-func (options *ServiceList) Execute(args []string) error {
-	services := getServices()
-
 	// print out
 	tableFormat := format.Format(DEFAULT_SERVICE_HEADER_FORMAT)
 	if err := tableFormat.Execute(os.Stdout, true, services.Items); err != nil {
diff --git a/internal/bbsimctl/commands/uni.go b/internal/bbsimctl/commands/uni.go
new file mode 100644
index 0000000..ba1c795
--- /dev/null
+++ b/internal/bbsimctl/commands/uni.go
@@ -0,0 +1,141 @@
+/*
+ * Portions copyright 2019-present Open Networking Foundation
+ * Original copyright 2019-present Ciena Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package commands
+
+import (
+	"context"
+	"os"
+
+	"github.com/jessevdk/go-flags"
+	pb "github.com/opencord/bbsim/api/bbsim"
+	"github.com/opencord/bbsim/internal/bbsimctl/config"
+	"github.com/opencord/cordctl/pkg/format"
+	log "github.com/sirupsen/logrus"
+)
+
+const (
+	DEFAULT_UNI_HEADER_FORMAT               = "table{{ .OnuSn }}\t{{ .OnuID }}\t{{ .ID }}\t{{ .MeID }}\t{{ .PortNo }}\t{{ .OperState }}"
+	DEFAULT_UNI_HEADER_FORMAT_WITH_SERVICES = "table{{ .OnuSn }}\t{{ .OnuID }}\t{{ .ID }}\t{{ .MeID }}\t{{ .PortNo }}\t{{ .OperState }}\t{{ .Services }}"
+)
+
+type UniIdInt string
+
+type UNIList struct {
+	Verbose bool `short:"v" long:"verbose" description:"Print all the Unis from all the ONUs"`
+}
+
+type UNIGet struct {
+	Verbose bool `short:"v" long:"verbose" description:"Print all the informations we have about UNIs"`
+	Args    struct {
+		OnuSn OnuSnString
+	} `positional-args:"yes" required:"yes"`
+}
+
+type UNIServices struct {
+	Verbose bool `short:"v" long:"verbose" description:"Print all the Services from the specified UNI"`
+	Args    struct {
+		OnuSn OnuSnString
+		UniId UniIdInt
+	} `positional-args:"yes"`
+}
+
+type UNIOptions struct {
+	List     UNIList     `command:"list"`
+	Get      UNIGet      `command:"get"`
+	Services UNIServices `command:"services"`
+}
+
+func RegisterUNICommands(parser *flags.Parser) {
+	_, _ = parser.AddCommand("uni", "UNI Commands", "Commands to query and manipulate UNIs", &UNIOptions{})
+}
+
+func (options *UNIList) Execute(args []string) error {
+	client, conn := connect()
+	defer conn.Close()
+
+	// Contact the server and print out its response.
+	ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
+	defer cancel()
+
+	unis, err := client.GetUnis(ctx, &pb.Empty{})
+	if err != nil {
+		log.Fatalf("could not get UNIs: %v", err)
+		return err
+	}
+
+	var tableFormat format.Format
+	if options.Verbose {
+		tableFormat = format.Format(DEFAULT_UNI_HEADER_FORMAT_WITH_SERVICES)
+	} else {
+		tableFormat = format.Format(DEFAULT_UNI_HEADER_FORMAT)
+	}
+	if err := tableFormat.Execute(os.Stdout, true, unis.Items); err != nil {
+		log.Fatalf("Error while formatting Unis table: %s", err)
+	}
+
+	return nil
+}
+
+func (options *UNIGet) Execute(args []string) error {
+
+	client, conn := connect()
+	defer conn.Close()
+
+	ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
+	defer cancel()
+	req := pb.ONURequest{
+		SerialNumber: string(options.Args.OnuSn),
+	}
+	res, err := client.GetOnuUnis(ctx, &req)
+
+	if err != nil {
+		log.Fatalf("Cannot not get unis for ONU %s: %v", options.Args.OnuSn, err)
+		return err
+	}
+
+	var tableFormat format.Format
+	if options.Verbose {
+		tableFormat = format.Format(DEFAULT_UNI_HEADER_FORMAT_WITH_SERVICES)
+	} else {
+		tableFormat = format.Format(DEFAULT_UNI_HEADER_FORMAT)
+	}
+	if err := tableFormat.Execute(os.Stdout, true, res.Items); err != nil {
+		log.Fatalf("Error while formatting Unis table: %s", err)
+	}
+
+	return nil
+}
+
+//Get Services for specified UNI
+//First get UNIs from specified ONU SN
+//Then get Services from appropriate UNI
+func (options *UNIServices) Execute(args []string) error {
+	services, err := getServices(string(options.Args.OnuSn), string(options.Args.UniId))
+
+	if err != nil {
+		log.Fatalf("Cannot not get services for ONU %s and UNI %s: %v", options.Args.OnuSn, options.Args.UniId, err)
+		return err
+	}
+
+	tableFormat := format.Format(DEFAULT_SERVICE_HEADER_FORMAT)
+	if err := tableFormat.Execute(os.Stdout, true, services.Items); err != nil {
+		log.Fatalf("Error while formatting ONUs table: %s", err)
+	}
+
+	return nil
+}