Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 Open Networking Laboratory |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net.device.impl; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 17 | |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame^] | 18 | import com.google.common.collect.Lists; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 19 | import org.apache.felix.scr.annotations.Activate; |
| 20 | import org.apache.felix.scr.annotations.Component; |
| 21 | import org.apache.felix.scr.annotations.Deactivate; |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 22 | import org.apache.felix.scr.annotations.Reference; |
| 23 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 24 | import org.apache.felix.scr.annotations.Service; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 25 | import org.onosproject.cluster.ClusterService; |
| 26 | import org.onosproject.cluster.NodeId; |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame^] | 27 | import org.onosproject.net.provider.AbstractListenerProviderRegistry; |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 28 | import org.onosproject.core.Permission; |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 29 | import org.onosproject.incubator.net.config.NetworkConfigEvent; |
| 30 | import org.onosproject.incubator.net.config.NetworkConfigListener; |
| 31 | import org.onosproject.incubator.net.config.NetworkConfigService; |
| 32 | import org.onosproject.incubator.net.config.basics.BasicDeviceConfig; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 33 | import org.onosproject.mastership.MastershipEvent; |
| 34 | import org.onosproject.mastership.MastershipListener; |
| 35 | import org.onosproject.mastership.MastershipService; |
| 36 | import org.onosproject.mastership.MastershipTerm; |
| 37 | import org.onosproject.mastership.MastershipTermService; |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 38 | import org.onosproject.net.DefaultAnnotations; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 39 | import org.onosproject.net.Device; |
samuel | 738dfaf | 2015-07-11 11:08:57 +0800 | [diff] [blame] | 40 | import org.onosproject.net.Device.Type; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 41 | import org.onosproject.net.DeviceId; |
| 42 | import org.onosproject.net.MastershipRole; |
| 43 | import org.onosproject.net.Port; |
| 44 | import org.onosproject.net.PortNumber; |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 45 | import org.onosproject.net.SparseAnnotations; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 46 | import org.onosproject.net.device.DefaultDeviceDescription; |
| 47 | import org.onosproject.net.device.DefaultPortDescription; |
| 48 | import org.onosproject.net.device.DeviceAdminService; |
| 49 | import org.onosproject.net.device.DeviceClockProviderService; |
| 50 | import org.onosproject.net.device.DeviceDescription; |
| 51 | import org.onosproject.net.device.DeviceEvent; |
| 52 | import org.onosproject.net.device.DeviceListener; |
| 53 | import org.onosproject.net.device.DeviceProvider; |
| 54 | import org.onosproject.net.device.DeviceProviderRegistry; |
| 55 | import org.onosproject.net.device.DeviceProviderService; |
| 56 | import org.onosproject.net.device.DeviceService; |
| 57 | import org.onosproject.net.device.DeviceStore; |
| 58 | import org.onosproject.net.device.DeviceStoreDelegate; |
| 59 | import org.onosproject.net.device.PortDescription; |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 60 | import org.onosproject.net.device.PortStatistics; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 61 | import org.onosproject.net.provider.AbstractProviderService; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 62 | import org.slf4j.Logger; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 63 | |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame^] | 64 | import java.util.Collection; |
| 65 | import java.util.HashSet; |
| 66 | import java.util.List; |
| 67 | import java.util.Objects; |
| 68 | import java.util.Set; |
| 69 | import java.util.concurrent.CompletableFuture; |
| 70 | import java.util.concurrent.ExecutionException; |
| 71 | import java.util.concurrent.ScheduledExecutorService; |
| 72 | import java.util.concurrent.TimeUnit; |
| 73 | |
| 74 | import static com.google.common.base.Preconditions.checkNotNull; |
| 75 | import static com.google.common.base.Preconditions.checkState; |
| 76 | import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor; |
| 77 | import static org.onlab.util.Tools.groupedThreads; |
| 78 | import static org.onosproject.net.MastershipRole.*; |
| 79 | import static org.onosproject.security.AppGuard.checkPermission; |
| 80 | import static org.slf4j.LoggerFactory.getLogger; |
Jonathan Hart | 2f66936 | 2015-02-11 16:19:20 -0800 | [diff] [blame] | 81 | |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 82 | |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 83 | /** |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 84 | * Provides implementation of the device SB & NB APIs. |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 85 | */ |
| 86 | @Component(immediate = true) |
| 87 | @Service |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 88 | public class DeviceManager |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame^] | 89 | extends AbstractListenerProviderRegistry<DeviceEvent, DeviceListener, DeviceProvider, DeviceProviderService> |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 90 | implements DeviceService, DeviceAdminService, DeviceProviderRegistry { |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 91 | |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 92 | private static final String DEVICE_ID_NULL = "Device ID cannot be null"; |
| 93 | private static final String PORT_NUMBER_NULL = "Port number cannot be null"; |
| 94 | private static final String DEVICE_DESCRIPTION_NULL = "Device description cannot be null"; |
| 95 | private static final String PORT_DESCRIPTION_NULL = "Port description cannot be null"; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 96 | |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 97 | private final Logger log = getLogger(getClass()); |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 98 | |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 99 | private final DeviceStoreDelegate delegate = new InternalStoreDelegate(); |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame] | 100 | |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 101 | private final MastershipListener mastershipListener = new InternalMastershipListener(); |
Madan Jampani | de003d9 | 2015-05-11 17:14:20 -0700 | [diff] [blame] | 102 | private NodeId localNodeId; |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 103 | |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 104 | private ScheduledExecutorService backgroundService; |
| 105 | |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 106 | private final NetworkConfigListener networkConfigListener = new InternalNetworkConfigListener(); |
| 107 | |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 108 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 109 | protected DeviceStore store; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 110 | |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 111 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 112 | protected ClusterService clusterService; |
| 113 | |
| 114 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Ayaka Koshibe | a7f044e | 2014-09-23 16:56:20 -0700 | [diff] [blame] | 115 | protected MastershipService mastershipService; |
| 116 | |
Yuta HIGUCHI | bcac499 | 2014-11-22 19:27:57 -0800 | [diff] [blame] | 117 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Ayaka Koshibe | 3de43ca | 2014-09-26 16:40:23 -0700 | [diff] [blame] | 118 | protected MastershipTermService termService; |
| 119 | |
Madan Jampani | 61056bc | 2014-09-27 09:07:26 -0700 | [diff] [blame] | 120 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Yuta HIGUCHI | 093e83e | 2014-10-10 22:26:11 -0700 | [diff] [blame] | 121 | protected DeviceClockProviderService deviceClockProviderService; |
Madan Jampani | 61056bc | 2014-09-27 09:07:26 -0700 | [diff] [blame] | 122 | |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 123 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 124 | protected NetworkConfigService networkConfigService; |
| 125 | |
| 126 | |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 127 | @Activate |
| 128 | public void activate() { |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 129 | backgroundService = newSingleThreadScheduledExecutor(groupedThreads("onos/device", "manager-background")); |
Madan Jampani | de003d9 | 2015-05-11 17:14:20 -0700 | [diff] [blame] | 130 | localNodeId = clusterService.getLocalNode().id(); |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 131 | |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame] | 132 | store.setDelegate(delegate); |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 133 | eventDispatcher.addSink(DeviceEvent.class, listenerRegistry); |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 134 | mastershipService.addListener(mastershipListener); |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 135 | networkConfigService.addListener(networkConfigListener); |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 136 | |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame^] | 137 | backgroundService.scheduleWithFixedDelay(() -> { |
| 138 | try { |
| 139 | mastershipCheck(); |
| 140 | } catch (Exception e) { |
| 141 | log.error("Exception thrown during integrity check", e); |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 142 | } |
| 143 | }, 1, 1, TimeUnit.MINUTES); |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 144 | log.info("Started"); |
| 145 | } |
| 146 | |
| 147 | @Deactivate |
| 148 | public void deactivate() { |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 149 | backgroundService.shutdown(); |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 150 | networkConfigService.removeListener(networkConfigListener); |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame] | 151 | store.unsetDelegate(delegate); |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 152 | mastershipService.removeListener(mastershipListener); |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 153 | eventDispatcher.removeSink(DeviceEvent.class); |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 154 | log.info("Stopped"); |
| 155 | } |
| 156 | |
| 157 | @Override |
tom | ad2d209 | 2014-09-06 23:24:20 -0700 | [diff] [blame] | 158 | public int getDeviceCount() { |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 159 | checkPermission(Permission.DEVICE_READ); |
tom | ad2d209 | 2014-09-06 23:24:20 -0700 | [diff] [blame] | 160 | return store.getDeviceCount(); |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | @Override |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 164 | public Iterable<Device> getDevices() { |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 165 | checkPermission(Permission.DEVICE_READ); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 166 | return store.getDevices(); |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 167 | } |
| 168 | |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 169 | @Override |
Yuta HIGUCHI | f1f2ac0 | 2014-11-26 14:02:22 -0800 | [diff] [blame] | 170 | public Iterable<Device> getAvailableDevices() { |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 171 | checkPermission(Permission.DEVICE_READ); |
Yuta HIGUCHI | f1f2ac0 | 2014-11-26 14:02:22 -0800 | [diff] [blame] | 172 | return store.getAvailableDevices(); |
| 173 | } |
| 174 | |
| 175 | @Override |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 176 | public Device getDevice(DeviceId deviceId) { |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 177 | checkPermission(Permission.DEVICE_READ); |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 178 | checkNotNull(deviceId, DEVICE_ID_NULL); |
tom | 132b58a | 2014-08-28 16:11:28 -0700 | [diff] [blame] | 179 | return store.getDevice(deviceId); |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | @Override |
tom | ad2d209 | 2014-09-06 23:24:20 -0700 | [diff] [blame] | 183 | public MastershipRole getRole(DeviceId deviceId) { |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 184 | checkPermission(Permission.DEVICE_READ); |
tom | ad2d209 | 2014-09-06 23:24:20 -0700 | [diff] [blame] | 185 | checkNotNull(deviceId, DEVICE_ID_NULL); |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 186 | return mastershipService.getLocalRole(deviceId); |
tom | ad2d209 | 2014-09-06 23:24:20 -0700 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | @Override |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 190 | public List<Port> getPorts(DeviceId deviceId) { |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 191 | checkPermission(Permission.DEVICE_READ); |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 192 | checkNotNull(deviceId, DEVICE_ID_NULL); |
tom | 132b58a | 2014-08-28 16:11:28 -0700 | [diff] [blame] | 193 | return store.getPorts(deviceId); |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | @Override |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 197 | public List<PortStatistics> getPortStatistics(DeviceId deviceId) { |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 198 | checkPermission(Permission.DEVICE_READ); |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 199 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 200 | return store.getPortStatistics(deviceId); |
| 201 | } |
| 202 | |
| 203 | @Override |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 204 | public Port getPort(DeviceId deviceId, PortNumber portNumber) { |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 205 | checkPermission(Permission.DEVICE_READ); |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 206 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 207 | checkNotNull(portNumber, PORT_NUMBER_NULL); |
tom | 132b58a | 2014-08-28 16:11:28 -0700 | [diff] [blame] | 208 | return store.getPort(deviceId, portNumber); |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | @Override |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 212 | public boolean isAvailable(DeviceId deviceId) { |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 213 | checkPermission(Permission.DEVICE_READ); |
| 214 | |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 215 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 216 | return store.isAvailable(deviceId); |
| 217 | } |
| 218 | |
Ayaka Koshibe | e60d452 | 2014-10-28 15:07:00 -0700 | [diff] [blame] | 219 | // Check a device for control channel connectivity. |
Yuta HIGUCHI | 5481532 | 2014-10-31 23:17:08 -0700 | [diff] [blame] | 220 | private boolean isReachable(DeviceId deviceId) { |
Ayaka Koshibe | 78bcbc1 | 2014-11-19 14:28:58 -0800 | [diff] [blame] | 221 | if (deviceId == null) { |
| 222 | return false; |
| 223 | } |
Yuta HIGUCHI | 5481532 | 2014-10-31 23:17:08 -0700 | [diff] [blame] | 224 | DeviceProvider provider = getProvider(deviceId); |
| 225 | if (provider != null) { |
| 226 | return provider.isReachable(deviceId); |
| 227 | } else { |
Yuta HIGUCHI | 72669c4 | 2014-11-13 14:48:17 -0800 | [diff] [blame] | 228 | log.debug("Provider not found for {}", deviceId); |
Ayaka Koshibe | e60d452 | 2014-10-28 15:07:00 -0700 | [diff] [blame] | 229 | return false; |
| 230 | } |
Ayaka Koshibe | e8708e3 | 2014-10-22 13:40:18 -0700 | [diff] [blame] | 231 | } |
| 232 | |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 233 | @Override |
| 234 | public void removeDevice(DeviceId deviceId) { |
| 235 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 236 | DeviceEvent event = store.removeDevice(deviceId); |
tom | 0efbb1d | 2014-09-09 11:54:28 -0700 | [diff] [blame] | 237 | if (event != null) { |
| 238 | log.info("Device {} administratively removed", deviceId); |
| 239 | post(event); |
| 240 | } |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 241 | } |
| 242 | |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 243 | @Override |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 244 | protected DeviceProviderService createProviderService( |
| 245 | DeviceProvider provider) { |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 246 | return new InternalDeviceProviderService(provider); |
| 247 | } |
| 248 | |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 249 | /** |
| 250 | * Checks if all the reachable devices have a valid mastership role. |
| 251 | */ |
| 252 | private void mastershipCheck() { |
| 253 | log.debug("Checking mastership"); |
| 254 | for (Device device : getDevices()) { |
| 255 | final DeviceId deviceId = device.id(); |
Jonathan Hart | 2f66936 | 2015-02-11 16:19:20 -0800 | [diff] [blame] | 256 | log.trace("Checking device {}", deviceId); |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 257 | |
| 258 | if (!isReachable(deviceId)) { |
| 259 | continue; |
| 260 | } |
| 261 | |
| 262 | if (mastershipService.getLocalRole(deviceId) != NONE) { |
| 263 | continue; |
| 264 | } |
| 265 | |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 266 | log.info("{} is reachable but did not have a valid role, reasserting", |
| 267 | deviceId); |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 268 | |
| 269 | // isReachable but was not MASTER or STANDBY, get a role and apply |
| 270 | // Note: NONE triggers request to MastershipService |
| 271 | reassertRole(deviceId, NONE); |
| 272 | } |
| 273 | } |
| 274 | |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 275 | // Personalized device provider service issued to the supplied provider. |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 276 | private class InternalDeviceProviderService |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 277 | extends AbstractProviderService<DeviceProvider> |
| 278 | implements DeviceProviderService { |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 279 | |
tom | cfde062 | 2014-09-09 11:02:42 -0700 | [diff] [blame] | 280 | InternalDeviceProviderService(DeviceProvider provider) { |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 281 | super(provider); |
| 282 | } |
| 283 | |
Yuta HIGUCHI | d26354d | 2014-10-31 14:14:38 -0700 | [diff] [blame] | 284 | /** |
| 285 | * Apply role in reaction to provider event. |
| 286 | * |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame^] | 287 | * @param deviceId device identifier |
| 288 | * @param newRole new role to apply to the device |
Yuta HIGUCHI | d26354d | 2014-10-31 14:14:38 -0700 | [diff] [blame] | 289 | * @return true if the request was sent to provider |
| 290 | */ |
| 291 | private boolean applyRole(DeviceId deviceId, MastershipRole newRole) { |
| 292 | |
| 293 | if (newRole.equals(MastershipRole.NONE)) { |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 294 | //no-op |
Yuta HIGUCHI | d26354d | 2014-10-31 14:14:38 -0700 | [diff] [blame] | 295 | return true; |
| 296 | } |
| 297 | |
| 298 | DeviceProvider provider = provider(); |
| 299 | if (provider == null) { |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 300 | log.warn("Provider for {} was not found. Cannot apply role {}", |
| 301 | deviceId, newRole); |
Yuta HIGUCHI | d26354d | 2014-10-31 14:14:38 -0700 | [diff] [blame] | 302 | return false; |
| 303 | } |
Yuta HIGUCHI | 5481532 | 2014-10-31 23:17:08 -0700 | [diff] [blame] | 304 | provider.roleChanged(deviceId, newRole); |
| 305 | // not triggering probe when triggered by provider service event |
Yuta HIGUCHI | d26354d | 2014-10-31 14:14:38 -0700 | [diff] [blame] | 306 | |
Yuta HIGUCHI | d26354d | 2014-10-31 14:14:38 -0700 | [diff] [blame] | 307 | return true; |
| 308 | } |
| 309 | |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 310 | |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 311 | @Override |
alshabib | b7b4063 | 2014-09-28 21:30:00 -0700 | [diff] [blame] | 312 | public void deviceConnected(DeviceId deviceId, |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 313 | DeviceDescription deviceDescription) { |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 314 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 315 | checkNotNull(deviceDescription, DEVICE_DESCRIPTION_NULL); |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 316 | checkValidity(); |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 317 | deviceDescription = validateDevice(deviceDescription, deviceId); |
Yuta HIGUCHI | 24b2e2a | 2014-10-07 15:53:57 -0700 | [diff] [blame] | 318 | |
Yuta HIGUCHI | d26354d | 2014-10-31 14:14:38 -0700 | [diff] [blame] | 319 | // check my Role |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 320 | CompletableFuture<MastershipRole> role = mastershipService.requestRoleFor(deviceId); |
HIGUCHI Yuta | 11530fb | 2015-05-27 13:10:20 -0700 | [diff] [blame] | 321 | try { |
| 322 | // Device subsystem must wait for role assignment |
| 323 | // to avoid losing Device information. |
| 324 | // (This node could be the only Node connected to the Device.) |
| 325 | role.get(); |
| 326 | } catch (InterruptedException e) { |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 327 | log.warn("Interrupted while waiting role-assignment for {}", deviceId); |
HIGUCHI Yuta | 11530fb | 2015-05-27 13:10:20 -0700 | [diff] [blame] | 328 | Thread.currentThread().interrupt(); |
| 329 | } catch (ExecutionException e) { |
| 330 | log.error("Exception thrown while waiting role-assignment for {}", |
| 331 | deviceId, e); |
| 332 | } |
| 333 | |
Yuta HIGUCHI | d26354d | 2014-10-31 14:14:38 -0700 | [diff] [blame] | 334 | final MastershipTerm term = termService.getMastershipTerm(deviceId); |
Madan Jampani | de003d9 | 2015-05-11 17:14:20 -0700 | [diff] [blame] | 335 | if (term == null || !localNodeId.equals(term.master())) { |
Yuta HIGUCHI | 2d3cd31 | 2014-10-31 11:38:04 -0700 | [diff] [blame] | 336 | log.info("Role of this node is STANDBY for {}", deviceId); |
Yuta HIGUCHI | d26354d | 2014-10-31 14:14:38 -0700 | [diff] [blame] | 337 | applyRole(deviceId, MastershipRole.STANDBY); |
Marc De Leenheer | b473b9d | 2015-02-06 15:21:03 -0800 | [diff] [blame] | 338 | } else { |
| 339 | log.info("Role of this node is MASTER for {}", deviceId); |
| 340 | // tell clock provider if this instance is the master |
| 341 | deviceClockProviderService.setMastershipTerm(deviceId, term); |
| 342 | applyRole(deviceId, MastershipRole.MASTER); |
Yuta HIGUCHI | 24b2e2a | 2014-10-07 15:53:57 -0700 | [diff] [blame] | 343 | } |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 344 | DeviceEvent event = store.createOrUpdateDevice(provider().id(), deviceId, |
| 345 | deviceDescription); |
tom | 80c0e5e | 2014-09-08 18:08:58 -0700 | [diff] [blame] | 346 | if (event != null) { |
Yuta HIGUCHI | d26354d | 2014-10-31 14:14:38 -0700 | [diff] [blame] | 347 | log.trace("event: {} {}", event.type(), event); |
tom | 568581d | 2014-09-08 20:13:36 -0700 | [diff] [blame] | 348 | post(event); |
tom | 80c0e5e | 2014-09-08 18:08:58 -0700 | [diff] [blame] | 349 | } |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 350 | } |
| 351 | |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 352 | // returns a DeviceDescription made from the union of the BasicDeviceConfig |
| 353 | // annotations if it exists |
| 354 | private DeviceDescription validateDevice(DeviceDescription deviceDescription, DeviceId deviceId) { |
| 355 | BasicDeviceConfig cfg = networkConfigService.getConfig(deviceId, BasicDeviceConfig.class); |
| 356 | checkState(cfg == null || cfg.isAllowed(), "Device " + deviceId + " is not allowed"); |
| 357 | log.info("Device {} connected", deviceId); |
| 358 | if (cfg != null) { |
| 359 | SparseAnnotations finalSparse = processAnnotations(cfg, deviceDescription, deviceId); |
| 360 | if (cfg.type() != Type.SWITCH) { |
| 361 | deviceDescription = new DefaultDeviceDescription(deviceDescription, |
| 362 | cfg.type(), finalSparse); |
| 363 | } else { |
| 364 | deviceDescription = new DefaultDeviceDescription(deviceDescription, |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame^] | 365 | deviceDescription.type(), finalSparse); |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 366 | } |
| 367 | } |
| 368 | return deviceDescription; |
| 369 | } |
| 370 | |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 371 | @Override |
| 372 | public void deviceDisconnected(DeviceId deviceId) { |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 373 | checkNotNull(deviceId, DEVICE_ID_NULL); |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 374 | checkValidity(); |
Yuta HIGUCHI | cf60390 | 2014-10-07 23:04:32 -0700 | [diff] [blame] | 375 | |
Yuta HIGUCHI | 2d3cd31 | 2014-10-31 11:38:04 -0700 | [diff] [blame] | 376 | log.info("Device {} disconnected from this node", deviceId); |
Ayaka Koshibe | d9f693e | 2014-09-29 18:04:54 -0700 | [diff] [blame] | 377 | |
alshabib | afc514a | 2014-12-01 14:44:05 -0800 | [diff] [blame] | 378 | List<Port> ports = store.getPorts(deviceId); |
| 379 | List<PortDescription> descs = Lists.newArrayList(); |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 380 | ports.forEach(port -> |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame^] | 381 | descs.add(new DefaultPortDescription(port.number(), |
| 382 | false, port.type(), |
| 383 | port.portSpeed()))); |
alshabib | afc514a | 2014-12-01 14:44:05 -0800 | [diff] [blame] | 384 | store.updatePorts(this.provider().id(), deviceId, descs); |
Ayaka Koshibe | b5c63a0 | 2014-10-18 18:42:27 -0700 | [diff] [blame] | 385 | try { |
Thomas Vachuska | 5f429d6 | 2015-05-28 15:34:36 -0700 | [diff] [blame] | 386 | if (mastershipService.getLocalRole(deviceId) == MASTER) { |
| 387 | post(store.markOffline(deviceId)); |
| 388 | } |
Ayaka Koshibe | b5c63a0 | 2014-10-18 18:42:27 -0700 | [diff] [blame] | 389 | } catch (IllegalStateException e) { |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame] | 390 | log.warn("Failed to mark {} offline", deviceId); |
| 391 | // only the MASTER should be marking off-line in normal cases, |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 392 | // but if I was the last STANDBY connection, etc. and no one else |
| 393 | // was there to mark the device offline, this instance may need to |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame] | 394 | // temporarily request for Master Role and mark offline. |
| 395 | |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 396 | //there are times when this node will correctly have mastership, BUT |
| 397 | //that isn't reflected in the ClockManager before the device disconnects. |
| 398 | //we want to let go of the device anyways, so make sure this happens. |
Yuta HIGUCHI | 0722fb2 | 2014-10-19 01:16:33 -0700 | [diff] [blame] | 399 | |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame] | 400 | // FIXME: Store semantics leaking out as IllegalStateException. |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 401 | // Consider revising store API to handle this scenario. |
| 402 | CompletableFuture<MastershipRole> roleFuture = mastershipService.requestRoleFor(deviceId); |
Madan Jampani | de003d9 | 2015-05-11 17:14:20 -0700 | [diff] [blame] | 403 | roleFuture.whenComplete((role, error) -> { |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 404 | MastershipTerm term = termService.getMastershipTerm(deviceId); |
| 405 | // TODO: Move this type of check inside device clock manager, etc. |
| 406 | if (term != null && localNodeId.equals(term.master())) { |
| 407 | log.info("Retry marking {} offline", deviceId); |
| 408 | deviceClockProviderService.setMastershipTerm(deviceId, term); |
| 409 | post(store.markOffline(deviceId)); |
| 410 | } else { |
| 411 | log.info("Failed again marking {} offline. {}", deviceId, role); |
| 412 | } |
| 413 | }); |
Ayaka Koshibe | b5c63a0 | 2014-10-18 18:42:27 -0700 | [diff] [blame] | 414 | } finally { |
Madan Jampani | c6e574f | 2015-05-29 13:41:52 -0700 | [diff] [blame] | 415 | try { |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 416 | //relinquish master role and ability to be backup. |
Madan Jampani | c6e574f | 2015-05-29 13:41:52 -0700 | [diff] [blame] | 417 | mastershipService.relinquishMastership(deviceId).get(); |
| 418 | } catch (InterruptedException e) { |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 419 | log.warn("Interrupted while reliquishing role for {}", deviceId); |
Madan Jampani | c6e574f | 2015-05-29 13:41:52 -0700 | [diff] [blame] | 420 | Thread.currentThread().interrupt(); |
| 421 | } catch (ExecutionException e) { |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 422 | log.error("Exception thrown while relinquishing role for {}", deviceId, e); |
Madan Jampani | c6e574f | 2015-05-29 13:41:52 -0700 | [diff] [blame] | 423 | } |
tom | 0efbb1d | 2014-09-09 11:54:28 -0700 | [diff] [blame] | 424 | } |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | @Override |
alshabib | b7b4063 | 2014-09-28 21:30:00 -0700 | [diff] [blame] | 428 | public void updatePorts(DeviceId deviceId, |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 429 | List<PortDescription> portDescriptions) { |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 430 | checkNotNull(deviceId, DEVICE_ID_NULL); |
alshabib | b7b4063 | 2014-09-28 21:30:00 -0700 | [diff] [blame] | 431 | checkNotNull(portDescriptions, |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame^] | 432 | "Port descriptions list cannot be null"); |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 433 | checkValidity(); |
Yuta HIGUCHI | 13c0b87 | 2014-10-30 18:09:22 -0700 | [diff] [blame] | 434 | if (!deviceClockProviderService.isTimestampAvailable(deviceId)) { |
| 435 | // Never been a master for this device |
| 436 | // any update will be ignored. |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 437 | log.trace("Ignoring {} port updates on standby node. {}", deviceId, portDescriptions); |
Yuta HIGUCHI | 13c0b87 | 2014-10-30 18:09:22 -0700 | [diff] [blame] | 438 | return; |
| 439 | } |
| 440 | |
Yuta HIGUCHI | 5f6739c | 2014-10-01 14:04:01 -0700 | [diff] [blame] | 441 | List<DeviceEvent> events = store.updatePorts(this.provider().id(), |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 442 | deviceId, portDescriptions); |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 443 | for (DeviceEvent event : events) { |
| 444 | post(event); |
| 445 | } |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | @Override |
alshabib | b7b4063 | 2014-09-28 21:30:00 -0700 | [diff] [blame] | 449 | public void portStatusChanged(DeviceId deviceId, |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 450 | PortDescription portDescription) { |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 451 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 452 | checkNotNull(portDescription, PORT_DESCRIPTION_NULL); |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 453 | checkValidity(); |
Ayaka Koshibe | b5c63a0 | 2014-10-18 18:42:27 -0700 | [diff] [blame] | 454 | |
Yuta HIGUCHI | 13c0b87 | 2014-10-30 18:09:22 -0700 | [diff] [blame] | 455 | if (!deviceClockProviderService.isTimestampAvailable(deviceId)) { |
| 456 | // Never been a master for this device |
| 457 | // any update will be ignored. |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 458 | log.trace("Ignoring {} port update on standby node. {}", deviceId, |
| 459 | portDescription); |
Yuta HIGUCHI | 13c0b87 | 2014-10-30 18:09:22 -0700 | [diff] [blame] | 460 | return; |
| 461 | } |
| 462 | |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 463 | final DeviceEvent event = store.updatePortStatus(this.provider().id(), |
| 464 | deviceId, portDescription); |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 465 | if (event != null) { |
alshabib | b7b4063 | 2014-09-28 21:30:00 -0700 | [diff] [blame] | 466 | log.info("Device {} port {} status changed", deviceId, event |
| 467 | .port().number()); |
tom | 0efbb1d | 2014-09-09 11:54:28 -0700 | [diff] [blame] | 468 | post(event); |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 469 | } |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 470 | } |
tom | 3f2bbd7 | 2014-09-24 12:07:58 -0700 | [diff] [blame] | 471 | |
| 472 | @Override |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 473 | public void receivedRoleReply(DeviceId deviceId, MastershipRole requested, |
Thomas Vachuska | b17c41f | 2015-05-19 11:16:05 -0700 | [diff] [blame] | 474 | MastershipRole response) { |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 475 | // Several things can happen here: |
| 476 | // 1. request and response match |
| 477 | // 2. request and response don't match |
| 478 | // 3. MastershipRole and requested match (and 1 or 2 are true) |
| 479 | // 4. MastershipRole and requested don't match (and 1 or 2 are true) |
| 480 | // |
| 481 | // 2, 4, and 3 with case 2 are failure modes. |
| 482 | |
tom | 3f2bbd7 | 2014-09-24 12:07:58 -0700 | [diff] [blame] | 483 | // FIXME: implement response to this notification |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 484 | |
Madan Jampani | f2af771 | 2015-05-29 18:43:52 -0700 | [diff] [blame] | 485 | log.debug("got reply to a role request for {}: asked for {}, and got {}", |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame^] | 486 | deviceId, requested, response); |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 487 | |
| 488 | if (requested == null && response == null) { |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 489 | // something was off with DeviceProvider, maybe check channel too? |
| 490 | log.warn("Failed to assert role [{}] onto Device {}", requested, deviceId); |
Yuta HIGUCHI | cf60390 | 2014-10-07 23:04:32 -0700 | [diff] [blame] | 491 | mastershipService.relinquishMastership(deviceId); |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 492 | return; |
Yuta HIGUCHI | cf60390 | 2014-10-07 23:04:32 -0700 | [diff] [blame] | 493 | } |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 494 | |
Thomas Vachuska | b17c41f | 2015-05-19 11:16:05 -0700 | [diff] [blame] | 495 | if (Objects.equals(requested, response)) { |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 496 | if (Objects.equals(requested, mastershipService.getLocalRole(deviceId))) { |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 497 | return; |
| 498 | } else { |
| 499 | return; |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 500 | // FIXME roleManager got the device to comply, but doesn't agree with |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 501 | // the store; use the store's view, then try to reassert. |
| 502 | } |
| 503 | } else { |
| 504 | // we didn't get back what we asked for. Reelect someone else. |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 505 | log.warn("Failed to assert role [{}] onto Device {}", response, deviceId); |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 506 | if (response == MastershipRole.MASTER) { |
| 507 | mastershipService.relinquishMastership(deviceId); |
| 508 | // TODO: Shouldn't we be triggering event? |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 509 | //final Device device = getDevice(deviceId); |
| 510 | //post(new DeviceEvent(DEVICE_MASTERSHIP_CHANGED, device)); |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 511 | } |
| 512 | } |
tom | 3f2bbd7 | 2014-09-24 12:07:58 -0700 | [diff] [blame] | 513 | } |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 514 | |
| 515 | @Override |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 516 | public void updatePortStatistics(DeviceId deviceId, Collection<PortStatistics> portStatistics) { |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 517 | checkNotNull(deviceId, DEVICE_ID_NULL); |
Thomas Vachuska | b17c41f | 2015-05-19 11:16:05 -0700 | [diff] [blame] | 518 | checkNotNull(portStatistics, "Port statistics list cannot be null"); |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 519 | checkValidity(); |
| 520 | |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 521 | DeviceEvent event = store.updatePortStatistics(this.provider().id(), |
| 522 | deviceId, portStatistics); |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 523 | post(event); |
| 524 | } |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 525 | |
| 526 | // supplements or replaces deviceDescription annotations with |
| 527 | // BasicDeviceConfig annotations |
| 528 | private SparseAnnotations processAnnotations(BasicDeviceConfig cfg, DeviceDescription deviceDescription, |
| 529 | DeviceId deviceId) { |
| 530 | SparseAnnotations originalAnnotations = deviceDescription.annotations(); |
| 531 | DefaultAnnotations.Builder newBuilder = DefaultAnnotations.builder(); |
| 532 | if (cfg.driver() != deviceId.toString()) { |
| 533 | newBuilder.set(cfg.DRIVER, cfg.driver()); |
| 534 | } |
| 535 | if (cfg.type() != Type.SWITCH) { |
| 536 | newBuilder.set(cfg.TYPE, cfg.type().toString()); |
| 537 | } |
| 538 | if (cfg.name() != null) { |
| 539 | newBuilder.set(cfg.NAME, cfg.name()); |
| 540 | } |
| 541 | if (cfg.latitude() != -1) { |
| 542 | newBuilder.set(cfg.LATITUDE, Double.toString(cfg.latitude())); |
| 543 | } |
| 544 | if (cfg.longitude() != -1) { |
| 545 | newBuilder.set(cfg.LONGITUDE, Double.toString(cfg.longitude())); |
| 546 | } |
| 547 | if (cfg.rackAddress() != null) { |
| 548 | newBuilder.set(cfg.RACK_ADDRESS, cfg.rackAddress()); |
| 549 | } |
| 550 | if (cfg.owner() != null) { |
| 551 | newBuilder.set(cfg.OWNER, cfg.owner()); |
| 552 | } |
| 553 | DefaultAnnotations newAnnotations = newBuilder.build(); |
| 554 | return DefaultAnnotations.union(originalAnnotations, newAnnotations); |
| 555 | } |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 556 | } |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 557 | |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 558 | // Applies the specified role to the device; ignores NONE |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame^] | 559 | |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 560 | /** |
| 561 | * Apply role to device and send probe if MASTER. |
| 562 | * |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame^] | 563 | * @param deviceId device identifier |
| 564 | * @param newRole new role to apply to the device |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 565 | * @return true if the request was sent to provider |
| 566 | */ |
| 567 | private boolean applyRoleAndProbe(DeviceId deviceId, MastershipRole newRole) { |
| 568 | if (newRole.equals(MastershipRole.NONE)) { |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 569 | //no-op |
Yuta HIGUCHI | d26354d | 2014-10-31 14:14:38 -0700 | [diff] [blame] | 570 | return true; |
| 571 | } |
Ayaka Koshibe | 317245a | 2014-10-29 00:34:43 -0700 | [diff] [blame] | 572 | |
Ayaka Koshibe | 78bcbc1 | 2014-11-19 14:28:58 -0800 | [diff] [blame] | 573 | DeviceProvider provider = getProvider(deviceId); |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 574 | if (provider == null) { |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 575 | log.warn("Provider for {} was not found. Cannot apply role {}", deviceId, newRole); |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 576 | return false; |
| 577 | } |
| 578 | provider.roleChanged(deviceId, newRole); |
| 579 | |
| 580 | if (newRole.equals(MastershipRole.MASTER)) { |
| 581 | // only trigger event when request was sent to provider |
Ayaka Koshibe | 78bcbc1 | 2014-11-19 14:28:58 -0800 | [diff] [blame] | 582 | provider.triggerProbe(deviceId); |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 583 | } |
| 584 | return true; |
| 585 | } |
| 586 | |
| 587 | /** |
| 588 | * Reaasert role for specified device connected to this node. |
| 589 | * |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame^] | 590 | * @param did device identifier |
| 591 | * @param nextRole role to apply. If NONE is specified, |
| 592 | * it will ask mastership service for a role and apply it. |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 593 | */ |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 594 | private void reassertRole(final DeviceId did, |
| 595 | final MastershipRole nextRole) { |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 596 | |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 597 | MastershipRole myNextRole = nextRole; |
| 598 | if (myNextRole == NONE) { |
| 599 | mastershipService.requestRoleFor(did); |
| 600 | MastershipTerm term = termService.getMastershipTerm(did); |
Madan Jampani | de003d9 | 2015-05-11 17:14:20 -0700 | [diff] [blame] | 601 | if (term != null && localNodeId.equals(term.master())) { |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 602 | myNextRole = MASTER; |
| 603 | } else { |
| 604 | myNextRole = STANDBY; |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | switch (myNextRole) { |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame^] | 609 | case MASTER: |
| 610 | final Device device = getDevice(did); |
| 611 | if ((device != null) && !isAvailable(did)) { |
| 612 | //flag the device as online. Is there a better way to do this? |
| 613 | DefaultDeviceDescription deviceDescription |
| 614 | = new DefaultDeviceDescription(did.uri(), |
| 615 | device.type(), |
| 616 | device.manufacturer(), |
| 617 | device.hwVersion(), |
| 618 | device.swVersion(), |
| 619 | device.serialNumber(), |
| 620 | device.chassisId()); |
| 621 | DeviceEvent devEvent = |
| 622 | store.createOrUpdateDevice(device.providerId(), did, |
| 623 | deviceDescription); |
| 624 | post(devEvent); |
| 625 | } |
| 626 | // TODO: should apply role only if there is mismatch |
| 627 | log.debug("Applying role {} to {}", myNextRole, did); |
| 628 | if (!applyRoleAndProbe(did, MASTER)) { |
| 629 | log.warn("Unsuccessful applying role {} to {}", myNextRole, did); |
| 630 | // immediately failed to apply role |
| 631 | mastershipService.relinquishMastership(did); |
| 632 | // FIXME disconnect? |
| 633 | } |
| 634 | break; |
| 635 | case STANDBY: |
| 636 | log.debug("Applying role {} to {}", myNextRole, did); |
| 637 | if (!applyRoleAndProbe(did, STANDBY)) { |
| 638 | log.warn("Unsuccessful applying role {} to {}", myNextRole, did); |
| 639 | // immediately failed to apply role |
| 640 | mastershipService.relinquishMastership(did); |
| 641 | // FIXME disconnect? |
| 642 | } |
| 643 | break; |
| 644 | case NONE: |
| 645 | default: |
| 646 | // should never reach here |
| 647 | log.error("You didn't see anything. I did not exist."); |
| 648 | break; |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 649 | } |
| 650 | } |
| 651 | |
Madan Jampani | 328371d | 2015-05-29 14:06:27 -0700 | [diff] [blame] | 652 | private void handleMastershipEvent(MastershipEvent event) { |
| 653 | if (event.type() != MastershipEvent.Type.MASTER_CHANGED) { |
| 654 | // Don't care if backup list changed. |
| 655 | return; |
| 656 | } |
| 657 | |
| 658 | final DeviceId did = event.subject(); |
| 659 | |
| 660 | // myRole suggested by MastershipService |
| 661 | MastershipRole myNextRole; |
| 662 | if (localNodeId.equals(event.roleInfo().master())) { |
| 663 | // confirm latest info |
| 664 | MastershipTerm term = termService.getMastershipTerm(did); |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 665 | final boolean iHaveControl = term != null && localNodeId.equals(term.master()); |
Madan Jampani | 328371d | 2015-05-29 14:06:27 -0700 | [diff] [blame] | 666 | if (iHaveControl) { |
| 667 | deviceClockProviderService.setMastershipTerm(did, term); |
| 668 | myNextRole = MASTER; |
| 669 | } else { |
| 670 | myNextRole = STANDBY; |
| 671 | } |
| 672 | } else if (event.roleInfo().backups().contains(localNodeId)) { |
| 673 | myNextRole = STANDBY; |
| 674 | } else { |
| 675 | myNextRole = NONE; |
| 676 | } |
| 677 | |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 678 | |
Madan Jampani | 328371d | 2015-05-29 14:06:27 -0700 | [diff] [blame] | 679 | final boolean isReachable = isReachable(did); |
| 680 | if (!isReachable) { |
| 681 | // device is not connected to this node |
| 682 | if (myNextRole != NONE) { |
| 683 | log.warn("Node was instructed to be {} role for {}, " |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame^] | 684 | + "but this node cannot reach the device. " |
| 685 | + "Relinquishing role. ", |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 686 | myNextRole, did); |
Madan Jampani | 328371d | 2015-05-29 14:06:27 -0700 | [diff] [blame] | 687 | mastershipService.relinquishMastership(did); |
| 688 | } |
| 689 | return; |
| 690 | } |
| 691 | |
| 692 | // device is connected to this node: |
| 693 | if (store.getDevice(did) != null) { |
| 694 | reassertRole(did, myNextRole); |
| 695 | } else { |
| 696 | log.debug("Device is not yet/no longer in the store: {}", did); |
| 697 | } |
| 698 | } |
| 699 | |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 700 | // Intercepts mastership events |
| 701 | private class InternalMastershipListener implements MastershipListener { |
| 702 | |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 703 | @Override |
| 704 | public void event(MastershipEvent event) { |
Madan Jampani | 328371d | 2015-05-29 14:06:27 -0700 | [diff] [blame] | 705 | backgroundService.submit(() -> { |
| 706 | try { |
| 707 | handleMastershipEvent(event); |
| 708 | } catch (Exception e) { |
| 709 | log.warn("Failed to handle {}", event, e); |
Yuta HIGUCHI | d26354d | 2014-10-31 14:14:38 -0700 | [diff] [blame] | 710 | } |
Madan Jampani | 328371d | 2015-05-29 14:06:27 -0700 | [diff] [blame] | 711 | }); |
Ayaka Koshibe | 317245a | 2014-10-29 00:34:43 -0700 | [diff] [blame] | 712 | } |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 713 | } |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame] | 714 | |
| 715 | // Store delegate to re-post events emitted from the store. |
Thomas Vachuska | b17c41f | 2015-05-19 11:16:05 -0700 | [diff] [blame] | 716 | private class InternalStoreDelegate implements DeviceStoreDelegate { |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame] | 717 | @Override |
| 718 | public void notify(DeviceEvent event) { |
| 719 | post(event); |
| 720 | } |
| 721 | } |
samuel | 738dfaf | 2015-07-11 11:08:57 +0800 | [diff] [blame] | 722 | |
| 723 | @Override |
| 724 | public Iterable<Device> getDevices(Type type) { |
| 725 | checkPermission(Permission.DEVICE_READ); |
| 726 | Set<Device> results = new HashSet<>(); |
| 727 | Iterable<Device> devices = store.getDevices(); |
| 728 | if (devices != null) { |
| 729 | devices.forEach(d -> { |
| 730 | if (type.equals(d.type())) { |
| 731 | results.add(d); |
| 732 | } |
| 733 | }); |
| 734 | } |
| 735 | return results; |
| 736 | } |
| 737 | |
| 738 | @Override |
| 739 | public Iterable<Device> getAvailableDevices(Type type) { |
| 740 | checkPermission(Permission.DEVICE_READ); |
| 741 | Set<Device> results = new HashSet<>(); |
| 742 | Iterable<Device> availableDevices = store.getAvailableDevices(); |
| 743 | if (availableDevices != null) { |
| 744 | availableDevices.forEach(d -> { |
| 745 | if (type.equals(d.type())) { |
| 746 | results.add(d); |
| 747 | } |
| 748 | }); |
| 749 | } |
| 750 | return results; |
| 751 | } |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 752 | |
| 753 | private class InternalNetworkConfigListener implements NetworkConfigListener { |
| 754 | @Override |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame^] | 755 | public boolean isRelevant(NetworkConfigEvent event) { |
| 756 | return (event.type() == NetworkConfigEvent.Type.CONFIG_ADDED || |
| 757 | event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED) |
| 758 | && event.configClass().equals(BasicDeviceConfig.class); |
| 759 | } |
| 760 | |
| 761 | @Override |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 762 | public void event(NetworkConfigEvent event) { |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame^] | 763 | log.info("Detected Device network config event {}", event.type()); |
| 764 | kickOutBadDevice(((DeviceId) event.subject())); |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 765 | } |
| 766 | } |
| 767 | |
| 768 | // checks if the specified device is allowed by the BasicDeviceConfig |
| 769 | // and if not, removes it |
| 770 | private void kickOutBadDevice(DeviceId deviceId) { |
| 771 | BasicDeviceConfig cfg = networkConfigService.getConfig(deviceId, BasicDeviceConfig.class); |
| 772 | if (!cfg.isAllowed()) { |
| 773 | Device badDevice = getDevice(deviceId); |
| 774 | if (badDevice != null) { |
| 775 | removeDevice(deviceId); |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 776 | } |
| 777 | } |
| 778 | } |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 779 | } |