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