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