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