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