blob: 68665e4dc5436bbee94b3a5c3045e30b9443cc87 [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.*;
alshabib339a3d92014-09-26 17:54:32 -070021import static org.slf4j.LoggerFactory.getLogger;
22
23import java.util.List;
24
tomd3097b02014-08-26 10:40:29 -070025import org.apache.felix.scr.annotations.Activate;
26import org.apache.felix.scr.annotations.Component;
27import org.apache.felix.scr.annotations.Deactivate;
tom5f38b3a2014-08-27 23:50:54 -070028import org.apache.felix.scr.annotations.Reference;
29import org.apache.felix.scr.annotations.ReferenceCardinality;
tomd3097b02014-08-26 10:40:29 -070030import org.apache.felix.scr.annotations.Service;
tomb41d1ac2014-09-24 01:51:24 -070031import org.onlab.onos.cluster.ClusterService;
Yuta HIGUCHI8e939d82014-10-08 08:09:18 -070032import org.onlab.onos.cluster.NodeId;
tom96dfcab2014-08-28 09:26:03 -070033import org.onlab.onos.event.AbstractListenerRegistry;
34import org.onlab.onos.event.EventDeliveryService;
Yuta HIGUCHI80912e62014-10-12 00:15:47 -070035import org.onlab.onos.mastership.MastershipEvent;
36import org.onlab.onos.mastership.MastershipListener;
37import org.onlab.onos.mastership.MastershipService;
38import org.onlab.onos.mastership.MastershipTerm;
39import org.onlab.onos.mastership.MastershipTermService;
tom32f66842014-08-27 19:27:47 -070040import org.onlab.onos.net.Device;
tomd3097b02014-08-26 10:40:29 -070041import org.onlab.onos.net.DeviceId;
42import org.onlab.onos.net.MastershipRole;
tom32f66842014-08-27 19:27:47 -070043import org.onlab.onos.net.Port;
44import org.onlab.onos.net.PortNumber;
Ayaka Koshibeea5b4ce2014-10-11 14:17:17 -070045import org.onlab.onos.net.device.DefaultDeviceDescription;
tome5ec3fd2014-09-04 15:18:06 -070046import org.onlab.onos.net.device.DeviceAdminService;
Yuta HIGUCHI093e83e2014-10-10 22:26:11 -070047import org.onlab.onos.net.device.DeviceClockProviderService;
tomd3097b02014-08-26 10:40:29 -070048import org.onlab.onos.net.device.DeviceDescription;
tom32f66842014-08-27 19:27:47 -070049import org.onlab.onos.net.device.DeviceEvent;
50import org.onlab.onos.net.device.DeviceListener;
tomd3097b02014-08-26 10:40:29 -070051import org.onlab.onos.net.device.DeviceProvider;
tom96dfcab2014-08-28 09:26:03 -070052import org.onlab.onos.net.device.DeviceProviderRegistry;
tomd3097b02014-08-26 10:40:29 -070053import org.onlab.onos.net.device.DeviceProviderService;
tom32f66842014-08-27 19:27:47 -070054import org.onlab.onos.net.device.DeviceService;
tom41a2c5f2014-09-19 09:20:35 -070055import org.onlab.onos.net.device.DeviceStore;
tomf80c9722014-09-24 14:49:18 -070056import org.onlab.onos.net.device.DeviceStoreDelegate;
tomd3097b02014-08-26 10:40:29 -070057import org.onlab.onos.net.device.PortDescription;
tom96dfcab2014-08-28 09:26:03 -070058import org.onlab.onos.net.provider.AbstractProviderRegistry;
tomd3097b02014-08-26 10:40:29 -070059import org.onlab.onos.net.provider.AbstractProviderService;
60import org.slf4j.Logger;
tomd3097b02014-08-26 10:40:29 -070061
tomd3097b02014-08-26 10:40:29 -070062/**
tome4729872014-09-23 00:37:37 -070063 * Provides implementation of the device SB & NB APIs.
tomd3097b02014-08-26 10:40:29 -070064 */
65@Component(immediate = true)
66@Service
tom41a2c5f2014-09-19 09:20:35 -070067public class DeviceManager
Thomas Vachuskad16ce182014-10-29 17:25:29 -070068 extends AbstractProviderRegistry<DeviceProvider, DeviceProviderService>
69 implements DeviceService, DeviceAdminService, DeviceProviderRegistry {
tom32f66842014-08-27 19:27:47 -070070
tome5ec3fd2014-09-04 15:18:06 -070071 private static final String DEVICE_ID_NULL = "Device ID cannot be null";
72 private static final String PORT_NUMBER_NULL = "Port number cannot be null";
73 private static final String DEVICE_DESCRIPTION_NULL = "Device description cannot be null";
74 private static final String PORT_DESCRIPTION_NULL = "Port description cannot be null";
75 private static final String ROLE_NULL = "Role cannot be null";
tomd3097b02014-08-26 10:40:29 -070076
tom5f38b3a2014-08-27 23:50:54 -070077 private final Logger log = getLogger(getClass());
tomd3097b02014-08-26 10:40:29 -070078
alshabib271a6202014-09-28 22:11:21 -070079 protected final AbstractListenerRegistry<DeviceEvent, DeviceListener> listenerRegistry =
80 new AbstractListenerRegistry<>();
tom32f66842014-08-27 19:27:47 -070081
alshabib339a3d92014-09-26 17:54:32 -070082 private final DeviceStoreDelegate delegate = new InternalStoreDelegate();
tomf80c9722014-09-24 14:49:18 -070083
tomc78acee2014-09-24 15:16:55 -070084 private final MastershipListener mastershipListener = new InternalMastershipListener();
tomb41d1ac2014-09-24 01:51:24 -070085
tom41a2c5f2014-09-19 09:20:35 -070086 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
87 protected DeviceStore store;
tomd3097b02014-08-26 10:40:29 -070088
tom5f38b3a2014-08-27 23:50:54 -070089 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
tome5ec3fd2014-09-04 15:18:06 -070090 protected EventDeliveryService eventDispatcher;
tom5f38b3a2014-08-27 23:50:54 -070091
Ayaka Koshibea7f044e2014-09-23 16:56:20 -070092 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
tomb41d1ac2014-09-24 01:51:24 -070093 protected ClusterService clusterService;
94
95 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Ayaka Koshibea7f044e2014-09-23 16:56:20 -070096 protected MastershipService mastershipService;
97
Ayaka Koshibe3de43ca2014-09-26 16:40:23 -070098 protected MastershipTermService termService;
99
Madan Jampani61056bc2014-09-27 09:07:26 -0700100 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Yuta HIGUCHI093e83e2014-10-10 22:26:11 -0700101 protected DeviceClockProviderService deviceClockProviderService;
Madan Jampani61056bc2014-09-27 09:07:26 -0700102
tomd3097b02014-08-26 10:40:29 -0700103 @Activate
104 public void activate() {
tomf80c9722014-09-24 14:49:18 -0700105 store.setDelegate(delegate);
tom96dfcab2014-08-28 09:26:03 -0700106 eventDispatcher.addSink(DeviceEvent.class, listenerRegistry);
tomb41d1ac2014-09-24 01:51:24 -0700107 mastershipService.addListener(mastershipListener);
Ayaka Koshibe3de43ca2014-09-26 16:40:23 -0700108 termService = mastershipService.requestTermService();
tomd3097b02014-08-26 10:40:29 -0700109 log.info("Started");
110 }
111
112 @Deactivate
113 public void deactivate() {
tomf80c9722014-09-24 14:49:18 -0700114 store.unsetDelegate(delegate);
tomb41d1ac2014-09-24 01:51:24 -0700115 mastershipService.removeListener(mastershipListener);
tom5f38b3a2014-08-27 23:50:54 -0700116 eventDispatcher.removeSink(DeviceEvent.class);
tomd3097b02014-08-26 10:40:29 -0700117 log.info("Stopped");
118 }
119
120 @Override
tomad2d2092014-09-06 23:24:20 -0700121 public int getDeviceCount() {
122 return store.getDeviceCount();
tomd3097b02014-08-26 10:40:29 -0700123 }
124
125 @Override
tom32f66842014-08-27 19:27:47 -0700126 public Iterable<Device> getDevices() {
tome5ec3fd2014-09-04 15:18:06 -0700127 return store.getDevices();
tomd3097b02014-08-26 10:40:29 -0700128 }
129
tom32f66842014-08-27 19:27:47 -0700130 @Override
131 public Device getDevice(DeviceId deviceId) {
132 checkNotNull(deviceId, DEVICE_ID_NULL);
tom132b58a2014-08-28 16:11:28 -0700133 return store.getDevice(deviceId);
tom32f66842014-08-27 19:27:47 -0700134 }
135
136 @Override
tomad2d2092014-09-06 23:24:20 -0700137 public MastershipRole getRole(DeviceId deviceId) {
138 checkNotNull(deviceId, DEVICE_ID_NULL);
tomb41d1ac2014-09-24 01:51:24 -0700139 return mastershipService.getLocalRole(deviceId);
tomad2d2092014-09-06 23:24:20 -0700140 }
141
142 @Override
tom32f66842014-08-27 19:27:47 -0700143 public List<Port> getPorts(DeviceId deviceId) {
144 checkNotNull(deviceId, DEVICE_ID_NULL);
tom132b58a2014-08-28 16:11:28 -0700145 return store.getPorts(deviceId);
tom32f66842014-08-27 19:27:47 -0700146 }
147
148 @Override
149 public Port getPort(DeviceId deviceId, PortNumber portNumber) {
150 checkNotNull(deviceId, DEVICE_ID_NULL);
151 checkNotNull(portNumber, PORT_NUMBER_NULL);
tom132b58a2014-08-28 16:11:28 -0700152 return store.getPort(deviceId, portNumber);
tom32f66842014-08-27 19:27:47 -0700153 }
154
155 @Override
tomff7eb7c2014-09-08 12:49:03 -0700156 public boolean isAvailable(DeviceId deviceId) {
157 checkNotNull(deviceId, DEVICE_ID_NULL);
158 return store.isAvailable(deviceId);
159 }
160
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700161 // Check a device for control channel connectivity.
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700162 private boolean isReachable(DeviceId deviceId) {
163 DeviceProvider provider = getProvider(deviceId);
164 if (provider != null) {
165 return provider.isReachable(deviceId);
166 } else {
167 log.error("Provider not found for {}", deviceId);
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700168 return false;
169 }
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700170 }
171
tome5ec3fd2014-09-04 15:18:06 -0700172 @Override
173 public void removeDevice(DeviceId deviceId) {
174 checkNotNull(deviceId, DEVICE_ID_NULL);
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700175 // XXX is this intended to apply to the full global topology?
176 // if so, we probably don't want the fact that we aren't
177 // MASTER to get in the way, as it would do now.
Yuta HIGUCHI67dce882014-10-21 21:13:26 -0700178 // FIXME: forward or broadcast and let the Master handler the event.
tome5ec3fd2014-09-04 15:18:06 -0700179 DeviceEvent event = store.removeDevice(deviceId);
tom0efbb1d2014-09-09 11:54:28 -0700180 if (event != null) {
181 log.info("Device {} administratively removed", deviceId);
182 post(event);
183 }
tome5ec3fd2014-09-04 15:18:06 -0700184 }
185
tom7869ad92014-09-09 14:32:08 -0700186 @Override
187 public void addListener(DeviceListener listener) {
188 listenerRegistry.addListener(listener);
189 }
190
191 @Override
192 public void removeListener(DeviceListener listener) {
193 listenerRegistry.removeListener(listener);
194 }
195
196 @Override
alshabibb7b40632014-09-28 21:30:00 -0700197 protected DeviceProviderService createProviderService(
198 DeviceProvider provider) {
tom7869ad92014-09-09 14:32:08 -0700199 return new InternalDeviceProviderService(provider);
200 }
201
tomd3097b02014-08-26 10:40:29 -0700202 // Personalized device provider service issued to the supplied provider.
tomdc361b62014-09-09 20:36:52 -0700203 private class InternalDeviceProviderService
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700204 extends AbstractProviderService<DeviceProvider>
205 implements DeviceProviderService {
tomd3097b02014-08-26 10:40:29 -0700206
tomcfde0622014-09-09 11:02:42 -0700207 InternalDeviceProviderService(DeviceProvider provider) {
tomd3097b02014-08-26 10:40:29 -0700208 super(provider);
209 }
210
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700211 /**
212 * Apply role in reaction to provider event.
213 *
214 * @param deviceId device identifier
215 * @param newRole new role to apply to the device
216 * @return true if the request was sent to provider
217 */
218 private boolean applyRole(DeviceId deviceId, MastershipRole newRole) {
219
220 if (newRole.equals(MastershipRole.NONE)) {
221 //no-op
222 return true;
223 }
224
225 DeviceProvider provider = provider();
226 if (provider == null) {
227 log.warn("Provider for {} was not found. Cannot apply role {}", deviceId, newRole);
228 return false;
229 }
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700230 provider.roleChanged(deviceId, newRole);
231 // not triggering probe when triggered by provider service event
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700232
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700233 return true;
234 }
235
236
tomd3097b02014-08-26 10:40:29 -0700237 @Override
alshabibb7b40632014-09-28 21:30:00 -0700238 public void deviceConnected(DeviceId deviceId,
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700239 DeviceDescription deviceDescription) {
tom32f66842014-08-27 19:27:47 -0700240 checkNotNull(deviceId, DEVICE_ID_NULL);
241 checkNotNull(deviceDescription, DEVICE_DESCRIPTION_NULL);
tomeadbb462014-09-07 16:10:19 -0700242 checkValidity();
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700243
244 log.info("Device {} connected", deviceId);
Yuta HIGUCHIdfe6e3b2014-10-30 11:31:51 -0700245 final NodeId myNodeId = clusterService.getLocalNode().id();
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700246
247 // check my Role
248 mastershipService.requestRoleFor(deviceId);
249 final MastershipTerm term = termService.getMastershipTerm(deviceId);
Yuta HIGUCHIdfe6e3b2014-10-30 11:31:51 -0700250 if (!myNodeId.equals(term.master())) {
Yuta HIGUCHI2d3cd312014-10-31 11:38:04 -0700251 log.info("Role of this node is STANDBY for {}", deviceId);
252 // TODO: Do we need to explicitly tell the Provider that
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700253 // this instance is not the MASTER
254 applyRole(deviceId, MastershipRole.STANDBY);
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700255 return;
256 }
Yuta HIGUCHI2d3cd312014-10-31 11:38:04 -0700257 log.info("Role of this node is MASTER for {}", deviceId);
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700258
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700259 // tell clock provider if this instance is the master
Yuta HIGUCHI093e83e2014-10-10 22:26:11 -0700260 deviceClockProviderService.setMastershipTerm(deviceId, term);
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700261
tome5ec3fd2014-09-04 15:18:06 -0700262 DeviceEvent event = store.createOrUpdateDevice(provider().id(),
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700263 deviceId, deviceDescription);
tom80c0e5e2014-09-08 18:08:58 -0700264
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700265 applyRole(deviceId, MastershipRole.MASTER);
266
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700267 // If there was a change of any kind, tell the provider
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700268 // that this instance is the master.
tom80c0e5e2014-09-08 18:08:58 -0700269 if (event != null) {
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700270 log.trace("event: {} {}", event.type(), event);
tom568581d2014-09-08 20:13:36 -0700271 post(event);
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700272 } else {
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700273 post(new DeviceEvent(DEVICE_MASTERSHIP_CHANGED, store.getDevice(deviceId)));
tom80c0e5e2014-09-08 18:08:58 -0700274 }
tomd3097b02014-08-26 10:40:29 -0700275 }
276
277 @Override
278 public void deviceDisconnected(DeviceId deviceId) {
tom32f66842014-08-27 19:27:47 -0700279 checkNotNull(deviceId, DEVICE_ID_NULL);
tomeadbb462014-09-07 16:10:19 -0700280 checkValidity();
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700281
Yuta HIGUCHI2d3cd312014-10-31 11:38:04 -0700282 log.info("Device {} disconnected from this node", deviceId);
Ayaka Koshibed9f693e2014-09-29 18:04:54 -0700283
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700284 DeviceEvent event = null;
285 try {
286 event = store.markOffline(deviceId);
287 } catch (IllegalStateException e) {
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700288 log.warn("Failed to mark {} offline", deviceId);
289 // only the MASTER should be marking off-line in normal cases,
290 // but if I was the last STANDBY connection, etc. and no one else
291 // was there to mark the device offline, this instance may need to
292 // temporarily request for Master Role and mark offline.
293
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700294 //there are times when this node will correctly have mastership, BUT
295 //that isn't reflected in the ClockManager before the device disconnects.
296 //we want to let go of the device anyways, so make sure this happens.
Yuta HIGUCHI0722fb22014-10-19 01:16:33 -0700297
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700298 // FIXME: Store semantics leaking out as IllegalStateException.
299 // Consider revising store API to handle this scenario.
300
301 MastershipRole role = mastershipService.requestRoleFor(deviceId);
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700302 MastershipTerm term = termService.getMastershipTerm(deviceId);
Yuta HIGUCHI0722fb22014-10-19 01:16:33 -0700303 final NodeId myNodeId = clusterService.getLocalNode().id();
304 // TODO: Move this type of check inside device clock manager, etc.
305 if (myNodeId.equals(term.master())) {
Yuta HIGUCHI2d3cd312014-10-31 11:38:04 -0700306 log.info("Retry marking {} offline", deviceId);
Yuta HIGUCHI0722fb22014-10-19 01:16:33 -0700307 deviceClockProviderService.setMastershipTerm(deviceId, term);
308 event = store.markOffline(deviceId);
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700309 } else {
Yuta HIGUCHI2d3cd312014-10-31 11:38:04 -0700310 log.info("Failed again marking {} offline. {}", deviceId, role);
Yuta HIGUCHI0722fb22014-10-19 01:16:33 -0700311 }
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700312 } finally {
313 //relinquish master role and ability to be backup.
314 mastershipService.relinquishMastership(deviceId);
315
316 if (event != null) {
Yuta HIGUCHI2d3cd312014-10-31 11:38:04 -0700317 log.info("Device {} disconnected from cluster", deviceId);
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700318 post(event);
319 }
tom0efbb1d2014-09-09 11:54:28 -0700320 }
tomd3097b02014-08-26 10:40:29 -0700321 }
322
323 @Override
alshabibb7b40632014-09-28 21:30:00 -0700324 public void updatePorts(DeviceId deviceId,
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700325 List<PortDescription> portDescriptions) {
tom32f66842014-08-27 19:27:47 -0700326 checkNotNull(deviceId, DEVICE_ID_NULL);
alshabibb7b40632014-09-28 21:30:00 -0700327 checkNotNull(portDescriptions,
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700328 "Port descriptions list cannot be null");
tomeadbb462014-09-07 16:10:19 -0700329 checkValidity();
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700330
Yuta HIGUCHI13c0b872014-10-30 18:09:22 -0700331 if (!deviceClockProviderService.isTimestampAvailable(deviceId)) {
332 // Never been a master for this device
333 // any update will be ignored.
334 log.trace("Ignoring {} port updates on standby node. {}", deviceId, portDescriptions);
335 return;
336 }
337
Yuta HIGUCHI5f6739c2014-10-01 14:04:01 -0700338 List<DeviceEvent> events = store.updatePorts(this.provider().id(),
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700339 deviceId, portDescriptions);
tom32f66842014-08-27 19:27:47 -0700340 for (DeviceEvent event : events) {
341 post(event);
342 }
tomd3097b02014-08-26 10:40:29 -0700343 }
344
345 @Override
alshabibb7b40632014-09-28 21:30:00 -0700346 public void portStatusChanged(DeviceId deviceId,
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700347 PortDescription portDescription) {
tom32f66842014-08-27 19:27:47 -0700348 checkNotNull(deviceId, DEVICE_ID_NULL);
349 checkNotNull(portDescription, PORT_DESCRIPTION_NULL);
tomeadbb462014-09-07 16:10:19 -0700350 checkValidity();
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700351
Yuta HIGUCHI13c0b872014-10-30 18:09:22 -0700352 if (!deviceClockProviderService.isTimestampAvailable(deviceId)) {
353 // Never been a master for this device
354 // any update will be ignored.
355 log.trace("Ignoring {} port update on standby node. {}", deviceId, portDescription);
356 return;
357 }
358
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700359 final DeviceEvent event = store.updatePortStatus(this.provider().id(),
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700360 deviceId, portDescription);
tomff7eb7c2014-09-08 12:49:03 -0700361 if (event != null) {
alshabibb7b40632014-09-28 21:30:00 -0700362 log.info("Device {} port {} status changed", deviceId, event
363 .port().number());
tom0efbb1d2014-09-09 11:54:28 -0700364 post(event);
tomff7eb7c2014-09-08 12:49:03 -0700365 }
tomd3097b02014-08-26 10:40:29 -0700366 }
tom3f2bbd72014-09-24 12:07:58 -0700367
368 @Override
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700369 public void receivedRoleReply(
370 DeviceId deviceId, MastershipRole requested, MastershipRole response) {
371 // Several things can happen here:
372 // 1. request and response match
373 // 2. request and response don't match
374 // 3. MastershipRole and requested match (and 1 or 2 are true)
375 // 4. MastershipRole and requested don't match (and 1 or 2 are true)
376 //
377 // 2, 4, and 3 with case 2 are failure modes.
378
tom3f2bbd72014-09-24 12:07:58 -0700379 // FIXME: implement response to this notification
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700380
381 log.info("got reply to a role request for {}: asked for {}, and got {}",
382 deviceId, requested, response);
383
384 if (requested == null && response == null) {
385 // something was off with DeviceProvider, maybe check channel too?
386 log.warn("Failed to assert role [{}] onto Device {}", requested, deviceId);
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700387 mastershipService.relinquishMastership(deviceId);
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700388 return;
Yuta HIGUCHIcf603902014-10-07 23:04:32 -0700389 }
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700390
391 if (requested.equals(response)) {
392 if (requested.equals(mastershipService.getLocalRole(deviceId))) {
393
394 return;
395 } else {
396 return;
397 // FIXME roleManager got the device to comply, but doesn't agree with
398 // the store; use the store's view, then try to reassert.
399 }
400 } else {
401 // we didn't get back what we asked for. Reelect someone else.
402 log.warn("Failed to assert role [{}] onto Device {}", response, deviceId);
403 if (response == MastershipRole.MASTER) {
404 mastershipService.relinquishMastership(deviceId);
405 // TODO: Shouldn't we be triggering event?
406 //final Device device = getDevice(deviceId);
407 //post(new DeviceEvent(DEVICE_MASTERSHIP_CHANGED, device));
408 }
409 }
410
tom3f2bbd72014-09-24 12:07:58 -0700411 }
tomd3097b02014-08-26 10:40:29 -0700412 }
tom32f66842014-08-27 19:27:47 -0700413
tomeadbb462014-09-07 16:10:19 -0700414 // Posts the specified event to the local event dispatcher.
tom32f66842014-08-27 19:27:47 -0700415 private void post(DeviceEvent event) {
416 if (event != null && eventDispatcher != null) {
417 eventDispatcher.post(event);
418 }
419 }
420
tomb41d1ac2014-09-24 01:51:24 -0700421 // Intercepts mastership events
Ayaka Koshibec4047702014-10-07 14:43:52 -0700422 private class InternalMastershipListener implements MastershipListener {
423
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700424 // Applies the specified role to the device; ignores NONE
425 /**
426 * Apply role in reaction to mastership event.
427 *
428 * @param deviceId device identifier
429 * @param newRole new role to apply to the device
430 * @return true if the request was sent to provider
431 */
432 private boolean applyRole(DeviceId deviceId, MastershipRole newRole) {
433 if (newRole.equals(MastershipRole.NONE)) {
434 //no-op
435 return true;
436 }
437
438 Device device = store.getDevice(deviceId);
439 // FIXME: Device might not be there yet. (eventual consistent)
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700440 // FIXME relinquish role
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700441 if (device == null) {
442 log.warn("{} was not there. Cannot apply role {}", deviceId, newRole);
443 return false;
444 }
445
446 DeviceProvider provider = getProvider(device.providerId());
447 if (provider == null) {
448 log.warn("Provider for {} was not found. Cannot apply role {}", deviceId, newRole);
449 return false;
450 }
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700451 provider.roleChanged(deviceId, newRole);
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700452
453 if (newRole.equals(MastershipRole.MASTER)) {
454 // only trigger event when request was sent to provider
455 // TODO: consider removing this from Device event type?
456 post(new DeviceEvent(DEVICE_MASTERSHIP_CHANGED, device));
457
458 provider.triggerProbe(device);
459 }
460 return true;
461 }
Ayaka Koshibe317245a2014-10-29 00:34:43 -0700462
tomb41d1ac2014-09-24 01:51:24 -0700463 @Override
464 public void event(MastershipEvent event) {
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700465
466 if (event.type() != MastershipEvent.Type.MASTER_CHANGED) {
467 // Don't care if backup list changed.
468 return;
469 }
470
Ayaka Koshibe4c891272014-10-08 17:14:16 -0700471 final DeviceId did = event.subject();
Ayaka Koshibeea5b4ce2014-10-11 14:17:17 -0700472 final NodeId myNodeId = clusterService.getLocalNode().id();
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700473
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700474 // myRole suggested by MastershipService
475 MastershipRole myNextRole;
Yuta HIGUCHI67dce882014-10-21 21:13:26 -0700476 if (myNodeId.equals(event.roleInfo().master())) {
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700477 // confirm latest info
Ayaka Koshibeea5b4ce2014-10-11 14:17:17 -0700478 MastershipTerm term = termService.getMastershipTerm(did);
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700479 final boolean iHaveControl = myNodeId.equals(term.master());
480 if (iHaveControl) {
Yuta HIGUCHI38b4d9d2014-10-30 11:32:41 -0700481 deviceClockProviderService.setMastershipTerm(did, term);
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700482 myNextRole = MASTER;
Yuta HIGUCHI38b4d9d2014-10-30 11:32:41 -0700483 } else {
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700484 myNextRole = STANDBY;
485 }
486 } else if (event.roleInfo().backups().contains(myNodeId)) {
487 myNextRole = STANDBY;
488 } else {
489 myNextRole = NONE;
490 }
491
492
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700493 final boolean isReachable = isReachable(did);
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700494 if (!isReachable) {
495 // device is not connected to this node
496 if (myNextRole != NONE) {
497 log.warn("Node was instructed to be {} role for {}, "
498 + "but this node cannot reach the device. "
499 + "Relinquishing role. ",
500 myNextRole, did);
501 mastershipService.relinquishMastership(did);
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700502 // FIXME disconnect?
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700503 }
504 return;
505 }
506
507 // device is connected to this node:
508
509 if (myNextRole == NONE) {
510 mastershipService.requestRoleFor(did);
511 MastershipTerm term = termService.getMastershipTerm(did);
512 if (myNodeId.equals(term.master())) {
513 myNextRole = MASTER;
514 } else {
515 myNextRole = STANDBY;
Yuta HIGUCHI38b4d9d2014-10-30 11:32:41 -0700516 }
tomb41d1ac2014-09-24 01:51:24 -0700517 }
Ayaka Koshibe317245a2014-10-29 00:34:43 -0700518
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700519 switch (myNextRole) {
520 case MASTER:
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700521 final Device device = getDevice(did);
Yuta HIGUCHId26354d2014-10-31 14:14:38 -0700522 if ((device != null) && !isAvailable(did)) {
523 //flag the device as online. Is there a better way to do this?
524 DefaultDeviceDescription deviceDescription
525 = new DefaultDeviceDescription(did.uri(),
526 device.type(),
527 device.manufacturer(),
528 device.hwVersion(),
529 device.swVersion(),
530 device.serialNumber(),
531 device.chassisId());
532 DeviceEvent devEvent =
533 store.createOrUpdateDevice(device.providerId(), did,
534 deviceDescription);
535 post(devEvent);
536 }
537 // TODO: should apply role only if there is mismatch
538 log.info("Applying role {} to {}", myNextRole, did);
539 applyRole(did, MASTER);
540 break;
541 case STANDBY:
542 log.info("Applying role {} to {}", myNextRole, did);
543 applyRole(did, STANDBY);
544 break;
545 case NONE:
546 default:
547 // should never reach here
548 log.error("You didn't see anything. I did not exist.");
549 break;
550 }
Ayaka Koshibe317245a2014-10-29 00:34:43 -0700551 }
tomb41d1ac2014-09-24 01:51:24 -0700552 }
tomf80c9722014-09-24 14:49:18 -0700553
554 // Store delegate to re-post events emitted from the store.
alshabib271a6202014-09-28 22:11:21 -0700555 private class InternalStoreDelegate
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700556 implements DeviceStoreDelegate {
tomf80c9722014-09-24 14:49:18 -0700557 @Override
558 public void notify(DeviceEvent event) {
559 post(event);
560 }
561 }
tomd3097b02014-08-26 10:40:29 -0700562}