VOL-2474 Implement Alarm Simulation in bbsim and bbsimctl;
Release 0.0.12

Change-Id: I65e51729d4fdfd1daf386b1ea1732ff40bf31db7
diff --git a/internal/bbsim/alarmsim/alarmsim.go b/internal/bbsim/alarmsim/alarmsim.go
new file mode 100644
index 0000000..3c81265
--- /dev/null
+++ b/internal/bbsim/alarmsim/alarmsim.go
@@ -0,0 +1,214 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+
+ * 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 alarmsim
+
+import (
+	"context"
+	"fmt"
+	"github.com/opencord/bbsim/api/bbsim"
+	"github.com/opencord/bbsim/internal/bbsim/devices"
+	"github.com/opencord/voltha-protos/v2/go/openolt"
+	"strconv"
+)
+
+// Find a key in the optional AlarmParameters, convert it to an integer,
+// return 'def' if no key exists or it cannot be converted.
+func extractInt(params []*bbsim.AlarmParameter, name string, def int) int {
+	for _, kv := range params {
+		if kv.Key == name {
+			i, err := strconv.Atoi(kv.Value)
+			if err == nil {
+				return i
+			}
+		}
+	}
+	return def
+}
+
+func BuildAlarmIndication(req *bbsim.AlarmRequest, o *devices.OltDevice) (*openolt.AlarmIndication, error) {
+	var alarm *openolt.AlarmIndication
+	var onu *devices.Onu
+	var err error
+
+	if req.AlarmType != bbsim.AlarmType_LOS {
+		// No ONU Id for LOS
+		onu, err = o.FindOnuBySn(req.SerialNumber)
+		if err != nil {
+			return nil, err
+		}
+	}
+
+	switch req.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{
+				Status: req.Status,
+				OnuId:  onu.ID,
+				IntfId: onu.PonPortID,
+			}},
+		}
+	case bbsim.AlarmType_ONU_STARTUP_FAILURE:
+		alarm = &openolt.AlarmIndication{
+			Data: &openolt.AlarmIndication_OnuStartupFailInd{&openolt.OnuStartupFailureIndication{
+				Status: req.Status,
+				OnuId:  onu.ID,
+				IntfId: onu.PonPortID,
+			}},
+		}
+	case bbsim.AlarmType_ONU_SIGNAL_DEGRADE:
+		alarm = &openolt.AlarmIndication{
+			Data: &openolt.AlarmIndication_OnuSignalDegradeInd{&openolt.OnuSignalDegradeIndication{
+				Status:              req.Status,
+				OnuId:               onu.ID,
+				IntfId:              onu.PonPortID,
+				InverseBitErrorRate: uint32(extractInt(req.Parameters, "InverseBitErrorRate", 0)),
+			}},
+		}
+	case bbsim.AlarmType_ONU_DRIFT_OF_WINDOW:
+		alarm = &openolt.AlarmIndication{
+			Data: &openolt.AlarmIndication_OnuDriftOfWindowInd{&openolt.OnuDriftOfWindowIndication{
+				Status: req.Status,
+				OnuId:  onu.ID,
+				IntfId: onu.PonPortID,
+				Drift:  uint32(extractInt(req.Parameters, "Drift", 0)),
+				NewEqd: uint32(extractInt(req.Parameters, "NewEqd", 0)),
+			}},
+		}
+	case bbsim.AlarmType_ONU_LOSS_OF_OMCI_CHANNEL:
+		alarm = &openolt.AlarmIndication{
+			Data: &openolt.AlarmIndication_OnuLossOmciInd{&openolt.OnuLossOfOmciChannelIndication{
+				Status: req.Status,
+				OnuId:  onu.ID,
+				IntfId: onu.PonPortID,
+			}},
+		}
+	case bbsim.AlarmType_ONU_TRANSMISSION_INTERFERENCE_WARNING:
+		alarm = &openolt.AlarmIndication{
+			Data: &openolt.AlarmIndication_OnuTiwiInd{&openolt.OnuTransmissionInterferenceWarning{
+				Status: req.Status,
+				OnuId:  onu.ID,
+				IntfId: onu.PonPortID,
+				Drift:  uint32(extractInt(req.Parameters, "Drift", 0)),
+			}},
+		}
+	case bbsim.AlarmType_ONU_ACTIVATION_FAILURE:
+		alarm = &openolt.AlarmIndication{
+			Data: &openolt.AlarmIndication_OnuActivationFailInd{&openolt.OnuActivationFailureIndication{
+				OnuId:      onu.ID,
+				IntfId:     onu.PonPortID,
+				FailReason: uint32(extractInt(req.Parameters, "FailReason", 0)),
+			}},
+		}
+	case bbsim.AlarmType_ONU_PROCESSING_ERROR:
+		alarm = &openolt.AlarmIndication{
+			Data: &openolt.AlarmIndication_OnuProcessingErrorInd{&openolt.OnuProcessingErrorIndication{
+				OnuId:  onu.ID,
+				IntfId: onu.PonPortID,
+			}},
+		}
+	case bbsim.AlarmType_ONU_LOSS_OF_KEY_SYNC_FAILURE:
+		alarm = &openolt.AlarmIndication{
+			Data: &openolt.AlarmIndication_OnuLossOfSyncFailInd{&openolt.OnuLossOfKeySyncFailureIndication{
+				OnuId:  onu.ID,
+				IntfId: onu.PonPortID,
+				Status: req.Status,
+			}},
+		}
+	case bbsim.AlarmType_ONU_ITU_PON_STATS:
+		alarm = &openolt.AlarmIndication{
+			Data: &openolt.AlarmIndication_OnuItuPonStatsInd{&openolt.OnuItuPonStatsIndication{
+				OnuId:     onu.ID,
+				IntfId:    onu.PonPortID,
+				RdiErrors: uint32(extractInt(req.Parameters, "RdiErrors", 0)),
+			}},
+		}
+	case bbsim.AlarmType_ONU_ALARM_LOS:
+		alarm = &openolt.AlarmIndication{
+			Data: &openolt.AlarmIndication_OnuAlarmInd{&openolt.OnuAlarmIndication{
+				LosStatus: req.Status,
+				OnuId:     onu.ID,
+				IntfId:    onu.PonPortID,
+			}},
+		}
+	case bbsim.AlarmType_ONU_ALARM_LOB:
+		alarm = &openolt.AlarmIndication{
+			Data: &openolt.AlarmIndication_OnuAlarmInd{&openolt.OnuAlarmIndication{
+				LobStatus: req.Status,
+				OnuId:     onu.ID,
+				IntfId:    onu.PonPortID,
+			}},
+		}
+	case bbsim.AlarmType_ONU_ALARM_LOPC_MISS:
+		alarm = &openolt.AlarmIndication{
+			Data: &openolt.AlarmIndication_OnuAlarmInd{&openolt.OnuAlarmIndication{
+				LopcMissStatus: req.Status,
+				OnuId:          onu.ID,
+				IntfId:         onu.PonPortID,
+			}},
+		}
+	case bbsim.AlarmType_ONU_ALARM_LOPC_MIC_ERROR:
+		alarm = &openolt.AlarmIndication{
+			Data: &openolt.AlarmIndication_OnuAlarmInd{&openolt.OnuAlarmIndication{
+				LopcMicErrorStatus: req.Status,
+				OnuId:              onu.ID,
+				IntfId:             onu.PonPortID,
+			}},
+		}
+	case bbsim.AlarmType_ONU_ALARM_LOFI:
+		alarm = &openolt.AlarmIndication{
+			Data: &openolt.AlarmIndication_OnuAlarmInd{&openolt.OnuAlarmIndication{
+				LofiStatus: req.Status,
+				OnuId:      onu.ID,
+				IntfId:     onu.PonPortID,
+			}},
+		}
+	case bbsim.AlarmType_ONU_ALARM_LOAMI:
+		alarm = &openolt.AlarmIndication{
+			Data: &openolt.AlarmIndication_OnuAlarmInd{&openolt.OnuAlarmIndication{
+				LoamiStatus: req.Status,
+				OnuId:       onu.ID,
+				IntfId:      onu.PonPortID,
+			}},
+		}
+	default:
+		return nil, fmt.Errorf("Unknown alarm type %v", req.AlarmType)
+	}
+
+	return alarm, nil
+}
+
+func SimulateAlarm(ctx context.Context, req *bbsim.AlarmRequest, o *devices.OltDevice) error {
+	alarmIndication, err := BuildAlarmIndication(req, o)
+	if err != nil {
+		return err
+	}
+
+	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 09e4080..ca311ce 100644
--- a/internal/bbsim/api/grpc_api_server.go
+++ b/internal/bbsim/api/grpc_api_server.go
@@ -21,6 +21,7 @@
 	"fmt"
 
 	"github.com/opencord/bbsim/api/bbsim"
+	"github.com/opencord/bbsim/internal/bbsim/alarmsim"
 	"github.com/opencord/bbsim/internal/bbsim/devices"
 	"github.com/opencord/bbsim/internal/common"
 	log "github.com/sirupsen/logrus"
@@ -129,3 +130,16 @@
 		Caller: log.StandardLogger().ReportCaller,
 	}, nil
 }
