blob: 842ae106b558fff5c887b6f423f2c79ff348d22d [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;
sangho538108b2015-04-08 14:29:20 -070053import org.onosproject.net.device.PortStatistics;
Brian O'Connorabafb502014-12-02 22:26:20 -080054import org.onosproject.net.provider.AbstractProviderRegistry;
55import org.onosproject.net.provider.AbstractProviderService;
tomd3097b02014-08-26 10:40:29 -070056import org.slf4j.Logger;
tomd3097b02014-08-26 10:40:29 -070057
sangho538108b2015-04-08 14:29:20 -070058import java.util.Collection;
Jonathan Hart2f669362015-02-11 16:19:20 -080059import java.util.List;
Jonathan Hart2f669362015-02-11 16:19:20 -080060import java.util.concurrent.ScheduledExecutorService;
61import java.util.concurrent.TimeUnit;
62
63import static com.google.common.base.Preconditions.checkNotNull;
Thomas Vachuska6f94ded2015-02-21 14:02:38 -080064import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
65import static org.onlab.util.Tools.groupedThreads;
66import static org.onosproject.net.MastershipRole.*;
Jonathan Hart2f669362015-02-11 16:19:20 -080067import 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 Vachuska6f94ded2015-02-21 14:02:38 -0800115 backgroundService = newSingleThreadScheduledExecutor(groupedThreads("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
sangho538108b2015-04-08 14:29:20 -0700179 public List<PortStatistics> getPortStatistics(DeviceId deviceId) {
180 checkNotNull(deviceId, DEVICE_ID_NULL);
181 return store.getPortStatistics(deviceId);
182 }
183
184 @Override
tom32f66842014-08-27 19:27:47 -0700185 public Port getPort(DeviceId deviceId, PortNumber portNumber) {
186 checkNotNull(deviceId, DEVICE_ID_NULL);
187 checkNotNull(portNumber, PORT_NUMBER_NULL);
tom132b58a2014-08-28 16:11:28 -0700188 return store.getPort(deviceId, portNumber);
tom32f66842014-08-27 19:27:47 -0700189 }
190
191 @Override
tomff7eb7c2014-09-08 12:49:03 -0700192 public boolean isAvailable(DeviceId deviceId) {
193 checkNotNull(deviceId, DEVICE_ID_NULL);
194 return store.isAvailable(deviceId);
195 }
196
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700197 // Check a device for control channel connectivity.
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700198 private boolean isReachable(DeviceId deviceId) {
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800199 if (deviceId == null) {
200 return false;
201 }
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700202 DeviceProvider provider = getProvider(deviceId);
203 if (provider != null) {
204 return provider.isReachable(deviceId);
205 } else {
Yuta HIGUCHI72669c42014-11-13 14:48:17 -0800206 log.debug("Provider not found for {}", deviceId);
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700207 return false;
208 }
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700209 }
210
tome5ec3fd2014-09-04 15:18:06 -0700211 @Override
212 public void removeDevice(DeviceId deviceId) {
213 checkNotNull(deviceId, DEVICE_ID_NULL);
214 DeviceEvent event = store.removeDevice(deviceId);
tom0efbb1d2014-09-09 11:54:28 -0700215 if (event != null) {
216 log.info("Device {} administratively removed", deviceId);
217 post(event);
218 }
tome5ec3fd2014-09-04 15:18:06 -0700219 }
220
tom7869ad92014-09-09 14:32:08 -0700221 @Override
222 public void addListener(DeviceListener listener) {
223 listenerRegistry.addListener(listener);
224 }
225
226 @Override
227 public void removeListener(DeviceListener listener) {
228 listenerRegistry.removeListener(listener);
229 }
230
231 @Override
alshabibb7b40632014-09-28 21:30:00 -0700232 protected DeviceProviderService createProviderService(
233 DeviceProvider provider) {
tom7869ad92014-09-09 14:32:08 -0700234 return new InternalDeviceProviderService(provider);
235 }
236
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800237 /**
238 * Checks if all the reachable devices have a valid mastership role.
239 */
240 private void mastershipCheck() {
241 log.debug("Checking mastership");
242 for (Device device : getDevices()) {
243 final DeviceId deviceId = device.id();
Jonathan Hart2f669362015-02-11 16:19:20 -0800244 log.trace("Checking device {}", deviceId);
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800245
246 if (!isReachable(deviceId)) {
247 continue;
248 }
249
250 if (mastershipService.getLocalRole(deviceId) != NONE) {
251 continue;
252 }
253
254 log.info("{} is reachable but did not have a valid role, reasserting", deviceId);
255
256 // isReachable but was not MASTER or STANDBY, get a role and apply
257 // Note: NONE triggers request to MastershipService
258 reassertRole(deviceId, NONE);
259 }
260 }
261
tomd3097b02014-08-26 10:40:29 -0700262 // Personalized device provider service issued to the supplied provider.
tomdc361b62014-09-09 20:36:52 -0700263 private class InternalDeviceProviderService
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700264 extends AbstractProviderService<DeviceProvider>
265 implements DeviceProviderService {
tomd3097b02014-08-26 10:40:29 -0700266
tomcfde0622014-09-09 11:02:42 -0700267 InternalDeviceProviderService(DeviceProvider provider) {
tomd3097b02014-08-26 10:40:29 -0700268 super(provider);
269 }
270
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700271 /**
272 * Apply role in reaction to provider event.
273 *
274 * @param deviceId device identifier
275 * @param newRole new role to apply to the device
276 * @return true if the request was sent to provider
277 */
278 private boolean applyRole(DeviceId deviceId, MastershipRole newRole) {
279
280 if (newRole.equals(MastershipRole.NONE)) {
281 //no-op
282 return true;
283 }
284
285 DeviceProvider provider = provider();
286 if (provider == null) {
287 log.warn("Provider for {} was not found. Cannot apply role {}", deviceId, newRole);
288 return false;
289 }
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700290 provider.roleChanged(deviceId, newRole);
291 // not triggering probe when triggered by provider service event
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700292
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700293 return true;
294 }
295
296
tomd3097b02014-08-26 10:40:29 -0700297 @Override
alshabibb7b40632014-09-28 21:30:00 -0700298 public void deviceConnected(DeviceId deviceId,
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700299 DeviceDescription deviceDescription) {
tom32f66842014-08-27 19:27:47 -0700300 checkNotNull(deviceId, DEVICE_ID_NULL);
301 checkNotNull(deviceDescription, DEVICE_DESCRIPTION_NULL);
tomeadbb462014-09-07 16:10:19 -0700302 checkValidity();
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700303
304 log.info("Device {} connected", deviceId);
Yuta HIGUCHIdfe6e3b2014-10-30 11:31:51 -0700305 final NodeId myNodeId = clusterService.getLocalNode().id();
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700306
307 // check my Role
308 mastershipService.requestRoleFor(deviceId);
309 final MastershipTerm term = termService.getMastershipTerm(deviceId);
Yuta HIGUCHIdfe6e3b2014-10-30 11:31:51 -0700310 if (!myNodeId.equals(term.master())) {
Yuta HIGUCHI2d3cd312014-10-31 11:38:04 -0700311 log.info("Role of this node is STANDBY for {}", deviceId);
312 // TODO: Do we need to explicitly tell the Provider that
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700313 // this instance is not the MASTER
314 applyRole(deviceId, MastershipRole.STANDBY);
Marc De Leenheerb473b9d2015-02-06 15:21:03 -0800315 } else {
316 log.info("Role of this node is MASTER for {}", deviceId);
317 // tell clock provider if this instance is the master
318 deviceClockProviderService.setMastershipTerm(deviceId, term);
319 applyRole(deviceId, MastershipRole.MASTER);
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700320 }
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700321
tome5ec3fd2014-09-04 15:18:06 -0700322 DeviceEvent event = store.createOrUpdateDevice(provider().id(),
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700323 deviceId, deviceDescription);
tom80c0e5e2014-09-08 18:08:58 -0700324
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700325 // If there was a change of any kind, tell the provider
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700326 // that this instance is the master.
tom80c0e5e2014-09-08 18:08:58 -0700327 if (event != null) {
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700328 log.trace("event: {} {}", event.type(), event);
tom568581d2014-09-08 20:13:36 -0700329 post(event);
tom80c0e5e2014-09-08 18:08:58 -0700330 }
tomd3097b02014-08-26 10:40:29 -0700331 }
332
333 @Override
334 public void deviceDisconnected(DeviceId deviceId) {
tom32f66842014-08-27 19:27:47 -0700335 checkNotNull(deviceId, DEVICE_ID_NULL);
tomeadbb462014-09-07 16:10:19 -0700336 checkValidity();
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700337
Yuta HIGUCHI2d3cd312014-10-31 11:38:04 -0700338 log.info("Device {} disconnected from this node", deviceId);
Ayaka Koshibed9f693e2014-09-29 18:04:54 -0700339
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700340 DeviceEvent event = null;
alshabibafc514a2014-12-01 14:44:05 -0800341 List<Port> ports = store.getPorts(deviceId);
342 List<PortDescription> descs = Lists.newArrayList();
343 ports.forEach(port ->
344 descs.add(new DefaultPortDescription(port.number(),
345 false, port.type(),
346 port.portSpeed())));
347 store.updatePorts(this.provider().id(), deviceId, descs);
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700348 try {
349 event = store.markOffline(deviceId);
350 } catch (IllegalStateException e) {
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700351 log.warn("Failed to mark {} offline", deviceId);
352 // only the MASTER should be marking off-line in normal cases,
353 // but if I was the last STANDBY connection, etc. and no one else
354 // was there to mark the device offline, this instance may need to
355 // temporarily request for Master Role and mark offline.
356
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700357 //there are times when this node will correctly have mastership, BUT
358 //that isn't reflected in the ClockManager before the device disconnects.
359 //we want to let go of the device anyways, so make sure this happens.
Yuta HIGUCHI0722fb22014-10-19 01:16:33 -0700360
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700361 // FIXME: Store semantics leaking out as IllegalStateException.
362 // Consider revising store API to handle this scenario.
363
364 MastershipRole role = mastershipService.requestRoleFor(deviceId);
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700365 MastershipTerm term = termService.getMastershipTerm(deviceId);
Yuta HIGUCHI0722fb22014-10-19 01:16:33 -0700366 final NodeId myNodeId = clusterService.getLocalNode().id();
367 // TODO: Move this type of check inside device clock manager, etc.
368 if (myNodeId.equals(term.master())) {
Yuta HIGUCHI2d3cd312014-10-31 11:38:04 -0700369 log.info("Retry marking {} offline", deviceId);
Yuta HIGUCHI0722fb22014-10-19 01:16:33 -0700370 deviceClockProviderService.setMastershipTerm(deviceId, term);
371 event = store.markOffline(deviceId);
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700372 } else {
Yuta HIGUCHI2d3cd312014-10-31 11:38:04 -0700373 log.info("Failed again marking {} offline. {}", deviceId, role);
Yuta HIGUCHI0722fb22014-10-19 01:16:33 -0700374 }
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700375 } finally {
376 //relinquish master role and ability to be backup.
377 mastershipService.relinquishMastership(deviceId);
378
379 if (event != null) {
Yuta HIGUCHI2d3cd312014-10-31 11:38:04 -0700380 log.info("Device {} disconnected from cluster", deviceId);
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700381 post(event);
382 }
tom0efbb1d2014-09-09 11:54:28 -0700383 }
tomd3097b02014-08-26 10:40:29 -0700384 }
385
386 @Override
alshabibb7b40632014-09-28 21:30:00 -0700387 public void updatePorts(DeviceId deviceId,
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700388 List<PortDescription> portDescriptions) {
tom32f66842014-08-27 19:27:47 -0700389 checkNotNull(deviceId, DEVICE_ID_NULL);
alshabibb7b40632014-09-28 21:30:00 -0700390 checkNotNull(portDescriptions,
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700391 "Port descriptions list cannot be null");
tomeadbb462014-09-07 16:10:19 -0700392 checkValidity();
Yuta HIGUCHI13c0b872014-10-30 18:09:22 -0700393 if (!deviceClockProviderService.isTimestampAvailable(deviceId)) {
394 // Never been a master for this device
395 // any update will be ignored.
396 log.trace("Ignoring {} port updates on standby node. {}", deviceId, portDescriptions);
397 return;
398 }
399
Yuta HIGUCHI5f6739c2014-10-01 14:04:01 -0700400 List<DeviceEvent> events = store.updatePorts(this.provider().id(),
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700401 deviceId, portDescriptions);
tom32f66842014-08-27 19:27:47 -0700402 for (DeviceEvent event : events) {
403 post(event);
404 }
tomd3097b02014-08-26 10:40:29 -0700405 }
406
407 @Override
alshabibb7b40632014-09-28 21:30:00 -0700408 public void portStatusChanged(DeviceId deviceId,
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700409 PortDescription portDescription) {
tom32f66842014-08-27 19:27:47 -0700410 checkNotNull(deviceId, DEVICE_ID_NULL);
411 checkNotNull(portDescription, PORT_DESCRIPTION_NULL);
tomeadbb462014-09-07 16:10:19 -0700412 checkValidity();
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700413
Yuta HIGUCHI13c0b872014-10-30 18:09:22 -0700414 if (!deviceClockProviderService.isTimestampAvailable(deviceId)) {
415 // Never been a master for this device
416 // any update will be ignored.
417 log.trace("Ignoring {} port update on standby node. {}", deviceId, portDescription);
418 return;
419 }
420
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700421 final DeviceEvent event = store.updatePortStatus(this.provider().id(),
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700422 deviceId, portDescription);
tomff7eb7c2014-09-08 12:49:03 -0700423 if (event != null) {
alshabibb7b40632014-09-28 21:30:00 -0700424 log.info("Device {} port {} status changed", deviceId, event
425 .port().number());
tom0efbb1d2014-09-09 11:54:28 -0700426 post(event);
tomff7eb7c2014-09-08 12:49:03 -0700427 }
tomd3097b02014-08-26 10:40:29 -0700428 }
tom3f2bbd72014-09-24 12:07:58 -0700429
430 @Override
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700431 public void receivedRoleReply(
432 DeviceId deviceId, MastershipRole requested, MastershipRole response) {
433 // Several things can happen here:
434 // 1. request and response match
435 // 2. request and response don't match
436 // 3. MastershipRole and requested match (and 1 or 2 are true)
437 // 4. MastershipRole and requested don't match (and 1 or 2 are true)
438 //
439 // 2, 4, and 3 with case 2 are failure modes.
440
tom3f2bbd72014-09-24 12:07:58 -0700441 // FIXME: implement response to this notification
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700442
443 log.info("got reply to a role request for {}: asked for {}, and got {}",
444 deviceId, requested, response);
445
446 if (requested == null && response == null) {
447 // something was off with DeviceProvider, maybe check channel too?
448 log.warn("Failed to assert role [{}] onto Device {}", requested, deviceId);
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700449 mastershipService.relinquishMastership(deviceId);
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700450 return;
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700451 }
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700452
453 if (requested.equals(response)) {
454 if (requested.equals(mastershipService.getLocalRole(deviceId))) {
455
456 return;
457 } else {
458 return;
459 // FIXME roleManager got the device to comply, but doesn't agree with
460 // the store; use the store's view, then try to reassert.
461 }
462 } else {
463 // we didn't get back what we asked for. Reelect someone else.
464 log.warn("Failed to assert role [{}] onto Device {}", response, deviceId);
465 if (response == MastershipRole.MASTER) {
466 mastershipService.relinquishMastership(deviceId);
467 // TODO: Shouldn't we be triggering event?
468 //final Device device = getDevice(deviceId);
469 //post(new DeviceEvent(DEVICE_MASTERSHIP_CHANGED, device));
470 }
471 }
472
tom3f2bbd72014-09-24 12:07:58 -0700473 }
sangho538108b2015-04-08 14:29:20 -0700474
475 @Override
476 public void updatePortStatistics(DeviceId deviceId, Collection<PortStatistics> portStatistics) {
477 checkNotNull(deviceId, DEVICE_ID_NULL);
478 checkNotNull(portStatistics,
479 "Port statistics list cannot be null");
480 checkValidity();
481
482 DeviceEvent event = store.updatePortStatistics(this.provider().id(),
483 deviceId, portStatistics);
484
485 post(event);
486 }
tomd3097b02014-08-26 10:40:29 -0700487 }
tom32f66842014-08-27 19:27:47 -0700488
tomeadbb462014-09-07 16:10:19 -0700489 // Posts the specified event to the local event dispatcher.
tom32f66842014-08-27 19:27:47 -0700490 private void post(DeviceEvent event) {
491 if (event != null && eventDispatcher != null) {
492 eventDispatcher.post(event);
493 }
494 }
495
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800496 // Applies the specified role to the device; ignores NONE
497 /**
498 * Apply role to device and send probe if MASTER.
499 *
500 * @param deviceId device identifier
501 * @param newRole new role to apply to the device
502 * @return true if the request was sent to provider
503 */
504 private boolean applyRoleAndProbe(DeviceId deviceId, MastershipRole newRole) {
505 if (newRole.equals(MastershipRole.NONE)) {
506 //no-op
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700507 return true;
508 }
Ayaka Koshibe317245a2014-10-29 00:34:43 -0700509
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800510 DeviceProvider provider = getProvider(deviceId);
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800511 if (provider == null) {
512 log.warn("Provider for {} was not found. Cannot apply role {}", deviceId, newRole);
513 return false;
514 }
515 provider.roleChanged(deviceId, newRole);
516
517 if (newRole.equals(MastershipRole.MASTER)) {
518 // only trigger event when request was sent to provider
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800519 provider.triggerProbe(deviceId);
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800520 }
521 return true;
522 }
523
524 /**
525 * Reaasert role for specified device connected to this node.
526 *
527 * @param did device identifier
528 * @param nextRole role to apply. If NONE is specified,
529 * it will ask mastership service for a role and apply it.
530 */
531 private void reassertRole(final DeviceId did,
532 final MastershipRole nextRole) {
533
534 final NodeId myNodeId = clusterService.getLocalNode().id();
535 MastershipRole myNextRole = nextRole;
536 if (myNextRole == NONE) {
537 mastershipService.requestRoleFor(did);
538 MastershipTerm term = termService.getMastershipTerm(did);
539 if (myNodeId.equals(term.master())) {
540 myNextRole = MASTER;
541 } else {
542 myNextRole = STANDBY;
543 }
544 }
545
546 switch (myNextRole) {
547 case MASTER:
548 final Device device = getDevice(did);
549 if ((device != null) && !isAvailable(did)) {
550 //flag the device as online. Is there a better way to do this?
551 DefaultDeviceDescription deviceDescription
552 = new DefaultDeviceDescription(did.uri(),
553 device.type(),
554 device.manufacturer(),
555 device.hwVersion(),
556 device.swVersion(),
557 device.serialNumber(),
558 device.chassisId());
559 DeviceEvent devEvent =
560 store.createOrUpdateDevice(device.providerId(), did,
561 deviceDescription);
562 post(devEvent);
563 }
564 // TODO: should apply role only if there is mismatch
565 log.info("Applying role {} to {}", myNextRole, did);
566 if (!applyRoleAndProbe(did, MASTER)) {
567 // immediately failed to apply role
568 mastershipService.relinquishMastership(did);
569 // FIXME disconnect?
570 }
571 break;
572 case STANDBY:
573 log.info("Applying role {} to {}", myNextRole, did);
574 if (!applyRoleAndProbe(did, STANDBY)) {
575 // immediately failed to apply role
576 mastershipService.relinquishMastership(did);
577 // FIXME disconnect?
578 }
579 break;
580 case NONE:
581 default:
582 // should never reach here
583 log.error("You didn't see anything. I did not exist.");
584 break;
585 }
586 }
587
588 // Intercepts mastership events
589 private class InternalMastershipListener implements MastershipListener {
590
tomb41d1ac2014-09-24 01:51:24 -0700591 @Override
592 public void event(MastershipEvent event) {
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700593
594 if (event.type() != MastershipEvent.Type.MASTER_CHANGED) {
595 // Don't care if backup list changed.
596 return;
597 }
598
Ayaka Koshibe4c891272014-10-08 17:14:16 -0700599 final DeviceId did = event.subject();
Ayaka Koshibeea5b4ce2014-10-11 14:17:17 -0700600 final NodeId myNodeId = clusterService.getLocalNode().id();
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700601
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700602 // myRole suggested by MastershipService
603 MastershipRole myNextRole;
Yuta HIGUCHI67dce882014-10-21 21:13:26 -0700604 if (myNodeId.equals(event.roleInfo().master())) {
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700605 // confirm latest info
Ayaka Koshibeea5b4ce2014-10-11 14:17:17 -0700606 MastershipTerm term = termService.getMastershipTerm(did);
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700607 final boolean iHaveControl = myNodeId.equals(term.master());
608 if (iHaveControl) {
Yuta HIGUCHI38b4d9d2014-10-30 11:32:41 -0700609 deviceClockProviderService.setMastershipTerm(did, term);
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700610 myNextRole = MASTER;
Yuta HIGUCHI38b4d9d2014-10-30 11:32:41 -0700611 } else {
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700612 myNextRole = STANDBY;
613 }
614 } else if (event.roleInfo().backups().contains(myNodeId)) {
615 myNextRole = STANDBY;
616 } else {
617 myNextRole = NONE;
618 }
619
620
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700621 final boolean isReachable = isReachable(did);
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700622 if (!isReachable) {
623 // device is not connected to this node
624 if (myNextRole != NONE) {
625 log.warn("Node was instructed to be {} role for {}, "
626 + "but this node cannot reach the device. "
627 + "Relinquishing role. ",
628 myNextRole, did);
629 mastershipService.relinquishMastership(did);
630 }
631 return;
632 }
633
634 // device is connected to this node:
alshabibfd3cc052015-02-10 15:16:57 -0800635 if (store.getDevice(did) != null) {
636 reassertRole(did, myNextRole);
637 } else {
638 log.warn("Device is not yet/no longer in the store: {}", did);
639 }
Ayaka Koshibe317245a2014-10-29 00:34:43 -0700640 }
tomb41d1ac2014-09-24 01:51:24 -0700641 }
tomf80c9722014-09-24 14:49:18 -0700642
643 // Store delegate to re-post events emitted from the store.
alshabib271a6202014-09-28 22:11:21 -0700644 private class InternalStoreDelegate
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700645 implements DeviceStoreDelegate {
tomf80c9722014-09-24 14:49:18 -0700646 @Override
647 public void notify(DeviceEvent event) {
648 post(event);
649 }
650 }
tomd3097b02014-08-26 10:40:29 -0700651}