blob: b00b9936a65d81d8bf27da37444b5781c5e56d66 [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;
Yuta HIGUCHId26354d2014-10-31 14:14:38 -070019import static org.onlab.onos.net.MastershipRole.*;
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -080020import static org.onlab.util.Tools.namedThreads;
alshabib339a3d92014-09-26 17:54:32 -070021import static org.slf4j.LoggerFactory.getLogger;
22
23import java.util.List;
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -080024import java.util.concurrent.Executors;
25import java.util.concurrent.ScheduledExecutorService;
26import java.util.concurrent.TimeUnit;
alshabib339a3d92014-09-26 17:54:32 -070027
tomd3097b02014-08-26 10:40:29 -070028import org.apache.felix.scr.annotations.Activate;
29import org.apache.felix.scr.annotations.Component;
30import org.apache.felix.scr.annotations.Deactivate;
tom5f38b3a2014-08-27 23:50:54 -070031import org.apache.felix.scr.annotations.Reference;
32import org.apache.felix.scr.annotations.ReferenceCardinality;
tomd3097b02014-08-26 10:40:29 -070033import org.apache.felix.scr.annotations.Service;
tomb41d1ac2014-09-24 01:51:24 -070034import org.onlab.onos.cluster.ClusterService;
Yuta HIGUCHI8e939d82014-10-08 08:09:18 -070035import org.onlab.onos.cluster.NodeId;
tom96dfcab2014-08-28 09:26:03 -070036import org.onlab.onos.event.AbstractListenerRegistry;
37import org.onlab.onos.event.EventDeliveryService;
Yuta HIGUCHI80912e62014-10-12 00:15:47 -070038import org.onlab.onos.mastership.MastershipEvent;
39import org.onlab.onos.mastership.MastershipListener;
40import org.onlab.onos.mastership.MastershipService;
41import org.onlab.onos.mastership.MastershipTerm;
42import org.onlab.onos.mastership.MastershipTermService;
tom32f66842014-08-27 19:27:47 -070043import org.onlab.onos.net.Device;
tomd3097b02014-08-26 10:40:29 -070044import org.onlab.onos.net.DeviceId;
45import org.onlab.onos.net.MastershipRole;
tom32f66842014-08-27 19:27:47 -070046import org.onlab.onos.net.Port;
47import org.onlab.onos.net.PortNumber;
Ayaka Koshibeea5b4ce2014-10-11 14:17:17 -070048import org.onlab.onos.net.device.DefaultDeviceDescription;
tome5ec3fd2014-09-04 15:18:06 -070049import org.onlab.onos.net.device.DeviceAdminService;
Yuta HIGUCHI093e83e2014-10-10 22:26:11 -070050import org.onlab.onos.net.device.DeviceClockProviderService;
tomd3097b02014-08-26 10:40:29 -070051import org.onlab.onos.net.device.DeviceDescription;
tom32f66842014-08-27 19:27:47 -070052import org.onlab.onos.net.device.DeviceEvent;
53import org.onlab.onos.net.device.DeviceListener;
tomd3097b02014-08-26 10:40:29 -070054import org.onlab.onos.net.device.DeviceProvider;
tom96dfcab2014-08-28 09:26:03 -070055import org.onlab.onos.net.device.DeviceProviderRegistry;
tomd3097b02014-08-26 10:40:29 -070056import org.onlab.onos.net.device.DeviceProviderService;
tom32f66842014-08-27 19:27:47 -070057import org.onlab.onos.net.device.DeviceService;
tom41a2c5f2014-09-19 09:20:35 -070058import org.onlab.onos.net.device.DeviceStore;
tomf80c9722014-09-24 14:49:18 -070059import org.onlab.onos.net.device.DeviceStoreDelegate;
tomd3097b02014-08-26 10:40:29 -070060import org.onlab.onos.net.device.PortDescription;
tom96dfcab2014-08-28 09:26:03 -070061import org.onlab.onos.net.provider.AbstractProviderRegistry;
tomd3097b02014-08-26 10:40:29 -070062import org.onlab.onos.net.provider.AbstractProviderService;
63import org.slf4j.Logger;
tomd3097b02014-08-26 10:40:29 -070064
tomd3097b02014-08-26 10:40:29 -070065/**
tome4729872014-09-23 00:37:37 -070066 * Provides implementation of the device SB & NB APIs.
tomd3097b02014-08-26 10:40:29 -070067 */
68@Component(immediate = true)
69@Service
tom41a2c5f2014-09-19 09:20:35 -070070public class DeviceManager
Thomas Vachuskad16ce182014-10-29 17:25:29 -070071 extends AbstractProviderRegistry<DeviceProvider, DeviceProviderService>
72 implements DeviceService, DeviceAdminService, DeviceProviderRegistry {
tom32f66842014-08-27 19:27:47 -070073
tome5ec3fd2014-09-04 15:18:06 -070074 private static final String DEVICE_ID_NULL = "Device ID cannot be null";
75 private static final String PORT_NUMBER_NULL = "Port number cannot be null";
76 private static final String DEVICE_DESCRIPTION_NULL = "Device description cannot be null";
77 private static final String PORT_DESCRIPTION_NULL = "Port description cannot be null";
78 private static final String ROLE_NULL = "Role cannot be null";
tomd3097b02014-08-26 10:40:29 -070079
tom5f38b3a2014-08-27 23:50:54 -070080 private final Logger log = getLogger(getClass());
tomd3097b02014-08-26 10:40:29 -070081
alshabib271a6202014-09-28 22:11:21 -070082 protected final AbstractListenerRegistry<DeviceEvent, DeviceListener> listenerRegistry =
83 new AbstractListenerRegistry<>();
tom32f66842014-08-27 19:27:47 -070084
alshabib339a3d92014-09-26 17:54:32 -070085 private final DeviceStoreDelegate delegate = new InternalStoreDelegate();
tomf80c9722014-09-24 14:49:18 -070086
tomc78acee2014-09-24 15:16:55 -070087 private final MastershipListener mastershipListener = new InternalMastershipListener();
tomb41d1ac2014-09-24 01:51:24 -070088
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -080089 private ScheduledExecutorService backgroundService;
90
tom41a2c5f2014-09-19 09:20:35 -070091 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
92 protected DeviceStore store;
tomd3097b02014-08-26 10:40:29 -070093
tom5f38b3a2014-08-27 23:50:54 -070094 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
tome5ec3fd2014-09-04 15:18:06 -070095 protected EventDeliveryService eventDispatcher;
tom5f38b3a2014-08-27 23:50:54 -070096
Ayaka Koshibea7f044e2014-09-23 16:56:20 -070097 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
tomb41d1ac2014-09-24 01:51:24 -070098 protected ClusterService clusterService;
99
100 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Ayaka Koshibea7f044e2014-09-23 16:56:20 -0700101 protected MastershipService mastershipService;
102
Yuta HIGUCHIbcac4992014-11-22 19:27:57 -0800103 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Ayaka Koshibe3de43ca2014-09-26 16:40:23 -0700104 protected MastershipTermService termService;
105
Madan Jampani61056bc2014-09-27 09:07:26 -0700106 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Yuta HIGUCHI093e83e2014-10-10 22:26:11 -0700107 protected DeviceClockProviderService deviceClockProviderService;
Madan Jampani61056bc2014-09-27 09:07:26 -0700108
tomd3097b02014-08-26 10:40:29 -0700109 @Activate
110 public void activate() {
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800111 backgroundService = Executors.newSingleThreadScheduledExecutor(namedThreads("device-manager-background"));
112
tomf80c9722014-09-24 14:49:18 -0700113 store.setDelegate(delegate);
tom96dfcab2014-08-28 09:26:03 -0700114 eventDispatcher.addSink(DeviceEvent.class, listenerRegistry);
tomb41d1ac2014-09-24 01:51:24 -0700115 mastershipService.addListener(mastershipListener);
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800116
117 backgroundService.scheduleWithFixedDelay(new Runnable() {
118
119 @Override
120 public void run() {
121 try {
122 mastershipCheck();
123 } catch (Exception e) {
124 log.error("Exception thrown during integrity check", e);
125 }
126 }
127 }, 1, 1, TimeUnit.MINUTES);
tomd3097b02014-08-26 10:40:29 -0700128 log.info("Started");
129 }
130
131 @Deactivate
132 public void deactivate() {
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800133 backgroundService.shutdown();
134
tomf80c9722014-09-24 14:49:18 -0700135 store.unsetDelegate(delegate);
tomb41d1ac2014-09-24 01:51:24 -0700136 mastershipService.removeListener(mastershipListener);
tom5f38b3a2014-08-27 23:50:54 -0700137 eventDispatcher.removeSink(DeviceEvent.class);
tomd3097b02014-08-26 10:40:29 -0700138 log.info("Stopped");
139 }
140
141 @Override
tomad2d2092014-09-06 23:24:20 -0700142 public int getDeviceCount() {
143 return store.getDeviceCount();
tomd3097b02014-08-26 10:40:29 -0700144 }
145
146 @Override
tom32f66842014-08-27 19:27:47 -0700147 public Iterable<Device> getDevices() {
tome5ec3fd2014-09-04 15:18:06 -0700148 return store.getDevices();
tomd3097b02014-08-26 10:40:29 -0700149 }
150
tom32f66842014-08-27 19:27:47 -0700151 @Override
Yuta HIGUCHIf1f2ac02014-11-26 14:02:22 -0800152 public Iterable<Device> getAvailableDevices() {
153 return store.getAvailableDevices();
154 }
155
156 @Override
tom32f66842014-08-27 19:27:47 -0700157 public Device getDevice(DeviceId deviceId) {
158 checkNotNull(deviceId, DEVICE_ID_NULL);
tom132b58a2014-08-28 16:11:28 -0700159 return store.getDevice(deviceId);
tom32f66842014-08-27 19:27:47 -0700160 }
161
162 @Override
tomad2d2092014-09-06 23:24:20 -0700163 public MastershipRole getRole(DeviceId deviceId) {
164 checkNotNull(deviceId, DEVICE_ID_NULL);
tomb41d1ac2014-09-24 01:51:24 -0700165 return mastershipService.getLocalRole(deviceId);
tomad2d2092014-09-06 23:24:20 -0700166 }
167
168 @Override
tom32f66842014-08-27 19:27:47 -0700169 public List<Port> getPorts(DeviceId deviceId) {
170 checkNotNull(deviceId, DEVICE_ID_NULL);
tom132b58a2014-08-28 16:11:28 -0700171 return store.getPorts(deviceId);
tom32f66842014-08-27 19:27:47 -0700172 }
173
174 @Override
175 public Port getPort(DeviceId deviceId, PortNumber portNumber) {
176 checkNotNull(deviceId, DEVICE_ID_NULL);
177 checkNotNull(portNumber, PORT_NUMBER_NULL);
tom132b58a2014-08-28 16:11:28 -0700178 return store.getPort(deviceId, portNumber);
tom32f66842014-08-27 19:27:47 -0700179 }
180
181 @Override
tomff7eb7c2014-09-08 12:49:03 -0700182 public boolean isAvailable(DeviceId deviceId) {
183 checkNotNull(deviceId, DEVICE_ID_NULL);
184 return store.isAvailable(deviceId);
185 }
186
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700187 // Check a device for control channel connectivity.
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700188 private boolean isReachable(DeviceId deviceId) {
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800189 if (deviceId == null) {
190 return false;
191 }
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700192 DeviceProvider provider = getProvider(deviceId);
193 if (provider != null) {
194 return provider.isReachable(deviceId);
195 } else {
Yuta HIGUCHI72669c42014-11-13 14:48:17 -0800196 log.debug("Provider not found for {}", deviceId);
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700197 return false;
198 }
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700199 }
200
tome5ec3fd2014-09-04 15:18:06 -0700201 @Override
202 public void removeDevice(DeviceId deviceId) {
203 checkNotNull(deviceId, DEVICE_ID_NULL);
204 DeviceEvent event = store.removeDevice(deviceId);
tom0efbb1d2014-09-09 11:54:28 -0700205 if (event != null) {
206 log.info("Device {} administratively removed", deviceId);
207 post(event);
208 }
tome5ec3fd2014-09-04 15:18:06 -0700209 }
210
tom7869ad92014-09-09 14:32:08 -0700211 @Override
212 public void addListener(DeviceListener listener) {
213 listenerRegistry.addListener(listener);
214 }
215
216 @Override
217 public void removeListener(DeviceListener listener) {
218 listenerRegistry.removeListener(listener);
219 }
220
221 @Override
alshabibb7b40632014-09-28 21:30:00 -0700222 protected DeviceProviderService createProviderService(
223 DeviceProvider provider) {
tom7869ad92014-09-09 14:32:08 -0700224 return new InternalDeviceProviderService(provider);
225 }
226
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800227 /**
228 * Checks if all the reachable devices have a valid mastership role.
229 */
230 private void mastershipCheck() {
231 log.debug("Checking mastership");
232 for (Device device : getDevices()) {
233 final DeviceId deviceId = device.id();
234 log.debug("Checking device {}", deviceId);
235
236 if (!isReachable(deviceId)) {
237 continue;
238 }
239
240 if (mastershipService.getLocalRole(deviceId) != NONE) {
241 continue;
242 }
243
244 log.info("{} is reachable but did not have a valid role, reasserting", deviceId);
245
246 // isReachable but was not MASTER or STANDBY, get a role and apply
247 // Note: NONE triggers request to MastershipService
248 reassertRole(deviceId, NONE);
249 }
250 }
251
tomd3097b02014-08-26 10:40:29 -0700252 // Personalized device provider service issued to the supplied provider.
tomdc361b62014-09-09 20:36:52 -0700253 private class InternalDeviceProviderService
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700254 extends AbstractProviderService<DeviceProvider>
255 implements DeviceProviderService {
tomd3097b02014-08-26 10:40:29 -0700256
tomcfde0622014-09-09 11:02:42 -0700257 InternalDeviceProviderService(DeviceProvider provider) {
tomd3097b02014-08-26 10:40:29 -0700258 super(provider);
259 }
260
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700261 /**
262 * Apply role in reaction to provider event.
263 *
264 * @param deviceId device identifier
265 * @param newRole new role to apply to the device
266 * @return true if the request was sent to provider
267 */
268 private boolean applyRole(DeviceId deviceId, MastershipRole newRole) {
269
270 if (newRole.equals(MastershipRole.NONE)) {
271 //no-op
272 return true;
273 }
274
275 DeviceProvider provider = provider();
276 if (provider == null) {
277 log.warn("Provider for {} was not found. Cannot apply role {}", deviceId, newRole);
278 return false;
279 }
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700280 provider.roleChanged(deviceId, newRole);
281 // not triggering probe when triggered by provider service event
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700282
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700283 return true;
284 }
285
286
tomd3097b02014-08-26 10:40:29 -0700287 @Override
alshabibb7b40632014-09-28 21:30:00 -0700288 public void deviceConnected(DeviceId deviceId,
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700289 DeviceDescription deviceDescription) {
tom32f66842014-08-27 19:27:47 -0700290 checkNotNull(deviceId, DEVICE_ID_NULL);
291 checkNotNull(deviceDescription, DEVICE_DESCRIPTION_NULL);
tomeadbb462014-09-07 16:10:19 -0700292 checkValidity();
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700293
294 log.info("Device {} connected", deviceId);
Yuta HIGUCHIdfe6e3b2014-10-30 11:31:51 -0700295 final NodeId myNodeId = clusterService.getLocalNode().id();
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700296
297 // check my Role
298 mastershipService.requestRoleFor(deviceId);
299 final MastershipTerm term = termService.getMastershipTerm(deviceId);
Yuta HIGUCHIdfe6e3b2014-10-30 11:31:51 -0700300 if (!myNodeId.equals(term.master())) {
Yuta HIGUCHI2d3cd312014-10-31 11:38:04 -0700301 log.info("Role of this node is STANDBY for {}", deviceId);
302 // TODO: Do we need to explicitly tell the Provider that
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700303 // this instance is not the MASTER
304 applyRole(deviceId, MastershipRole.STANDBY);
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700305 return;
306 }
Yuta HIGUCHI2d3cd312014-10-31 11:38:04 -0700307 log.info("Role of this node is MASTER for {}", deviceId);
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700308
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700309 // tell clock provider if this instance is the master
Yuta HIGUCHI093e83e2014-10-10 22:26:11 -0700310 deviceClockProviderService.setMastershipTerm(deviceId, term);
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700311
tome5ec3fd2014-09-04 15:18:06 -0700312 DeviceEvent event = store.createOrUpdateDevice(provider().id(),
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700313 deviceId, deviceDescription);
tom80c0e5e2014-09-08 18:08:58 -0700314
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700315 applyRole(deviceId, MastershipRole.MASTER);
316
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700317 // If there was a change of any kind, tell the provider
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700318 // that this instance is the master.
tom80c0e5e2014-09-08 18:08:58 -0700319 if (event != null) {
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700320 log.trace("event: {} {}", event.type(), event);
tom568581d2014-09-08 20:13:36 -0700321 post(event);
tom80c0e5e2014-09-08 18:08:58 -0700322 }
tomd3097b02014-08-26 10:40:29 -0700323 }
324
325 @Override
326 public void deviceDisconnected(DeviceId deviceId) {
tom32f66842014-08-27 19:27:47 -0700327 checkNotNull(deviceId, DEVICE_ID_NULL);
tomeadbb462014-09-07 16:10:19 -0700328 checkValidity();
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700329
Yuta HIGUCHI2d3cd312014-10-31 11:38:04 -0700330 log.info("Device {} disconnected from this node", deviceId);
Ayaka Koshibed9f693e2014-09-29 18:04:54 -0700331
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700332 DeviceEvent event = null;
333 try {
334 event = store.markOffline(deviceId);
335 } catch (IllegalStateException e) {
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700336 log.warn("Failed to mark {} offline", deviceId);
337 // only the MASTER should be marking off-line in normal cases,
338 // but if I was the last STANDBY connection, etc. and no one else
339 // was there to mark the device offline, this instance may need to
340 // temporarily request for Master Role and mark offline.
341
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700342 //there are times when this node will correctly have mastership, BUT
343 //that isn't reflected in the ClockManager before the device disconnects.
344 //we want to let go of the device anyways, so make sure this happens.
Yuta HIGUCHI0722fb22014-10-19 01:16:33 -0700345
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700346 // FIXME: Store semantics leaking out as IllegalStateException.
347 // Consider revising store API to handle this scenario.
348
349 MastershipRole role = mastershipService.requestRoleFor(deviceId);
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700350 MastershipTerm term = termService.getMastershipTerm(deviceId);
Yuta HIGUCHI0722fb22014-10-19 01:16:33 -0700351 final NodeId myNodeId = clusterService.getLocalNode().id();
352 // TODO: Move this type of check inside device clock manager, etc.
353 if (myNodeId.equals(term.master())) {
Yuta HIGUCHI2d3cd312014-10-31 11:38:04 -0700354 log.info("Retry marking {} offline", deviceId);
Yuta HIGUCHI0722fb22014-10-19 01:16:33 -0700355 deviceClockProviderService.setMastershipTerm(deviceId, term);
356 event = store.markOffline(deviceId);
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700357 } else {
Yuta HIGUCHI2d3cd312014-10-31 11:38:04 -0700358 log.info("Failed again marking {} offline. {}", deviceId, role);
Yuta HIGUCHI0722fb22014-10-19 01:16:33 -0700359 }
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700360 } finally {
361 //relinquish master role and ability to be backup.
362 mastershipService.relinquishMastership(deviceId);
363
364 if (event != null) {
Yuta HIGUCHI2d3cd312014-10-31 11:38:04 -0700365 log.info("Device {} disconnected from cluster", deviceId);
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700366 post(event);
367 }
tom0efbb1d2014-09-09 11:54:28 -0700368 }
tomd3097b02014-08-26 10:40:29 -0700369 }
370
371 @Override
alshabibb7b40632014-09-28 21:30:00 -0700372 public void updatePorts(DeviceId deviceId,
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700373 List<PortDescription> portDescriptions) {
tom32f66842014-08-27 19:27:47 -0700374 checkNotNull(deviceId, DEVICE_ID_NULL);
alshabibb7b40632014-09-28 21:30:00 -0700375 checkNotNull(portDescriptions,
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700376 "Port descriptions list cannot be null");
tomeadbb462014-09-07 16:10:19 -0700377 checkValidity();
Yuta HIGUCHI13c0b872014-10-30 18:09:22 -0700378 if (!deviceClockProviderService.isTimestampAvailable(deviceId)) {
379 // Never been a master for this device
380 // any update will be ignored.
381 log.trace("Ignoring {} port updates on standby node. {}", deviceId, portDescriptions);
382 return;
383 }
384
Yuta HIGUCHI5f6739c2014-10-01 14:04:01 -0700385 List<DeviceEvent> events = store.updatePorts(this.provider().id(),
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700386 deviceId, portDescriptions);
tom32f66842014-08-27 19:27:47 -0700387 for (DeviceEvent event : events) {
388 post(event);
389 }
tomd3097b02014-08-26 10:40:29 -0700390 }
391
392 @Override
alshabibb7b40632014-09-28 21:30:00 -0700393 public void portStatusChanged(DeviceId deviceId,
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700394 PortDescription portDescription) {
tom32f66842014-08-27 19:27:47 -0700395 checkNotNull(deviceId, DEVICE_ID_NULL);
396 checkNotNull(portDescription, PORT_DESCRIPTION_NULL);
tomeadbb462014-09-07 16:10:19 -0700397 checkValidity();
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700398
Yuta HIGUCHI13c0b872014-10-30 18:09:22 -0700399 if (!deviceClockProviderService.isTimestampAvailable(deviceId)) {
400 // Never been a master for this device
401 // any update will be ignored.
402 log.trace("Ignoring {} port update on standby node. {}", deviceId, portDescription);
403 return;
404 }
405
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700406 final DeviceEvent event = store.updatePortStatus(this.provider().id(),
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700407 deviceId, portDescription);
tomff7eb7c2014-09-08 12:49:03 -0700408 if (event != null) {
alshabibb7b40632014-09-28 21:30:00 -0700409 log.info("Device {} port {} status changed", deviceId, event
410 .port().number());
tom0efbb1d2014-09-09 11:54:28 -0700411 post(event);
tomff7eb7c2014-09-08 12:49:03 -0700412 }
tomd3097b02014-08-26 10:40:29 -0700413 }
tom3f2bbd72014-09-24 12:07:58 -0700414
415 @Override
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700416 public void receivedRoleReply(
417 DeviceId deviceId, MastershipRole requested, MastershipRole response) {
418 // Several things can happen here:
419 // 1. request and response match
420 // 2. request and response don't match
421 // 3. MastershipRole and requested match (and 1 or 2 are true)
422 // 4. MastershipRole and requested don't match (and 1 or 2 are true)
423 //
424 // 2, 4, and 3 with case 2 are failure modes.
425
tom3f2bbd72014-09-24 12:07:58 -0700426 // FIXME: implement response to this notification
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700427
428 log.info("got reply to a role request for {}: asked for {}, and got {}",
429 deviceId, requested, response);
430
431 if (requested == null && response == null) {
432 // something was off with DeviceProvider, maybe check channel too?
433 log.warn("Failed to assert role [{}] onto Device {}", requested, deviceId);
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700434 mastershipService.relinquishMastership(deviceId);
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700435 return;
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700436 }
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700437
438 if (requested.equals(response)) {
439 if (requested.equals(mastershipService.getLocalRole(deviceId))) {
440
441 return;
442 } else {
443 return;
444 // FIXME roleManager got the device to comply, but doesn't agree with
445 // the store; use the store's view, then try to reassert.
446 }
447 } else {
448 // we didn't get back what we asked for. Reelect someone else.
449 log.warn("Failed to assert role [{}] onto Device {}", response, deviceId);
450 if (response == MastershipRole.MASTER) {
451 mastershipService.relinquishMastership(deviceId);
452 // TODO: Shouldn't we be triggering event?
453 //final Device device = getDevice(deviceId);
454 //post(new DeviceEvent(DEVICE_MASTERSHIP_CHANGED, device));
455 }
456 }
457
tom3f2bbd72014-09-24 12:07:58 -0700458 }
tomd3097b02014-08-26 10:40:29 -0700459 }
tom32f66842014-08-27 19:27:47 -0700460
tomeadbb462014-09-07 16:10:19 -0700461 // Posts the specified event to the local event dispatcher.
tom32f66842014-08-27 19:27:47 -0700462 private void post(DeviceEvent event) {
463 if (event != null && eventDispatcher != null) {
464 eventDispatcher.post(event);
465 }
466 }
467
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800468 // Applies the specified role to the device; ignores NONE
469 /**
470 * Apply role to device and send probe if MASTER.
471 *
472 * @param deviceId device identifier
473 * @param newRole new role to apply to the device
474 * @return true if the request was sent to provider
475 */
476 private boolean applyRoleAndProbe(DeviceId deviceId, MastershipRole newRole) {
477 if (newRole.equals(MastershipRole.NONE)) {
478 //no-op
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700479 return true;
480 }
Ayaka Koshibe317245a2014-10-29 00:34:43 -0700481
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800482 DeviceProvider provider = getProvider(deviceId);
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800483 if (provider == null) {
484 log.warn("Provider for {} was not found. Cannot apply role {}", deviceId, newRole);
485 return false;
486 }
487 provider.roleChanged(deviceId, newRole);
488
489 if (newRole.equals(MastershipRole.MASTER)) {
490 // only trigger event when request was sent to provider
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800491 provider.triggerProbe(deviceId);
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800492 }
493 return true;
494 }
495
496 /**
497 * Reaasert role for specified device connected to this node.
498 *
499 * @param did device identifier
500 * @param nextRole role to apply. If NONE is specified,
501 * it will ask mastership service for a role and apply it.
502 */
503 private void reassertRole(final DeviceId did,
504 final MastershipRole nextRole) {
505
506 final NodeId myNodeId = clusterService.getLocalNode().id();
507 MastershipRole myNextRole = nextRole;
508 if (myNextRole == NONE) {
509 mastershipService.requestRoleFor(did);
510 MastershipTerm term = termService.getMastershipTerm(did);
511 if (myNodeId.equals(term.master())) {
512 myNextRole = MASTER;
513 } else {
514 myNextRole = STANDBY;
515 }
516 }
517
518 switch (myNextRole) {
519 case MASTER:
520 final Device device = getDevice(did);
521 if ((device != null) && !isAvailable(did)) {
522 //flag the device as online. Is there a better way to do this?
523 DefaultDeviceDescription deviceDescription
524 = new DefaultDeviceDescription(did.uri(),
525 device.type(),
526 device.manufacturer(),
527 device.hwVersion(),
528 device.swVersion(),
529 device.serialNumber(),
530 device.chassisId());
531 DeviceEvent devEvent =
532 store.createOrUpdateDevice(device.providerId(), did,
533 deviceDescription);
534 post(devEvent);
535 }
536 // TODO: should apply role only if there is mismatch
537 log.info("Applying role {} to {}", myNextRole, did);
538 if (!applyRoleAndProbe(did, MASTER)) {
539 // immediately failed to apply role
540 mastershipService.relinquishMastership(did);
541 // FIXME disconnect?
542 }
543 break;
544 case STANDBY:
545 log.info("Applying role {} to {}", myNextRole, did);
546 if (!applyRoleAndProbe(did, STANDBY)) {
547 // immediately failed to apply role
548 mastershipService.relinquishMastership(did);
549 // FIXME disconnect?
550 }
551 break;
552 case NONE:
553 default:
554 // should never reach here
555 log.error("You didn't see anything. I did not exist.");
556 break;
557 }
558 }
559
560 // Intercepts mastership events
561 private class InternalMastershipListener implements MastershipListener {
562
tomb41d1ac2014-09-24 01:51:24 -0700563 @Override
564 public void event(MastershipEvent event) {
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700565
566 if (event.type() != MastershipEvent.Type.MASTER_CHANGED) {
567 // Don't care if backup list changed.
568 return;
569 }
570
Ayaka Koshibe4c891272014-10-08 17:14:16 -0700571 final DeviceId did = event.subject();
Ayaka Koshibeea5b4ce2014-10-11 14:17:17 -0700572 final NodeId myNodeId = clusterService.getLocalNode().id();
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700573
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700574 // myRole suggested by MastershipService
575 MastershipRole myNextRole;
Yuta HIGUCHI67dce882014-10-21 21:13:26 -0700576 if (myNodeId.equals(event.roleInfo().master())) {
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700577 // confirm latest info
Ayaka Koshibeea5b4ce2014-10-11 14:17:17 -0700578 MastershipTerm term = termService.getMastershipTerm(did);
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700579 final boolean iHaveControl = myNodeId.equals(term.master());
580 if (iHaveControl) {
Yuta HIGUCHI38b4d9d2014-10-30 11:32:41 -0700581 deviceClockProviderService.setMastershipTerm(did, term);
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700582 myNextRole = MASTER;
Yuta HIGUCHI38b4d9d2014-10-30 11:32:41 -0700583 } else {
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700584 myNextRole = STANDBY;
585 }
586 } else if (event.roleInfo().backups().contains(myNodeId)) {
587 myNextRole = STANDBY;
588 } else {
589 myNextRole = NONE;
590 }
591
592
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700593 final boolean isReachable = isReachable(did);
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700594 if (!isReachable) {
595 // device is not connected to this node
596 if (myNextRole != NONE) {
597 log.warn("Node was instructed to be {} role for {}, "
598 + "but this node cannot reach the device. "
599 + "Relinquishing role. ",
600 myNextRole, did);
601 mastershipService.relinquishMastership(did);
602 }
603 return;
604 }
605
606 // device is connected to this node:
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800607 reassertRole(did, myNextRole);
Ayaka Koshibe317245a2014-10-29 00:34:43 -0700608 }
tomb41d1ac2014-09-24 01:51:24 -0700609 }
tomf80c9722014-09-24 14:49:18 -0700610
611 // Store delegate to re-post events emitted from the store.
alshabib271a6202014-09-28 22:11:21 -0700612 private class InternalStoreDelegate
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700613 implements DeviceStoreDelegate {
tomf80c9722014-09-24 14:49:18 -0700614 @Override
615 public void notify(DeviceEvent event) {
616 post(event);
617 }
618 }
tomd3097b02014-08-26 10:40:29 -0700619}