blob: 546b497e552801b38b4dc65284563f0b45f57ea4 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
2 * Copyright 2014 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
tombe988312014-09-19 18:38:47 -070016package org.onlab.onos.net.device.impl;
tomd3097b02014-08-26 10:40:29 -070017
alshabib339a3d92014-09-26 17:54:32 -070018import static com.google.common.base.Preconditions.checkNotNull;
19import static org.onlab.onos.net.device.DeviceEvent.Type.DEVICE_MASTERSHIP_CHANGED;
Yuta HIGUCHId26354d2014-10-31 14:14:38 -070020import static org.onlab.onos.net.MastershipRole.*;
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -080021import static org.onlab.util.Tools.namedThreads;
alshabib339a3d92014-09-26 17:54:32 -070022import static org.slf4j.LoggerFactory.getLogger;
23
24import java.util.List;
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -080025import java.util.concurrent.Executors;
26import java.util.concurrent.ScheduledExecutorService;
27import java.util.concurrent.TimeUnit;
alshabib339a3d92014-09-26 17:54:32 -070028
tomd3097b02014-08-26 10:40:29 -070029import org.apache.felix.scr.annotations.Activate;
30import org.apache.felix.scr.annotations.Component;
31import org.apache.felix.scr.annotations.Deactivate;
tom5f38b3a2014-08-27 23:50:54 -070032import org.apache.felix.scr.annotations.Reference;
33import org.apache.felix.scr.annotations.ReferenceCardinality;
tomd3097b02014-08-26 10:40:29 -070034import org.apache.felix.scr.annotations.Service;
tomb41d1ac2014-09-24 01:51:24 -070035import org.onlab.onos.cluster.ClusterService;
Yuta HIGUCHI8e939d82014-10-08 08:09:18 -070036import org.onlab.onos.cluster.NodeId;
tom96dfcab2014-08-28 09:26:03 -070037import org.onlab.onos.event.AbstractListenerRegistry;
38import org.onlab.onos.event.EventDeliveryService;
Yuta HIGUCHI80912e62014-10-12 00:15:47 -070039import org.onlab.onos.mastership.MastershipEvent;
40import org.onlab.onos.mastership.MastershipListener;
41import org.onlab.onos.mastership.MastershipService;
42import org.onlab.onos.mastership.MastershipTerm;
43import org.onlab.onos.mastership.MastershipTermService;
tom32f66842014-08-27 19:27:47 -070044import org.onlab.onos.net.Device;
tomd3097b02014-08-26 10:40:29 -070045import org.onlab.onos.net.DeviceId;
46import org.onlab.onos.net.MastershipRole;
tom32f66842014-08-27 19:27:47 -070047import org.onlab.onos.net.Port;
48import org.onlab.onos.net.PortNumber;
Ayaka Koshibeea5b4ce2014-10-11 14:17:17 -070049import org.onlab.onos.net.device.DefaultDeviceDescription;
tome5ec3fd2014-09-04 15:18:06 -070050import org.onlab.onos.net.device.DeviceAdminService;
Yuta HIGUCHI093e83e2014-10-10 22:26:11 -070051import org.onlab.onos.net.device.DeviceClockProviderService;
tomd3097b02014-08-26 10:40:29 -070052import org.onlab.onos.net.device.DeviceDescription;
tom32f66842014-08-27 19:27:47 -070053import org.onlab.onos.net.device.DeviceEvent;
54import org.onlab.onos.net.device.DeviceListener;
tomd3097b02014-08-26 10:40:29 -070055import org.onlab.onos.net.device.DeviceProvider;
tom96dfcab2014-08-28 09:26:03 -070056import org.onlab.onos.net.device.DeviceProviderRegistry;
tomd3097b02014-08-26 10:40:29 -070057import org.onlab.onos.net.device.DeviceProviderService;
tom32f66842014-08-27 19:27:47 -070058import org.onlab.onos.net.device.DeviceService;
tom41a2c5f2014-09-19 09:20:35 -070059import org.onlab.onos.net.device.DeviceStore;
tomf80c9722014-09-24 14:49:18 -070060import org.onlab.onos.net.device.DeviceStoreDelegate;
tomd3097b02014-08-26 10:40:29 -070061import org.onlab.onos.net.device.PortDescription;
tom96dfcab2014-08-28 09:26:03 -070062import org.onlab.onos.net.provider.AbstractProviderRegistry;
tomd3097b02014-08-26 10:40:29 -070063import org.onlab.onos.net.provider.AbstractProviderService;
64import org.slf4j.Logger;
tomd3097b02014-08-26 10:40:29 -070065
tomd3097b02014-08-26 10:40:29 -070066/**
tome4729872014-09-23 00:37:37 -070067 * Provides implementation of the device SB & NB APIs.
tomd3097b02014-08-26 10:40:29 -070068 */
69@Component(immediate = true)
70@Service
tom41a2c5f2014-09-19 09:20:35 -070071public class DeviceManager
Thomas Vachuskad16ce182014-10-29 17:25:29 -070072 extends AbstractProviderRegistry<DeviceProvider, DeviceProviderService>
73 implements DeviceService, DeviceAdminService, DeviceProviderRegistry {
tom32f66842014-08-27 19:27:47 -070074
tome5ec3fd2014-09-04 15:18:06 -070075 private static final String DEVICE_ID_NULL = "Device ID cannot be null";
76 private static final String PORT_NUMBER_NULL = "Port number cannot be null";
77 private static final String DEVICE_DESCRIPTION_NULL = "Device description cannot be null";
78 private static final String PORT_DESCRIPTION_NULL = "Port description cannot be null";
79 private static final String ROLE_NULL = "Role cannot be null";
tomd3097b02014-08-26 10:40:29 -070080
tom5f38b3a2014-08-27 23:50:54 -070081 private final Logger log = getLogger(getClass());
tomd3097b02014-08-26 10:40:29 -070082
alshabib271a6202014-09-28 22:11:21 -070083 protected final AbstractListenerRegistry<DeviceEvent, DeviceListener> listenerRegistry =
84 new AbstractListenerRegistry<>();
tom32f66842014-08-27 19:27:47 -070085
alshabib339a3d92014-09-26 17:54:32 -070086 private final DeviceStoreDelegate delegate = new InternalStoreDelegate();
tomf80c9722014-09-24 14:49:18 -070087
tomc78acee2014-09-24 15:16:55 -070088 private final MastershipListener mastershipListener = new InternalMastershipListener();
tomb41d1ac2014-09-24 01:51:24 -070089
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -080090 private ScheduledExecutorService backgroundService;
91
tom41a2c5f2014-09-19 09:20:35 -070092 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
93 protected DeviceStore store;
tomd3097b02014-08-26 10:40:29 -070094
tom5f38b3a2014-08-27 23:50:54 -070095 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
tome5ec3fd2014-09-04 15:18:06 -070096 protected EventDeliveryService eventDispatcher;
tom5f38b3a2014-08-27 23:50:54 -070097
Ayaka Koshibea7f044e2014-09-23 16:56:20 -070098 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
tomb41d1ac2014-09-24 01:51:24 -070099 protected ClusterService clusterService;
100
101 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Ayaka Koshibea7f044e2014-09-23 16:56:20 -0700102 protected MastershipService mastershipService;
103
Ayaka Koshibe3de43ca2014-09-26 16:40:23 -0700104 protected MastershipTermService termService;
105
Madan Jampani61056bc2014-09-27 09:07:26 -0700106 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Yuta HIGUCHI093e83e2014-10-10 22:26:11 -0700107 protected DeviceClockProviderService deviceClockProviderService;
Madan Jampani61056bc2014-09-27 09:07:26 -0700108
tomd3097b02014-08-26 10:40:29 -0700109 @Activate
110 public void activate() {
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800111 backgroundService = Executors.newSingleThreadScheduledExecutor(namedThreads("device-manager-background"));
112
tomf80c9722014-09-24 14:49:18 -0700113 store.setDelegate(delegate);
tom96dfcab2014-08-28 09:26:03 -0700114 eventDispatcher.addSink(DeviceEvent.class, listenerRegistry);
tomb41d1ac2014-09-24 01:51:24 -0700115 mastershipService.addListener(mastershipListener);
Ayaka Koshibe3de43ca2014-09-26 16:40:23 -0700116 termService = mastershipService.requestTermService();
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800117
118 backgroundService.scheduleWithFixedDelay(new Runnable() {
119
120 @Override
121 public void run() {
122 try {
123 mastershipCheck();
124 } catch (Exception e) {
125 log.error("Exception thrown during integrity check", e);
126 }
127 }
128 }, 1, 1, TimeUnit.MINUTES);
tomd3097b02014-08-26 10:40:29 -0700129 log.info("Started");
130 }
131
132 @Deactivate
133 public void deactivate() {
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800134 backgroundService.shutdown();
135
tomf80c9722014-09-24 14:49:18 -0700136 store.unsetDelegate(delegate);
tomb41d1ac2014-09-24 01:51:24 -0700137 mastershipService.removeListener(mastershipListener);
tom5f38b3a2014-08-27 23:50:54 -0700138 eventDispatcher.removeSink(DeviceEvent.class);
tomd3097b02014-08-26 10:40:29 -0700139 log.info("Stopped");
140 }
141
142 @Override
tomad2d2092014-09-06 23:24:20 -0700143 public int getDeviceCount() {
144 return store.getDeviceCount();
tomd3097b02014-08-26 10:40:29 -0700145 }
146
147 @Override
tom32f66842014-08-27 19:27:47 -0700148 public Iterable<Device> getDevices() {
tome5ec3fd2014-09-04 15:18:06 -0700149 return store.getDevices();
tomd3097b02014-08-26 10:40:29 -0700150 }
151
tom32f66842014-08-27 19:27:47 -0700152 @Override
153 public Device getDevice(DeviceId deviceId) {
154 checkNotNull(deviceId, DEVICE_ID_NULL);
tom132b58a2014-08-28 16:11:28 -0700155 return store.getDevice(deviceId);
tom32f66842014-08-27 19:27:47 -0700156 }
157
158 @Override
tomad2d2092014-09-06 23:24:20 -0700159 public MastershipRole getRole(DeviceId deviceId) {
160 checkNotNull(deviceId, DEVICE_ID_NULL);
tomb41d1ac2014-09-24 01:51:24 -0700161 return mastershipService.getLocalRole(deviceId);
tomad2d2092014-09-06 23:24:20 -0700162 }
163
164 @Override
tom32f66842014-08-27 19:27:47 -0700165 public List<Port> getPorts(DeviceId deviceId) {
166 checkNotNull(deviceId, DEVICE_ID_NULL);
tom132b58a2014-08-28 16:11:28 -0700167 return store.getPorts(deviceId);
tom32f66842014-08-27 19:27:47 -0700168 }
169
170 @Override
171 public Port getPort(DeviceId deviceId, PortNumber portNumber) {
172 checkNotNull(deviceId, DEVICE_ID_NULL);
173 checkNotNull(portNumber, PORT_NUMBER_NULL);
tom132b58a2014-08-28 16:11:28 -0700174 return store.getPort(deviceId, portNumber);
tom32f66842014-08-27 19:27:47 -0700175 }
176
177 @Override
tomff7eb7c2014-09-08 12:49:03 -0700178 public boolean isAvailable(DeviceId deviceId) {
179 checkNotNull(deviceId, DEVICE_ID_NULL);
180 return store.isAvailable(deviceId);
181 }
182
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700183 // Check a device for control channel connectivity.
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700184 private boolean isReachable(DeviceId deviceId) {
185 DeviceProvider provider = getProvider(deviceId);
186 if (provider != null) {
187 return provider.isReachable(deviceId);
188 } else {
Yuta HIGUCHI72669c42014-11-13 14:48:17 -0800189 log.debug("Provider not found for {}", deviceId);
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700190 return false;
191 }
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700192 }
193
tome5ec3fd2014-09-04 15:18:06 -0700194 @Override
195 public void removeDevice(DeviceId deviceId) {
196 checkNotNull(deviceId, DEVICE_ID_NULL);
197 DeviceEvent event = store.removeDevice(deviceId);
tom0efbb1d2014-09-09 11:54:28 -0700198 if (event != null) {
199 log.info("Device {} administratively removed", deviceId);
200 post(event);
201 }
tome5ec3fd2014-09-04 15:18:06 -0700202 }
203
tom7869ad92014-09-09 14:32:08 -0700204 @Override
205 public void addListener(DeviceListener listener) {
206 listenerRegistry.addListener(listener);
207 }
208
209 @Override
210 public void removeListener(DeviceListener listener) {
211 listenerRegistry.removeListener(listener);
212 }
213
214 @Override
alshabibb7b40632014-09-28 21:30:00 -0700215 protected DeviceProviderService createProviderService(
216 DeviceProvider provider) {
tom7869ad92014-09-09 14:32:08 -0700217 return new InternalDeviceProviderService(provider);
218 }
219
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800220 /**
221 * Checks if all the reachable devices have a valid mastership role.
222 */
223 private void mastershipCheck() {
224 log.debug("Checking mastership");
225 for (Device device : getDevices()) {
226 final DeviceId deviceId = device.id();
227 log.debug("Checking device {}", deviceId);
228
229 if (!isReachable(deviceId)) {
230 continue;
231 }
232
233 if (mastershipService.getLocalRole(deviceId) != NONE) {
234 continue;
235 }
236
237 log.info("{} is reachable but did not have a valid role, reasserting", deviceId);
238
239 // isReachable but was not MASTER or STANDBY, get a role and apply
240 // Note: NONE triggers request to MastershipService
241 reassertRole(deviceId, NONE);
242 }
243 }
244
tomd3097b02014-08-26 10:40:29 -0700245 // Personalized device provider service issued to the supplied provider.
tomdc361b62014-09-09 20:36:52 -0700246 private class InternalDeviceProviderService
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700247 extends AbstractProviderService<DeviceProvider>
248 implements DeviceProviderService {
tomd3097b02014-08-26 10:40:29 -0700249
tomcfde0622014-09-09 11:02:42 -0700250 InternalDeviceProviderService(DeviceProvider provider) {
tomd3097b02014-08-26 10:40:29 -0700251 super(provider);
252 }
253
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700254 /**
255 * Apply role in reaction to provider event.
256 *
257 * @param deviceId device identifier
258 * @param newRole new role to apply to the device
259 * @return true if the request was sent to provider
260 */
261 private boolean applyRole(DeviceId deviceId, MastershipRole newRole) {
262
263 if (newRole.equals(MastershipRole.NONE)) {
264 //no-op
265 return true;
266 }
267
268 DeviceProvider provider = provider();
269 if (provider == null) {
270 log.warn("Provider for {} was not found. Cannot apply role {}", deviceId, newRole);
271 return false;
272 }
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700273 provider.roleChanged(deviceId, newRole);
274 // not triggering probe when triggered by provider service event
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700275
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700276 return true;
277 }
278
279
tomd3097b02014-08-26 10:40:29 -0700280 @Override
alshabibb7b40632014-09-28 21:30:00 -0700281 public void deviceConnected(DeviceId deviceId,
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700282 DeviceDescription deviceDescription) {
tom32f66842014-08-27 19:27:47 -0700283 checkNotNull(deviceId, DEVICE_ID_NULL);
284 checkNotNull(deviceDescription, DEVICE_DESCRIPTION_NULL);
tomeadbb462014-09-07 16:10:19 -0700285 checkValidity();
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700286
287 log.info("Device {} connected", deviceId);
Yuta HIGUCHIdfe6e3b2014-10-30 11:31:51 -0700288 final NodeId myNodeId = clusterService.getLocalNode().id();
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700289
290 // check my Role
291 mastershipService.requestRoleFor(deviceId);
292 final MastershipTerm term = termService.getMastershipTerm(deviceId);
Yuta HIGUCHIdfe6e3b2014-10-30 11:31:51 -0700293 if (!myNodeId.equals(term.master())) {
Yuta HIGUCHI2d3cd312014-10-31 11:38:04 -0700294 log.info("Role of this node is STANDBY for {}", deviceId);
295 // TODO: Do we need to explicitly tell the Provider that
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700296 // this instance is not the MASTER
297 applyRole(deviceId, MastershipRole.STANDBY);
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700298 return;
299 }
Yuta HIGUCHI2d3cd312014-10-31 11:38:04 -0700300 log.info("Role of this node is MASTER for {}", deviceId);
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700301
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700302 // tell clock provider if this instance is the master
Yuta HIGUCHI093e83e2014-10-10 22:26:11 -0700303 deviceClockProviderService.setMastershipTerm(deviceId, term);
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700304
tome5ec3fd2014-09-04 15:18:06 -0700305 DeviceEvent event = store.createOrUpdateDevice(provider().id(),
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700306 deviceId, deviceDescription);
tom80c0e5e2014-09-08 18:08:58 -0700307
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700308 applyRole(deviceId, MastershipRole.MASTER);
309
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700310 // If there was a change of any kind, tell the provider
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700311 // that this instance is the master.
tom80c0e5e2014-09-08 18:08:58 -0700312 if (event != null) {
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700313 log.trace("event: {} {}", event.type(), event);
tom568581d2014-09-08 20:13:36 -0700314 post(event);
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700315 } else {
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700316 post(new DeviceEvent(DEVICE_MASTERSHIP_CHANGED, store.getDevice(deviceId)));
tom80c0e5e2014-09-08 18:08:58 -0700317 }
tomd3097b02014-08-26 10:40:29 -0700318 }
319
320 @Override
321 public void deviceDisconnected(DeviceId deviceId) {
tom32f66842014-08-27 19:27:47 -0700322 checkNotNull(deviceId, DEVICE_ID_NULL);
tomeadbb462014-09-07 16:10:19 -0700323 checkValidity();
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700324
Yuta HIGUCHI2d3cd312014-10-31 11:38:04 -0700325 log.info("Device {} disconnected from this node", deviceId);
Ayaka Koshibed9f693e2014-09-29 18:04:54 -0700326
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700327 DeviceEvent event = null;
328 try {
329 event = store.markOffline(deviceId);
330 } catch (IllegalStateException e) {
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700331 log.warn("Failed to mark {} offline", deviceId);
332 // only the MASTER should be marking off-line in normal cases,
333 // but if I was the last STANDBY connection, etc. and no one else
334 // was there to mark the device offline, this instance may need to
335 // temporarily request for Master Role and mark offline.
336
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700337 //there are times when this node will correctly have mastership, BUT
338 //that isn't reflected in the ClockManager before the device disconnects.
339 //we want to let go of the device anyways, so make sure this happens.
Yuta HIGUCHI0722fb22014-10-19 01:16:33 -0700340
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700341 // FIXME: Store semantics leaking out as IllegalStateException.
342 // Consider revising store API to handle this scenario.
343
344 MastershipRole role = mastershipService.requestRoleFor(deviceId);
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700345 MastershipTerm term = termService.getMastershipTerm(deviceId);
Yuta HIGUCHI0722fb22014-10-19 01:16:33 -0700346 final NodeId myNodeId = clusterService.getLocalNode().id();
347 // TODO: Move this type of check inside device clock manager, etc.
348 if (myNodeId.equals(term.master())) {
Yuta HIGUCHI2d3cd312014-10-31 11:38:04 -0700349 log.info("Retry marking {} offline", deviceId);
Yuta HIGUCHI0722fb22014-10-19 01:16:33 -0700350 deviceClockProviderService.setMastershipTerm(deviceId, term);
351 event = store.markOffline(deviceId);
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700352 } else {
Yuta HIGUCHI2d3cd312014-10-31 11:38:04 -0700353 log.info("Failed again marking {} offline. {}", deviceId, role);
Yuta HIGUCHI0722fb22014-10-19 01:16:33 -0700354 }
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700355 } finally {
356 //relinquish master role and ability to be backup.
357 mastershipService.relinquishMastership(deviceId);
358
359 if (event != null) {
Yuta HIGUCHI2d3cd312014-10-31 11:38:04 -0700360 log.info("Device {} disconnected from cluster", deviceId);
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700361 post(event);
362 }
tom0efbb1d2014-09-09 11:54:28 -0700363 }
tomd3097b02014-08-26 10:40:29 -0700364 }
365
366 @Override
alshabibb7b40632014-09-28 21:30:00 -0700367 public void updatePorts(DeviceId deviceId,
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700368 List<PortDescription> portDescriptions) {
tom32f66842014-08-27 19:27:47 -0700369 checkNotNull(deviceId, DEVICE_ID_NULL);
alshabibb7b40632014-09-28 21:30:00 -0700370 checkNotNull(portDescriptions,
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700371 "Port descriptions list cannot be null");
tomeadbb462014-09-07 16:10:19 -0700372 checkValidity();
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700373
Yuta HIGUCHI13c0b872014-10-30 18:09:22 -0700374 if (!deviceClockProviderService.isTimestampAvailable(deviceId)) {
375 // Never been a master for this device
376 // any update will be ignored.
377 log.trace("Ignoring {} port updates on standby node. {}", deviceId, portDescriptions);
378 return;
379 }
380
Yuta HIGUCHI5f6739c2014-10-01 14:04:01 -0700381 List<DeviceEvent> events = store.updatePorts(this.provider().id(),
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700382 deviceId, portDescriptions);
tom32f66842014-08-27 19:27:47 -0700383 for (DeviceEvent event : events) {
384 post(event);
385 }
tomd3097b02014-08-26 10:40:29 -0700386 }
387
388 @Override
alshabibb7b40632014-09-28 21:30:00 -0700389 public void portStatusChanged(DeviceId deviceId,
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700390 PortDescription portDescription) {
tom32f66842014-08-27 19:27:47 -0700391 checkNotNull(deviceId, DEVICE_ID_NULL);
392 checkNotNull(portDescription, PORT_DESCRIPTION_NULL);
tomeadbb462014-09-07 16:10:19 -0700393 checkValidity();
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700394
Yuta HIGUCHI13c0b872014-10-30 18:09:22 -0700395 if (!deviceClockProviderService.isTimestampAvailable(deviceId)) {
396 // Never been a master for this device
397 // any update will be ignored.
398 log.trace("Ignoring {} port update on standby node. {}", deviceId, portDescription);
399 return;
400 }
401
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700402 final DeviceEvent event = store.updatePortStatus(this.provider().id(),
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700403 deviceId, portDescription);
tomff7eb7c2014-09-08 12:49:03 -0700404 if (event != null) {
alshabibb7b40632014-09-28 21:30:00 -0700405 log.info("Device {} port {} status changed", deviceId, event
406 .port().number());
tom0efbb1d2014-09-09 11:54:28 -0700407 post(event);
tomff7eb7c2014-09-08 12:49:03 -0700408 }
tomd3097b02014-08-26 10:40:29 -0700409 }
tom3f2bbd72014-09-24 12:07:58 -0700410
411 @Override
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700412 public void receivedRoleReply(
413 DeviceId deviceId, MastershipRole requested, MastershipRole response) {
414 // Several things can happen here:
415 // 1. request and response match
416 // 2. request and response don't match
417 // 3. MastershipRole and requested match (and 1 or 2 are true)
418 // 4. MastershipRole and requested don't match (and 1 or 2 are true)
419 //
420 // 2, 4, and 3 with case 2 are failure modes.
421
tom3f2bbd72014-09-24 12:07:58 -0700422 // FIXME: implement response to this notification
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700423
424 log.info("got reply to a role request for {}: asked for {}, and got {}",
425 deviceId, requested, response);
426
427 if (requested == null && response == null) {
428 // something was off with DeviceProvider, maybe check channel too?
429 log.warn("Failed to assert role [{}] onto Device {}", requested, deviceId);
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700430 mastershipService.relinquishMastership(deviceId);
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700431 return;
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700432 }
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700433
434 if (requested.equals(response)) {
435 if (requested.equals(mastershipService.getLocalRole(deviceId))) {
436
437 return;
438 } else {
439 return;
440 // FIXME roleManager got the device to comply, but doesn't agree with
441 // the store; use the store's view, then try to reassert.
442 }
443 } else {
444 // we didn't get back what we asked for. Reelect someone else.
445 log.warn("Failed to assert role [{}] onto Device {}", response, deviceId);
446 if (response == MastershipRole.MASTER) {
447 mastershipService.relinquishMastership(deviceId);
448 // TODO: Shouldn't we be triggering event?
449 //final Device device = getDevice(deviceId);
450 //post(new DeviceEvent(DEVICE_MASTERSHIP_CHANGED, device));
451 }
452 }
453
tom3f2bbd72014-09-24 12:07:58 -0700454 }
tomd3097b02014-08-26 10:40:29 -0700455 }
tom32f66842014-08-27 19:27:47 -0700456
tomeadbb462014-09-07 16:10:19 -0700457 // Posts the specified event to the local event dispatcher.
tom32f66842014-08-27 19:27:47 -0700458 private void post(DeviceEvent event) {
459 if (event != null && eventDispatcher != null) {
460 eventDispatcher.post(event);
461 }
462 }
463
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800464 // Applies the specified role to the device; ignores NONE
465 /**
466 * Apply role to device and send probe if MASTER.
467 *
468 * @param deviceId device identifier
469 * @param newRole new role to apply to the device
470 * @return true if the request was sent to provider
471 */
472 private boolean applyRoleAndProbe(DeviceId deviceId, MastershipRole newRole) {
473 if (newRole.equals(MastershipRole.NONE)) {
474 //no-op
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700475 return true;
476 }
Ayaka Koshibe317245a2014-10-29 00:34:43 -0700477
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800478 Device device = store.getDevice(deviceId);
479 // FIXME: Device might not be there yet. (eventual consistent)
480 // FIXME relinquish role
481 if (device == null) {
482 log.warn("{} was not there. Cannot apply role {}", deviceId, newRole);
483 return false;
484 }
485
486 DeviceProvider provider = getProvider(device.providerId());
487 if (provider == null) {
488 log.warn("Provider for {} was not found. Cannot apply role {}", deviceId, newRole);
489 return false;
490 }
491 provider.roleChanged(deviceId, newRole);
492
493 if (newRole.equals(MastershipRole.MASTER)) {
494 // only trigger event when request was sent to provider
495 // TODO: consider removing this from Device event type?
496 post(new DeviceEvent(DEVICE_MASTERSHIP_CHANGED, device));
497
498 provider.triggerProbe(device);
499 }
500 return true;
501 }
502
503 /**
504 * Reaasert role for specified device connected to this node.
505 *
506 * @param did device identifier
507 * @param nextRole role to apply. If NONE is specified,
508 * it will ask mastership service for a role and apply it.
509 */
510 private void reassertRole(final DeviceId did,
511 final MastershipRole nextRole) {
512
513 final NodeId myNodeId = clusterService.getLocalNode().id();
514 MastershipRole myNextRole = nextRole;
515 if (myNextRole == NONE) {
516 mastershipService.requestRoleFor(did);
517 MastershipTerm term = termService.getMastershipTerm(did);
518 if (myNodeId.equals(term.master())) {
519 myNextRole = MASTER;
520 } else {
521 myNextRole = STANDBY;
522 }
523 }
524
525 switch (myNextRole) {
526 case MASTER:
527 final Device device = getDevice(did);
528 if ((device != null) && !isAvailable(did)) {
529 //flag the device as online. Is there a better way to do this?
530 DefaultDeviceDescription deviceDescription
531 = new DefaultDeviceDescription(did.uri(),
532 device.type(),
533 device.manufacturer(),
534 device.hwVersion(),
535 device.swVersion(),
536 device.serialNumber(),
537 device.chassisId());
538 DeviceEvent devEvent =
539 store.createOrUpdateDevice(device.providerId(), did,
540 deviceDescription);
541 post(devEvent);
542 }
543 // TODO: should apply role only if there is mismatch
544 log.info("Applying role {} to {}", myNextRole, did);
545 if (!applyRoleAndProbe(did, MASTER)) {
546 // immediately failed to apply role
547 mastershipService.relinquishMastership(did);
548 // FIXME disconnect?
549 }
550 break;
551 case STANDBY:
552 log.info("Applying role {} to {}", myNextRole, did);
553 if (!applyRoleAndProbe(did, STANDBY)) {
554 // immediately failed to apply role
555 mastershipService.relinquishMastership(did);
556 // FIXME disconnect?
557 }
558 break;
559 case NONE:
560 default:
561 // should never reach here
562 log.error("You didn't see anything. I did not exist.");
563 break;
564 }
565 }
566
567 // Intercepts mastership events
568 private class InternalMastershipListener implements MastershipListener {
569
tomb41d1ac2014-09-24 01:51:24 -0700570 @Override
571 public void event(MastershipEvent event) {
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700572
573 if (event.type() != MastershipEvent.Type.MASTER_CHANGED) {
574 // Don't care if backup list changed.
575 return;
576 }
577
Ayaka Koshibe4c891272014-10-08 17:14:16 -0700578 final DeviceId did = event.subject();
Ayaka Koshibeea5b4ce2014-10-11 14:17:17 -0700579 final NodeId myNodeId = clusterService.getLocalNode().id();
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700580
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700581 // myRole suggested by MastershipService
582 MastershipRole myNextRole;
Yuta HIGUCHI67dce882014-10-21 21:13:26 -0700583 if (myNodeId.equals(event.roleInfo().master())) {
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700584 // confirm latest info
Ayaka Koshibeea5b4ce2014-10-11 14:17:17 -0700585 MastershipTerm term = termService.getMastershipTerm(did);
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700586 final boolean iHaveControl = myNodeId.equals(term.master());
587 if (iHaveControl) {
Yuta HIGUCHI38b4d9d2014-10-30 11:32:41 -0700588 deviceClockProviderService.setMastershipTerm(did, term);
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700589 myNextRole = MASTER;
Yuta HIGUCHI38b4d9d2014-10-30 11:32:41 -0700590 } else {
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700591 myNextRole = STANDBY;
592 }
593 } else if (event.roleInfo().backups().contains(myNodeId)) {
594 myNextRole = STANDBY;
595 } else {
596 myNextRole = NONE;
597 }
598
599
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700600 final boolean isReachable = isReachable(did);
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700601 if (!isReachable) {
602 // device is not connected to this node
603 if (myNextRole != NONE) {
604 log.warn("Node was instructed to be {} role for {}, "
605 + "but this node cannot reach the device. "
606 + "Relinquishing role. ",
607 myNextRole, did);
608 mastershipService.relinquishMastership(did);
609 }
610 return;
611 }
612
613 // device is connected to this node:
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800614 reassertRole(did, myNextRole);
Ayaka Koshibe317245a2014-10-29 00:34:43 -0700615 }
tomb41d1ac2014-09-24 01:51:24 -0700616 }
tomf80c9722014-09-24 14:49:18 -0700617
618 // Store delegate to re-post events emitted from the store.
alshabib271a6202014-09-28 22:11:21 -0700619 private class InternalStoreDelegate
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700620 implements DeviceStoreDelegate {
tomf80c9722014-09-24 14:49:18 -0700621 @Override
622 public void notify(DeviceEvent event) {
623 post(event);
624 }
625 }
tomd3097b02014-08-26 10:40:29 -0700626}