SEBA-432
SEBA-565
SEBA-654 (alarms)

implemented

fix Jenkins make errors
fix merge conflicts
address review comments

Change-Id: Ia2e95afb33ce55054afa1fcbd9beb6ada62dd764
diff --git a/device/device_olt.go b/device/device_olt.go
index daffcec..5f34a03 100644
--- a/device/device_olt.go
+++ b/device/device_olt.go
@@ -31,8 +31,9 @@
 	GetDevkey() Devkey
 }
 
+// Devkey key for OLT/ONU devices
 type Devkey struct {
-	ID uint32
+	ID     uint32
 	Intfid uint32
 }
 
@@ -47,30 +48,68 @@
 	Name               string
 	InternalState      DeviceState
 	OperState          string
-	Intfs              []intf
+	NniIntfs		   []nniIntf
+	PonIntfs		   []ponIntf
 	HeartbeatSignature uint32
-	mu            *sync.Mutex
+	mu                 *sync.Mutex
 }
 
-type intf struct {
-	Type      string
-	IntfID    uint32
-	OperState string
+// AlarmState informs about the present state of the supported alarms
+type AlarmState uint32
+
+const (
+	// PonLosCleared alarm state for PON-LOS
+	PonLosCleared AlarmState = iota
+	// NniLosCleared alarm state for NNI-LOS
+	NniLosCleared
+	// PonLosRaised alarm state for PON-LOS
+	PonLosRaised
+	// NniLosRaised  for NNI-LOS
+	NniLosRaised
+)
+
+type ponIntf struct {
+	Type       string
+	IntfID     uint32
+	OperState  string
+	AlarmState AlarmState
 }
 
+type nniIntf struct {
+	Type       string
+	IntfID     uint32
+	OperState  string
+	AlarmState AlarmState
+}
+
+// Constants for port types
+const (
+	IntfPon = "pon"
+	IntfNni = "nni"
+)
+
 /* OltState
 OLT_INACTIVE -> OLT_PREACTIVE -> ACTIVE
-    (ActivateOLT)   (Enable)
+        (ActivateOLT)      (Enable)
        <-              <-
 */
 
+// Constants for OLT states
 const (
-	OLT_INACTIVE DeviceState  = iota // OLT/ONUs are not instantiated
-	OLT_PREACTIVE        // Before PacketInDaemon Running
-	OLT_ACTIVE            // After PacketInDaemon Running
+	OLT_INACTIVE  DeviceState = iota // OLT/ONUs are not instantiated
+	OLT_PREACTIVE                    // Before PacketInDaemon Running
+	OLT_ACTIVE                       // After PacketInDaemon Running
 )
 
-// NewOlt creates and return new Olt object
+// OLTAlarmStateToString is used to get alarm state as string
+var OLTAlarmStateToString = map[AlarmState]string{
+	PonLosCleared: "PonLosCleared",
+	NniLosCleared: "NniLosCleared",
+	PonLosRaised:  "PonLosRaised",
+	NniLosRaised:  "NniLosRaised",
+}
+
+// NewOlt initialises the new olt variable with the given values
 func NewOlt(oltid uint32, npon uint32, nnni uint32) *Olt {
 	olt := Olt{}
 	olt.ID = oltid
@@ -81,18 +120,21 @@
 	olt.OperState = "up"
 	olt.Manufacture = "BBSIM"
 	olt.SerialNumber = "BBSIMOLT00" + strconv.FormatInt(int64(oltid), 10)
-	olt.Intfs = make([]intf, olt.NumPonIntf+olt.NumNniIntf)
+	olt.NniIntfs = make([]nniIntf, olt.NumNniIntf)
+	olt.PonIntfs = make([]ponIntf, olt.NumPonIntf)
 	olt.HeartbeatSignature = oltid
 	olt.mu = &sync.Mutex{}
 	for i := uint32(0); i < olt.NumNniIntf; i++ {
-		olt.Intfs[i].IntfID = i
-		olt.Intfs[i].OperState = "up"
-		olt.Intfs[i].Type = "nni"
+		olt.NniIntfs[i].IntfID = i
+		olt.NniIntfs[i].OperState = "up"
+		olt.NniIntfs[i].Type = IntfNni
+		olt.NniIntfs[i].AlarmState = NniLosCleared
 	}
-	for i := uint32(olt.NumNniIntf); i < olt.NumPonIntf+olt.NumNniIntf; i++ {
-		olt.Intfs[i].IntfID = i
-		olt.Intfs[i].OperState = "up"
-		olt.Intfs[i].Type = "pon"
+	for i := uint32(0); i < olt.NumPonIntf; i++ {
+		olt.PonIntfs[i].IntfID = i
+		olt.PonIntfs[i].OperState = "up"
+		olt.PonIntfs[i].Type = IntfPon
+		olt.PonIntfs[i].AlarmState = PonLosCleared
 	}
 	return &olt
 }
@@ -102,14 +144,16 @@
 	olt.InternalState = OLT_INACTIVE
 	olt.OperState = "up"
 	for i := uint32(0); i < olt.NumNniIntf; i++ {
-		olt.Intfs[i].IntfID = i
-		olt.Intfs[i].OperState = "up"
-		olt.Intfs[i].Type = "nni"
+		olt.NniIntfs[i].IntfID = i
+		olt.NniIntfs[i].OperState = "up"
+		olt.NniIntfs[i].Type = IntfNni
+		olt.NniIntfs[i].AlarmState = NniLosCleared
 	}
-	for i := uint32(olt.NumNniIntf); i < olt.NumPonIntf+olt.NumNniIntf; i++ {
-		olt.Intfs[i].IntfID = i
-		olt.Intfs[i].OperState = "up"
-		olt.Intfs[i].Type = "pon"
+	for i := uint32(olt.NumNniIntf); i < olt.NumPonIntf; i++ {
+		olt.PonIntfs[i].IntfID = i
+		olt.PonIntfs[i].OperState = "up"
+		olt.PonIntfs[i].Type = IntfPon
+		olt.PonIntfs[i].AlarmState = PonLosCleared
 	}
 }
 
@@ -121,7 +165,7 @@
 }
 
 // GetDevkey returns device key of OLT
-func (olt *Olt) GetDevkey () Devkey {
+func (olt *Olt) GetDevkey() Devkey {
 	return Devkey{ID: olt.ID}
 }
 
@@ -130,4 +174,16 @@
 	olt.mu.Lock()
 	defer olt.mu.Unlock()
 	olt.InternalState = intstate
+}
+
+// UpdateNniPortState updates the status of the nni-port
+func (olt *Olt) UpdateNniPortState(portID uint32, alarmState AlarmState, operState string) {
+	olt.NniIntfs[portID].AlarmState = alarmState
+	olt.NniIntfs[portID].OperState = operState
+}
+
+// UpdatePonPortState updates the status of the pon-port
+func (olt *Olt) UpdatePonPortState(portID uint32, alarmState AlarmState, operState string) {
+	olt.PonIntfs[portID].AlarmState = alarmState
+	olt.PonIntfs[portID].OperState = operState
 }
\ No newline at end of file
diff --git a/device/device_onu.go b/device/device_onu.go
index f2338d7..0add821 100644
--- a/device/device_onu.go
+++ b/device/device_onu.go
@@ -21,18 +21,33 @@
 	"sync"
 
 	"gerrit.opencord.org/voltha-bbsim/common/logger"
-	"gerrit.opencord.org/voltha-bbsim/protos"
+	openolt "gerrit.opencord.org/voltha-bbsim/protos"
 	log "github.com/sirupsen/logrus"
 )
 
