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" |
| 22 | |
| 23 | "voltha-go-controller/internal/pkg/intf" |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame] | 24 | "voltha-go-controller/log" |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 25 | ) |
| 26 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 27 | // 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 Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 29 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 30 | // FlowEventType - Type of event enumeration |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 31 | type FlowEventType string |
| 32 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 33 | // FlowEventHandler - Func prototype for flow event handling funcs |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 34 | type FlowEventHandler func(context.Context, *FlowEvent, intf.FlowStatus) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 35 | |
| 36 | var eventMapper map[FlowEventType]FlowEventHandler |
| 37 | |
| 38 | const ( |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 39 | // EventTypeUsIgmpFlowAdded - Event type for IGMP US flow add |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 40 | EventTypeUsIgmpFlowAdded FlowEventType = "USIgmpFlowAdded" |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 41 | // EventTypeServiceFlowAdded - Event type for Service flow add |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 42 | EventTypeServiceFlowAdded FlowEventType = "ServiceFlowAdded" |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 43 | // EventTypeControlFlowAdded - Event type for Control flow add |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 44 | EventTypeControlFlowAdded FlowEventType = "ControlFlowAdded" |
| 45 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 46 | // EventTypeDeviceFlowRemoved - Event type for Device flow del |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 47 | EventTypeDeviceFlowRemoved FlowEventType = "DeviceFlowRemoved" |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 48 | // EventTypeMcastFlowRemoved - Event type for Mcast flow del |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 49 | EventTypeMcastFlowRemoved FlowEventType = "McastFlowRemoved" |
| 50 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 51 | // EventTypeServiceFlowRemoved - Event type for Service flow del |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 52 | EventTypeServiceFlowRemoved FlowEventType = "ServiceFlowRemoved" |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 53 | // EventTypeControlFlowRemoved - Event type for Control flow del |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 54 | EventTypeControlFlowRemoved FlowEventType = "ControlFlowRemoved" |
| 55 | ) |
| 56 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 57 | // FlowEvent - Event info for Flow event processing |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 58 | type FlowEvent struct { |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 59 | eventData interface{} |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 60 | device string |
| 61 | cookie string |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 62 | eType FlowEventType |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 63 | } |
| 64 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 65 | // InitEventFuncMapper - Initialization of flow event mapper |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 66 | func 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 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 78 | // ExecuteFlowEvent - Process flow based event triggers |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 79 | 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] | 80 | 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] | 81 | var event interface{} |
| 82 | |
| 83 | flowEventMap, err := vd.GetFlowEventRegister(flowStatus.FlowModType) |
| 84 | if err != nil { |
Hitesh Chhabra | 2b2347d | 2023-07-31 17:36:48 +0530 | [diff] [blame] | 85 | 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] | 86 | return false |
| 87 | } |
| 88 | flowEventMap.MapLock.Lock() |
| 89 | |
| 90 | if event, _ = flowEventMap.Get(cookie); event == nil { |
| 91 | logger.Debugw(ctx, "Event already processed or event not registered for the cookie", log.Fields{"Cookie": cookie}) |
| 92 | flowEventMap.MapLock.Unlock() |
| 93 | return false |
| 94 | } |
| 95 | flowEventMap.Remove(cookie) |
| 96 | flowEventMap.MapLock.Unlock() |
| 97 | flowEvent := event.(*FlowEvent) |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 98 | eventMapper[flowEvent.eType](cntx, flowEvent, flowStatus) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 99 | return true |
| 100 | } |
| 101 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 102 | // ProcessUsIgmpFlowAddEvent - Process Us Igmp Flow event trigger |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 103 | func ProcessUsIgmpFlowAddEvent(cntx context.Context, event *FlowEvent, flowStatus intf.FlowStatus) { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 104 | logger.Infow(ctx, "Processing Post Flow Add Event for US Igmp", log.Fields{"Cookie": event.cookie, "event": event}) |
| 105 | vpv := event.eventData.(*VoltPortVnet) |
| 106 | if isFlowStatusSuccess(flowStatus.Status, true) { |
| 107 | vpv.services.Range(ReceiverUpInd) |
| 108 | } else { |
| 109 | vpv.IgmpFlowInstallFailure(event.cookie, flowStatus.Status, flowStatus.Reason) |
| 110 | } |
| 111 | } |
| 112 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 113 | // ProcessServiceFlowAddEvent - Process Service Flow event trigger |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 114 | func ProcessServiceFlowAddEvent(cntx context.Context, event *FlowEvent, flowStatus intf.FlowStatus) { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 115 | logger.Infow(ctx, "Processing Post Flow Add Event for Service", log.Fields{"Cookie": event.cookie, "event": event}) |
| 116 | vs := event.eventData.(*VoltService) |
| 117 | if isFlowStatusSuccess(flowStatus.Status, true) { |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 118 | vs.FlowInstallSuccess(cntx, event.cookie, flowStatus.AdditionalData) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 119 | } else { |
| 120 | vs.FlowInstallFailure(event.cookie, flowStatus.Status, flowStatus.Reason) |
| 121 | } |
| 122 | } |
| 123 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 124 | // ProcessControlFlowAddEvent - Process Control Flow event trigger |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 125 | func ProcessControlFlowAddEvent(cntx context.Context, event *FlowEvent, flowStatus intf.FlowStatus) { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 126 | logger.Infow(ctx, "Processing Post Flow Add Event for VPV", log.Fields{"Cookie": event.cookie, "event": event}) |
| 127 | vpv := event.eventData.(*VoltPortVnet) |
| 128 | if !isFlowStatusSuccess(flowStatus.Status, true) { |
| 129 | vpv.FlowInstallFailure(event.cookie, flowStatus.Status, flowStatus.Reason) |
| 130 | } |
| 131 | } |
| 132 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 133 | // ProcessServiceFlowDelEvent - Process Service Flow event trigger |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 134 | func ProcessServiceFlowDelEvent(cntx context.Context, event *FlowEvent, flowStatus intf.FlowStatus) { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 135 | logger.Infow(ctx, "Processing Post Flow Remove Event for Service", log.Fields{"Cookie": event.cookie, "event": event}) |
| 136 | vs := event.eventData.(*VoltService) |
| 137 | if isFlowStatusSuccess(flowStatus.Status, false) { |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 138 | vs.FlowRemoveSuccess(cntx, event.cookie) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 139 | } else { |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 140 | vs.FlowRemoveFailure(cntx, event.cookie, flowStatus.Status, flowStatus.Reason) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 144 | // ProcessControlFlowDelEvent - Process Control Flow event trigger |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 145 | func ProcessControlFlowDelEvent(cntx context.Context, event *FlowEvent, flowStatus intf.FlowStatus) { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 146 | logger.Infow(ctx, "Processing Post Flow Remove Event for VPV", log.Fields{"Cookie": event.cookie, "event": event}) |
| 147 | vpv := event.eventData.(*VoltPortVnet) |
| 148 | if isFlowStatusSuccess(flowStatus.Status, false) { |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 149 | vpv.FlowRemoveSuccess(cntx, event.cookie, event.device) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 150 | } else { |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 151 | vpv.FlowRemoveFailure(cntx, event.cookie, event.device, flowStatus.Status, flowStatus.Reason) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 155 | // ProcessMcastFlowDelEvent - Process Control Flow event trigger |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 156 | func ProcessMcastFlowDelEvent(cntx context.Context, event *FlowEvent, flowStatus intf.FlowStatus) { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 157 | logger.Infow(ctx, "Processing Post Flow Remove Event for Mcast/Igmp", log.Fields{"Cookie": event.cookie, "event": event}) |
| 158 | mvp := event.eventData.(*MvlanProfile) |
| 159 | if isFlowStatusSuccess(flowStatus.Status, false) { |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 160 | mvp.FlowRemoveSuccess(cntx, event.cookie, event.device) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 161 | } else { |
| 162 | mvp.FlowRemoveFailure(event.cookie, event.device, flowStatus.Status, flowStatus.Reason) |
| 163 | } |
| 164 | } |
| 165 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 166 | // ProcessDeviceFlowDelEvent - Process Control Flow event trigger |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 167 | func ProcessDeviceFlowDelEvent(cntx context.Context, event *FlowEvent, flowStatus intf.FlowStatus) { |
Hitesh Chhabra | 2b2347d | 2023-07-31 17:36:48 +0530 | [diff] [blame] | 168 | 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] | 169 | vnet := event.eventData.(*VoltVnet) |
| 170 | if isFlowStatusSuccess(flowStatus.Status, false) { |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 171 | vnet.FlowRemoveSuccess(cntx, event.cookie, event.device) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 172 | } else { |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 173 | vnet.FlowRemoveFailure(cntx, event.cookie, event.device, flowStatus.Status, flowStatus.Reason) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 177 | // 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] | 178 | func isFlowStatusSuccess(status uint32, flowAdd bool) bool { |
Hitesh Chhabra | 2b2347d | 2023-07-31 17:36:48 +0530 | [diff] [blame] | 179 | logger.Infow(ctx, "Processing isFlowStatusSuccess", log.Fields{"Status": status, "FlowAdd": flowAdd}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 180 | result := false |
| 181 | errorCode := infraerrorcode.ErrorCode(status) |
| 182 | |
| 183 | if errorCode == infraerrorcode.ErrOk { |
| 184 | result = true |
| 185 | } else if !flowAdd && errorCode == infraerrorcode.ErrNotExists { |
| 186 | result = true |
| 187 | } |
| 188 | return result |
| 189 | } |