blob: 83ead455140646db92e17af012cc1b934902c6cb [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.
vinokuma926cb3e2023-03-29 11:41:06 +053014 */
Naveen Sampath04696f72022-06-13 15:19:14 +053015
16package controller
17
18import (
19 "context"
vinokuma926cb3e2023-03-29 11:41:06 +053020 "time"
Naveen Sampath04696f72022-06-13 15:19:14 +053021 infraerror "voltha-go-controller/internal/pkg/errorcodes"
22 infraerrorcode "voltha-go-controller/internal/pkg/errorcodes/service"
Naveen Sampath04696f72022-06-13 15:19:14 +053023
24 "voltha-go-controller/internal/pkg/of"
Tinoj Joseph1d108322022-07-13 10:07:39 +053025 "voltha-go-controller/log"
Naveen Sampath04696f72022-06-13 15:19:14 +053026)
27
28const (
vinokuma926cb3e2023-03-29 11:41:06 +053029 // MaxRetryCount - Maximum retry attempts on failure
Naveen Sampath04696f72022-06-13 15:19:14 +053030 MaxRetryCount int = 1
31)
32
33// AddFlowsTask structure
34type AddFlowsTask struct {
Naveen Sampath04696f72022-06-13 15:19:14 +053035 ctx context.Context
36 flow *of.VoltFlow
37 device *Device
38 timestamp string
vinokuma926cb3e2023-03-29 11:41:06 +053039 taskID uint8
Naveen Sampath04696f72022-06-13 15:19:14 +053040}
41
42// NewAddFlowsTask is constructor for AddFlowsTask
43func NewAddFlowsTask(ctx context.Context, flow *of.VoltFlow, device *Device) *AddFlowsTask {
44 var aft AddFlowsTask
45 aft.device = device
46 aft.flow = flow
47 aft.ctx = ctx
48 tstamp := (time.Now()).Format(time.RFC3339Nano)
49 aft.timestamp = tstamp
50 return &aft
51}
52
53// Name to add flow task
54func (aft *AddFlowsTask) Name() string {
55 for _, flow := range aft.flow.SubFlows {
Akash Sonie863fe42023-11-30 14:35:01 +053056 logger.Debugw(ctx, "Flow Cookies", log.Fields{"Cookie": flow.Cookie})
Naveen Sampath04696f72022-06-13 15:19:14 +053057 }
58 return "Add Flows Task"
59}
60
61// TaskID to return task ID
62func (aft *AddFlowsTask) TaskID() uint8 {
63 return aft.taskID
64}
65
66// Timestamp to return timestamp
67func (aft *AddFlowsTask) Timestamp() string {
68 return aft.timestamp
69}
70
71// Stop to stop the add flow task
72func (aft *AddFlowsTask) Stop() {
73}
74
75// Start to start adding flow task
76func (aft *AddFlowsTask) Start(ctx context.Context, taskID uint8) error {
77 var err error
78 aft.taskID = taskID
79 aft.ctx = ctx
80 flowsToProcess := make(map[uint64]*of.VoltSubFlow)
Naveen Sampath04696f72022-06-13 15:19:14 +053081 // First add/delete the flows first locally before passing them to actual device
82 for _, flow := range aft.flow.SubFlows {
Akash Sonie863fe42023-11-30 14:35:01 +053083 logger.Debugw(ctx, "Flow Mod Request", log.Fields{"Cookie": flow.Cookie, "Oper": aft.flow.Command, "Port": aft.flow.PortID})
Naveen Sampath04696f72022-06-13 15:19:14 +053084 if aft.flow.Command == of.CommandAdd {
Naveen Sampath04696f72022-06-13 15:19:14 +053085 flowsToProcess[flow.Cookie] = flow
86 } else {
87 dbFlow, ok := aft.device.GetFlow(flow.Cookie)
88 if !ok {
89 logger.Warnw(ctx, "Delete Flow Error: Flow Does not Exist", log.Fields{"Cookie": flow.Cookie, "Device": aft.device.ID})
90 } else {
91 // dbFlow.State = of.FlowDelPending
92 // aft.device.AddFlowToDb(dbFlow)
93 flowsToProcess[flow.Cookie] = dbFlow
94 }
Akash Sonief452f12024-12-12 18:20:28 +053095 // Below call will delete flow from DB and will not allow to maintain flow count and state. Hence commenting the below call.
96 //aft.device.triggerFlowNotification(ctx, flow.Cookie, aft.flow.Command, of.BwAvailDetails{}, nil, false)
Naveen Sampath04696f72022-06-13 15:19:14 +053097 }
98 }
99
Naveen Sampath04696f72022-06-13 15:19:14 +0530100 // PortName and PortID are used for validation of PortID, whether it is still valid and associated with old PortName or
101 // PortID got assigned to another PortName. If the condition met, skip these flow update to voltha core
102 if aft.flow.PortName != "" && aft.flow.PortID != 0 {
103 portName, _ := aft.device.GetPortName(aft.flow.PortID)
104 if aft.flow.PortName != portName && portName != "" {
105 for _, flow := range aft.flow.SubFlows {
Akash Soni6168f312023-05-18 20:57:33 +0530106 logger.Warnw(ctx, "Skip Flow Update", log.Fields{"Reason": "Port Deleted", "PortName": aft.flow.PortName, "PortNo": aft.flow.PortID, "Cookie": flow.Cookie, "Operation": aft.flow.Command})
Naveen Sampath04696f72022-06-13 15:19:14 +0530107 if aft.flow.Command == of.CommandDel {
Akash Sonief452f12024-12-12 18:20:28 +0530108 aft.device.triggerFlowNotification(ctx, flow.Cookie, aft.flow.Command, of.BwAvailDetails{}, nil)
Naveen Sampath04696f72022-06-13 15:19:14 +0530109 }
110 }
111 return nil
112 }
113 }
114
115 if !aft.device.isSBOperAllowed(aft.flow.ForceAction) {
116 for _, flow := range aft.flow.SubFlows {
Akash Soni6168f312023-05-18 20:57:33 +0530117 logger.Warnw(ctx, "Skipping Flow Table Update", log.Fields{"Reason": "Device State not UP", "State": aft.device.State, "Cookie": flow.Cookie, "Operation": aft.flow.Command})
Naveen Sampath04696f72022-06-13 15:19:14 +0530118 }
119 return nil
120 }
121
122 flows := of.ProcessVoltFlow(aft.device.ID, aft.flow.Command, flowsToProcess)
123 for _, flow := range flows {
124 attempt := 0
125 if vc := aft.device.VolthaClient(); vc != nil {
126 for {
127 if _, err = vc.UpdateLogicalDeviceFlowTable(aft.ctx, flow); err != nil {
128 logger.Errorw(ctx, "Update Flow Table Failed", log.Fields{"Cookie": flow.GetFlowMod().Cookie, "Reason": err.Error(), "Operation": aft.flow.Command})
129 statusCode, _ := infraerror.GetErrorInfo(err)
130
131 // Retry on flow delete failure once.
132 // Do NOT retry incase of failure with reason: Entry Not Found
133 if aft.flow.Command == of.CommandDel && statusCode != uint32(infraerrorcode.ErrNotExists) {
134 if attempt != MaxRetryCount {
Akash Soni6168f312023-05-18 20:57:33 +0530135 logger.Warnw(ctx, "Retrying Flow Delete", log.Fields{"Cookie": flow.GetFlowMod().Cookie, "Attempt": attempt})
Naveen Sampath04696f72022-06-13 15:19:14 +0530136 attempt++
137 continue
138 }
139 logger.Errorw(ctx, "Flow Delete failed even aft max retries", log.Fields{"Flow": flow, "Attempt": attempt})
140 }
141 }
142 break
143 }
Akash Sonief452f12024-12-12 18:20:28 +0530144 aft.device.triggerFlowNotification(ctx, flow.FlowMod.Cookie, aft.flow.Command, of.BwAvailDetails{}, err)
Naveen Sampath04696f72022-06-13 15:19:14 +0530145 } else {
146 logger.Errorw(ctx, "Update Flow Table Failed: Voltha Client Unavailable", log.Fields{"Flow": flow})
147 }
148 }
149 return nil
150}
151
152func isFlowOperSuccess(statusCode uint32, oper of.Command) bool {
153 volthaErrorCode := infraerrorcode.ErrorCode(statusCode)
154
155 if volthaErrorCode == infraerrorcode.ErrOk {
156 return true
157 }
158
159 if oper == of.CommandAdd && volthaErrorCode == infraerrorcode.ErrAlreadyExists {
160 return true
Naveen Sampath04696f72022-06-13 15:19:14 +0530161 } else if oper == of.CommandDel && volthaErrorCode == infraerrorcode.ErrNotExists {
162 return true
163 }
164 return false
165}
166
167// func getBwAvailInfo(bwAvailInfo []*voltha.ResponseMsg) of.BwAvailDetails {
168// var bwInfo of.BwAvailDetails
169// // convert the bw details sent from olt to a struct
170// // received msg format:
171// // additional_data[Data{ResponseMsg
172// //{"key":"prevBW","value":"111111"},
173// //{"key":"presentBW","value":"10000"}]
174// if len(bwAvailInfo) > 1 {
175// prevBwResp := bwAvailInfo[0]
176// if prevBwResp.Key == of.PrevBwInfo {
177// _, err := strconv.Atoi(prevBwResp.Val)
178// if err == nil {
179// bwInfo.PrevBw = prevBwResp.Val
180// }
181// }
182
183// presentBwResp := bwAvailInfo[1]
184// if presentBwResp.Key == of.PresentBwInfo {
185// _, err := strconv.Atoi(prevBwResp.Val)
186// if err == nil {
187// bwInfo.PresentBw = presentBwResp.Val
188// }
189// }
190// if bwInfo.PresentBw == bwInfo.PrevBw {
191// return of.BwAvailDetails{}
192// }
193// logger.Infow(ctx, "Bandwidth-consumed-info", log.Fields{"BwConsumed": bwInfo})
194// }
195// return bwInfo
196// }