Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present 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.openflow.controller.impl; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 17 | |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 18 | import com.google.common.collect.ArrayListMultimap; |
alshabib | 64def64 | 2014-12-02 23:27:37 -0800 | [diff] [blame] | 19 | import com.google.common.collect.Lists; |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 20 | import com.google.common.collect.Multimap; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 21 | import org.apache.felix.scr.annotations.Activate; |
| 22 | import org.apache.felix.scr.annotations.Component; |
| 23 | import org.apache.felix.scr.annotations.Deactivate; |
Jonathan Hart | bbd91d4 | 2015-02-27 11:18:04 -0800 | [diff] [blame] | 24 | import org.apache.felix.scr.annotations.Modified; |
Charles Chan | 3b00e1b | 2015-08-26 23:12:52 +0800 | [diff] [blame] | 25 | import org.apache.felix.scr.annotations.Property; |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 26 | import org.apache.felix.scr.annotations.Reference; |
| 27 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 28 | import org.apache.felix.scr.annotations.Service; |
Charles Chan | 3b00e1b | 2015-08-26 23:12:52 +0800 | [diff] [blame] | 29 | import org.onosproject.cfg.ComponentConfigService; |
Charles Chan | ecfdfb7 | 2015-11-24 19:05:50 -0800 | [diff] [blame] | 30 | import org.onosproject.core.CoreService; |
Thomas Vachuska | 3358af2 | 2015-05-19 18:40:34 -0700 | [diff] [blame] | 31 | import org.onosproject.net.driver.DefaultDriverProviderService; |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 32 | import org.onosproject.net.driver.DriverService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 33 | import org.onosproject.openflow.controller.DefaultOpenFlowPacketContext; |
| 34 | import org.onosproject.openflow.controller.Dpid; |
| 35 | import org.onosproject.openflow.controller.OpenFlowController; |
| 36 | import org.onosproject.openflow.controller.OpenFlowEventListener; |
Jian Li | a78cdb2 | 2016-04-21 13:03:58 -0700 | [diff] [blame^] | 37 | import org.onosproject.openflow.controller.OpenFlowMessageListener; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 38 | import org.onosproject.openflow.controller.OpenFlowPacketContext; |
| 39 | import org.onosproject.openflow.controller.OpenFlowSwitch; |
| 40 | import org.onosproject.openflow.controller.OpenFlowSwitchListener; |
| 41 | import org.onosproject.openflow.controller.PacketListener; |
| 42 | import org.onosproject.openflow.controller.RoleState; |
| 43 | import org.onosproject.openflow.controller.driver.OpenFlowAgent; |
Jonathan Hart | bbd91d4 | 2015-02-27 11:18:04 -0800 | [diff] [blame] | 44 | import org.osgi.service.component.ComponentContext; |
Marc De Leenheer | b931137 | 2015-07-09 11:36:49 -0700 | [diff] [blame] | 45 | import org.projectfloodlight.openflow.protocol.OFCalientFlowStatsEntry; |
| 46 | import org.projectfloodlight.openflow.protocol.OFCalientFlowStatsReply; |
Marc De Leenheer | 631ffce | 2014-10-28 16:29:07 -0700 | [diff] [blame] | 47 | import org.projectfloodlight.openflow.protocol.OFCircuitPortStatus; |
| 48 | import org.projectfloodlight.openflow.protocol.OFExperimenter; |
alshabib | 64def64 | 2014-12-02 23:27:37 -0800 | [diff] [blame] | 49 | import org.projectfloodlight.openflow.protocol.OFFactories; |
| 50 | import org.projectfloodlight.openflow.protocol.OFFlowStatsEntry; |
| 51 | import org.projectfloodlight.openflow.protocol.OFFlowStatsReply; |
sangho | 6a0bb17 | 2015-02-05 12:24:48 -0800 | [diff] [blame] | 52 | import org.projectfloodlight.openflow.protocol.OFGroupDescStatsEntry; |
| 53 | import org.projectfloodlight.openflow.protocol.OFGroupDescStatsReply; |
| 54 | import org.projectfloodlight.openflow.protocol.OFGroupStatsEntry; |
| 55 | import org.projectfloodlight.openflow.protocol.OFGroupStatsReply; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 56 | import org.projectfloodlight.openflow.protocol.OFMessage; |
| 57 | import org.projectfloodlight.openflow.protocol.OFPacketIn; |
Marc De Leenheer | 631ffce | 2014-10-28 16:29:07 -0700 | [diff] [blame] | 58 | import org.projectfloodlight.openflow.protocol.OFPortDesc; |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 59 | import org.projectfloodlight.openflow.protocol.OFPortStatsEntry; |
| 60 | import org.projectfloodlight.openflow.protocol.OFPortStatsReply; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 61 | import org.projectfloodlight.openflow.protocol.OFPortStatus; |
Ayaka Koshibe | 38594c2 | 2014-10-22 13:36:12 -0700 | [diff] [blame] | 62 | import org.projectfloodlight.openflow.protocol.OFStatsReply; |
alshabib | 64def64 | 2014-12-02 23:27:37 -0800 | [diff] [blame] | 63 | import org.projectfloodlight.openflow.protocol.OFStatsReplyFlags; |
Jian Li | 2266bff | 2016-04-21 11:01:25 -0700 | [diff] [blame] | 64 | import org.projectfloodlight.openflow.protocol.OFTableStatsEntry; |
| 65 | import org.projectfloodlight.openflow.protocol.OFTableStatsReply; |
Marc De Leenheer | b931137 | 2015-07-09 11:36:49 -0700 | [diff] [blame] | 66 | import org.projectfloodlight.openflow.protocol.action.OFActionOutput; |
| 67 | import org.projectfloodlight.openflow.protocol.instruction.OFInstruction; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 68 | import org.slf4j.Logger; |
| 69 | import org.slf4j.LoggerFactory; |
| 70 | |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 71 | import java.util.Collection; |
Marc De Leenheer | b931137 | 2015-07-09 11:36:49 -0700 | [diff] [blame] | 72 | import java.util.Collections; |
Marc De Leenheer | b931137 | 2015-07-09 11:36:49 -0700 | [diff] [blame] | 73 | import java.util.LinkedList; |
| 74 | import java.util.List; |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 75 | import java.util.Set; |
| 76 | import java.util.concurrent.ConcurrentHashMap; |
HIGUCHI Yuta | 1979f55 | 2015-12-28 21:24:26 -0800 | [diff] [blame] | 77 | import java.util.concurrent.ConcurrentMap; |
Jonathan Hart | 6d44d19 | 2015-05-11 18:01:19 -0700 | [diff] [blame] | 78 | import java.util.concurrent.CopyOnWriteArraySet; |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 79 | import java.util.concurrent.ExecutorService; |
| 80 | import java.util.concurrent.Executors; |
| 81 | import java.util.concurrent.locks.Lock; |
| 82 | import java.util.concurrent.locks.ReentrantLock; |
Jian Li | 2266bff | 2016-04-21 11:01:25 -0700 | [diff] [blame] | 83 | |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 84 | import static org.onlab.util.Tools.groupedThreads; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 85 | |
| 86 | @Component(immediate = true) |
| 87 | @Service |
| 88 | public class OpenFlowControllerImpl implements OpenFlowController { |
Charles Chan | ecfdfb7 | 2015-11-24 19:05:50 -0800 | [diff] [blame] | 89 | private static final String APP_ID = "org.onosproject.openflow-base"; |
Brian O'Connor | ff27850 | 2015-09-22 14:49:52 -0700 | [diff] [blame] | 90 | private static final String DEFAULT_OFPORT = "6633,6653"; |
Charles Chan | 3b00e1b | 2015-08-26 23:12:52 +0800 | [diff] [blame] | 91 | private static final int DEFAULT_WORKER_THREADS = 16; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 92 | |
| 93 | private static final Logger log = |
| 94 | LoggerFactory.getLogger(OpenFlowControllerImpl.class); |
| 95 | |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 96 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Charles Chan | ecfdfb7 | 2015-11-24 19:05:50 -0800 | [diff] [blame] | 97 | protected CoreService coreService; |
| 98 | |
| 99 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 100 | protected DriverService driverService; |
| 101 | |
Thomas Vachuska | 3358af2 | 2015-05-19 18:40:34 -0700 | [diff] [blame] | 102 | // References exists merely for sequencing purpose to assure drivers are loaded |
| 103 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 104 | protected DefaultDriverProviderService defaultDriverProviderService; |
| 105 | |
Charles Chan | 3b00e1b | 2015-08-26 23:12:52 +0800 | [diff] [blame] | 106 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 107 | protected ComponentConfigService cfgService; |
| 108 | |
Brian O'Connor | e2a399e | 2015-09-22 15:32:50 -0700 | [diff] [blame] | 109 | @Property(name = "openflowPorts", value = DEFAULT_OFPORT, |
| 110 | label = "Port numbers (comma separated) used by OpenFlow protocol; default is 6633,6653") |
| 111 | private String openflowPorts = DEFAULT_OFPORT; |
Charles Chan | 3b00e1b | 2015-08-26 23:12:52 +0800 | [diff] [blame] | 112 | |
| 113 | @Property(name = "workerThreads", intValue = DEFAULT_WORKER_THREADS, |
| 114 | label = "Number of controller worker threads; default is 16") |
| 115 | private int workerThreads = DEFAULT_WORKER_THREADS; |
| 116 | |
Ray Milkey | 7ec0d1b | 2015-11-13 08:51:35 -0800 | [diff] [blame] | 117 | protected ExecutorService executorMsgs = |
Andrea Campanella | dda9356 | 2016-03-02 11:08:12 -0800 | [diff] [blame] | 118 | Executors.newFixedThreadPool(32, groupedThreads("onos/of", "event-stats-%d", log)); |
Pavlin Radoslavov | 369c643 | 2014-12-03 16:25:14 -0800 | [diff] [blame] | 119 | |
| 120 | private final ExecutorService executorBarrier = |
Andrea Campanella | dda9356 | 2016-03-02 11:08:12 -0800 | [diff] [blame] | 121 | Executors.newFixedThreadPool(4, groupedThreads("onos/of", "event-barrier-%d", log)); |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 122 | |
HIGUCHI Yuta | 1979f55 | 2015-12-28 21:24:26 -0800 | [diff] [blame] | 123 | protected ConcurrentMap<Dpid, OpenFlowSwitch> connectedSwitches = |
Jonathan Hart | 6d44d19 | 2015-05-11 18:01:19 -0700 | [diff] [blame] | 124 | new ConcurrentHashMap<>(); |
HIGUCHI Yuta | 1979f55 | 2015-12-28 21:24:26 -0800 | [diff] [blame] | 125 | protected ConcurrentMap<Dpid, OpenFlowSwitch> activeMasterSwitches = |
Jonathan Hart | 6d44d19 | 2015-05-11 18:01:19 -0700 | [diff] [blame] | 126 | new ConcurrentHashMap<>(); |
HIGUCHI Yuta | 1979f55 | 2015-12-28 21:24:26 -0800 | [diff] [blame] | 127 | protected ConcurrentMap<Dpid, OpenFlowSwitch> activeEqualSwitches = |
Jonathan Hart | 6d44d19 | 2015-05-11 18:01:19 -0700 | [diff] [blame] | 128 | new ConcurrentHashMap<>(); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 129 | |
| 130 | protected OpenFlowSwitchAgent agent = new OpenFlowSwitchAgent(); |
Jonathan Hart | 6d44d19 | 2015-05-11 18:01:19 -0700 | [diff] [blame] | 131 | protected Set<OpenFlowSwitchListener> ofSwitchListener = new CopyOnWriteArraySet<>(); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 132 | |
| 133 | protected Multimap<Integer, PacketListener> ofPacketListener = |
| 134 | ArrayListMultimap.create(); |
| 135 | |
Jonathan Hart | 6d44d19 | 2015-05-11 18:01:19 -0700 | [diff] [blame] | 136 | protected Set<OpenFlowEventListener> ofEventListener = new CopyOnWriteArraySet<>(); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 137 | |
Jian Li | a78cdb2 | 2016-04-21 13:03:58 -0700 | [diff] [blame^] | 138 | protected Set<OpenFlowMessageListener> ofMessageListener = new CopyOnWriteArraySet<>(); |
Jian Li | 28247b5 | 2016-01-07 17:24:15 -0800 | [diff] [blame] | 139 | |
sangho | 6a0bb17 | 2015-02-05 12:24:48 -0800 | [diff] [blame] | 140 | protected Multimap<Dpid, OFFlowStatsEntry> fullFlowStats = |
| 141 | ArrayListMultimap.create(); |
| 142 | |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 143 | protected Multimap<Dpid, OFTableStatsEntry> fullTableStats = |
| 144 | ArrayListMultimap.create(); |
| 145 | |
sangho | 6a0bb17 | 2015-02-05 12:24:48 -0800 | [diff] [blame] | 146 | protected Multimap<Dpid, OFGroupStatsEntry> fullGroupStats = |
| 147 | ArrayListMultimap.create(); |
| 148 | |
| 149 | protected Multimap<Dpid, OFGroupDescStatsEntry> fullGroupDescStats = |
alshabib | 64def64 | 2014-12-02 23:27:37 -0800 | [diff] [blame] | 150 | ArrayListMultimap.create(); |
| 151 | |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 152 | protected Multimap<Dpid, OFPortStatsEntry> fullPortStats = |
| 153 | ArrayListMultimap.create(); |
| 154 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 155 | private final Controller ctrl = new Controller(); |
| 156 | |
| 157 | @Activate |
Jonathan Hart | bbd91d4 | 2015-02-27 11:18:04 -0800 | [diff] [blame] | 158 | public void activate(ComponentContext context) { |
Andrea Campanella | 3556f36 | 2016-04-28 15:18:10 -0700 | [diff] [blame] | 159 | coreService.registerApplication(APP_ID, this::cleanup); |
Charles Chan | 3b00e1b | 2015-08-26 23:12:52 +0800 | [diff] [blame] | 160 | cfgService.registerProperties(getClass()); |
Brian O'Connor | ff27850 | 2015-09-22 14:49:52 -0700 | [diff] [blame] | 161 | ctrl.setConfigParams(context.getProperties()); |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 162 | ctrl.start(agent, driverService); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Andrea Campanella | 3556f36 | 2016-04-28 15:18:10 -0700 | [diff] [blame] | 165 | private void cleanup() { |
| 166 | // Close listening channel and all OF channels. Clean information about switches |
| 167 | // before deactivating |
Charles Chan | ecfdfb7 | 2015-11-24 19:05:50 -0800 | [diff] [blame] | 168 | ctrl.stop(); |
| 169 | connectedSwitches.values().forEach(OpenFlowSwitch::disconnectSwitch); |
Andrea Campanella | 3556f36 | 2016-04-28 15:18:10 -0700 | [diff] [blame] | 170 | connectedSwitches.clear(); |
| 171 | activeMasterSwitches.clear(); |
| 172 | activeEqualSwitches.clear(); |
Charles Chan | ecfdfb7 | 2015-11-24 19:05:50 -0800 | [diff] [blame] | 173 | } |
| 174 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 175 | @Deactivate |
| 176 | public void deactivate() { |
Andrea Campanella | 3556f36 | 2016-04-28 15:18:10 -0700 | [diff] [blame] | 177 | if (!connectedSwitches.isEmpty()) { |
| 178 | cleanup(); |
| 179 | } |
Charles Chan | 3b00e1b | 2015-08-26 23:12:52 +0800 | [diff] [blame] | 180 | cfgService.unregisterProperties(getClass(), false); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Jonathan Hart | bbd91d4 | 2015-02-27 11:18:04 -0800 | [diff] [blame] | 183 | @Modified |
| 184 | public void modified(ComponentContext context) { |
Charles Chan | 3b00e1b | 2015-08-26 23:12:52 +0800 | [diff] [blame] | 185 | ctrl.stop(); |
Brian O'Connor | ff27850 | 2015-09-22 14:49:52 -0700 | [diff] [blame] | 186 | ctrl.setConfigParams(context.getProperties()); |
Charles Chan | 3b00e1b | 2015-08-26 23:12:52 +0800 | [diff] [blame] | 187 | ctrl.start(agent, driverService); |
Jonathan Hart | bbd91d4 | 2015-02-27 11:18:04 -0800 | [diff] [blame] | 188 | } |
| 189 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 190 | @Override |
| 191 | public Iterable<OpenFlowSwitch> getSwitches() { |
| 192 | return connectedSwitches.values(); |
| 193 | } |
| 194 | |
| 195 | @Override |
| 196 | public Iterable<OpenFlowSwitch> getMasterSwitches() { |
| 197 | return activeMasterSwitches.values(); |
| 198 | } |
| 199 | |
| 200 | @Override |
| 201 | public Iterable<OpenFlowSwitch> getEqualSwitches() { |
| 202 | return activeEqualSwitches.values(); |
| 203 | } |
| 204 | |
| 205 | @Override |
| 206 | public OpenFlowSwitch getSwitch(Dpid dpid) { |
| 207 | return connectedSwitches.get(dpid); |
| 208 | } |
| 209 | |
| 210 | @Override |
| 211 | public OpenFlowSwitch getMasterSwitch(Dpid dpid) { |
| 212 | return activeMasterSwitches.get(dpid); |
| 213 | } |
| 214 | |
| 215 | @Override |
| 216 | public OpenFlowSwitch getEqualSwitch(Dpid dpid) { |
| 217 | return activeEqualSwitches.get(dpid); |
| 218 | } |
| 219 | |
| 220 | @Override |
| 221 | public void addListener(OpenFlowSwitchListener listener) { |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 222 | if (!ofSwitchListener.contains(listener)) { |
| 223 | this.ofSwitchListener.add(listener); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 224 | } |
| 225 | } |
| 226 | |
| 227 | @Override |
| 228 | public void removeListener(OpenFlowSwitchListener listener) { |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 229 | this.ofSwitchListener.remove(listener); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | @Override |
Jian Li | a78cdb2 | 2016-04-21 13:03:58 -0700 | [diff] [blame^] | 233 | public void addMessageListener(OpenFlowMessageListener listener) { |
| 234 | ofMessageListener.add(listener); |
| 235 | } |
| 236 | |
| 237 | @Override |
| 238 | public void removeMessageListener(OpenFlowMessageListener listener) { |
| 239 | ofMessageListener.remove(listener); |
| 240 | } |
| 241 | |
| 242 | @Override |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 243 | public void addPacketListener(int priority, PacketListener listener) { |
| 244 | ofPacketListener.put(priority, listener); |
| 245 | } |
| 246 | |
| 247 | @Override |
| 248 | public void removePacketListener(PacketListener listener) { |
| 249 | ofPacketListener.values().remove(listener); |
| 250 | } |
| 251 | |
| 252 | @Override |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 253 | public void addEventListener(OpenFlowEventListener listener) { |
| 254 | ofEventListener.add(listener); |
| 255 | } |
| 256 | |
| 257 | @Override |
| 258 | public void removeEventListener(OpenFlowEventListener listener) { |
| 259 | ofEventListener.remove(listener); |
| 260 | } |
| 261 | |
| 262 | @Override |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 263 | public void write(Dpid dpid, OFMessage msg) { |
| 264 | this.getSwitch(dpid).sendMsg(msg); |
| 265 | } |
| 266 | |
| 267 | @Override |
| 268 | public void processPacket(Dpid dpid, OFMessage msg) { |
sangho | 6a0bb17 | 2015-02-05 12:24:48 -0800 | [diff] [blame] | 269 | Collection<OFFlowStatsEntry> flowStats; |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 270 | Collection<OFTableStatsEntry> tableStats; |
sangho | 6a0bb17 | 2015-02-05 12:24:48 -0800 | [diff] [blame] | 271 | Collection<OFGroupStatsEntry> groupStats; |
| 272 | Collection<OFGroupDescStatsEntry> groupDescStats; |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 273 | Collection<OFPortStatsEntry> portStats; |
sangho | 6a0bb17 | 2015-02-05 12:24:48 -0800 | [diff] [blame] | 274 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 275 | switch (msg.getType()) { |
| 276 | case PORT_STATUS: |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 277 | for (OpenFlowSwitchListener l : ofSwitchListener) { |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 278 | l.portChanged(dpid, (OFPortStatus) msg); |
| 279 | } |
| 280 | break; |
Ayaka Koshibe | 38594c2 | 2014-10-22 13:36:12 -0700 | [diff] [blame] | 281 | case FEATURES_REPLY: |
| 282 | for (OpenFlowSwitchListener l : ofSwitchListener) { |
| 283 | l.switchChanged(dpid); |
| 284 | } |
| 285 | break; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 286 | case PACKET_IN: |
| 287 | OpenFlowPacketContext pktCtx = DefaultOpenFlowPacketContext |
| 288 | .packetContextFromPacketIn(this.getSwitch(dpid), |
| 289 | (OFPacketIn) msg); |
| 290 | for (PacketListener p : ofPacketListener.values()) { |
| 291 | p.handlePacket(pktCtx); |
| 292 | } |
| 293 | break; |
Pavlin Radoslavov | 369c643 | 2014-12-03 16:25:14 -0800 | [diff] [blame] | 294 | // TODO: Consider using separate threadpool for sensitive messages. |
| 295 | // ie. Back to back error could cause us to starve. |
| 296 | case FLOW_REMOVED: |
| 297 | case ERROR: |
Andrea Campanella | dda9356 | 2016-03-02 11:08:12 -0800 | [diff] [blame] | 298 | executorMsgs.execute(new OFMessageHandler(dpid, msg)); |
Pavlin Radoslavov | 369c643 | 2014-12-03 16:25:14 -0800 | [diff] [blame] | 299 | break; |
Ayaka Koshibe | 38594c2 | 2014-10-22 13:36:12 -0700 | [diff] [blame] | 300 | case STATS_REPLY: |
| 301 | OFStatsReply reply = (OFStatsReply) msg; |
sangho | 6a0bb17 | 2015-02-05 12:24:48 -0800 | [diff] [blame] | 302 | switch (reply.getStatsType()) { |
| 303 | case PORT_DESC: |
| 304 | for (OpenFlowSwitchListener l : ofSwitchListener) { |
| 305 | l.switchChanged(dpid); |
| 306 | } |
| 307 | break; |
| 308 | case FLOW: |
| 309 | flowStats = publishFlowStats(dpid, (OFFlowStatsReply) reply); |
| 310 | if (flowStats != null) { |
| 311 | OFFlowStatsReply.Builder rep = |
| 312 | OFFactories.getFactory(msg.getVersion()).buildFlowStatsReply(); |
| 313 | rep.setEntries(Lists.newLinkedList(flowStats)); |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 314 | rep.setXid(reply.getXid()); |
Andrea Campanella | dda9356 | 2016-03-02 11:08:12 -0800 | [diff] [blame] | 315 | executorMsgs.execute(new OFMessageHandler(dpid, rep.build())); |
sangho | 6a0bb17 | 2015-02-05 12:24:48 -0800 | [diff] [blame] | 316 | } |
| 317 | break; |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 318 | case TABLE: |
| 319 | tableStats = publishTableStats(dpid, (OFTableStatsReply) reply); |
| 320 | if (tableStats != null) { |
| 321 | OFTableStatsReply.Builder rep = |
| 322 | OFFactories.getFactory(msg.getVersion()).buildTableStatsReply(); |
| 323 | rep.setEntries(Lists.newLinkedList(tableStats)); |
Andrea Campanella | dda9356 | 2016-03-02 11:08:12 -0800 | [diff] [blame] | 324 | executorMsgs.execute(new OFMessageHandler(dpid, rep.build())); |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 325 | } |
| 326 | break; |
sangho | 6a0bb17 | 2015-02-05 12:24:48 -0800 | [diff] [blame] | 327 | case GROUP: |
| 328 | groupStats = publishGroupStats(dpid, (OFGroupStatsReply) reply); |
| 329 | if (groupStats != null) { |
| 330 | OFGroupStatsReply.Builder rep = |
| 331 | OFFactories.getFactory(msg.getVersion()).buildGroupStatsReply(); |
| 332 | rep.setEntries(Lists.newLinkedList(groupStats)); |
| 333 | rep.setXid(reply.getXid()); |
Andrea Campanella | dda9356 | 2016-03-02 11:08:12 -0800 | [diff] [blame] | 334 | executorMsgs.execute(new OFMessageHandler(dpid, rep.build())); |
sangho | 6a0bb17 | 2015-02-05 12:24:48 -0800 | [diff] [blame] | 335 | } |
| 336 | break; |
| 337 | case GROUP_DESC: |
| 338 | groupDescStats = publishGroupDescStats(dpid, |
| 339 | (OFGroupDescStatsReply) reply); |
| 340 | if (groupDescStats != null) { |
| 341 | OFGroupDescStatsReply.Builder rep = |
Marc De Leenheer | b931137 | 2015-07-09 11:36:49 -0700 | [diff] [blame] | 342 | OFFactories.getFactory(msg.getVersion()).buildGroupDescStatsReply(); |
sangho | 6a0bb17 | 2015-02-05 12:24:48 -0800 | [diff] [blame] | 343 | rep.setEntries(Lists.newLinkedList(groupDescStats)); |
| 344 | rep.setXid(reply.getXid()); |
Andrea Campanella | dda9356 | 2016-03-02 11:08:12 -0800 | [diff] [blame] | 345 | executorMsgs.execute(new OFMessageHandler(dpid, rep.build())); |
sangho | 6a0bb17 | 2015-02-05 12:24:48 -0800 | [diff] [blame] | 346 | } |
| 347 | break; |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 348 | case PORT: |
Andrea Campanella | dda9356 | 2016-03-02 11:08:12 -0800 | [diff] [blame] | 349 | executorMsgs.execute(new OFMessageHandler(dpid, reply)); |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 350 | break; |
alshabib | 5eb7939 | 2015-08-19 18:09:55 -0700 | [diff] [blame] | 351 | case METER: |
Andrea Campanella | dda9356 | 2016-03-02 11:08:12 -0800 | [diff] [blame] | 352 | executorMsgs.execute(new OFMessageHandler(dpid, reply)); |
alshabib | 5eb7939 | 2015-08-19 18:09:55 -0700 | [diff] [blame] | 353 | break; |
Marc De Leenheer | b931137 | 2015-07-09 11:36:49 -0700 | [diff] [blame] | 354 | case EXPERIMENTER: |
| 355 | if (reply instanceof OFCalientFlowStatsReply) { |
| 356 | // Convert Calient flow statistics to regular flow stats |
| 357 | // TODO: parse remaining fields such as power levels etc. when we have proper monitoring API |
| 358 | OFFlowStatsReply.Builder fsr = getSwitch(dpid).factory().buildFlowStatsReply(); |
| 359 | List<OFFlowStatsEntry> entries = new LinkedList<>(); |
| 360 | for (OFCalientFlowStatsEntry entry : ((OFCalientFlowStatsReply) msg).getEntries()) { |
| 361 | |
| 362 | // Single instruction, i.e., output to port |
| 363 | OFActionOutput action = OFFactories |
| 364 | .getFactory(msg.getVersion()) |
| 365 | .actions() |
| 366 | .buildOutput() |
| 367 | .setPort(entry.getOutPort()) |
| 368 | .build(); |
| 369 | OFInstruction instruction = OFFactories |
| 370 | .getFactory(msg.getVersion()) |
| 371 | .instructions() |
| 372 | .applyActions(Collections.singletonList(action)); |
| 373 | OFFlowStatsEntry fs = getSwitch(dpid).factory().buildFlowStatsEntry() |
| 374 | .setMatch(entry.getMatch()) |
| 375 | .setTableId(entry.getTableId()) |
| 376 | .setDurationSec(entry.getDurationSec()) |
| 377 | .setDurationNsec(entry.getDurationNsec()) |
| 378 | .setPriority(entry.getPriority()) |
| 379 | .setIdleTimeout(entry.getIdleTimeout()) |
| 380 | .setHardTimeout(entry.getHardTimeout()) |
| 381 | .setFlags(entry.getFlags()) |
| 382 | .setCookie(entry.getCookie()) |
| 383 | .setInstructions(Collections.singletonList(instruction)) |
| 384 | .build(); |
| 385 | entries.add(fs); |
| 386 | } |
| 387 | fsr.setEntries(entries); |
| 388 | |
| 389 | flowStats = publishFlowStats(dpid, fsr.build()); |
| 390 | if (flowStats != null) { |
| 391 | OFFlowStatsReply.Builder rep = |
| 392 | OFFactories.getFactory(msg.getVersion()).buildFlowStatsReply(); |
| 393 | rep.setEntries(Lists.newLinkedList(flowStats)); |
Andrea Campanella | dda9356 | 2016-03-02 11:08:12 -0800 | [diff] [blame] | 394 | executorMsgs.execute(new OFMessageHandler(dpid, rep.build())); |
Marc De Leenheer | b931137 | 2015-07-09 11:36:49 -0700 | [diff] [blame] | 395 | } |
| 396 | } else { |
Andrea Campanella | dda9356 | 2016-03-02 11:08:12 -0800 | [diff] [blame] | 397 | executorMsgs.execute(new OFMessageHandler(dpid, reply)); |
Marc De Leenheer | b931137 | 2015-07-09 11:36:49 -0700 | [diff] [blame] | 398 | } |
| 399 | break; |
sangho | 6a0bb17 | 2015-02-05 12:24:48 -0800 | [diff] [blame] | 400 | default: |
alshabib | 5eb7939 | 2015-08-19 18:09:55 -0700 | [diff] [blame] | 401 | log.warn("Discarding unknown stats reply type {}", reply.getStatsType()); |
Marc De Leenheer | b931137 | 2015-07-09 11:36:49 -0700 | [diff] [blame] | 402 | break; |
alshabib | 64def64 | 2014-12-02 23:27:37 -0800 | [diff] [blame] | 403 | } |
| 404 | break; |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 405 | case BARRIER_REPLY: |
Andrea Campanella | dda9356 | 2016-03-02 11:08:12 -0800 | [diff] [blame] | 406 | executorBarrier.execute(new OFMessageHandler(dpid, msg)); |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 407 | break; |
Marc De Leenheer | 631ffce | 2014-10-28 16:29:07 -0700 | [diff] [blame] | 408 | case EXPERIMENTER: |
Marc De Leenheer | b931137 | 2015-07-09 11:36:49 -0700 | [diff] [blame] | 409 | long experimenter = ((OFExperimenter) msg).getExperimenter(); |
| 410 | if (experimenter == 0x748771) { |
| 411 | // LINC-OE port stats |
Marc De Leenheer | 631ffce | 2014-10-28 16:29:07 -0700 | [diff] [blame] | 412 | OFCircuitPortStatus circuitPortStatus = (OFCircuitPortStatus) msg; |
| 413 | OFPortStatus.Builder portStatus = this.getSwitch(dpid).factory().buildPortStatus(); |
| 414 | OFPortDesc.Builder portDesc = this.getSwitch(dpid).factory().buildPortDesc(); |
| 415 | portDesc.setPortNo(circuitPortStatus.getPortNo()) |
| 416 | .setHwAddr(circuitPortStatus.getHwAddr()) |
| 417 | .setName(circuitPortStatus.getName()) |
| 418 | .setConfig(circuitPortStatus.getConfig()) |
| 419 | .setState(circuitPortStatus.getState()); |
| 420 | portStatus.setReason(circuitPortStatus.getReason()).setDesc(portDesc.build()); |
| 421 | for (OpenFlowSwitchListener l : ofSwitchListener) { |
| 422 | l.portChanged(dpid, portStatus.build()); |
| 423 | } |
| 424 | } else { |
| 425 | log.warn("Handling experimenter type {} not yet implemented", |
| 426 | ((OFExperimenter) msg).getExperimenter(), msg); |
| 427 | } |
| 428 | break; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 429 | default: |
| 430 | log.warn("Handling message type {} not yet implemented {}", |
| 431 | msg.getType(), msg); |
| 432 | } |
| 433 | } |
| 434 | |
sangho | 6a0bb17 | 2015-02-05 12:24:48 -0800 | [diff] [blame] | 435 | private synchronized Collection<OFFlowStatsEntry> publishFlowStats(Dpid dpid, |
| 436 | OFFlowStatsReply reply) { |
alshabib | 64def64 | 2014-12-02 23:27:37 -0800 | [diff] [blame] | 437 | //TODO: Get rid of synchronized |
sangho | 6a0bb17 | 2015-02-05 12:24:48 -0800 | [diff] [blame] | 438 | fullFlowStats.putAll(dpid, reply.getEntries()); |
alshabib | 64def64 | 2014-12-02 23:27:37 -0800 | [diff] [blame] | 439 | if (!reply.getFlags().contains(OFStatsReplyFlags.REPLY_MORE)) { |
sangho | 6a0bb17 | 2015-02-05 12:24:48 -0800 | [diff] [blame] | 440 | return fullFlowStats.removeAll(dpid); |
| 441 | } |
| 442 | return null; |
| 443 | } |
| 444 | |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 445 | private synchronized Collection<OFTableStatsEntry> publishTableStats(Dpid dpid, |
| 446 | OFTableStatsReply reply) { |
| 447 | //TODO: Get rid of synchronized |
| 448 | fullTableStats.putAll(dpid, reply.getEntries()); |
| 449 | if (!reply.getFlags().contains(OFStatsReplyFlags.REPLY_MORE)) { |
| 450 | return fullTableStats.removeAll(dpid); |
| 451 | } |
| 452 | return null; |
| 453 | } |
| 454 | |
sangho | 6a0bb17 | 2015-02-05 12:24:48 -0800 | [diff] [blame] | 455 | private synchronized Collection<OFGroupStatsEntry> publishGroupStats(Dpid dpid, |
| 456 | OFGroupStatsReply reply) { |
| 457 | //TODO: Get rid of synchronized |
| 458 | fullGroupStats.putAll(dpid, reply.getEntries()); |
| 459 | if (!reply.getFlags().contains(OFStatsReplyFlags.REPLY_MORE)) { |
| 460 | return fullGroupStats.removeAll(dpid); |
| 461 | } |
| 462 | return null; |
| 463 | } |
| 464 | |
| 465 | private synchronized Collection<OFGroupDescStatsEntry> publishGroupDescStats(Dpid dpid, |
| 466 | OFGroupDescStatsReply reply) { |
| 467 | //TODO: Get rid of synchronized |
| 468 | fullGroupDescStats.putAll(dpid, reply.getEntries()); |
| 469 | if (!reply.getFlags().contains(OFStatsReplyFlags.REPLY_MORE)) { |
| 470 | return fullGroupDescStats.removeAll(dpid); |
alshabib | 64def64 | 2014-12-02 23:27:37 -0800 | [diff] [blame] | 471 | } |
| 472 | return null; |
| 473 | } |
| 474 | |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 475 | private synchronized Collection<OFPortStatsEntry> publishPortStats(Dpid dpid, |
| 476 | OFPortStatsReply reply) { |
| 477 | fullPortStats.putAll(dpid, reply.getEntries()); |
| 478 | if (!reply.getFlags().contains(OFStatsReplyFlags.REPLY_MORE)) { |
| 479 | return fullPortStats.removeAll(dpid); |
| 480 | } |
| 481 | return null; |
| 482 | } |
| 483 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 484 | @Override |
| 485 | public void setRole(Dpid dpid, RoleState role) { |
Yuta HIGUCHI | 79cbd1c | 2014-10-02 16:57:57 -0700 | [diff] [blame] | 486 | final OpenFlowSwitch sw = getSwitch(dpid); |
| 487 | if (sw == null) { |
| 488 | log.debug("Switch not connected. Ignoring setRole({}, {})", dpid, role); |
| 489 | return; |
| 490 | } |
| 491 | sw.setRole(role); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | /** |
| 495 | * Implementation of an OpenFlow Agent which is responsible for |
| 496 | * keeping track of connected switches and the state in which |
| 497 | * they are. |
| 498 | */ |
| 499 | public class OpenFlowSwitchAgent implements OpenFlowAgent { |
| 500 | |
| 501 | private final Logger log = LoggerFactory.getLogger(OpenFlowSwitchAgent.class); |
| 502 | private final Lock switchLock = new ReentrantLock(); |
| 503 | |
| 504 | @Override |
| 505 | public boolean addConnectedSwitch(Dpid dpid, OpenFlowSwitch sw) { |
alshabib | 9eab22f | 2014-10-20 17:17:31 -0700 | [diff] [blame] | 506 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 507 | if (connectedSwitches.get(dpid) != null) { |
| 508 | log.error("Trying to add connectedSwitch but found a previous " |
| 509 | + "value for dpid: {}", dpid); |
| 510 | return false; |
| 511 | } else { |
Yuta HIGUCHI | eb3f30b | 2014-10-22 11:34:49 -0700 | [diff] [blame] | 512 | log.info("Added switch {}", dpid); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 513 | connectedSwitches.put(dpid, sw); |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 514 | for (OpenFlowSwitchListener l : ofSwitchListener) { |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 515 | l.switchAdded(dpid); |
| 516 | } |
| 517 | return true; |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | @Override |
| 522 | public boolean validActivation(Dpid dpid) { |
| 523 | if (connectedSwitches.get(dpid) == null) { |
| 524 | log.error("Trying to activate switch but is not in " |
| 525 | + "connected switches: dpid {}. Aborting ..", |
| 526 | dpid); |
| 527 | return false; |
| 528 | } |
| 529 | if (activeMasterSwitches.get(dpid) != null || |
| 530 | activeEqualSwitches.get(dpid) != null) { |
| 531 | log.error("Trying to activate switch but it is already " |
| 532 | + "activated: dpid {}. Found in activeMaster: {} " |
Ray Milkey | 6bc43c2 | 2015-11-06 13:22:38 -0800 | [diff] [blame] | 533 | + "Found in activeEqual: {}. Aborting ..", |
| 534 | dpid, |
| 535 | (activeMasterSwitches.get(dpid) == null) ? 'N' : 'Y', |
| 536 | (activeEqualSwitches.get(dpid) == null) ? 'N' : 'Y'); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 537 | return false; |
| 538 | } |
| 539 | return true; |
| 540 | } |
| 541 | |
| 542 | |
| 543 | @Override |
| 544 | public boolean addActivatedMasterSwitch(Dpid dpid, OpenFlowSwitch sw) { |
| 545 | switchLock.lock(); |
| 546 | try { |
| 547 | if (!validActivation(dpid)) { |
| 548 | return false; |
| 549 | } |
| 550 | activeMasterSwitches.put(dpid, sw); |
| 551 | return true; |
| 552 | } finally { |
| 553 | switchLock.unlock(); |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | @Override |
| 558 | public boolean addActivatedEqualSwitch(Dpid dpid, OpenFlowSwitch sw) { |
| 559 | switchLock.lock(); |
| 560 | try { |
| 561 | if (!validActivation(dpid)) { |
| 562 | return false; |
| 563 | } |
| 564 | activeEqualSwitches.put(dpid, sw); |
| 565 | log.info("Added Activated EQUAL Switch {}", dpid); |
| 566 | return true; |
| 567 | } finally { |
| 568 | switchLock.unlock(); |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | @Override |
| 573 | public void transitionToMasterSwitch(Dpid dpid) { |
| 574 | switchLock.lock(); |
| 575 | try { |
| 576 | if (activeMasterSwitches.containsKey(dpid)) { |
| 577 | return; |
| 578 | } |
| 579 | OpenFlowSwitch sw = activeEqualSwitches.remove(dpid); |
| 580 | if (sw == null) { |
| 581 | sw = getSwitch(dpid); |
| 582 | if (sw == null) { |
| 583 | log.error("Transition to master called on sw {}, but switch " |
| 584 | + "was not found in controller-cache", dpid); |
| 585 | return; |
| 586 | } |
| 587 | } |
| 588 | log.info("Transitioned switch {} to MASTER", dpid); |
| 589 | activeMasterSwitches.put(dpid, sw); |
| 590 | } finally { |
| 591 | switchLock.unlock(); |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | |
| 596 | @Override |
| 597 | public void transitionToEqualSwitch(Dpid dpid) { |
| 598 | switchLock.lock(); |
| 599 | try { |
| 600 | if (activeEqualSwitches.containsKey(dpid)) { |
| 601 | return; |
| 602 | } |
| 603 | OpenFlowSwitch sw = activeMasterSwitches.remove(dpid); |
| 604 | if (sw == null) { |
| 605 | sw = getSwitch(dpid); |
| 606 | if (sw == null) { |
| 607 | log.error("Transition to equal called on sw {}, but switch " |
| 608 | + "was not found in controller-cache", dpid); |
| 609 | return; |
| 610 | } |
| 611 | } |
| 612 | log.info("Transitioned switch {} to EQUAL", dpid); |
| 613 | activeEqualSwitches.put(dpid, sw); |
| 614 | } finally { |
| 615 | switchLock.unlock(); |
| 616 | } |
| 617 | |
| 618 | } |
| 619 | |
| 620 | @Override |
| 621 | public void removeConnectedSwitch(Dpid dpid) { |
| 622 | connectedSwitches.remove(dpid); |
| 623 | OpenFlowSwitch sw = activeMasterSwitches.remove(dpid); |
| 624 | if (sw == null) { |
Thomas Vachuska | 3358af2 | 2015-05-19 18:40:34 -0700 | [diff] [blame] | 625 | log.debug("sw was null for {}", dpid); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 626 | sw = activeEqualSwitches.remove(dpid); |
| 627 | } |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 628 | for (OpenFlowSwitchListener l : ofSwitchListener) { |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 629 | l.switchRemoved(dpid); |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | @Override |
Jian Li | a78cdb2 | 2016-04-21 13:03:58 -0700 | [diff] [blame^] | 634 | public void processDownstreamMessage(Dpid dpid, List<OFMessage> m) { |
| 635 | for (OpenFlowMessageListener listener : ofMessageListener) { |
| 636 | listener.handleOutgoingMessage(dpid, m); |
| 637 | } |
| 638 | } |
| 639 | |
| 640 | |
| 641 | @Override |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 642 | public void processMessage(Dpid dpid, OFMessage m) { |
| 643 | processPacket(dpid, m); |
Jian Li | a78cdb2 | 2016-04-21 13:03:58 -0700 | [diff] [blame^] | 644 | |
| 645 | for (OpenFlowMessageListener listener : ofMessageListener) { |
| 646 | listener.handleIncomingMessage(dpid, m); |
| 647 | } |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 648 | } |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 649 | |
| 650 | @Override |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 651 | public void returnRoleReply(Dpid dpid, RoleState requested, RoleState response) { |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 652 | for (OpenFlowSwitchListener l : ofSwitchListener) { |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 653 | l.receivedRoleReply(dpid, requested, response); |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 654 | } |
| 655 | } |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 656 | } |
| 657 | |
Jian Li | 152b885 | 2015-12-07 14:47:25 -0800 | [diff] [blame] | 658 | /** |
Jian Li | 2266bff | 2016-04-21 11:01:25 -0700 | [diff] [blame] | 659 | * OpenFlow message handler. |
Jian Li | 152b885 | 2015-12-07 14:47:25 -0800 | [diff] [blame] | 660 | */ |
Ray Milkey | 7ec0d1b | 2015-11-13 08:51:35 -0800 | [diff] [blame] | 661 | protected final class OFMessageHandler implements Runnable { |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 662 | |
Ray Milkey | 7ec0d1b | 2015-11-13 08:51:35 -0800 | [diff] [blame] | 663 | protected final OFMessage msg; |
| 664 | protected final Dpid dpid; |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 665 | |
| 666 | public OFMessageHandler(Dpid dpid, OFMessage msg) { |
| 667 | this.msg = msg; |
| 668 | this.dpid = dpid; |
| 669 | } |
| 670 | |
| 671 | @Override |
| 672 | public void run() { |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 673 | for (OpenFlowEventListener listener : ofEventListener) { |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 674 | listener.handleMessage(dpid, msg); |
| 675 | } |
| 676 | } |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 677 | } |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 678 | } |