blob: be52fbbdac4f5ef9a37793b640373a3455dbb216 [file] [log] [blame]
Naveen Sampath04696f72022-06-13 15:19:14 +05301/*
2* Copyright 2022-present Open Networking Foundation
3* Licensed under the Apache License, Version 2.0 (the "License");
4* you may not use this file except in compliance with the License.
5* You may obtain a copy of the License at
6*
7* http://www.apache.org/licenses/LICENSE-2.0
8*
9* Unless required by applicable law or agreed to in writing, software
10* distributed under the License is distributed on an "AS IS" BASIS,
11* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12* See the License for the specific language governing permissions and
13* limitations under the License.
14 */
15
16package application
17
18import (
Tinoj Joseph07cc5372022-07-18 22:53:51 +053019 "context"
20
Naveen Sampath04696f72022-06-13 15:19:14 +053021 infraerrorcode "voltha-go-controller/internal/pkg/errorcodes/service"
22
23 "voltha-go-controller/internal/pkg/intf"
Tinoj Joseph1d108322022-07-13 10:07:39 +053024 "voltha-go-controller/log"
Naveen Sampath04696f72022-06-13 15:19:14 +053025)
26
vinokuma926cb3e2023-03-29 11:41:06 +053027// Generic Framework to enabling all flow based event trigger and handling.
28// The eventMapper can be updated for dynamic func caller for future events
Naveen Sampath04696f72022-06-13 15:19:14 +053029
vinokuma926cb3e2023-03-29 11:41:06 +053030// FlowEventType - Type of event enumeration
Naveen Sampath04696f72022-06-13 15:19:14 +053031type FlowEventType string
32
vinokuma926cb3e2023-03-29 11:41:06 +053033// FlowEventHandler - Func prototype for flow event handling funcs
Tinoj Joseph07cc5372022-07-18 22:53:51 +053034type FlowEventHandler func(context.Context, *FlowEvent, intf.FlowStatus)
Naveen Sampath04696f72022-06-13 15:19:14 +053035
36var eventMapper map[FlowEventType]FlowEventHandler
37
38const (
vinokuma926cb3e2023-03-29 11:41:06 +053039 // EventTypeUsIgmpFlowAdded - Event type for IGMP US flow add
Naveen Sampath04696f72022-06-13 15:19:14 +053040 EventTypeUsIgmpFlowAdded FlowEventType = "USIgmpFlowAdded"
vinokuma926cb3e2023-03-29 11:41:06 +053041 // EventTypeServiceFlowAdded - Event type for Service flow add
Naveen Sampath04696f72022-06-13 15:19:14 +053042 EventTypeServiceFlowAdded FlowEventType = "ServiceFlowAdded"
vinokuma926cb3e2023-03-29 11:41:06 +053043 // EventTypeControlFlowAdded - Event type for Control flow add
Naveen Sampath04696f72022-06-13 15:19:14 +053044 EventTypeControlFlowAdded FlowEventType = "ControlFlowAdded"
45
vinokuma926cb3e2023-03-29 11:41:06 +053046 // EventTypeDeviceFlowRemoved - Event type for Device flow del
Naveen Sampath04696f72022-06-13 15:19:14 +053047 EventTypeDeviceFlowRemoved FlowEventType = "DeviceFlowRemoved"
vinokuma926cb3e2023-03-29 11:41:06 +053048 // EventTypeMcastFlowRemoved - Event type for Mcast flow del
Naveen Sampath04696f72022-06-13 15:19:14 +053049 EventTypeMcastFlowRemoved FlowEventType = "McastFlowRemoved"
50
vinokuma926cb3e2023-03-29 11:41:06 +053051 // EventTypeServiceFlowRemoved - Event type for Service flow del
Naveen Sampath04696f72022-06-13 15:19:14 +053052 EventTypeServiceFlowRemoved FlowEventType = "ServiceFlowRemoved"
vinokuma926cb3e2023-03-29 11:41:06 +053053 // EventTypeControlFlowRemoved - Event type for Control flow del
Naveen Sampath04696f72022-06-13 15:19:14 +053054 EventTypeControlFlowRemoved FlowEventType = "ControlFlowRemoved"
55)
56
vinokuma926cb3e2023-03-29 11:41:06 +053057// FlowEvent - Event info for Flow event processing
Naveen Sampath04696f72022-06-13 15:19:14 +053058type FlowEvent struct {
vinokuma926cb3e2023-03-29 11:41:06 +053059 eventData interface{}
Naveen Sampath04696f72022-06-13 15:19:14 +053060 device string
61 cookie string
vinokuma926cb3e2023-03-29 11:41:06 +053062 eType FlowEventType
Naveen Sampath04696f72022-06-13 15:19:14 +053063}
64
vinokuma926cb3e2023-03-29 11:41:06 +053065// InitEventFuncMapper - Initialization of flow event mapper
Naveen Sampath04696f72022-06-13 15:19:14 +053066func InitEventFuncMapper() {
67 eventMapper = map[FlowEventType]FlowEventHandler{
68 EventTypeUsIgmpFlowAdded: ProcessUsIgmpFlowAddEvent,
69 EventTypeControlFlowAdded: ProcessControlFlowAddEvent,
70 EventTypeServiceFlowAdded: ProcessServiceFlowAddEvent,
71 EventTypeControlFlowRemoved: ProcessControlFlowDelEvent,
72 EventTypeServiceFlowRemoved: ProcessServiceFlowDelEvent,
73 EventTypeDeviceFlowRemoved: ProcessDeviceFlowDelEvent,
74 EventTypeMcastFlowRemoved: ProcessMcastFlowDelEvent,
75 }
76}
77
vinokuma926cb3e2023-03-29 11:41:06 +053078// ExecuteFlowEvent - Process flow based event triggers
Tinoj Joseph07cc5372022-07-18 22:53:51 +053079func ExecuteFlowEvent(cntx context.Context, vd *VoltDevice, cookie string, flowStatus intf.FlowStatus) bool {
Naveen Sampath04696f72022-06-13 15:19:14 +053080 var event interface{}
81
82 flowEventMap, err := vd.GetFlowEventRegister(flowStatus.FlowModType)
83 if err != nil {
84 logger.Debugw(ctx, "Flow event map does not exists", log.Fields{"flowMod": flowStatus.FlowModType, "Error": err})
85 return false
86 }
87 flowEventMap.MapLock.Lock()
88
89 if event, _ = flowEventMap.Get(cookie); event == nil {
90 logger.Debugw(ctx, "Event already processed or event not registered for the cookie", log.Fields{"Cookie": cookie})
91 flowEventMap.MapLock.Unlock()
92 return false
93 }
94 flowEventMap.Remove(cookie)
95 flowEventMap.MapLock.Unlock()
96 flowEvent := event.(*FlowEvent)
Tinoj Joseph07cc5372022-07-18 22:53:51 +053097 eventMapper[flowEvent.eType](cntx, flowEvent, flowStatus)
Naveen Sampath04696f72022-06-13 15:19:14 +053098 return true
99}
100
vinokuma926cb3e2023-03-29 11:41:06 +0530101// ProcessUsIgmpFlowAddEvent - Process Us Igmp Flow event trigger
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530102func ProcessUsIgmpFlowAddEvent(cntx context.Context, event *FlowEvent, flowStatus intf.FlowStatus) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530103 logger.Infow(ctx, "Processing Post Flow Add Event for US Igmp", log.Fields{"Cookie": event.cookie, "event": event})
104 vpv := event.eventData.(*VoltPortVnet)
105 if isFlowStatusSuccess(flowStatus.Status, true) {
106 vpv.services.Range(ReceiverUpInd)
107 } else {
108 vpv.IgmpFlowInstallFailure(event.cookie, flowStatus.Status, flowStatus.Reason)
109 }
110}
111
vinokuma926cb3e2023-03-29 11:41:06 +0530112// ProcessServiceFlowAddEvent - Process Service Flow event trigger
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530113func ProcessServiceFlowAddEvent(cntx context.Context, event *FlowEvent, flowStatus intf.FlowStatus) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530114 logger.Infow(ctx, "Processing Post Flow Add Event for Service", log.Fields{"Cookie": event.cookie, "event": event})
115 vs := event.eventData.(*VoltService)
116 if isFlowStatusSuccess(flowStatus.Status, true) {
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530117 vs.FlowInstallSuccess(cntx, event.cookie, flowStatus.AdditionalData)
Naveen Sampath04696f72022-06-13 15:19:14 +0530118 } else {
119 vs.FlowInstallFailure(event.cookie, flowStatus.Status, flowStatus.Reason)
120 }
121}
122
vinokuma926cb3e2023-03-29 11:41:06 +0530123// ProcessControlFlowAddEvent - Process Control Flow event trigger
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530124func ProcessControlFlowAddEvent(cntx context.Context, event *FlowEvent, flowStatus intf.FlowStatus) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530125 logger.Infow(ctx, "Processing Post Flow Add Event for VPV", log.Fields{"Cookie": event.cookie, "event": event})
126 vpv := event.eventData.(*VoltPortVnet)
127 if !isFlowStatusSuccess(flowStatus.Status, true) {
128 vpv.FlowInstallFailure(event.cookie, flowStatus.Status, flowStatus.Reason)
129 }
130}
131
vinokuma926cb3e2023-03-29 11:41:06 +0530132// ProcessServiceFlowDelEvent - Process Service Flow event trigger
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530133func ProcessServiceFlowDelEvent(cntx context.Context, event *FlowEvent, flowStatus intf.FlowStatus) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530134 logger.Infow(ctx, "Processing Post Flow Remove Event for Service", log.Fields{"Cookie": event.cookie, "event": event})
135 vs := event.eventData.(*VoltService)
136 if isFlowStatusSuccess(flowStatus.Status, false) {
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530137 vs.FlowRemoveSuccess(cntx, event.cookie)
Naveen Sampath04696f72022-06-13 15:19:14 +0530138 } else {
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530139 vs.FlowRemoveFailure(cntx, event.cookie, flowStatus.Status, flowStatus.Reason)
Naveen Sampath04696f72022-06-13 15:19:14 +0530140 }
141}
142
vinokuma926cb3e2023-03-29 11:41:06 +0530143// ProcessControlFlowDelEvent - Process Control Flow event trigger
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530144func ProcessControlFlowDelEvent(cntx context.Context, event *FlowEvent, flowStatus intf.FlowStatus) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530145 logger.Infow(ctx, "Processing Post Flow Remove Event for VPV", log.Fields{"Cookie": event.cookie, "event": event})
146 vpv := event.eventData.(*VoltPortVnet)
147 if isFlowStatusSuccess(flowStatus.Status, false) {
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530148 vpv.FlowRemoveSuccess(cntx, event.cookie, event.device)
Naveen Sampath04696f72022-06-13 15:19:14 +0530149 } else {
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530150 vpv.FlowRemoveFailure(cntx, event.cookie, event.device, flowStatus.Status, flowStatus.Reason)
Naveen Sampath04696f72022-06-13 15:19:14 +0530151 }
152}
153
vinokuma926cb3e2023-03-29 11:41:06 +0530154// ProcessMcastFlowDelEvent - Process Control Flow event trigger
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530155func ProcessMcastFlowDelEvent(cntx context.Context, event *FlowEvent, flowStatus intf.FlowStatus) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530156 logger.Infow(ctx, "Processing Post Flow Remove Event for Mcast/Igmp", log.Fields{"Cookie": event.cookie, "event": event})
157 mvp := event.eventData.(*MvlanProfile)
158 if isFlowStatusSuccess(flowStatus.Status, false) {
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530159 mvp.FlowRemoveSuccess(cntx, event.cookie, event.device)
Naveen Sampath04696f72022-06-13 15:19:14 +0530160 } else {
161 mvp.FlowRemoveFailure(event.cookie, event.device, flowStatus.Status, flowStatus.Reason)
162 }
163}
164
vinokuma926cb3e2023-03-29 11:41:06 +0530165// ProcessDeviceFlowDelEvent - Process Control Flow event trigger
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530166func ProcessDeviceFlowDelEvent(cntx context.Context, event *FlowEvent, flowStatus intf.FlowStatus) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530167 logger.Infow(ctx, "Processing Post Flow Remove Event for VNET", log.Fields{"Cookie": event.cookie, "event": event})
168 vnet := event.eventData.(*VoltVnet)
169 if isFlowStatusSuccess(flowStatus.Status, false) {
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530170 vnet.FlowRemoveSuccess(cntx, event.cookie, event.device)
Naveen Sampath04696f72022-06-13 15:19:14 +0530171 } else {
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530172 vnet.FlowRemoveFailure(cntx, event.cookie, event.device, flowStatus.Status, flowStatus.Reason)
Naveen Sampath04696f72022-06-13 15:19:14 +0530173 }
174}
175
vinokuma926cb3e2023-03-29 11:41:06 +0530176// TODO: Update the func or flowStatus struct once all flow status are based on NB error code
Naveen Sampath04696f72022-06-13 15:19:14 +0530177func isFlowStatusSuccess(status uint32, flowAdd bool) bool {
178 result := false
179 errorCode := infraerrorcode.ErrorCode(status)
180
181 if errorCode == infraerrorcode.ErrOk {
182 result = true
183 } else if !flowAdd && errorCode == infraerrorcode.ErrNotExists {
184 result = true
185 }
186 return result
187}