blob: e8cccf465b70249c5abcc61f2b34ae5353acec9a [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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.device.impl;
tomd3097b02014-08-26 10:40:29 -070017
alshabibafc514a2014-12-01 14:44:05 -080018import com.google.common.collect.Lists;
tomd3097b02014-08-26 10:40:29 -070019import org.apache.felix.scr.annotations.Activate;
20import org.apache.felix.scr.annotations.Component;
21import org.apache.felix.scr.annotations.Deactivate;
tom5f38b3a2014-08-27 23:50:54 -070022import org.apache.felix.scr.annotations.Reference;
23import org.apache.felix.scr.annotations.ReferenceCardinality;
tomd3097b02014-08-26 10:40:29 -070024import org.apache.felix.scr.annotations.Service;
Brian O'Connorabafb502014-12-02 22:26:20 -080025import org.onosproject.cluster.ClusterService;
26import org.onosproject.cluster.NodeId;
27import org.onosproject.event.AbstractListenerRegistry;
28import org.onosproject.event.EventDeliveryService;
29import org.onosproject.mastership.MastershipEvent;
30import org.onosproject.mastership.MastershipListener;
31import org.onosproject.mastership.MastershipService;
32import org.onosproject.mastership.MastershipTerm;
33import org.onosproject.mastership.MastershipTermService;
34import org.onosproject.net.Device;
35import org.onosproject.net.DeviceId;
36import org.onosproject.net.MastershipRole;
37import org.onosproject.net.Port;
38import org.onosproject.net.PortNumber;
39import org.onosproject.net.device.DefaultDeviceDescription;
40import org.onosproject.net.device.DefaultPortDescription;
41import org.onosproject.net.device.DeviceAdminService;
42import org.onosproject.net.device.DeviceClockProviderService;
43import org.onosproject.net.device.DeviceDescription;
44import org.onosproject.net.device.DeviceEvent;
45import org.onosproject.net.device.DeviceListener;
46import org.onosproject.net.device.DeviceProvider;
47import org.onosproject.net.device.DeviceProviderRegistry;
48import org.onosproject.net.device.DeviceProviderService;
49import org.onosproject.net.device.DeviceService;
50import org.onosproject.net.device.DeviceStore;
51import org.onosproject.net.device.DeviceStoreDelegate;
52import org.onosproject.net.device.PortDescription;
53import org.onosproject.net.provider.AbstractProviderRegistry;
54import org.onosproject.net.provider.AbstractProviderService;
tomd3097b02014-08-26 10:40:29 -070055import org.slf4j.Logger;
tomd3097b02014-08-26 10:40:29 -070056
Jonathan Hart2f669362015-02-11 16:19:20 -080057import java.util.List;
58import java.util.concurrent.Executors;
59import java.util.concurrent.ScheduledExecutorService;
60import java.util.concurrent.TimeUnit;
61
62import static com.google.common.base.Preconditions.checkNotNull;
63import static org.onlab.util.Tools.namedThreads;
64import static org.onosproject.net.MastershipRole.MASTER;
65import static org.onosproject.net.MastershipRole.NONE;
66import static org.onosproject.net.MastershipRole.STANDBY;
67import static org.slf4j.LoggerFactory.getLogger;
68
tomd3097b02014-08-26 10:40:29 -070069/**
tome4729872014-09-23 00:37:37 -070070 * Provides implementation of the device SB & NB APIs.
tomd3097b02014-08-26 10:40:29 -070071 */
72@Component(immediate = true)
73@Service
tom41a2c5f2014-09-19 09:20:35 -070074public class DeviceManager
Thomas Vachuskad16ce182014-10-29 17:25:29 -070075 extends AbstractProviderRegistry<DeviceProvider, DeviceProviderService>
76 implements DeviceService, DeviceAdminService, DeviceProviderRegistry {
tom32f66842014-08-27 19:27:47 -070077
tome5ec3fd2014-09-04 15:18:06 -070078 private static final String DEVICE_ID_NULL = "Device ID cannot be null";
79 private static final String PORT_NUMBER_NULL = "Port number cannot be null";
80 private static final String DEVICE_DESCRIPTION_NULL = "Device description cannot be null";
81 private static final String PORT_DESCRIPTION_NULL = "Port description cannot be null";
82 private static final String ROLE_NULL = "Role cannot be null";
tomd3097b02014-08-26 10:40:29 -070083
tom5f38b3a2014-08-27 23:50:54 -070084 private final Logger log = getLogger(getClass());
tomd3097b02014-08-26 10:40:29 -070085
alshabib271a6202014-09-28 22:11:21 -070086 protected final AbstractListenerRegistry<DeviceEvent, DeviceListener> listenerRegistry =
87 new AbstractListenerRegistry<>();
tom32f66842014-08-27 19:27:47 -070088
alshabib339a3d92014-09-26 17:54:32 -070089 private final DeviceStoreDelegate delegate = new InternalStoreDelegate();
tomf80c9722014-09-24 14:49:18 -070090
tomc78acee2014-09-24 15:16:55 -070091 private final MastershipListener mastershipListener = new InternalMastershipListener();
tomb41d1ac2014-09-24 01:51:24 -070092
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -080093 private ScheduledExecutorService backgroundService;
94
tom41a2c5f2014-09-19 09:20:35 -070095 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
96 protected DeviceStore store;
tomd3097b02014-08-26 10:40:29 -070097
tom5f38b3a2014-08-27 23:50:54 -070098 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
tome5ec3fd2014-09-04 15:18:06 -070099 protected EventDeliveryService eventDispatcher;
tom5f38b3a2014-08-27 23:50:54 -0700100
Ayaka Koshibea7f044e2014-09-23 16:56:20 -0700101 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
tomb41d1ac2014-09-24 01:51:24 -0700102 protected ClusterService clusterService;
103
104 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Ayaka Koshibea7f044e2014-09-23 16:56:20 -0700105 protected MastershipService mastershipService;
106
Yuta HIGUCHIbcac4992014-11-22 19:27:57 -0800107 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Ayaka Koshibe3de43ca2014-09-26 16:40:23 -0700108 protected MastershipTermService termService;
109
Madan Jampani61056bc2014-09-27 09:07:26 -0700110 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Yuta HIGUCHI093e83e2014-10-10 22:26:11 -0700111 protected DeviceClockProviderService deviceClockProviderService;
Madan Jampani61056bc2014-09-27 09:07:26 -0700112
tomd3097b02014-08-26 10:40:29 -0700113 @Activate
114 public void activate() {
Thomas Vachuska9ea3e6f2015-01-23 16:34:22 -0800115 backgroundService = Executors.newSingleThreadScheduledExecutor(namedThreads("onos-device-manager-background"));
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800116
tomf80c9722014-09-24 14:49:18 -0700117 store.setDelegate(delegate);
tom96dfcab2014-08-28 09:26:03 -0700118 eventDispatcher.addSink(DeviceEvent.class, listenerRegistry);
tomb41d1ac2014-09-24 01:51:24 -0700119 mastershipService.addListener(mastershipListener);
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800120
121 backgroundService.scheduleWithFixedDelay(new Runnable() {
122
123 @Override
124 public void run() {
125 try {
126 mastershipCheck();
127 } catch (Exception e) {
128 log.error("Exception thrown during integrity check", e);
129 }
130 }
131 }, 1, 1, TimeUnit.MINUTES);
tomd3097b02014-08-26 10:40:29 -0700132 log.info("Started");
133 }
134
135 @Deactivate
136 public void deactivate() {
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800137 backgroundService.shutdown();
138
tomf80c9722014-09-24 14:49:18 -0700139 store.unsetDelegate(delegate);
tomb41d1ac2014-09-24 01:51:24 -0700140 mastershipService.removeListener(mastershipListener);
tom5f38b3a2014-08-27 23:50:54 -0700141 eventDispatcher.removeSink(DeviceEvent.class);
tomd3097b02014-08-26 10:40:29 -0700142 log.info("Stopped");
143 }
144
145 @Override
tomad2d2092014-09-06 23:24:20 -0700146 public int getDeviceCount() {
147 return store.getDeviceCount();
tomd3097b02014-08-26 10:40:29 -0700148 }
149
150 @Override
tom32f66842014-08-27 19:27:47 -0700151 public Iterable<Device> getDevices() {
tome5ec3fd2014-09-04 15:18:06 -0700152 return store.getDevices();
tomd3097b02014-08-26 10:40:29 -0700153 }
154
tom32f66842014-08-27 19:27:47 -0700155 @Override
Yuta HIGUCHIf1f2ac02014-11-26 14:02:22 -0800156 public Iterable<Device> getAvailableDevices() {
157 return store.getAvailableDevices();
158 }
159
160 @Override
tom32f66842014-08-27 19:27:47 -0700161 public Device getDevice(DeviceId deviceId) {
162 checkNotNull(deviceId, DEVICE_ID_NULL);
tom132b58a2014-08-28 16:11:28 -0700163 return store.getDevice(deviceId);
tom32f66842014-08-27 19:27:47 -0700164 }
165
166 @Override
tomad2d2092014-09-06 23:24:20 -0700167 public MastershipRole getRole(DeviceId deviceId) {
168 checkNotNull(deviceId, DEVICE_ID_NULL);
tomb41d1ac2014-09-24 01:51:24 -0700169 return mastershipService.getLocalRole(deviceId);
tomad2d2092014-09-06 23:24:20 -0700170 }
171
172 @Override
tom32f66842014-08-27 19:27:47 -0700173 public List<Port> getPorts(DeviceId deviceId) {
174 checkNotNull(deviceId, DEVICE_ID_NULL);
tom132b58a2014-08-28 16:11:28 -0700175 return store.getPorts(deviceId);
tom32f66842014-08-27 19:27:47 -0700176 }
177
178 @Override
179 public Port getPort(DeviceId deviceId, PortNumber portNumber) {
180 checkNotNull(deviceId, DEVICE_ID_NULL);
181 checkNotNull(portNumber, PORT_NUMBER_NULL);
tom132b58a2014-08-28 16:11:28 -0700182 return store.getPort(deviceId, portNumber);
tom32f66842014-08-27 19:27:47 -0700183 }
184
185 @Override
tomff7eb7c2014-09-08 12:49:03 -0700186 public boolean isAvailable(DeviceId deviceId) {
187 checkNotNull(deviceId, DEVICE_ID_NULL);
188 return store.isAvailable(deviceId);
189 }
190
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700191 // Check a device for control channel connectivity.
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700192 private boolean isReachable(DeviceId deviceId) {
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800193 if (deviceId == null) {
194 return false;
195 }
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700196 DeviceProvider provider = getProvider(deviceId);
197 if (provider != null) {
198 return provider.isReachable(deviceId);
199 } else {
Yuta HIGUCHI72669c42014-11-13 14:48:17 -0800200 log.debug("Provider not found for {}", deviceId);
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700201 return false;
202 }
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700203 }
204
tome5ec3fd2014-09-04 15:18:06 -0700205 @Override
206 public void removeDevice(DeviceId deviceId) {
207 checkNotNull(deviceId, DEVICE_ID_NULL);
208 DeviceEvent event = store.removeDevice(deviceId);
tom0efbb1d2014-09-09 11:54:28 -0700209 if (event != null) {
210 log.info("Device {} administratively removed", deviceId);
211 post(event);
212 }
tome5ec3fd2014-09-04 15:18:06 -0700213 }
214
tom7869ad92014-09-09 14:32:08 -0700215 @Override
216 public void addListener(DeviceListener listener) {
217 listenerRegistry.addListener(listener);
218 }
219
220 @Override
221 public void removeListener(DeviceListener listener) {
222 listenerRegistry.removeListener(listener);
223 }
224
225 @Override
alshabibb7b40632014-09-28 21:30:00 -0700226 protected DeviceProviderService createProviderService(
227 DeviceProvider provider) {
tom7869ad92014-09-09 14:32:08 -0700228 return new InternalDeviceProviderService(provider);
229 }
230
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800231 /**
232 * Checks if all the reachable devices have a valid mastership role.
233 */
234 private void mastershipCheck() {
235 log.debug("Checking mastership");
236 for (Device device : getDevices()) {
237 final DeviceId deviceId = device.id();
Jonathan Hart2f669362015-02-11 16:19:20 -0800238 log.trace("Checking device {}", deviceId);
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800239
240 if (!isReachable(deviceId)) {
241 continue;
242 }
243
244 if (mastershipService.getLocalRole(deviceId) != NONE) {
245 continue;
246 }
247
248 log.info("{} is reachable but did not have a valid role, reasserting", deviceId);
249
250 // isReachable but was not MASTER or STANDBY, get a role and apply
251 // Note: NONE triggers request to MastershipService
252 reassertRole(deviceId, NONE);
253 }
254 }
255
tomd3097b02014-08-26 10:40:29 -0700256 // Personalized device provider service issued to the supplied provider.
tomdc361b62014-09-09 20:36:52 -0700257 private class InternalDeviceProviderService
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700258 extends AbstractProviderService<DeviceProvider>
259 implements DeviceProviderService {
tomd3097b02014-08-26 10:40:29 -0700260
tomcfde0622014-09-09 11:02:42 -0700261 InternalDeviceProviderService(DeviceProvider provider) {
tomd3097b02014-08-26 10:40:29 -0700262 super(provider);
263 }
264
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700265 /**
266 * Apply role in reaction to provider event.
267 *
268 * @param deviceId device identifier
269 * @param newRole new role to apply to the device
270 * @return true if the request was sent to provider
271 */
272 private boolean applyRole(DeviceId deviceId, MastershipRole newRole) {
273
274 if (newRole.equals(MastershipRole.NONE)) {
275 //no-op
276 return true;
277 }
278
279 DeviceProvider provider = provider();
280 if (provider == null) {
281 log.warn("Provider for {} was not found. Cannot apply role {}", deviceId, newRole);
282 return false;
283 }
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700284 provider.roleChanged(deviceId, newRole);
285 // not triggering probe when triggered by provider service event
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700286
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700287 return true;
288 }
289
290
tomd3097b02014-08-26 10:40:29 -0700291 @Override
alshabibb7b40632014-09-28 21:30:00 -0700292 public void deviceConnected(DeviceId deviceId,
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700293 DeviceDescription deviceDescription) {
tom32f66842014-08-27 19:27:47 -0700294 checkNotNull(deviceId, DEVICE_ID_NULL);
295 checkNotNull(deviceDescription, DEVICE_DESCRIPTION_NULL);
tomeadbb462014-09-07 16:10:19 -0700296 checkValidity();
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700297
298 log.info("Device {} connected", deviceId);
Yuta HIGUCHIdfe6e3b2014-10-30 11:31:51 -0700299 final NodeId myNodeId = clusterService.getLocalNode().id();
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700300
301 // check my Role
302 mastershipService.requestRoleFor(deviceId);
303 final MastershipTerm term = termService.getMastershipTerm(deviceId);
Yuta HIGUCHIdfe6e3b2014-10-30 11:31:51 -0700304 if (!myNodeId.equals(term.master())) {
Yuta HIGUCHI2d3cd312014-10-31 11:38:04 -0700305 log.info("Role of this node is STANDBY for {}", deviceId);
306 // TODO: Do we need to explicitly tell the Provider that
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700307 // this instance is not the MASTER
308 applyRole(deviceId, MastershipRole.STANDBY);
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700309 return;
310 }
Yuta HIGUCHI2d3cd312014-10-31 11:38:04 -0700311 log.info("Role of this node is MASTER for {}", deviceId);
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700312
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700313 // tell clock provider if this instance is the master
Yuta HIGUCHI093e83e2014-10-10 22:26:11 -0700314 deviceClockProviderService.setMastershipTerm(deviceId, term);
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700315
tome5ec3fd2014-09-04 15:18:06 -0700316 DeviceEvent event = store.createOrUpdateDevice(provider().id(),
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700317 deviceId, deviceDescription);
tom80c0e5e2014-09-08 18:08:58 -0700318
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700319 applyRole(deviceId, MastershipRole.MASTER);
320
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700321 // If there was a change of any kind, tell the provider
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700322 // that this instance is the master.
tom80c0e5e2014-09-08 18:08:58 -0700323 if (event != null) {
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700324 log.trace("event: {} {}", event.type(), event);
tom568581d2014-09-08 20:13:36 -0700325 post(event);
tom80c0e5e2014-09-08 18:08:58 -0700326 }
tomd3097b02014-08-26 10:40:29 -0700327 }
328
329 @Override
330 public void deviceDisconnected(DeviceId deviceId) {
tom32f66842014-08-27 19:27:47 -0700331 checkNotNull(deviceId, DEVICE_ID_NULL);
tomeadbb462014-09-07 16:10:19 -0700332 checkValidity();
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700333
Yuta HIGUCHI2d3cd312014-10-31 11:38:04 -0700334 log.info("Device {} disconnected from this node", deviceId);
Ayaka Koshibed9f693e2014-09-29 18:04:54 -0700335
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700336 DeviceEvent event = null;
alshabibafc514a2014-12-01 14:44:05 -0800337 List<Port> ports = store.getPorts(deviceId);
338 List<PortDescription> descs = Lists.newArrayList();
339 ports.forEach(port ->
340 descs.add(new DefaultPortDescription(port.number(),
341 false, port.type(),
342 port.portSpeed())));
343 store.updatePorts(this.provider().id(), deviceId, descs);
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700344 try {
345 event = store.markOffline(deviceId);
346 } catch (IllegalStateException e) {
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700347 log.warn("Failed to mark {} offline", deviceId);
348 // only the MASTER should be marking off-line in normal cases,
349 // but if I was the last STANDBY connection, etc. and no one else
350 // was there to mark the device offline, this instance may need to
351 // temporarily request for Master Role and mark offline.
352
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700353 //there are times when this node will correctly have mastership, BUT
354 //that isn't reflected in the ClockManager before the device disconnects.
355 //we want to let go of the device anyways, so make sure this happens.
Yuta HIGUCHI0722fb22014-10-19 01:16:33 -0700356
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700357 // FIXME: Store semantics leaking out as IllegalStateException.
358 // Consider revising store API to handle this scenario.
359
360 MastershipRole role = mastershipService.requestRoleFor(deviceId);
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700361 MastershipTerm term = termService.getMastershipTerm(deviceId);
Yuta HIGUCHI0722fb22014-10-19 01:16:33 -0700362 final NodeId myNodeId = clusterService.getLocalNode().id();
363 // TODO: Move this type of check inside device clock manager, etc.
364 if (myNodeId.equals(term.master())) {
Yuta HIGUCHI2d3cd312014-10-31 11:38:04 -0700365 log.info("Retry marking {} offline", deviceId);
Yuta HIGUCHI0722fb22014-10-19 01:16:33 -0700366 deviceClockProviderService.setMastershipTerm(deviceId, term);
367 event = store.markOffline(deviceId);
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700368 } else {
Yuta HIGUCHI2d3cd312014-10-31 11:38:04 -0700369 log.info("Failed again marking {} offline. {}", deviceId, role);
Yuta HIGUCHI0722fb22014-10-19 01:16:33 -0700370 }
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700371 } finally {
372 //relinquish master role and ability to be backup.
373 mastershipService.relinquishMastership(deviceId);
374
375 if (event != null) {
Yuta HIGUCHI2d3cd312014-10-31 11:38:04 -0700376 log.info("Device {} disconnected from cluster", deviceId);
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700377 post(event);
378 }
tom0efbb1d2014-09-09 11:54:28 -0700379 }
tomd3097b02014-08-26 10:40:29 -0700380 }
381
382 @Override
alshabibb7b40632014-09-28 21:30:00 -0700383 public void updatePorts(DeviceId deviceId,
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700384 List<PortDescription> portDescriptions) {
tom32f66842014-08-27 19:27:47 -0700385 checkNotNull(deviceId, DEVICE_ID_NULL);
alshabibb7b40632014-09-28 21:30:00 -0700386 checkNotNull(portDescriptions,
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700387 "Port descriptions list cannot be null");
tomeadbb462014-09-07 16:10:19 -0700388 checkValidity();
Yuta HIGUCHI13c0b872014-10-30 18:09:22 -0700389 if (!deviceClockProviderService.isTimestampAvailable(deviceId)) {
390 // Never been a master for this device
391 // any update will be ignored.
392 log.trace("Ignoring {} port updates on standby node. {}", deviceId, portDescriptions);
393 return;
394 }
395
Yuta HIGUCHI5f6739c2014-10-01 14:04:01 -0700396 List<DeviceEvent> events = store.updatePorts(this.provider().id(),
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700397 deviceId, portDescriptions);
tom32f66842014-08-27 19:27:47 -0700398 for (DeviceEvent event : events) {
399 post(event);
400 }
tomd3097b02014-08-26 10:40:29 -0700401 }
402
403 @Override
alshabibb7b40632014-09-28 21:30:00 -0700404 public void portStatusChanged(DeviceId deviceId,
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700405 PortDescription portDescription) {
tom32f66842014-08-27 19:27:47 -0700406 checkNotNull(deviceId, DEVICE_ID_NULL);
407 checkNotNull(portDescription, PORT_DESCRIPTION_NULL);
tomeadbb462014-09-07 16:10:19 -0700408 checkValidity();
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700409
Yuta HIGUCHI13c0b872014-10-30 18:09:22 -0700410 if (!deviceClockProviderService.isTimestampAvailable(deviceId)) {
411 // Never been a master for this device
412 // any update will be ignored.
413 log.trace("Ignoring {} port update on standby node. {}", deviceId, portDescription);
414 return;
415 }
416
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700417 final DeviceEvent event = store.updatePortStatus(this.provider().id(),
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700418 deviceId, portDescription);
tomff7eb7c2014-09-08 12:49:03 -0700419 if (event != null) {
alshabibb7b40632014-09-28 21:30:00 -0700420 log.info("Device {} port {} status changed", deviceId, event
421 .port().number());
tom0efbb1d2014-09-09 11:54:28 -0700422 post(event);
tomff7eb7c2014-09-08 12:49:03 -0700423 }
tomd3097b02014-08-26 10:40:29 -0700424 }
tom3f2bbd72014-09-24 12:07:58 -0700425
426 @Override
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700427 public void receivedRoleReply(
428 DeviceId deviceId, MastershipRole requested, MastershipRole response) {
429 // Several things can happen here:
430 // 1. request and response match
431 // 2. request and response don't match
432 // 3. MastershipRole and requested match (and 1 or 2 are true)
433 // 4. MastershipRole and requested don't match (and 1 or 2 are true)
434 //
435 // 2, 4, and 3 with case 2 are failure modes.
436
tom3f2bbd72014-09-24 12:07:58 -0700437 // FIXME: implement response to this notification
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700438
439 log.info("got reply to a role request for {}: asked for {}, and got {}",
440 deviceId, requested, response);
441
442 if (requested == null && response == null) {
443 // something was off with DeviceProvider, maybe check channel too?
444 log.warn("Failed to assert role [{}] onto Device {}", requested, deviceId);
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700445 mastershipService.relinquishMastership(deviceId);
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700446 return;
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700447 }
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700448
449 if (requested.equals(response)) {
450 if (requested.equals(mastershipService.getLocalRole(deviceId))) {
451
452 return;
453 } else {
454 return;
455 // FIXME roleManager got the device to comply, but doesn't agree with
456 // the store; use the store's view, then try to reassert.
457 }
458 } else {
459 // we didn't get back what we asked for. Reelect someone else.
460 log.warn("Failed to assert role [{}] onto Device {}", response, deviceId);
461 if (response == MastershipRole.MASTER) {
462 mastershipService.relinquishMastership(deviceId);
463 // TODO: Shouldn't we be triggering event?
464 //final Device device = getDevice(deviceId);
465 //post(new DeviceEvent(DEVICE_MASTERSHIP_CHANGED, device));
466 }
467 }
468
tom3f2bbd72014-09-24 12:07:58 -0700469 }
tomd3097b02014-08-26 10:40:29 -0700470 }
tom32f66842014-08-27 19:27:47 -0700471
tomeadbb462014-09-07 16:10:19 -0700472 // Posts the specified event to the local event dispatcher.
tom32f66842014-08-27 19:27:47 -0700473 private void post(DeviceEvent event) {
474 if (event != null && eventDispatcher != null) {
475 eventDispatcher.post(event);
476 }
477 }
478
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800479 // Applies the specified role to the device; ignores NONE
480 /**
481 * Apply role to device and send probe if MASTER.
482 *
483 * @param deviceId device identifier
484 * @param newRole new role to apply to the device
485 * @return true if the request was sent to provider
486 */
487 private boolean applyRoleAndProbe(DeviceId deviceId, MastershipRole newRole) {
488 if (newRole.equals(MastershipRole.NONE)) {
489 //no-op
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700490 return true;
491 }
Ayaka Koshibe317245a2014-10-29 00:34:43 -0700492
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800493 DeviceProvider provider = getProvider(deviceId);
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800494 if (provider == null) {
495 log.warn("Provider for {} was not found. Cannot apply role {}", deviceId, newRole);
496 return false;
497 }
498 provider.roleChanged(deviceId, newRole);
499
500 if (newRole.equals(MastershipRole.MASTER)) {
501 // only trigger event when request was sent to provider
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800502 provider.triggerProbe(deviceId);
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800503 }
504 return true;
505 }
506
507 /**
508 * Reaasert role for specified device connected to this node.
509 *
510 * @param did device identifier
511 * @param nextRole role to apply. If NONE is specified,
512 * it will ask mastership service for a role and apply it.
513 */
514 private void reassertRole(final DeviceId did,
515 final MastershipRole nextRole) {
516
517 final NodeId myNodeId = clusterService.getLocalNode().id();
518 MastershipRole myNextRole = nextRole;
519 if (myNextRole == NONE) {
520 mastershipService.requestRoleFor(did);
521 MastershipTerm term = termService.getMastershipTerm(did);
522 if (myNodeId.equals(term.master())) {
523 myNextRole = MASTER;
524 } else {
525 myNextRole = STANDBY;
526 }
527 }
528
529 switch (myNextRole) {
530 case MASTER:
531 final Device device = getDevice(did);
532 if ((device != null) && !isAvailable(did)) {
533 //flag the device as online. Is there a better way to do this?
534 DefaultDeviceDescription deviceDescription
535 = new DefaultDeviceDescription(did.uri(),
536 device.type(),
537 device.manufacturer(),
538 device.hwVersion(),
539 device.swVersion(),
540 device.serialNumber(),
541 device.chassisId());
542 DeviceEvent devEvent =
543 store.createOrUpdateDevice(device.providerId(), did,
544 deviceDescription);
545 post(devEvent);
546 }
547 // TODO: should apply role only if there is mismatch
548 log.info("Applying role {} to {}", myNextRole, did);
549 if (!applyRoleAndProbe(did, MASTER)) {
550 // immediately failed to apply role
551 mastershipService.relinquishMastership(did);
552 // FIXME disconnect?
553 }
554 break;
555 case STANDBY:
556 log.info("Applying role {} to {}", myNextRole, did);
557 if (!applyRoleAndProbe(did, STANDBY)) {
558 // immediately failed to apply role
559 mastershipService.relinquishMastership(did);
560 // FIXME disconnect?
561 }
562 break;
563 case NONE:
564 default:
565 // should never reach here
566 log.error("You didn't see anything. I did not exist.");
567 break;
568 }
569 }
570
571 // Intercepts mastership events
572 private class InternalMastershipListener implements MastershipListener {
573
tomb41d1ac2014-09-24 01:51:24 -0700574 @Override
575 public void event(MastershipEvent event) {
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700576
577 if (event.type() != MastershipEvent.Type.MASTER_CHANGED) {
578 // Don't care if backup list changed.
579 return;
580 }
581
Ayaka Koshibe4c891272014-10-08 17:14:16 -0700582 final DeviceId did = event.subject();
Ayaka Koshibeea5b4ce2014-10-11 14:17:17 -0700583 final NodeId myNodeId = clusterService.getLocalNode().id();
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700584
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700585 // myRole suggested by MastershipService
586 MastershipRole myNextRole;
Yuta HIGUCHI67dce882014-10-21 21:13:26 -0700587 if (myNodeId.equals(event.roleInfo().master())) {
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700588 // confirm latest info
Ayaka Koshibeea5b4ce2014-10-11 14:17:17 -0700589 MastershipTerm term = termService.getMastershipTerm(did);
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700590 final boolean iHaveControl = myNodeId.equals(term.master());
591 if (iHaveControl) {
Yuta HIGUCHI38b4d9d2014-10-30 11:32:41 -0700592 deviceClockProviderService.setMastershipTerm(did, term);
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700593 myNextRole = MASTER;
Yuta HIGUCHI38b4d9d2014-10-30 11:32:41 -0700594 } else {
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700595 myNextRole = STANDBY;
596 }
597 } else if (event.roleInfo().backups().contains(myNodeId)) {
598 myNextRole = STANDBY;
599 } else {
600 myNextRole = NONE;
601 }
602
603
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700604 final boolean isReachable = isReachable(did);
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700605 if (!isReachable) {
606 // device is not connected to this node
607 if (myNextRole != NONE) {
608 log.warn("Node was instructed to be {} role for {}, "
609 + "but this node cannot reach the device. "
610 + "Relinquishing role. ",
611 myNextRole, did);
612 mastershipService.relinquishMastership(did);
613 }
614 return;
615 }
616
617 // device is connected to this node:
alshabibfd3cc052015-02-10 15:16:57 -0800618 if (store.getDevice(did) != null) {
619 reassertRole(did, myNextRole);
620 } else {
621 log.warn("Device is not yet/no longer in the store: {}", did);
622 }
Ayaka Koshibe317245a2014-10-29 00:34:43 -0700623 }
tomb41d1ac2014-09-24 01:51:24 -0700624 }
tomf80c9722014-09-24 14:49:18 -0700625
626 // Store delegate to re-post events emitted from the store.
alshabib271a6202014-09-28 22:11:21 -0700627 private class InternalStoreDelegate
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700628 implements DeviceStoreDelegate {
tomf80c9722014-09-24 14:49:18 -0700629 @Override
630 public void notify(DeviceEvent event) {
631 post(event);
632 }
633 }
tomd3097b02014-08-26 10:40:29 -0700634}