blob: 77595502d8fd542c30d063c985ad9e52e2132289 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
2 * Copyright 2014 Open Networking Laboratory
3 *
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
7 *
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.
15 */
tombe988312014-09-19 18:38:47 -070016package org.onlab.onos.net.device.impl;
tomd3097b02014-08-26 10:40:29 -070017
alshabib339a3d92014-09-26 17:54:32 -070018import static com.google.common.base.Preconditions.checkNotNull;
19import static org.onlab.onos.net.device.DeviceEvent.Type.DEVICE_MASTERSHIP_CHANGED;
20import static org.slf4j.LoggerFactory.getLogger;
21
22import java.util.List;
23
tomd3097b02014-08-26 10:40:29 -070024import org.apache.felix.scr.annotations.Activate;
25import org.apache.felix.scr.annotations.Component;
26import org.apache.felix.scr.annotations.Deactivate;
tom5f38b3a2014-08-27 23:50:54 -070027import org.apache.felix.scr.annotations.Reference;
28import org.apache.felix.scr.annotations.ReferenceCardinality;
tomd3097b02014-08-26 10:40:29 -070029import org.apache.felix.scr.annotations.Service;
tomb41d1ac2014-09-24 01:51:24 -070030import org.onlab.onos.cluster.ClusterService;
Yuta HIGUCHI8e939d82014-10-08 08:09:18 -070031import org.onlab.onos.cluster.NodeId;
Ayaka Koshibe317245a2014-10-29 00:34:43 -070032import org.onlab.onos.cluster.RoleInfo;
tom96dfcab2014-08-28 09:26:03 -070033import org.onlab.onos.event.AbstractListenerRegistry;
34import org.onlab.onos.event.EventDeliveryService;
Yuta HIGUCHI80912e62014-10-12 00:15:47 -070035import org.onlab.onos.mastership.MastershipEvent;
36import org.onlab.onos.mastership.MastershipListener;
37import org.onlab.onos.mastership.MastershipService;
38import org.onlab.onos.mastership.MastershipTerm;
39import org.onlab.onos.mastership.MastershipTermService;
tom32f66842014-08-27 19:27:47 -070040import org.onlab.onos.net.Device;
tomd3097b02014-08-26 10:40:29 -070041import org.onlab.onos.net.DeviceId;
42import org.onlab.onos.net.MastershipRole;
tom32f66842014-08-27 19:27:47 -070043import org.onlab.onos.net.Port;
44import org.onlab.onos.net.PortNumber;
Ayaka Koshibeea5b4ce2014-10-11 14:17:17 -070045import org.onlab.onos.net.device.DefaultDeviceDescription;
tome5ec3fd2014-09-04 15:18:06 -070046import org.onlab.onos.net.device.DeviceAdminService;
Yuta HIGUCHI093e83e2014-10-10 22:26:11 -070047import org.onlab.onos.net.device.DeviceClockProviderService;
tomd3097b02014-08-26 10:40:29 -070048import org.onlab.onos.net.device.DeviceDescription;
tom32f66842014-08-27 19:27:47 -070049import org.onlab.onos.net.device.DeviceEvent;
50import org.onlab.onos.net.device.DeviceListener;
tomd3097b02014-08-26 10:40:29 -070051import org.onlab.onos.net.device.DeviceProvider;
tom96dfcab2014-08-28 09:26:03 -070052import org.onlab.onos.net.device.DeviceProviderRegistry;
tomd3097b02014-08-26 10:40:29 -070053import org.onlab.onos.net.device.DeviceProviderService;
tom32f66842014-08-27 19:27:47 -070054import org.onlab.onos.net.device.DeviceService;
tom41a2c5f2014-09-19 09:20:35 -070055import org.onlab.onos.net.device.DeviceStore;
tomf80c9722014-09-24 14:49:18 -070056import org.onlab.onos.net.device.DeviceStoreDelegate;
tomd3097b02014-08-26 10:40:29 -070057import org.onlab.onos.net.device.PortDescription;
tom96dfcab2014-08-28 09:26:03 -070058import org.onlab.onos.net.provider.AbstractProviderRegistry;
tomd3097b02014-08-26 10:40:29 -070059import org.onlab.onos.net.provider.AbstractProviderService;
60import org.slf4j.Logger;
tomd3097b02014-08-26 10:40:29 -070061
Ayaka Koshibe317245a2014-10-29 00:34:43 -070062import com.google.common.collect.HashMultimap;
63
tomd3097b02014-08-26 10:40:29 -070064/**
tome4729872014-09-23 00:37:37 -070065 * Provides implementation of the device SB & NB APIs.
tomd3097b02014-08-26 10:40:29 -070066 */
67@Component(immediate = true)
68@Service
tom41a2c5f2014-09-19 09:20:35 -070069public class DeviceManager
Thomas Vachuskad16ce182014-10-29 17:25:29 -070070 extends AbstractProviderRegistry<DeviceProvider, DeviceProviderService>
71 implements DeviceService, DeviceAdminService, DeviceProviderRegistry {
tom32f66842014-08-27 19:27:47 -070072
tome5ec3fd2014-09-04 15:18:06 -070073 private static final String DEVICE_ID_NULL = "Device ID cannot be null";
74 private static final String PORT_NUMBER_NULL = "Port number cannot be null";
75 private static final String DEVICE_DESCRIPTION_NULL = "Device description cannot be null";
76 private static final String PORT_DESCRIPTION_NULL = "Port description cannot be null";
77 private static final String ROLE_NULL = "Role cannot be null";
tomd3097b02014-08-26 10:40:29 -070078
tom5f38b3a2014-08-27 23:50:54 -070079 private final Logger log = getLogger(getClass());
tomd3097b02014-08-26 10:40:29 -070080
alshabib271a6202014-09-28 22:11:21 -070081 protected final AbstractListenerRegistry<DeviceEvent, DeviceListener> listenerRegistry =
82 new AbstractListenerRegistry<>();
tom32f66842014-08-27 19:27:47 -070083
alshabib339a3d92014-09-26 17:54:32 -070084 private final DeviceStoreDelegate delegate = new InternalStoreDelegate();
tomf80c9722014-09-24 14:49:18 -070085
tomc78acee2014-09-24 15:16:55 -070086 private final MastershipListener mastershipListener = new InternalMastershipListener();
tomb41d1ac2014-09-24 01:51:24 -070087
tom41a2c5f2014-09-19 09:20:35 -070088 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
89 protected DeviceStore store;
tomd3097b02014-08-26 10:40:29 -070090
tom5f38b3a2014-08-27 23:50:54 -070091 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
tome5ec3fd2014-09-04 15:18:06 -070092 protected EventDeliveryService eventDispatcher;
tom5f38b3a2014-08-27 23:50:54 -070093
Ayaka Koshibea7f044e2014-09-23 16:56:20 -070094 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
tomb41d1ac2014-09-24 01:51:24 -070095 protected ClusterService clusterService;
96
97 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Ayaka Koshibea7f044e2014-09-23 16:56:20 -070098 protected MastershipService mastershipService;
99
Ayaka Koshibe3de43ca2014-09-26 16:40:23 -0700100 protected MastershipTermService termService;
101
Madan Jampani61056bc2014-09-27 09:07:26 -0700102 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Yuta HIGUCHI093e83e2014-10-10 22:26:11 -0700103 protected DeviceClockProviderService deviceClockProviderService;
Madan Jampani61056bc2014-09-27 09:07:26 -0700104
tomd3097b02014-08-26 10:40:29 -0700105 @Activate
106 public void activate() {
tomf80c9722014-09-24 14:49:18 -0700107 store.setDelegate(delegate);
tom96dfcab2014-08-28 09:26:03 -0700108 eventDispatcher.addSink(DeviceEvent.class, listenerRegistry);
tomb41d1ac2014-09-24 01:51:24 -0700109 mastershipService.addListener(mastershipListener);
Ayaka Koshibe3de43ca2014-09-26 16:40:23 -0700110 termService = mastershipService.requestTermService();
tomd3097b02014-08-26 10:40:29 -0700111 log.info("Started");
112 }
113
114 @Deactivate
115 public void deactivate() {
tomf80c9722014-09-24 14:49:18 -0700116 store.unsetDelegate(delegate);
tomb41d1ac2014-09-24 01:51:24 -0700117 mastershipService.removeListener(mastershipListener);
tom5f38b3a2014-08-27 23:50:54 -0700118 eventDispatcher.removeSink(DeviceEvent.class);
tomd3097b02014-08-26 10:40:29 -0700119 log.info("Stopped");
120 }
121
122 @Override
tomad2d2092014-09-06 23:24:20 -0700123 public int getDeviceCount() {
124 return store.getDeviceCount();
tomd3097b02014-08-26 10:40:29 -0700125 }
126
127 @Override
tom32f66842014-08-27 19:27:47 -0700128 public Iterable<Device> getDevices() {
tome5ec3fd2014-09-04 15:18:06 -0700129 return store.getDevices();
tomd3097b02014-08-26 10:40:29 -0700130 }
131
tom32f66842014-08-27 19:27:47 -0700132 @Override
133 public Device getDevice(DeviceId deviceId) {
134 checkNotNull(deviceId, DEVICE_ID_NULL);
tom132b58a2014-08-28 16:11:28 -0700135 return store.getDevice(deviceId);
tom32f66842014-08-27 19:27:47 -0700136 }
137
138 @Override
tomad2d2092014-09-06 23:24:20 -0700139 public MastershipRole getRole(DeviceId deviceId) {
140 checkNotNull(deviceId, DEVICE_ID_NULL);
tomb41d1ac2014-09-24 01:51:24 -0700141 return mastershipService.getLocalRole(deviceId);
tomad2d2092014-09-06 23:24:20 -0700142 }
143
144 @Override
tom32f66842014-08-27 19:27:47 -0700145 public List<Port> getPorts(DeviceId deviceId) {
146 checkNotNull(deviceId, DEVICE_ID_NULL);
tom132b58a2014-08-28 16:11:28 -0700147 return store.getPorts(deviceId);
tom32f66842014-08-27 19:27:47 -0700148 }
149
150 @Override
151 public Port getPort(DeviceId deviceId, PortNumber portNumber) {
152 checkNotNull(deviceId, DEVICE_ID_NULL);
153 checkNotNull(portNumber, PORT_NUMBER_NULL);
tom132b58a2014-08-28 16:11:28 -0700154 return store.getPort(deviceId, portNumber);
tom32f66842014-08-27 19:27:47 -0700155 }
156
157 @Override
tomff7eb7c2014-09-08 12:49:03 -0700158 public boolean isAvailable(DeviceId deviceId) {
159 checkNotNull(deviceId, DEVICE_ID_NULL);
160 return store.isAvailable(deviceId);
161 }
162
tomb41d1ac2014-09-24 01:51:24 -0700163 // Applies the specified role to the device; ignores NONE
164 private void applyRole(DeviceId deviceId, MastershipRole newRole) {
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700165 if (newRole.equals(MastershipRole.NONE)) {
166 return;
tome5ec3fd2014-09-04 15:18:06 -0700167 }
tome5ec3fd2014-09-04 15:18:06 -0700168
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700169 Device device = store.getDevice(deviceId);
170 // FIXME: Device might not be there yet. (eventual consistent)
171 if (device == null) {
172 return;
173 }
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700174
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700175 DeviceProvider provider = getProvider(device.providerId());
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700176 if (provider != null) {
177 provider.roleChanged(device, newRole);
178 // only trigger event when request was sent to provider
179 // TODO: consider removing this from Device event type?
180 post(new DeviceEvent(DEVICE_MASTERSHIP_CHANGED, device));
181
182 if (newRole.equals(MastershipRole.MASTER)) {
183 provider.triggerProbe(device);
184 }
185 }
186 }
187
188 // Check a device for control channel connectivity.
189 private boolean isReachable(Device device) {
190 // FIXME: Device might not be there yet. (eventual consistent)
191 if (device == null) {
192 return false;
193 }
194 DeviceProvider provider = getProvider(device.providerId());
195 return provider.isReachable(device);
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700196 }
197
tome5ec3fd2014-09-04 15:18:06 -0700198 @Override
199 public void removeDevice(DeviceId deviceId) {
200 checkNotNull(deviceId, DEVICE_ID_NULL);
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700201 // XXX is this intended to apply to the full global topology?
202 // if so, we probably don't want the fact that we aren't
203 // MASTER to get in the way, as it would do now.
Yuta HIGUCHI67dce882014-10-21 21:13:26 -0700204 // FIXME: forward or broadcast and let the Master handler the event.
tome5ec3fd2014-09-04 15:18:06 -0700205 DeviceEvent event = store.removeDevice(deviceId);
tom0efbb1d2014-09-09 11:54:28 -0700206 if (event != null) {
207 log.info("Device {} administratively removed", deviceId);
208 post(event);
209 }
tome5ec3fd2014-09-04 15:18:06 -0700210 }
211
tom7869ad92014-09-09 14:32:08 -0700212 @Override
213 public void addListener(DeviceListener listener) {
214 listenerRegistry.addListener(listener);
215 }
216
217 @Override
218 public void removeListener(DeviceListener listener) {
219 listenerRegistry.removeListener(listener);
220 }
221
222 @Override
alshabibb7b40632014-09-28 21:30:00 -0700223 protected DeviceProviderService createProviderService(
224 DeviceProvider provider) {
tom7869ad92014-09-09 14:32:08 -0700225 return new InternalDeviceProviderService(provider);
226 }
227
tomd3097b02014-08-26 10:40:29 -0700228 // Personalized device provider service issued to the supplied provider.
tomdc361b62014-09-09 20:36:52 -0700229 private class InternalDeviceProviderService
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700230 extends AbstractProviderService<DeviceProvider>
231 implements DeviceProviderService {
tomd3097b02014-08-26 10:40:29 -0700232
tomcfde0622014-09-09 11:02:42 -0700233 InternalDeviceProviderService(DeviceProvider provider) {
tomd3097b02014-08-26 10:40:29 -0700234 super(provider);
235 }
236
237 @Override
alshabibb7b40632014-09-28 21:30:00 -0700238 public void deviceConnected(DeviceId deviceId,
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700239 DeviceDescription deviceDescription) {
tom32f66842014-08-27 19:27:47 -0700240 checkNotNull(deviceId, DEVICE_ID_NULL);
241 checkNotNull(deviceDescription, DEVICE_DESCRIPTION_NULL);
tomeadbb462014-09-07 16:10:19 -0700242 checkValidity();
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700243
244 log.info("Device {} connected", deviceId);
245 // check my Role
246 MastershipRole role = mastershipService.requestRoleFor(deviceId);
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700247 if (role != MastershipRole.MASTER) {
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700248 // TODO: Do we need to explicitly tell the Provider that
249 // this instance is no longer the MASTER? probably not
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700250// Device device = getDevice(deviceId);
251// if (device != null) {
252// // FIXME roleChanged should take DeviceId instead of Device
253// provider().roleChanged(device, role);
254// }
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700255 return;
256 }
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700257 MastershipTerm term = mastershipService.requestTermService()
258 .getMastershipTerm(deviceId);
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700259
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700260 if (!term.master().equals(clusterService.getLocalNode().id())) {
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700261 // lost mastership after requestRole told this instance was MASTER.
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700262 log.info("lost mastership before getting term info.");
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700263 return;
264 }
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700265
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700266 // tell clock provider if this instance is the master
Yuta HIGUCHI093e83e2014-10-10 22:26:11 -0700267 deviceClockProviderService.setMastershipTerm(deviceId, term);
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700268
tome5ec3fd2014-09-04 15:18:06 -0700269 DeviceEvent event = store.createOrUpdateDevice(provider().id(),
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700270 deviceId, deviceDescription);
tom80c0e5e2014-09-08 18:08:58 -0700271
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700272 // If there was a change of any kind, tell the provider
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700273 // that this instance is the master.
274 // Note: event can be null, if mastership was lost between
275 // roleRequest and store update calls.
tom80c0e5e2014-09-08 18:08:58 -0700276 if (event != null) {
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700277 // TODO: Check switch reconnected case. Is it assured that
278 // event will never be null?
279 // Could there be a situation MastershipService told this
280 // instance is the new Master, but
281 // event returned from the store is null?
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700282
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700283 // FIXME: 1st argument should be deviceId, to allow setting
284 // certain roles even if the store returned null.
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700285 log.info("event: {} {}", event.type(), event);
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700286 provider().roleChanged(event.subject(), role);
tom568581d2014-09-08 20:13:36 -0700287 post(event);
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700288 } else {
289 log.info("No event to publish");
tom80c0e5e2014-09-08 18:08:58 -0700290 }
tomd3097b02014-08-26 10:40:29 -0700291 }
292
293 @Override
294 public void deviceDisconnected(DeviceId deviceId) {
tom32f66842014-08-27 19:27:47 -0700295 checkNotNull(deviceId, DEVICE_ID_NULL);
tomeadbb462014-09-07 16:10:19 -0700296 checkValidity();
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700297
Ayaka Koshibed9f693e2014-09-29 18:04:54 -0700298
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700299 DeviceEvent event = null;
300 try {
301 event = store.markOffline(deviceId);
302 } catch (IllegalStateException e) {
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700303 log.warn("Failed to mark {} offline", deviceId);
304 // only the MASTER should be marking off-line in normal cases,
305 // but if I was the last STANDBY connection, etc. and no one else
306 // was there to mark the device offline, this instance may need to
307 // temporarily request for Master Role and mark offline.
308
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700309 //there are times when this node will correctly have mastership, BUT
310 //that isn't reflected in the ClockManager before the device disconnects.
311 //we want to let go of the device anyways, so make sure this happens.
Yuta HIGUCHI0722fb22014-10-19 01:16:33 -0700312
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700313 // FIXME: Store semantics leaking out as IllegalStateException.
314 // Consider revising store API to handle this scenario.
315
316 MastershipRole role = mastershipService.requestRoleFor(deviceId);
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700317 MastershipTerm term = termService.getMastershipTerm(deviceId);
Yuta HIGUCHI0722fb22014-10-19 01:16:33 -0700318 final NodeId myNodeId = clusterService.getLocalNode().id();
319 // TODO: Move this type of check inside device clock manager, etc.
320 if (myNodeId.equals(term.master())) {
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700321 log.info("Marking {} offline", deviceId);
Yuta HIGUCHI0722fb22014-10-19 01:16:33 -0700322 deviceClockProviderService.setMastershipTerm(deviceId, term);
323 event = store.markOffline(deviceId);
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700324 } else {
325 log.error("Failed again marking {} offline. {}", deviceId, role);
Yuta HIGUCHI0722fb22014-10-19 01:16:33 -0700326 }
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700327 } finally {
328 //relinquish master role and ability to be backup.
329 mastershipService.relinquishMastership(deviceId);
330
331 if (event != null) {
332 log.info("Device {} disconnected", deviceId);
333 post(event);
334 }
tom0efbb1d2014-09-09 11:54:28 -0700335 }
tomd3097b02014-08-26 10:40:29 -0700336 }
337
338 @Override
alshabibb7b40632014-09-28 21:30:00 -0700339 public void updatePorts(DeviceId deviceId,
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700340 List<PortDescription> portDescriptions) {
tom32f66842014-08-27 19:27:47 -0700341 checkNotNull(deviceId, DEVICE_ID_NULL);
alshabibb7b40632014-09-28 21:30:00 -0700342 checkNotNull(portDescriptions,
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700343 "Port descriptions list cannot be null");
tomeadbb462014-09-07 16:10:19 -0700344 checkValidity();
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700345
Yuta HIGUCHI5f6739c2014-10-01 14:04:01 -0700346 List<DeviceEvent> events = store.updatePorts(this.provider().id(),
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700347 deviceId, portDescriptions);
tom32f66842014-08-27 19:27:47 -0700348 for (DeviceEvent event : events) {
349 post(event);
350 }
tomd3097b02014-08-26 10:40:29 -0700351 }
352
353 @Override
alshabibb7b40632014-09-28 21:30:00 -0700354 public void portStatusChanged(DeviceId deviceId,
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700355 PortDescription portDescription) {
tom32f66842014-08-27 19:27:47 -0700356 checkNotNull(deviceId, DEVICE_ID_NULL);
357 checkNotNull(portDescription, PORT_DESCRIPTION_NULL);
tomeadbb462014-09-07 16:10:19 -0700358 checkValidity();
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700359
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700360 final DeviceEvent event = store.updatePortStatus(this.provider().id(),
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700361 deviceId, portDescription);
tomff7eb7c2014-09-08 12:49:03 -0700362 if (event != null) {
alshabibb7b40632014-09-28 21:30:00 -0700363 log.info("Device {} port {} status changed", deviceId, event
364 .port().number());
tom0efbb1d2014-09-09 11:54:28 -0700365 post(event);
tomff7eb7c2014-09-08 12:49:03 -0700366 }
tomd3097b02014-08-26 10:40:29 -0700367 }
tom3f2bbd72014-09-24 12:07:58 -0700368
369 @Override
370 public void unableToAssertRole(DeviceId deviceId, MastershipRole role) {
371 // FIXME: implement response to this notification
alshabibb7b40632014-09-28 21:30:00 -0700372 log.warn("Failed to assert role [{}] onto Device {}", role,
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700373 deviceId);
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700374 if (role == MastershipRole.MASTER) {
375 mastershipService.relinquishMastership(deviceId);
Yuta HIGUCHI67dce882014-10-21 21:13:26 -0700376 // TODO: Shouldn't we be triggering event?
377 //final Device device = getDevice(deviceId);
378 //post(new DeviceEvent(DEVICE_MASTERSHIP_CHANGED, device));
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700379 }
tom3f2bbd72014-09-24 12:07:58 -0700380 }
tomd3097b02014-08-26 10:40:29 -0700381 }
tom32f66842014-08-27 19:27:47 -0700382
tomeadbb462014-09-07 16:10:19 -0700383 // Posts the specified event to the local event dispatcher.
tom32f66842014-08-27 19:27:47 -0700384 private void post(DeviceEvent event) {
385 if (event != null && eventDispatcher != null) {
386 eventDispatcher.post(event);
387 }
388 }
389
tomb41d1ac2014-09-24 01:51:24 -0700390 // Intercepts mastership events
Ayaka Koshibec4047702014-10-07 14:43:52 -0700391 private class InternalMastershipListener implements MastershipListener {
392
Ayaka Koshibe317245a2014-10-29 00:34:43 -0700393 // random cache size
394 private final int cacheSize = 5;
395 // temporarily stores term number + events to check for duplicates. A hack.
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700396 private HashMultimap<Integer, RoleInfo> eventCache =
Ayaka Koshibe317245a2014-10-29 00:34:43 -0700397 HashMultimap.create();
398
tomb41d1ac2014-09-24 01:51:24 -0700399 @Override
400 public void event(MastershipEvent event) {
Ayaka Koshibe4c891272014-10-08 17:14:16 -0700401 final DeviceId did = event.subject();
Ayaka Koshibeea5b4ce2014-10-11 14:17:17 -0700402 final NodeId myNodeId = clusterService.getLocalNode().id();
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700403
Yuta HIGUCHI67dce882014-10-21 21:13:26 -0700404 if (myNodeId.equals(event.roleInfo().master())) {
Ayaka Koshibeea5b4ce2014-10-11 14:17:17 -0700405 MastershipTerm term = termService.getMastershipTerm(did);
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700406
Ayaka Koshibe317245a2014-10-29 00:34:43 -0700407 // TODO duplicate suppression should probably occur in the MastershipManager
408 // itself, so listeners that can't deal with duplicates don't have to
409 // so this check themselves.
410 if (checkDuplicate(event.roleInfo(), term.termNumber())) {
411 return;
412 }
413
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700414 if (!myNodeId.equals(term.master())) {
415 // something went wrong in consistency, let go
Yuta HIGUCHI305ec832014-10-24 22:23:17 -0700416 log.warn("Mastership has changed after this event."
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700417 + "Term Service suggests {} for {}", term, did);
Yuta HIGUCHI305ec832014-10-24 22:23:17 -0700418 // FIXME: Is it possible to let go of MASTER role
419 // but remain on STANDBY list?
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700420 mastershipService.relinquishMastership(did);
421 applyRole(did, MastershipRole.STANDBY);
422 return;
Ayaka Koshibec4047702014-10-07 14:43:52 -0700423 }
Ayaka Koshibeea5b4ce2014-10-11 14:17:17 -0700424
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700425 // only set the new term if I am the master
426 deviceClockProviderService.setMastershipTerm(did, term);
427
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700428 // if the device is null here, we are the first master to claim the
429 // device. No worries, the DeviceManager will create one soon.
430 Device device = getDevice(did);
431 if ((device != null) && !isAvailable(did)) {
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700432 if (!isReachable(device)) {
433 log.warn("Device {} has disconnected after this event", did);
434 mastershipService.relinquishMastership(did);
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700435 return;
436 }
Ayaka Koshibeea5b4ce2014-10-11 14:17:17 -0700437 //flag the device as online. Is there a better way to do this?
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700438 DeviceEvent devEvent =
439 store.createOrUpdateDevice(device.providerId(), did,
440 new DefaultDeviceDescription(
441 did.uri(), device.type(), device.manufacturer(),
442 device.hwVersion(), device.swVersion(),
443 device.serialNumber(), device.chassisId()));
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700444 post(devEvent);
Ayaka Koshibeea5b4ce2014-10-11 14:17:17 -0700445 }
Ayaka Koshibeea5b4ce2014-10-11 14:17:17 -0700446 applyRole(did, MastershipRole.MASTER);
Yuta HIGUCHI67dce882014-10-21 21:13:26 -0700447 } else if (event.roleInfo().backups().contains(myNodeId)) {
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700448 if (!isReachable(getDevice(did))) {
449 log.warn("Device {} has disconnected after this event", did);
450 mastershipService.relinquishMastership(did);
Yuta HIGUCHIeb5a0b92014-10-29 15:45:55 -0700451 return;
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700452 }
Ayaka Koshibec4047702014-10-07 14:43:52 -0700453 applyRole(did, MastershipRole.STANDBY);
tomb41d1ac2014-09-24 01:51:24 -0700454 }
455 }
Ayaka Koshibe317245a2014-10-29 00:34:43 -0700456
457 // checks for duplicate event, returning true if one is found.
458 private boolean checkDuplicate(RoleInfo roleInfo, int term) {
Yuta HIGUCHIeb5a0b92014-10-29 15:45:55 -0700459 // turning off duplicate check
460 return false;
461// synchronized (eventCache) {
462// if (eventCache.get(term).contains(roleInfo)) {
463// log.info("duplicate event detected; ignoring");
464// return true;
465// } else {
466// eventCache.put(term, roleInfo);
467// // purge by-term oldest entries to keep the cache size under limit
468// if (eventCache.size() > cacheSize) {
469// eventCache.removeAll(term - cacheSize);
470// }
471// return false;
472// }
473// }
Ayaka Koshibe317245a2014-10-29 00:34:43 -0700474 }
475
tomb41d1ac2014-09-24 01:51:24 -0700476 }
tomf80c9722014-09-24 14:49:18 -0700477
478 // Store delegate to re-post events emitted from the store.
alshabib271a6202014-09-28 22:11:21 -0700479 private class InternalStoreDelegate
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700480 implements DeviceStoreDelegate {
tomf80c9722014-09-24 14:49:18 -0700481 @Override
482 public void notify(DeviceEvent event) {
483 post(event);
484 }
485 }
tomd3097b02014-08-26 10:40:29 -0700486}