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" |
Maninder | dfadc98 | 2020-10-28 14:04:33 +0530 | [diff] [blame] | 26 | "github.com/opencord/voltha-lib-go/v4/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 { |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 36 | groupIDs := agent.groupLoader.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 { |
| 39 | if groupHandle, have := agent.groupLoader.Lock(groupID); have { |
| 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 | } |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 64 | dType, err := agent.adapterMgr.GetDeviceType(ctx, &voltha.ID{Id: device.Type}) |
| 65 | if err != nil { |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 66 | desc = fmt.Sprintf("non-existent-device-type-%s", device.Type) |
| 67 | agent.logDeviceUpdate(ctx, "addGroupsToAdapter", nil, nil, operStatus, &desc) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 68 | return coreutils.DoneResponse(), status.Errorf(codes.FailedPrecondition, "non-existent-device-type-%s", device.Type) |
| 69 | } |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 70 | |
| 71 | groupsToAdd := make([]*ofp.OfpGroupEntry, 0) |
| 72 | groupsToDelete := make([]*ofp.OfpGroupEntry, 0) |
| 73 | for _, group := range newGroups { |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 74 | groupHandle, created, err := agent.groupLoader.LockOrCreate(ctx, group) |
| 75 | if err != nil { |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 76 | desc = err.Error() |
| 77 | agent.logDeviceUpdate(ctx, "addGroupsToAdapter", nil, nil, operStatus, &desc) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 78 | return coreutils.DoneResponse(), err |
| 79 | } |
| 80 | |
| 81 | if created { |
| 82 | groupsToAdd = append(groupsToAdd, group) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 83 | } else { |
| 84 | groupToChange := groupHandle.GetReadOnly() |
| 85 | if !proto.Equal(groupToChange, group) { |
| 86 | //Group needs to be updated. |
| 87 | if err := groupHandle.Update(ctx, group); err != nil { |
| 88 | groupHandle.Unlock() |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 89 | desc = fmt.Sprintf("failure-updating-group-%s-to-device-%s", strconv.Itoa(int(group.Desc.GroupId)), agent.deviceID) |
| 90 | agent.logDeviceUpdate(ctx, "addGroupsToAdapter", nil, nil, operStatus, &desc) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 91 | return coreutils.DoneResponse(), status.Errorf(codes.Internal, "failure-updating-group-%s-to-device-%s", strconv.Itoa(int(group.Desc.GroupId)), agent.deviceID) |
| 92 | } |
| 93 | groupsToDelete = append(groupsToDelete, groupToChange) |
| 94 | groupsToAdd = append(groupsToAdd, group) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 95 | } else { |
| 96 | //No need to change the group. It is already exist. |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 97 | 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] | 98 | } |
| 99 | } |
| 100 | |
| 101 | groupHandle.Unlock() |
| 102 | } |
| 103 | // Sanity check |
| 104 | if (len(groupsToAdd)) == 0 { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 105 | 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] | 106 | return coreutils.DoneResponse(), nil |
| 107 | } |
| 108 | |
| 109 | // Send update to adapters |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 110 | subCtx, cancel := context.WithTimeout(log.WithSpanFromContext(context.Background(), ctx), agent.defaultTimeout) |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 111 | subCtx = coreutils.WithRPCMetadataFromContext(subCtx, ctx) |
| 112 | |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 113 | response := coreutils.NewResponse() |
| 114 | if !dType.AcceptsAddRemoveFlowUpdates { |
Kent Hagerman | a7c9d79 | 2020-07-16 17:39:01 -0400 | [diff] [blame] | 115 | updatedAllGroups := agent.listDeviceGroups() |
| 116 | rpcResponse, err := agent.adapterProxy.UpdateFlowsBulk(subCtx, device, nil, updatedAllGroups, flowMetadata) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 117 | if err != nil { |
| 118 | cancel() |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 119 | desc = err.Error() |
| 120 | agent.logDeviceUpdate(ctx, "addGroupsToAdapter", nil, nil, operStatus, &desc) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 121 | return coreutils.DoneResponse(), err |
| 122 | } |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 123 | go agent.waitForAdapterFlowResponse(subCtx, cancel, "addGroupsToAdapter", rpcResponse, response) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 124 | } else { |
| 125 | flowChanges := &ofp.FlowChanges{ |
| 126 | ToAdd: &voltha.Flows{Items: []*ofp.OfpFlowStats{}}, |
| 127 | ToRemove: &voltha.Flows{Items: []*ofp.OfpFlowStats{}}, |
| 128 | } |
| 129 | groupChanges := &ofp.FlowGroupChanges{ |
| 130 | ToAdd: &voltha.FlowGroups{Items: groupsToAdd}, |
| 131 | ToRemove: &voltha.FlowGroups{Items: groupsToDelete}, |
| 132 | ToUpdate: &voltha.FlowGroups{Items: []*ofp.OfpGroupEntry{}}, |
| 133 | } |
| 134 | rpcResponse, err := agent.adapterProxy.UpdateFlowsIncremental(subCtx, device, flowChanges, groupChanges, flowMetadata) |
| 135 | if err != nil { |
| 136 | cancel() |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 137 | desc = err.Error() |
| 138 | agent.logDeviceUpdate(ctx, "addGroupsToAdapter", nil, nil, operStatus, &desc) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 139 | return coreutils.DoneResponse(), err |
| 140 | } |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 141 | go agent.waitForAdapterFlowResponse(subCtx, cancel, "addGroupsToAdapter", rpcResponse, response) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 142 | } |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 143 | operStatus.Code = common.OperationResp_OPERATION_IN_PROGRESS |
| 144 | agent.logDeviceUpdate(ctx, "addGroupsToAdapter", nil, nil, operStatus, &desc) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 145 | return response, nil |
| 146 | } |
| 147 | |
| 148 | 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] | 149 | 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] | 150 | |
| 151 | if (len(groupsToDel)) == 0 { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 152 | logger.Debugw(ctx, "nothing-to-delete", log.Fields{"device-id": agent.deviceID}) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 153 | return coreutils.DoneResponse(), nil |
| 154 | } |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 155 | |
| 156 | var desc string |
| 157 | operStatus := &common.OperationResp{Code: common.OperationResp_OPERATION_FAILURE} |
| 158 | |
| 159 | defer agent.logDeviceUpdate(ctx, "deleteGroupsFromAdapter", nil, nil, operStatus, &desc) |
| 160 | |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 161 | device, err := agent.getDeviceReadOnly(ctx) |
| 162 | if err != nil { |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 163 | desc = err.Error() |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 164 | return coreutils.DoneResponse(), status.Errorf(codes.Aborted, "%s", err) |
| 165 | } |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 166 | dType, err := agent.adapterMgr.GetDeviceType(ctx, &voltha.ID{Id: device.Type}) |
| 167 | if err != nil { |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 168 | desc = fmt.Sprintf("non-existent-device-type-%s", device.Type) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 169 | return coreutils.DoneResponse(), status.Errorf(codes.FailedPrecondition, "non-existent-device-type-%s", device.Type) |
| 170 | } |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 171 | |
| 172 | for _, group := range groupsToDel { |
| 173 | if groupHandle, have := agent.groupLoader.Lock(group.Desc.GroupId); have { |
| 174 | // Update the store and cache |
| 175 | if err := groupHandle.Delete(ctx); err != nil { |
| 176 | groupHandle.Unlock() |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 177 | desc = err.Error() |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 178 | return coreutils.DoneResponse(), err |
| 179 | } |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 180 | groupHandle.Unlock() |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | // Send update to adapters |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 185 | subCtx, cancel := context.WithTimeout(log.WithSpanFromContext(context.Background(), ctx), agent.defaultTimeout) |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 186 | subCtx = coreutils.WithRPCMetadataFromContext(subCtx, ctx) |
| 187 | |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 188 | response := coreutils.NewResponse() |
| 189 | if !dType.AcceptsAddRemoveFlowUpdates { |
Kent Hagerman | a7c9d79 | 2020-07-16 17:39:01 -0400 | [diff] [blame] | 190 | updatedAllGroups := agent.listDeviceGroups() |
| 191 | rpcResponse, err := agent.adapterProxy.UpdateFlowsBulk(subCtx, device, nil, updatedAllGroups, flowMetadata) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 192 | if err != nil { |
| 193 | cancel() |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 194 | desc = err.Error() |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 195 | return coreutils.DoneResponse(), err |
| 196 | } |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 197 | go agent.waitForAdapterFlowResponse(subCtx, cancel, "deleteGroupsFromAdapter", rpcResponse, response) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 198 | } else { |
| 199 | flowChanges := &ofp.FlowChanges{ |
| 200 | ToAdd: &voltha.Flows{Items: []*ofp.OfpFlowStats{}}, |
| 201 | ToRemove: &voltha.Flows{Items: []*ofp.OfpFlowStats{}}, |
| 202 | } |
| 203 | groupChanges := &ofp.FlowGroupChanges{ |
| 204 | ToAdd: &voltha.FlowGroups{Items: []*ofp.OfpGroupEntry{}}, |
| 205 | ToRemove: &voltha.FlowGroups{Items: groupsToDel}, |
| 206 | ToUpdate: &voltha.FlowGroups{Items: []*ofp.OfpGroupEntry{}}, |
| 207 | } |
| 208 | rpcResponse, err := agent.adapterProxy.UpdateFlowsIncremental(subCtx, device, flowChanges, groupChanges, flowMetadata) |
| 209 | if err != nil { |
| 210 | cancel() |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 211 | desc = err.Error() |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 212 | return coreutils.DoneResponse(), err |
| 213 | } |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 214 | go agent.waitForAdapterFlowResponse(subCtx, cancel, "deleteGroupsFromAdapter", rpcResponse, response) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 215 | } |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 216 | operStatus.Code = common.OperationResp_OPERATION_IN_PROGRESS |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 217 | return response, nil |
| 218 | } |
| 219 | |
| 220 | 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] | 221 | 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] | 222 | |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 223 | var desc string |
| 224 | operStatus := &common.OperationResp{Code: common.OperationResp_OPERATION_FAILURE} |
| 225 | |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 226 | if (len(updatedGroups)) == 0 { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 227 | 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] | 228 | return coreutils.DoneResponse(), nil |
| 229 | } |
| 230 | |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 231 | device, err := agent.getDeviceReadOnly(ctx) |
| 232 | if err != nil { |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 233 | desc = err.Error() |
| 234 | agent.logDeviceUpdate(ctx, "updateGroupsToAdapter", nil, nil, operStatus, &desc) |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 235 | return coreutils.DoneResponse(), status.Errorf(codes.Aborted, "%s", err) |
| 236 | } |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 237 | 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] | 238 | desc = fmt.Sprintf("invalid device states-oper-%s-connect-%s-admin-%s", device.OperStatus, device.ConnectStatus, device.AdminState) |
| 239 | agent.logDeviceUpdate(ctx, "updateGroupsToAdapter", nil, nil, operStatus, &desc) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 240 | return coreutils.DoneResponse(), status.Errorf(codes.FailedPrecondition, "invalid device states-oper-%s-connect-%s-admin-%s", device.OperStatus, device.ConnectStatus, device.AdminState) |
| 241 | } |
| 242 | dType, err := agent.adapterMgr.GetDeviceType(ctx, &voltha.ID{Id: device.Type}) |
| 243 | if err != nil { |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 244 | desc = fmt.Sprintf("non-existent-device-type-%s", device.Type) |
| 245 | agent.logDeviceUpdate(ctx, "updateGroupsToAdapter", nil, nil, operStatus, &desc) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 246 | return coreutils.DoneResponse(), status.Errorf(codes.FailedPrecondition, "non-existent-device-type-%s", device.Type) |
| 247 | } |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 248 | |
Kent Hagerman | a7c9d79 | 2020-07-16 17:39:01 -0400 | [diff] [blame] | 249 | groupsToUpdate := make([]*ofp.OfpGroupEntry, 0) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 250 | for _, group := range updatedGroups { |
| 251 | if groupHandle, have := agent.groupLoader.Lock(group.Desc.GroupId); have { |
| 252 | // Update the store and cache |
| 253 | if err := groupHandle.Update(ctx, group); err != nil { |
| 254 | groupHandle.Unlock() |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 255 | desc = err.Error() |
| 256 | agent.logDeviceUpdate(ctx, "updateGroupsToAdapter", nil, nil, operStatus, &desc) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 257 | return coreutils.DoneResponse(), err |
| 258 | } |
| 259 | groupsToUpdate = append(groupsToUpdate, group) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 260 | groupHandle.Unlock() |
| 261 | } |
| 262 | } |
| 263 | |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 264 | subCtx, cancel := context.WithTimeout(log.WithSpanFromContext(context.Background(), ctx), agent.defaultTimeout) |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 265 | subCtx = coreutils.WithRPCMetadataFromContext(subCtx, ctx) |
| 266 | |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 267 | response := coreutils.NewResponse() |
| 268 | // Process bulk flow update differently than incremental update |
| 269 | if !dType.AcceptsAddRemoveFlowUpdates { |
Kent Hagerman | a7c9d79 | 2020-07-16 17:39:01 -0400 | [diff] [blame] | 270 | updatedAllGroups := agent.listDeviceGroups() |
| 271 | rpcResponse, err := agent.adapterProxy.UpdateFlowsBulk(subCtx, device, nil, updatedAllGroups, nil) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 272 | if err != nil { |
| 273 | cancel() |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 274 | desc = err.Error() |
| 275 | agent.logDeviceUpdate(ctx, "updateGroupsToAdapter", nil, nil, operStatus, &desc) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 276 | return coreutils.DoneResponse(), err |
| 277 | } |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 278 | go agent.waitForAdapterFlowResponse(subCtx, cancel, "updateGroupsToAdapter", rpcResponse, response) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 279 | } else { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 280 | logger.Debugw(ctx, "updating-groups", |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 281 | log.Fields{ |
| 282 | "device-id": agent.deviceID, |
| 283 | "groups-to-update": groupsToUpdate, |
| 284 | }) |
| 285 | |
| 286 | // Sanity check |
| 287 | if (len(groupsToUpdate)) == 0 { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 288 | 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] | 289 | cancel() |
| 290 | return coreutils.DoneResponse(), nil |
| 291 | } |
| 292 | |
| 293 | flowChanges := &ofp.FlowChanges{ |
| 294 | ToAdd: &voltha.Flows{Items: []*ofp.OfpFlowStats{}}, |
| 295 | ToRemove: &voltha.Flows{Items: []*ofp.OfpFlowStats{}}, |
| 296 | } |
| 297 | groupChanges := &ofp.FlowGroupChanges{ |
| 298 | ToAdd: &voltha.FlowGroups{Items: []*ofp.OfpGroupEntry{}}, |
| 299 | ToRemove: &voltha.FlowGroups{Items: []*ofp.OfpGroupEntry{}}, |
| 300 | ToUpdate: &voltha.FlowGroups{Items: groupsToUpdate}, |
| 301 | } |
| 302 | rpcResponse, err := agent.adapterProxy.UpdateFlowsIncremental(subCtx, device, flowChanges, groupChanges, flowMetadata) |
| 303 | if err != nil { |
| 304 | cancel() |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 305 | desc = err.Error() |
| 306 | agent.logDeviceUpdate(ctx, "updateGroupsToAdapter", nil, nil, operStatus, &desc) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 307 | return coreutils.DoneResponse(), err |
| 308 | } |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 309 | go agent.waitForAdapterFlowResponse(subCtx, cancel, "updateGroupsToAdapter", rpcResponse, response) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 310 | } |
| 311 | |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 312 | operStatus.Code = common.OperationResp_OPERATION_IN_PROGRESS |
| 313 | agent.logDeviceUpdate(ctx, "updateGroupsToAdapter", nil, nil, operStatus, &desc) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 314 | return response, nil |
| 315 | } |