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