Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 1 | /* |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 2 | Copyright 2020 the original author or authors. |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 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 | package openflow |
| 17 | |
| 18 | import ( |
| 19 | "encoding/json" |
David Bainbridge | f8ce7d2 | 2020-04-08 12:49:41 -0700 | [diff] [blame] | 20 | |
Jonathan Hart | 828908c | 2020-04-15 14:23:45 -0700 | [diff] [blame] | 21 | ofp "github.com/opencord/goloxi/of13" |
David K. Bainbridge | aea73cd | 2020-01-27 10:44:50 -0800 | [diff] [blame] | 22 | "github.com/opencord/voltha-lib-go/v3/pkg/log" |
| 23 | "github.com/opencord/voltha-protos/v3/go/openflow_13" |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 24 | "golang.org/x/net/context" |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 25 | ) |
| 26 | |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 27 | func (ofc *OFConnection) handleMeterModRequest(request *ofp.MeterMod) { |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 28 | if logger.V(log.DebugLevel) { |
Don Newton | 7577f07 | 2020-01-06 12:41:11 -0500 | [diff] [blame] | 29 | js, _ := json.Marshal(request) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 30 | logger.Debugw("handleMeterModRequest called", |
| 31 | log.Fields{ |
| 32 | "device-id": ofc.DeviceID, |
| 33 | "request": js}) |
Don Newton | 7577f07 | 2020-01-06 12:41:11 -0500 | [diff] [blame] | 34 | } |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 35 | |
David Bainbridge | f8ce7d2 | 2020-04-08 12:49:41 -0700 | [diff] [blame] | 36 | volthaClient := ofc.VolthaClient.Get() |
| 37 | if volthaClient == nil { |
David K. Bainbridge | 9cb404e | 2020-01-28 14:32:29 -0800 | [diff] [blame] | 38 | logger.Errorw("no-voltha-connection", |
| 39 | log.Fields{"device-id": ofc.DeviceID}) |
| 40 | return |
| 41 | } |
| 42 | |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 43 | meterModUpdate := openflow_13.MeterModUpdate{Id: ofc.DeviceID} |
| 44 | meterMod := openflow_13.OfpMeterMod{ |
| 45 | MeterId: request.MeterId, |
| 46 | Flags: uint32(request.Flags), |
| 47 | Command: openflow_13.OfpMeterModCommand(request.Command), |
| 48 | } |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 49 | var bands []*openflow_13.OfpMeterBandHeader |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 50 | for _, ofpBand := range request.GetMeters() { |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 51 | var band openflow_13.OfpMeterBandHeader |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 52 | switch ofpBand.GetType() { |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 53 | case ofp.OFPMBTDrop: |
| 54 | ofpDrop := ofpBand.(ofp.IMeterBandDrop) |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 55 | band.Type = openflow_13.OfpMeterBandType_OFPMBT_DROP |
| 56 | band.Rate = ofpDrop.GetRate() |
Don Newton | b437c6f | 2019-12-18 11:51:57 -0500 | [diff] [blame] | 57 | band.BurstSize = ofpDrop.GetBurstSize() |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 58 | case ofp.OFPMBTDSCPRemark: |
| 59 | ofpDscpRemark := ofpBand.(ofp.IMeterBandDscpRemark) |
| 60 | var dscpRemark openflow_13.OfpMeterBandDscpRemark |
| 61 | band.Type = openflow_13.OfpMeterBandType_OFPMBT_DSCP_REMARK |
| 62 | band.BurstSize = ofpDscpRemark.GetBurstSize() |
| 63 | band.Rate = ofpDscpRemark.GetRate() |
| 64 | dscpRemark.PrecLevel = uint32(ofpDscpRemark.GetPrecLevel()) |
Don Newton | b437c6f | 2019-12-18 11:51:57 -0500 | [diff] [blame] | 65 | /* |
| 66 | var meterBandHeaderDscp openflow_13.OfpMeterBandHeader_DscpRemark |
| 67 | meterBandHeaderDscp.DscpRemark = &dscpRemark |
| 68 | band.Data = &meterBandHeaderDscp |
| 69 | |
| 70 | */ |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 71 | case ofp.OFPMBTExperimenter: |
| 72 | ofpExperimenter := ofpBand.(ofp.IMeterBandExperimenter) |
| 73 | var experimenter openflow_13.OfpMeterBandExperimenter |
| 74 | experimenter.Experimenter = ofpExperimenter.GetExperimenter() |
| 75 | band.Type = openflow_13.OfpMeterBandType_OFPMBT_EXPERIMENTER |
| 76 | band.BurstSize = ofpExperimenter.GetBurstSize() |
| 77 | band.Rate = ofpExperimenter.GetRate() |
Don Newton | b437c6f | 2019-12-18 11:51:57 -0500 | [diff] [blame] | 78 | /* |
| 79 | var meterBandHeaderExperimenter openflow_13.OfpMeterBandHeader_Experimenter |
| 80 | meterBandHeaderExperimenter.Experimenter = &experimenter |
| 81 | band.Data = &meterBandHeaderExperimenter |
| 82 | |
| 83 | */ |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 84 | } |
| 85 | bands = append(bands, &band) |
| 86 | } |
| 87 | meterMod.Bands = bands |
| 88 | meterModUpdate.MeterMod = &meterMod |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 89 | if logger.V(log.DebugLevel) { |
Don Newton | 7577f07 | 2020-01-06 12:41:11 -0500 | [diff] [blame] | 90 | meterModJS, _ := json.Marshal(meterModUpdate) |
| 91 | logger.Debugw("handleMeterModUpdate sending request", |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 92 | log.Fields{ |
| 93 | "device-id": ofc.DeviceID, |
| 94 | "meter-mod-request": meterModJS}) |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 95 | } |
David Bainbridge | f8ce7d2 | 2020-04-08 12:49:41 -0700 | [diff] [blame] | 96 | if _, err := volthaClient.UpdateLogicalDeviceMeterTable(context.Background(), &meterModUpdate); err != nil { |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 97 | logger.Errorw("Error calling UpdateLogicalDeviceMeterTable", |
| 98 | log.Fields{ |
| 99 | "device-id": ofc.DeviceID, |
| 100 | "error": err}) |
Don Newton | 7577f07 | 2020-01-06 12:41:11 -0500 | [diff] [blame] | 101 | } |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 102 | } |