Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 1 | /* |
| 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 | |
| 16 | package application |
| 17 | |
| 18 | import ( |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 19 | "context" |
| 20 | |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 21 | infraerrorcode "voltha-go-controller/internal/pkg/errorcodes/service" |
Sridhar Ravindra | 3ec1423 | 2024-01-01 19:11:48 +0530 | [diff] [blame] | 22 | "voltha-go-controller/internal/pkg/util" |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 23 | |
| 24 | "voltha-go-controller/internal/pkg/intf" |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame] | 25 | "voltha-go-controller/log" |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 26 | ) |
| 27 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 28 | // 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 Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 30 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 31 | // FlowEventType - Type of event enumeration |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 32 | type FlowEventType string |
| 33 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 34 | // FlowEventHandler - Func prototype for flow event handling funcs |
Sridhar Ravindra | 3ec1423 | 2024-01-01 19:11:48 +0530 | [diff] [blame] | 35 | type FlowEventHandler func(context.Context, *FlowEvent, intf.FlowStatus, *util.ConcurrentMap) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 36 | |
| 37 | var eventMapper map[FlowEventType]FlowEventHandler |
| 38 | |
| 39 | const ( |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 40 | // EventTypeUsIgmpFlowAdded - Event type for IGMP US flow add |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 41 | EventTypeUsIgmpFlowAdded FlowEventType = "USIgmpFlowAdded" |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 42 | // EventTypeServiceFlowAdded - Event type for Service flow add |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 43 | EventTypeServiceFlowAdded FlowEventType = "ServiceFlowAdded" |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 44 | // EventTypeControlFlowAdded - Event type for Control flow add |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 45 | EventTypeControlFlowAdded FlowEventType = "ControlFlowAdded" |
| 46 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 47 | // EventTypeDeviceFlowRemoved - Event type for Device flow del |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 48 | EventTypeDeviceFlowRemoved FlowEventType = "DeviceFlowRemoved" |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 49 | // EventTypeMcastFlowRemoved - Event type for Mcast flow del |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 50 | EventTypeMcastFlowRemoved FlowEventType = "McastFlowRemoved" |
| 51 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 52 | // EventTypeServiceFlowRemoved - Event type for Service flow del |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 53 | EventTypeServiceFlowRemoved FlowEventType = "ServiceFlowRemoved" |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 54 | // EventTypeControlFlowRemoved - Event type for Control flow del |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 55 | EventTypeControlFlowRemoved FlowEventType = "ControlFlowRemoved" |
| 56 | ) |
| 57 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 58 | // FlowEvent - Event info for Flow event processing |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 59 | type FlowEvent struct { |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 60 | eventData interface{} |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 61 | device string |
| 62 | cookie string |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 63 | eType FlowEventType |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 64 | } |
| 65 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 66 | // InitEventFuncMapper - Initialization of flow event mapper |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 67 | func 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 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 79 | // ExecuteFlowEvent - Process flow based event triggers |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 80 | func ExecuteFlowEvent(cntx context.Context, vd *VoltDevice, cookie string, flowStatus intf.FlowStatus) bool { |
Hitesh Chhabra | 2b2347d | 2023-07-31 17:36:48 +0530 | [diff] [blame] | 81 | logger.Debugw(ctx, "Execute Flow event", log.Fields{"Cookie": cookie, "flowMod": flowStatus.FlowModType}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 82 | var event interface{} |
| 83 | |
| 84 | flowEventMap, err := vd.GetFlowEventRegister(flowStatus.FlowModType) |
| 85 | if err != nil { |
Hitesh Chhabra | 2b2347d | 2023-07-31 17:36:48 +0530 | [diff] [blame] | 86 | logger.Warnw(ctx, "Flow event map does not exists", log.Fields{"flowMod": flowStatus.FlowModType, "Error": err}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 87 | 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 Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 96 | flowEventMap.MapLock.Unlock() |
| 97 | flowEvent := event.(*FlowEvent) |
Sridhar Ravindra | 3ec1423 | 2024-01-01 19:11:48 +0530 | [diff] [blame] | 98 | 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 Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 104 | return true |
| 105 | } |
| 106 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 107 | // ProcessUsIgmpFlowAddEvent - Process Us Igmp Flow event trigger |
Sridhar Ravindra | 3ec1423 | 2024-01-01 19:11:48 +0530 | [diff] [blame] | 108 | func ProcessUsIgmpFlowAddEvent(cntx context.Context, event *FlowEvent, flowStatus intf.FlowStatus, flowEventMap *util.ConcurrentMap) { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 109 | 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 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 118 | // ProcessServiceFlowAddEvent - Process Service Flow event trigger |
Sridhar Ravindra | 3ec1423 | 2024-01-01 19:11:48 +0530 | [diff] [blame] | 119 | func ProcessServiceFlowAddEvent(cntx context.Context, event *FlowEvent, flowStatus intf.FlowStatus, flowEventMap *util.ConcurrentMap) { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 120 | 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 Ravindra | 3ec1423 | 2024-01-01 19:11:48 +0530 | [diff] [blame] | 123 | vs.FlowInstallSuccess(cntx, event.cookie, flowStatus.AdditionalData, flowEventMap) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 124 | } else { |
Sridhar Ravindra | 3ec1423 | 2024-01-01 19:11:48 +0530 | [diff] [blame] | 125 | vs.FlowInstallFailure(cntx, event.cookie, flowStatus.Status, flowStatus.Reason, flowEventMap) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 129 | // ProcessControlFlowAddEvent - Process Control Flow event trigger |
Sridhar Ravindra | 3ec1423 | 2024-01-01 19:11:48 +0530 | [diff] [blame] | 130 | func ProcessControlFlowAddEvent(cntx context.Context, event *FlowEvent, flowStatus intf.FlowStatus, flowEventMap *util.ConcurrentMap) { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 131 | 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 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 138 | // ProcessServiceFlowDelEvent - Process Service Flow event trigger |
Sridhar Ravindra | 3ec1423 | 2024-01-01 19:11:48 +0530 | [diff] [blame] | 139 | func ProcessServiceFlowDelEvent(cntx context.Context, event *FlowEvent, flowStatus intf.FlowStatus, flowEventMap *util.ConcurrentMap) { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 140 | 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 Ravindra | 3ec1423 | 2024-01-01 19:11:48 +0530 | [diff] [blame] | 143 | vs.FlowRemoveSuccess(cntx, event.cookie, flowEventMap) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 144 | } else { |
Sridhar Ravindra | 3ec1423 | 2024-01-01 19:11:48 +0530 | [diff] [blame] | 145 | vs.FlowRemoveFailure(cntx, event.cookie, flowStatus.Status, flowStatus.Reason, flowEventMap) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 149 | // ProcessControlFlowDelEvent - Process Control Flow event trigger |
Sridhar Ravindra | 3ec1423 | 2024-01-01 19:11:48 +0530 | [diff] [blame] | 150 | func ProcessControlFlowDelEvent(cntx context.Context, event *FlowEvent, flowStatus intf.FlowStatus, flowEventMap *util.ConcurrentMap) { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 151 | 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 Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 154 | vpv.FlowRemoveSuccess(cntx, event.cookie, event.device) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 155 | } else { |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 156 | vpv.FlowRemoveFailure(cntx, event.cookie, event.device, flowStatus.Status, flowStatus.Reason) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 160 | // ProcessMcastFlowDelEvent - Process Control Flow event trigger |
Sridhar Ravindra | 3ec1423 | 2024-01-01 19:11:48 +0530 | [diff] [blame] | 161 | func ProcessMcastFlowDelEvent(cntx context.Context, event *FlowEvent, flowStatus intf.FlowStatus, flowEventMap *util.ConcurrentMap) { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 162 | 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 Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 165 | mvp.FlowRemoveSuccess(cntx, event.cookie, event.device) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 166 | } else { |
| 167 | mvp.FlowRemoveFailure(event.cookie, event.device, flowStatus.Status, flowStatus.Reason) |
| 168 | } |
| 169 | } |
| 170 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 171 | // ProcessDeviceFlowDelEvent - Process Control Flow event trigger |
Sridhar Ravindra | 3ec1423 | 2024-01-01 19:11:48 +0530 | [diff] [blame] | 172 | func ProcessDeviceFlowDelEvent(cntx context.Context, event *FlowEvent, flowStatus intf.FlowStatus, flowEventMap *util.ConcurrentMap) { |
Hitesh Chhabra | 2b2347d | 2023-07-31 17:36:48 +0530 | [diff] [blame] | 173 | logger.Debugw(ctx, "Processing Post Flow Remove Event for VNET", log.Fields{"Cookie": event.cookie, "event": event}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 174 | vnet := event.eventData.(*VoltVnet) |
| 175 | if isFlowStatusSuccess(flowStatus.Status, false) { |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 176 | vnet.FlowRemoveSuccess(cntx, event.cookie, event.device) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 177 | } else { |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 178 | vnet.FlowRemoveFailure(cntx, event.cookie, event.device, flowStatus.Status, flowStatus.Reason) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 179 | } |
| 180 | } |
| 181 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 182 | // TODO: Update the func or flowStatus struct once all flow status are based on NB error code |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 183 | func isFlowStatusSuccess(status uint32, flowAdd bool) bool { |
Hitesh Chhabra | 2b2347d | 2023-07-31 17:36:48 +0530 | [diff] [blame] | 184 | logger.Infow(ctx, "Processing isFlowStatusSuccess", log.Fields{"Status": status, "FlowAdd": flowAdd}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 185 | 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 | } |