Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2014-present 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 | |
Simon Hunt | ffbad3b | 2017-05-16 15:37:51 -0700 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableList; |
| 19 | import com.google.common.collect.Maps; |
| 20 | import com.google.common.collect.Multimap; |
| 21 | import com.google.common.util.concurrent.Futures; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 22 | import org.apache.felix.scr.annotations.Activate; |
| 23 | import org.apache.felix.scr.annotations.Component; |
| 24 | import org.apache.felix.scr.annotations.Deactivate; |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 25 | import org.apache.felix.scr.annotations.Reference; |
| 26 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 27 | import org.apache.felix.scr.annotations.Service; |
Saurav Das | d5ec9e9 | 2017-01-17 10:40:18 -0800 | [diff] [blame] | 28 | import org.joda.time.DateTime; |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 29 | import org.onlab.util.Tools; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 30 | import org.onosproject.cluster.ClusterService; |
| 31 | import org.onosproject.cluster.NodeId; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 32 | import org.onosproject.mastership.MastershipEvent; |
| 33 | import org.onosproject.mastership.MastershipListener; |
| 34 | import org.onosproject.mastership.MastershipService; |
| 35 | import org.onosproject.mastership.MastershipTerm; |
| 36 | import org.onosproject.mastership.MastershipTermService; |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 37 | import org.onosproject.net.ConnectPoint; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 38 | import org.onosproject.net.Device; |
samuel | 738dfaf | 2015-07-11 11:08:57 +0800 | [diff] [blame] | 39 | import org.onosproject.net.Device.Type; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 40 | import org.onosproject.net.DeviceId; |
| 41 | import org.onosproject.net.MastershipRole; |
| 42 | import org.onosproject.net.Port; |
| 43 | import org.onosproject.net.PortNumber; |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 44 | import org.onosproject.net.config.Config; |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 45 | import org.onosproject.net.config.NetworkConfigEvent; |
| 46 | import org.onosproject.net.config.NetworkConfigListener; |
| 47 | import org.onosproject.net.config.NetworkConfigService; |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 48 | import org.onosproject.net.config.PortConfigOperator; |
| 49 | import org.onosproject.net.config.PortConfigOperatorRegistry; |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 50 | import org.onosproject.net.config.basics.BasicDeviceConfig; |
Yuta HIGUCHI | 7438f5a | 2017-02-15 22:09:46 -0800 | [diff] [blame] | 51 | import org.onosproject.net.config.basics.PortAnnotationConfig; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 52 | import org.onosproject.net.device.DefaultPortDescription; |
| 53 | import org.onosproject.net.device.DeviceAdminService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 54 | import org.onosproject.net.device.DeviceDescription; |
| 55 | import org.onosproject.net.device.DeviceEvent; |
| 56 | import org.onosproject.net.device.DeviceListener; |
| 57 | import org.onosproject.net.device.DeviceProvider; |
| 58 | import org.onosproject.net.device.DeviceProviderRegistry; |
| 59 | import org.onosproject.net.device.DeviceProviderService; |
| 60 | import org.onosproject.net.device.DeviceService; |
| 61 | import org.onosproject.net.device.DeviceStore; |
| 62 | import org.onosproject.net.device.DeviceStoreDelegate; |
| 63 | import org.onosproject.net.device.PortDescription; |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 64 | import org.onosproject.net.device.PortStatistics; |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 65 | import org.onosproject.net.provider.AbstractListenerProviderRegistry; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 66 | import org.onosproject.net.provider.AbstractProviderService; |
Yuta HIGUCHI | 3a2a987 | 2016-11-29 20:24:23 -0800 | [diff] [blame] | 67 | import org.onosproject.net.provider.Provider; |
| 68 | import org.onosproject.net.provider.ProviderId; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 69 | import org.slf4j.Logger; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 70 | |
Simon Hunt | ffbad3b | 2017-05-16 15:37:51 -0700 | [diff] [blame] | 71 | import java.util.Collection; |
| 72 | import java.util.HashSet; |
| 73 | import java.util.List; |
| 74 | import java.util.Map; |
| 75 | import java.util.Objects; |
| 76 | import java.util.Optional; |
| 77 | import java.util.Set; |
| 78 | import java.util.concurrent.CompletableFuture; |
| 79 | import java.util.concurrent.ConcurrentHashMap; |
| 80 | import java.util.concurrent.CopyOnWriteArrayList; |
| 81 | import java.util.concurrent.ExecutionException; |
| 82 | import java.util.concurrent.ScheduledExecutorService; |
| 83 | import java.util.concurrent.TimeUnit; |
| 84 | import java.util.stream.Collectors; |
Jonathan Hart | 2f66936 | 2015-02-11 16:19:20 -0800 | [diff] [blame] | 85 | |
Ray Milkey | 9ef2223 | 2016-07-14 12:42:37 -0700 | [diff] [blame] | 86 | import static com.google.common.base.Preconditions.checkNotNull; |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 87 | import static com.google.common.collect.Multimaps.newListMultimap; |
| 88 | import static com.google.common.collect.Multimaps.synchronizedListMultimap; |
Ray Milkey | 9ef2223 | 2016-07-14 12:42:37 -0700 | [diff] [blame] | 89 | import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor; |
| 90 | import static org.onlab.util.Tools.groupedThreads; |
| 91 | import static org.onosproject.net.MastershipRole.MASTER; |
| 92 | import static org.onosproject.net.MastershipRole.NONE; |
| 93 | import static org.onosproject.net.MastershipRole.STANDBY; |
Ray Milkey | 9ef2223 | 2016-07-14 12:42:37 -0700 | [diff] [blame] | 94 | import static org.onosproject.security.AppGuard.checkPermission; |
| 95 | import static org.onosproject.security.AppPermission.Type.DEVICE_READ; |
| 96 | import static org.slf4j.LoggerFactory.getLogger; |
| 97 | |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 98 | /** |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 99 | * Provides implementation of the device SB & NB APIs. |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 100 | */ |
| 101 | @Component(immediate = true) |
| 102 | @Service |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 103 | public class DeviceManager |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 104 | extends AbstractListenerProviderRegistry<DeviceEvent, DeviceListener, DeviceProvider, DeviceProviderService> |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 105 | implements DeviceService, DeviceAdminService, DeviceProviderRegistry, PortConfigOperatorRegistry { |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 106 | |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 107 | private static final String DEVICE_ID_NULL = "Device ID cannot be null"; |
| 108 | private static final String PORT_NUMBER_NULL = "Port number cannot be null"; |
| 109 | private static final String DEVICE_DESCRIPTION_NULL = "Device description cannot be null"; |
| 110 | private static final String PORT_DESCRIPTION_NULL = "Port description cannot be null"; |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 111 | 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] | 112 | |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 113 | private final Logger log = getLogger(getClass()); |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 114 | |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 115 | private final DeviceStoreDelegate delegate = new InternalStoreDelegate(); |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame] | 116 | |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 117 | private final MastershipListener mastershipListener = new InternalMastershipListener(); |
Madan Jampani | de003d9 | 2015-05-11 17:14:20 -0700 | [diff] [blame] | 118 | private NodeId localNodeId; |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 119 | |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 120 | private ScheduledExecutorService backgroundService; |
| 121 | |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 122 | private final NetworkConfigListener networkConfigListener = new InternalNetworkConfigListener(); |
| 123 | |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 124 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 125 | protected DeviceStore store; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 126 | |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 127 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 128 | protected ClusterService clusterService; |
| 129 | |
| 130 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Ayaka Koshibe | a7f044e | 2014-09-23 16:56:20 -0700 | [diff] [blame] | 131 | protected MastershipService mastershipService; |
| 132 | |
Yuta HIGUCHI | bcac499 | 2014-11-22 19:27:57 -0800 | [diff] [blame] | 133 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Ayaka Koshibe | 3de43ca | 2014-09-26 16:40:23 -0700 | [diff] [blame] | 134 | protected MastershipTermService termService; |
| 135 | |
Madan Jampani | 61056bc | 2014-09-27 09:07:26 -0700 | [diff] [blame] | 136 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 137 | protected NetworkConfigService networkConfigService; |
| 138 | |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 139 | |
| 140 | /** |
| 141 | * List of all registered PortConfigOperator. |
| 142 | */ |
| 143 | private final List<PortConfigOperator> portOps = new CopyOnWriteArrayList<>(); |
| 144 | |
| 145 | /** |
| 146 | * Index to look up PortConfigOperator from Config each PortConfigOperator uses. |
| 147 | */ |
| 148 | private final Multimap<Class<? extends Config<ConnectPoint>>, PortConfigOperator> portOpsIndex |
Simon Hunt | ffbad3b | 2017-05-16 15:37:51 -0700 | [diff] [blame] | 149 | = synchronizedListMultimap( |
| 150 | newListMultimap(new ConcurrentHashMap<>(), CopyOnWriteArrayList::new)); |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 151 | |
Yuta HIGUCHI | 7438f5a | 2017-02-15 22:09:46 -0800 | [diff] [blame] | 152 | // not part of portOps. must be executed at the end |
| 153 | private PortAnnotationOperator portAnnotationOp; |
| 154 | |
Saurav Das | d5ec9e9 | 2017-01-17 10:40:18 -0800 | [diff] [blame] | 155 | /** |
| 156 | * Local storage for connectivity status of devices. |
| 157 | */ |
| 158 | private class LocalStatus { |
| 159 | boolean connected; |
| 160 | DateTime dateTime; |
| 161 | |
| 162 | public LocalStatus(boolean b, DateTime now) { |
| 163 | connected = b; |
| 164 | dateTime = now; |
| 165 | } |
| 166 | } |
Simon Hunt | ffbad3b | 2017-05-16 15:37:51 -0700 | [diff] [blame] | 167 | |
Saurav Das | d5ec9e9 | 2017-01-17 10:40:18 -0800 | [diff] [blame] | 168 | private final Map<DeviceId, LocalStatus> deviceLocalStatus = |
| 169 | Maps.newConcurrentMap(); |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 170 | |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 171 | @Activate |
| 172 | public void activate() { |
Yuta HIGUCHI | 7438f5a | 2017-02-15 22:09:46 -0800 | [diff] [blame] | 173 | portAnnotationOp = new PortAnnotationOperator(networkConfigService); |
| 174 | portOpsIndex.put(PortAnnotationConfig.class, portAnnotationOp); |
| 175 | |
HIGUCHI Yuta | 060da9a | 2016-03-11 19:16:35 -0800 | [diff] [blame] | 176 | backgroundService = newSingleThreadScheduledExecutor( |
Simon Hunt | ffbad3b | 2017-05-16 15:37:51 -0700 | [diff] [blame] | 177 | groupedThreads("onos/device", "manager-background", log)); |
Madan Jampani | de003d9 | 2015-05-11 17:14:20 -0700 | [diff] [blame] | 178 | localNodeId = clusterService.getLocalNode().id(); |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 179 | |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame] | 180 | store.setDelegate(delegate); |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 181 | eventDispatcher.addSink(DeviceEvent.class, listenerRegistry); |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 182 | mastershipService.addListener(mastershipListener); |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 183 | networkConfigService.addListener(networkConfigListener); |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 184 | |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 185 | backgroundService.scheduleWithFixedDelay(() -> { |
| 186 | try { |
| 187 | mastershipCheck(); |
| 188 | } catch (Exception e) { |
| 189 | log.error("Exception thrown during integrity check", e); |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 190 | } |
| 191 | }, 1, 1, TimeUnit.MINUTES); |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 192 | log.info("Started"); |
| 193 | } |
| 194 | |
| 195 | @Deactivate |
| 196 | public void deactivate() { |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 197 | backgroundService.shutdown(); |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 198 | networkConfigService.removeListener(networkConfigListener); |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame] | 199 | store.unsetDelegate(delegate); |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 200 | mastershipService.removeListener(mastershipListener); |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 201 | eventDispatcher.removeSink(DeviceEvent.class); |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 202 | log.info("Stopped"); |
| 203 | } |
| 204 | |
| 205 | @Override |
tom | ad2d209 | 2014-09-06 23:24:20 -0700 | [diff] [blame] | 206 | public int getDeviceCount() { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 207 | checkPermission(DEVICE_READ); |
tom | ad2d209 | 2014-09-06 23:24:20 -0700 | [diff] [blame] | 208 | return store.getDeviceCount(); |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | @Override |
mskala | 32000d3 | 2017-07-14 16:27:06 +0200 | [diff] [blame^] | 212 | public int getAvailableDeviceCount() { |
| 213 | checkPermission(DEVICE_READ); |
| 214 | return store.getAvailableDeviceCount(); |
| 215 | } |
| 216 | |
| 217 | @Override |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 218 | public Iterable<Device> getDevices() { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 219 | checkPermission(DEVICE_READ); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 220 | return store.getDevices(); |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 221 | } |
| 222 | |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 223 | @Override |
Yuta HIGUCHI | f1f2ac0 | 2014-11-26 14:02:22 -0800 | [diff] [blame] | 224 | public Iterable<Device> getAvailableDevices() { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 225 | checkPermission(DEVICE_READ); |
Yuta HIGUCHI | f1f2ac0 | 2014-11-26 14:02:22 -0800 | [diff] [blame] | 226 | return store.getAvailableDevices(); |
| 227 | } |
| 228 | |
| 229 | @Override |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 230 | public Device getDevice(DeviceId deviceId) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 231 | checkPermission(DEVICE_READ); |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 232 | checkNotNull(deviceId, DEVICE_ID_NULL); |
tom | 132b58a | 2014-08-28 16:11:28 -0700 | [diff] [blame] | 233 | return store.getDevice(deviceId); |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | @Override |
tom | ad2d209 | 2014-09-06 23:24:20 -0700 | [diff] [blame] | 237 | public MastershipRole getRole(DeviceId deviceId) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 238 | checkPermission(DEVICE_READ); |
tom | ad2d209 | 2014-09-06 23:24:20 -0700 | [diff] [blame] | 239 | checkNotNull(deviceId, DEVICE_ID_NULL); |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 240 | return mastershipService.getLocalRole(deviceId); |
tom | ad2d209 | 2014-09-06 23:24:20 -0700 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | @Override |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 244 | public List<Port> getPorts(DeviceId deviceId) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 245 | checkPermission(DEVICE_READ); |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 246 | checkNotNull(deviceId, DEVICE_ID_NULL); |
tom | 132b58a | 2014-08-28 16:11:28 -0700 | [diff] [blame] | 247 | return store.getPorts(deviceId); |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | @Override |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 251 | public List<PortStatistics> getPortStatistics(DeviceId deviceId) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 252 | checkPermission(DEVICE_READ); |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 253 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 254 | return store.getPortStatistics(deviceId); |
| 255 | } |
| 256 | |
| 257 | @Override |
Dusan Pajin | 11ff4a8 | 2015-08-20 18:03:05 +0200 | [diff] [blame] | 258 | public List<PortStatistics> getPortDeltaStatistics(DeviceId deviceId) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 259 | checkPermission(DEVICE_READ); |
Dusan Pajin | 11ff4a8 | 2015-08-20 18:03:05 +0200 | [diff] [blame] | 260 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 261 | return store.getPortDeltaStatistics(deviceId); |
| 262 | } |
| 263 | |
| 264 | @Override |
Viswanath KSP | 22774cd | 2016-08-20 20:06:30 +0530 | [diff] [blame] | 265 | public PortStatistics getStatisticsForPort(DeviceId deviceId, PortNumber portNumber) { |
| 266 | checkPermission(DEVICE_READ); |
| 267 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 268 | checkNotNull(portNumber, PORT_NUMBER_NULL); |
| 269 | return store.getStatisticsForPort(deviceId, portNumber); |
| 270 | } |
| 271 | |
| 272 | @Override |
| 273 | public PortStatistics getDeltaStatisticsForPort(DeviceId deviceId, PortNumber portNumber) { |
| 274 | checkPermission(DEVICE_READ); |
| 275 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 276 | checkNotNull(portNumber, PORT_NUMBER_NULL); |
| 277 | return store.getDeltaStatisticsForPort(deviceId, portNumber); |
| 278 | } |
| 279 | |
| 280 | @Override |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 281 | public Port getPort(DeviceId deviceId, PortNumber portNumber) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 282 | checkPermission(DEVICE_READ); |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 283 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 284 | checkNotNull(portNumber, PORT_NUMBER_NULL); |
tom | 132b58a | 2014-08-28 16:11:28 -0700 | [diff] [blame] | 285 | return store.getPort(deviceId, portNumber); |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | @Override |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 289 | public boolean isAvailable(DeviceId deviceId) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 290 | checkPermission(DEVICE_READ); |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 291 | |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 292 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 293 | return store.isAvailable(deviceId); |
| 294 | } |
| 295 | |
Saurav Das | d5ec9e9 | 2017-01-17 10:40:18 -0800 | [diff] [blame] | 296 | @Override |
| 297 | public String localStatus(DeviceId deviceId) { |
| 298 | LocalStatus ls = deviceLocalStatus.get(deviceId); |
| 299 | if (ls == null) { |
| 300 | return "No Record"; |
| 301 | } |
| 302 | String timeAgo = Tools.timeAgo(ls.dateTime.getMillis()); |
| 303 | return (ls.connected) ? "connected " + timeAgo : "disconnected " + timeAgo; |
| 304 | } |
| 305 | |
Ayaka Koshibe | e60d452 | 2014-10-28 15:07:00 -0700 | [diff] [blame] | 306 | // Check a device for control channel connectivity. |
Yuta HIGUCHI | 5481532 | 2014-10-31 23:17:08 -0700 | [diff] [blame] | 307 | private boolean isReachable(DeviceId deviceId) { |
Ayaka Koshibe | 78bcbc1 | 2014-11-19 14:28:58 -0800 | [diff] [blame] | 308 | if (deviceId == null) { |
| 309 | return false; |
| 310 | } |
Yuta HIGUCHI | 5481532 | 2014-10-31 23:17:08 -0700 | [diff] [blame] | 311 | DeviceProvider provider = getProvider(deviceId); |
| 312 | if (provider != null) { |
| 313 | return provider.isReachable(deviceId); |
| 314 | } else { |
Yuta HIGUCHI | 72669c4 | 2014-11-13 14:48:17 -0800 | [diff] [blame] | 315 | log.debug("Provider not found for {}", deviceId); |
Ayaka Koshibe | e60d452 | 2014-10-28 15:07:00 -0700 | [diff] [blame] | 316 | return false; |
| 317 | } |
Ayaka Koshibe | e8708e3 | 2014-10-22 13:40:18 -0700 | [diff] [blame] | 318 | } |
| 319 | |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 320 | @Override |
| 321 | public void removeDevice(DeviceId deviceId) { |
| 322 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 323 | DeviceEvent event = store.removeDevice(deviceId); |
tom | 0efbb1d | 2014-09-09 11:54:28 -0700 | [diff] [blame] | 324 | if (event != null) { |
| 325 | log.info("Device {} administratively removed", deviceId); |
| 326 | post(event); |
| 327 | } |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 328 | } |
| 329 | |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 330 | @Override |
Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 331 | public void changePortState(DeviceId deviceId, PortNumber portNumber, |
| 332 | boolean enable) { |
| 333 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 334 | checkNotNull(deviceId, PORT_NUMBER_NULL); |
| 335 | DeviceProvider provider = getProvider(deviceId); |
| 336 | if (provider != null) { |
| 337 | log.warn("Port {} on device {} being administratively brought {}", |
| 338 | portNumber, deviceId, |
| 339 | (enable) ? "UP" : "DOWN"); |
| 340 | provider.changePortState(deviceId, portNumber, enable); |
| 341 | } else { |
| 342 | log.warn("Provider not found for {}", deviceId); |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | @Override |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 347 | protected DeviceProviderService createProviderService( |
| 348 | DeviceProvider provider) { |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 349 | return new InternalDeviceProviderService(provider); |
| 350 | } |
| 351 | |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 352 | /** |
| 353 | * Checks if all the reachable devices have a valid mastership role. |
| 354 | */ |
| 355 | private void mastershipCheck() { |
| 356 | log.debug("Checking mastership"); |
| 357 | for (Device device : getDevices()) { |
| 358 | final DeviceId deviceId = device.id(); |
Ray Milkey | c710467 | 2016-08-31 12:04:34 -0700 | [diff] [blame] | 359 | MastershipRole myRole = mastershipService.getLocalRole(deviceId); |
| 360 | log.trace("Checking device {}. Current role is {}", deviceId, myRole); |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 361 | if (!isReachable(deviceId)) { |
Ray Milkey | c710467 | 2016-08-31 12:04:34 -0700 | [diff] [blame] | 362 | if (myRole != NONE) { |
helenyrwu | fd296b6 | 2016-06-22 17:43:02 -0700 | [diff] [blame] | 363 | // can't be master if device is not reachable |
| 364 | try { |
Ray Milkey | c710467 | 2016-08-31 12:04:34 -0700 | [diff] [blame] | 365 | if (myRole == MASTER) { |
| 366 | post(store.markOffline(deviceId)); |
| 367 | } |
helenyrwu | fd296b6 | 2016-06-22 17:43:02 -0700 | [diff] [blame] | 368 | //relinquish master role and ability to be backup. |
| 369 | mastershipService.relinquishMastership(deviceId).get(); |
| 370 | } catch (InterruptedException e) { |
| 371 | log.warn("Interrupted while reliquishing role for {}", deviceId); |
| 372 | Thread.currentThread().interrupt(); |
| 373 | } catch (ExecutionException e) { |
| 374 | log.error("Exception thrown while relinquishing role for {}", deviceId, e); |
| 375 | } |
Bharath Thiruveedula | 651a7da | 2016-12-13 02:52:50 +0530 | [diff] [blame] | 376 | } else { |
| 377 | // check if the device has master, if not, mark it offline |
Bharath Thiruveedula | 651a7da | 2016-12-13 02:52:50 +0530 | [diff] [blame] | 378 | // only the nodes which has mastership role can mark any device offline. |
| 379 | CompletableFuture<MastershipRole> roleFuture = mastershipService.requestRoleFor(deviceId); |
| 380 | roleFuture.thenAccept(role -> { |
| 381 | MastershipTerm term = termService.getMastershipTerm(deviceId); |
| 382 | if (term != null && localNodeId.equals(term.master())) { |
| 383 | log.info("Marking unreachable device {} offline", deviceId); |
| 384 | post(store.markOffline(deviceId)); |
| 385 | } else { |
| 386 | log.info("Failed marking {} offline. {}", deviceId, role); |
| 387 | } |
| 388 | mastershipService.relinquishMastership(deviceId); |
| 389 | }); |
helenyrwu | fd296b6 | 2016-06-22 17:43:02 -0700 | [diff] [blame] | 390 | } |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 391 | continue; |
| 392 | } |
| 393 | |
Ray Milkey | c710467 | 2016-08-31 12:04:34 -0700 | [diff] [blame] | 394 | if (myRole != NONE) { |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 395 | continue; |
| 396 | } |
| 397 | |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 398 | 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] | 399 | |
| 400 | // isReachable but was not MASTER or STANDBY, get a role and apply |
| 401 | // Note: NONE triggers request to MastershipService |
| 402 | reassertRole(deviceId, NONE); |
| 403 | } |
| 404 | } |
| 405 | |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 406 | // Personalized device provider service issued to the supplied provider. |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 407 | private class InternalDeviceProviderService |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 408 | extends AbstractProviderService<DeviceProvider> |
| 409 | implements DeviceProviderService { |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 410 | |
tom | cfde062 | 2014-09-09 11:02:42 -0700 | [diff] [blame] | 411 | InternalDeviceProviderService(DeviceProvider provider) { |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 412 | super(provider); |
| 413 | } |
| 414 | |
Yuta HIGUCHI | d26354d | 2014-10-31 14:14:38 -0700 | [diff] [blame] | 415 | /** |
| 416 | * Apply role in reaction to provider event. |
| 417 | * |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 418 | * @param deviceId device identifier |
| 419 | * @param newRole new role to apply to the device |
Yuta HIGUCHI | d26354d | 2014-10-31 14:14:38 -0700 | [diff] [blame] | 420 | * @return true if the request was sent to provider |
| 421 | */ |
| 422 | private boolean applyRole(DeviceId deviceId, MastershipRole newRole) { |
| 423 | |
| 424 | if (newRole.equals(MastershipRole.NONE)) { |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 425 | //no-op |
Yuta HIGUCHI | d26354d | 2014-10-31 14:14:38 -0700 | [diff] [blame] | 426 | return true; |
| 427 | } |
| 428 | |
| 429 | DeviceProvider provider = provider(); |
| 430 | if (provider == null) { |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 431 | log.warn("Provider for {} was not found. Cannot apply role {}", |
| 432 | deviceId, newRole); |
Yuta HIGUCHI | d26354d | 2014-10-31 14:14:38 -0700 | [diff] [blame] | 433 | return false; |
| 434 | } |
Yuta HIGUCHI | 5481532 | 2014-10-31 23:17:08 -0700 | [diff] [blame] | 435 | provider.roleChanged(deviceId, newRole); |
| 436 | // not triggering probe when triggered by provider service event |
Yuta HIGUCHI | d26354d | 2014-10-31 14:14:38 -0700 | [diff] [blame] | 437 | return true; |
| 438 | } |
| 439 | |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 440 | @Override |
alshabib | b7b4063 | 2014-09-28 21:30:00 -0700 | [diff] [blame] | 441 | public void deviceConnected(DeviceId deviceId, |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 442 | DeviceDescription deviceDescription) { |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 443 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 444 | checkNotNull(deviceDescription, DEVICE_DESCRIPTION_NULL); |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 445 | checkValidity(); |
Yuta HIGUCHI | 24b2e2a | 2014-10-07 15:53:57 -0700 | [diff] [blame] | 446 | |
Saurav Das | d5ec9e9 | 2017-01-17 10:40:18 -0800 | [diff] [blame] | 447 | deviceLocalStatus.put(deviceId, new LocalStatus(true, DateTime.now())); |
| 448 | |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 449 | BasicDeviceConfig cfg = networkConfigService.getConfig(deviceId, BasicDeviceConfig.class); |
| 450 | if (!isAllowed(cfg)) { |
| 451 | log.warn("Device {} is not allowed", deviceId); |
| 452 | return; |
| 453 | } |
| 454 | // Generate updated description and establish my Role |
| 455 | deviceDescription = BasicDeviceOperator.combine(cfg, deviceDescription); |
Madan Jampani | 565a66a | 2015-07-25 17:01:13 -0700 | [diff] [blame] | 456 | Futures.getUnchecked(mastershipService.requestRoleFor(deviceId) |
Simon Hunt | ffbad3b | 2017-05-16 15:37:51 -0700 | [diff] [blame] | 457 | .thenAccept(role -> { |
| 458 | log.info("Local role is {} for {}", role, deviceId); |
| 459 | applyRole(deviceId, role); |
| 460 | })); |
HIGUCHI Yuta | 11530fb | 2015-05-27 13:10:20 -0700 | [diff] [blame] | 461 | |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 462 | DeviceEvent event = store.createOrUpdateDevice(provider().id(), deviceId, |
| 463 | deviceDescription); |
helenyrwu | fd296b6 | 2016-06-22 17:43:02 -0700 | [diff] [blame] | 464 | if (deviceDescription.isDefaultAvailable()) { |
| 465 | log.info("Device {} connected", deviceId); |
| 466 | } else { |
| 467 | log.info("Device {} registered", deviceId); |
| 468 | } |
tom | 80c0e5e | 2014-09-08 18:08:58 -0700 | [diff] [blame] | 469 | if (event != null) { |
Yuta HIGUCHI | d26354d | 2014-10-31 14:14:38 -0700 | [diff] [blame] | 470 | log.trace("event: {} {}", event.type(), event); |
tom | 568581d | 2014-09-08 20:13:36 -0700 | [diff] [blame] | 471 | post(event); |
tom | 80c0e5e | 2014-09-08 18:08:58 -0700 | [diff] [blame] | 472 | } |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 473 | } |
| 474 | |
Yuta HIGUCHI | 6eb00cc | 2016-06-10 11:55:12 -0700 | [diff] [blame] | 475 | private PortDescription ensurePortEnabledState(PortDescription desc, boolean enabled) { |
| 476 | if (desc.isEnabled() != enabled) { |
| 477 | return new DefaultPortDescription(desc.portNumber(), |
| 478 | enabled, |
| 479 | desc.type(), |
| 480 | desc.portSpeed(), |
| 481 | desc.annotations()); |
| 482 | } |
| 483 | return desc; |
| 484 | } |
| 485 | |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 486 | @Override |
| 487 | public void deviceDisconnected(DeviceId deviceId) { |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 488 | checkNotNull(deviceId, DEVICE_ID_NULL); |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 489 | checkValidity(); |
Saurav Das | d5ec9e9 | 2017-01-17 10:40:18 -0800 | [diff] [blame] | 490 | deviceLocalStatus.put(deviceId, new LocalStatus(false, DateTime.now())); |
Yuta HIGUCHI | 2d3cd31 | 2014-10-31 11:38:04 -0700 | [diff] [blame] | 491 | log.info("Device {} disconnected from this node", deviceId); |
Ayaka Koshibe | d9f693e | 2014-09-29 18:04:54 -0700 | [diff] [blame] | 492 | |
Yuta HIGUCHI | 6eb00cc | 2016-06-10 11:55:12 -0700 | [diff] [blame] | 493 | List<PortDescription> descs = store.getPortDescriptions(provider().id(), deviceId) |
| 494 | .map(desc -> ensurePortEnabledState(desc, false)) |
| 495 | .collect(Collectors.toList()); |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 496 | |
alshabib | afc514a | 2014-12-01 14:44:05 -0800 | [diff] [blame] | 497 | store.updatePorts(this.provider().id(), deviceId, descs); |
Ayaka Koshibe | b5c63a0 | 2014-10-18 18:42:27 -0700 | [diff] [blame] | 498 | try { |
Madan Jampani | 565a66a | 2015-07-25 17:01:13 -0700 | [diff] [blame] | 499 | if (mastershipService.isLocalMaster(deviceId)) { |
Thomas Vachuska | 5f429d6 | 2015-05-28 15:34:36 -0700 | [diff] [blame] | 500 | post(store.markOffline(deviceId)); |
| 501 | } |
Ayaka Koshibe | b5c63a0 | 2014-10-18 18:42:27 -0700 | [diff] [blame] | 502 | } catch (IllegalStateException e) { |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame] | 503 | log.warn("Failed to mark {} offline", deviceId); |
| 504 | // only the MASTER should be marking off-line in normal cases, |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 505 | // but if I was the last STANDBY connection, etc. and no one else |
| 506 | // was there to mark the device offline, this instance may need to |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame] | 507 | // temporarily request for Master Role and mark offline. |
| 508 | |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 509 | //there are times when this node will correctly have mastership, BUT |
| 510 | //that isn't reflected in the ClockManager before the device disconnects. |
| 511 | //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] | 512 | |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame] | 513 | // FIXME: Store semantics leaking out as IllegalStateException. |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 514 | // Consider revising store API to handle this scenario. |
| 515 | CompletableFuture<MastershipRole> roleFuture = mastershipService.requestRoleFor(deviceId); |
Madan Jampani | de003d9 | 2015-05-11 17:14:20 -0700 | [diff] [blame] | 516 | roleFuture.whenComplete((role, error) -> { |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 517 | MastershipTerm term = termService.getMastershipTerm(deviceId); |
| 518 | // TODO: Move this type of check inside device clock manager, etc. |
| 519 | if (term != null && localNodeId.equals(term.master())) { |
| 520 | log.info("Retry marking {} offline", deviceId); |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 521 | post(store.markOffline(deviceId)); |
| 522 | } else { |
| 523 | log.info("Failed again marking {} offline. {}", deviceId, role); |
| 524 | } |
| 525 | }); |
Ayaka Koshibe | b5c63a0 | 2014-10-18 18:42:27 -0700 | [diff] [blame] | 526 | } finally { |
Madan Jampani | c6e574f | 2015-05-29 13:41:52 -0700 | [diff] [blame] | 527 | try { |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 528 | //relinquish master role and ability to be backup. |
Madan Jampani | c6e574f | 2015-05-29 13:41:52 -0700 | [diff] [blame] | 529 | mastershipService.relinquishMastership(deviceId).get(); |
| 530 | } catch (InterruptedException e) { |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 531 | log.warn("Interrupted while reliquishing role for {}", deviceId); |
Madan Jampani | c6e574f | 2015-05-29 13:41:52 -0700 | [diff] [blame] | 532 | Thread.currentThread().interrupt(); |
| 533 | } catch (ExecutionException e) { |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 534 | log.error("Exception thrown while relinquishing role for {}", deviceId, e); |
Madan Jampani | c6e574f | 2015-05-29 13:41:52 -0700 | [diff] [blame] | 535 | } |
tom | 0efbb1d | 2014-09-09 11:54:28 -0700 | [diff] [blame] | 536 | } |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | @Override |
alshabib | b7b4063 | 2014-09-28 21:30:00 -0700 | [diff] [blame] | 540 | public void updatePorts(DeviceId deviceId, |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 541 | List<PortDescription> portDescriptions) { |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 542 | checkNotNull(deviceId, DEVICE_ID_NULL); |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 543 | checkNotNull(portDescriptions, PORT_DESC_LIST_NULL); |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 544 | checkValidity(); |
Madan Jampani | 565a66a | 2015-07-25 17:01:13 -0700 | [diff] [blame] | 545 | if (!mastershipService.isLocalMaster(deviceId)) { |
Yuta HIGUCHI | 13c0b87 | 2014-10-30 18:09:22 -0700 | [diff] [blame] | 546 | // Never been a master for this device |
| 547 | // any update will be ignored. |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 548 | log.trace("Ignoring {} port updates on standby node. {}", deviceId, portDescriptions); |
Yuta HIGUCHI | 13c0b87 | 2014-10-30 18:09:22 -0700 | [diff] [blame] | 549 | return; |
| 550 | } |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 551 | portDescriptions = portDescriptions.stream() |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 552 | .map(e -> applyAllPortOps(deviceId, e)) |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 553 | .collect(Collectors.toList()); |
Yuta HIGUCHI | 5f6739c | 2014-10-01 14:04:01 -0700 | [diff] [blame] | 554 | List<DeviceEvent> events = store.updatePorts(this.provider().id(), |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 555 | deviceId, portDescriptions); |
Thomas Vachuska | 5923b9a | 2016-01-26 10:52:57 -0800 | [diff] [blame] | 556 | if (events != null) { |
| 557 | for (DeviceEvent event : events) { |
| 558 | post(event); |
| 559 | } |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 560 | } |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | @Override |
alshabib | b7b4063 | 2014-09-28 21:30:00 -0700 | [diff] [blame] | 564 | public void portStatusChanged(DeviceId deviceId, |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 565 | PortDescription portDescription) { |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 566 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 567 | checkNotNull(portDescription, PORT_DESCRIPTION_NULL); |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 568 | checkValidity(); |
Ayaka Koshibe | b5c63a0 | 2014-10-18 18:42:27 -0700 | [diff] [blame] | 569 | |
Madan Jampani | 565a66a | 2015-07-25 17:01:13 -0700 | [diff] [blame] | 570 | if (!mastershipService.isLocalMaster(deviceId)) { |
Yuta HIGUCHI | 13c0b87 | 2014-10-30 18:09:22 -0700 | [diff] [blame] | 571 | // Never been a master for this device |
| 572 | // any update will be ignored. |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 573 | log.trace("Ignoring {} port update on standby node. {}", deviceId, |
| 574 | portDescription); |
Yuta HIGUCHI | 13c0b87 | 2014-10-30 18:09:22 -0700 | [diff] [blame] | 575 | return; |
| 576 | } |
Ray Milkey | 9ef2223 | 2016-07-14 12:42:37 -0700 | [diff] [blame] | 577 | Device device = getDevice(deviceId); |
| 578 | if (device == null) { |
| 579 | log.trace("Device not found: {}", deviceId); |
nitinanand | ddfa8c9 | 2017-03-24 16:14:23 +0530 | [diff] [blame] | 580 | return; |
Ray Milkey | 9ef2223 | 2016-07-14 12:42:37 -0700 | [diff] [blame] | 581 | } |
Rimon Ashkenazy | 8ebfff0 | 2016-02-01 11:56:36 +0200 | [diff] [blame] | 582 | if ((Device.Type.ROADM.equals(device.type())) || |
Simon Hunt | ffbad3b | 2017-05-16 15:37:51 -0700 | [diff] [blame] | 583 | (Device.Type.OTN.equals(device.type()))) { |
HIGUCHI Yuta | 6972ae6 | 2016-05-12 19:57:46 -0700 | [diff] [blame] | 584 | // FIXME This is ignoring all other info in portDescription given as input?? |
Yuta HIGUCHI | 6eb00cc | 2016-06-10 11:55:12 -0700 | [diff] [blame] | 585 | PortDescription storedPortDesc = store.getPortDescription(provider().id(), |
Simon Hunt | ffbad3b | 2017-05-16 15:37:51 -0700 | [diff] [blame] | 586 | deviceId, |
| 587 | portDescription.portNumber()); |
Yuta HIGUCHI | 6eb00cc | 2016-06-10 11:55:12 -0700 | [diff] [blame] | 588 | portDescription = ensurePortEnabledState(storedPortDesc, |
| 589 | portDescription.isEnabled()); |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 590 | } |
| 591 | |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 592 | portDescription = applyAllPortOps(deviceId, portDescription); |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 593 | final DeviceEvent event = store.updatePortStatus(this.provider().id(), |
HIGUCHI Yuta | 6972ae6 | 2016-05-12 19:57:46 -0700 | [diff] [blame] | 594 | deviceId, |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 595 | portDescription); |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 596 | if (event != null) { |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 597 | log.info("Device {} port {} status changed", deviceId, event.port().number()); |
tom | 0efbb1d | 2014-09-09 11:54:28 -0700 | [diff] [blame] | 598 | post(event); |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 599 | } |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 600 | } |
tom | 3f2bbd7 | 2014-09-24 12:07:58 -0700 | [diff] [blame] | 601 | |
| 602 | @Override |
Michal Mach | ce77433 | 2017-01-25 11:02:55 +0100 | [diff] [blame] | 603 | public void deletePort(DeviceId deviceId, PortDescription basePortDescription) { |
| 604 | |
| 605 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 606 | checkNotNull(basePortDescription, PORT_DESCRIPTION_NULL); |
| 607 | checkValidity(); |
| 608 | |
| 609 | if (!mastershipService.isLocalMaster(deviceId)) { |
| 610 | // Never been a master for this device |
| 611 | // any update will be ignored. |
| 612 | log.trace("Ignoring {} port update on standby node. {}", deviceId, |
| 613 | basePortDescription); |
| 614 | return; |
| 615 | } |
| 616 | |
| 617 | Device device = getDevice(deviceId); |
| 618 | if (device == null) { |
| 619 | log.trace("Device not found: {}", deviceId); |
| 620 | } |
| 621 | |
| 622 | PortDescription newPortDescription = new DefaultPortDescription(basePortDescription.portNumber(), |
| 623 | basePortDescription.isEnabled(), |
| 624 | true, |
| 625 | basePortDescription.type(), |
| 626 | basePortDescription.portSpeed(), |
| 627 | basePortDescription.annotations()); |
| 628 | final DeviceEvent event = store.updatePortStatus(this.provider().id(), |
| 629 | deviceId, |
| 630 | newPortDescription); |
| 631 | if (event != null) { |
| 632 | log.info("Device {} port {} status changed", deviceId, event.port().number()); |
| 633 | post(event); |
| 634 | } |
| 635 | } |
| 636 | |
| 637 | @Override |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 638 | public void receivedRoleReply(DeviceId deviceId, MastershipRole requested, |
Thomas Vachuska | b17c41f | 2015-05-19 11:16:05 -0700 | [diff] [blame] | 639 | MastershipRole response) { |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 640 | // Several things can happen here: |
| 641 | // 1. request and response match |
| 642 | // 2. request and response don't match |
| 643 | // 3. MastershipRole and requested match (and 1 or 2 are true) |
| 644 | // 4. MastershipRole and requested don't match (and 1 or 2 are true) |
| 645 | // |
| 646 | // 2, 4, and 3 with case 2 are failure modes. |
| 647 | |
tom | 3f2bbd7 | 2014-09-24 12:07:58 -0700 | [diff] [blame] | 648 | // FIXME: implement response to this notification |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 649 | |
Madan Jampani | f2af771 | 2015-05-29 18:43:52 -0700 | [diff] [blame] | 650 | 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] | 651 | deviceId, requested, response); |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 652 | |
| 653 | if (requested == null && response == null) { |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 654 | // something was off with DeviceProvider, maybe check channel too? |
Shashikanth VH | 387a1ca | 2016-02-09 20:35:21 +0530 | [diff] [blame] | 655 | log.warn("Failed to assert role onto Device {}", deviceId); |
Yuta HIGUCHI | cf60390 | 2014-10-07 23:04:32 -0700 | [diff] [blame] | 656 | mastershipService.relinquishMastership(deviceId); |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 657 | return; |
Yuta HIGUCHI | cf60390 | 2014-10-07 23:04:32 -0700 | [diff] [blame] | 658 | } |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 659 | |
Thomas Vachuska | b17c41f | 2015-05-19 11:16:05 -0700 | [diff] [blame] | 660 | if (Objects.equals(requested, response)) { |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 661 | if (Objects.equals(requested, mastershipService.getLocalRole(deviceId))) { |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 662 | return; |
| 663 | } else { |
HIGUCHI Yuta | 1979f55 | 2015-12-28 21:24:26 -0800 | [diff] [blame] | 664 | log.warn("Role mismatch on {}. set to {}, but store demands {}", |
| 665 | deviceId, response, mastershipService.getLocalRole(deviceId)); |
| 666 | // roleManager got the device to comply, but doesn't agree with |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 667 | // the store; use the store's view, then try to reassert. |
HIGUCHI Yuta | 060da9a | 2016-03-11 19:16:35 -0800 | [diff] [blame] | 668 | backgroundService.execute(() -> reassertRole(deviceId, mastershipService.getLocalRole(deviceId))); |
HIGUCHI Yuta | 1979f55 | 2015-12-28 21:24:26 -0800 | [diff] [blame] | 669 | return; |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 670 | } |
| 671 | } else { |
| 672 | // we didn't get back what we asked for. Reelect someone else. |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 673 | log.warn("Failed to assert role [{}] onto Device {}", response, deviceId); |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 674 | if (response == MastershipRole.MASTER) { |
| 675 | mastershipService.relinquishMastership(deviceId); |
| 676 | // TODO: Shouldn't we be triggering event? |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 677 | //final Device device = getDevice(deviceId); |
| 678 | //post(new DeviceEvent(DEVICE_MASTERSHIP_CHANGED, device)); |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 679 | } |
| 680 | } |
tom | 3f2bbd7 | 2014-09-24 12:07:58 -0700 | [diff] [blame] | 681 | } |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 682 | |
| 683 | @Override |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 684 | public void updatePortStatistics(DeviceId deviceId, Collection<PortStatistics> portStatistics) { |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 685 | checkNotNull(deviceId, DEVICE_ID_NULL); |
Thomas Vachuska | b17c41f | 2015-05-19 11:16:05 -0700 | [diff] [blame] | 686 | checkNotNull(portStatistics, "Port statistics list cannot be null"); |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 687 | checkValidity(); |
| 688 | |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 689 | DeviceEvent event = store.updatePortStatistics(this.provider().id(), |
| 690 | deviceId, portStatistics); |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 691 | post(event); |
| 692 | } |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 693 | } |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 694 | |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 695 | // by default allowed, otherwise check flag |
| 696 | private boolean isAllowed(BasicDeviceConfig cfg) { |
| 697 | return (cfg == null || cfg.isAllowed()); |
| 698 | } |
| 699 | |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 700 | // Applies the specified role to the device; ignores NONE |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 701 | |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 702 | /** |
| 703 | * Apply role to device and send probe if MASTER. |
| 704 | * |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 705 | * @param deviceId device identifier |
| 706 | * @param newRole new role to apply to the device |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 707 | * @return true if the request was sent to provider |
| 708 | */ |
| 709 | private boolean applyRoleAndProbe(DeviceId deviceId, MastershipRole newRole) { |
| 710 | if (newRole.equals(MastershipRole.NONE)) { |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 711 | //no-op |
Yuta HIGUCHI | d26354d | 2014-10-31 14:14:38 -0700 | [diff] [blame] | 712 | return true; |
| 713 | } |
Ayaka Koshibe | 317245a | 2014-10-29 00:34:43 -0700 | [diff] [blame] | 714 | |
Ayaka Koshibe | 78bcbc1 | 2014-11-19 14:28:58 -0800 | [diff] [blame] | 715 | DeviceProvider provider = getProvider(deviceId); |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 716 | if (provider == null) { |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 717 | log.warn("Provider for {} was not found. Cannot apply role {}", deviceId, newRole); |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 718 | return false; |
| 719 | } |
| 720 | provider.roleChanged(deviceId, newRole); |
| 721 | |
| 722 | if (newRole.equals(MastershipRole.MASTER)) { |
HIGUCHI Yuta | 1979f55 | 2015-12-28 21:24:26 -0800 | [diff] [blame] | 723 | log.debug("sent TriggerProbe({})", deviceId); |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 724 | // only trigger event when request was sent to provider |
Ayaka Koshibe | 78bcbc1 | 2014-11-19 14:28:58 -0800 | [diff] [blame] | 725 | provider.triggerProbe(deviceId); |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 726 | } |
| 727 | return true; |
| 728 | } |
| 729 | |
| 730 | /** |
| 731 | * Reaasert role for specified device connected to this node. |
| 732 | * |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 733 | * @param did device identifier |
| 734 | * @param nextRole role to apply. If NONE is specified, |
| 735 | * it will ask mastership service for a role and apply it. |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 736 | */ |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 737 | private void reassertRole(final DeviceId did, |
| 738 | final MastershipRole nextRole) { |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 739 | |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 740 | MastershipRole myNextRole = nextRole; |
| 741 | if (myNextRole == NONE) { |
HIGUCHI Yuta | 1979f55 | 2015-12-28 21:24:26 -0800 | [diff] [blame] | 742 | try { |
| 743 | mastershipService.requestRoleFor(did).get(); |
| 744 | MastershipTerm term = termService.getMastershipTerm(did); |
| 745 | if (term != null && localNodeId.equals(term.master())) { |
| 746 | myNextRole = MASTER; |
| 747 | } else { |
| 748 | myNextRole = STANDBY; |
| 749 | } |
| 750 | } catch (InterruptedException e) { |
| 751 | Thread.currentThread().interrupt(); |
| 752 | log.error("Interrupted waiting for Mastership", e); |
| 753 | } catch (ExecutionException e) { |
| 754 | log.error("Encountered an error waiting for Mastership", e); |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 755 | } |
| 756 | } |
| 757 | |
| 758 | switch (myNextRole) { |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 759 | case MASTER: |
| 760 | final Device device = getDevice(did); |
| 761 | if ((device != null) && !isAvailable(did)) { |
helenyrwu | fd296b6 | 2016-06-22 17:43:02 -0700 | [diff] [blame] | 762 | store.markOnline(did); |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 763 | } |
| 764 | // TODO: should apply role only if there is mismatch |
| 765 | log.debug("Applying role {} to {}", myNextRole, did); |
| 766 | if (!applyRoleAndProbe(did, MASTER)) { |
| 767 | log.warn("Unsuccessful applying role {} to {}", myNextRole, did); |
| 768 | // immediately failed to apply role |
| 769 | mastershipService.relinquishMastership(did); |
| 770 | // FIXME disconnect? |
| 771 | } |
| 772 | break; |
| 773 | case STANDBY: |
| 774 | log.debug("Applying role {} to {}", myNextRole, did); |
| 775 | if (!applyRoleAndProbe(did, STANDBY)) { |
| 776 | log.warn("Unsuccessful applying role {} to {}", myNextRole, did); |
| 777 | // immediately failed to apply role |
| 778 | mastershipService.relinquishMastership(did); |
| 779 | // FIXME disconnect? |
| 780 | } |
| 781 | break; |
| 782 | case NONE: |
| 783 | default: |
| 784 | // should never reach here |
| 785 | log.error("You didn't see anything. I did not exist."); |
| 786 | break; |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 787 | } |
| 788 | } |
| 789 | |
Madan Jampani | 328371d | 2015-05-29 14:06:27 -0700 | [diff] [blame] | 790 | private void handleMastershipEvent(MastershipEvent event) { |
Jon Hall | 7a8bfc6 | 2016-05-26 17:59:04 -0700 | [diff] [blame] | 791 | if (event.type() == MastershipEvent.Type.BACKUPS_CHANGED) { |
Madan Jampani | 328371d | 2015-05-29 14:06:27 -0700 | [diff] [blame] | 792 | // Don't care if backup list changed. |
| 793 | return; |
| 794 | } |
Madan Jampani | 328371d | 2015-05-29 14:06:27 -0700 | [diff] [blame] | 795 | final DeviceId did = event.subject(); |
| 796 | |
| 797 | // myRole suggested by MastershipService |
| 798 | MastershipRole myNextRole; |
Jon Hall | 7a8bfc6 | 2016-05-26 17:59:04 -0700 | [diff] [blame] | 799 | if (event.type() == MastershipEvent.Type.SUSPENDED) { |
| 800 | myNextRole = NONE; // FIXME STANDBY OR NONE? |
| 801 | } else if (localNodeId.equals(event.roleInfo().master())) { |
Madan Jampani | 328371d | 2015-05-29 14:06:27 -0700 | [diff] [blame] | 802 | // confirm latest info |
| 803 | MastershipTerm term = termService.getMastershipTerm(did); |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 804 | final boolean iHaveControl = term != null && localNodeId.equals(term.master()); |
Madan Jampani | 328371d | 2015-05-29 14:06:27 -0700 | [diff] [blame] | 805 | if (iHaveControl) { |
Madan Jampani | 328371d | 2015-05-29 14:06:27 -0700 | [diff] [blame] | 806 | myNextRole = MASTER; |
| 807 | } else { |
| 808 | myNextRole = STANDBY; |
| 809 | } |
| 810 | } else if (event.roleInfo().backups().contains(localNodeId)) { |
| 811 | myNextRole = STANDBY; |
| 812 | } else { |
| 813 | myNextRole = NONE; |
| 814 | } |
| 815 | |
Madan Jampani | 328371d | 2015-05-29 14:06:27 -0700 | [diff] [blame] | 816 | final boolean isReachable = isReachable(did); |
| 817 | if (!isReachable) { |
| 818 | // device is not connected to this node |
Jon Hall | a90c44c | 2017-01-24 16:02:07 -0800 | [diff] [blame] | 819 | if (mastershipService.getLocalRole(did) == NONE) { |
| 820 | log.debug("Node was instructed to be {} role for {}, " |
Simon Hunt | ffbad3b | 2017-05-16 15:37:51 -0700 | [diff] [blame] | 821 | + "but this node cannot reach the device " |
| 822 | + "and role is already None. Ignoring request.", |
| 823 | myNextRole, did); |
Jon Hall | a90c44c | 2017-01-24 16:02:07 -0800 | [diff] [blame] | 824 | } else if (myNextRole != NONE) { |
Madan Jampani | 328371d | 2015-05-29 14:06:27 -0700 | [diff] [blame] | 825 | log.warn("Node was instructed to be {} role for {}, " |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 826 | + "but this node cannot reach the device. " |
| 827 | + "Relinquishing role. ", |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 828 | myNextRole, did); |
Madan Jampani | 328371d | 2015-05-29 14:06:27 -0700 | [diff] [blame] | 829 | mastershipService.relinquishMastership(did); |
| 830 | } |
| 831 | return; |
| 832 | } |
| 833 | |
| 834 | // device is connected to this node: |
| 835 | if (store.getDevice(did) != null) { |
| 836 | reassertRole(did, myNextRole); |
| 837 | } else { |
| 838 | log.debug("Device is not yet/no longer in the store: {}", did); |
| 839 | } |
| 840 | } |
| 841 | |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 842 | // Intercepts mastership events |
| 843 | private class InternalMastershipListener implements MastershipListener { |
| 844 | |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 845 | @Override |
| 846 | public void event(MastershipEvent event) { |
HIGUCHI Yuta | 060da9a | 2016-03-11 19:16:35 -0800 | [diff] [blame] | 847 | backgroundService.execute(() -> { |
Madan Jampani | 328371d | 2015-05-29 14:06:27 -0700 | [diff] [blame] | 848 | try { |
| 849 | handleMastershipEvent(event); |
| 850 | } catch (Exception e) { |
| 851 | log.warn("Failed to handle {}", event, e); |
Yuta HIGUCHI | d26354d | 2014-10-31 14:14:38 -0700 | [diff] [blame] | 852 | } |
Madan Jampani | 328371d | 2015-05-29 14:06:27 -0700 | [diff] [blame] | 853 | }); |
Ayaka Koshibe | 317245a | 2014-10-29 00:34:43 -0700 | [diff] [blame] | 854 | } |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 855 | } |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame] | 856 | |
| 857 | // Store delegate to re-post events emitted from the store. |
Thomas Vachuska | b17c41f | 2015-05-19 11:16:05 -0700 | [diff] [blame] | 858 | private class InternalStoreDelegate implements DeviceStoreDelegate { |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame] | 859 | @Override |
| 860 | public void notify(DeviceEvent event) { |
| 861 | post(event); |
| 862 | } |
| 863 | } |
samuel | 738dfaf | 2015-07-11 11:08:57 +0800 | [diff] [blame] | 864 | |
| 865 | @Override |
| 866 | public Iterable<Device> getDevices(Type type) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 867 | checkPermission(DEVICE_READ); |
samuel | 738dfaf | 2015-07-11 11:08:57 +0800 | [diff] [blame] | 868 | Set<Device> results = new HashSet<>(); |
| 869 | Iterable<Device> devices = store.getDevices(); |
| 870 | if (devices != null) { |
| 871 | devices.forEach(d -> { |
| 872 | if (type.equals(d.type())) { |
| 873 | results.add(d); |
| 874 | } |
| 875 | }); |
| 876 | } |
| 877 | return results; |
| 878 | } |
| 879 | |
| 880 | @Override |
| 881 | public Iterable<Device> getAvailableDevices(Type type) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 882 | checkPermission(DEVICE_READ); |
samuel | 738dfaf | 2015-07-11 11:08:57 +0800 | [diff] [blame] | 883 | Set<Device> results = new HashSet<>(); |
| 884 | Iterable<Device> availableDevices = store.getAvailableDevices(); |
| 885 | if (availableDevices != null) { |
| 886 | availableDevices.forEach(d -> { |
| 887 | if (type.equals(d.type())) { |
| 888 | results.add(d); |
| 889 | } |
| 890 | }); |
| 891 | } |
| 892 | return results; |
| 893 | } |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 894 | |
| 895 | private class InternalNetworkConfigListener implements NetworkConfigListener { |
| 896 | @Override |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 897 | public boolean isRelevant(NetworkConfigEvent event) { |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 898 | return (event.type() == NetworkConfigEvent.Type.CONFIG_ADDED |
| 899 | || event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED) |
| 900 | && (event.configClass().equals(BasicDeviceConfig.class) |
Simon Hunt | ffbad3b | 2017-05-16 15:37:51 -0700 | [diff] [blame] | 901 | || portOpsIndex.containsKey(event.configClass())); |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 902 | } |
| 903 | |
| 904 | @Override |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 905 | public void event(NetworkConfigEvent event) { |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 906 | DeviceEvent de = null; |
| 907 | if (event.configClass().equals(BasicDeviceConfig.class)) { |
Thomas Vachuska | 138de8b | 2016-01-11 21:31:38 -0800 | [diff] [blame] | 908 | log.debug("Detected device network config event {}", event.type()); |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 909 | DeviceId did = (DeviceId) event.subject(); |
Simon Hunt | ffbad3b | 2017-05-16 15:37:51 -0700 | [diff] [blame] | 910 | DeviceProvider dp = getProvider(did); |
| 911 | BasicDeviceConfig cfg = |
| 912 | networkConfigService.getConfig(did, BasicDeviceConfig.class); |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 913 | |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 914 | if (!isAllowed(cfg)) { |
| 915 | kickOutBadDevice(did); |
| 916 | } else { |
| 917 | Device dev = getDevice(did); |
Simon Hunt | ffbad3b | 2017-05-16 15:37:51 -0700 | [diff] [blame] | 918 | DeviceDescription desc = |
| 919 | (dev == null) ? null : BasicDeviceOperator.descriptionOf(dev); |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 920 | desc = BasicDeviceOperator.combine(cfg, desc); |
Simon Hunt | ffbad3b | 2017-05-16 15:37:51 -0700 | [diff] [blame] | 921 | if (desc != null && dp != null) { |
| 922 | de = store.createOrUpdateDevice(dp.id(), did, desc); |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 923 | } |
| 924 | } |
| 925 | } |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 926 | if (portOpsIndex.containsKey(event.configClass())) { |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 927 | ConnectPoint cpt = (ConnectPoint) event.subject(); |
| 928 | DeviceId did = cpt.deviceId(); |
Simon Hunt | ffbad3b | 2017-05-16 15:37:51 -0700 | [diff] [blame] | 929 | DeviceProvider dp = getProvider(did); |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 930 | |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 931 | // Note: assuming PortOperator can modify existing port, |
| 932 | // but cannot add new port purely from Config. |
Simon Hunt | ffbad3b | 2017-05-16 15:37:51 -0700 | [diff] [blame] | 933 | de = Optional.ofNullable(dp) |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 934 | .map(provider -> store.getPortDescription(provider.id(), did, cpt.port())) |
| 935 | .map(desc -> applyAllPortOps(cpt, desc)) |
Simon Hunt | ffbad3b | 2017-05-16 15:37:51 -0700 | [diff] [blame] | 936 | .map(desc -> store.updatePortStatus(dp.id(), did, desc)) |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 937 | .orElse(null); |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 938 | } |
| 939 | |
| 940 | if (de != null) { |
| 941 | post(de); |
| 942 | } |
| 943 | } |
| 944 | |
Simon Hunt | 8f60ff8 | 2017-04-24 17:19:30 -0700 | [diff] [blame] | 945 | // removes the specified device if it exists |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 946 | private void kickOutBadDevice(DeviceId deviceId) { |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 947 | Device badDevice = getDevice(deviceId); |
| 948 | if (badDevice != null) { |
| 949 | removeDevice(deviceId); |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 950 | } |
| 951 | } |
| 952 | } |
Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 953 | |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 954 | @Override |
| 955 | @SafeVarargs |
| 956 | public final void registerPortConfigOperator(PortConfigOperator portOp, |
Simon Hunt | ffbad3b | 2017-05-16 15:37:51 -0700 | [diff] [blame] | 957 | Class<? extends Config<ConnectPoint>>... configs) { |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 958 | checkNotNull(portOp); |
| 959 | |
| 960 | portOp.bindService(networkConfigService); |
| 961 | |
| 962 | // update both portOpsIndex and portOps |
| 963 | synchronized (portOpsIndex) { |
| 964 | for (Class<? extends Config<ConnectPoint>> config : configs) { |
| 965 | portOpsIndex.put(config, portOp); |
| 966 | } |
| 967 | |
| 968 | portOps.add(portOp); |
| 969 | } |
| 970 | |
| 971 | // TODO: Should we be applying to all existing Ports? |
| 972 | Tools.stream(store.getAvailableDevices()) |
Simon Hunt | ffbad3b | 2017-05-16 15:37:51 -0700 | [diff] [blame] | 973 | .map(Device::id) |
| 974 | .filter(mastershipService::isLocalMaster) |
| 975 | // for each locally managed Device, update all port descriptions |
| 976 | .map(did -> { |
| 977 | ProviderId pid = Optional.ofNullable(getProvider(did)) |
| 978 | .map(Provider::id) |
| 979 | .orElse(null); |
| 980 | if (pid == null) { |
| 981 | log.warn("Provider not found for {}", did); |
| 982 | return ImmutableList.<DeviceEvent>of(); |
| 983 | } |
| 984 | List<PortDescription> pds |
| 985 | = store.getPortDescriptions(pid, did) |
| 986 | .map(pdesc -> applyAllPortOps(did, pdesc)) |
| 987 | .collect(Collectors.toList()); |
| 988 | return store.updatePorts(pid, did, pds); |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 989 | }) |
Simon Hunt | ffbad3b | 2017-05-16 15:37:51 -0700 | [diff] [blame] | 990 | // ..and port port update event if necessary |
| 991 | .forEach(evts -> evts.forEach(this::post)); |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 992 | } |
| 993 | |
| 994 | @Override |
| 995 | public void unregisterPortConfigOperator(PortConfigOperator portOp) { |
| 996 | checkNotNull(portOp); |
| 997 | |
| 998 | |
| 999 | // remove all portOp |
| 1000 | synchronized (portOpsIndex) { |
| 1001 | portOps.remove(portOp); |
| 1002 | |
| 1003 | // had to do this since COWArrayList iterator doesn't support remove |
| 1004 | portOpsIndex.keySet().forEach(key -> portOpsIndex.remove(key, portOp)); |
| 1005 | } |
| 1006 | |
| 1007 | } |
| 1008 | |
| 1009 | /** |
| 1010 | * Merges the appropriate PortConfig with the description. |
| 1011 | * |
| 1012 | * @param did ID of the Device where the port is attached |
| 1013 | * @param desc {@link PortDescription} |
| 1014 | * @return merged {@link PortDescription} |
| 1015 | */ |
| 1016 | private PortDescription applyAllPortOps(DeviceId did, PortDescription desc) { |
| 1017 | return applyAllPortOps(new ConnectPoint(did, desc.portNumber()), desc); |
| 1018 | } |
| 1019 | |
| 1020 | /** |
| 1021 | * Merges the appropriate PortConfig with the description. |
| 1022 | * |
Simon Hunt | ffbad3b | 2017-05-16 15:37:51 -0700 | [diff] [blame] | 1023 | * @param cpt ConnectPoint where the port is attached |
| 1024 | * @param desc {@link PortDescription} |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 1025 | * @return merged {@link PortDescription} |
| 1026 | */ |
| 1027 | private PortDescription applyAllPortOps(ConnectPoint cpt, PortDescription desc) { |
| 1028 | PortDescription work = desc; |
| 1029 | for (PortConfigOperator portOp : portOps) { |
| 1030 | work = portOp.combine(cpt, work); |
| 1031 | } |
Yuta HIGUCHI | 7438f5a | 2017-02-15 22:09:46 -0800 | [diff] [blame] | 1032 | return portAnnotationOp.combine(cpt, work); |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 1033 | } |
| 1034 | |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 1035 | } |