+
+func (s BBSimServer) SetAlarmIndication(ctx context.Context, req *bbsim.AlarmRequest) (*bbsim.Response, error) {
+	o := devices.GetOLT()
+	err := alarmsim.SimulateAlarm(ctx, req, o)
+	if err != nil {
+		return nil, err
+	}
+
+	res := &bbsim.Response{}
+	res.StatusCode = int32(codes.OK)
+	res.Message = fmt.Sprintf("Alarm Indication Sent.")
+	return res, nil
+}
diff --git a/internal/bbsim/devices/messageTypes.go b/internal/bbsim/devices/messageTypes.go
index acfd832..fd1d4dc 100644
--- a/internal/bbsim/devices/messageTypes.go
+++ b/internal/bbsim/devices/messageTypes.go
@@ -42,6 +42,8 @@
 	SendEapolFlow  MessageType = 12
 	SendDhcpFlow   MessageType = 13
 	OnuPacketIn    MessageType = 14
+
+	AlarmIndication MessageType = 15 // message data is an openolt.AlarmIndication
 )
 
 func (m MessageType) String() string {
diff --git a/internal/bbsim/devices/olt.go b/internal/bbsim/devices/olt.go
index b16e405..e5ed6bc 100644
--- a/internal/bbsim/devices/olt.go
+++ b/internal/bbsim/devices/olt.go
@@ -411,6 +411,18 @@
 	return nil, errors.New(fmt.Sprintf("Cannot find NniPort with id %d in OLT %d", id, o.ID))
 }
 
