Jonathan Hart | 60c5d77 | 2020-03-30 18:28:40 -0700 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2020 the original author or authors. |
| 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 openflow |
| 18 | |
| 19 | import ( |
| 20 | "context" |
Andrea Campanella | 22aa3ed | 2021-03-04 15:58:55 +0100 | [diff] [blame^] | 21 | "fmt" |
Jonathan Hart | 828908c | 2020-04-15 14:23:45 -0700 | [diff] [blame] | 22 | "github.com/opencord/goloxi" |
| 23 | ofp "github.com/opencord/goloxi/of13" |
Maninder | 12b909f | 2020-10-23 14:23:36 +0530 | [diff] [blame] | 24 | "github.com/opencord/voltha-lib-go/v4/pkg/log" |
| 25 | "github.com/opencord/voltha-protos/v4/go/openflow_13" |
| 26 | "github.com/opencord/voltha-protos/v4/go/voltha" |
Jonathan Hart | 60c5d77 | 2020-03-30 18:28:40 -0700 | [diff] [blame] | 27 | ) |
| 28 | |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 29 | func (ofc *OFConnection) handleGroupMod(ctx context.Context, groupMod ofp.IGroupMod) { |
Girish Kumar | 01e0c63 | 2020-08-10 16:48:56 +0000 | [diff] [blame] | 30 | span, ctx := log.CreateChildSpan(ctx, "openflow-group-modification") |
| 31 | defer span.Finish() |
Jonathan Hart | 60c5d77 | 2020-03-30 18:28:40 -0700 | [diff] [blame] | 32 | |
| 33 | volthaClient := ofc.VolthaClient.Get() |
| 34 | if volthaClient == nil { |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 35 | logger.Errorw(ctx, "no-voltha-connection", |
Jonathan Hart | 60c5d77 | 2020-03-30 18:28:40 -0700 | [diff] [blame] | 36 | log.Fields{"device-id": ofc.DeviceID}) |
| 37 | return |
| 38 | } |
| 39 | |
| 40 | groupUpdate := &openflow_13.FlowGroupTableUpdate{ |
| 41 | Id: ofc.DeviceID, |
| 42 | GroupMod: &voltha.OfpGroupMod{ |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 43 | Command: openflowGroupModCommandToVoltha(ctx, groupMod.GetCommand()), |
| 44 | Type: openflowGroupTypeToVoltha(ctx, groupMod.GetGroupType()), |
Jonathan Hart | 60c5d77 | 2020-03-30 18:28:40 -0700 | [diff] [blame] | 45 | GroupId: groupMod.GetGroupId(), |
| 46 | Buckets: openflowBucketsToVoltha(groupMod.GetBuckets()), |
| 47 | }, |
| 48 | } |
| 49 | |
Girish Kumar | 01e0c63 | 2020-08-10 16:48:56 +0000 | [diff] [blame] | 50 | _, err := volthaClient.UpdateLogicalDeviceFlowGroupTable(log.WithSpanFromContext(context.Background(), ctx), groupUpdate) |
Jonathan Hart | 60c5d77 | 2020-03-30 18:28:40 -0700 | [diff] [blame] | 51 | if err != nil { |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 52 | logger.Errorw(ctx, "Error updating group table", |
Jonathan Hart | 60c5d77 | 2020-03-30 18:28:40 -0700 | [diff] [blame] | 53 | log.Fields{"device-id": ofc.DeviceID, "error": err}) |
| 54 | } |
| 55 | |
| 56 | } |
| 57 | |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 58 | func openflowGroupModCommandToVoltha(ctx context.Context, command ofp.GroupModCommand) openflow_13.OfpGroupModCommand { |
Jonathan Hart | 60c5d77 | 2020-03-30 18:28:40 -0700 | [diff] [blame] | 59 | switch command { |
| 60 | case ofp.OFPGCAdd: |
| 61 | return openflow_13.OfpGroupModCommand_OFPGC_ADD |
| 62 | case ofp.OFPGCModify: |
| 63 | return openflow_13.OfpGroupModCommand_OFPGC_MODIFY |
| 64 | case ofp.OFPGCDelete: |
| 65 | return openflow_13.OfpGroupModCommand_OFPGC_DELETE |
| 66 | } |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 67 | logger.Errorw(ctx, "Unknown group mod command", log.Fields{"command": command}) |
Jonathan Hart | 60c5d77 | 2020-03-30 18:28:40 -0700 | [diff] [blame] | 68 | return 0 |
| 69 | } |
| 70 | |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 71 | func openflowGroupTypeToVoltha(ctx context.Context, t ofp.GroupType) openflow_13.OfpGroupType { |
Jonathan Hart | 60c5d77 | 2020-03-30 18:28:40 -0700 | [diff] [blame] | 72 | switch t { |
| 73 | case ofp.OFPGTAll: |
| 74 | return openflow_13.OfpGroupType_OFPGT_ALL |
| 75 | case ofp.OFPGTSelect: |
| 76 | return openflow_13.OfpGroupType_OFPGT_SELECT |
| 77 | case ofp.OFPGTIndirect: |
| 78 | return openflow_13.OfpGroupType_OFPGT_INDIRECT |
| 79 | case ofp.OFPGTFf: |
| 80 | return openflow_13.OfpGroupType_OFPGT_FF |
| 81 | } |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 82 | logger.Errorw(ctx, "Unknown openflow group type", log.Fields{"type": t}) |
Jonathan Hart | 60c5d77 | 2020-03-30 18:28:40 -0700 | [diff] [blame] | 83 | return 0 |
| 84 | } |
| 85 | |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 86 | func volthaGroupTypeToOpenflow(ctx context.Context, t openflow_13.OfpGroupType) ofp.GroupType { |
Jonathan Hart | 60c5d77 | 2020-03-30 18:28:40 -0700 | [diff] [blame] | 87 | switch t { |
| 88 | case openflow_13.OfpGroupType_OFPGT_ALL: |
| 89 | return ofp.OFPGTAll |
| 90 | case openflow_13.OfpGroupType_OFPGT_SELECT: |
| 91 | return ofp.OFPGTSelect |
| 92 | case openflow_13.OfpGroupType_OFPGT_INDIRECT: |
| 93 | return ofp.OFPGTIndirect |
| 94 | case openflow_13.OfpGroupType_OFPGT_FF: |
| 95 | return ofp.OFPGTFf |
| 96 | } |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 97 | logger.Errorw(ctx, "Unknown voltha group type", log.Fields{"type": t}) |
Jonathan Hart | 60c5d77 | 2020-03-30 18:28:40 -0700 | [diff] [blame] | 98 | return 0 |
| 99 | } |
| 100 | |
| 101 | func openflowBucketsToVoltha(buckets []*ofp.Bucket) []*openflow_13.OfpBucket { |
| 102 | outBuckets := make([]*openflow_13.OfpBucket, len(buckets)) |
| 103 | |
| 104 | for i, bucket := range buckets { |
| 105 | b := &openflow_13.OfpBucket{ |
| 106 | Weight: uint32(bucket.Weight), |
| 107 | WatchPort: uint32(bucket.WatchPort), |
| 108 | WatchGroup: bucket.WatchGroup, |
| 109 | Actions: openflowActionsToVoltha(bucket.Actions), |
| 110 | } |
| 111 | outBuckets[i] = b |
| 112 | } |
| 113 | |
| 114 | return outBuckets |
| 115 | } |
| 116 | |
| 117 | func openflowActionsToVoltha(actions []goloxi.IAction) []*openflow_13.OfpAction { |
| 118 | outActions := make([]*openflow_13.OfpAction, len(actions)) |
| 119 | |
| 120 | for i, action := range actions { |
| 121 | outActions[i] = extractAction(action) |
| 122 | } |
| 123 | |
| 124 | return outActions |
| 125 | } |
| 126 | |
Andrea Campanella | 22aa3ed | 2021-03-04 15:58:55 +0100 | [diff] [blame^] | 127 | func volthaBucketsToOpenflow(ctx context.Context, buckets []*openflow_13.OfpBucket) ([]*ofp.Bucket, error) { |
Jonathan Hart | 60c5d77 | 2020-03-30 18:28:40 -0700 | [diff] [blame] | 128 | outBuckets := make([]*ofp.Bucket, len(buckets)) |
| 129 | |
| 130 | for i, bucket := range buckets { |
Andrea Campanella | 22aa3ed | 2021-03-04 15:58:55 +0100 | [diff] [blame^] | 131 | actions, err := volthaActionsToOpenflow(ctx, bucket.Actions) |
| 132 | if err != nil { |
| 133 | return nil, err |
| 134 | } |
Jonathan Hart | 60c5d77 | 2020-03-30 18:28:40 -0700 | [diff] [blame] | 135 | b := &ofp.Bucket{ |
Jonathan Hart | 60c5d77 | 2020-03-30 18:28:40 -0700 | [diff] [blame] | 136 | Weight: uint16(bucket.Weight), |
| 137 | WatchPort: ofp.Port(bucket.WatchPort), |
| 138 | WatchGroup: bucket.WatchGroup, |
| 139 | Actions: actions, |
| 140 | } |
| 141 | outBuckets[i] = b |
| 142 | } |
| 143 | |
Andrea Campanella | 22aa3ed | 2021-03-04 15:58:55 +0100 | [diff] [blame^] | 144 | return outBuckets, nil |
Jonathan Hart | 60c5d77 | 2020-03-30 18:28:40 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Andrea Campanella | 22aa3ed | 2021-03-04 15:58:55 +0100 | [diff] [blame^] | 147 | func volthaActionsToOpenflow(ctx context.Context, actions []*openflow_13.OfpAction) ([]goloxi.IAction, error) { |
Jonathan Hart | 60c5d77 | 2020-03-30 18:28:40 -0700 | [diff] [blame] | 148 | outActions := make([]goloxi.IAction, len(actions)) |
| 149 | |
Jonathan Hart | 60c5d77 | 2020-03-30 18:28:40 -0700 | [diff] [blame] | 150 | for i, action := range actions { |
Andrea Campanella | 22aa3ed | 2021-03-04 15:58:55 +0100 | [diff] [blame^] | 151 | outAction, err := parseAction(ctx, action) |
| 152 | if err == nil { |
| 153 | outActions[i] = outAction |
| 154 | } else { |
| 155 | return nil, fmt.Errorf("can't-parse-action %v", err) |
| 156 | } |
| 157 | |
Jonathan Hart | 60c5d77 | 2020-03-30 18:28:40 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Andrea Campanella | 22aa3ed | 2021-03-04 15:58:55 +0100 | [diff] [blame^] | 160 | return outActions, nil |
Jonathan Hart | 60c5d77 | 2020-03-30 18:28:40 -0700 | [diff] [blame] | 161 | } |