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 | |
| 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 |
| 29 | |
| 30 | //FlowEventType - Type of event enumeration |
| 31 | type FlowEventType string |
| 32 | |
| 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 ( |
| 39 | //EventTypeUsIgmpFlowAdded - Event type for IGMP US flow add |
| 40 | EventTypeUsIgmpFlowAdded FlowEventType = "USIgmpFlowAdded" |
| 41 | //EventTypeServiceFlowAdded - Event type for Service flow add |
| 42 | EventTypeServiceFlowAdded FlowEventType = "ServiceFlowAdded" |
| 43 | //EventTypeControlFlowAdded - Event type for Control flow add |
| 44 | EventTypeControlFlowAdded FlowEventType = "ControlFlowAdded" |
| 45 | |
| 46 | //EventTypeDeviceFlowRemoved - Event type for Device flow del |
| 47 | EventTypeDeviceFlowRemoved FlowEventType = "DeviceFlowRemoved" |
| 48 | //EventTypeMcastFlowRemoved - Event type for Mcast flow del |
| 49 | EventTypeMcastFlowRemoved FlowEventType = "McastFlowRemoved" |
| 50 | |
| 51 | //EventTypeServiceFlowRemoved - Event type for Service flow del |
| 52 | EventTypeServiceFlowRemoved FlowEventType = "ServiceFlowRemoved" |
| 53 | //EventTypeControlFlowRemoved - Event type for Control flow del |
| 54 | EventTypeControlFlowRemoved FlowEventType = "ControlFlowRemoved" |
| 55 | ) |
| 56 | |
| 57 | //FlowEvent - Event info for Flow event processing |
| 58 | type FlowEvent struct { |
| 59 | eType FlowEventType |
| 60 | device string |
| 61 | cookie string |
| 62 | eventData interface{} |
| 63 | } |
| 64 | |
| 65 | //InitEventFuncMapper - Initialization of flow event mapper |
| 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 | |
| 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 { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 80 | 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 Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 97 | eventMapper[flowEvent.eType](cntx, flowEvent, flowStatus) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 98 | return true |
| 99 | } |
| 100 | |
| 101 | //ProcessUsIgmpFlowAddEvent - Process Us Igmp Flow event trigger |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 102 | func ProcessUsIgmpFlowAddEvent(cntx context.Context, event *FlowEvent, flowStatus intf.FlowStatus) { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 103 | |
| 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 | |
| 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 | |
| 116 | logger.Infow(ctx, "Processing Post Flow Add Event for Service", log.Fields{"Cookie": event.cookie, "event": event}) |
| 117 | vs := event.eventData.(*VoltService) |
| 118 | if isFlowStatusSuccess(flowStatus.Status, true) { |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 119 | vs.FlowInstallSuccess(cntx, event.cookie, flowStatus.AdditionalData) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 120 | } else { |
| 121 | vs.FlowInstallFailure(event.cookie, flowStatus.Status, flowStatus.Reason) |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | //ProcessControlFlowAddEvent - Process Control Flow event trigger |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 126 | func ProcessControlFlowAddEvent(cntx context.Context, event *FlowEvent, flowStatus intf.FlowStatus) { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 127 | |
| 128 | logger.Infow(ctx, "Processing Post Flow Add Event for VPV", log.Fields{"Cookie": event.cookie, "event": event}) |
| 129 | vpv := event.eventData.(*VoltPortVnet) |
| 130 | if !isFlowStatusSuccess(flowStatus.Status, true) { |
| 131 | vpv.FlowInstallFailure(event.cookie, flowStatus.Status, flowStatus.Reason) |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | //ProcessServiceFlowDelEvent - Process Service Flow event trigger |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 136 | func ProcessServiceFlowDelEvent(cntx context.Context, event *FlowEvent, flowStatus intf.FlowStatus) { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 137 | |
| 138 | logger.Infow(ctx, "Processing Post Flow Remove Event for Service", log.Fields{"Cookie": event.cookie, "event": event}) |
| 139 | vs := event.eventData.(*VoltService) |
| 140 | if isFlowStatusSuccess(flowStatus.Status, false) { |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 141 | vs.FlowRemoveSuccess(cntx, event.cookie) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 142 | } else { |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 143 | vs.FlowRemoveFailure(cntx, event.cookie, flowStatus.Status, flowStatus.Reason) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | |
| 147 | //ProcessControlFlowDelEvent - Process Control Flow event trigger |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 148 | func ProcessControlFlowDelEvent(cntx context.Context, event *FlowEvent, flowStatus intf.FlowStatus) { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 149 | |
| 150 | logger.Infow(ctx, "Processing Post Flow Remove Event for VPV", log.Fields{"Cookie": event.cookie, "event": event}) |
| 151 | vpv := event.eventData.(*VoltPortVnet) |
| 152 | if isFlowStatusSuccess(flowStatus.Status, false) { |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 153 | vpv.FlowRemoveSuccess(cntx, event.cookie, event.device) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 154 | } else { |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 155 | vpv.FlowRemoveFailure(cntx, event.cookie, event.device, flowStatus.Status, flowStatus.Reason) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | |
| 159 | //ProcessMcastFlowDelEvent - Process Control Flow event trigger |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 160 | func ProcessMcastFlowDelEvent(cntx context.Context, event *FlowEvent, flowStatus intf.FlowStatus) { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 161 | |
| 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 | |
| 171 | //ProcessDeviceFlowDelEvent - Process Control Flow event trigger |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 172 | func ProcessDeviceFlowDelEvent(cntx context.Context, event *FlowEvent, flowStatus intf.FlowStatus) { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 173 | |
| 174 | logger.Infow(ctx, "Processing Post Flow Remove Event for VNET", log.Fields{"Cookie": event.cookie, "event": event}) |
| 175 | vnet := event.eventData.(*VoltVnet) |
| 176 | if isFlowStatusSuccess(flowStatus.Status, false) { |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 177 | vnet.FlowRemoveSuccess(cntx, event.cookie, event.device) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 178 | } else { |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 179 | vnet.FlowRemoveFailure(cntx, event.cookie, event.device, flowStatus.Status, flowStatus.Reason) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | |
| 183 | //TODO: Update the func or flowStatus struct once all flow status are based on NB error code |
| 184 | func isFlowStatusSuccess(status uint32, flowAdd bool) bool { |
| 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 | } |