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