[VOL-3785] New Kafka event notifications for errors in OMCI communication and MDS audit

Change-Id: I98ca4092d18d364bc05e4a899886a9bcdc38cf17
diff --git a/VERSION b/VERSION
index 97102b4..45929fe 100755
--- a/VERSION
+++ b/VERSION
@@ -1 +1 @@
-2.2.2-dev
+2.2.2-dev270
diff --git a/internal/pkg/common/defines.go b/internal/pkg/common/defines.go
index d0135aa..06bc4b2 100755
--- a/internal/pkg/common/defines.go
+++ b/internal/pkg/common/defines.go
@@ -345,3 +345,17 @@
 type WaitGroupWithTimeOut struct {
 	sync.WaitGroup
 }
+
+///////////////////////////////////////////////////////////
+
+// event notifications
+const (
+	OnuMibAuditFailureMds     = "ONU_MIB_AUDIT_FAILURE_MDS"
+	OnuMibAuditFailureMdsDesc = "MIB audit failed due to MDS value mismatch between ONU and adapter"
+
+	OnuOmciCommunicationFailureConfig     = "ONU_OMCI_COMMUNICATION_FAILURE_CONFIG"
+	OnuOmciCommunicationFailureConfigDesc = "OMCI communication during ONU configuration failed"
+
+	OnuOmciCommunicationFailureSwUpgrade     = "ONU_OMCI_COMMUNICATION_FAILURE_SW_UPGRADE"
+	OnuOmciCommunicationFailureSwUpgradeDesc = "OMCI communication during ONU SW upgrade failed"
+)
diff --git a/internal/pkg/common/interfaces.go b/internal/pkg/common/interfaces.go
index 4c4878a..a204539 100755
--- a/internal/pkg/common/interfaces.go
+++ b/internal/pkg/common/interfaces.go
@@ -150,6 +150,8 @@
 
 	AllocateFreeTcont(context.Context, uint16) (uint16, bool, error)
 	FreeTcont(context.Context, uint16)
+
+	SendOnuDeviceEvent(context.Context, string, string)
 }
 
 // IonuMetricsManager interface to onuMetricsManager
