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. |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 14 | */ |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 15 | |
| 16 | package controller |
| 17 | |
| 18 | import ( |
| 19 | "context" |
| 20 | "time" |
| 21 | |
| 22 | infraerror "voltha-go-controller/internal/pkg/errorcodes" |
| 23 | infraerrorcode "voltha-go-controller/internal/pkg/errorcodes/service" |
| 24 | |
| 25 | "voltha-go-controller/internal/pkg/of" |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame] | 26 | "voltha-go-controller/log" |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 27 | |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 28 | "google.golang.org/grpc/codes" |
| 29 | ) |
| 30 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 31 | // ModGroupTask - Group Modification Task |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 32 | type ModGroupTask struct { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 33 | ctx context.Context |
| 34 | group *of.Group |
| 35 | device *Device |
| 36 | timestamp string |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 37 | taskID uint8 |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 38 | } |
| 39 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 40 | // NewModGroupTask - Initializes new group task |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 41 | func NewModGroupTask(ctx context.Context, group *of.Group, device *Device) *ModGroupTask { |
| 42 | var grp ModGroupTask |
| 43 | grp.device = device |
| 44 | grp.group = group |
| 45 | grp.ctx = ctx |
| 46 | tstamp := (time.Now()).Format(time.RFC3339Nano) |
| 47 | grp.timestamp = tstamp |
| 48 | return &grp |
| 49 | } |
| 50 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 51 | // Name - Name of task |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 52 | func (grp *ModGroupTask) Name() string { |
| 53 | return "Group Mod Task" |
| 54 | } |
| 55 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 56 | // TaskID - Task id |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 57 | func (grp *ModGroupTask) TaskID() uint8 { |
| 58 | return grp.taskID |
| 59 | } |
| 60 | |
| 61 | // Timestamp to return timestamp of the task |
| 62 | func (grp *ModGroupTask) Timestamp() string { |
| 63 | return grp.timestamp |
| 64 | } |
| 65 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 66 | // Stop - task stop |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 67 | func (grp *ModGroupTask) Stop() { |
| 68 | } |
| 69 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 70 | // Start - task start |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 71 | func (grp *ModGroupTask) Start(ctx context.Context, taskID uint8) error { |
| 72 | var err error |
| 73 | grp.taskID = taskID |
| 74 | grp.ctx = ctx |
| 75 | i := 0 |
| 76 | |
| 77 | processGroupModResult := func(err error) bool { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 78 | statusCode, statusMsg := infraerror.GetErrorInfo(err) |
| 79 | |
| 80 | if infraerrorcode.ErrorCode(statusCode) != infraerrorcode.ErrOk { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 81 | if grp.group.Command == of.GroupCommandAdd && (codes.Code(statusCode) == codes.AlreadyExists) { |
| 82 | logger.Warnw(ctx, "Update Group Table Failed - Ignoring since Group Already exists", |
| 83 | log.Fields{"groupId": grp.group.GroupID, "groupOp": grp.group.Command, "Status": statusCode, "errorReason": statusMsg}) |
| 84 | return true |
| 85 | } |
| 86 | logger.Errorw(ctx, "Update Group Table Failed", |
| 87 | log.Fields{"groupId": grp.group.GroupID, "groupOp": grp.group.Command, "Status": statusCode, "errorReason": statusMsg}) |
| 88 | return false |
| 89 | } |
Akash Soni | 6168f31 | 2023-05-18 20:57:33 +0530 | [diff] [blame] | 90 | logger.Debugw(ctx, "Group Mod Result", log.Fields{"groupID": grp.group.GroupID, "Error Code": statusCode}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 91 | return true |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | if grp.group.Command != of.GroupCommandDel { |
| 95 | grp.group.State = of.GroupOperPending |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 96 | grp.device.UpdateGroupEntry(ctx, grp.group) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 97 | } else { |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 98 | grp.device.DelGroupEntry(ctx, grp.group) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | if !grp.device.isSBOperAllowed(grp.group.ForceAction) { |
Akash Soni | 6168f31 | 2023-05-18 20:57:33 +0530 | [diff] [blame] | 102 | logger.Warnw(ctx, "Skipping Group Table Update", log.Fields{"Reason": "Device State not UP", "State": grp.device.State, "GroupID": grp.group.GroupID, "Operation": grp.group.Command}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 103 | return nil |
| 104 | } |
| 105 | |
| 106 | groupUpdate := of.CreateGroupTableUpdate(grp.group) |
| 107 | if vc := grp.device.VolthaClient(); vc != nil { |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 108 | // Retry on group mod failure |
| 109 | // Retry attempts = 3 |
| 110 | // Delay between retry = 100ms. Total Possible Delay = 200ms |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 111 | for { |
| 112 | logger.Infow(ctx, "Group Mod Triggered", log.Fields{"GroupId": grp.group.GroupID, "Attempt": i}) |
| 113 | _, err = vc.UpdateLogicalDeviceFlowGroupTable(grp.ctx, groupUpdate) |
| 114 | if isSuccess := processGroupModResult(err); isSuccess { |
| 115 | break |
| 116 | } |
| 117 | i++ |
| 118 | if i < 3 { |
| 119 | time.Sleep(100 * time.Millisecond) |
| 120 | continue |
| 121 | } |
| 122 | logger.Errorw(ctx, "Update Group Table Failed on all 3 attempts. Dropping request", log.Fields{"GroupId": grp.group.GroupID, "Bucket": grp.group.Buckets}) |
| 123 | break |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 124 | } |
| 125 | return err |
| 126 | } |
| 127 | logger.Error(ctx, "Update Group Flow Table Failed: Voltha Client Unavailable") |
| 128 | return nil |
| 129 | } |