SEBA-827: Implement OLT Alarms with REST and bbsimctl
          separate onu and olt alarms
          rebase
          address review comments and print alarms table for list

Change-Id: I3bc8aa03908f973c3342ec93c7399895c48639a2
diff --git a/internal/bbsim/alarmsim/alarmsim.go b/internal/bbsim/alarmsim/alarmsim.go
index 9607526..266282a 100644
--- a/internal/bbsim/alarmsim/alarmsim.go
+++ b/internal/bbsim/alarmsim/alarmsim.go
@@ -19,14 +19,17 @@
 import (
 	"context"
 	"fmt"
+	"strconv"
+
 	"github.com/opencord/bbsim/api/bbsim"
 	"github.com/opencord/bbsim/internal/bbsim/devices"
 	"github.com/opencord/voltha-protos/v2/go/openolt"
-	"strconv"
+	"google.golang.org/grpc/codes"
+	"google.golang.org/grpc/status"
 )
 
-// AlarmNameMap string to enum map
-var AlarmNameMap = map[string]bbsim.AlarmType_Types{
+// OnuAlarmNameMap string to enum map
+var OnuAlarmNameMap = map[string]bbsim.AlarmType_Types{
 	"DyingGasp":                bbsim.AlarmType_DYING_GASP,
 	"StartupFailure":           bbsim.AlarmType_ONU_STARTUP_FAILURE,
 	"SignalDegrade":            bbsim.AlarmType_ONU_SIGNAL_DEGRADE,
@@ -45,13 +48,18 @@
 	"LOPC_MIC_ERROR": bbsim.AlarmType_ONU_ALARM_LOPC_MIC_ERROR,
 	"LossOfFrame":    bbsim.AlarmType_ONU_ALARM_LOFI,
 	"LossOfPloam":    bbsim.AlarmType_ONU_ALARM_LOAMI,
+}
 
-	// Whole-PON / Non-onu-specific
+// OltAlarmNameMap string to enum map
+var OltAlarmNameMap = map[string]bbsim.AlarmType_Types{
+	// PON / Non-onu-specific
 	"PonLossOfSignal": bbsim.AlarmType_LOS,
+	// NNI / Non-onu-specific
+	"NniLossOfSignal": bbsim.AlarmType_LOS,
 }
 
 func AlarmNameToEnum(name string) (*bbsim.AlarmType_Types, error) {
-	v, okay := AlarmNameMap[name]
+	v, okay := OnuAlarmNameMap[name]
 	if !okay {
 		return nil, fmt.Errorf("Unknown Alarm Name: %v", name)
 	}
@@ -73,8 +81,8 @@
 	return def
 }
 
-// BuildAlarmIndication function forms openolt alarmIndication as per AlarmRequest
-func BuildAlarmIndication(req *bbsim.AlarmRequest, o *devices.OltDevice) (*openolt.AlarmIndication, error) {
+// BuildOnuAlarmIndication function forms openolt alarmIndication as per ONUAlarmRequest
+func BuildOnuAlarmIndication(req *bbsim.ONUAlarmRequest, o *devices.OltDevice) (*openolt.AlarmIndication, error) {
 	var alarm *openolt.AlarmIndication
 	var onu *devices.Onu
 	var err error
@@ -93,14 +101,6 @@
 	}
 
 	switch *alarmType {
-	case bbsim.AlarmType_LOS:
-		alarm = &openolt.AlarmIndication{
-			Data: &openolt.AlarmIndication_LosInd{&openolt.LosIndication{
-				// No ONU Id for LOS
-				Status: req.Status,
-				IntfId: uint32(extractInt(req.Parameters, "InterfaceId", 0)),
-			}},
-		}
 	case bbsim.AlarmType_DYING_GASP:
 		alarm = &openolt.AlarmIndication{
 			Data: &openolt.AlarmIndication_DyingGaspInd{&openolt.DyingGaspIndication{
@@ -248,9 +248,9 @@
 	return alarm, nil
 }
 
-// SimulateAlarm accept request for alarms and send proper alarmIndication to openolt stream
-func SimulateAlarm(ctx context.Context, req *bbsim.AlarmRequest, o *devices.OltDevice) error {
-	alarmIndication, err := BuildAlarmIndication(req, o)
+// SimulateOnuAlarm accept request for Onu alarms and send proper alarmIndication to openolt stream
+func SimulateOnuAlarm(ctx context.Context, req *bbsim.ONUAlarmRequest, o *devices.OltDevice) error {
+	alarmIndication, err := BuildOnuAlarmIndication(req, o)
 	if err != nil {
 		return err
 	}
@@ -262,3 +262,73 @@
 
 	return nil
 }
+
+// InterfaceIDToPortNo converts InterfaceID to voltha PortID
+// Refer openolt adapter code(master) voltha-openolt-adapter/adaptercore/olt_platform.go: IntfIDToPortNo()
+func InterfaceIDToPortNo(req *bbsim.OLTAlarmRequest) uint32 {
+	// Converts interface-id to port-numbers that can be understood by the VOLTHA
+	if req.InterfaceType == "nni" || req.InterfaceType == "PonLossOfSignal" {
+		// nni at voltha starts with 1,048,576
+		// nni = 1,048,576 + InterfaceID
+		return 0x1<<20 + req.InterfaceID
+	} else if req.InterfaceType == "pon" || req.InterfaceType == "NniLossOfSignal" {
+		// pon = 536,870,912 + InterfaceID
+		return (0x2 << 28) + req.InterfaceID
+		// In bbsim, pon starts from 1
+	}
+	return 0
+}
+
+// IsPonPortPresentInOlt verifies if given Pon port is present in olt
+func IsPonPortPresentInOlt(PonPort uint32) bool {
+	o := devices.GetOLT()
+	for _, intf := range o.Pons {
+		if intf.ID == PonPort {
+			return true
+		}
+	}
+	return false
+}
+
+// IsNniPortPresentInOlt verifies if given nni port is present in olt
+func IsNniPortPresentInOlt(nniPort uint32) bool {
+	o := devices.GetOLT()
+	for _, intf := range o.Nnis {
+		if intf.ID == nniPort {
+			return true
+		}
+	}
+	return false
+}
+
+// SimulateOltAlarm accept request for Olt alarms and send proper alarmIndication to openolt stream
+func SimulateOltAlarm(ctx context.Context, req *bbsim.OLTAlarmRequest, o *devices.OltDevice) error {
+	var alarmIndication *openolt.AlarmIndication
+	var err error
+
+	//check if its a valid port id
+	switch req.InterfaceType {
+	case "nni":
+		if !IsNniPortPresentInOlt(uint32(req.InterfaceID)) {
+			return status.Errorf(codes.NotFound, strconv.Itoa(int(req.InterfaceID))+" NNI not present in olt")
+		}
+
+	case "pon":
+		if !IsPonPortPresentInOlt(uint32(req.InterfaceID)) {
+			return status.Errorf(codes.NotFound, strconv.Itoa(int(req.InterfaceID))+" PON not present in olt")
+		}
+	}
+	alarmIndication = &openolt.AlarmIndication{
+		Data: &openolt.AlarmIndication_LosInd{&openolt.LosIndication{
+			Status: req.Status,
+			IntfId: InterfaceIDToPortNo(req),
+		}},
+	}
+
+	err = o.SendAlarmIndication(ctx, alarmIndication)
+	if err != nil {
+		return err
+	}
+
+	return nil
+}
diff --git a/internal/bbsim/api/grpc_api_server.go b/internal/bbsim/api/grpc_api_server.go
index ca311ce..7191c2e 100644
--- a/internal/bbsim/api/grpc_api_server.go
+++ b/internal/bbsim/api/grpc_api_server.go
@@ -131,15 +131,29 @@
 	}, nil
 }
 
-func (s BBSimServer) SetAlarmIndication(ctx context.Context, req *bbsim.AlarmRequest) (*bbsim.Response, error) {
+func (s BBSimServer) SetOnuAlarmIndication(ctx context.Context, req *bbsim.ONUAlarmRequest) (*bbsim.Response, error) {
 	o := devices.GetOLT()
-	err := alarmsim.SimulateAlarm(ctx, req, o)
+	err := alarmsim.SimulateOnuAlarm(ctx, req, o)
 	if err != nil {
 		return nil, err
 	}
 
 	res := &bbsim.Response{}
 	res.StatusCode = int32(codes.OK)
-	res.Message = fmt.Sprintf("Alarm Indication Sent.")
+	res.Message = fmt.Sprintf("Onu Alarm Indication Sent.")
+	return res, nil
+}
+
+// SetOltAlarmIndication generates OLT Alarms for LOS
+func (s BBSimServer) SetOltAlarmIndication(ctx context.Context, req *bbsim.OLTAlarmRequest) (*bbsim.Response, error) {
+	o := devices.GetOLT()
+	err := alarmsim.SimulateOltAlarm(ctx, req, o)
+	if err != nil {
+		return nil, err
+	}
+
+	res := &bbsim.Response{}
+	res.StatusCode = int32(codes.OK)
+	res.Message = fmt.Sprintf("Olt Alarm Indication Sent.")
 	return res, nil
 }
diff --git a/internal/bbsimctl/commands/olt.go b/internal/bbsimctl/commands/olt.go
index 9ff6a30..d77cf77 100644
--- a/internal/bbsimctl/commands/olt.go
+++ b/internal/bbsimctl/commands/olt.go
@@ -47,12 +47,13 @@
 type OltReboot struct{}
 
 type oltOptions struct {
-	Get      OltGet      `command:"get"`
-	NNI      OltNNIs     `command:"nnis"`
-	PON      OltPONs     `command:"pons"`
-	Shutdown OltShutdown `command:"shutdown"`
-	Poweron  OltPoweron  `command:"poweron"`
-	Reboot   OltReboot   `command:"reboot"`
+	Get      OltGet          `command:"get"`
+	NNI      OltNNIs         `command:"nnis"`
+	PON      OltPONs         `command:"pons"`
+	Shutdown OltShutdown     `command:"shutdown"`
+	Poweron  OltPoweron      `command:"poweron"`
+	Reboot   OltReboot       `command:"reboot"`
+	Alarms   OltAlarmOptions `command:"alarms"`
 }
 
 func RegisterOltCommands(parser *flags.Parser) {
diff --git a/internal/bbsimctl/commands/oltalarms.go b/internal/bbsimctl/commands/oltalarms.go
new file mode 100755
index 0000000..645aade
--- /dev/null
+++ b/internal/bbsimctl/commands/oltalarms.go
@@ -0,0 +1,145 @@
+/*
+ * 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"
+	"fmt"
+	"os"
+	"strings"
+
+	"github.com/jessevdk/go-flags"
+	"github.com/olekukonko/tablewriter"
+	pb "github.com/opencord/bbsim/api/bbsim"
+	"github.com/opencord/bbsim/internal/bbsim/alarmsim"
+	"github.com/opencord/bbsim/internal/bbsimctl/config"
+	log "github.com/sirupsen/logrus"
+)
+
+const (
+	DEFAULT_OLT_ALARM_LIST_FORMAT = "table{{ .Name }}"
+)
+
+type OltInterfaceStatus string
+
+type OltInterfaceID uint32
+
+type OltAlarmNameString string
+
+type OltAlarmListOutput struct {
+	Name string
+}
+
+type OltAlarmRaise struct {
+	Args struct {
+		Name   OltAlarmNameString
+		IntfID OltInterfaceID
+	} `positional-args:"yes" required:"yes"`
+}
+
+type OltAlarmClear struct {
+	Args struct {
+		Name   OltAlarmNameString
+		IntfID OltInterfaceID
+	} `positional-args:"yes" required:"yes"`
+}
+
+type OltAlarmList struct{}
+
+type OltAlarmOptions struct {
+	Raise OltAlarmRaise `command:"raise"`
+	Clear OltAlarmClear `command:"clear"`
+	List  OltAlarmList  `command:"list"`
+}
+
+// Execute alarm raise
+func (o *OltAlarmRaise) Execute(args []string) error {
+	client, conn := connect()
+	defer conn.Close()
+
+	ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
+	defer cancel()
+
+	req := pb.OLTAlarmRequest{InterfaceType: string(o.Args.Name),
+		InterfaceID: uint32(o.Args.IntfID),
+		Status:      "on"}
+
+	res, err := client.SetOltAlarmIndication(ctx, &req)
+	if err != nil {
+		log.Fatalf("Cannot raise OLT alarm: %v", err)
+		return err
+	}
+
+	fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message))
+	return nil
+}
+
+// Execute alarm clear
+func (o *OltAlarmClear) Execute(args []string) error {
+	client, conn := connect()
+	defer conn.Close()
+
+	ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
+	defer cancel()
+
+	req := pb.OLTAlarmRequest{InterfaceType: string(o.Args.Name),
+		InterfaceID: uint32(o.Args.IntfID),
+		Status:      "off"}
+
+	res, err := client.SetOltAlarmIndication(ctx, &req)
+
+	if err != nil {
+		log.Fatalf("Cannot clear OLT alarm: %v", err)
+		return err
+	}
+
+	fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message))
+	return nil
+}
+
+// Execute OLT alarm list
+func (o *OltAlarmList) Execute(args []string) error {
+	OltAlarmsValue := [][]string{}
+	OltAlarmstable := tablewriter.NewWriter(os.Stdout)
+	fmt.Fprintf(os.Stdout, "OLT Alarms List:\n")
+	OltAlarmstable.SetHeader([]string{"OLT Alarms"})
+
+	alarmNames := make([]AlarmListOutput, len(alarmsim.OltAlarmNameMap))
+	i := 0
+	for k := range alarmsim.OltAlarmNameMap {
+		alarmNames[i] = AlarmListOutput{Name: k}
+		OltAlarmsValue = append(OltAlarmsValue, []string{k})
+		i++
+	}
+	for _, v := range OltAlarmsValue {
+		OltAlarmstable.Append(v)
+	}
+	OltAlarmstable.Render()
+	return nil
+}
+
+func (o *OltAlarmNameString) Complete(match string) []flags.Completion {
+	list := make([]flags.Completion, 0)
+	for k := range alarmsim.OltAlarmNameMap {
+		if strings.HasPrefix(k, match) {
+			list = append(list, flags.Completion{Item: k})
+		}
+	}
+
+	return list
+}
diff --git a/internal/bbsimctl/commands/onu.go b/internal/bbsimctl/commands/onu.go
index 9e41990..2815501 100644
--- a/internal/bbsimctl/commands/onu.go
+++ b/internal/bbsimctl/commands/onu.go
@@ -92,6 +92,7 @@
 	RestartDchp       ONUDhcpRestart       `command:"dhcp_restart"`
 	Igmp              ONUIgmp              `command:"igmp"`
 	TrafficSchedulers ONUTrafficSchedulers `command:"traffic_schedulers"`
+	Alarms            AlarmOptions         `command:"alarms"`
 }
 
 type ONUTrafficSchedulers struct {
diff --git a/internal/bbsimctl/commands/alarms.go b/internal/bbsimctl/commands/onualarms.go
similarity index 78%
rename from internal/bbsimctl/commands/alarms.go
rename to internal/bbsimctl/commands/onualarms.go
index 044d74a..dcf53a3 100755
--- a/internal/bbsimctl/commands/alarms.go
+++ b/internal/bbsimctl/commands/onualarms.go
@@ -20,18 +20,15 @@
 import (
 	"context"
 	"fmt"
+	"os"
+	"strings"
+
 	"github.com/jessevdk/go-flags"
+	"github.com/olekukonko/tablewriter"
 	pb "github.com/opencord/bbsim/api/bbsim"
 	"github.com/opencord/bbsim/internal/bbsim/alarmsim"
 	"github.com/opencord/bbsim/internal/bbsimctl/config"
-	"github.com/opencord/cordctl/pkg/format"
 	log "github.com/sirupsen/logrus"
-	"os"
-	"strings"
-)
-
-const (
-	DEFAULT_ALARM_LIST_FORMAT = "table{{ .Name }}"
 )
 
 type AlarmNameString string
@@ -64,8 +61,8 @@
 	List  AlarmList  `command:"list"`
 }
 
-// add optional parameters from the command-line to the AlarmRequest
-func addParameters(parameters []string, req *pb.AlarmRequest) error {
+// add optional parameters from the command-line to the ONUAlarmRequest
+func addParameters(parameters []string, req *pb.ONUAlarmRequest) error {
 	req.Parameters = make([]*pb.AlarmParameter, len(parameters))
 	for i, kv := range parameters {
 		parts := strings.Split(kv, "=")
@@ -77,10 +74,6 @@
 	return nil
 }
 
-func RegisterAlarmCommands(parser *flags.Parser) {
-	parser.AddCommand("alarm", "Alarm Commands", "Commands to raise and clear alarms", &AlarmOptions{})
-}
-
 // Execute alarm raise
 func (o *AlarmRaise) Execute(args []string) error {
 	client, conn := connect()
@@ -89,7 +82,7 @@
 	ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
 	defer cancel()
 
-	req := pb.AlarmRequest{AlarmType: string(o.Args.Name),
+	req := pb.ONUAlarmRequest{AlarmType: string(o.Args.Name),
 		SerialNumber: string(o.Args.SerialNumber),
 		Status:       "on"}
 
@@ -98,7 +91,7 @@
 		return err
 	}
 
-	res, err := client.SetAlarmIndication(ctx, &req)
+	res, err := client.SetOnuAlarmIndication(ctx, &req)
 	if err != nil {
 		log.Fatalf("Cannot raise alarm: %v", err)
 		return err
@@ -116,7 +109,7 @@
 	ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
 	defer cancel()
 
-	req := pb.AlarmRequest{AlarmType: string(o.Args.Name),
+	req := pb.ONUAlarmRequest{AlarmType: string(o.Args.Name),
 		SerialNumber: string(o.Args.SerialNumber),
 		Status:       "off"}
 
@@ -125,7 +118,7 @@
 		return err
 	}
 
-	res, err := client.SetAlarmIndication(ctx, &req)
+	res, err := client.SetOnuAlarmIndication(ctx, &req)
 
 	if err != nil {
 		log.Fatalf("Cannot clear alarm: %v", err)
@@ -136,23 +129,30 @@
 	return nil
 }
 
-// Execute alarm list
+// Execute OLT alarm list
 func (o *AlarmList) Execute(args []string) error {
-	alarmNames := make([]AlarmListOutput, len(alarmsim.AlarmNameMap))
+	OnuAlarmsValue := [][]string{}
+	OnuAlarmstable := tablewriter.NewWriter(os.Stdout)
+	fmt.Fprintf(os.Stdout, "ONU Alarms List:\n")
+	OnuAlarmstable.SetHeader([]string{"ONU Alarms"})
+
+	alarmNames := make([]AlarmListOutput, len(alarmsim.OnuAlarmNameMap))
 	i := 0
-	for k := range alarmsim.AlarmNameMap {
+	for k := range alarmsim.OnuAlarmNameMap {
 		alarmNames[i] = AlarmListOutput{Name: k}
+		OnuAlarmsValue = append(OnuAlarmsValue, []string{k})
 		i++
 	}
-	// print out
-	tableFormat := format.Format(DEFAULT_ALARM_LIST_FORMAT)
-	tableFormat.Execute(os.Stdout, true, alarmNames)
+	for _, v := range OnuAlarmsValue {
+		OnuAlarmstable.Append(v)
+	}
+	OnuAlarmstable.Render()
 	return nil
 }
 
 func (onuSn *AlarmNameString) Complete(match string) []flags.Completion {
 	list := make([]flags.Completion, 0)
-	for k := range alarmsim.AlarmNameMap {
+	for k := range alarmsim.OnuAlarmNameMap {
 		if strings.HasPrefix(k, match) {
 			list = append(list, flags.Completion{Item: k})
 		}