blob: 597f15f1a8437a63a0a81bddaf5bbf6c8124f543 [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) {
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800185 if (deviceId == null) {
186 return false;
187 }
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700188 DeviceProvider provider = getProvider(deviceId);
189 if (provider != null) {
190 return provider.isReachable(deviceId);
191 } else {
Yuta HIGUCHI72669c42014-11-13 14:48:17 -0800192 log.debug("Provider not found for {}", deviceId);
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700193 return false;
194 }
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700195 }
196
tome5ec3fd2014-09-04 15:18:06 -0700197 @Override
198 public void removeDevice(DeviceId deviceId) {
199 checkNotNull(deviceId, DEVICE_ID_NULL);
200 DeviceEvent event = store.removeDevice(deviceId);
tom0efbb1d2014-09-09 11:54:28 -0700201 if (event != null) {
202 log.info("Device {} administratively removed", deviceId);
203 post(event);
204 }
tome5ec3fd2014-09-04 15:18:06 -0700205 }
206
tom7869ad92014-09-09 14:32:08 -0700207 @Override
208 public void addListener(DeviceListener listener) {
209 listenerRegistry.addListener(listener);
210 }
211
212 @Override
213 public void removeListener(DeviceListener listener) {
214 listenerRegistry.removeListener(listener);
215 }
216
217 @Override
alshabibb7b40632014-09-28 21:30:00 -0700218 protected DeviceProviderService createProviderService(
219 DeviceProvider provider) {
tom7869ad92014-09-09 14:32:08 -0700220 return new InternalDeviceProviderService(provider);
221 }
222
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800223 /**
224 * Checks if all the reachable devices have a valid mastership role.
225 */
226 private void mastershipCheck() {
227 log.debug("Checking mastership");
228 for (Device device : getDevices()) {
229 final DeviceId deviceId = device.id();
230 log.debug("Checking device {}", deviceId);
231
232 if (!isReachable(deviceId)) {
233 continue;
234 }
235
236 if (mastershipService.getLocalRole(deviceId) != NONE) {
237 continue;
238 }
239
240 log.info("{} is reachable but did not have a valid role, reasserting", deviceId);
241
242 // isReachable but was not MASTER or STANDBY, get a role and apply
243 // Note: NONE triggers request to MastershipService
244 reassertRole(deviceId, NONE);
245 }
246 }
247
tomd3097b02014-08-26 10:40:29 -0700248 // Personalized device provider service issued to the supplied provider.
tomdc361b62014-09-09 20:36:52 -0700249 private class InternalDeviceProviderService
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700250 extends AbstractProviderService<DeviceProvider>
251 implements DeviceProviderService {
tomd3097b02014-08-26 10:40:29 -0700252
tomcfde0622014-09-09 11:02:42 -0700253 InternalDeviceProviderService(DeviceProvider provider) {
tomd3097b02014-08-26 10:40:29 -0700254 super(provider);
255 }
256
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700257 /**
258 * Apply role in reaction to provider event.
259 *
260 * @param deviceId device identifier
261 * @param newRole new role to apply to the device
262 * @return true if the request was sent to provider
263 */
264 private boolean applyRole(DeviceId deviceId, MastershipRole newRole) {
265
266 if (newRole.equals(MastershipRole.NONE)) {
267 //no-op
268 return true;
269 }
270
271 DeviceProvider provider = provider();
272 if (provider == null) {
273 log.warn("Provider for {} was not found. Cannot apply role {}", deviceId, newRole);
274 return false;
275 }
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700276 provider.roleChanged(deviceId, newRole);
277 // not triggering probe when triggered by provider service event
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700278
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700279 return true;
280 }
281
282
tomd3097b02014-08-26 10:40:29 -0700283 @Override
alshabibb7b40632014-09-28 21:30:00 -0700284 public void deviceConnected(DeviceId deviceId,
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700285 DeviceDescription deviceDescription) {
tom32f66842014-08-27 19:27:47 -0700286 checkNotNull(deviceId, DEVICE_ID_NULL);
287 checkNotNull(deviceDescription, DEVICE_DESCRIPTION_NULL);
tomeadbb462014-09-07 16:10:19 -0700288 checkValidity();
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700289
290 log.info("Device {} connected", deviceId);
Yuta HIGUCHIdfe6e3b2014-10-30 11:31:51 -0700291 final NodeId myNodeId = clusterService.getLocalNode().id();
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700292
293 // check my Role
294 mastershipService.requestRoleFor(deviceId);
295 final MastershipTerm term = termService.getMastershipTerm(deviceId);
Yuta HIGUCHIdfe6e3b2014-10-30 11:31:51 -0700296 if (!myNodeId.equals(term.master())) {
Yuta HIGUCHI2d3cd312014-10-31 11:38:04 -0700297 log.info("Role of this node is STANDBY for {}", deviceId);
298 // TODO: Do we need to explicitly tell the Provider that
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700299 // this instance is not the MASTER
300 applyRole(deviceId, MastershipRole.STANDBY);
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700301 return;
302 }
Yuta HIGUCHI2d3cd312014-10-31 11:38:04 -0700303 log.info("Role of this node is MASTER for {}", deviceId);
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700304
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700305 // tell clock provider if this instance is the master
Yuta HIGUCHI093e83e2014-10-10 22:26:11 -0700306 deviceClockProviderService.setMastershipTerm(deviceId, term);
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700307
tome5ec3fd2014-09-04 15:18:06 -0700308 DeviceEvent event = store.createOrUpdateDevice(provider().id(),
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700309 deviceId, deviceDescription);
tom80c0e5e2014-09-08 18:08:58 -0700310
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700311 applyRole(deviceId, MastershipRole.MASTER);
312
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700313 // If there was a change of any kind, tell the provider
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700314 // that this instance is the master.
tom80c0e5e2014-09-08 18:08:58 -0700315 if (event != null) {
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700316 log.trace("event: {} {}", event.type(), event);
tom568581d2014-09-08 20:13:36 -0700317 post(event);
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700318 } else {
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700319 post(new DeviceEvent(DEVICE_MASTERSHIP_CHANGED, store.getDevice(deviceId)));
tom80c0e5e2014-09-08 18:08:58 -0700320 }
tomd3097b02014-08-26 10:40:29 -0700321 }
322
323 @Override
324 public void deviceDisconnected(DeviceId deviceId) {
tom32f66842014-08-27 19:27:47 -0700325 checkNotNull(deviceId, DEVICE_ID_NULL);
tomeadbb462014-09-07 16:10:19 -0700326 checkValidity();
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700327
Yuta HIGUCHI2d3cd312014-10-31 11:38:04 -0700328 log.info("Device {} disconnected from this node", deviceId);
Ayaka Koshibed9f693e2014-09-29 18:04:54 -0700329
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700330 DeviceEvent event = null;
331 try {
332 event = store.markOffline(deviceId);
333 } catch (IllegalStateException e) {
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700334 log.warn("Failed to mark {} offline", deviceId);
335 // only the MASTER should be marking off-line in normal cases,
336 // but if I was the last STANDBY connection, etc. and no one else
337 // was there to mark the device offline, this instance may need to
338 // temporarily request for Master Role and mark offline.
339
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700340 //there are times when this node will correctly have mastership, BUT
341 //that isn't reflected in the ClockManager before the device disconnects.
342 //we want to let go of the device anyways, so make sure this happens.
Yuta HIGUCHI0722fb22014-10-19 01:16:33 -0700343
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700344 // FIXME: Store semantics leaking out as IllegalStateException.
345 // Consider revising store API to handle this scenario.
346
347 MastershipRole role = mastershipService.requestRoleFor(deviceId);
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700348 MastershipTerm term = termService.getMastershipTerm(deviceId);
Yuta HIGUCHI0722fb22014-10-19 01:16:33 -0700349 final NodeId myNodeId = clusterService.getLocalNode().id();
350 // TODO: Move this type of check inside device clock manager, etc.
351 if (myNodeId.equals(term.master())) {
Yuta HIGUCHI2d3cd312014-10-31 11:38:04 -0700352 log.info("Retry marking {} offline", deviceId);
Yuta HIGUCHI0722fb22014-10-19 01:16:33 -0700353 deviceClockProviderService.setMastershipTerm(deviceId, term);
354 event = store.markOffline(deviceId);
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700355 } else {
Yuta HIGUCHI2d3cd312014-10-31 11:38:04 -0700356 log.info("Failed again marking {} offline. {}", deviceId, role);
Yuta HIGUCHI0722fb22014-10-19 01:16:33 -0700357 }
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700358 } finally {
359 //relinquish master role and ability to be backup.
360 mastershipService.relinquishMastership(deviceId);
361
362 if (event != null) {
Yuta HIGUCHI2d3cd312014-10-31 11:38:04 -0700363 log.info("Device {} disconnected from cluster", deviceId);
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700364 post(event);
365 }
tom0efbb1d2014-09-09 11:54:28 -0700366 }
tomd3097b02014-08-26 10:40:29 -0700367 }
368
369 @Override
alshabibb7b40632014-09-28 21:30:00 -0700370 public void updatePorts(DeviceId deviceId,
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700371 List<PortDescription> portDescriptions) {
tom32f66842014-08-27 19:27:47 -0700372 checkNotNull(deviceId, DEVICE_ID_NULL);
alshabibb7b40632014-09-28 21:30:00 -0700373 checkNotNull(portDescriptions,
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700374 "Port descriptions list cannot be null");
tomeadbb462014-09-07 16:10:19 -0700375 checkValidity();
Yuta HIGUCHI13c0b872014-10-30 18:09:22 -0700376 if (!deviceClockProviderService.isTimestampAvailable(deviceId)) {
377 // Never been a master for this device
378 // any update will be ignored.
379 log.trace("Ignoring {} port updates on standby node. {}", deviceId, portDescriptions);
380 return;
381 }
382
Yuta HIGUCHI5f6739c2014-10-01 14:04:01 -0700383 List<DeviceEvent> events = store.updatePorts(this.provider().id(),
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700384 deviceId, portDescriptions);
tom32f66842014-08-27 19:27:47 -0700385 for (DeviceEvent event : events) {
386 post(event);
387 }
tomd3097b02014-08-26 10:40:29 -0700388 }
389
390 @Override
alshabibb7b40632014-09-28 21:30:00 -0700391 public void portStatusChanged(DeviceId deviceId,
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700392 PortDescription portDescription) {
tom32f66842014-08-27 19:27:47 -0700393 checkNotNull(deviceId, DEVICE_ID_NULL);
394 checkNotNull(portDescription, PORT_DESCRIPTION_NULL);
tomeadbb462014-09-07 16:10:19 -0700395 checkValidity();
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700396
Yuta HIGUCHI13c0b872014-10-30 18:09:22 -0700397 if (!deviceClockProviderService.isTimestampAvailable(deviceId)) {
398 // Never been a master for this device
399 // any update will be ignored.
400 log.trace("Ignoring {} port update on standby node. {}", deviceId, portDescription);
401 return;
402 }
403
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700404 final DeviceEvent event = store.updatePortStatus(this.provider().id(),
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700405 deviceId, portDescription);
tomff7eb7c2014-09-08 12:49:03 -0700406 if (event != null) {
alshabibb7b40632014-09-28 21:30:00 -0700407 log.info("Device {} port {} status changed", deviceId, event
408 .port().number());
tom0efbb1d2014-09-09 11:54:28 -0700409 post(event);
tomff7eb7c2014-09-08 12:49:03 -0700410 }
tomd3097b02014-08-26 10:40:29 -0700411 }
tom3f2bbd72014-09-24 12:07:58 -0700412
413 @Override
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700414 public void receivedRoleReply(
415 DeviceId deviceId, MastershipRole requested, MastershipRole response) {
416 // Several things can happen here:
417 // 1. request and response match
418 // 2. request and response don't match
419 // 3. MastershipRole and requested match (and 1 or 2 are true)
420 // 4. MastershipRole and requested don't match (and 1 or 2 are true)
421 //
422 // 2, 4, and 3 with case 2 are failure modes.
423
tom3f2bbd72014-09-24 12:07:58 -0700424 // FIXME: implement response to this notification
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700425
426 log.info("got reply to a role request for {}: asked for {}, and got {}",
427 deviceId, requested, response);
428
429 if (requested == null && response == null) {
430 // something was off with DeviceProvider, maybe check channel too?
431 log.warn("Failed to assert role [{}] onto Device {}", requested, deviceId);
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700432 mastershipService.relinquishMastership(deviceId);
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700433 return;
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700434 }
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700435
436 if (requested.equals(response)) {
437 if (requested.equals(mastershipService.getLocalRole(deviceId))) {
438
439 return;
440 } else {
441 return;
442 // FIXME roleManager got the device to comply, but doesn't agree with
443 // the store; use the store's view, then try to reassert.
444 }
445 } else {
446 // we didn't get back what we asked for. Reelect someone else.
447 log.warn("Failed to assert role [{}] onto Device {}", response, deviceId);
448 if (response == MastershipRole.MASTER) {
449 mastershipService.relinquishMastership(deviceId);
450 // TODO: Shouldn't we be triggering event?
451 //final Device device = getDevice(deviceId);
452 //post(new DeviceEvent(DEVICE_MASTERSHIP_CHANGED, device));
453 }
454 }
455
tom3f2bbd72014-09-24 12:07:58 -0700456 }
tomd3097b02014-08-26 10:40:29 -0700457 }
tom32f66842014-08-27 19:27:47 -0700458
tomeadbb462014-09-07 16:10:19 -0700459 // Posts the specified event to the local event dispatcher.
tom32f66842014-08-27 19:27:47 -0700460 private void post(DeviceEvent event) {
461 if (event != null && eventDispatcher != null) {
462 eventDispatcher.post(event);
463 }
464 }
465
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800466 // Applies the specified role to the device; ignores NONE
467 /**
468 * Apply role to device and send probe if MASTER.
469 *
470 * @param deviceId device identifier
471 * @param newRole new role to apply to the device
472 * @return true if the request was sent to provider
473 */
474 private boolean applyRoleAndProbe(DeviceId deviceId, MastershipRole newRole) {
475 if (newRole.equals(MastershipRole.NONE)) {
476 //no-op
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700477 return true;
478 }
Ayaka Koshibe317245a2014-10-29 00:34:43 -0700479
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800480 DeviceProvider provider = getProvider(deviceId);
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800481 if (provider == null) {
482 log.warn("Provider for {} was not found. Cannot apply role {}", deviceId, newRole);
483 return false;
484 }
485 provider.roleChanged(deviceId, newRole);
486
487 if (newRole.equals(MastershipRole.MASTER)) {
488 // only trigger event when request was sent to provider
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800489 provider.triggerProbe(deviceId);
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800490 }
491 return true;
492 }
493
494 /**
495 * Reaasert role for specified device connected to this node.
496 *
497 * @param did device identifier
498 * @param nextRole role to apply. If NONE is specified,
499 * it will ask mastership service for a role and apply it.
500 */
501 private void reassertRole(final DeviceId did,
502 final MastershipRole nextRole) {
503
504 final NodeId myNodeId = clusterService.getLocalNode().id();
505 MastershipRole myNextRole = nextRole;
506 if (myNextRole == NONE) {
507 mastershipService.requestRoleFor(did);
508 MastershipTerm term = termService.getMastershipTerm(did);
509 if (myNodeId.equals(term.master())) {
510 myNextRole = MASTER;
511 } else {
512 myNextRole = STANDBY;
513 }
514 }
515
516 switch (myNextRole) {
517 case MASTER:
518 final Device device = getDevice(did);
519 if ((device != null) && !isAvailable(did)) {
520 //flag the device as online. Is there a better way to do this?
521 DefaultDeviceDescription deviceDescription
522 = new DefaultDeviceDescription(did.uri(),
523 device.type(),
524 device.manufacturer(),
525 device.hwVersion(),
526 device.swVersion(),
527 device.serialNumber(),
528 device.chassisId());
529 DeviceEvent devEvent =
530 store.createOrUpdateDevice(device.providerId(), did,
531 deviceDescription);
532 post(devEvent);
533 }
534 // TODO: should apply role only if there is mismatch
535 log.info("Applying role {} to {}", myNextRole, did);
536 if (!applyRoleAndProbe(did, MASTER)) {
537 // immediately failed to apply role
538 mastershipService.relinquishMastership(did);
539 // FIXME disconnect?
540 }
541 break;
542 case STANDBY:
543 log.info("Applying role {} to {}", myNextRole, did);
544 if (!applyRoleAndProbe(did, STANDBY)) {
545 // immediately failed to apply role
546 mastershipService.relinquishMastership(did);
547 // FIXME disconnect?
548 }
549 break;
550 case NONE:
551 default:
552 // should never reach here
553 log.error("You didn't see anything. I did not exist.");
554 break;
555 }
556 }
557
558 // Intercepts mastership events
559 private class InternalMastershipListener implements MastershipListener {
560
tomb41d1ac2014-09-24 01:51:24 -0700561 @Override
562 public void event(MastershipEvent event) {
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700563
564 if (event.type() != MastershipEvent.Type.MASTER_CHANGED) {
565 // Don't care if backup list changed.
566 return;
567 }
568
Ayaka Koshibe4c891272014-10-08 17:14:16 -0700569 final DeviceId did = event.subject();
Ayaka Koshibeea5b4ce2014-10-11 14:17:17 -0700570 final NodeId myNodeId = clusterService.getLocalNode().id();
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700571
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700572 // myRole suggested by MastershipService
573 MastershipRole myNextRole;
Yuta HIGUCHI67dce882014-10-21 21:13:26 -0700574 if (myNodeId.equals(event.roleInfo().master())) {
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700575 // confirm latest info
Ayaka Koshibeea5b4ce2014-10-11 14:17:17 -0700576 MastershipTerm term = termService.getMastershipTerm(did);
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700577 final boolean iHaveControl = myNodeId.equals(term.master());
578 if (iHaveControl) {
Yuta HIGUCHI38b4d9d2014-10-30 11:32:41 -0700579 deviceClockProviderService.setMastershipTerm(did, term);
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700580 myNextRole = MASTER;
Yuta HIGUCHI38b4d9d2014-10-30 11:32:41 -0700581 } else {
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700582 myNextRole = STANDBY;
583 }
584 } else if (event.roleInfo().backups().contains(myNodeId)) {
585 myNextRole = STANDBY;
586 } else {
587 myNextRole = NONE;
588 }
589
590
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700591 final boolean isReachable = isReachable(did);
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700592 if (!isReachable) {
593 // device is not connected to this node
594 if (myNextRole != NONE) {
595 log.warn("Node was instructed to be {} role for {}, "
596 + "but this node cannot reach the device. "
597 + "Relinquishing role. ",
598 myNextRole, did);
599 mastershipService.relinquishMastership(did);
600 }
601 return;
602 }
603
604 // device is connected to this node:
Yuta HIGUCHI63323fd2014-11-11 12:16:58 -0800605 reassertRole(did, myNextRole);
Ayaka Koshibe317245a2014-10-29 00:34:43 -0700606 }
tomb41d1ac2014-09-24 01:51:24 -0700607 }
tomf80c9722014-09-24 14:49:18 -0700608
609 // Store delegate to re-post events emitted from the store.
alshabib271a6202014-09-28 22:11:21 -0700610 private class InternalStoreDelegate
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700611 implements DeviceStoreDelegate {
tomf80c9722014-09-24 14:49:18 -0700612 @Override
613 public void notify(DeviceEvent event) {
614 post(event);
615 }
616 }
tomd3097b02014-08-26 10:40:29 -0700617}