+// Constants for the ONU states
 const (
-	ONU_INACTIVE   DeviceState = iota	//TODO: Each stage name should be more accurate
+	ONU_INACTIVE DeviceState = iota // TODO: Each stage name should be more accurate
 	ONU_ACTIVE
 	ONU_OMCIACTIVE
 	ONU_AUTHENTICATED
+	ONU_LOS_RAISED
+	ONU_OMCI_CHANNEL_LOS_RAISED
+	ONU_LOS_ON_OLT_PON_LOS // TODO give more suitable and crisp name
 	ONU_FREE
 )
 
+// ONUState maps int value of device state to string
+var ONUState = map[DeviceState]string{
+	ONU_INACTIVE:                "ONU_INACTIVE",
+	ONU_ACTIVE:                  "ONU_ACTIVE",
+	ONU_OMCIACTIVE:              "ONU_OMCIACTIVE",
+	ONU_FREE:                    "ONU_FREE",
+	ONU_LOS_RAISED:              "ONU_LOS_RAISED",
+	ONU_OMCI_CHANNEL_LOS_RAISED: "ONU_OMCI_CHANNEL_LOS_RAISED",
+	ONU_LOS_ON_OLT_PON_LOS:      "ONU_LOS_ON_OLT_PON_LOS",
+}
+
 // Onu structure stores information of ONUs
 type Onu struct {
 	InternalState DeviceState
@@ -42,6 +57,7 @@
 	SerialNumber  *openolt.SerialNumber
 	OnuID         uint32
 	GemportID     uint16
+	FlowIDs       []uint32
 	mu            *sync.Mutex
 }
 