diff --git a/internal/pkg/common/omci_cc.go b/internal/pkg/common/omci_cc.go
index a3df18a..d11173f 100755
--- a/internal/pkg/common/omci_cc.go
+++ b/internal/pkg/common/omci_cc.go
@@ -4296,8 +4296,9 @@
 			break loop
 		case <-time.After(time.Duration(aTimeout) * time.Second):
 			if retryCounter == retries {
-				logger.Errorw(ctx, "reqMon: timeout waiting for response - no of max retries reached!",
+				logger.Errorw(ctx, "reqMon: timeout waiting for response - no of max retries reached - send ONU device event!",
 					log.Fields{"tid": tid, "retries": retryCounter, "device-id": oo.deviceID})
+				oo.pOnuDeviceEntry.SendOnuDeviceEvent(ctx, OnuOmciCommunicationFailureSwUpgrade, OnuOmciCommunicationFailureSwUpgradeDesc)
 				break loop
 			} else {
 				logger.Infow(ctx, "reqMon: timeout waiting for response - retry",
@@ -4705,8 +4706,9 @@
 			break loop
 		case <-time.After(time.Duration(aTimeout) * time.Second):
 			if retryCounter == retries {
-				logger.Errorw(ctx, "reqMon: timeout waiting for response - no of max retries reached!",
+				logger.Errorw(ctx, "reqMon: timeout waiting for response - no of max retries reached - send ONU device event!",
 					log.Fields{"tid": tid, "retries": retryCounter, "device-id": oo.deviceID})
+				oo.pOnuDeviceEntry.SendOnuDeviceEvent(ctx, OnuOmciCommunicationFailureConfig, OnuOmciCommunicationFailureConfigDesc)
 				break loop
 			} else {
 				logger.Infow(ctx, "reqMon: timeout waiting for response - retry",
diff --git a/internal/pkg/mib/mib_sync.go b/internal/pkg/mib/mib_sync.go
index 7f33fc0..88fa8c6 100755
--- a/internal/pkg/mib/mib_sync.go
+++ b/internal/pkg/mib/mib_sync.go
@@ -1093,8 +1093,9 @@
 			logger.Debugw(ctx, "MibSync FSM - mib reaudit - MDS check ok", log.Fields{"device-id": oo.deviceID})
 			_ = oo.PMibUploadFsm.PFsm.Event(UlEvSuccess)
 		} else {
-			logger.Errorw(ctx, "MibSync FSM - mib audit - MDS check failed for the second time!", log.Fields{"device-id": oo.deviceID})
-			//TODO: send new event notification "MDS counter mismatch" to the core
+			logger.Errorw(ctx, "MibSync FSM - mib audit - MDS check failed for the second time - send ONU device event!",
+				log.Fields{"device-id": oo.deviceID})
+			oo.SendOnuDeviceEvent(ctx, cmn.OnuMibAuditFailureMds, cmn.OnuMibAuditFailureMdsDesc)
 			_ = oo.PMibUploadFsm.PFsm.Event(UlEvMismatch)
 		}
 	} else if oo.PMibUploadFsm.PFsm.Is(UlStExaminingMds) {
diff --git a/internal/pkg/mib/onu_device_entry.go b/internal/pkg/mib/onu_device_entry.go
index ea47bbf..cb50e75 100755
--- a/internal/pkg/mib/onu_device_entry.go
+++ b/internal/pkg/mib/onu_device_entry.go
@@ -22,6 +22,7 @@
 	"encoding/json"
 	"errors"
 	"fmt"
+	"strconv"
 	"sync"
 	"time"
 
@@ -30,8 +31,10 @@
 	me "github.com/opencord/omci-lib-go/v2/generated"
 	"github.com/opencord/voltha-lib-go/v7/pkg/db"
 	"github.com/opencord/voltha-lib-go/v7/pkg/db/kvstore"
+	"github.com/opencord/voltha-lib-go/v7/pkg/events/eventif"
 	vgrpc "github.com/opencord/voltha-lib-go/v7/pkg/grpc"
 	"github.com/opencord/voltha-protos/v5/go/inter_adapter"
+	"github.com/opencord/voltha-protos/v5/go/voltha"
 
 	"github.com/opencord/voltha-lib-go/v7/pkg/log"
 
@@ -156,6 +159,7 @@
 type OnuDeviceEntry struct {
 	deviceID                   string
 	baseDeviceHandler          cmn.IdeviceHandler
+	eventProxy                 eventif.EventProxy
 	pOpenOnuAc                 cmn.IopenONUAC
 	pOnuTP                     cmn.IonuUniTechProf
 	coreClient                 *vgrpc.Client
@@ -214,6 +218,7 @@
 	onuDeviceEntry.deviceID = dh.GetDeviceID()
 	logger.Debugw(ctx, "init-onuDeviceEntry", log.Fields{"device-id": onuDeviceEntry.deviceID})
 	onuDeviceEntry.baseDeviceHandler = dh
+	onuDeviceEntry.eventProxy = dh.GetEventProxy()
 	onuDeviceEntry.pOpenOnuAc = openonu
 	onuDeviceEntry.coreClient = cc
 	onuDeviceEntry.devState = cmn.DeviceStatusInit
@@ -999,3 +1004,22 @@
 	}
 	oo.PDevOmciCC = nil
 }
+
+//SendOnuDeviceEvent sends an ONU DeviceEvent via eventProxy
+func (oo *OnuDeviceEntry) SendOnuDeviceEvent(ctx context.Context, aDeviceEventName string, aDescription string) {
+
+	oo.MutexPersOnuConfig.RLock()
+	context := make(map[string]string)
+	context["onu-id"] = strconv.FormatUint(uint64(oo.SOnuPersistentData.PersOnuID), 10)
+	context["intf-id"] = strconv.FormatUint(uint64(oo.SOnuPersistentData.PersIntfID), 10)
+	context["onu-serial-number"] = oo.SOnuPersistentData.PersSerialNumber
+	oo.MutexPersOnuConfig.RUnlock()
+
+	deviceEvent := &voltha.DeviceEvent{
+		ResourceId:      oo.deviceID,
+		DeviceEventName: aDeviceEventName,
+		Description:     aDescription,
+		Context:         context,
+	}
+	_ = oo.eventProxy.SendDeviceEvent(ctx, deviceEvent, voltha.EventCategory_COMMUNICATION, voltha.EventSubCategory_ONU, time.Now().Unix())
+}