Mahir Gunyel | 03de0d3 | 2020-06-03 01:36:59 -0700 | [diff] [blame] | 1 | /* |
Joey Armstrong | 5f51f2e | 2023-01-17 17:06:26 -0500 | [diff] [blame] | 2 | * Copyright 2018-2023 Open Networking Foundation (ONF) and the ONF Contributors |
Mahir Gunyel | 03de0d3 | 2020-06-03 01:36:59 -0700 | [diff] [blame] | 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" |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 21 | |
| 22 | "github.com/gogo/protobuf/proto" |
| 23 | coreutils "github.com/opencord/voltha-go/rw_core/utils" |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 24 | "github.com/opencord/voltha-lib-go/v7/pkg/log" |
| 25 | "github.com/opencord/voltha-protos/v5/go/common" |
| 26 | ofp "github.com/opencord/voltha-protos/v5/go/openflow_13" |
| 27 | "github.com/opencord/voltha-protos/v5/go/voltha" |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 28 | "google.golang.org/grpc/codes" |
| 29 | "google.golang.org/grpc/status" |
Mahir Gunyel | 03de0d3 | 2020-06-03 01:36:59 -0700 | [diff] [blame] | 30 | ) |
| 31 | |
| 32 | // listDeviceGroups returns logical device flow groups |
| 33 | func (agent *Agent) listDeviceGroups() map[uint32]*ofp.OfpGroupEntry { |
khenaidoo | 7585a96 | 2021-06-10 16:15:38 -0400 | [diff] [blame] | 34 | groupIDs := agent.groupCache.ListIDs() |
Mahir Gunyel | 03de0d3 | 2020-06-03 01:36:59 -0700 | [diff] [blame] | 35 | groups := make(map[uint32]*ofp.OfpGroupEntry, len(groupIDs)) |
| 36 | for groupID := range groupIDs { |
khenaidoo | 7585a96 | 2021-06-10 16:15:38 -0400 | [diff] [blame] | 37 | if groupHandle, have := agent.groupCache.Lock(groupID); have { |
Mahir Gunyel | 03de0d3 | 2020-06-03 01:36:59 -0700 | [diff] [blame] | 38 | groups[groupID] = groupHandle.GetReadOnly() |
| 39 | groupHandle.Unlock() |
| 40 | } |
| 41 | } |
| 42 | return groups |
| 43 | } |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 44 | |
khenaidoo | 9beaaf1 | 2021-10-19 17:32:01 -0400 | [diff] [blame] | 45 | func (agent *Agent) addGroupsToAdapter(ctx context.Context, newGroups []*ofp.OfpGroupEntry, flowMetadata *ofp.FlowMetadata) (coreutils.Response, error) { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 46 | 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] | 47 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 48 | var err error |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 49 | var desc string |
| 50 | operStatus := &common.OperationResp{Code: common.OperationResp_OPERATION_FAILURE} |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 51 | defer func() { agent.logDeviceUpdate(ctx, nil, nil, operStatus, err, desc) }() |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 52 | |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 53 | if (len(newGroups)) == 0 { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 54 | desc = "no new groups" |
| 55 | operStatus.Code = common.OperationResp_OPERATION_SUCCESS |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 56 | return coreutils.DoneResponse(), nil |
| 57 | } |
| 58 | |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 59 | device, err := agent.getDeviceReadOnly(ctx) |
| 60 | if err != nil { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 61 | return coreutils.DoneResponse(), err |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 62 | } |
Maninder | 2195ccc | 2021-06-23 20:23:01 +0530 | [diff] [blame] | 63 | |
| 64 | if !agent.proceedWithRequest(device) { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 65 | err = status.Errorf(codes.FailedPrecondition, "%s", "cannot complete operation as device deletion is in progress or reconciling is in progress/failed") |
| 66 | return coreutils.DoneResponse(), err |
Maninder | 2195ccc | 2021-06-23 20:23:01 +0530 | [diff] [blame] | 67 | } |
| 68 | |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 69 | dType, err := agent.adapterMgr.GetDeviceType(ctx, &voltha.ID{Id: device.Type}) |
| 70 | if err != nil { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 71 | return coreutils.DoneResponse(), err |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 72 | } |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 73 | |
| 74 | groupsToAdd := make([]*ofp.OfpGroupEntry, 0) |
| 75 | groupsToDelete := make([]*ofp.OfpGroupEntry, 0) |
| 76 | for _, group := range newGroups { |
khenaidoo | 7585a96 | 2021-06-10 16:15:38 -0400 | [diff] [blame] | 77 | groupHandle, created, err := agent.groupCache.LockOrCreate(ctx, group) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 78 | if err != nil { |
| 79 | return coreutils.DoneResponse(), err |
| 80 | } |
| 81 | |
| 82 | if created { |
| 83 | groupsToAdd = append(groupsToAdd, group) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 84 | } else { |
| 85 | groupToChange := groupHandle.GetReadOnly() |
| 86 | if !proto.Equal(groupToChange, group) { |
| 87 | //Group needs to be updated. |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 88 | if err = groupHandle.Update(ctx, group); err != nil { |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 89 | groupHandle.Unlock() |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 90 | return coreutils.DoneResponse(), err |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 91 | } |
| 92 | groupsToDelete = append(groupsToDelete, groupToChange) |
| 93 | groupsToAdd = append(groupsToAdd, group) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 94 | } else { |
| 95 | //No need to change the group. It is already exist. |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 96 | 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] | 97 | } |
| 98 | } |
| 99 | |
| 100 | groupHandle.Unlock() |
| 101 | } |
| 102 | // Sanity check |
| 103 | if (len(groupsToAdd)) == 0 { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 104 | desc = "no group to update" |
| 105 | operStatus.Code = common.OperationResp_OPERATION_SUCCESS |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 106 | return coreutils.DoneResponse(), nil |
| 107 | } |
| 108 | |
| 109 | // Send update to adapters |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 110 | response := coreutils.NewResponse() |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 111 | subCtx := coreutils.WithSpanAndRPCMetadataFromContext(ctx) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 112 | if !dType.AcceptsAddRemoveFlowUpdates { |
Kent Hagerman | a7c9d79 | 2020-07-16 17:39:01 -0400 | [diff] [blame] | 113 | updatedAllGroups := agent.listDeviceGroups() |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 114 | ctr, groupSlice := 0, make([]*ofp.OfpGroupEntry, len(updatedAllGroups)) |
| 115 | for _, group := range updatedAllGroups { |
| 116 | groupSlice[ctr] = group |
| 117 | ctr++ |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 118 | } |
khenaidoo | 9beaaf1 | 2021-10-19 17:32:01 -0400 | [diff] [blame] | 119 | go agent.sendBulkFlows(subCtx, device, nil, &ofp.FlowGroups{Items: groupSlice}, flowMetadata, response) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 120 | } else { |
| 121 | flowChanges := &ofp.FlowChanges{ |
khenaidoo | 9beaaf1 | 2021-10-19 17:32:01 -0400 | [diff] [blame] | 122 | ToAdd: &ofp.Flows{Items: []*ofp.OfpFlowStats{}}, |
| 123 | ToRemove: &ofp.Flows{Items: []*ofp.OfpFlowStats{}}, |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 124 | } |
| 125 | groupChanges := &ofp.FlowGroupChanges{ |
khenaidoo | 9beaaf1 | 2021-10-19 17:32:01 -0400 | [diff] [blame] | 126 | ToAdd: &ofp.FlowGroups{Items: groupsToAdd}, |
| 127 | ToRemove: &ofp.FlowGroups{Items: groupsToDelete}, |
| 128 | ToUpdate: &ofp.FlowGroups{Items: []*ofp.OfpGroupEntry{}}, |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 129 | } |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 130 | go agent.sendIncrementalFlows(subCtx, device, flowChanges, groupChanges, flowMetadata, response) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 131 | } |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 132 | operStatus.Code = common.OperationResp_OPERATION_IN_PROGRESS |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 133 | return response, nil |
| 134 | } |
| 135 | |
khenaidoo | 9beaaf1 | 2021-10-19 17:32:01 -0400 | [diff] [blame] | 136 | func (agent *Agent) deleteGroupsFromAdapter(ctx context.Context, groupsToDel []*ofp.OfpGroupEntry, flowMetadata *ofp.FlowMetadata) (coreutils.Response, error) { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 137 | 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] | 138 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 139 | var desc string |
| 140 | var err error |
| 141 | operStatus := &common.OperationResp{Code: common.OperationResp_OPERATION_FAILURE} |
| 142 | defer func() { agent.logDeviceUpdate(ctx, nil, nil, operStatus, err, desc) }() |
| 143 | |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 144 | if (len(groupsToDel)) == 0 { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 145 | desc = "nothing to delete" |
| 146 | operStatus.Code = common.OperationResp_OPERATION_SUCCESS |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 147 | return coreutils.DoneResponse(), nil |
| 148 | } |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 149 | |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 150 | device, err := agent.getDeviceReadOnly(ctx) |
| 151 | if err != nil { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 152 | return coreutils.DoneResponse(), err |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 153 | } |
Maninder | 2195ccc | 2021-06-23 20:23:01 +0530 | [diff] [blame] | 154 | |
| 155 | if !agent.proceedWithRequest(device) { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 156 | err = status.Errorf(codes.FailedPrecondition, "%s", "cannot complete operation as device deletion is in progress or reconciling is in progress/failed") |
| 157 | return coreutils.DoneResponse(), err |
Maninder | 2195ccc | 2021-06-23 20:23:01 +0530 | [diff] [blame] | 158 | } |
| 159 | |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 160 | dType, err := agent.adapterMgr.GetDeviceType(ctx, &voltha.ID{Id: device.Type}) |
| 161 | if err != nil { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 162 | return coreutils.DoneResponse(), err |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 163 | } |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 164 | |
| 165 | for _, group := range groupsToDel { |
khenaidoo | 7585a96 | 2021-06-10 16:15:38 -0400 | [diff] [blame] | 166 | if groupHandle, have := agent.groupCache.Lock(group.Desc.GroupId); have { |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 167 | // Update the store and cache |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 168 | if err = groupHandle.Delete(ctx); err != nil { |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 169 | groupHandle.Unlock() |
| 170 | return coreutils.DoneResponse(), err |
| 171 | } |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 172 | groupHandle.Unlock() |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | // Send update to adapters |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 177 | response := coreutils.NewResponse() |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 178 | subCtx := coreutils.WithSpanAndRPCMetadataFromContext(ctx) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 179 | if !dType.AcceptsAddRemoveFlowUpdates { |
Kent Hagerman | a7c9d79 | 2020-07-16 17:39:01 -0400 | [diff] [blame] | 180 | updatedAllGroups := agent.listDeviceGroups() |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 181 | ctr, groupSlice := 0, make([]*ofp.OfpGroupEntry, len(updatedAllGroups)) |
| 182 | for _, group := range updatedAllGroups { |
| 183 | groupSlice[ctr] = group |
| 184 | ctr++ |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 185 | } |
khenaidoo | 9beaaf1 | 2021-10-19 17:32:01 -0400 | [diff] [blame] | 186 | go agent.sendBulkFlows(subCtx, device, nil, &ofp.FlowGroups{Items: groupSlice}, flowMetadata, response) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 187 | } else { |
| 188 | flowChanges := &ofp.FlowChanges{ |
khenaidoo | 9beaaf1 | 2021-10-19 17:32:01 -0400 | [diff] [blame] | 189 | ToAdd: &ofp.Flows{Items: []*ofp.OfpFlowStats{}}, |
| 190 | ToRemove: &ofp.Flows{Items: []*ofp.OfpFlowStats{}}, |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 191 | } |
| 192 | groupChanges := &ofp.FlowGroupChanges{ |
khenaidoo | 9beaaf1 | 2021-10-19 17:32:01 -0400 | [diff] [blame] | 193 | ToAdd: &ofp.FlowGroups{Items: []*ofp.OfpGroupEntry{}}, |
| 194 | ToRemove: &ofp.FlowGroups{Items: groupsToDel}, |
| 195 | ToUpdate: &ofp.FlowGroups{Items: []*ofp.OfpGroupEntry{}}, |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 196 | } |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 197 | go agent.sendIncrementalFlows(subCtx, device, flowChanges, groupChanges, flowMetadata, response) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 198 | } |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 199 | operStatus.Code = common.OperationResp_OPERATION_IN_PROGRESS |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 200 | return response, nil |
| 201 | } |
| 202 | |
khenaidoo | 9beaaf1 | 2021-10-19 17:32:01 -0400 | [diff] [blame] | 203 | func (agent *Agent) updateGroupsToAdapter(ctx context.Context, updatedGroups []*ofp.OfpGroupEntry, flowMetadata *ofp.FlowMetadata) (coreutils.Response, error) { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 204 | 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] | 205 | |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 206 | var desc string |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 207 | var err error |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 208 | operStatus := &common.OperationResp{Code: common.OperationResp_OPERATION_FAILURE} |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 209 | defer func() { agent.logDeviceUpdate(ctx, nil, nil, operStatus, err, desc) }() |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 210 | |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 211 | if (len(updatedGroups)) == 0 { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 212 | desc = "no groups to update" |
| 213 | operStatus.Code = common.OperationResp_OPERATION_SUCCESS |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 214 | return coreutils.DoneResponse(), nil |
| 215 | } |
| 216 | |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 217 | device, err := agent.getDeviceReadOnly(ctx) |
| 218 | if err != nil { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 219 | return coreutils.DoneResponse(), err |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 220 | } |
Maninder | 2195ccc | 2021-06-23 20:23:01 +0530 | [diff] [blame] | 221 | |
| 222 | if !agent.proceedWithRequest(device) { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 223 | err = status.Errorf(codes.FailedPrecondition, "%s", "cannot complete operation as device deletion is in progress or reconciling is in progress/failed") |
| 224 | return coreutils.DoneResponse(), err |
Maninder | 2195ccc | 2021-06-23 20:23:01 +0530 | [diff] [blame] | 225 | } |
| 226 | |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 227 | if device.OperStatus != voltha.OperStatus_ACTIVE || device.ConnectStatus != voltha.ConnectStatus_REACHABLE || device.AdminState != voltha.AdminState_ENABLED { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 228 | err = status.Errorf(codes.FailedPrecondition, "invalid device states-oper-%s-connect-%s-admin-%s", device.OperStatus, device.ConnectStatus, device.AdminState) |
| 229 | return coreutils.DoneResponse(), err |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 230 | } |
Maninder | 2195ccc | 2021-06-23 20:23:01 +0530 | [diff] [blame] | 231 | |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 232 | dType, err := agent.adapterMgr.GetDeviceType(ctx, &voltha.ID{Id: device.Type}) |
| 233 | if err != nil { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 234 | err = status.Errorf(codes.FailedPrecondition, "non-existent-device-type-%s", device.Type) |
| 235 | return coreutils.DoneResponse(), err |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 236 | } |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 237 | |
Kent Hagerman | a7c9d79 | 2020-07-16 17:39:01 -0400 | [diff] [blame] | 238 | groupsToUpdate := make([]*ofp.OfpGroupEntry, 0) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 239 | for _, group := range updatedGroups { |
khenaidoo | 7585a96 | 2021-06-10 16:15:38 -0400 | [diff] [blame] | 240 | if groupHandle, have := agent.groupCache.Lock(group.Desc.GroupId); have { |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 241 | // Update the store and cache |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 242 | if err = groupHandle.Update(ctx, group); err != nil { |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 243 | groupHandle.Unlock() |
| 244 | return coreutils.DoneResponse(), err |
| 245 | } |
| 246 | groupsToUpdate = append(groupsToUpdate, group) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 247 | groupHandle.Unlock() |
| 248 | } |
| 249 | } |
| 250 | |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 251 | response := coreutils.NewResponse() |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 252 | subCtx := coreutils.WithSpanAndRPCMetadataFromContext(ctx) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 253 | // Process bulk flow update differently than incremental update |
| 254 | if !dType.AcceptsAddRemoveFlowUpdates { |
Kent Hagerman | a7c9d79 | 2020-07-16 17:39:01 -0400 | [diff] [blame] | 255 | updatedAllGroups := agent.listDeviceGroups() |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 256 | ctr, groupSlice := 0, make([]*ofp.OfpGroupEntry, len(updatedAllGroups)) |
| 257 | for _, group := range updatedAllGroups { |
| 258 | groupSlice[ctr] = group |
| 259 | ctr++ |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 260 | } |
khenaidoo | 9beaaf1 | 2021-10-19 17:32:01 -0400 | [diff] [blame] | 261 | go agent.sendBulkFlows(subCtx, device, nil, &ofp.FlowGroups{Items: groupSlice}, flowMetadata, response) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 262 | } else { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 263 | logger.Debugw(ctx, "updating-groups", |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 264 | log.Fields{ |
| 265 | "device-id": agent.deviceID, |
| 266 | "groups-to-update": groupsToUpdate, |
| 267 | }) |
| 268 | |
| 269 | // Sanity check |
| 270 | if (len(groupsToUpdate)) == 0 { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 271 | desc = "nothing to update" |
| 272 | operStatus.Code = common.OperationResp_OPERATION_SUCCESS |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 273 | return coreutils.DoneResponse(), nil |
| 274 | } |
| 275 | |
| 276 | flowChanges := &ofp.FlowChanges{ |
khenaidoo | 9beaaf1 | 2021-10-19 17:32:01 -0400 | [diff] [blame] | 277 | ToAdd: &ofp.Flows{Items: []*ofp.OfpFlowStats{}}, |
| 278 | ToRemove: &ofp.Flows{Items: []*ofp.OfpFlowStats{}}, |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 279 | } |
| 280 | groupChanges := &ofp.FlowGroupChanges{ |
khenaidoo | 9beaaf1 | 2021-10-19 17:32:01 -0400 | [diff] [blame] | 281 | ToAdd: &ofp.FlowGroups{Items: []*ofp.OfpGroupEntry{}}, |
| 282 | ToRemove: &ofp.FlowGroups{Items: []*ofp.OfpGroupEntry{}}, |
| 283 | ToUpdate: &ofp.FlowGroups{Items: groupsToUpdate}, |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 284 | } |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 285 | go agent.sendIncrementalFlows(subCtx, device, flowChanges, groupChanges, flowMetadata, response) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 286 | } |
| 287 | |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 288 | operStatus.Code = common.OperationResp_OPERATION_IN_PROGRESS |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 289 | return response, nil |
| 290 | } |