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