blob: d90162318775111aab7ff2180c4b60a2e204879c [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 Open Networking Laboratory
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
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;
Madan Jampanide003d92015-05-11 17:14:20 -070019
tomd3097b02014-08-26 10:40:29 -070020import org.apache.felix.scr.annotations.Activate;
21import org.apache.felix.scr.annotations.Component;
22import org.apache.felix.scr.annotations.Deactivate;
tom5f38b3a2014-08-27 23:50:54 -070023import org.apache.felix.scr.annotations.Reference;
24import org.apache.felix.scr.annotations.ReferenceCardinality;
tomd3097b02014-08-26 10:40:29 -070025import org.apache.felix.scr.annotations.Service;
Brian O'Connorabafb502014-12-02 22:26:20 -080026import org.onosproject.cluster.ClusterService;
27import org.onosproject.cluster.NodeId;
Simon Huntff663742015-05-14 13:33:05 -070028import org.onosproject.event.ListenerRegistry;
Brian O'Connorabafb502014-12-02 22:26:20 -080029import org.onosproject.event.EventDeliveryService;
30import org.onosproject.mastership.MastershipEvent;
31import org.onosproject.mastership.MastershipListener;
32import org.onosproject.mastership.MastershipService;
33import org.onosproject.mastership.MastershipTerm;
34import org.onosproject.mastership.MastershipTermService;
35import org.onosproject.net.Device;
36import org.onosproject.net.DeviceId;
37import org.onosproject.net.MastershipRole;
38import org.onosproject.net.Port;
39import org.onosproject.net.PortNumber;
40import org.onosproject.net.device.DefaultDeviceDescription;
41import org.onosproject.net.device.DefaultPortDescription;
42import org.onosproject.net.device.DeviceAdminService;
43import org.onosproject.net.device.DeviceClockProviderService;
44import org.onosproject.net.device.DeviceDescription;
45import org.onosproject.net.device.DeviceEvent;
46import org.onosproject.net.device.DeviceListener;
47import org.onosproject.net.device.DeviceProvider;
48import org.onosproject.net.device.DeviceProviderRegistry;
49import org.onosproject.net.device.DeviceProviderService;
50import org.onosproject.net.device.DeviceService;
51import org.onosproject.net.device.DeviceStore;
52import org.onosproject.net.device.DeviceStoreDelegate;
53import org.onosproject.net.device.PortDescription;
sangho538108b2015-04-08 14:29:20 -070054import org.onosproject.net.device.PortStatistics;
Brian O'Connorabafb502014-12-02 22:26:20 -080055import org.onosproject.net.provider.AbstractProviderRegistry;
56import org.onosproject.net.provider.AbstractProviderService;
tomd3097b02014-08-26 10:40:29 -070057import org.slf4j.Logger;
tomd3097b02014-08-26 10:40:29 -070058
sangho538108b2015-04-08 14:29:20 -070059import java.util.Collection;
Jonathan Hart2f669362015-02-11 16:19:20 -080060import java.util.List;
Thomas Vachuskab17c41f2015-05-19 11:16:05 -070061import java.util.Objects;
Madan Jampanide003d92015-05-11 17:14:20 -070062import java.util.concurrent.CompletableFuture;
Jonathan Hart2f669362015-02-11 16:19:20 -080063import java.util.concurrent.ScheduledExecutorService;
64import java.util.concurrent.TimeUnit;
65
66import static com.google.common.base.Preconditions.checkNotNull;
Thomas Vachuska6f94ded2015-02-21 14:02:38 -080067import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
68import static org.onlab.util.Tools.groupedThreads;
69import static org.onosproject.net.MastershipRole.*;
Jonathan Hart2f669362015-02-11 16:19:20 -080070import static org.slf4j.LoggerFactory.getLogger;
71
tomd3097b02014-08-26 10:40:29 -070072/**
tome4729872014-09-23 00:37:37 -070073 * Provides implementation of the device SB & NB APIs.
tomd3097b02014-08-26 10:40:29 -070074 */
75@Component(immediate = true)
76@Service
tom41a2c5f2014-09-19 09:20:35 -070077public class DeviceManager
Thomas Vachuskad16ce182014-10-29 17:25:29 -070078 extends AbstractProviderRegistry<DeviceProvider, DeviceProviderService>
79 implements DeviceService, DeviceAdminService, DeviceProviderRegistry {
tom32f66842014-08-27 19:27:47 -070080
tome5ec3fd2014-09-04 15:18:06 -070081 private static final String DEVICE_ID_NULL = "Device ID cannot be null";
82 private static final String PORT_NUMBER_NULL = "Port number cannot be null";
83 private static final String DEVICE_DESCRIPTION_NULL = "Device description cannot be null";
84 private static final String PORT_DESCRIPTION_NULL = "Port description cannot be null";
tomd3097b02014-08-26 10:40:29 -070085
tom5f38b3a2014-08-27 23:50:54 -070086 private final Logger log = getLogger(getClass());
tomd3097b02014-08-26 10:40:29 -070087
Simon Huntff663742015-05-14 13:33:05 -070088 protected final ListenerRegistry<DeviceEvent, DeviceListener> listenerRegistry =
89 new ListenerRegistry<>();
tom32f66842014-08-27 19:27:47 -070090
alshabib339a3d92014-09-26 17:54:32 -070091 private final DeviceStoreDelegate delegate = new InternalStoreDelegate();
tomf80c9722014-09-24 14:49:18 -070092
tomc78acee2014-09-24 15:16:55 -070093 private final MastershipListener mastershipListener = new InternalMastershipListener();
Madan Jampanide003d92015-05-11 17:14:20 -070094 private NodeId localNodeId;
tomb41d1ac2014-09-24 01:51:24 -070095
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -080096 private ScheduledExecutorService backgroundService;
97
tom41a2c5f2014-09-19 09:20:35 -070098 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
99 protected DeviceStore store;
tomd3097b02014-08-26 10:40:29 -0700100
tom5f38b3a2014-08-27 23:50:54 -0700101 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
tome5ec3fd2014-09-04 15:18:06 -0700102 protected EventDeliveryService eventDispatcher;
tom5f38b3a2014-08-27 23:50:54 -0700103
Ayaka Koshibea7f044e2014-09-23 16:56:20 -0700104 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
tomb41d1ac2014-09-24 01:51:24 -0700105 protected ClusterService clusterService;
106
107 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Ayaka Koshibea7f044e2014-09-23 16:56:20 -0700108 protected MastershipService mastershipService;
109
Yuta HIGUCHIbcac4992014-11-22 19:27:57 -0800110 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Ayaka Koshibe3de43ca2014-09-26 16:40:23 -0700111 protected MastershipTermService termService;
112
Madan Jampani61056bc2014-09-27 09:07:26 -0700113 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Yuta HIGUCHI093e83e2014-10-10 22:26:11 -0700114 protected DeviceClockProviderService deviceClockProviderService;
Madan Jampani61056bc2014-09-27 09:07:26 -0700115
tomd3097b02014-08-26 10:40:29 -0700116 @Activate
117 public void activate() {
Thomas Vachuska6f94ded2015-02-21 14:02:38 -0800118 backgroundService = newSingleThreadScheduledExecutor(groupedThreads("onos/device", "manager-background"));
Madan Jampanide003d92015-05-11 17:14:20 -0700119 localNodeId = clusterService.getLocalNode().id();
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800120
tomf80c9722014-09-24 14:49:18 -0700121 store.setDelegate(delegate);
tom96dfcab2014-08-28 09:26:03 -0700122 eventDispatcher.addSink(DeviceEvent.class, listenerRegistry);
tomb41d1ac2014-09-24 01:51:24 -0700123 mastershipService.addListener(mastershipListener);
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800124
125 backgroundService.scheduleWithFixedDelay(new Runnable() {
126
127 @Override
128 public void run() {
129 try {
130 mastershipCheck();
131 } catch (Exception e) {
132 log.error("Exception thrown during integrity check", e);
133 }
134 }
135 }, 1, 1, TimeUnit.MINUTES);
tomd3097b02014-08-26 10:40:29 -0700136 log.info("Started");
137 }
138
139 @Deactivate
140 public void deactivate() {
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800141 backgroundService.shutdown();
142
tomf80c9722014-09-24 14:49:18 -0700143 store.unsetDelegate(delegate);
tomb41d1ac2014-09-24 01:51:24 -0700144 mastershipService.removeListener(mastershipListener);
tom5f38b3a2014-08-27 23:50:54 -0700145 eventDispatcher.removeSink(DeviceEvent.class);
tomd3097b02014-08-26 10:40:29 -0700146 log.info("Stopped");
147 }
148
149 @Override
tomad2d2092014-09-06 23:24:20 -0700150 public int getDeviceCount() {
151 return store.getDeviceCount();
tomd3097b02014-08-26 10:40:29 -0700152 }
153
154 @Override
tom32f66842014-08-27 19:27:47 -0700155 public Iterable<Device> getDevices() {
tome5ec3fd2014-09-04 15:18:06 -0700156 return store.getDevices();
tomd3097b02014-08-26 10:40:29 -0700157 }
158
tom32f66842014-08-27 19:27:47 -0700159 @Override
Yuta HIGUCHIf1f2ac02014-11-26 14:02:22 -0800160 public Iterable<Device> getAvailableDevices() {
161 return store.getAvailableDevices();
162 }
163
164 @Override
tom32f66842014-08-27 19:27:47 -0700165 public Device getDevice(DeviceId deviceId) {
166 checkNotNull(deviceId, DEVICE_ID_NULL);
tom132b58a2014-08-28 16:11:28 -0700167 return store.getDevice(deviceId);
tom32f66842014-08-27 19:27:47 -0700168 }
169
170 @Override
tomad2d2092014-09-06 23:24:20 -0700171 public MastershipRole getRole(DeviceId deviceId) {
172 checkNotNull(deviceId, DEVICE_ID_NULL);
tomb41d1ac2014-09-24 01:51:24 -0700173 return mastershipService.getLocalRole(deviceId);
tomad2d2092014-09-06 23:24:20 -0700174 }
175
176 @Override
tom32f66842014-08-27 19:27:47 -0700177 public List<Port> getPorts(DeviceId deviceId) {
178 checkNotNull(deviceId, DEVICE_ID_NULL);
tom132b58a2014-08-28 16:11:28 -0700179 return store.getPorts(deviceId);
tom32f66842014-08-27 19:27:47 -0700180 }
181
182 @Override
sangho538108b2015-04-08 14:29:20 -0700183 public List<PortStatistics> getPortStatistics(DeviceId deviceId) {
184 checkNotNull(deviceId, DEVICE_ID_NULL);
185 return store.getPortStatistics(deviceId);
186 }
187
188 @Override
tom32f66842014-08-27 19:27:47 -0700189 public Port getPort(DeviceId deviceId, PortNumber portNumber) {
190 checkNotNull(deviceId, DEVICE_ID_NULL);
191 checkNotNull(portNumber, PORT_NUMBER_NULL);
tom132b58a2014-08-28 16:11:28 -0700192 return store.getPort(deviceId, portNumber);
tom32f66842014-08-27 19:27:47 -0700193 }
194
195 @Override
tomff7eb7c2014-09-08 12:49:03 -0700196 public boolean isAvailable(DeviceId deviceId) {
197 checkNotNull(deviceId, DEVICE_ID_NULL);
198 return store.isAvailable(deviceId);
199 }
200
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700201 // Check a device for control channel connectivity.
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700202 private boolean isReachable(DeviceId deviceId) {
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800203 if (deviceId == null) {
204 return false;
205 }
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700206 DeviceProvider provider = getProvider(deviceId);
207 if (provider != null) {
208 return provider.isReachable(deviceId);
209 } else {
Yuta HIGUCHI72669c42014-11-13 14:48:17 -0800210 log.debug("Provider not found for {}", deviceId);
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700211 return false;
212 }
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700213 }
214
tome5ec3fd2014-09-04 15:18:06 -0700215 @Override
216 public void removeDevice(DeviceId deviceId) {
217 checkNotNull(deviceId, DEVICE_ID_NULL);
218 DeviceEvent event = store.removeDevice(deviceId);
tom0efbb1d2014-09-09 11:54:28 -0700219 if (event != null) {
220 log.info("Device {} administratively removed", deviceId);
221 post(event);
222 }
tome5ec3fd2014-09-04 15:18:06 -0700223 }
224
tom7869ad92014-09-09 14:32:08 -0700225 @Override
226 public void addListener(DeviceListener listener) {
227 listenerRegistry.addListener(listener);
228 }
229
230 @Override
231 public void removeListener(DeviceListener listener) {
232 listenerRegistry.removeListener(listener);
233 }
234
235 @Override
alshabibb7b40632014-09-28 21:30:00 -0700236 protected DeviceProviderService createProviderService(
237 DeviceProvider provider) {
tom7869ad92014-09-09 14:32:08 -0700238 return new InternalDeviceProviderService(provider);
239 }
240
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800241 /**
242 * Checks if all the reachable devices have a valid mastership role.
243 */
244 private void mastershipCheck() {
245 log.debug("Checking mastership");
246 for (Device device : getDevices()) {
247 final DeviceId deviceId = device.id();
Jonathan Hart2f669362015-02-11 16:19:20 -0800248 log.trace("Checking device {}", deviceId);
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800249
250 if (!isReachable(deviceId)) {
251 continue;
252 }
253
254 if (mastershipService.getLocalRole(deviceId) != NONE) {
255 continue;
256 }
257
258 log.info("{} is reachable but did not have a valid role, reasserting", deviceId);
259
260 // isReachable but was not MASTER or STANDBY, get a role and apply
261 // Note: NONE triggers request to MastershipService
262 reassertRole(deviceId, NONE);
263 }
264 }
265
tomd3097b02014-08-26 10:40:29 -0700266 // Personalized device provider service issued to the supplied provider.
tomdc361b62014-09-09 20:36:52 -0700267 private class InternalDeviceProviderService
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700268 extends AbstractProviderService<DeviceProvider>
269 implements DeviceProviderService {
tomd3097b02014-08-26 10:40:29 -0700270
tomcfde0622014-09-09 11:02:42 -0700271 InternalDeviceProviderService(DeviceProvider provider) {
tomd3097b02014-08-26 10:40:29 -0700272 super(provider);
273 }
274
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700275 /**
276 * Apply role in reaction to provider event.
277 *
278 * @param deviceId device identifier
279 * @param newRole new role to apply to the device
280 * @return true if the request was sent to provider
281 */
282 private boolean applyRole(DeviceId deviceId, MastershipRole newRole) {
283
284 if (newRole.equals(MastershipRole.NONE)) {
285 //no-op
286 return true;
287 }
288
289 DeviceProvider provider = provider();
290 if (provider == null) {
291 log.warn("Provider for {} was not found. Cannot apply role {}", deviceId, newRole);
292 return false;
293 }
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700294 provider.roleChanged(deviceId, newRole);
295 // not triggering probe when triggered by provider service event
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700296
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700297 return true;
298 }
299
300
tomd3097b02014-08-26 10:40:29 -0700301 @Override
alshabibb7b40632014-09-28 21:30:00 -0700302 public void deviceConnected(DeviceId deviceId,
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700303 DeviceDescription deviceDescription) {
tom32f66842014-08-27 19:27:47 -0700304 checkNotNull(deviceId, DEVICE_ID_NULL);
305 checkNotNull(deviceDescription, DEVICE_DESCRIPTION_NULL);
tomeadbb462014-09-07 16:10:19 -0700306 checkValidity();
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700307
308 log.info("Device {} connected", deviceId);
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700309 // check my Role
310 mastershipService.requestRoleFor(deviceId);
311 final MastershipTerm term = termService.getMastershipTerm(deviceId);
Madan Jampanide003d92015-05-11 17:14:20 -0700312 if (term == null || !localNodeId.equals(term.master())) {
Yuta HIGUCHI2d3cd312014-10-31 11:38:04 -0700313 log.info("Role of this node is STANDBY for {}", deviceId);
314 // TODO: Do we need to explicitly tell the Provider that
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700315 // this instance is not the MASTER
316 applyRole(deviceId, MastershipRole.STANDBY);
Marc De Leenheerb473b9d2015-02-06 15:21:03 -0800317 } else {
318 log.info("Role of this node is MASTER for {}", deviceId);
319 // tell clock provider if this instance is the master
320 deviceClockProviderService.setMastershipTerm(deviceId, term);
321 applyRole(deviceId, MastershipRole.MASTER);
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700322 }
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700323
tome5ec3fd2014-09-04 15:18:06 -0700324 DeviceEvent event = store.createOrUpdateDevice(provider().id(),
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700325 deviceId, deviceDescription);
tom80c0e5e2014-09-08 18:08:58 -0700326
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700327 // If there was a change of any kind, tell the provider
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700328 // that this instance is the master.
tom80c0e5e2014-09-08 18:08:58 -0700329 if (event != null) {
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700330 log.trace("event: {} {}", event.type(), event);
tom568581d2014-09-08 20:13:36 -0700331 post(event);
tom80c0e5e2014-09-08 18:08:58 -0700332 }
tomd3097b02014-08-26 10:40:29 -0700333 }
334
335 @Override
336 public void deviceDisconnected(DeviceId deviceId) {
tom32f66842014-08-27 19:27:47 -0700337 checkNotNull(deviceId, DEVICE_ID_NULL);
tomeadbb462014-09-07 16:10:19 -0700338 checkValidity();
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700339
Yuta HIGUCHI2d3cd312014-10-31 11:38:04 -0700340 log.info("Device {} disconnected from this node", deviceId);
Ayaka Koshibed9f693e2014-09-29 18:04:54 -0700341
alshabibafc514a2014-12-01 14:44:05 -0800342 List<Port> ports = store.getPorts(deviceId);
343 List<PortDescription> descs = Lists.newArrayList();
344 ports.forEach(port ->
345 descs.add(new DefaultPortDescription(port.number(),
346 false, port.type(),
347 port.portSpeed())));
348 store.updatePorts(this.provider().id(), deviceId, descs);
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700349 try {
Madan Jampanide003d92015-05-11 17:14:20 -0700350 post(store.markOffline(deviceId));
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700351 } catch (IllegalStateException e) {
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700352 log.warn("Failed to mark {} offline", deviceId);
353 // only the MASTER should be marking off-line in normal cases,
354 // but if I was the last STANDBY connection, etc. and no one else
355 // was there to mark the device offline, this instance may need to
356 // temporarily request for Master Role and mark offline.
357
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700358 //there are times when this node will correctly have mastership, BUT
359 //that isn't reflected in the ClockManager before the device disconnects.
360 //we want to let go of the device anyways, so make sure this happens.
Yuta HIGUCHI0722fb22014-10-19 01:16:33 -0700361
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700362 // FIXME: Store semantics leaking out as IllegalStateException.
363 // Consider revising store API to handle this scenario.
Madan Jampanide003d92015-05-11 17:14:20 -0700364 CompletableFuture<MastershipRole> roleFuture = mastershipService.requestRoleFor(deviceId);
365 roleFuture.whenComplete((role, error) -> {
366 MastershipTerm term = termService.getMastershipTerm(deviceId);
367 // TODO: Move this type of check inside device clock manager, etc.
368 if (term != null && localNodeId.equals(term.master())) {
369 log.info("Retry marking {} offline", deviceId);
370 deviceClockProviderService.setMastershipTerm(deviceId, term);
371 post(store.markOffline(deviceId));
372 } else {
373 log.info("Failed again marking {} offline. {}", deviceId, role);
374 }
375 });
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700376 } finally {
377 //relinquish master role and ability to be backup.
378 mastershipService.relinquishMastership(deviceId);
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
Thomas Vachuskab17c41f2015-05-19 11:16:05 -0700427 public void receivedRoleReply(DeviceId deviceId, MastershipRole requested,
428 MastershipRole response) {
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700429 // 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 {}",
Thomas Vachuskab17c41f2015-05-19 11:16:05 -0700440 deviceId, requested, response);
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700441
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
Thomas Vachuskab17c41f2015-05-19 11:16:05 -0700449 if (Objects.equals(requested, response)) {
450 if (Objects.equals(requested, mastershipService.getLocalRole(deviceId))) {
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700451 return;
452 } else {
453 return;
454 // FIXME roleManager got the device to comply, but doesn't agree with
455 // the store; use the store's view, then try to reassert.
456 }
457 } else {
458 // we didn't get back what we asked for. Reelect someone else.
459 log.warn("Failed to assert role [{}] onto Device {}", response, deviceId);
460 if (response == MastershipRole.MASTER) {
461 mastershipService.relinquishMastership(deviceId);
462 // TODO: Shouldn't we be triggering event?
463 //final Device device = getDevice(deviceId);
464 //post(new DeviceEvent(DEVICE_MASTERSHIP_CHANGED, device));
465 }
466 }
tom3f2bbd72014-09-24 12:07:58 -0700467 }
sangho538108b2015-04-08 14:29:20 -0700468
469 @Override
470 public void updatePortStatistics(DeviceId deviceId, Collection<PortStatistics> portStatistics) {
471 checkNotNull(deviceId, DEVICE_ID_NULL);
Thomas Vachuskab17c41f2015-05-19 11:16:05 -0700472 checkNotNull(portStatistics, "Port statistics list cannot be null");
sangho538108b2015-04-08 14:29:20 -0700473 checkValidity();
474
475 DeviceEvent event = store.updatePortStatistics(this.provider().id(),
Thomas Vachuskab17c41f2015-05-19 11:16:05 -0700476 deviceId, portStatistics);
sangho538108b2015-04-08 14:29:20 -0700477 post(event);
478 }
tomd3097b02014-08-26 10:40:29 -0700479 }
tom32f66842014-08-27 19:27:47 -0700480
tomeadbb462014-09-07 16:10:19 -0700481 // Posts the specified event to the local event dispatcher.
tom32f66842014-08-27 19:27:47 -0700482 private void post(DeviceEvent event) {
483 if (event != null && eventDispatcher != null) {
484 eventDispatcher.post(event);
485 }
486 }
487
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800488 // Applies the specified role to the device; ignores NONE
489 /**
490 * Apply role to device and send probe if MASTER.
491 *
492 * @param deviceId device identifier
493 * @param newRole new role to apply to the device
494 * @return true if the request was sent to provider
495 */
496 private boolean applyRoleAndProbe(DeviceId deviceId, MastershipRole newRole) {
497 if (newRole.equals(MastershipRole.NONE)) {
498 //no-op
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700499 return true;
500 }
Ayaka Koshibe317245a2014-10-29 00:34:43 -0700501
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800502 DeviceProvider provider = getProvider(deviceId);
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800503 if (provider == null) {
504 log.warn("Provider for {} was not found. Cannot apply role {}", deviceId, newRole);
505 return false;
506 }
507 provider.roleChanged(deviceId, newRole);
508
509 if (newRole.equals(MastershipRole.MASTER)) {
510 // only trigger event when request was sent to provider
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800511 provider.triggerProbe(deviceId);
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800512 }
513 return true;
514 }
515
516 /**
517 * Reaasert role for specified device connected to this node.
518 *
519 * @param did device identifier
520 * @param nextRole role to apply. If NONE is specified,
521 * it will ask mastership service for a role and apply it.
522 */
523 private void reassertRole(final DeviceId did,
524 final MastershipRole nextRole) {
525
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800526 MastershipRole myNextRole = nextRole;
527 if (myNextRole == NONE) {
528 mastershipService.requestRoleFor(did);
529 MastershipTerm term = termService.getMastershipTerm(did);
Madan Jampanide003d92015-05-11 17:14:20 -0700530 if (term != null && localNodeId.equals(term.master())) {
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800531 myNextRole = MASTER;
532 } else {
533 myNextRole = STANDBY;
534 }
535 }
536
537 switch (myNextRole) {
538 case MASTER:
539 final Device device = getDevice(did);
540 if ((device != null) && !isAvailable(did)) {
541 //flag the device as online. Is there a better way to do this?
542 DefaultDeviceDescription deviceDescription
543 = new DefaultDeviceDescription(did.uri(),
544 device.type(),
545 device.manufacturer(),
546 device.hwVersion(),
547 device.swVersion(),
548 device.serialNumber(),
549 device.chassisId());
550 DeviceEvent devEvent =
551 store.createOrUpdateDevice(device.providerId(), did,
552 deviceDescription);
553 post(devEvent);
554 }
555 // TODO: should apply role only if there is mismatch
556 log.info("Applying role {} to {}", myNextRole, did);
557 if (!applyRoleAndProbe(did, MASTER)) {
558 // immediately failed to apply role
559 mastershipService.relinquishMastership(did);
560 // FIXME disconnect?
561 }
562 break;
563 case STANDBY:
564 log.info("Applying role {} to {}", myNextRole, did);
565 if (!applyRoleAndProbe(did, STANDBY)) {
566 // immediately failed to apply role
567 mastershipService.relinquishMastership(did);
568 // FIXME disconnect?
569 }
570 break;
571 case NONE:
572 default:
573 // should never reach here
574 log.error("You didn't see anything. I did not exist.");
575 break;
576 }
577 }
578
579 // Intercepts mastership events
580 private class InternalMastershipListener implements MastershipListener {
581
tomb41d1ac2014-09-24 01:51:24 -0700582 @Override
583 public void event(MastershipEvent event) {
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700584 if (event.type() != MastershipEvent.Type.MASTER_CHANGED) {
585 // Don't care if backup list changed.
586 return;
587 }
588
Ayaka Koshibe4c891272014-10-08 17:14:16 -0700589 final DeviceId did = event.subject();
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700590
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700591 // myRole suggested by MastershipService
592 MastershipRole myNextRole;
Madan Jampanide003d92015-05-11 17:14:20 -0700593 if (localNodeId.equals(event.roleInfo().master())) {
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700594 // confirm latest info
Ayaka Koshibeea5b4ce2014-10-11 14:17:17 -0700595 MastershipTerm term = termService.getMastershipTerm(did);
Madan Jampanide003d92015-05-11 17:14:20 -0700596 final boolean iHaveControl = term != null && localNodeId.equals(term.master());
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700597 if (iHaveControl) {
Yuta HIGUCHI38b4d9d2014-10-30 11:32:41 -0700598 deviceClockProviderService.setMastershipTerm(did, term);
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700599 myNextRole = MASTER;
Yuta HIGUCHI38b4d9d2014-10-30 11:32:41 -0700600 } else {
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700601 myNextRole = STANDBY;
602 }
Madan Jampanide003d92015-05-11 17:14:20 -0700603 } else if (event.roleInfo().backups().contains(localNodeId)) {
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700604 myNextRole = STANDBY;
605 } else {
606 myNextRole = NONE;
607 }
608
609
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700610 final boolean isReachable = isReachable(did);
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700611 if (!isReachable) {
612 // device is not connected to this node
613 if (myNextRole != NONE) {
614 log.warn("Node was instructed to be {} role for {}, "
615 + "but this node cannot reach the device. "
616 + "Relinquishing role. ",
617 myNextRole, did);
618 mastershipService.relinquishMastership(did);
619 }
620 return;
621 }
622
623 // device is connected to this node:
alshabibfd3cc052015-02-10 15:16:57 -0800624 if (store.getDevice(did) != null) {
625 reassertRole(did, myNextRole);
626 } else {
Thomas Vachuska3358af22015-05-19 18:40:34 -0700627 log.debug("Device is not yet/no longer in the store: {}", did);
alshabibfd3cc052015-02-10 15:16:57 -0800628 }
Ayaka Koshibe317245a2014-10-29 00:34:43 -0700629 }
tomb41d1ac2014-09-24 01:51:24 -0700630 }
tomf80c9722014-09-24 14:49:18 -0700631
632 // Store delegate to re-post events emitted from the store.
Thomas Vachuskab17c41f2015-05-19 11:16:05 -0700633 private class InternalStoreDelegate implements DeviceStoreDelegate {
tomf80c9722014-09-24 14:49:18 -0700634 @Override
635 public void notify(DeviceEvent event) {
636 post(event);
637 }
638 }
tomd3097b02014-08-26 10:40:29 -0700639}