alshabib | e27055b | 2015-07-09 21:43:10 -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 | e27055b | 2015-07-09 21:43:10 -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 | |
| 17 | package org.onosproject.provider.of.meter.impl; |
| 18 | |
alshabib | 1d2bc40 | 2015-07-31 17:04:11 -0700 | [diff] [blame] | 19 | |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 20 | import com.google.common.cache.Cache; |
| 21 | import com.google.common.cache.CacheBuilder; |
| 22 | import com.google.common.cache.RemovalCause; |
| 23 | import com.google.common.cache.RemovalNotification; |
Yuta HIGUCHI | 67f2cca | 2017-01-19 19:31:58 -0800 | [diff] [blame] | 24 | import com.google.common.collect.ImmutableSet; |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 25 | import com.google.common.collect.Maps; |
| 26 | import org.apache.felix.scr.annotations.Activate; |
alshabib | e27055b | 2015-07-09 21:43:10 -0700 | [diff] [blame] | 27 | import org.apache.felix.scr.annotations.Component; |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 28 | import org.apache.felix.scr.annotations.Deactivate; |
alshabib | e27055b | 2015-07-09 21:43:10 -0700 | [diff] [blame] | 29 | import org.apache.felix.scr.annotations.Reference; |
| 30 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
alshabib | 7bb0501 | 2015-08-05 10:15:09 -0700 | [diff] [blame] | 31 | import org.onosproject.core.CoreService; |
Charles Chan | a59f9b76 | 2017-07-30 18:09:44 -0700 | [diff] [blame] | 32 | import org.onosproject.net.driver.Driver; |
| 33 | import org.onosproject.net.driver.DriverService; |
alshabib | 10c810b | 2015-08-18 16:59:04 -0700 | [diff] [blame] | 34 | import org.onosproject.net.meter.Band; |
| 35 | import org.onosproject.net.meter.DefaultBand; |
| 36 | import org.onosproject.net.meter.DefaultMeter; |
| 37 | import org.onosproject.net.meter.Meter; |
| 38 | import org.onosproject.net.meter.MeterFailReason; |
Jordi Ortiz | aa8de49 | 2016-12-01 00:21:36 +0100 | [diff] [blame] | 39 | import org.onosproject.net.meter.MeterFeatures; |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 40 | import org.onosproject.net.meter.MeterId; |
alshabib | 10c810b | 2015-08-18 16:59:04 -0700 | [diff] [blame] | 41 | import org.onosproject.net.meter.MeterOperation; |
| 42 | import org.onosproject.net.meter.MeterOperations; |
| 43 | import org.onosproject.net.meter.MeterProvider; |
| 44 | import org.onosproject.net.meter.MeterProviderRegistry; |
| 45 | import org.onosproject.net.meter.MeterProviderService; |
| 46 | import org.onosproject.net.meter.MeterState; |
Yuta HIGUCHI | 67f2cca | 2017-01-19 19:31:58 -0800 | [diff] [blame] | 47 | import org.onosproject.net.Device; |
alshabib | 7bb0501 | 2015-08-05 10:15:09 -0700 | [diff] [blame] | 48 | import org.onosproject.net.DeviceId; |
alshabib | e27055b | 2015-07-09 21:43:10 -0700 | [diff] [blame] | 49 | import org.onosproject.net.provider.AbstractProvider; |
| 50 | import org.onosproject.net.provider.ProviderId; |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 51 | import org.onosproject.openflow.controller.Dpid; |
alshabib | e27055b | 2015-07-09 21:43:10 -0700 | [diff] [blame] | 52 | import org.onosproject.openflow.controller.OpenFlowController; |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 53 | import org.onosproject.openflow.controller.OpenFlowEventListener; |
| 54 | import org.onosproject.openflow.controller.OpenFlowSwitch; |
| 55 | import org.onosproject.openflow.controller.OpenFlowSwitchListener; |
| 56 | import org.onosproject.openflow.controller.RoleState; |
Jordi Ortiz | aa8de49 | 2016-12-01 00:21:36 +0100 | [diff] [blame] | 57 | import org.onosproject.provider.of.meter.util.MeterFeaturesBuilder; |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 58 | import org.projectfloodlight.openflow.protocol.OFErrorMsg; |
| 59 | import org.projectfloodlight.openflow.protocol.OFErrorType; |
| 60 | import org.projectfloodlight.openflow.protocol.OFMessage; |
alshabib | 7bb0501 | 2015-08-05 10:15:09 -0700 | [diff] [blame] | 61 | import org.projectfloodlight.openflow.protocol.OFMeterBandStats; |
| 62 | import org.projectfloodlight.openflow.protocol.OFMeterConfigStatsReply; |
Jordi Ortiz | aa8de49 | 2016-12-01 00:21:36 +0100 | [diff] [blame] | 63 | import org.projectfloodlight.openflow.protocol.OFMeterFeatures; |
alshabib | 7bb0501 | 2015-08-05 10:15:09 -0700 | [diff] [blame] | 64 | import org.projectfloodlight.openflow.protocol.OFMeterStats; |
| 65 | import org.projectfloodlight.openflow.protocol.OFMeterStatsReply; |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 66 | import org.projectfloodlight.openflow.protocol.OFPortStatus; |
| 67 | import org.projectfloodlight.openflow.protocol.OFStatsReply; |
alshabib | 7bb0501 | 2015-08-05 10:15:09 -0700 | [diff] [blame] | 68 | import org.projectfloodlight.openflow.protocol.OFStatsType; |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 69 | import org.projectfloodlight.openflow.protocol.OFVersion; |
| 70 | import org.projectfloodlight.openflow.protocol.errormsg.OFMeterModFailedErrorMsg; |
alshabib | e27055b | 2015-07-09 21:43:10 -0700 | [diff] [blame] | 71 | import org.slf4j.Logger; |
| 72 | |
alshabib | 7bb0501 | 2015-08-05 10:15:09 -0700 | [diff] [blame] | 73 | import java.util.Collection; |
Yuta HIGUCHI | 67f2cca | 2017-01-19 19:31:58 -0800 | [diff] [blame] | 74 | import java.util.EnumSet; |
alshabib | 7bb0501 | 2015-08-05 10:15:09 -0700 | [diff] [blame] | 75 | import java.util.List; |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 76 | import java.util.Map; |
Yuta HIGUCHI | 67f2cca | 2017-01-19 19:31:58 -0800 | [diff] [blame] | 77 | import java.util.Set; |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 78 | import java.util.concurrent.TimeUnit; |
| 79 | import java.util.concurrent.atomic.AtomicLong; |
alshabib | 7bb0501 | 2015-08-05 10:15:09 -0700 | [diff] [blame] | 80 | import java.util.stream.Collectors; |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 81 | |
Jordi Ortiz | aa8de49 | 2016-12-01 00:21:36 +0100 | [diff] [blame] | 82 | import static org.onosproject.net.DeviceId.deviceId; |
| 83 | import static org.onosproject.openflow.controller.Dpid.uri; |
alshabib | e27055b | 2015-07-09 21:43:10 -0700 | [diff] [blame] | 84 | import static org.slf4j.LoggerFactory.getLogger; |
| 85 | |
| 86 | /** |
| 87 | * Provider which uses an OpenFlow controller to handle meters. |
| 88 | */ |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 89 | @Component(immediate = true, enabled = true) |
alshabib | e27055b | 2015-07-09 21:43:10 -0700 | [diff] [blame] | 90 | public class OpenFlowMeterProvider extends AbstractProvider implements MeterProvider { |
| 91 | |
| 92 | private final Logger log = getLogger(getClass()); |
| 93 | |
| 94 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 95 | protected OpenFlowController controller; |
| 96 | |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 97 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 98 | protected MeterProviderRegistry providerRegistry; |
| 99 | |
alshabib | 7bb0501 | 2015-08-05 10:15:09 -0700 | [diff] [blame] | 100 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 101 | protected CoreService coreService; |
| 102 | |
Charles Chan | a59f9b76 | 2017-07-30 18:09:44 -0700 | [diff] [blame] | 103 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 104 | protected DriverService driverService; |
| 105 | |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 106 | private MeterProviderService providerService; |
| 107 | |
| 108 | private static final AtomicLong XID_COUNTER = new AtomicLong(1); |
| 109 | |
| 110 | static final int POLL_INTERVAL = 10; |
| 111 | static final long TIMEOUT = 30; |
| 112 | |
alshabib | 7bb0501 | 2015-08-05 10:15:09 -0700 | [diff] [blame] | 113 | private Cache<Long, MeterOperation> pendingOperations; |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 114 | |
| 115 | |
| 116 | private InternalMeterListener listener = new InternalMeterListener(); |
| 117 | private Map<Dpid, MeterStatsCollector> collectors = Maps.newHashMap(); |
alshabib | e27055b | 2015-07-09 21:43:10 -0700 | [diff] [blame] | 118 | |
Yuta HIGUCHI | 67f2cca | 2017-01-19 19:31:58 -0800 | [diff] [blame] | 119 | private static final Set<Device.Type> NO_METER_SUPPORT = |
| 120 | ImmutableSet.copyOf(EnumSet.of(Device.Type.ROADM, |
| 121 | Device.Type.ROADM_OTN, |
| 122 | Device.Type.FIBER_SWITCH, |
| 123 | Device.Type.OTN)); |
| 124 | |
alshabib | e27055b | 2015-07-09 21:43:10 -0700 | [diff] [blame] | 125 | /** |
| 126 | * Creates a OpenFlow meter provider. |
| 127 | */ |
| 128 | public OpenFlowMeterProvider() { |
| 129 | super(new ProviderId("of", "org.onosproject.provider.meter")); |
| 130 | } |
alshabib | bc37196 | 2015-07-09 22:26:21 -0700 | [diff] [blame] | 131 | |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 132 | @Activate |
| 133 | public void activate() { |
| 134 | providerService = providerRegistry.register(this); |
| 135 | |
| 136 | pendingOperations = CacheBuilder.newBuilder() |
| 137 | .expireAfterWrite(TIMEOUT, TimeUnit.SECONDS) |
alshabib | 7bb0501 | 2015-08-05 10:15:09 -0700 | [diff] [blame] | 138 | .removalListener((RemovalNotification<Long, MeterOperation> notification) -> { |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 139 | if (notification.getCause() == RemovalCause.EXPIRED) { |
| 140 | providerService.meterOperationFailed(notification.getValue(), |
| 141 | MeterFailReason.TIMEOUT); |
| 142 | } |
| 143 | }).build(); |
| 144 | |
| 145 | controller.addEventListener(listener); |
| 146 | controller.addListener(listener); |
| 147 | |
| 148 | controller.getSwitches().forEach((sw -> createStatsCollection(sw))); |
| 149 | } |
| 150 | |
| 151 | @Deactivate |
| 152 | public void deactivate() { |
| 153 | providerRegistry.unregister(this); |
Charles Chan | ecfdfb7 | 2015-11-24 19:05:50 -0800 | [diff] [blame] | 154 | collectors.values().forEach(MeterStatsCollector::stop); |
| 155 | collectors.clear(); |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 156 | controller.removeEventListener(listener); |
| 157 | controller.removeListener(listener); |
| 158 | providerService = null; |
| 159 | } |
| 160 | |
alshabib | bc37196 | 2015-07-09 22:26:21 -0700 | [diff] [blame] | 161 | @Override |
| 162 | public void performMeterOperation(DeviceId deviceId, MeterOperations meterOps) { |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 163 | Dpid dpid = Dpid.dpid(deviceId.uri()); |
| 164 | OpenFlowSwitch sw = controller.getSwitch(dpid); |
| 165 | if (sw == null) { |
| 166 | log.error("Unknown device {}", deviceId); |
| 167 | meterOps.operations().forEach(op -> |
| 168 | providerService.meterOperationFailed(op, |
| 169 | MeterFailReason.UNKNOWN_DEVICE) |
| 170 | ); |
| 171 | return; |
| 172 | } |
alshabib | bc37196 | 2015-07-09 22:26:21 -0700 | [diff] [blame] | 173 | |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 174 | meterOps.operations().forEach(op -> performOperation(sw, op)); |
alshabib | bc37196 | 2015-07-09 22:26:21 -0700 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | @Override |
| 178 | public void performMeterOperation(DeviceId deviceId, MeterOperation meterOp) { |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 179 | Dpid dpid = Dpid.dpid(deviceId.uri()); |
| 180 | OpenFlowSwitch sw = controller.getSwitch(dpid); |
| 181 | if (sw == null) { |
| 182 | log.error("Unknown device {}", deviceId); |
| 183 | providerService.meterOperationFailed(meterOp, |
| 184 | MeterFailReason.UNKNOWN_DEVICE); |
| 185 | return; |
| 186 | } |
alshabib | bc37196 | 2015-07-09 22:26:21 -0700 | [diff] [blame] | 187 | |
alshabib | 7bb0501 | 2015-08-05 10:15:09 -0700 | [diff] [blame] | 188 | performOperation(sw, meterOp); |
| 189 | |
Jordi Ortiz | 9fe79a2 | 2017-04-25 12:57:11 +0200 | [diff] [blame] | 190 | if (meterOp.type().equals(MeterOperation.Type.REMOVE)) { |
| 191 | forceMeterStats(deviceId); |
| 192 | } |
| 193 | |
| 194 | } |
| 195 | |
| 196 | private void forceMeterStats(DeviceId deviceId) { |
| 197 | Dpid dpid = Dpid.dpid(deviceId.uri()); |
| 198 | OpenFlowSwitch sw = controller.getSwitch(dpid); |
| 199 | |
| 200 | MeterStatsCollector once = new MeterStatsCollector(sw, 1); |
Palash Kala | f95c38b | 2017-05-15 23:52:48 +0900 | [diff] [blame] | 201 | once.sendMeterStatisticRequest(); |
Jordi Ortiz | 9fe79a2 | 2017-04-25 12:57:11 +0200 | [diff] [blame] | 202 | |
alshabib | bc37196 | 2015-07-09 22:26:21 -0700 | [diff] [blame] | 203 | } |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 204 | |
| 205 | private void performOperation(OpenFlowSwitch sw, MeterOperation op) { |
| 206 | |
| 207 | pendingOperations.put(op.meter().id().id(), op); |
| 208 | |
| 209 | |
| 210 | Meter meter = op.meter(); |
| 211 | MeterModBuilder builder = MeterModBuilder.builder(meter.id().id(), sw.factory()); |
| 212 | if (meter.isBurst()) { |
| 213 | builder.burst(); |
| 214 | } |
| 215 | builder.withBands(meter.bands()) |
| 216 | .withId(meter.id()) |
| 217 | .withRateUnit(meter.unit()); |
| 218 | |
| 219 | switch (op.type()) { |
| 220 | case ADD: |
| 221 | sw.sendMsg(builder.add()); |
| 222 | break; |
| 223 | case REMOVE: |
| 224 | sw.sendMsg(builder.remove()); |
| 225 | break; |
| 226 | case MODIFY: |
| 227 | sw.sendMsg(builder.modify()); |
| 228 | break; |
| 229 | default: |
| 230 | log.warn("Unknown Meter command {}; not sending anything", |
| 231 | op.type()); |
| 232 | providerService.meterOperationFailed(op, |
| 233 | MeterFailReason.UNKNOWN_COMMAND); |
| 234 | } |
| 235 | |
| 236 | } |
| 237 | |
| 238 | private void createStatsCollection(OpenFlowSwitch sw) { |
Madan Jampani | 84382b9 | 2016-06-22 08:26:49 -0700 | [diff] [blame] | 239 | if (sw != null && isMeterSupported(sw)) { |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 240 | MeterStatsCollector msc = new MeterStatsCollector(sw, POLL_INTERVAL); |
Thomas Vachuska | a394b95 | 2016-06-14 15:02:09 -0700 | [diff] [blame] | 241 | stopCollectorIfNeeded(collectors.put(new Dpid(sw.getId()), msc)); |
Palash Kala | a439afe | 2017-05-16 14:53:15 +0900 | [diff] [blame] | 242 | msc.start(); |
Thomas Vachuska | a394b95 | 2016-06-14 15:02:09 -0700 | [diff] [blame] | 243 | } |
| 244 | } |
| 245 | |
| 246 | private void stopCollectorIfNeeded(MeterStatsCollector collector) { |
| 247 | if (collector != null) { |
| 248 | collector.stop(); |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 249 | } |
| 250 | } |
| 251 | |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 252 | // TODO: ONOS-3546 Support per device enabling/disabling via network config |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 253 | private boolean isMeterSupported(OpenFlowSwitch sw) { |
| 254 | if (sw.factory().getVersion() == OFVersion.OF_10 || |
| 255 | sw.factory().getVersion() == OFVersion.OF_11 || |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 256 | sw.factory().getVersion() == OFVersion.OF_12 || |
Yuta HIGUCHI | 67f2cca | 2017-01-19 19:31:58 -0800 | [diff] [blame] | 257 | NO_METER_SUPPORT.contains(sw.deviceType()) || |
Charles Chan | a59f9b76 | 2017-07-30 18:09:44 -0700 | [diff] [blame] | 258 | !isMeterCapable(sw)) { |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 259 | return false; |
| 260 | } |
| 261 | |
| 262 | return true; |
| 263 | } |
| 264 | |
Charles Chan | a59f9b76 | 2017-07-30 18:09:44 -0700 | [diff] [blame] | 265 | /** |
| 266 | * Determine whether the given switch is meter-capable. |
| 267 | * |
| 268 | * @param sw switch |
| 269 | * @return the boolean value of meterCapable property, or true if it is not configured. |
| 270 | */ |
| 271 | private boolean isMeterCapable(OpenFlowSwitch sw) { |
| 272 | Driver driver = driverService.getDriver(DeviceId.deviceId(Dpid.uri(sw.getDpid()))); |
| 273 | String isMeterCapable = driver.getProperty(METER_CAPABLE); |
| 274 | return isMeterCapable == null || Boolean.parseBoolean(isMeterCapable); |
| 275 | } |
| 276 | |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 277 | private void pushMeterStats(Dpid dpid, OFStatsReply msg) { |
alshabib | 7bb0501 | 2015-08-05 10:15:09 -0700 | [diff] [blame] | 278 | DeviceId deviceId = DeviceId.deviceId(Dpid.uri(dpid)); |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 279 | |
alshabib | 7bb0501 | 2015-08-05 10:15:09 -0700 | [diff] [blame] | 280 | if (msg.getStatsType() == OFStatsType.METER) { |
| 281 | OFMeterStatsReply reply = (OFMeterStatsReply) msg; |
| 282 | Collection<Meter> meters = buildMeters(deviceId, reply.getEntries()); |
| 283 | //TODO do meter accounting here. |
| 284 | providerService.pushMeterMetrics(deviceId, meters); |
| 285 | } else if (msg.getStatsType() == OFStatsType.METER_CONFIG) { |
| 286 | OFMeterConfigStatsReply reply = (OFMeterConfigStatsReply) msg; |
| 287 | // FIXME: Map<Long, Meter> meters = collectMeters(deviceId, reply); |
| 288 | } |
| 289 | |
| 290 | } |
| 291 | |
Jordi Ortiz | aa8de49 | 2016-12-01 00:21:36 +0100 | [diff] [blame] | 292 | private MeterFeatures buildMeterFeatures(Dpid dpid, OFMeterFeatures mf) { |
| 293 | if (mf != null) { |
| 294 | return new MeterFeaturesBuilder(mf, deviceId(uri(dpid))) |
| 295 | .build(); |
| 296 | } else { |
| 297 | // This will usually happen for OpenFlow devices prior to 1.3 |
| 298 | return MeterFeaturesBuilder.noMeterFeatures(deviceId(uri(dpid))); |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | private void pushMeterFeatures(Dpid dpid, OFMeterFeatures meterFeatures) { |
| 303 | providerService.pushMeterFeatures(deviceId(uri(dpid)), |
| 304 | buildMeterFeatures(dpid, meterFeatures)); |
| 305 | } |
| 306 | |
| 307 | private void destroyMeterFeatures(Dpid dpid) { |
| 308 | providerService.deleteMeterFeatures(deviceId(uri(dpid))); |
| 309 | } |
| 310 | |
alshabib | 7bb0501 | 2015-08-05 10:15:09 -0700 | [diff] [blame] | 311 | private Map<Long, Meter> collectMeters(DeviceId deviceId, |
| 312 | OFMeterConfigStatsReply reply) { |
| 313 | return Maps.newHashMap(); |
| 314 | //TODO: Needs a fix to be applied to loxi MeterConfig stat is incorrect |
| 315 | } |
| 316 | |
| 317 | private Collection<Meter> buildMeters(DeviceId deviceId, |
| 318 | List<OFMeterStats> entries) { |
| 319 | return entries.stream().map(stat -> { |
| 320 | DefaultMeter.Builder builder = DefaultMeter.builder(); |
| 321 | Collection<Band> bands = buildBands(stat.getBandStats()); |
| 322 | builder.forDevice(deviceId) |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 323 | .withId(MeterId.meterId(stat.getMeterId())) |
alshabib | 7bb0501 | 2015-08-05 10:15:09 -0700 | [diff] [blame] | 324 | //FIXME: need to encode appId in meter id, but that makes |
| 325 | // things a little annoying for debugging |
| 326 | .fromApp(coreService.getAppId("org.onosproject.core")) |
| 327 | .withBands(bands); |
| 328 | DefaultMeter meter = builder.build(); |
| 329 | meter.setState(MeterState.ADDED); |
| 330 | meter.setLife(stat.getDurationSec()); |
| 331 | meter.setProcessedBytes(stat.getByteInCount().getValue()); |
| 332 | meter.setProcessedPackets(stat.getPacketInCount().getValue()); |
| 333 | meter.setReferenceCount(stat.getFlowCount()); |
| 334 | |
| 335 | // marks the meter as seen on the dataplane |
| 336 | pendingOperations.invalidate(stat.getMeterId()); |
| 337 | return meter; |
| 338 | }).collect(Collectors.toSet()); |
| 339 | } |
| 340 | |
| 341 | private Collection<Band> buildBands(List<OFMeterBandStats> bandStats) { |
| 342 | return bandStats.stream().map(stat -> { |
Jordi Ortiz | af75c13 | 2017-06-22 16:06:36 +0200 | [diff] [blame] | 343 | DefaultBand band = ((DefaultBand.Builder) DefaultBand.builder().ofType(DefaultBand.Type.DROP)).build(); |
alshabib | 7bb0501 | 2015-08-05 10:15:09 -0700 | [diff] [blame] | 344 | band.setBytes(stat.getByteBandCount().getValue()); |
| 345 | band.setPackets(stat.getPacketBandCount().getValue()); |
| 346 | return band; |
Jordi Ortiz | af75c13 | 2017-06-22 16:06:36 +0200 | [diff] [blame] | 347 | }).collect(Collectors.toList()); |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | private void signalMeterError(OFMeterModFailedErrorMsg meterError, |
| 351 | MeterOperation op) { |
| 352 | switch (meterError.getCode()) { |
| 353 | case UNKNOWN: |
| 354 | providerService.meterOperationFailed(op, |
| 355 | MeterFailReason.UNKNOWN_DEVICE); |
| 356 | break; |
| 357 | case METER_EXISTS: |
| 358 | providerService.meterOperationFailed(op, |
| 359 | MeterFailReason.EXISTING_METER); |
| 360 | break; |
| 361 | case INVALID_METER: |
| 362 | providerService.meterOperationFailed(op, |
| 363 | MeterFailReason.INVALID_METER); |
| 364 | break; |
| 365 | case UNKNOWN_METER: |
| 366 | providerService.meterOperationFailed(op, |
| 367 | MeterFailReason.UNKNOWN); |
| 368 | break; |
| 369 | case BAD_COMMAND: |
| 370 | providerService.meterOperationFailed(op, |
| 371 | MeterFailReason.UNKNOWN_COMMAND); |
| 372 | break; |
| 373 | case BAD_FLAGS: |
| 374 | providerService.meterOperationFailed(op, |
| 375 | MeterFailReason.UNKNOWN_FLAGS); |
| 376 | break; |
| 377 | case BAD_RATE: |
| 378 | providerService.meterOperationFailed(op, |
| 379 | MeterFailReason.BAD_RATE); |
| 380 | break; |
| 381 | case BAD_BURST: |
| 382 | providerService.meterOperationFailed(op, |
| 383 | MeterFailReason.BAD_BURST); |
| 384 | break; |
| 385 | case BAD_BAND: |
| 386 | providerService.meterOperationFailed(op, |
| 387 | MeterFailReason.BAD_BAND); |
| 388 | break; |
| 389 | case BAD_BAND_VALUE: |
| 390 | providerService.meterOperationFailed(op, |
| 391 | MeterFailReason.BAD_BAND_VALUE); |
| 392 | break; |
| 393 | case OUT_OF_METERS: |
| 394 | providerService.meterOperationFailed(op, |
| 395 | MeterFailReason.OUT_OF_METERS); |
| 396 | break; |
| 397 | case OUT_OF_BANDS: |
| 398 | providerService.meterOperationFailed(op, |
| 399 | MeterFailReason.OUT_OF_BANDS); |
| 400 | break; |
| 401 | default: |
| 402 | providerService.meterOperationFailed(op, |
| 403 | MeterFailReason.UNKNOWN); |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | private class InternalMeterListener |
| 408 | implements OpenFlowSwitchListener, OpenFlowEventListener { |
| 409 | @Override |
| 410 | public void handleMessage(Dpid dpid, OFMessage msg) { |
| 411 | switch (msg.getType()) { |
| 412 | case STATS_REPLY: |
| 413 | pushMeterStats(dpid, (OFStatsReply) msg); |
| 414 | break; |
| 415 | case ERROR: |
| 416 | OFErrorMsg error = (OFErrorMsg) msg; |
| 417 | if (error.getErrType() == OFErrorType.METER_MOD_FAILED) { |
| 418 | MeterOperation op = |
| 419 | pendingOperations.getIfPresent(error.getXid()); |
| 420 | pendingOperations.invalidate(error.getXid()); |
| 421 | if (op == null) { |
| 422 | log.warn("Unknown Meter operation failed {}", error); |
| 423 | } else { |
| 424 | OFMeterModFailedErrorMsg meterError = |
| 425 | (OFMeterModFailedErrorMsg) error; |
| 426 | signalMeterError(meterError, op); |
| 427 | } |
| 428 | } |
| 429 | break; |
| 430 | default: |
| 431 | break; |
| 432 | } |
| 433 | |
| 434 | } |
| 435 | |
| 436 | @Override |
| 437 | public void switchAdded(Dpid dpid) { |
| 438 | createStatsCollection(controller.getSwitch(dpid)); |
Jordi Ortiz | aa8de49 | 2016-12-01 00:21:36 +0100 | [diff] [blame] | 439 | pushMeterFeatures(dpid, controller.getSwitch(dpid).getMeterFeatures()); |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | @Override |
| 443 | public void switchRemoved(Dpid dpid) { |
Thomas Vachuska | a394b95 | 2016-06-14 15:02:09 -0700 | [diff] [blame] | 444 | stopCollectorIfNeeded(collectors.remove(dpid)); |
Jordi Ortiz | aa8de49 | 2016-12-01 00:21:36 +0100 | [diff] [blame] | 445 | destroyMeterFeatures(dpid); |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | @Override |
| 449 | public void switchChanged(Dpid dpid) { |
| 450 | |
| 451 | } |
| 452 | |
| 453 | @Override |
| 454 | public void portChanged(Dpid dpid, OFPortStatus status) { |
| 455 | |
| 456 | } |
| 457 | |
| 458 | @Override |
| 459 | public void receivedRoleReply(Dpid dpid, RoleState requested, RoleState response) { |
| 460 | |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | |
| 465 | |
alshabib | e27055b | 2015-07-09 21:43:10 -0700 | [diff] [blame] | 466 | } |