Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame^] | 2 | * Copyright 2014 Open Networking Laboratory |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.provider.of.flow.impl; |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 17 | |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame^] | 18 | import static org.slf4j.LoggerFactory.getLogger; |
Jonathan Hart | 2ffcd10 | 2015-01-16 16:47:50 -0800 | [diff] [blame] | 19 | |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame^] | 20 | import java.util.Collections; |
| 21 | import java.util.List; |
| 22 | import java.util.Map; |
| 23 | import java.util.Optional; |
| 24 | import java.util.Set; |
| 25 | import java.util.concurrent.TimeUnit; |
| 26 | import java.util.stream.Collectors; |
| 27 | |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 28 | import org.apache.felix.scr.annotations.Activate; |
| 29 | import org.apache.felix.scr.annotations.Component; |
| 30 | import org.apache.felix.scr.annotations.Deactivate; |
| 31 | import org.apache.felix.scr.annotations.Reference; |
| 32 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 33 | import org.onosproject.core.ApplicationId; |
| 34 | import org.onosproject.net.DeviceId; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 35 | import org.onosproject.net.flow.CompletedBatchOperation; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 36 | import org.onosproject.net.flow.FlowEntry; |
| 37 | import org.onosproject.net.flow.FlowRule; |
| 38 | import org.onosproject.net.flow.FlowRuleBatchEntry; |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 39 | import org.onosproject.net.flow.FlowRuleBatchOperation; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 40 | import org.onosproject.net.flow.FlowRuleProvider; |
| 41 | import org.onosproject.net.flow.FlowRuleProviderRegistry; |
| 42 | import org.onosproject.net.flow.FlowRuleProviderService; |
| 43 | import org.onosproject.net.provider.AbstractProvider; |
| 44 | import org.onosproject.net.provider.ProviderId; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 45 | import org.onosproject.openflow.controller.Dpid; |
| 46 | import org.onosproject.openflow.controller.OpenFlowController; |
| 47 | import org.onosproject.openflow.controller.OpenFlowEventListener; |
| 48 | import org.onosproject.openflow.controller.OpenFlowSwitch; |
| 49 | import org.onosproject.openflow.controller.OpenFlowSwitchListener; |
| 50 | import org.onosproject.openflow.controller.RoleState; |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame^] | 51 | import org.onosproject.openflow.controller.ThirdPartyMessage; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 52 | import org.projectfloodlight.openflow.protocol.OFBarrierRequest; |
| 53 | import org.projectfloodlight.openflow.protocol.OFErrorMsg; |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 54 | import org.projectfloodlight.openflow.protocol.OFErrorType; |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 55 | import org.projectfloodlight.openflow.protocol.OFFlowMod; |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 56 | import org.projectfloodlight.openflow.protocol.OFFlowRemoved; |
alshabib | 5c370ff | 2014-09-18 10:12:14 -0700 | [diff] [blame] | 57 | import org.projectfloodlight.openflow.protocol.OFFlowStatsReply; |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 58 | import org.projectfloodlight.openflow.protocol.OFMessage; |
| 59 | import org.projectfloodlight.openflow.protocol.OFPortStatus; |
alshabib | 5c370ff | 2014-09-18 10:12:14 -0700 | [diff] [blame] | 60 | import org.projectfloodlight.openflow.protocol.OFStatsReply; |
sangho | 89bf6fb | 2015-02-09 09:33:13 -0800 | [diff] [blame] | 61 | import org.projectfloodlight.openflow.protocol.OFStatsType; |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 62 | import org.projectfloodlight.openflow.protocol.errormsg.OFFlowModFailedErrorMsg; |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 63 | import org.slf4j.Logger; |
| 64 | |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame^] | 65 | import com.google.common.cache.Cache; |
| 66 | import com.google.common.cache.CacheBuilder; |
| 67 | import com.google.common.cache.RemovalCause; |
| 68 | import com.google.common.cache.RemovalNotification; |
| 69 | import com.google.common.collect.Maps; |
| 70 | import com.google.common.collect.Sets; |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 71 | |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 72 | /** |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame^] | 73 | * Provider which uses an OpenFlow controller to detect network end-station |
| 74 | * hosts. |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 75 | */ |
| 76 | @Component(immediate = true) |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame^] | 77 | public class OpenFlowRuleProvider extends AbstractProvider |
| 78 | implements FlowRuleProvider { |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 79 | |
| 80 | private final Logger log = getLogger(getClass()); |
| 81 | |
| 82 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 83 | protected FlowRuleProviderRegistry providerRegistry; |
| 84 | |
| 85 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 86 | protected OpenFlowController controller; |
| 87 | |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 88 | private FlowRuleProviderService providerService; |
| 89 | |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 90 | private final InternalFlowProvider listener = new InternalFlowProvider(); |
| 91 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 92 | private Cache<Long, InternalCacheEntry> pendingBatches; |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 93 | |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 94 | private final Map<Dpid, FlowStatsCollector> collectors = Maps.newHashMap(); |
| 95 | |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 96 | /** |
| 97 | * Creates an OpenFlow host provider. |
| 98 | */ |
| 99 | public OpenFlowRuleProvider() { |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 100 | super(new ProviderId("of", "org.onosproject.provider.openflow")); |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | @Activate |
| 104 | public void activate() { |
| 105 | providerService = providerRegistry.register(this); |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 106 | controller.addListener(listener); |
| 107 | controller.addEventListener(listener); |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 108 | |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame^] | 109 | pendingBatches = CacheBuilder |
| 110 | .newBuilder() |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 111 | .expireAfterWrite(10, TimeUnit.SECONDS) |
| 112 | .removalListener((RemovalNotification<Long, InternalCacheEntry> notification) -> { |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame^] | 113 | if (notification.getCause() == RemovalCause.EXPIRED) { |
| 114 | providerService |
| 115 | .batchOperationCompleted(notification |
| 116 | .getKey(), |
| 117 | notification |
| 118 | .getValue() |
| 119 | .failedCompletion()); |
| 120 | } |
| 121 | }).build(); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 122 | |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 123 | for (OpenFlowSwitch sw : controller.getSwitches()) { |
| 124 | FlowStatsCollector fsc = new FlowStatsCollector(sw, POLL_INTERVAL); |
| 125 | fsc.start(); |
| 126 | collectors.put(new Dpid(sw.getId()), fsc); |
| 127 | } |
| 128 | |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 129 | log.info("Started"); |
| 130 | } |
| 131 | |
| 132 | @Deactivate |
| 133 | public void deactivate() { |
| 134 | providerRegistry.unregister(this); |
| 135 | providerService = null; |
| 136 | |
| 137 | log.info("Stopped"); |
| 138 | } |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 139 | |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 140 | @Override |
| 141 | public void applyFlowRule(FlowRule... flowRules) { |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 142 | for (FlowRule flowRule : flowRules) { |
| 143 | applyRule(flowRule); |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 144 | } |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 145 | } |
| 146 | |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 147 | private void applyRule(FlowRule flowRule) { |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame^] | 148 | OpenFlowSwitch sw = controller.getSwitch(Dpid.dpid(flowRule.deviceId() |
| 149 | .uri())); |
| 150 | if (flowRule.payLoad().payLoad().length > 0) { |
| 151 | OFMessage msg = new ThirdPartyMessage(flowRule.payLoad().payLoad()); |
| 152 | sw.sendMsg(msg); |
| 153 | return; |
| 154 | } |
alshabib | bdcbb10 | 2015-04-22 14:16:38 -0700 | [diff] [blame] | 155 | sw.sendMsg(FlowModBuilder.builder(flowRule, sw.factory(), |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame^] | 156 | Optional.empty()).buildFlowAdd()); |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 157 | } |
| 158 | |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 159 | @Override |
| 160 | public void removeFlowRule(FlowRule... flowRules) { |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 161 | for (FlowRule flowRule : flowRules) { |
| 162 | removeRule(flowRule); |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 163 | } |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 164 | |
| 165 | } |
| 166 | |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 167 | private void removeRule(FlowRule flowRule) { |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame^] | 168 | OpenFlowSwitch sw = controller.getSwitch(Dpid.dpid(flowRule.deviceId() |
| 169 | .uri())); |
| 170 | if (flowRule.payLoad().payLoad().length > 0) { |
| 171 | OFMessage msg = new ThirdPartyMessage(flowRule.payLoad().payLoad()); |
| 172 | sw.sendMsg(msg); |
| 173 | return; |
| 174 | } |
alshabib | bdcbb10 | 2015-04-22 14:16:38 -0700 | [diff] [blame] | 175 | sw.sendMsg(FlowModBuilder.builder(flowRule, sw.factory(), |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame^] | 176 | Optional.empty()).buildFlowDel()); |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 177 | } |
| 178 | |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 179 | @Override |
| 180 | public void removeRulesById(ApplicationId id, FlowRule... flowRules) { |
| 181 | // TODO: optimize using the ApplicationId |
| 182 | removeFlowRule(flowRules); |
| 183 | } |
| 184 | |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 185 | @Override |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 186 | public void executeBatch(FlowRuleBatchOperation batch) { |
| 187 | |
| 188 | pendingBatches.put(batch.id(), new InternalCacheEntry(batch)); |
| 189 | |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame^] | 190 | OpenFlowSwitch sw = controller.getSwitch(Dpid.dpid(batch.deviceId() |
| 191 | .uri())); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 192 | OFFlowMod mod; |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 193 | for (FlowRuleBatchEntry fbe : batch.getOperations()) { |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame^] | 194 | // flow is the third party privacy flow |
| 195 | if (fbe.target().payLoad().payLoad().length > 0) { |
| 196 | OFMessage msg = new ThirdPartyMessage(fbe.target().payLoad() |
| 197 | .payLoad()); |
| 198 | sw.sendMsg(msg); |
| 199 | continue; |
| 200 | } |
| 201 | FlowModBuilder builder = FlowModBuilder.builder(fbe.target(), sw |
| 202 | .factory(), Optional.of(batch.id())); |
Sho SHIMIZU | aba9d00 | 2015-01-29 14:51:04 -0800 | [diff] [blame] | 203 | switch (fbe.operator()) { |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame^] | 204 | case ADD: |
| 205 | mod = builder.buildFlowAdd(); |
| 206 | break; |
| 207 | case REMOVE: |
| 208 | mod = builder.buildFlowDel(); |
| 209 | break; |
| 210 | case MODIFY: |
| 211 | mod = builder.buildFlowMod(); |
| 212 | break; |
| 213 | default: |
| 214 | log.error("Unsupported batch operation {}; skipping flowmod {}", |
| 215 | fbe.operator(), fbe); |
| 216 | continue; |
| 217 | } |
Saurav Das | 3ea4662 | 2015-04-22 14:01:34 -0700 | [diff] [blame] | 218 | sw.sendMsg(mod); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 219 | } |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame^] | 220 | OFBarrierRequest.Builder builder = sw.factory().buildBarrierRequest() |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 221 | .setXid(batch.id()); |
| 222 | sw.sendMsg(builder.build()); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 223 | } |
| 224 | |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 225 | private class InternalFlowProvider |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 226 | implements OpenFlowSwitchListener, OpenFlowEventListener { |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 227 | |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 228 | @Override |
| 229 | public void switchAdded(Dpid dpid) { |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame^] | 230 | FlowStatsCollector fsc = new FlowStatsCollector( |
| 231 | controller |
| 232 | .getSwitch(dpid), |
| 233 | POLL_INTERVAL); |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 234 | fsc.start(); |
| 235 | collectors.put(dpid, fsc); |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | @Override |
| 239 | public void switchRemoved(Dpid dpid) { |
alshabib | dfc7afb | 2014-10-21 20:13:27 -0700 | [diff] [blame] | 240 | FlowStatsCollector collector = collectors.remove(dpid); |
| 241 | if (collector != null) { |
| 242 | collector.stop(); |
| 243 | } |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | @Override |
Ayaka Koshibe | 38594c2 | 2014-10-22 13:36:12 -0700 | [diff] [blame] | 247 | public void switchChanged(Dpid dpid) { |
| 248 | } |
| 249 | |
| 250 | @Override |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 251 | public void portChanged(Dpid dpid, OFPortStatus status) { |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame^] | 252 | // TODO: Decide whether to evict flows internal store. |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | @Override |
| 256 | public void handleMessage(Dpid dpid, OFMessage msg) { |
alshabib | da1644e | 2015-03-13 14:01:35 -0700 | [diff] [blame] | 257 | OpenFlowSwitch sw = controller.getSwitch(dpid); |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 258 | switch (msg.getType()) { |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame^] | 259 | case FLOW_REMOVED: |
| 260 | OFFlowRemoved removed = (OFFlowRemoved) msg; |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 261 | |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame^] | 262 | FlowEntry fr = new FlowEntryBuilder(dpid, removed).build(); |
| 263 | providerService.flowRemoved(fr); |
| 264 | break; |
| 265 | case STATS_REPLY: |
| 266 | if (((OFStatsReply) msg).getStatsType() == OFStatsType.FLOW) { |
| 267 | pushFlowMetrics(dpid, (OFFlowStatsReply) msg); |
| 268 | } |
| 269 | break; |
| 270 | case BARRIER_REPLY: |
| 271 | try { |
| 272 | InternalCacheEntry entry = pendingBatches.getIfPresent(msg |
| 273 | .getXid()); |
| 274 | if (entry != null) { |
| 275 | providerService |
| 276 | .batchOperationCompleted(msg.getXid(), |
| 277 | entry.completed()); |
| 278 | } else { |
| 279 | log.warn("Received unknown Barrier Reply: {}", |
| 280 | msg.getXid()); |
sangho | 89bf6fb | 2015-02-09 09:33:13 -0800 | [diff] [blame] | 281 | } |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame^] | 282 | } finally { |
| 283 | pendingBatches.invalidate(msg.getXid()); |
| 284 | } |
| 285 | break; |
| 286 | case ERROR: |
| 287 | log.warn("received Error message {} from {}", msg, dpid); |
| 288 | |
| 289 | OFErrorMsg error = (OFErrorMsg) msg; |
| 290 | if (error.getErrType() == OFErrorType.FLOW_MOD_FAILED) { |
| 291 | OFFlowModFailedErrorMsg fmFailed = (OFFlowModFailedErrorMsg) error; |
| 292 | if (fmFailed.getData().getParsedMessage().isPresent()) { |
| 293 | OFMessage m = fmFailed.getData().getParsedMessage() |
| 294 | .get(); |
| 295 | OFFlowMod fm = (OFFlowMod) m; |
| 296 | InternalCacheEntry entry = pendingBatches |
| 297 | .getIfPresent(msg.getXid()); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 298 | if (entry != null) { |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame^] | 299 | entry.appendFailure(new FlowEntryBuilder(dpid, fm) |
| 300 | .build()); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 301 | } else { |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame^] | 302 | log.error("No matching batch for this error: {}", |
| 303 | error); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 304 | } |
Yuta HIGUCHI | 82e5326 | 2014-11-27 10:28:51 -0800 | [diff] [blame] | 305 | } else { |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame^] | 306 | // FIXME: Potentially add flowtracking to avoid this |
| 307 | // message. |
| 308 | log.error("Flow installation failed but switch didn't" |
| 309 | + " tell us which one."); |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 310 | } |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame^] | 311 | } else { |
| 312 | log.warn("Received error {}", error); |
| 313 | } |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 314 | |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame^] | 315 | default: |
| 316 | log.debug("Unhandled message type: {}", msg.getType()); |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | } |
| 320 | |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 321 | @Override |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 322 | public void receivedRoleReply(Dpid dpid, RoleState requested, |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 323 | RoleState response) { |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 324 | // Do nothing here for now. |
| 325 | } |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 326 | |
sangho | 89bf6fb | 2015-02-09 09:33:13 -0800 | [diff] [blame] | 327 | private void pushFlowMetrics(Dpid dpid, OFFlowStatsReply replies) { |
alshabib | 64def64 | 2014-12-02 23:27:37 -0800 | [diff] [blame] | 328 | |
alshabib | 54ce589 | 2014-09-23 17:50:51 -0700 | [diff] [blame] | 329 | DeviceId did = DeviceId.deviceId(Dpid.uri(dpid)); |
Saurav Das | fa2fa93 | 2015-03-03 11:29:48 -0800 | [diff] [blame] | 330 | OpenFlowSwitch sw = controller.getSwitch(dpid); |
alshabib | 54ce589 | 2014-09-23 17:50:51 -0700 | [diff] [blame] | 331 | |
alshabib | 64def64 | 2014-12-02 23:27:37 -0800 | [diff] [blame] | 332 | List<FlowEntry> flowEntries = replies.getEntries().stream() |
alshabib | bdcbb10 | 2015-04-22 14:16:38 -0700 | [diff] [blame] | 333 | .map(entry -> new FlowEntryBuilder(dpid, entry).build()) |
alshabib | 64def64 | 2014-12-02 23:27:37 -0800 | [diff] [blame] | 334 | .collect(Collectors.toList()); |
alshabib | 54ce589 | 2014-09-23 17:50:51 -0700 | [diff] [blame] | 335 | |
alshabib | 64def64 | 2014-12-02 23:27:37 -0800 | [diff] [blame] | 336 | providerService.pushFlowMetrics(did, flowEntries); |
| 337 | |
alshabib | 5c370ff | 2014-09-18 10:12:14 -0700 | [diff] [blame] | 338 | } |
| 339 | |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 340 | } |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 341 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 342 | /** |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame^] | 343 | * The internal cache entry holding the original request as well as |
| 344 | * accumulating the any failures along the way. |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 345 | * |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame^] | 346 | * If this entry is evicted from the cache then the entire operation is |
| 347 | * considered failed. Otherwise, only the failures reported by the device |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 348 | * will be propagated up. |
| 349 | */ |
| 350 | private class InternalCacheEntry { |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 351 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 352 | private final FlowRuleBatchOperation operation; |
| 353 | private final Set<FlowRule> failures = Sets.newConcurrentHashSet(); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 354 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 355 | public InternalCacheEntry(FlowRuleBatchOperation operation) { |
| 356 | this.operation = operation; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 357 | } |
| 358 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 359 | /** |
| 360 | * Appends a failed rule to the set of failed items. |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame^] | 361 | * |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 362 | * @param rule the failed rule |
| 363 | */ |
| 364 | public void appendFailure(FlowRule rule) { |
| 365 | failures.add(rule); |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 366 | } |
| 367 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 368 | /** |
| 369 | * Fails the entire batch and returns the failed operation. |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame^] | 370 | * |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 371 | * @return the failed operation |
| 372 | */ |
| 373 | public CompletedBatchOperation failedCompletion() { |
| 374 | Set<FlowRule> fails = operation.getOperations().stream() |
| 375 | .map(op -> op.target()).collect(Collectors.toSet()); |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame^] | 376 | return new CompletedBatchOperation(false, |
| 377 | Collections |
| 378 | .unmodifiableSet(fails), |
| 379 | operation.deviceId()); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 380 | } |
| 381 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 382 | /** |
| 383 | * Returns the completed operation and whether the batch suceeded. |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame^] | 384 | * |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 385 | * @return the completed operation |
| 386 | */ |
| 387 | public CompletedBatchOperation completed() { |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame^] | 388 | return new CompletedBatchOperation( |
| 389 | failures.isEmpty(), |
| 390 | Collections |
| 391 | .unmodifiableSet(failures), |
| 392 | operation.deviceId()); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 393 | } |
| 394 | |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 395 | } |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 396 | |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 397 | } |