+func (o *OltDevice) sendAlarmIndication(alarmInd *openolt.AlarmIndication, stream openolt.Openolt_EnableIndicationServer) {
+	data := &openolt.Indication_AlarmInd{AlarmInd: alarmInd}
+	if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
+		oltLogger.Errorf("Failed to send Alarm Indication: %v", err)
+		return
+	}
+
+	oltLogger.WithFields(log.Fields{
+		"AlarmIndication": alarmInd,
+	}).Debug("Sent Indication_AlarmInd")
+}
+
 func (o *OltDevice) sendOltIndication(msg OltIndicationMessage, stream openolt.Openolt_EnableIndicationServer) {
 	data := &openolt.Indication_OltInd{OltInd: &openolt.OltIndication{OperState: msg.OperState.String()}}
 	if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
@@ -546,6 +558,9 @@
 					o.OperState.Event("disable")
 				}
 				o.sendOltIndication(msg, stream)
+			case AlarmIndication:
+				alarmInd, _ := message.Data.(*openolt.AlarmIndication)
+				o.sendAlarmIndication(alarmInd, stream)
 			case NniIndication:
 				msg, _ := message.Data.(NniIndicationMessage)
 				o.sendNniIndication(msg, stream)
@@ -1016,3 +1031,14 @@
 	oltLogger.Info("received RemoveTrafficSchedulers")
 	return new(openolt.Empty), nil
 }
