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
+}