@@ -54,9 +70,9 @@
 // NewOnus initializes and returns slice of Onu objects
 func NewOnus(oltid uint32, intfid uint32, nonus uint32, nnni uint32) []*Onu {
 	onus := []*Onu{}
-	for i := 0; i < int(nonus); i++ {
+	for i := 1; i <= int(nonus); i++ {
 		onu := Onu{}
-		onu.InternalState = ONU_FREE
+		onu.InternalState = ONU_FREE // New Onu Initialised with state ONU_FREE
 		onu.mu = &sync.Mutex{}
 		onu.IntfID = intfid
 		onu.OltID = oltid
@@ -94,14 +110,12 @@
 }
 
 // UpdateOnusOpStatus method updates ONU oper status
-func UpdateOnusOpStatus(ponif uint32, onus []*Onu, opstatus string) {
-	for _, onu := range onus {
-		onu.OperState = opstatus
-		logger.WithFields(log.Fields{
-			"onu":           onu.SerialNumber,
-			"pon_interface": ponif,
-		}).Info("ONU discovered.")
-	}
+func UpdateOnusOpStatus(ponif uint32, onu *Onu, opstatus string) {
+	onu.OperState = opstatus
+	logger.WithFields(log.Fields{
+		"onu":           onu.SerialNumber,
+		"pon_interface": ponif,
+	}).Info("ONU OperState Updated")
 }
 
 // UpdateIntState method updates ONU internal state
@@ -112,8 +126,8 @@
 }
 
 // GetDevkey returns ONU device key
-func (onu *Onu) GetDevkey () Devkey {
-	return Devkey{ID: onu.OnuID, Intfid:onu.IntfID}
+func (onu *Onu) GetDevkey() Devkey {
+	return Devkey{ID: onu.OnuID, Intfid: onu.IntfID}
 }
 
 // GetIntState returns ONU internal state
@@ -122,3 +136,17 @@
 	defer onu.mu.Unlock()
 	return onu.InternalState
 }
+
+// DeleteFlowID method search and delete flowID from the onu flowIDs slice
+func (onu *Onu) DeleteFlowID(flowID uint32) {
+	for pos, id := range onu.FlowIDs {
+		if id == flowID {
+			// delete the flowID by shifting all flowIDs by one
+			onu.FlowIDs = append(onu.FlowIDs[:pos], onu.FlowIDs[pos+1:]...)
+			t := make([]uint32, len(onu.FlowIDs))
+			copy(t, onu.FlowIDs)
+			onu.FlowIDs = t
+			break
+		}
+	}
+}