Mahir Gunyel | 03de0d3 | 2020-06-03 01:36:59 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 3 | |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package device |
| 18 | |
| 19 | import ( |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 20 | "context" |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 21 | "fmt" |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 22 | "strconv" |
| 23 | |
| 24 | "github.com/gogo/protobuf/proto" |
| 25 | coreutils "github.com/opencord/voltha-go/rw_core/utils" |
yasin sapli | 5458a1c | 2021-06-14 22:24:38 +0000 | [diff] [blame] | 26 | "github.com/opencord/voltha-lib-go/v5/pkg/log" |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 27 | "github.com/opencord/voltha-protos/v4/go/common" |
Maninder | dfadc98 | 2020-10-28 14:04:33 +0530 | [diff] [blame] | 28 | ofp "github.com/opencord/voltha-protos/v4/go/openflow_13" |
| 29 | "github.com/opencord/voltha-protos/v4/go/voltha" |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 30 | "google.golang.org/grpc/codes" |
| 31 | "google.golang.org/grpc/status" |
Mahir Gunyel | 03de0d3 | 2020-06-03 01:36:59 -0700 | [diff] [blame] | 32 | ) |
| 33 | |
| 34 | // listDeviceGroups returns logical device flow groups |
| 35 | func (agent *Agent) listDeviceGroups() map[uint32]*ofp.OfpGroupEntry { |
khenaidoo | 7585a96 | 2021-06-10 16:15:38 -0400 | [diff] [blame] | 36 | groupIDs := agent.groupCache.ListIDs() |
Mahir Gunyel | 03de0d3 | 2020-06-03 01:36:59 -0700 | [diff] [blame] | 37 | groups := make(map[uint32]*ofp.OfpGroupEntry, len(groupIDs)) |
| 38 | for groupID := range groupIDs { |
khenaidoo | 7585a96 | 2021-06-10 16:15:38 -0400 | [diff] [blame] | 39 | if groupHandle, have := agent.groupCache.Lock(groupID); have { |
Mahir Gunyel | 03de0d3 | 2020-06-03 01:36:59 -0700 | [diff] [blame] | 40 | groups[groupID] = groupHandle.GetReadOnly() |
| 41 | groupHandle.Unlock() |
| 42 | } |
| 43 | } |
| 44 | return groups |
| 45 | } |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 46 | |
| 47 | func (agent *Agent) addGroupsToAdapter(ctx context.Context, newGroups []*ofp.OfpGroupEntry, flowMetadata *voltha.FlowMetadata) (coreutils.Response, error) { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 48 | logger.Debugw(ctx, "add-groups-to-adapters", log.Fields{"device-id": agent.deviceID, "groups": newGroups, "flow-metadata": flowMetadata}) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 49 | |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 50 | var desc string |
| 51 | operStatus := &common.OperationResp{Code: common.OperationResp_OPERATION_FAILURE} |
| 52 | |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 53 | if (len(newGroups)) == 0 { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 54 | logger.Debugw(ctx, "nothing-to-update", log.Fields{"device-id": agent.deviceID, "groups": newGroups}) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 55 | return coreutils.DoneResponse(), nil |
| 56 | } |
| 57 | |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 58 | device, err := agent.getDeviceReadOnly(ctx) |
| 59 | if err != nil { |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 60 | desc = err.Error() |
| 61 | agent.logDeviceUpdate(ctx, "addGroupsToAdapter", nil, nil, operStatus, &desc) |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 62 | return coreutils.DoneResponse(), status.Errorf(codes.Aborted, "%s", err) |
| 63 | } |
Maninder | 2195ccc | 2021-06-23 20:23:01 +0530 | [diff] [blame] | 64 | |
| 65 | if !agent.proceedWithRequest(device) { |
| 66 | desc = fmt.Sprintf("deviceId:%s, Cannot complete operation as device deletion is in progress or reconciling is in progress/failed", device.Id) |
| 67 | agent.logDeviceUpdate(ctx, "addGroupsToAdapter", nil, nil, operStatus, &desc) |
| 68 | return coreutils.DoneResponse(), status.Errorf(codes.FailedPrecondition, "%s", desc) |
| 69 | } |
| 70 | |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 71 | dType, err := agent.adapterMgr.GetDeviceType(ctx, &voltha.ID{Id: device.Type}) |
| 72 | if err != nil { |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 73 | desc = fmt.Sprintf("non-existent-device-type-%s", device.Type) |
| 74 | agent.logDeviceUpdate(ctx, "addGroupsToAdapter", nil, nil, operStatus, &desc) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 75 | return coreutils.DoneResponse(), status.Errorf(codes.FailedPrecondition, "non-existent-device-type-%s", device.Type) |
| 76 | } |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 77 | |
| 78 | groupsToAdd := make([]*ofp.OfpGroupEntry, 0) |
| 79 | groupsToDelete := make([]*ofp.OfpGroupEntry, 0) |
| 80 | for _, group := range newGroups { |
khenaidoo | 7585a96 | 2021-06-10 16:15:38 -0400 | [diff] [blame] | 81 | groupHandle, created, err := agent.groupCache.LockOrCreate(ctx, group) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 82 | if err != nil { |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 83 | desc = err.Error() |
| 84 | agent.logDeviceUpdate(ctx, "addGroupsToAdapter", nil, nil, operStatus, &desc) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 85 | return coreutils.DoneResponse(), err |
| 86 | } |
| 87 | |
| 88 | if created { |
| 89 | groupsToAdd = append(groupsToAdd, group) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 90 | } else { |
| 91 | groupToChange := groupHandle.GetReadOnly() |
| 92 | if !proto.Equal(groupToChange, group) { |
| 93 | //Group needs to be updated. |
| 94 | if err := groupHandle.Update(ctx, group); err != nil { |
| 95 | groupHandle.Unlock() |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 96 | desc = fmt.Sprintf("failure-updating-group-%s-to-device-%s", strconv.Itoa(int(group.Desc.GroupId)), agent.deviceID) |
| 97 | agent.logDeviceUpdate(ctx, "addGroupsToAdapter", nil, nil, operStatus, &desc) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 98 | return coreutils.DoneResponse(), status.Errorf(codes.Internal, "failure-updating-group-%s-to-device-%s", strconv.Itoa(int(group.Desc.GroupId)), agent.deviceID) |
| 99 | } |
| 100 | groupsToDelete = append(groupsToDelete, groupToChange) |
| 101 | groupsToAdd = append(groupsToAdd, group) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 102 | } else { |
| 103 | //No need to change the group. It is already exist. |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 104 | logger.Debugw(ctx, "no-need-to-change-already-existing-group", log.Fields{"device-id": agent.deviceID, "group": newGroups, "flow-metadata": flowMetadata}) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | |
| 108 | groupHandle.Unlock() |
| 109 | } |
| 110 | // Sanity check |
| 111 | if (len(groupsToAdd)) == 0 { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 112 | logger.Debugw(ctx, "no-groups-to-update", log.Fields{"device-id": agent.deviceID, "groups": newGroups}) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 113 | return coreutils.DoneResponse(), nil |
| 114 | } |
| 115 | |
| 116 | // Send update to adapters |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 117 | subCtx, cancel := context.WithTimeout(log.WithSpanFromContext(context.Background(), ctx), agent.defaultTimeout) |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 118 | subCtx = coreutils.WithRPCMetadataFromContext(subCtx, ctx) |
| 119 | |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 120 | response := coreutils.NewResponse() |
| 121 | if !dType.AcceptsAddRemoveFlowUpdates { |
Kent Hagerman | a7c9d79 | 2020-07-16 17:39:01 -0400 | [diff] [blame] | 122 | updatedAllGroups := agent.listDeviceGroups() |
| 123 | rpcResponse, err := agent.adapterProxy.UpdateFlowsBulk(subCtx, device, nil, updatedAllGroups, flowMetadata) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 124 | if err != nil { |
| 125 | cancel() |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 126 | desc = err.Error() |
| 127 | agent.logDeviceUpdate(ctx, "addGroupsToAdapter", nil, nil, operStatus, &desc) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 128 | return coreutils.DoneResponse(), err |
| 129 | } |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 130 | go agent.waitForAdapterFlowResponse(subCtx, cancel, "addGroupsToAdapter", rpcResponse, response) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 131 | } else { |
| 132 | flowChanges := &ofp.FlowChanges{ |
| 133 | ToAdd: &voltha.Flows{Items: []*ofp.OfpFlowStats{}}, |
| 134 | ToRemove: &voltha.Flows{Items: []*ofp.OfpFlowStats{}}, |
| 135 | } |
| 136 | groupChanges := &ofp.FlowGroupChanges{ |
| 137 | ToAdd: &voltha.FlowGroups{Items: groupsToAdd}, |
| 138 | ToRemove: &voltha.FlowGroups{Items: groupsToDelete}, |
| 139 | ToUpdate: &voltha.FlowGroups{Items: []*ofp.OfpGroupEntry{}}, |
| 140 | } |
| 141 | rpcResponse, err := agent.adapterProxy.UpdateFlowsIncremental(subCtx, device, flowChanges, groupChanges, flowMetadata) |
| 142 | if err != nil { |
| 143 | cancel() |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 144 | desc = err.Error() |
| 145 | agent.logDeviceUpdate(ctx, "addGroupsToAdapter", nil, nil, operStatus, &desc) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 146 | return coreutils.DoneResponse(), err |
| 147 | } |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 148 | go agent.waitForAdapterFlowResponse(subCtx, cancel, "addGroupsToAdapter", rpcResponse, response) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 149 | } |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 150 | operStatus.Code = common.OperationResp_OPERATION_IN_PROGRESS |
| 151 | agent.logDeviceUpdate(ctx, "addGroupsToAdapter", nil, nil, operStatus, &desc) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 152 | return response, nil |
| 153 | } |
| 154 | |
| 155 | func (agent *Agent) deleteGroupsFromAdapter(ctx context.Context, groupsToDel []*ofp.OfpGroupEntry, flowMetadata *voltha.FlowMetadata) (coreutils.Response, error) { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 156 | logger.Debugw(ctx, "delete-groups-from-adapter", log.Fields{"device-id": agent.deviceID, "groups": groupsToDel}) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 157 | |
| 158 | if (len(groupsToDel)) == 0 { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 159 | logger.Debugw(ctx, "nothing-to-delete", log.Fields{"device-id": agent.deviceID}) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 160 | return coreutils.DoneResponse(), nil |
| 161 | } |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 162 | |
| 163 | var desc string |
| 164 | operStatus := &common.OperationResp{Code: common.OperationResp_OPERATION_FAILURE} |
| 165 | |
| 166 | defer agent.logDeviceUpdate(ctx, "deleteGroupsFromAdapter", nil, nil, operStatus, &desc) |
| 167 | |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 168 | device, err := agent.getDeviceReadOnly(ctx) |
| 169 | if err != nil { |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 170 | desc = err.Error() |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 171 | return coreutils.DoneResponse(), status.Errorf(codes.Aborted, "%s", err) |
| 172 | } |
Maninder | 2195ccc | 2021-06-23 20:23:01 +0530 | [diff] [blame] | 173 | |
| 174 | if !agent.proceedWithRequest(device) { |
| 175 | desc = fmt.Sprintf("deviceId:%s, Cannot complete operation as device deletion is in progress or reconciling is in progress/failed", device.Id) |
| 176 | agent.logDeviceUpdate(ctx, "deleteGroupsFromAdapter", nil, nil, operStatus, &desc) |
| 177 | return coreutils.DoneResponse(), status.Errorf(codes.FailedPrecondition, "%s", desc) |
| 178 | } |
| 179 | |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 180 | dType, err := agent.adapterMgr.GetDeviceType(ctx, &voltha.ID{Id: device.Type}) |
| 181 | if err != nil { |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 182 | desc = fmt.Sprintf("non-existent-device-type-%s", device.Type) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 183 | return coreutils.DoneResponse(), status.Errorf(codes.FailedPrecondition, "non-existent-device-type-%s", device.Type) |
| 184 | } |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 185 | |
| 186 | for _, group := range groupsToDel { |
khenaidoo | 7585a96 | 2021-06-10 16:15:38 -0400 | [diff] [blame] | 187 | if groupHandle, have := agent.groupCache.Lock(group.Desc.GroupId); have { |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 188 | // Update the store and cache |
| 189 | if err := groupHandle.Delete(ctx); err != nil { |
| 190 | groupHandle.Unlock() |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 191 | desc = err.Error() |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 192 | return coreutils.DoneResponse(), err |
| 193 | } |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 194 | groupHandle.Unlock() |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | // Send update to adapters |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 199 | subCtx, cancel := context.WithTimeout(log.WithSpanFromContext(context.Background(), ctx), agent.defaultTimeout) |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 200 | subCtx = coreutils.WithRPCMetadataFromContext(subCtx, ctx) |
| 201 | |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 202 | response := coreutils.NewResponse() |
| 203 | if !dType.AcceptsAddRemoveFlowUpdates { |
Kent Hagerman | a7c9d79 | 2020-07-16 17:39:01 -0400 | [diff] [blame] | 204 | updatedAllGroups := agent.listDeviceGroups() |
| 205 | rpcResponse, err := agent.adapterProxy.UpdateFlowsBulk(subCtx, device, nil, updatedAllGroups, flowMetadata) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 206 | if err != nil { |
| 207 | cancel() |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 208 | desc = err.Error() |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 209 | return coreutils.DoneResponse(), err |
| 210 | } |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 211 | go agent.waitForAdapterFlowResponse(subCtx, cancel, "deleteGroupsFromAdapter", rpcResponse, response) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 212 | } else { |
| 213 | flowChanges := &ofp.FlowChanges{ |
| 214 | ToAdd: &voltha.Flows{Items: []*ofp.OfpFlowStats{}}, |
| 215 | ToRemove: &voltha.Flows{Items: []*ofp.OfpFlowStats{}}, |
| 216 | } |
| 217 | groupChanges := &ofp.FlowGroupChanges{ |
| 218 | ToAdd: &voltha.FlowGroups{Items: []*ofp.OfpGroupEntry{}}, |
| 219 | ToRemove: &voltha.FlowGroups{Items: groupsToDel}, |
| 220 | ToUpdate: &voltha.FlowGroups{Items: []*ofp.OfpGroupEntry{}}, |
| 221 | } |
| 222 | rpcResponse, err := agent.adapterProxy.UpdateFlowsIncremental(subCtx, device, flowChanges, groupChanges, flowMetadata) |
| 223 | if err != nil { |
| 224 | cancel() |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 225 | desc = err.Error() |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 226 | return coreutils.DoneResponse(), err |
| 227 | } |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 228 | go agent.waitForAdapterFlowResponse(subCtx, cancel, "deleteGroupsFromAdapter", rpcResponse, response) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 229 | } |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 230 | operStatus.Code = common.OperationResp_OPERATION_IN_PROGRESS |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 231 | return response, nil |
| 232 | } |
| 233 | |
| 234 | func (agent *Agent) updateGroupsToAdapter(ctx context.Context, updatedGroups []*ofp.OfpGroupEntry, flowMetadata *voltha.FlowMetadata) (coreutils.Response, error) { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 235 | logger.Debugw(ctx, "update-groups-to-adapter", log.Fields{"device-id": agent.deviceID, "groups": updatedGroups}) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 236 | |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 237 | var desc string |
| 238 | operStatus := &common.OperationResp{Code: common.OperationResp_OPERATION_FAILURE} |
| 239 | |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 240 | if (len(updatedGroups)) == 0 { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 241 | logger.Debugw(ctx, "nothing-to-update", log.Fields{"device-id": agent.deviceID, "groups": updatedGroups}) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 242 | return coreutils.DoneResponse(), nil |
| 243 | } |
| 244 | |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 245 | device, err := agent.getDeviceReadOnly(ctx) |
| 246 | if err != nil { |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 247 | desc = err.Error() |
| 248 | agent.logDeviceUpdate(ctx, "updateGroupsToAdapter", nil, nil, operStatus, &desc) |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 249 | return coreutils.DoneResponse(), status.Errorf(codes.Aborted, "%s", err) |
| 250 | } |
Maninder | 2195ccc | 2021-06-23 20:23:01 +0530 | [diff] [blame] | 251 | |
| 252 | if !agent.proceedWithRequest(device) { |
| 253 | desc = fmt.Sprintf("deviceId:%s, Cannot complete operation as device deletion is in progress or Reconciling is in progress/failed", device.Id) |
| 254 | agent.logDeviceUpdate(ctx, "updateGroupsToAdapter", nil, nil, operStatus, &desc) |
| 255 | return coreutils.DoneResponse(), status.Errorf(codes.FailedPrecondition, "%s", desc) |
| 256 | } |
| 257 | |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 258 | if device.OperStatus != voltha.OperStatus_ACTIVE || device.ConnectStatus != voltha.ConnectStatus_REACHABLE || device.AdminState != voltha.AdminState_ENABLED { |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 259 | desc = fmt.Sprintf("invalid device states-oper-%s-connect-%s-admin-%s", device.OperStatus, device.ConnectStatus, device.AdminState) |
| 260 | agent.logDeviceUpdate(ctx, "updateGroupsToAdapter", nil, nil, operStatus, &desc) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 261 | return coreutils.DoneResponse(), status.Errorf(codes.FailedPrecondition, "invalid device states-oper-%s-connect-%s-admin-%s", device.OperStatus, device.ConnectStatus, device.AdminState) |
| 262 | } |
Maninder | 2195ccc | 2021-06-23 20:23:01 +0530 | [diff] [blame] | 263 | |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 264 | dType, err := agent.adapterMgr.GetDeviceType(ctx, &voltha.ID{Id: device.Type}) |
| 265 | if err != nil { |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 266 | desc = fmt.Sprintf("non-existent-device-type-%s", device.Type) |
| 267 | agent.logDeviceUpdate(ctx, "updateGroupsToAdapter", nil, nil, operStatus, &desc) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 268 | return coreutils.DoneResponse(), status.Errorf(codes.FailedPrecondition, "non-existent-device-type-%s", device.Type) |
| 269 | } |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 270 | |
Kent Hagerman | a7c9d79 | 2020-07-16 17:39:01 -0400 | [diff] [blame] | 271 | groupsToUpdate := make([]*ofp.OfpGroupEntry, 0) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 272 | for _, group := range updatedGroups { |
khenaidoo | 7585a96 | 2021-06-10 16:15:38 -0400 | [diff] [blame] | 273 | if groupHandle, have := agent.groupCache.Lock(group.Desc.GroupId); have { |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 274 | // Update the store and cache |
| 275 | if err := groupHandle.Update(ctx, group); err != nil { |
| 276 | groupHandle.Unlock() |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 277 | desc = err.Error() |
| 278 | agent.logDeviceUpdate(ctx, "updateGroupsToAdapter", nil, nil, operStatus, &desc) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 279 | return coreutils.DoneResponse(), err |
| 280 | } |
| 281 | groupsToUpdate = append(groupsToUpdate, group) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 282 | groupHandle.Unlock() |
| 283 | } |
| 284 | } |
| 285 | |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 286 | subCtx, cancel := context.WithTimeout(log.WithSpanFromContext(context.Background(), ctx), agent.defaultTimeout) |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 287 | subCtx = coreutils.WithRPCMetadataFromContext(subCtx, ctx) |
| 288 | |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 289 | response := coreutils.NewResponse() |
| 290 | // Process bulk flow update differently than incremental update |
| 291 | if !dType.AcceptsAddRemoveFlowUpdates { |
Kent Hagerman | a7c9d79 | 2020-07-16 17:39:01 -0400 | [diff] [blame] | 292 | updatedAllGroups := agent.listDeviceGroups() |
| 293 | rpcResponse, err := agent.adapterProxy.UpdateFlowsBulk(subCtx, device, nil, updatedAllGroups, nil) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 294 | if err != nil { |
| 295 | cancel() |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 296 | desc = err.Error() |
| 297 | agent.logDeviceUpdate(ctx, "updateGroupsToAdapter", nil, nil, operStatus, &desc) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 298 | return coreutils.DoneResponse(), err |
| 299 | } |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 300 | go agent.waitForAdapterFlowResponse(subCtx, cancel, "updateGroupsToAdapter", rpcResponse, response) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 301 | } else { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 302 | logger.Debugw(ctx, "updating-groups", |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 303 | log.Fields{ |
| 304 | "device-id": agent.deviceID, |
| 305 | "groups-to-update": groupsToUpdate, |
| 306 | }) |
| 307 | |
| 308 | // Sanity check |
| 309 | if (len(groupsToUpdate)) == 0 { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 310 | logger.Debugw(ctx, "nothing-to-update", log.Fields{"device-id": agent.deviceID, "groups": groupsToUpdate}) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 311 | cancel() |
| 312 | return coreutils.DoneResponse(), nil |
| 313 | } |
| 314 | |
| 315 | flowChanges := &ofp.FlowChanges{ |
| 316 | ToAdd: &voltha.Flows{Items: []*ofp.OfpFlowStats{}}, |
| 317 | ToRemove: &voltha.Flows{Items: []*ofp.OfpFlowStats{}}, |
| 318 | } |
| 319 | groupChanges := &ofp.FlowGroupChanges{ |
| 320 | ToAdd: &voltha.FlowGroups{Items: []*ofp.OfpGroupEntry{}}, |
| 321 | ToRemove: &voltha.FlowGroups{Items: []*ofp.OfpGroupEntry{}}, |
| 322 | ToUpdate: &voltha.FlowGroups{Items: groupsToUpdate}, |
| 323 | } |
| 324 | rpcResponse, err := agent.adapterProxy.UpdateFlowsIncremental(subCtx, device, flowChanges, groupChanges, flowMetadata) |
| 325 | if err != nil { |
| 326 | cancel() |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 327 | desc = err.Error() |
| 328 | agent.logDeviceUpdate(ctx, "updateGroupsToAdapter", nil, nil, operStatus, &desc) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 329 | return coreutils.DoneResponse(), err |
| 330 | } |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 331 | go agent.waitForAdapterFlowResponse(subCtx, cancel, "updateGroupsToAdapter", rpcResponse, response) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 332 | } |
| 333 | |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 334 | operStatus.Code = common.OperationResp_OPERATION_IN_PROGRESS |
| 335 | agent.logDeviceUpdate(ctx, "updateGroupsToAdapter", nil, nil, operStatus, &desc) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 336 | return response, nil |
| 337 | } |