alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [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 org.onosproject.provider.of.meter.impl; |
| 17 | |
alshabib | 10c810b | 2015-08-18 16:59:04 -0700 | [diff] [blame] | 18 | import org.onosproject.net.meter.Band; |
| 19 | import org.onosproject.net.meter.Meter; |
| 20 | import org.onosproject.net.meter.MeterId; |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 21 | import org.projectfloodlight.openflow.protocol.OFFactory; |
| 22 | import org.projectfloodlight.openflow.protocol.OFMeterFlags; |
| 23 | import org.projectfloodlight.openflow.protocol.OFMeterMod; |
| 24 | import org.projectfloodlight.openflow.protocol.OFMeterModCommand; |
cansu.toprak | 409289d | 2017-10-27 10:04:05 +0300 | [diff] [blame^] | 25 | import org.projectfloodlight.openflow.protocol.OFVersion; |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 26 | import org.projectfloodlight.openflow.protocol.meterband.OFMeterBand; |
| 27 | import org.projectfloodlight.openflow.protocol.meterband.OFMeterBandDrop; |
| 28 | import org.projectfloodlight.openflow.protocol.meterband.OFMeterBandDscpRemark; |
| 29 | import org.slf4j.Logger; |
| 30 | |
| 31 | import java.util.Collection; |
Jimmy Jin | e9b7a02 | 2016-08-12 16:56:48 -0700 | [diff] [blame] | 32 | import java.util.EnumSet; |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 33 | import java.util.List; |
Sho SHIMIZU | 4590604 | 2016-01-13 23:05:54 -0800 | [diff] [blame] | 34 | import java.util.Objects; |
Jimmy Jin | e9b7a02 | 2016-08-12 16:56:48 -0700 | [diff] [blame] | 35 | import java.util.Set; |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 36 | import java.util.stream.Collectors; |
| 37 | |
| 38 | import static com.google.common.base.Preconditions.checkArgument; |
| 39 | import static com.google.common.base.Preconditions.checkNotNull; |
| 40 | import static org.slf4j.LoggerFactory.getLogger; |
| 41 | |
| 42 | /** |
| 43 | * Builder for a meter modification. |
| 44 | */ |
| 45 | public final class MeterModBuilder { |
| 46 | |
| 47 | private final Logger log = getLogger(getClass()); |
| 48 | |
| 49 | private final long xid; |
| 50 | private final OFFactory factory; |
| 51 | private Meter.Unit unit = Meter.Unit.KB_PER_SEC; |
| 52 | private boolean burst = false; |
alshabib | 7bb0501 | 2015-08-05 10:15:09 -0700 | [diff] [blame] | 53 | private Long id; |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 54 | private Collection<Band> bands; |
| 55 | |
| 56 | public MeterModBuilder(long xid, OFFactory factory) { |
| 57 | this.xid = xid; |
| 58 | this.factory = factory; |
| 59 | } |
| 60 | |
| 61 | public static MeterModBuilder builder(long xid, OFFactory factory) { |
| 62 | return new MeterModBuilder(xid, factory); |
| 63 | } |
| 64 | |
| 65 | public MeterModBuilder withRateUnit(Meter.Unit unit) { |
| 66 | this.unit = unit; |
| 67 | return this; |
| 68 | } |
| 69 | |
| 70 | public MeterModBuilder burst() { |
| 71 | this.burst = true; |
| 72 | return this; |
| 73 | } |
| 74 | |
| 75 | public MeterModBuilder withId(MeterId meterId) { |
| 76 | this.id = meterId.id(); |
| 77 | return this; |
| 78 | } |
| 79 | |
| 80 | public MeterModBuilder withBands(Collection<Band> bands) { |
| 81 | this.bands = bands; |
| 82 | return this; |
| 83 | } |
| 84 | |
| 85 | public OFMeterMod add() { |
| 86 | validate(); |
| 87 | OFMeterMod.Builder builder = builderMeterMod(); |
Jimmy Jin | e9b7a02 | 2016-08-12 16:56:48 -0700 | [diff] [blame] | 88 | builder.setCommand(OFMeterModCommand.ADD); |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 89 | return builder.build(); |
| 90 | } |
| 91 | |
| 92 | public OFMeterMod remove() { |
| 93 | validate(); |
| 94 | OFMeterMod.Builder builder = builderMeterMod(); |
Jimmy Jin | e9b7a02 | 2016-08-12 16:56:48 -0700 | [diff] [blame] | 95 | builder.setCommand(OFMeterModCommand.DELETE); |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 96 | return builder.build(); |
| 97 | } |
| 98 | |
| 99 | public OFMeterMod modify() { |
| 100 | validate(); |
| 101 | OFMeterMod.Builder builder = builderMeterMod(); |
Jimmy Jin | e9b7a02 | 2016-08-12 16:56:48 -0700 | [diff] [blame] | 102 | builder.setCommand(OFMeterModCommand.MODIFY); |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 103 | return builder.build(); |
| 104 | } |
| 105 | |
| 106 | private OFMeterMod.Builder builderMeterMod() { |
| 107 | OFMeterMod.Builder builder = factory.buildMeterMod(); |
Jimmy Jin | e9b7a02 | 2016-08-12 16:56:48 -0700 | [diff] [blame] | 108 | Set<OFMeterFlags> flags = EnumSet.noneOf(OFMeterFlags.class); |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 109 | if (burst) { |
Jimmy Jin | e9b7a02 | 2016-08-12 16:56:48 -0700 | [diff] [blame] | 110 | flags.add(OFMeterFlags.BURST); |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 111 | } |
| 112 | switch (unit) { |
| 113 | case PKTS_PER_SEC: |
Jimmy Jin | e9b7a02 | 2016-08-12 16:56:48 -0700 | [diff] [blame] | 114 | flags.add(OFMeterFlags.PKTPS); |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 115 | break; |
| 116 | case KB_PER_SEC: |
Jimmy Jin | e9b7a02 | 2016-08-12 16:56:48 -0700 | [diff] [blame] | 117 | flags.add(OFMeterFlags.KBPS); |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 118 | break; |
| 119 | default: |
| 120 | log.warn("Unknown unit type {}", unit); |
| 121 | } |
cansu.toprak | 409289d | 2017-10-27 10:04:05 +0300 | [diff] [blame^] | 122 | |
| 123 | if (factory.getVersion().getWireVersion() >= OFVersion.OF_14.getWireVersion()) { |
| 124 | builder.setBands(buildBands()); |
| 125 | } else { |
| 126 | builder.setMeters(buildBands()); |
| 127 | } |
| 128 | |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 129 | builder.setFlags(flags) |
| 130 | .setMeterId(id) |
| 131 | .setXid(xid); |
| 132 | return builder; |
| 133 | } |
| 134 | |
| 135 | private List<OFMeterBand> buildBands() { |
| 136 | return bands.stream().map(b -> { |
| 137 | switch (b.type()) { |
| 138 | case DROP: |
| 139 | OFMeterBandDrop.Builder dropBuilder = |
| 140 | factory.meterBands().buildDrop(); |
| 141 | if (burst) { |
| 142 | dropBuilder.setBurstSize(b.burst()); |
| 143 | } |
| 144 | dropBuilder.setRate(b.rate()); |
| 145 | return dropBuilder.build(); |
| 146 | case REMARK: |
| 147 | OFMeterBandDscpRemark.Builder remarkBand = |
| 148 | factory.meterBands().buildDscpRemark(); |
| 149 | if (burst) { |
| 150 | remarkBand.setBurstSize(b.burst()); |
| 151 | } |
| 152 | remarkBand.setRate(b.rate()); |
| 153 | remarkBand.setPrecLevel(b.dropPrecedence()); |
| 154 | return remarkBand.build(); |
| 155 | default: |
| 156 | log.warn("Unknown band type {}", b.type()); |
| 157 | return null; |
| 158 | } |
Sho SHIMIZU | 4590604 | 2016-01-13 23:05:54 -0800 | [diff] [blame] | 159 | }).filter(Objects::nonNull).collect(Collectors.toList()); |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | private void validate() { |
| 163 | checkNotNull(id, "id cannot be null"); |
| 164 | checkNotNull(bands, "Must have bands"); |
Jon Hall | cbd1b39 | 2017-01-18 20:15:44 -0800 | [diff] [blame] | 165 | checkArgument(!bands.isEmpty(), "Must have at lease one band"); |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 166 | } |
| 167 | } |