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