[VOL-2778] Introducing Service definition in order to support the TT workflow

Change-Id: Ib171502e8940b5d0b219620a4503f7095d376d7a
diff --git a/internal/bbsimctl/commands/onu.go b/internal/bbsimctl/commands/onu.go
index 771df4c..20837b5 100644
--- a/internal/bbsimctl/commands/onu.go
+++ b/internal/bbsimctl/commands/onu.go
@@ -34,7 +34,8 @@
 )
 
 const (
-	DEFAULT_ONU_DEVICE_HEADER_FORMAT = "table{{ .PonPortID }}\t{{ .ID }}\t{{ .PortNo }}\t{{ .SerialNumber }}\t{{ .HwAddress }}\t{{ .STag }}\t{{ .CTag }}\t{{ .OperState }}\t{{ .InternalState }}"
+	DEFAULT_ONU_DEVICE_HEADER_FORMAT               = "table{{ .PonPortID }}\t{{ .ID }}\t{{ .PortNo }}\t{{ .SerialNumber }}\t{{ .OperState }}\t{{ .InternalState }}"
+	DEFAULT_ONU_DEVICE_HEADER_FORMAT_WITH_SERVICES = "table{{ .PonPortID }}\t{{ .ID }}\t{{ .PortNo }}\t{{ .SerialNumber }}\t{{ .OperState }}\t{{ .InternalState }}\t{{ .Services }}"
 )
 
 type OnuSnString string
@@ -44,9 +45,18 @@
 const IgmpLeaveKey string = "leave"
 const IgmpJoinKeyV3 string = "joinv3"
 
-type ONUList struct{}
+type ONUList struct {
+	Verbose bool `short:"v" long:"verbose" description:"Print all the informations we have about ONUs"`
+}
 
 type ONUGet struct {
+	Verbose bool `short:"v" long:"verbose" description:"Print all the informations we have about ONUs"`
+	Args    struct {
+		OnuSn OnuSnString
+	} `positional-args:"yes" required:"yes"`
+}
+
+type ONUServices struct {
 	Args struct {
 		OnuSn OnuSnString
 	} `positional-args:"yes" required:"yes"`
@@ -98,6 +108,7 @@
 type ONUOptions struct {
 	List              ONUList              `command:"list"`
 	Get               ONUGet               `command:"get"`
+	Services          ONUServices          `command:"services"`
 	ShutDown          ONUShutDown          `command:"shutdown"`
 	PowerOn           ONUPowerOn           `command:"poweron"`
 	RestartEapol      ONUEapolRestart      `command:"auth_restart"`
@@ -143,7 +154,12 @@
 	onus := getONUs()
 
 	// print out
-	tableFormat := format.Format(DEFAULT_ONU_DEVICE_HEADER_FORMAT)
+	var tableFormat format.Format
+	if options.Verbose {
+		tableFormat = format.Format(DEFAULT_ONU_DEVICE_HEADER_FORMAT_WITH_SERVICES)
+	} else {
+		tableFormat = format.Format(DEFAULT_ONU_DEVICE_HEADER_FORMAT)
+	}
 	if err := tableFormat.Execute(os.Stdout, true, onus.Items); err != nil {
 		log.Fatalf("Error while formatting ONUs table: %s", err)
 	}
@@ -163,11 +179,16 @@
 	res, err := client.GetONU(ctx, &req)
 
 	if err != nil {
-		log.Fatalf("Cannot not shutdown ONU %s: %v", options.Args.OnuSn, err)
+		log.Fatalf("Cannot not get ONU %s: %v", options.Args.OnuSn, err)
 		return err
 	}
 
-	tableFormat := format.Format(DEFAULT_ONU_DEVICE_HEADER_FORMAT)
+	var tableFormat format.Format
+	if options.Verbose {
+		tableFormat = format.Format(DEFAULT_ONU_DEVICE_HEADER_FORMAT_WITH_SERVICES)
+	} else {
+		tableFormat = format.Format(DEFAULT_ONU_DEVICE_HEADER_FORMAT)
+	}
 	if err := tableFormat.Execute(os.Stdout, true, []*pb.ONU{res}); err != nil {
 		log.Fatalf("Error while formatting ONUs table: %s", err)
 	}
@@ -175,6 +196,31 @@
 	return nil
 }
 
+func (options *ONUServices) 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.GetOnuServices(ctx, &req)
+
+	if err != nil {
+		log.Fatalf("Cannot not get services for ONU %s: %v", options.Args.OnuSn, err)
+		return err
+	}
+
+	tableFormat := format.Format(DEFAULT_SERVICE_HEADER_FORMAT)
+	if err := tableFormat.Execute(os.Stdout, true, res.Items); err != nil {
+		log.Fatalf("Error while formatting Services table: %s", err)
+	}
+
+	return nil
+}
+
 func (options *ONUShutDown) Execute(args []string) error {
 
 	client, conn := connect()
diff --git a/internal/bbsimctl/commands/services.go b/internal/bbsimctl/commands/services.go
new file mode 100644
index 0000000..929266d
--- /dev/null
+++ b/internal/bbsimctl/commands/services.go
@@ -0,0 +1,71 @@
+/*
+ * 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"
+	"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"
+	"os"
+)
+
+const (
+	DEFAULT_SERVICE_HEADER_FORMAT = "table{{ .OnuSn }}\t{{ .Name }}\t{{ .HwAddress }}\t{{ .STag }}\t{{ .CTag }}\t{{ .NeedsEapol }}\t{{ .NeedsDhcp }}\t{{ .NeedsIgmp }}\t{{ .GemPort }}\t{{ .EapolState }}\t{{ .DhcpState }}"
+)
+
+type ServiceList struct{}
+
+type ServiceOptions struct {
+	List ServiceList `command:"list"`
+}
+
+func RegisterServiceCommands(parser *flags.Parser) {
+	_, _ = parser.AddCommand("service", "Service Commands", "Commands to interact with ONU Services", &ServiceOptions{})
+}
+
+func getServices() *pb.Services {
+
+	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()
+
+	services, err := client.GetServices(ctx, &pb.Empty{})
+	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 {
+		log.Fatalf("Error while formatting ONUs table: %s", err)
+	}
+
+	return nil
+}