blob: 63123da6cda0de44f6b952da887b350b031bef95 [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"
Sridhar Ravindra3ec14232024-01-01 19:11:48 +053022 "voltha-go-controller/internal/pkg/util"
Naveen Sampath04696f72022-06-13 15:19:14 +053023
24 "voltha-go-controller/internal/pkg/intf"
Tinoj Joseph1d108322022-07-13 10:07:39 +053025 "voltha-go-controller/log"
Naveen Sampath04696f72022-06-13 15:19:14 +053026)
27
vinokuma926cb3e2023-03-29 11:41:06 +053028// Generic Framework to enabling all flow based event trigger and handling.
29// The eventMapper can be updated for dynamic func caller for future events
Naveen Sampath04696f72022-06-13 15:19:14 +053030
vinokuma926cb3e2023-03-29 11:41:06 +053031// FlowEventType - Type of event enumeration
Naveen Sampath04696f72022-06-13 15:19:14 +053032type FlowEventType string
33
vinokuma926cb3e2023-03-29 11:41:06 +053034// FlowEventHandler - Func prototype for flow event handling funcs
Sridhar Ravindra3ec14232024-01-01 19:11:48 +053035type FlowEventHandler func(context.Context, *FlowEvent, intf.FlowStatus, *util.ConcurrentMap)
Naveen Sampath04696f72022-06-13 15:19:14 +053036
37var eventMapper map[FlowEventType]FlowEventHandler
38
39const (
vinokuma926cb3e2023-03-29 11:41:06 +053040 // EventTypeUsIgmpFlowAdded - Event type for IGMP US flow add
Naveen Sampath04696f72022-06-13 15:19:14 +053041 EventTypeUsIgmpFlowAdded FlowEventType = "USIgmpFlowAdded"
vinokuma926cb3e2023-03-29 11:41:06 +053042 // EventTypeServiceFlowAdded - Event type for Service flow add
Naveen Sampath04696f72022-06-13 15:19:14 +053043 EventTypeServiceFlowAdded FlowEventType = "ServiceFlowAdded"
vinokuma926cb3e2023-03-29 11:41:06 +053044 // EventTypeControlFlowAdded - Event type for Control flow add
Naveen Sampath04696f72022-06-13 15:19:14 +053045 EventTypeControlFlowAdded FlowEventType = "ControlFlowAdded"
46
vinokuma926cb3e2023-03-29 11:41:06 +053047 // EventTypeDeviceFlowRemoved - Event type for Device flow del
Naveen Sampath04696f72022-06-13 15:19:14 +053048 EventTypeDeviceFlowRemoved FlowEventType = "DeviceFlowRemoved"
vinokuma926cb3e2023-03-29 11:41:06 +053049 // EventTypeMcastFlowRemoved - Event type for Mcast flow del
Naveen Sampath04696f72022-06-13 15:19:14 +053050 EventTypeMcastFlowRemoved FlowEventType = "McastFlowRemoved"
51
vinokuma926cb3e2023-03-29 11:41:06 +053052 // EventTypeServiceFlowRemoved - Event type for Service flow del
Naveen Sampath04696f72022-06-13 15:19:14 +053053 EventTypeServiceFlowRemoved FlowEventType = "ServiceFlowRemoved"
vinokuma926cb3e2023-03-29 11:41:06 +053054 // EventTypeControlFlowRemoved - Event type for Control flow del
Naveen Sampath04696f72022-06-13 15:19:14 +053055 EventTypeControlFlowRemoved FlowEventType = "ControlFlowRemoved"
56)
57
vinokuma926cb3e2023-03-29 11:41:06 +053058// FlowEvent - Event info for Flow event processing
Naveen Sampath04696f72022-06-13 15:19:14 +053059type FlowEvent struct {
vinokuma926cb3e2023-03-29 11:41:06 +053060 eventData interface{}
Naveen Sampath04696f72022-06-13 15:19:14 +053061 device string
62 cookie string
vinokuma926cb3e2023-03-29 11:41:06 +053063 eType FlowEventType
Naveen Sampath04696f72022-06-13 15:19:14 +053064}
65
vinokuma926cb3e2023-03-29 11:41:06 +053066// InitEventFuncMapper - Initialization of flow event mapper
Naveen Sampath04696f72022-06-13 15:19:14 +053067func InitEventFuncMapper() {
68 eventMapper = map[FlowEventType]FlowEventHandler{
69 EventTypeUsIgmpFlowAdded: ProcessUsIgmpFlowAddEvent,
70 EventTypeControlFlowAdded: ProcessControlFlowAddEvent,
71 EventTypeServiceFlowAdded: ProcessServiceFlowAddEvent,
72 EventTypeControlFlowRemoved: ProcessControlFlowDelEvent,
73 EventTypeServiceFlowRemoved: ProcessServiceFlowDelEvent,
74 EventTypeDeviceFlowRemoved: ProcessDeviceFlowDelEvent,
75 EventTypeMcastFlowRemoved: ProcessMcastFlowDelEvent,
76 }
77}
78
vinokuma926cb3e2023-03-29 11:41:06 +053079// ExecuteFlowEvent - Process flow based event triggers
Tinoj Joseph07cc5372022-07-18 22:53:51 +053080func ExecuteFlowEvent(cntx context.Context, vd *VoltDevice, cookie string, flowStatus intf.FlowStatus) bool {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +053081 logger.Debugw(ctx, "Execute Flow event", log.Fields{"Cookie": cookie, "flowMod": flowStatus.FlowModType})
Naveen Sampath04696f72022-06-13 15:19:14 +053082 var event interface{}
83
84 flowEventMap, err := vd.GetFlowEventRegister(flowStatus.FlowModType)
85 if err != nil {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +053086 logger.Warnw(ctx, "Flow event map does not exists", log.Fields{"flowMod": flowStatus.FlowModType, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +053087 return false
88 }
89 flowEventMap.MapLock.Lock()
90
91 if event, _ = flowEventMap.Get(cookie); event == nil {
92 logger.Debugw(ctx, "Event already processed or event not registered for the cookie", log.Fields{"Cookie": cookie})
93 flowEventMap.MapLock.Unlock()
94 return false
95 }
Naveen Sampath04696f72022-06-13 15:19:14 +053096 flowEventMap.MapLock.Unlock()
97 flowEvent := event.(*FlowEvent)
Sridhar Ravindra3ec14232024-01-01 19:11:48 +053098 if flowEvent.eType != EventTypeServiceFlowAdded && flowEvent.eType != EventTypeServiceFlowRemoved {
99 flowEventMap.MapLock.Lock()
100 flowEventMap.Remove(cookie)
101 flowEventMap.MapLock.Unlock()
102 }
103 eventMapper[flowEvent.eType](cntx, flowEvent, flowStatus, flowEventMap)
Naveen Sampath04696f72022-06-13 15:19:14 +0530104 return true
105}
106
vinokuma926cb3e2023-03-29 11:41:06 +0530107// ProcessUsIgmpFlowAddEvent - Process Us Igmp Flow event trigger
Sridhar Ravindra3ec14232024-01-01 19:11:48 +0530108func ProcessUsIgmpFlowAddEvent(cntx context.Context, event *FlowEvent, flowStatus intf.FlowStatus, flowEventMap *util.ConcurrentMap) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530109 logger.Infow(ctx, "Processing Post Flow Add Event for US Igmp", log.Fields{"Cookie": event.cookie, "event": event})
110 vpv := event.eventData.(*VoltPortVnet)
111 if isFlowStatusSuccess(flowStatus.Status, true) {
112 vpv.services.Range(ReceiverUpInd)
113 } else {
114 vpv.IgmpFlowInstallFailure(event.cookie, flowStatus.Status, flowStatus.Reason)
115 }
116}
117
vinokuma926cb3e2023-03-29 11:41:06 +0530118// ProcessServiceFlowAddEvent - Process Service Flow event trigger
Sridhar Ravindra3ec14232024-01-01 19:11:48 +0530119func ProcessServiceFlowAddEvent(cntx context.Context, event *FlowEvent, flowStatus intf.FlowStatus, flowEventMap *util.ConcurrentMap) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530120 logger.Infow(ctx, "Processing Post Flow Add Event for Service", log.Fields{"Cookie": event.cookie, "event": event})
121 vs := event.eventData.(*VoltService)
122 if isFlowStatusSuccess(flowStatus.Status, true) {
Sridhar Ravindra3ec14232024-01-01 19:11:48 +0530123 vs.FlowInstallSuccess(cntx, event.cookie, flowStatus.AdditionalData, flowEventMap)
Naveen Sampath04696f72022-06-13 15:19:14 +0530124 } else {
Sridhar Ravindra3ec14232024-01-01 19:11:48 +0530125 vs.FlowInstallFailure(cntx, event.cookie, flowStatus.Status, flowStatus.Reason, flowEventMap)
Naveen Sampath04696f72022-06-13 15:19:14 +0530126 }
127}
128
vinokuma926cb3e2023-03-29 11:41:06 +0530129// ProcessControlFlowAddEvent - Process Control Flow event trigger
Sridhar Ravindra3ec14232024-01-01 19:11:48 +0530130func ProcessControlFlowAddEvent(cntx context.Context, event *FlowEvent, flowStatus intf.FlowStatus, flowEventMap *util.ConcurrentMap) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530131 logger.Infow(ctx, "Processing Post Flow Add Event for VPV", log.Fields{"Cookie": event.cookie, "event": event})
132 vpv := event.eventData.(*VoltPortVnet)
133 if !isFlowStatusSuccess(flowStatus.Status, true) {
134 vpv.FlowInstallFailure(event.cookie, flowStatus.Status, flowStatus.Reason)
135 }
136}
137
vinokuma926cb3e2023-03-29 11:41:06 +0530138// ProcessServiceFlowDelEvent - Process Service Flow event trigger
Sridhar Ravindra3ec14232024-01-01 19:11:48 +0530139func ProcessServiceFlowDelEvent(cntx context.Context, event *FlowEvent, flowStatus intf.FlowStatus, flowEventMap *util.ConcurrentMap) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530140 logger.Infow(ctx, "Processing Post Flow Remove Event for Service", log.Fields{"Cookie": event.cookie, "event": event})
141 vs := event.eventData.(*VoltService)
142 if isFlowStatusSuccess(flowStatus.Status, false) {
Sridhar Ravindra3ec14232024-01-01 19:11:48 +0530143 vs.FlowRemoveSuccess(cntx, event.cookie, flowEventMap)
Naveen Sampath04696f72022-06-13 15:19:14 +0530144 } else {
Sridhar Ravindra3ec14232024-01-01 19:11:48 +0530145 vs.FlowRemoveFailure(cntx, event.cookie, flowStatus.Status, flowStatus.Reason, flowEventMap)
Naveen Sampath04696f72022-06-13 15:19:14 +0530146 }
147}
148
vinokuma926cb3e2023-03-29 11:41:06 +0530149// ProcessControlFlowDelEvent - Process Control Flow event trigger
Sridhar Ravindra3ec14232024-01-01 19:11:48 +0530150func ProcessControlFlowDelEvent(cntx context.Context, event *FlowEvent, flowStatus intf.FlowStatus, flowEventMap *util.ConcurrentMap) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530151 logger.Infow(ctx, "Processing Post Flow Remove Event for VPV", log.Fields{"Cookie": event.cookie, "event": event})
152 vpv := event.eventData.(*VoltPortVnet)
153 if isFlowStatusSuccess(flowStatus.Status, false) {
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530154 vpv.FlowRemoveSuccess(cntx, event.cookie, event.device)
Naveen Sampath04696f72022-06-13 15:19:14 +0530155 } else {
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530156 vpv.FlowRemoveFailure(cntx, event.cookie, event.device, flowStatus.Status, flowStatus.Reason)
Naveen Sampath04696f72022-06-13 15:19:14 +0530157 }
158}
159
vinokuma926cb3e2023-03-29 11:41:06 +0530160// ProcessMcastFlowDelEvent - Process Control Flow event trigger
Sridhar Ravindra3ec14232024-01-01 19:11:48 +0530161func ProcessMcastFlowDelEvent(cntx context.Context, event *FlowEvent, flowStatus intf.FlowStatus, flowEventMap *util.ConcurrentMap) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530162 logger.Infow(ctx, "Processing Post Flow Remove Event for Mcast/Igmp", log.Fields{"Cookie": event.cookie, "event": event})
163 mvp := event.eventData.(*MvlanProfile)
164 if isFlowStatusSuccess(flowStatus.Status, false) {
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530165 mvp.FlowRemoveSuccess(cntx, event.cookie, event.device)
Naveen Sampath04696f72022-06-13 15:19:14 +0530166 } else {
167 mvp.FlowRemoveFailure(event.cookie, event.device, flowStatus.Status, flowStatus.Reason)
168 }
169}
170
vinokuma926cb3e2023-03-29 11:41:06 +0530171// ProcessDeviceFlowDelEvent - Process Control Flow event trigger
Sridhar Ravindra3ec14232024-01-01 19:11:48 +0530172func ProcessDeviceFlowDelEvent(cntx context.Context, event *FlowEvent, flowStatus intf.FlowStatus, flowEventMap *util.ConcurrentMap) {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +0530173 logger.Debugw(ctx, "Processing Post Flow Remove Event for VNET", log.Fields{"Cookie": event.cookie, "event": event})
Naveen Sampath04696f72022-06-13 15:19:14 +0530174 vnet := event.eventData.(*VoltVnet)
175 if isFlowStatusSuccess(flowStatus.Status, false) {
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530176 vnet.FlowRemoveSuccess(cntx, event.cookie, event.device)
Naveen Sampath04696f72022-06-13 15:19:14 +0530177 } else {
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530178 vnet.FlowRemoveFailure(cntx, event.cookie, event.device, flowStatus.Status, flowStatus.Reason)
Naveen Sampath04696f72022-06-13 15:19:14 +0530179 }
180}
181
vinokuma926cb3e2023-03-29 11:41:06 +0530182// TODO: Update the func or flowStatus struct once all flow status are based on NB error code
Naveen Sampath04696f72022-06-13 15:19:14 +0530183func isFlowStatusSuccess(status uint32, flowAdd bool) bool {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +0530184 logger.Infow(ctx, "Processing isFlowStatusSuccess", log.Fields{"Status": status, "FlowAdd": flowAdd})
Naveen Sampath04696f72022-06-13 15:19:14 +0530185 result := false
186 errorCode := infraerrorcode.ErrorCode(status)
187
188 if errorCode == infraerrorcode.ErrOk {
189 result = true
190 } else if !flowAdd && errorCode == infraerrorcode.ErrNotExists {
191 result = true
192 }
193 return result
194}