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 | |
| 28 | func (ofc *OFConnection) handleGroupMod(groupMod ofp.IGroupMod) { |
| 29 | |
| 30 | volthaClient := ofc.VolthaClient.Get() |
| 31 | if volthaClient == nil { |
| 32 | logger.Errorw("no-voltha-connection", |
| 33 | log.Fields{"device-id": ofc.DeviceID}) |
| 34 | return |
| 35 | } |
| 36 | |
| 37 | groupUpdate := &openflow_13.FlowGroupTableUpdate{ |
| 38 | Id: ofc.DeviceID, |
| 39 | GroupMod: &voltha.OfpGroupMod{ |
| 40 | Command: openflowGroupModCommandToVoltha(groupMod.GetCommand()), |
| 41 | Type: openflowGroupTypeToVoltha(groupMod.GetGroupType()), |
| 42 | GroupId: groupMod.GetGroupId(), |
| 43 | Buckets: openflowBucketsToVoltha(groupMod.GetBuckets()), |
| 44 | }, |
| 45 | } |
| 46 | |
| 47 | _, err := volthaClient.UpdateLogicalDeviceFlowGroupTable(context.Background(), groupUpdate) |
| 48 | if err != nil { |
| 49 | logger.Errorw("Error updating group table", |
| 50 | log.Fields{"device-id": ofc.DeviceID, "error": err}) |
| 51 | } |
| 52 | |
| 53 | } |
| 54 | |
| 55 | func openflowGroupModCommandToVoltha(command ofp.GroupModCommand) openflow_13.OfpGroupModCommand { |
| 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 | } |
| 64 | logger.Errorw("Unknown group mod command", log.Fields{"command": command}) |
| 65 | return 0 |
| 66 | } |
| 67 | |
| 68 | func openflowGroupTypeToVoltha(t ofp.GroupType) openflow_13.OfpGroupType { |
| 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 | } |
| 79 | logger.Errorw("Unknown openflow group type", log.Fields{"type": t}) |
| 80 | return 0 |
| 81 | } |
| 82 | |
| 83 | func volthaGroupTypeToOpenflow(t openflow_13.OfpGroupType) ofp.GroupType { |
| 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 | } |
| 94 | logger.Errorw("Unknown voltha group type", log.Fields{"type": t}) |
| 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 | |
| 124 | func volthaBucketsToOpenflow(buckets []*openflow_13.OfpBucket) []*ofp.Bucket { |
| 125 | outBuckets := make([]*ofp.Bucket, len(buckets)) |
| 126 | |
| 127 | for i, bucket := range buckets { |
Jonathan Hart | 828908c | 2020-04-15 14:23:45 -0700 | [diff] [blame] | 128 | actions := volthaActionsToOpenflow(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 | |
Jonathan Hart | 828908c | 2020-04-15 14:23:45 -0700 | [diff] [blame] | 141 | func volthaActionsToOpenflow(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 { |
Jonathan Hart | 828908c | 2020-04-15 14:23:45 -0700 | [diff] [blame] | 145 | outActions[i] = parseAction(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 | } |