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