+
+// assumes caller has properly formulated an openolt.AlarmIndication
+func (o OltDevice) SendAlarmIndication(context context.Context, ind *openolt.AlarmIndication) error {
+	msg := Message{
+		Type: AlarmIndication,
+		Data: ind,
+	}
+
+	o.channel <- msg
+	return nil
+}
diff --git a/internal/bbsimctl/commands/alarms.go b/internal/bbsimctl/commands/alarms.go
new file mode 100755
index 0000000..3e3efec
--- /dev/null
+++ b/internal/bbsimctl/commands/alarms.go
@@ -0,0 +1,188 @@
+/*
+ * 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"
+	"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"
+	"strings"
+)
+
+const (
+	DEFAULT_ALARM_LIST_FORMAT = "table{{ .Name }}"
+)
+
+type AlarmListOutput struct {
+	Name string
+}
+
+type AlarmRaise struct {
+	Parameters []string `short:"p" description:"Additional Alarm Parameter in name=value form"`
+	Args       struct {
+		Name         string
+		SerialNumber string
+	} `positional-args:"yes" required:"yes"`
+}
+
+type AlarmClear struct {
+	Parameters []string `short:"p" description:"Additional Alarm Parameter in name=value form"`
+	Args       struct {
+		Name         string
+		SerialNumber string
+	} `positional-args:"yes" required:"yes"`
+}
+
+type AlarmList struct{}
+
+type AlarmOptions struct {
+	Raise AlarmRaise `command:"raise"`
+	Clear AlarmClear `command:"clear"`
+	List  AlarmList  `command:"list"`
+}
+
+var AlarmNameMap = map[string]pb.AlarmType_Types{"DyingGasp": pb.AlarmType_DYING_GASP,
+	"StartupFailure":           pb.AlarmType_ONU_STARTUP_FAILURE,
+	"SignalDegrade":            pb.AlarmType_ONU_SIGNAL_DEGRADE,
+	"DriftOfWindow":            pb.AlarmType_ONU_DRIFT_OF_WINDOW,
+	"LossOfOmciChannel":        pb.AlarmType_ONU_LOSS_OF_OMCI_CHANNEL,
+	"SignalsFailure":           pb.AlarmType_ONU_SIGNALS_FAILURE,
+	"TransmissionInterference": pb.AlarmType_ONU_TRANSMISSION_INTERFERENCE_WARNING,
+	"ActivationFailure":        pb.AlarmType_ONU_ACTIVATION_FAILURE,
+	"ProcessingError":          pb.AlarmType_ONU_PROCESSING_ERROR,
+	"LossOfKeySyncFailure":     pb.AlarmType_ONU_LOSS_OF_KEY_SYNC_FAILURE,
+
+	// Break out OnuAlarm into its subcases.
+	"LossOfSignal":   pb.AlarmType_ONU_ALARM_LOS,
+	"LossOfBurst":    pb.AlarmType_ONU_ALARM_LOB,
+	"LOPC_MISS":      pb.AlarmType_ONU_ALARM_LOPC_MISS,
+	"LOPC_MIC_ERROR": pb.AlarmType_ONU_ALARM_LOPC_MIC_ERROR,
+	"LossOfFrame":    pb.AlarmType_ONU_ALARM_LOFI,
+	"LossOfPloam":    pb.AlarmType_ONU_ALARM_LOAMI,
+
+	// Whole-PON / Non-onu-specific
+	"PonLossOfSignal": pb.AlarmType_LOS,
+}
+
+func alarmNameToEnum(name string) (*pb.AlarmType_Types, error) {
+	v, okay := AlarmNameMap[name]
+	if !okay {
+		return nil, fmt.Errorf("Unknown Alarm Name: %v", name)
+	}
+
+	return &v, nil
+}
+
+// add optional parameters from the command-line to the AlarmRequest
+func addParameters(parameters []string, req *pb.AlarmRequest) error {
+	req.Parameters = make([]*pb.AlarmParameter, len(parameters))
+	for i, kv := range parameters {
+		parts := strings.Split(kv, "=")
+		if len(parts) != 2 {
+			return fmt.Errorf("Invalid parameter %v", kv)
+		}
+		req.Parameters[i] = &pb.AlarmParameter{Key: parts[0], Value: parts[1]}
+	}
+	return nil
+}
+
+func RegisterAlarmCommands(parser *flags.Parser) {
+	parser.AddCommand("alarm", "Alarm Commands", "Commands to raise and clear alarms", &AlarmOptions{})
+}
+
+func (o *AlarmRaise) Execute(args []string) error {
+	alarmType, err := alarmNameToEnum(o.Args.Name)
+	if err != nil {
+		return err
+	}
+
+	client, conn := connect()
+	defer conn.Close()
+
+	ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
+	defer cancel()
+
+	req := pb.AlarmRequest{AlarmType: *alarmType,
+		SerialNumber: o.Args.SerialNumber,
+		Status:       "on"}
+
+	err = addParameters(o.Parameters, &req)
+	if err != nil {
+		return err
+	}
+
+	res, err := client.SetAlarmIndication(ctx, &req)
+
+	if err != nil {
+		log.Fatalf("Cannot raise alarm: %v", err)
+		return err
+	}
+
+	fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message))
+	return nil
+}
+
+func (o *AlarmClear) Execute(args []string) error {
+	alarmType, err := alarmNameToEnum(o.Args.Name)
+	if err != nil {
+		return err
+	}
+
+	client, conn := connect()
+	defer conn.Close()
+
+	ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
+	defer cancel()
+
+	req := pb.AlarmRequest{AlarmType: *alarmType,
+		SerialNumber: o.Args.SerialNumber,
+		Status:       "off"}
+
+	err = addParameters(o.Parameters, &req)
+	if err != nil {
+		return err
+	}
+
+	res, err := client.SetAlarmIndication(ctx, &req)
+
+	if err != nil {
+		log.Fatalf("Cannot clear alarm: %v", err)
+		return err
+	}
+
+	fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message))
+	return nil
+}
+
+func (o *AlarmList) Execute(args []string) error {
+	alarmNames := make([]AlarmListOutput, len(AlarmNameMap))
+	i := 0
+	for k := range AlarmNameMap {
+		alarmNames[i] = AlarmListOutput{Name: k}
+		i++
+	}
+	// print out
+	tableFormat := format.Format(DEFAULT_ALARM_LIST_FORMAT)
+	tableFormat.Execute(os.Stdout, true, alarmNames)
+	return nil
+}