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