blob: d12664b16652f1691080b727e1d4045c4ffe9f0c [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;
Madan Jampanide003d92015-05-11 17:14:20 -070061import java.util.concurrent.CompletableFuture;
Jonathan Hart2f669362015-02-11 16:19:20 -080062import java.util.concurrent.ScheduledExecutorService;
63import java.util.concurrent.TimeUnit;
64
65import static com.google.common.base.Preconditions.checkNotNull;
Thomas Vachuska6f94ded2015-02-21 14:02:38 -080066import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
67import static org.onlab.util.Tools.groupedThreads;
68import static org.onosproject.net.MastershipRole.*;
Jonathan Hart2f669362015-02-11 16:19:20 -080069import static org.slf4j.LoggerFactory.getLogger;
70
tomd3097b02014-08-26 10:40:29 -070071/**
tome4729872014-09-23 00:37:37 -070072 * Provides implementation of the device SB & NB APIs.
tomd3097b02014-08-26 10:40:29 -070073 */
74@Component(immediate = true)
75@Service
tom41a2c5f2014-09-19 09:20:35 -070076public class DeviceManager
Thomas Vachuskad16ce182014-10-29 17:25:29 -070077 extends AbstractProviderRegistry<DeviceProvider, DeviceProviderService>
78 implements DeviceService, DeviceAdminService, DeviceProviderRegistry {
tom32f66842014-08-27 19:27:47 -070079
tome5ec3fd2014-09-04 15:18:06 -070080 private static final String DEVICE_ID_NULL = "Device ID cannot be null";
81 private static final String PORT_NUMBER_NULL = "Port number cannot be null";
82 private static final String DEVICE_DESCRIPTION_NULL = "Device description cannot be null";
83 private static final String PORT_DESCRIPTION_NULL = "Port description cannot be null";
tomd3097b02014-08-26 10:40:29 -070084
tom5f38b3a2014-08-27 23:50:54 -070085 private final Logger log = getLogger(getClass());
tomd3097b02014-08-26 10:40:29 -070086
Simon Huntff663742015-05-14 13:33:05 -070087 protected final ListenerRegistry<DeviceEvent, DeviceListener> listenerRegistry =
88 new ListenerRegistry<>();
tom32f66842014-08-27 19:27:47 -070089
alshabib339a3d92014-09-26 17:54:32 -070090 private final DeviceStoreDelegate delegate = new InternalStoreDelegate();
tomf80c9722014-09-24 14:49:18 -070091
tomc78acee2014-09-24 15:16:55 -070092 private final MastershipListener mastershipListener = new InternalMastershipListener();
Madan Jampanide003d92015-05-11 17:14:20 -070093 private NodeId localNodeId;
tomb41d1ac2014-09-24 01:51:24 -070094
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -080095 private ScheduledExecutorService backgroundService;
96
tom41a2c5f2014-09-19 09:20:35 -070097 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
98 protected DeviceStore store;
tomd3097b02014-08-26 10:40:29 -070099
tom5f38b3a2014-08-27 23:50:54 -0700100 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
tome5ec3fd2014-09-04 15:18:06 -0700101 protected EventDeliveryService eventDispatcher;
tom5f38b3a2014-08-27 23:50:54 -0700102
Ayaka Koshibea7f044e2014-09-23 16:56:20 -0700103 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
tomb41d1ac2014-09-24 01:51:24 -0700104 protected ClusterService clusterService;
105
106 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Ayaka Koshibea7f044e2014-09-23 16:56:20 -0700107 protected MastershipService mastershipService;
108
Yuta HIGUCHIbcac4992014-11-22 19:27:57 -0800109 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Ayaka Koshibe3de43ca2014-09-26 16:40:23 -0700110 protected MastershipTermService termService;
111
Madan Jampani61056bc2014-09-27 09:07:26 -0700112 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Yuta HIGUCHI093e83e2014-10-10 22:26:11 -0700113 protected DeviceClockProviderService deviceClockProviderService;
Madan Jampani61056bc2014-09-27 09:07:26 -0700114
tomd3097b02014-08-26 10:40:29 -0700115 @Activate
116 public void activate() {
Thomas Vachuska6f94ded2015-02-21 14:02:38 -0800117 backgroundService = newSingleThreadScheduledExecutor(groupedThreads("onos/device", "manager-background"));
Madan Jampanide003d92015-05-11 17:14:20 -0700118 localNodeId = clusterService.getLocalNode().id();
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800119
tomf80c9722014-09-24 14:49:18 -0700120 store.setDelegate(delegate);
tom96dfcab2014-08-28 09:26:03 -0700121 eventDispatcher.addSink(DeviceEvent.class, listenerRegistry);
tomb41d1ac2014-09-24 01:51:24 -0700122 mastershipService.addListener(mastershipListener);
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800123
124 backgroundService.scheduleWithFixedDelay(new Runnable() {
125
126 @Override
127 public void run() {
128 try {
129 mastershipCheck();
130 } catch (Exception e) {
131 log.error("Exception thrown during integrity check", e);
132 }
133 }
134 }, 1, 1, TimeUnit.MINUTES);
tomd3097b02014-08-26 10:40:29 -0700135 log.info("Started");
136 }
137
138 @Deactivate
139 public void deactivate() {
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800140 backgroundService.shutdown();
141
tomf80c9722014-09-24 14:49:18 -0700142 store.unsetDelegate(delegate);
tomb41d1ac2014-09-24 01:51:24 -0700143 mastershipService.removeListener(mastershipListener);
tom5f38b3a2014-08-27 23:50:54 -0700144 eventDispatcher.removeSink(DeviceEvent.class);
tomd3097b02014-08-26 10:40:29 -0700145 log.info("Stopped");
146 }
147
148 @Override
tomad2d2092014-09-06 23:24:20 -0700149 public int getDeviceCount() {
150 return store.getDeviceCount();
tomd3097b02014-08-26 10:40:29 -0700151 }
152
153 @Override
tom32f66842014-08-27 19:27:47 -0700154 public Iterable<Device> getDevices() {
tome5ec3fd2014-09-04 15:18:06 -0700155 return store.getDevices();
tomd3097b02014-08-26 10:40:29 -0700156 }
157
tom32f66842014-08-27 19:27:47 -0700158 @Override
Yuta HIGUCHIf1f2ac02014-11-26 14:02:22 -0800159 public Iterable<Device> getAvailableDevices() {
160 return store.getAvailableDevices();
161 }
162
163 @Override
tom32f66842014-08-27 19:27:47 -0700164 public Device getDevice(DeviceId deviceId) {
165 checkNotNull(deviceId, DEVICE_ID_NULL);
tom132b58a2014-08-28 16:11:28 -0700166 return store.getDevice(deviceId);
tom32f66842014-08-27 19:27:47 -0700167 }
168
169 @Override
tomad2d2092014-09-06 23:24:20 -0700170 public MastershipRole getRole(DeviceId deviceId) {
171 checkNotNull(deviceId, DEVICE_ID_NULL);
tomb41d1ac2014-09-24 01:51:24 -0700172 return mastershipService.getLocalRole(deviceId);
tomad2d2092014-09-06 23:24:20 -0700173 }
174
175 @Override
tom32f66842014-08-27 19:27:47 -0700176 public List<Port> getPorts(DeviceId deviceId) {
177 checkNotNull(deviceId, DEVICE_ID_NULL);
tom132b58a2014-08-28 16:11:28 -0700178 return store.getPorts(deviceId);
tom32f66842014-08-27 19:27:47 -0700179 }
180
181 @Override
sangho538108b2015-04-08 14:29:20 -0700182 public List<PortStatistics> getPortStatistics(DeviceId deviceId) {
183 checkNotNull(deviceId, DEVICE_ID_NULL);
184 return store.getPortStatistics(deviceId);
185 }
186
187 @Override
tom32f66842014-08-27 19:27:47 -0700188 public Port getPort(DeviceId deviceId, PortNumber portNumber) {
189 checkNotNull(deviceId, DEVICE_ID_NULL);
190 checkNotNull(portNumber, PORT_NUMBER_NULL);
tom132b58a2014-08-28 16:11:28 -0700191 return store.getPort(deviceId, portNumber);
tom32f66842014-08-27 19:27:47 -0700192 }
193
194 @Override
tomff7eb7c2014-09-08 12:49:03 -0700195 public boolean isAvailable(DeviceId deviceId) {
196 checkNotNull(deviceId, DEVICE_ID_NULL);
197 return store.isAvailable(deviceId);
198 }
199
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700200 // Check a device for control channel connectivity.
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700201 private boolean isReachable(DeviceId deviceId) {
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800202 if (deviceId == null) {
203 return false;
204 }
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700205 DeviceProvider provider = getProvider(deviceId);
206 if (provider != null) {
207 return provider.isReachable(deviceId);
208 } else {
Yuta HIGUCHI72669c42014-11-13 14:48:17 -0800209 log.debug("Provider not found for {}", deviceId);
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700210 return false;
211 }
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700212 }
213
tome5ec3fd2014-09-04 15:18:06 -0700214 @Override
215 public void removeDevice(DeviceId deviceId) {
216 checkNotNull(deviceId, DEVICE_ID_NULL);
217 DeviceEvent event = store.removeDevice(deviceId);
tom0efbb1d2014-09-09 11:54:28 -0700218 if (event != null) {
219 log.info("Device {} administratively removed", deviceId);
220 post(event);
221 }
tome5ec3fd2014-09-04 15:18:06 -0700222 }
223
tom7869ad92014-09-09 14:32:08 -0700224 @Override
225 public void addListener(DeviceListener listener) {
226 listenerRegistry.addListener(listener);
227 }
228
229 @Override
230 public void removeListener(DeviceListener listener) {
231 listenerRegistry.removeListener(listener);
232 }
233
234 @Override
alshabibb7b40632014-09-28 21:30:00 -0700235 protected DeviceProviderService createProviderService(
236 DeviceProvider provider) {
tom7869ad92014-09-09 14:32:08 -0700237 return new InternalDeviceProviderService(provider);
238 }
239
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800240 /**
241 * Checks if all the reachable devices have a valid mastership role.
242 */
243 private void mastershipCheck() {
244 log.debug("Checking mastership");
245 for (Device device : getDevices()) {
246 final DeviceId deviceId = device.id();
Jonathan Hart2f669362015-02-11 16:19:20 -0800247 log.trace("Checking device {}", deviceId);
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800248
249 if (!isReachable(deviceId)) {
250 continue;
251 }
252
253 if (mastershipService.getLocalRole(deviceId) != NONE) {
254 continue;
255 }
256
257 log.info("{} is reachable but did not have a valid role, reasserting", deviceId);
258
259 // isReachable but was not MASTER or STANDBY, get a role and apply
260 // Note: NONE triggers request to MastershipService
261 reassertRole(deviceId, NONE);
262 }
263 }
264
tomd3097b02014-08-26 10:40:29 -0700265 // Personalized device provider service issued to the supplied provider.
tomdc361b62014-09-09 20:36:52 -0700266 private class InternalDeviceProviderService
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700267 extends AbstractProviderService<DeviceProvider>
268 implements DeviceProviderService {
tomd3097b02014-08-26 10:40:29 -0700269
tomcfde0622014-09-09 11:02:42 -0700270 InternalDeviceProviderService(DeviceProvider provider) {
tomd3097b02014-08-26 10:40:29 -0700271 super(provider);
272 }
273
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700274 /**
275 * Apply role in reaction to provider event.
276 *
277 * @param deviceId device identifier
278 * @param newRole new role to apply to the device
279 * @return true if the request was sent to provider
280 */
281 private boolean applyRole(DeviceId deviceId, MastershipRole newRole) {
282
283 if (newRole.equals(MastershipRole.NONE)) {
284 //no-op
285 return true;
286 }
287
288 DeviceProvider provider = provider();
289 if (provider == null) {
290 log.warn("Provider for {} was not found. Cannot apply role {}", deviceId, newRole);
291 return false;
292 }
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700293 provider.roleChanged(deviceId, newRole);
294 // not triggering probe when triggered by provider service event
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700295
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700296 return true;
297 }
298
299
tomd3097b02014-08-26 10:40:29 -0700300 @Override
alshabibb7b40632014-09-28 21:30:00 -0700301 public void deviceConnected(DeviceId deviceId,
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700302 DeviceDescription deviceDescription) {
tom32f66842014-08-27 19:27:47 -0700303 checkNotNull(deviceId, DEVICE_ID_NULL);
304 checkNotNull(deviceDescription, DEVICE_DESCRIPTION_NULL);
tomeadbb462014-09-07 16:10:19 -0700305 checkValidity();
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700306
307 log.info("Device {} connected", deviceId);
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700308 // check my Role
309 mastershipService.requestRoleFor(deviceId);
310 final MastershipTerm term = termService.getMastershipTerm(deviceId);
Madan Jampanide003d92015-05-11 17:14:20 -0700311 if (term == null || !localNodeId.equals(term.master())) {
Yuta HIGUCHI2d3cd312014-10-31 11:38:04 -0700312 log.info("Role of this node is STANDBY for {}", deviceId);
313 // TODO: Do we need to explicitly tell the Provider that
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700314 // this instance is not the MASTER
315 applyRole(deviceId, MastershipRole.STANDBY);
Marc De Leenheerb473b9d2015-02-06 15:21:03 -0800316 } else {
317 log.info("Role of this node is MASTER for {}", deviceId);
318 // tell clock provider if this instance is the master
319 deviceClockProviderService.setMastershipTerm(deviceId, term);
320 applyRole(deviceId, MastershipRole.MASTER);
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700321 }
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700322
tome5ec3fd2014-09-04 15:18:06 -0700323 DeviceEvent event = store.createOrUpdateDevice(provider().id(),
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700324 deviceId, deviceDescription);
tom80c0e5e2014-09-08 18:08:58 -0700325
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700326 // If there was a change of any kind, tell the provider
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700327 // that this instance is the master.
tom80c0e5e2014-09-08 18:08:58 -0700328 if (event != null) {
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700329 log.trace("event: {} {}", event.type(), event);
tom568581d2014-09-08 20:13:36 -0700330 post(event);
tom80c0e5e2014-09-08 18:08:58 -0700331 }
tomd3097b02014-08-26 10:40:29 -0700332 }
333
334 @Override
335 public void deviceDisconnected(DeviceId deviceId) {
tom32f66842014-08-27 19:27:47 -0700336 checkNotNull(deviceId, DEVICE_ID_NULL);
tomeadbb462014-09-07 16:10:19 -0700337 checkValidity();
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700338
Yuta HIGUCHI2d3cd312014-10-31 11:38:04 -0700339 log.info("Device {} disconnected from this node", deviceId);
Ayaka Koshibed9f693e2014-09-29 18:04:54 -0700340
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 {
Madan Jampanide003d92015-05-11 17:14:20 -0700349 post(store.markOffline(deviceId));
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700350 } 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.
Madan Jampanide003d92015-05-11 17:14:20 -0700363 CompletableFuture<MastershipRole> roleFuture = mastershipService.requestRoleFor(deviceId);
364 roleFuture.whenComplete((role, error) -> {
365 MastershipTerm term = termService.getMastershipTerm(deviceId);
366 // TODO: Move this type of check inside device clock manager, etc.
367 if (term != null && localNodeId.equals(term.master())) {
368 log.info("Retry marking {} offline", deviceId);
369 deviceClockProviderService.setMastershipTerm(deviceId, term);
370 post(store.markOffline(deviceId));
371 } else {
372 log.info("Failed again marking {} offline. {}", deviceId, role);
373 }
374 });
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700375 } finally {
376 //relinquish master role and ability to be backup.
377 mastershipService.relinquishMastership(deviceId);
tom0efbb1d2014-09-09 11:54:28 -0700378 }
tomd3097b02014-08-26 10:40:29 -0700379 }
380
381 @Override
alshabibb7b40632014-09-28 21:30:00 -0700382 public void updatePorts(DeviceId deviceId,
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700383 List<PortDescription> portDescriptions) {
tom32f66842014-08-27 19:27:47 -0700384 checkNotNull(deviceId, DEVICE_ID_NULL);
alshabibb7b40632014-09-28 21:30:00 -0700385 checkNotNull(portDescriptions,
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700386 "Port descriptions list cannot be null");
tomeadbb462014-09-07 16:10:19 -0700387 checkValidity();
Yuta HIGUCHI13c0b872014-10-30 18:09:22 -0700388 if (!deviceClockProviderService.isTimestampAvailable(deviceId)) {
389 // Never been a master for this device
390 // any update will be ignored.
391 log.trace("Ignoring {} port updates on standby node. {}", deviceId, portDescriptions);
392 return;
393 }
394
Yuta HIGUCHI5f6739c2014-10-01 14:04:01 -0700395 List<DeviceEvent> events = store.updatePorts(this.provider().id(),
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700396 deviceId, portDescriptions);
tom32f66842014-08-27 19:27:47 -0700397 for (DeviceEvent event : events) {
398 post(event);
399 }
tomd3097b02014-08-26 10:40:29 -0700400 }
401
402 @Override
alshabibb7b40632014-09-28 21:30:00 -0700403 public void portStatusChanged(DeviceId deviceId,
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700404 PortDescription portDescription) {
tom32f66842014-08-27 19:27:47 -0700405 checkNotNull(deviceId, DEVICE_ID_NULL);
406 checkNotNull(portDescription, PORT_DESCRIPTION_NULL);
tomeadbb462014-09-07 16:10:19 -0700407 checkValidity();
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700408
Yuta HIGUCHI13c0b872014-10-30 18:09:22 -0700409 if (!deviceClockProviderService.isTimestampAvailable(deviceId)) {
410 // Never been a master for this device
411 // any update will be ignored.
412 log.trace("Ignoring {} port update on standby node. {}", deviceId, portDescription);
413 return;
414 }
415
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700416 final DeviceEvent event = store.updatePortStatus(this.provider().id(),
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700417 deviceId, portDescription);
tomff7eb7c2014-09-08 12:49:03 -0700418 if (event != null) {
alshabibb7b40632014-09-28 21:30:00 -0700419 log.info("Device {} port {} status changed", deviceId, event
420 .port().number());
tom0efbb1d2014-09-09 11:54:28 -0700421 post(event);
tomff7eb7c2014-09-08 12:49:03 -0700422 }
tomd3097b02014-08-26 10:40:29 -0700423 }
tom3f2bbd72014-09-24 12:07:58 -0700424
425 @Override
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700426 public void receivedRoleReply(
427 DeviceId deviceId, MastershipRole requested, MastershipRole response) {
428 // Several things can happen here:
429 // 1. request and response match
430 // 2. request and response don't match
431 // 3. MastershipRole and requested match (and 1 or 2 are true)
432 // 4. MastershipRole and requested don't match (and 1 or 2 are true)
433 //
434 // 2, 4, and 3 with case 2 are failure modes.
435
tom3f2bbd72014-09-24 12:07:58 -0700436 // FIXME: implement response to this notification
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700437
438 log.info("got reply to a role request for {}: asked for {}, and got {}",
439 deviceId, requested, response);
440
441 if (requested == null && response == null) {
442 // something was off with DeviceProvider, maybe check channel too?
443 log.warn("Failed to assert role [{}] onto Device {}", requested, deviceId);
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700444 mastershipService.relinquishMastership(deviceId);
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700445 return;
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700446 }
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700447
448 if (requested.equals(response)) {
449 if (requested.equals(mastershipService.getLocalRole(deviceId))) {
450
451 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 }
467
tom3f2bbd72014-09-24 12:07:58 -0700468 }
sangho538108b2015-04-08 14:29:20 -0700469
470 @Override
471 public void updatePortStatistics(DeviceId deviceId, Collection<PortStatistics> portStatistics) {
472 checkNotNull(deviceId, DEVICE_ID_NULL);
473 checkNotNull(portStatistics,
474 "Port statistics list cannot be null");
475 checkValidity();
476
477 DeviceEvent event = store.updatePortStatistics(this.provider().id(),
478 deviceId, portStatistics);
479
480 post(event);
481 }
tomd3097b02014-08-26 10:40:29 -0700482 }
tom32f66842014-08-27 19:27:47 -0700483
tomeadbb462014-09-07 16:10:19 -0700484 // Posts the specified event to the local event dispatcher.
tom32f66842014-08-27 19:27:47 -0700485 private void post(DeviceEvent event) {
486 if (event != null && eventDispatcher != null) {
487 eventDispatcher.post(event);
488 }
489 }
490
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800491 // Applies the specified role to the device; ignores NONE
492 /**
493 * Apply role to device and send probe if MASTER.
494 *
495 * @param deviceId device identifier
496 * @param newRole new role to apply to the device
497 * @return true if the request was sent to provider
498 */
499 private boolean applyRoleAndProbe(DeviceId deviceId, MastershipRole newRole) {
500 if (newRole.equals(MastershipRole.NONE)) {
501 //no-op
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700502 return true;
503 }
Ayaka Koshibe317245a2014-10-29 00:34:43 -0700504
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800505 DeviceProvider provider = getProvider(deviceId);
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800506 if (provider == null) {
507 log.warn("Provider for {} was not found. Cannot apply role {}", deviceId, newRole);
508 return false;
509 }
510 provider.roleChanged(deviceId, newRole);
511
512 if (newRole.equals(MastershipRole.MASTER)) {
513 // only trigger event when request was sent to provider
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800514 provider.triggerProbe(deviceId);
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800515 }
516 return true;
517 }
518
519 /**
520 * Reaasert role for specified device connected to this node.
521 *
522 * @param did device identifier
523 * @param nextRole role to apply. If NONE is specified,
524 * it will ask mastership service for a role and apply it.
525 */
526 private void reassertRole(final DeviceId did,
527 final MastershipRole nextRole) {
528
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800529 MastershipRole myNextRole = nextRole;
530 if (myNextRole == NONE) {
531 mastershipService.requestRoleFor(did);
532 MastershipTerm term = termService.getMastershipTerm(did);
Madan Jampanide003d92015-05-11 17:14:20 -0700533 if (term != null && localNodeId.equals(term.master())) {
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800534 myNextRole = MASTER;
535 } else {
536 myNextRole = STANDBY;
537 }
538 }
539
540 switch (myNextRole) {
541 case MASTER:
542 final Device device = getDevice(did);
543 if ((device != null) && !isAvailable(did)) {
544 //flag the device as online. Is there a better way to do this?
545 DefaultDeviceDescription deviceDescription
546 = new DefaultDeviceDescription(did.uri(),
547 device.type(),
548 device.manufacturer(),
549 device.hwVersion(),
550 device.swVersion(),
551 device.serialNumber(),
552 device.chassisId());
553 DeviceEvent devEvent =
554 store.createOrUpdateDevice(device.providerId(), did,
555 deviceDescription);
556 post(devEvent);
557 }
558 // TODO: should apply role only if there is mismatch
559 log.info("Applying role {} to {}", myNextRole, did);
560 if (!applyRoleAndProbe(did, MASTER)) {
561 // immediately failed to apply role
562 mastershipService.relinquishMastership(did);
563 // FIXME disconnect?
564 }
565 break;
566 case STANDBY:
567 log.info("Applying role {} to {}", myNextRole, did);
568 if (!applyRoleAndProbe(did, STANDBY)) {
569 // immediately failed to apply role
570 mastershipService.relinquishMastership(did);
571 // FIXME disconnect?
572 }
573 break;
574 case NONE:
575 default:
576 // should never reach here
577 log.error("You didn't see anything. I did not exist.");
578 break;
579 }
580 }
581
582 // Intercepts mastership events
583 private class InternalMastershipListener implements MastershipListener {
584
tomb41d1ac2014-09-24 01:51:24 -0700585 @Override
586 public void event(MastershipEvent event) {
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700587
588 if (event.type() != MastershipEvent.Type.MASTER_CHANGED) {
589 // Don't care if backup list changed.
590 return;
591 }
592
Ayaka Koshibe4c891272014-10-08 17:14:16 -0700593 final DeviceId did = event.subject();
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700594
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700595 // myRole suggested by MastershipService
596 MastershipRole myNextRole;
Madan Jampanide003d92015-05-11 17:14:20 -0700597 if (localNodeId.equals(event.roleInfo().master())) {
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700598 // confirm latest info
Ayaka Koshibeea5b4ce2014-10-11 14:17:17 -0700599 MastershipTerm term = termService.getMastershipTerm(did);
Madan Jampanide003d92015-05-11 17:14:20 -0700600 final boolean iHaveControl = term != null && localNodeId.equals(term.master());
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700601 if (iHaveControl) {
Yuta HIGUCHI38b4d9d2014-10-30 11:32:41 -0700602 deviceClockProviderService.setMastershipTerm(did, term);
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700603 myNextRole = MASTER;
Yuta HIGUCHI38b4d9d2014-10-30 11:32:41 -0700604 } else {
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700605 myNextRole = STANDBY;
606 }
Madan Jampanide003d92015-05-11 17:14:20 -0700607 } else if (event.roleInfo().backups().contains(localNodeId)) {
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700608 myNextRole = STANDBY;
609 } else {
610 myNextRole = NONE;
611 }
612
613
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700614 final boolean isReachable = isReachable(did);
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700615 if (!isReachable) {
616 // device is not connected to this node
617 if (myNextRole != NONE) {
618 log.warn("Node was instructed to be {} role for {}, "
619 + "but this node cannot reach the device. "
620 + "Relinquishing role. ",
621 myNextRole, did);
622 mastershipService.relinquishMastership(did);
623 }
624 return;
625 }
626
627 // device is connected to this node:
alshabibfd3cc052015-02-10 15:16:57 -0800628 if (store.getDevice(did) != null) {
629 reassertRole(did, myNextRole);
630 } else {
631 log.warn("Device is not yet/no longer in the store: {}", did);
632 }
Ayaka Koshibe317245a2014-10-29 00:34:43 -0700633 }
tomb41d1ac2014-09-24 01:51:24 -0700634 }
tomf80c9722014-09-24 14:49:18 -0700635
636 // Store delegate to re-post events emitted from the store.
alshabib271a6202014-09-28 22:11:21 -0700637 private class InternalStoreDelegate
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700638 implements DeviceStoreDelegate {
tomf80c9722014-09-24 14:49:18 -0700639 @Override
640 public void notify(DeviceEvent event) {
641 post(event);
642 }
643 }
tomd3097b02014-08-26 10:40:29 -0700644}