blob: 32ab007463728a583a000a2e6c510694bea31fe8 [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 HIGUCHId40e9502014-10-30 15:06:32 -0700257 MastershipTerm term = termService.getMastershipTerm(deviceId);
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700258
Yuta HIGUCHIdfe6e3b2014-10-30 11:31:51 -0700259 final NodeId myNodeId = clusterService.getLocalNode().id();
260 if (!myNodeId.equals(term.master())) {
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700261 // lost mastership after requestRole told this instance was MASTER.
Yuta HIGUCHI2d3cd312014-10-31 11:38:04 -0700262 log.info("Role of this node is STANDBY for {}", deviceId);
263 // TODO: Do we need to explicitly tell the Provider that
264 // this instance is no longer the MASTER?
265 //applyRole(deviceId, MastershipRole.STANDBY);
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700266 return;
267 }
Yuta HIGUCHI2d3cd312014-10-31 11:38:04 -0700268 log.info("Role of this node is MASTER for {}", deviceId);
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700269
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700270 // tell clock provider if this instance is the master
Yuta HIGUCHI093e83e2014-10-10 22:26:11 -0700271 deviceClockProviderService.setMastershipTerm(deviceId, term);
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700272
tome5ec3fd2014-09-04 15:18:06 -0700273 DeviceEvent event = store.createOrUpdateDevice(provider().id(),
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700274 deviceId, deviceDescription);
tom80c0e5e2014-09-08 18:08:58 -0700275
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700276 // If there was a change of any kind, tell the provider
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700277 // that this instance is the master.
278 // Note: event can be null, if mastership was lost between
279 // roleRequest and store update calls.
tom80c0e5e2014-09-08 18:08:58 -0700280 if (event != null) {
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700281 // TODO: Check switch reconnected case. Is it assured that
282 // event will never be null?
283 // Could there be a situation MastershipService told this
284 // instance is the new Master, but
285 // event returned from the store is null?
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700286
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700287 // FIXME: 1st argument should be deviceId, to allow setting
288 // certain roles even if the store returned null.
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700289 log.info("event: {} {}", event.type(), event);
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700290 provider().roleChanged(event.subject(), role);
tom568581d2014-09-08 20:13:36 -0700291 post(event);
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700292 } else {
293 log.info("No event to publish");
tom80c0e5e2014-09-08 18:08:58 -0700294 }
tomd3097b02014-08-26 10:40:29 -0700295 }
296
297 @Override
298 public void deviceDisconnected(DeviceId deviceId) {
tom32f66842014-08-27 19:27:47 -0700299 checkNotNull(deviceId, DEVICE_ID_NULL);
tomeadbb462014-09-07 16:10:19 -0700300 checkValidity();
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700301
Yuta HIGUCHI2d3cd312014-10-31 11:38:04 -0700302 log.info("Device {} disconnected from this node", deviceId);
Ayaka Koshibed9f693e2014-09-29 18:04:54 -0700303
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700304 DeviceEvent event = null;
305 try {
306 event = store.markOffline(deviceId);
307 } catch (IllegalStateException e) {
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700308 log.warn("Failed to mark {} offline", deviceId);
309 // only the MASTER should be marking off-line in normal cases,
310 // but if I was the last STANDBY connection, etc. and no one else
311 // was there to mark the device offline, this instance may need to
312 // temporarily request for Master Role and mark offline.
313
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700314 //there are times when this node will correctly have mastership, BUT
315 //that isn't reflected in the ClockManager before the device disconnects.
316 //we want to let go of the device anyways, so make sure this happens.
Yuta HIGUCHI0722fb22014-10-19 01:16:33 -0700317
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700318 // FIXME: Store semantics leaking out as IllegalStateException.
319 // Consider revising store API to handle this scenario.
320
321 MastershipRole role = mastershipService.requestRoleFor(deviceId);
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700322 MastershipTerm term = termService.getMastershipTerm(deviceId);
Yuta HIGUCHI0722fb22014-10-19 01:16:33 -0700323 final NodeId myNodeId = clusterService.getLocalNode().id();
324 // TODO: Move this type of check inside device clock manager, etc.
325 if (myNodeId.equals(term.master())) {
Yuta HIGUCHI2d3cd312014-10-31 11:38:04 -0700326 log.info("Retry marking {} offline", deviceId);
Yuta HIGUCHI0722fb22014-10-19 01:16:33 -0700327 deviceClockProviderService.setMastershipTerm(deviceId, term);
328 event = store.markOffline(deviceId);
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700329 } else {
Yuta HIGUCHI2d3cd312014-10-31 11:38:04 -0700330 log.info("Failed again marking {} offline. {}", deviceId, role);
Yuta HIGUCHI0722fb22014-10-19 01:16:33 -0700331 }
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700332 } finally {
333 //relinquish master role and ability to be backup.
334 mastershipService.relinquishMastership(deviceId);
335
336 if (event != null) {
Yuta HIGUCHI2d3cd312014-10-31 11:38:04 -0700337 log.info("Device {} disconnected from cluster", deviceId);
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700338 post(event);
339 }
tom0efbb1d2014-09-09 11:54:28 -0700340 }
tomd3097b02014-08-26 10:40:29 -0700341 }
342
343 @Override
alshabibb7b40632014-09-28 21:30:00 -0700344 public void updatePorts(DeviceId deviceId,
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700345 List<PortDescription> portDescriptions) {
tom32f66842014-08-27 19:27:47 -0700346 checkNotNull(deviceId, DEVICE_ID_NULL);
alshabibb7b40632014-09-28 21:30:00 -0700347 checkNotNull(portDescriptions,
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700348 "Port descriptions list cannot be null");
tomeadbb462014-09-07 16:10:19 -0700349 checkValidity();
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700350
Yuta HIGUCHI13c0b872014-10-30 18:09:22 -0700351 if (!deviceClockProviderService.isTimestampAvailable(deviceId)) {
352 // Never been a master for this device
353 // any update will be ignored.
354 log.trace("Ignoring {} port updates on standby node. {}", deviceId, portDescriptions);
355 return;
356 }
357
Yuta HIGUCHI5f6739c2014-10-01 14:04:01 -0700358 List<DeviceEvent> events = store.updatePorts(this.provider().id(),
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700359 deviceId, portDescriptions);
tom32f66842014-08-27 19:27:47 -0700360 for (DeviceEvent event : events) {
361 post(event);
362 }
tomd3097b02014-08-26 10:40:29 -0700363 }
364
365 @Override
alshabibb7b40632014-09-28 21:30:00 -0700366 public void portStatusChanged(DeviceId deviceId,
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700367 PortDescription portDescription) {
tom32f66842014-08-27 19:27:47 -0700368 checkNotNull(deviceId, DEVICE_ID_NULL);
369 checkNotNull(portDescription, PORT_DESCRIPTION_NULL);
tomeadbb462014-09-07 16:10:19 -0700370 checkValidity();
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700371
Yuta HIGUCHI13c0b872014-10-30 18:09:22 -0700372 if (!deviceClockProviderService.isTimestampAvailable(deviceId)) {
373 // Never been a master for this device
374 // any update will be ignored.
375 log.trace("Ignoring {} port update on standby node. {}", deviceId, portDescription);
376 return;
377 }
378
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700379 final DeviceEvent event = store.updatePortStatus(this.provider().id(),
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700380 deviceId, portDescription);
tomff7eb7c2014-09-08 12:49:03 -0700381 if (event != null) {
alshabibb7b40632014-09-28 21:30:00 -0700382 log.info("Device {} port {} status changed", deviceId, event
383 .port().number());
tom0efbb1d2014-09-09 11:54:28 -0700384 post(event);
tomff7eb7c2014-09-08 12:49:03 -0700385 }
tomd3097b02014-08-26 10:40:29 -0700386 }
tom3f2bbd72014-09-24 12:07:58 -0700387
388 @Override
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700389 public void receivedRoleReply(
390 DeviceId deviceId, MastershipRole requested, MastershipRole response) {
391 // Several things can happen here:
392 // 1. request and response match
393 // 2. request and response don't match
394 // 3. MastershipRole and requested match (and 1 or 2 are true)
395 // 4. MastershipRole and requested don't match (and 1 or 2 are true)
396 //
397 // 2, 4, and 3 with case 2 are failure modes.
398
tom3f2bbd72014-09-24 12:07:58 -0700399 // FIXME: implement response to this notification
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700400
401 log.info("got reply to a role request for {}: asked for {}, and got {}",
402 deviceId, requested, response);
403
404 if (requested == null && response == null) {
405 // something was off with DeviceProvider, maybe check channel too?
406 log.warn("Failed to assert role [{}] onto Device {}", requested, deviceId);
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700407 mastershipService.relinquishMastership(deviceId);
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700408 return;
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700409 }
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700410
411 if (requested.equals(response)) {
412 if (requested.equals(mastershipService.getLocalRole(deviceId))) {
413
414 return;
415 } else {
416 return;
417 // FIXME roleManager got the device to comply, but doesn't agree with
418 // the store; use the store's view, then try to reassert.
419 }
420 } else {
421 // we didn't get back what we asked for. Reelect someone else.
422 log.warn("Failed to assert role [{}] onto Device {}", response, deviceId);
423 if (response == MastershipRole.MASTER) {
424 mastershipService.relinquishMastership(deviceId);
425 // TODO: Shouldn't we be triggering event?
426 //final Device device = getDevice(deviceId);
427 //post(new DeviceEvent(DEVICE_MASTERSHIP_CHANGED, device));
428 }
429 }
430
tom3f2bbd72014-09-24 12:07:58 -0700431 }
tomd3097b02014-08-26 10:40:29 -0700432 }
tom32f66842014-08-27 19:27:47 -0700433
tomeadbb462014-09-07 16:10:19 -0700434 // Posts the specified event to the local event dispatcher.
tom32f66842014-08-27 19:27:47 -0700435 private void post(DeviceEvent event) {
436 if (event != null && eventDispatcher != null) {
437 eventDispatcher.post(event);
438 }
439 }
440
tomb41d1ac2014-09-24 01:51:24 -0700441 // Intercepts mastership events
Ayaka Koshibec4047702014-10-07 14:43:52 -0700442 private class InternalMastershipListener implements MastershipListener {
443
Ayaka Koshibe317245a2014-10-29 00:34:43 -0700444 // random cache size
445 private final int cacheSize = 5;
446 // temporarily stores term number + events to check for duplicates. A hack.
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700447 private HashMultimap<Integer, RoleInfo> eventCache =
Ayaka Koshibe317245a2014-10-29 00:34:43 -0700448 HashMultimap.create();
449
tomb41d1ac2014-09-24 01:51:24 -0700450 @Override
451 public void event(MastershipEvent event) {
Ayaka Koshibe4c891272014-10-08 17:14:16 -0700452 final DeviceId did = event.subject();
Ayaka Koshibeea5b4ce2014-10-11 14:17:17 -0700453 final NodeId myNodeId = clusterService.getLocalNode().id();
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700454
Yuta HIGUCHI67dce882014-10-21 21:13:26 -0700455 if (myNodeId.equals(event.roleInfo().master())) {
Ayaka Koshibeea5b4ce2014-10-11 14:17:17 -0700456 MastershipTerm term = termService.getMastershipTerm(did);
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700457
Ayaka Koshibe317245a2014-10-29 00:34:43 -0700458 // TODO duplicate suppression should probably occur in the MastershipManager
459 // itself, so listeners that can't deal with duplicates don't have to
460 // so this check themselves.
Yuta HIGUCHI225e63e2014-10-29 23:05:04 -0700461// if (checkDuplicate(event.roleInfo(), term.termNumber())) {
462// return;
463// }
Ayaka Koshibe317245a2014-10-29 00:34:43 -0700464
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700465 if (!myNodeId.equals(term.master())) {
466 // something went wrong in consistency, let go
Yuta HIGUCHI305ec832014-10-24 22:23:17 -0700467 log.warn("Mastership has changed after this event."
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700468 + "Term Service suggests {} for {}", term, did);
Yuta HIGUCHI305ec832014-10-24 22:23:17 -0700469 // FIXME: Is it possible to let go of MASTER role
470 // but remain on STANDBY list?
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700471 mastershipService.relinquishMastership(did);
472 applyRole(did, MastershipRole.STANDBY);
473 return;
Ayaka Koshibec4047702014-10-07 14:43:52 -0700474 }
Ayaka Koshibeea5b4ce2014-10-11 14:17:17 -0700475
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700476 // only set the new term if I am the master
477 deviceClockProviderService.setMastershipTerm(did, term);
478
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700479 // if the device is null here, we are the first master to claim the
480 // device. No worries, the DeviceManager will create one soon.
481 Device device = getDevice(did);
482 if ((device != null) && !isAvailable(did)) {
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700483 if (!isReachable(device)) {
484 log.warn("Device {} has disconnected after this event", did);
485 mastershipService.relinquishMastership(did);
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700486 return;
487 }
Ayaka Koshibeea5b4ce2014-10-11 14:17:17 -0700488 //flag the device as online. Is there a better way to do this?
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700489 DeviceEvent devEvent =
490 store.createOrUpdateDevice(device.providerId(), did,
491 new DefaultDeviceDescription(
492 did.uri(), device.type(), device.manufacturer(),
493 device.hwVersion(), device.swVersion(),
494 device.serialNumber(), device.chassisId()));
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700495 post(devEvent);
Ayaka Koshibeea5b4ce2014-10-11 14:17:17 -0700496 }
Ayaka Koshibeea5b4ce2014-10-11 14:17:17 -0700497 applyRole(did, MastershipRole.MASTER);
Yuta HIGUCHI67dce882014-10-21 21:13:26 -0700498 } else if (event.roleInfo().backups().contains(myNodeId)) {
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700499 if (!isReachable(getDevice(did))) {
500 log.warn("Device {} has disconnected after this event", did);
501 mastershipService.relinquishMastership(did);
Yuta HIGUCHIeb5a0b92014-10-29 15:45:55 -0700502 return;
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700503 }
Ayaka Koshibec4047702014-10-07 14:43:52 -0700504 applyRole(did, MastershipRole.STANDBY);
Yuta HIGUCHI38b4d9d2014-10-30 11:32:41 -0700505 } else {
506 // Event suggests that this Node has no connection to this Device
507 // confirm.
508 final Device device = getDevice(did);
509 if (!isReachable(device)) {
510 // not connection to device, as expected
511 return;
512 }
513 // connection seems to exist
514 log.info("Detected mastership info mismatch, requesting Role");
515 mastershipService.requestRoleFor(did);
516 final MastershipTerm term = termService.getMastershipTerm(did);
517 if (myNodeId.equals(term.master())) {
518 // became MASTER
519 // TODO: consider slicing out method for applying MASTER role
520 deviceClockProviderService.setMastershipTerm(did, term);
521
522 //flag the device as online. Is there a better way to do this?
523 DeviceEvent devEvent =
524 store.createOrUpdateDevice(device.providerId(), did,
525 new DefaultDeviceDescription(
526 did.uri(), device.type(), device.manufacturer(),
527 device.hwVersion(), device.swVersion(),
528 device.serialNumber(), device.chassisId()));
529 applyRole(did, MastershipRole.MASTER);
530 post(devEvent);
531 } else {
532 applyRole(did, MastershipRole.STANDBY);
533 }
tomb41d1ac2014-09-24 01:51:24 -0700534 }
535 }
Ayaka Koshibe317245a2014-10-29 00:34:43 -0700536
537 // checks for duplicate event, returning true if one is found.
538 private boolean checkDuplicate(RoleInfo roleInfo, int term) {
Yuta HIGUCHIeb5a0b92014-10-29 15:45:55 -0700539 // turning off duplicate check
540 return false;
541// synchronized (eventCache) {
542// if (eventCache.get(term).contains(roleInfo)) {
543// log.info("duplicate event detected; ignoring");
544// return true;
545// } else {
546// eventCache.put(term, roleInfo);
547// // purge by-term oldest entries to keep the cache size under limit
548// if (eventCache.size() > cacheSize) {
549// eventCache.removeAll(term - cacheSize);
550// }
551// return false;
552// }
553// }
Ayaka Koshibe317245a2014-10-29 00:34:43 -0700554 }
555
tomb41d1ac2014-09-24 01:51:24 -0700556 }
tomf80c9722014-09-24 14:49:18 -0700557
558 // Store delegate to re-post events emitted from the store.
alshabib271a6202014-09-28 22:11:21 -0700559 private class InternalStoreDelegate
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700560 implements DeviceStoreDelegate {
tomf80c9722014-09-24 14:49:18 -0700561 @Override
562 public void notify(DeviceEvent event) {
563 post(event);
564 }
565 }
tomd3097b02014-08-26 10:40:29 -0700566}