Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
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; |
Carmelo Cascone | 1da7a4d | 2018-06-27 18:03:11 +0200 | [diff] [blame] | 19 | import com.google.common.collect.Lists; |
Simon Hunt | ffbad3b | 2017-05-16 15:37:51 -0700 | [diff] [blame] | 20 | import com.google.common.collect.Maps; |
| 21 | import com.google.common.collect.Multimap; |
pier | 388ec25 | 2020-04-15 20:53:14 +0200 | [diff] [blame] | 22 | import com.google.common.util.concurrent.Futures; |
Jonghwan Hyun | 6ecf56d | 2017-08-01 16:07:44 -0700 | [diff] [blame] | 23 | import org.onlab.util.KryoNamespace; |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 24 | import org.onlab.util.Tools; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 25 | import org.onosproject.cluster.ClusterService; |
pierventre | b2f636b | 2022-01-03 17:19:24 +0100 | [diff] [blame] | 26 | import org.onosproject.cluster.ControllerNode; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 27 | import org.onosproject.cluster.NodeId; |
pierventre | b2f636b | 2022-01-03 17:19:24 +0100 | [diff] [blame] | 28 | import org.onosproject.mastership.MastershipAdminService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 29 | import org.onosproject.mastership.MastershipEvent; |
| 30 | import org.onosproject.mastership.MastershipListener; |
| 31 | import org.onosproject.mastership.MastershipService; |
| 32 | import org.onosproject.mastership.MastershipTerm; |
| 33 | import org.onosproject.mastership.MastershipTermService; |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 34 | import org.onosproject.net.ConnectPoint; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 35 | import org.onosproject.net.Device; |
samuel | 738dfaf | 2015-07-11 11:08:57 +0800 | [diff] [blame] | 36 | import org.onosproject.net.Device.Type; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 37 | import org.onosproject.net.DeviceId; |
| 38 | import org.onosproject.net.MastershipRole; |
| 39 | import org.onosproject.net.Port; |
| 40 | import org.onosproject.net.PortNumber; |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 41 | import org.onosproject.net.config.Config; |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 42 | import org.onosproject.net.config.NetworkConfigEvent; |
| 43 | import org.onosproject.net.config.NetworkConfigListener; |
| 44 | import org.onosproject.net.config.NetworkConfigService; |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 45 | import org.onosproject.net.config.PortConfigOperator; |
| 46 | import org.onosproject.net.config.PortConfigOperatorRegistry; |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 47 | import org.onosproject.net.config.basics.BasicDeviceConfig; |
Palash Kala | a06a616 | 2017-11-15 20:42:40 +0900 | [diff] [blame] | 48 | import org.onosproject.net.config.basics.DeviceAnnotationConfig; |
Yuta HIGUCHI | 7438f5a | 2017-02-15 22:09:46 -0800 | [diff] [blame] | 49 | import org.onosproject.net.config.basics.PortAnnotationConfig; |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 50 | import org.onosproject.net.config.basics.PortDescriptionsConfig; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 51 | import org.onosproject.net.device.DefaultPortDescription; |
| 52 | import org.onosproject.net.device.DeviceAdminService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 53 | import org.onosproject.net.device.DeviceDescription; |
| 54 | import org.onosproject.net.device.DeviceEvent; |
| 55 | import org.onosproject.net.device.DeviceListener; |
| 56 | import org.onosproject.net.device.DeviceProvider; |
| 57 | import org.onosproject.net.device.DeviceProviderRegistry; |
| 58 | import org.onosproject.net.device.DeviceProviderService; |
| 59 | import org.onosproject.net.device.DeviceService; |
| 60 | import org.onosproject.net.device.DeviceStore; |
| 61 | import org.onosproject.net.device.DeviceStoreDelegate; |
| 62 | import org.onosproject.net.device.PortDescription; |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 63 | import org.onosproject.net.device.PortStatistics; |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 64 | import org.onosproject.net.provider.AbstractListenerProviderRegistry; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 65 | import org.onosproject.net.provider.AbstractProviderService; |
Yuta HIGUCHI | 3a2a987 | 2016-11-29 20:24:23 -0800 | [diff] [blame] | 66 | import org.onosproject.net.provider.Provider; |
| 67 | import org.onosproject.net.provider.ProviderId; |
Jonghwan Hyun | 6ecf56d | 2017-08-01 16:07:44 -0700 | [diff] [blame] | 68 | import org.onosproject.store.cluster.messaging.ClusterCommunicationService; |
| 69 | import org.onosproject.store.cluster.messaging.MessageSubject; |
| 70 | import org.onosproject.store.serializers.KryoNamespaces; |
| 71 | import org.onosproject.store.service.Serializer; |
Jordan Halterman | 9416aea | 2017-11-17 12:40:21 -0800 | [diff] [blame] | 72 | import org.onosproject.upgrade.UpgradeService; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 73 | import org.osgi.service.component.annotations.Activate; |
| 74 | import org.osgi.service.component.annotations.Component; |
| 75 | import org.osgi.service.component.annotations.Deactivate; |
| 76 | import org.osgi.service.component.annotations.Reference; |
| 77 | import org.osgi.service.component.annotations.ReferenceCardinality; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 78 | import org.slf4j.Logger; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 79 | |
Yuta HIGUCHI | 0c47d53 | 2017-08-18 23:16:35 -0700 | [diff] [blame] | 80 | import java.time.Instant; |
Simon Hunt | ffbad3b | 2017-05-16 15:37:51 -0700 | [diff] [blame] | 81 | import java.util.Collection; |
| 82 | import java.util.HashSet; |
| 83 | import java.util.List; |
| 84 | import java.util.Map; |
| 85 | import java.util.Objects; |
| 86 | import java.util.Optional; |
| 87 | import java.util.Set; |
| 88 | import java.util.concurrent.CompletableFuture; |
| 89 | import java.util.concurrent.ConcurrentHashMap; |
| 90 | import java.util.concurrent.CopyOnWriteArrayList; |
| 91 | import java.util.concurrent.ExecutionException; |
Jonghwan Hyun | 6ecf56d | 2017-08-01 16:07:44 -0700 | [diff] [blame] | 92 | import java.util.concurrent.ExecutorService; |
Simon Hunt | ffbad3b | 2017-05-16 15:37:51 -0700 | [diff] [blame] | 93 | import java.util.concurrent.ScheduledExecutorService; |
| 94 | import java.util.concurrent.TimeUnit; |
pierventre | 00ac250 | 2021-12-02 09:42:28 +0100 | [diff] [blame] | 95 | import java.util.concurrent.atomic.AtomicBoolean; |
Simon Hunt | ffbad3b | 2017-05-16 15:37:51 -0700 | [diff] [blame] | 96 | import java.util.stream.Collectors; |
Jonathan Hart | 2f66936 | 2015-02-11 16:19:20 -0800 | [diff] [blame] | 97 | |
Ray Milkey | 9ef2223 | 2016-07-14 12:42:37 -0700 | [diff] [blame] | 98 | import static com.google.common.base.Preconditions.checkNotNull; |
Jonghwan Hyun | 6ecf56d | 2017-08-01 16:07:44 -0700 | [diff] [blame] | 99 | import static com.google.common.base.Preconditions.checkState; |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 100 | import static com.google.common.collect.Multimaps.newListMultimap; |
| 101 | import static com.google.common.collect.Multimaps.synchronizedListMultimap; |
Jonghwan Hyun | 6ecf56d | 2017-08-01 16:07:44 -0700 | [diff] [blame] | 102 | import static java.util.concurrent.Executors.newSingleThreadExecutor; |
Ray Milkey | 9ef2223 | 2016-07-14 12:42:37 -0700 | [diff] [blame] | 103 | import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor; |
pierventre | 00ac250 | 2021-12-02 09:42:28 +0100 | [diff] [blame] | 104 | import static java.lang.System.currentTimeMillis; |
Ray Milkey | 9ef2223 | 2016-07-14 12:42:37 -0700 | [diff] [blame] | 105 | import static org.onlab.util.Tools.groupedThreads; |
| 106 | import static org.onosproject.net.MastershipRole.MASTER; |
| 107 | import static org.onosproject.net.MastershipRole.NONE; |
| 108 | import static org.onosproject.net.MastershipRole.STANDBY; |
Ray Milkey | 9ef2223 | 2016-07-14 12:42:37 -0700 | [diff] [blame] | 109 | import static org.onosproject.security.AppGuard.checkPermission; |
| 110 | import static org.onosproject.security.AppPermission.Type.DEVICE_READ; |
| 111 | import static org.slf4j.LoggerFactory.getLogger; |
| 112 | |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 113 | /** |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 114 | * Provides implementation of the device SB & NB APIs. |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 115 | */ |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 116 | @Component(immediate = true, |
| 117 | service = {DeviceService.class, DeviceAdminService.class, |
| 118 | DeviceProviderRegistry.class, PortConfigOperatorRegistry.class }) |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 119 | public class DeviceManager |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 120 | extends AbstractListenerProviderRegistry<DeviceEvent, DeviceListener, DeviceProvider, DeviceProviderService> |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 121 | implements DeviceService, DeviceAdminService, DeviceProviderRegistry, PortConfigOperatorRegistry { |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 122 | |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 123 | private static final String DEVICE_ID_NULL = "Device ID cannot be null"; |
| 124 | private static final String PORT_NUMBER_NULL = "Port number cannot be null"; |
| 125 | private static final String DEVICE_DESCRIPTION_NULL = "Device description cannot be null"; |
| 126 | private static final String PORT_DESCRIPTION_NULL = "Port description cannot be null"; |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 127 | private static final String PORT_DESC_LIST_NULL = "Port description list cannot be null"; |
Jonghwan Hyun | 6ecf56d | 2017-08-01 16:07:44 -0700 | [diff] [blame] | 128 | private static final String EVENT_NON_MASTER = "Non-master node cannot handle this event"; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 129 | |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 130 | private final Logger log = getLogger(getClass()); |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 131 | |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 132 | private final DeviceStoreDelegate delegate = new InternalStoreDelegate(); |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame] | 133 | |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 134 | private final MastershipListener mastershipListener = new InternalMastershipListener(); |
Madan Jampani | de003d9 | 2015-05-11 17:14:20 -0700 | [diff] [blame] | 135 | private NodeId localNodeId; |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 136 | |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 137 | private ScheduledExecutorService backgroundService; |
| 138 | |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 139 | private final NetworkConfigListener networkConfigListener = new InternalNetworkConfigListener(); |
| 140 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 141 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 142 | protected DeviceStore store; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 143 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 144 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 145 | protected ClusterService clusterService; |
| 146 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 147 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Ayaka Koshibe | a7f044e | 2014-09-23 16:56:20 -0700 | [diff] [blame] | 148 | protected MastershipService mastershipService; |
| 149 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 150 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
pierventre | b2f636b | 2022-01-03 17:19:24 +0100 | [diff] [blame] | 151 | protected MastershipAdminService mastershipAdminService; |
| 152 | |
| 153 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Ayaka Koshibe | 3de43ca | 2014-09-26 16:40:23 -0700 | [diff] [blame] | 154 | protected MastershipTermService termService; |
| 155 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 156 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jordan Halterman | 9416aea | 2017-11-17 12:40:21 -0800 | [diff] [blame] | 157 | protected UpgradeService upgradeService; |
| 158 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 159 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 160 | protected NetworkConfigService networkConfigService; |
| 161 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 162 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jonghwan Hyun | 6ecf56d | 2017-08-01 16:07:44 -0700 | [diff] [blame] | 163 | protected ClusterCommunicationService communicationService; |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 164 | |
pierventre | b2f636b | 2022-01-03 17:19:24 +0100 | [diff] [blame] | 165 | private ExecutorService clusterRequestExecutor; |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 166 | /** |
| 167 | * List of all registered PortConfigOperator. |
| 168 | */ |
| 169 | private final List<PortConfigOperator> portOps = new CopyOnWriteArrayList<>(); |
| 170 | |
| 171 | /** |
| 172 | * Index to look up PortConfigOperator from Config each PortConfigOperator uses. |
| 173 | */ |
| 174 | private final Multimap<Class<? extends Config<ConnectPoint>>, PortConfigOperator> portOpsIndex |
Simon Hunt | ffbad3b | 2017-05-16 15:37:51 -0700 | [diff] [blame] | 175 | = synchronizedListMultimap( |
| 176 | newListMultimap(new ConcurrentHashMap<>(), CopyOnWriteArrayList::new)); |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 177 | |
Yuta HIGUCHI | 7438f5a | 2017-02-15 22:09:46 -0800 | [diff] [blame] | 178 | // not part of portOps. must be executed at the end |
| 179 | private PortAnnotationOperator portAnnotationOp; |
Palash Kala | a06a616 | 2017-11-15 20:42:40 +0900 | [diff] [blame] | 180 | private DeviceAnnotationOperator deviceAnnotationOp; |
Yuta HIGUCHI | 7438f5a | 2017-02-15 22:09:46 -0800 | [diff] [blame] | 181 | |
Jonghwan Hyun | 6ecf56d | 2017-08-01 16:07:44 -0700 | [diff] [blame] | 182 | private static final MessageSubject PORT_UPDOWN_SUBJECT = |
| 183 | new MessageSubject("port-updown-req"); |
| 184 | |
pierventre | b2f636b | 2022-01-03 17:19:24 +0100 | [diff] [blame] | 185 | private static final MessageSubject PROBE_SUBJECT = |
| 186 | new MessageSubject("probe-req"); |
| 187 | private static final long PROBE_TIMEOUT_MILLIS = 5000; |
| 188 | private static final int PROBE_ATTEMPTS = 3; |
| 189 | |
Jonghwan Hyun | 6ecf56d | 2017-08-01 16:07:44 -0700 | [diff] [blame] | 190 | private static final Serializer SERIALIZER = Serializer.using( |
| 191 | KryoNamespace.newBuilder() |
| 192 | .register(KryoNamespaces.API) |
| 193 | .register(InternalPortUpDownEvent.class) |
| 194 | .nextId(KryoNamespaces.BEGIN_USER_CUSTOM_ID) |
| 195 | .build("DeviceManager")); |
| 196 | |
Saurav Das | d5ec9e9 | 2017-01-17 10:40:18 -0800 | [diff] [blame] | 197 | /** |
| 198 | * Local storage for connectivity status of devices. |
| 199 | */ |
| 200 | private class LocalStatus { |
| 201 | boolean connected; |
Yuta HIGUCHI | 0c47d53 | 2017-08-18 23:16:35 -0700 | [diff] [blame] | 202 | Instant dateTime; |
Saurav Das | d5ec9e9 | 2017-01-17 10:40:18 -0800 | [diff] [blame] | 203 | |
Yuta HIGUCHI | 0c47d53 | 2017-08-18 23:16:35 -0700 | [diff] [blame] | 204 | public LocalStatus(boolean b, Instant now) { |
Saurav Das | d5ec9e9 | 2017-01-17 10:40:18 -0800 | [diff] [blame] | 205 | connected = b; |
| 206 | dateTime = now; |
| 207 | } |
| 208 | } |
Simon Hunt | ffbad3b | 2017-05-16 15:37:51 -0700 | [diff] [blame] | 209 | |
Saurav Das | d5ec9e9 | 2017-01-17 10:40:18 -0800 | [diff] [blame] | 210 | private final Map<DeviceId, LocalStatus> deviceLocalStatus = |
| 211 | Maps.newConcurrentMap(); |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 212 | |
pierventre | 00ac250 | 2021-12-02 09:42:28 +0100 | [diff] [blame] | 213 | // To remember whether or not the role was acknowledged by the device |
| 214 | private final Map<DeviceId, Long> roleToAcknowledge = |
| 215 | Maps.newConcurrentMap(); |
| 216 | private ScheduledExecutorService backgroundRoleChecker; |
| 217 | private static final int ROLE_TIMEOUT_SECONDS = 10; |
| 218 | |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 219 | @Activate |
| 220 | public void activate() { |
Yuta HIGUCHI | 7438f5a | 2017-02-15 22:09:46 -0800 | [diff] [blame] | 221 | portAnnotationOp = new PortAnnotationOperator(networkConfigService); |
Palash Kala | a06a616 | 2017-11-15 20:42:40 +0900 | [diff] [blame] | 222 | deviceAnnotationOp = new DeviceAnnotationOperator(networkConfigService); |
Yuta HIGUCHI | 7438f5a | 2017-02-15 22:09:46 -0800 | [diff] [blame] | 223 | portOpsIndex.put(PortAnnotationConfig.class, portAnnotationOp); |
| 224 | |
HIGUCHI Yuta | 060da9a | 2016-03-11 19:16:35 -0800 | [diff] [blame] | 225 | backgroundService = newSingleThreadScheduledExecutor( |
Simon Hunt | ffbad3b | 2017-05-16 15:37:51 -0700 | [diff] [blame] | 226 | groupedThreads("onos/device", "manager-background", log)); |
Madan Jampani | de003d9 | 2015-05-11 17:14:20 -0700 | [diff] [blame] | 227 | localNodeId = clusterService.getLocalNode().id(); |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 228 | |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame] | 229 | store.setDelegate(delegate); |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 230 | eventDispatcher.addSink(DeviceEvent.class, listenerRegistry); |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 231 | mastershipService.addListener(mastershipListener); |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 232 | networkConfigService.addListener(networkConfigListener); |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 233 | |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 234 | backgroundService.scheduleWithFixedDelay(() -> { |
| 235 | try { |
| 236 | mastershipCheck(); |
| 237 | } catch (Exception e) { |
pierventre | 00ac250 | 2021-12-02 09:42:28 +0100 | [diff] [blame] | 238 | log.error("Exception thrown during mastership integrity check", e); |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 239 | } |
| 240 | }, 1, 1, TimeUnit.MINUTES); |
Jonghwan Hyun | 6ecf56d | 2017-08-01 16:07:44 -0700 | [diff] [blame] | 241 | |
pierventre | b2f636b | 2022-01-03 17:19:24 +0100 | [diff] [blame] | 242 | clusterRequestExecutor = newSingleThreadExecutor(); |
Jonghwan Hyun | 6ecf56d | 2017-08-01 16:07:44 -0700 | [diff] [blame] | 243 | |
pierventre | b2f636b | 2022-01-03 17:19:24 +0100 | [diff] [blame] | 244 | communicationService.addSubscriber( |
Jonghwan Hyun | 6ecf56d | 2017-08-01 16:07:44 -0700 | [diff] [blame] | 245 | PORT_UPDOWN_SUBJECT, |
| 246 | SERIALIZER::decode, |
| 247 | this::handlePortRequest, |
pierventre | b2f636b | 2022-01-03 17:19:24 +0100 | [diff] [blame] | 248 | clusterRequestExecutor); |
| 249 | |
| 250 | communicationService.addSubscriber( |
| 251 | PROBE_SUBJECT, |
| 252 | SERIALIZER::decode, |
| 253 | this::handleProbeRequest, |
| 254 | SERIALIZER::encode, |
| 255 | clusterRequestExecutor); |
pierventre | 00ac250 | 2021-12-02 09:42:28 +0100 | [diff] [blame] | 256 | |
| 257 | backgroundRoleChecker = newSingleThreadScheduledExecutor( |
| 258 | groupedThreads("onos/device", "manager-role", log)); |
| 259 | backgroundRoleChecker.scheduleAtFixedRate(() -> { |
| 260 | try { |
| 261 | roleCheck(); |
| 262 | } catch (Exception e) { |
| 263 | log.error("Exception thrown while verifying role acknowledgement from all devices", e); |
| 264 | } |
| 265 | }, 0, 10, TimeUnit.SECONDS); |
Jonghwan Hyun | 6ecf56d | 2017-08-01 16:07:44 -0700 | [diff] [blame] | 266 | |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 267 | log.info("Started"); |
| 268 | } |
| 269 | |
| 270 | @Deactivate |
| 271 | public void deactivate() { |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 272 | backgroundService.shutdown(); |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 273 | networkConfigService.removeListener(networkConfigListener); |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame] | 274 | store.unsetDelegate(delegate); |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 275 | mastershipService.removeListener(mastershipListener); |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 276 | eventDispatcher.removeSink(DeviceEvent.class); |
Jonghwan Hyun | 6ecf56d | 2017-08-01 16:07:44 -0700 | [diff] [blame] | 277 | communicationService.removeSubscriber(PORT_UPDOWN_SUBJECT); |
pierventre | b2f636b | 2022-01-03 17:19:24 +0100 | [diff] [blame] | 278 | clusterRequestExecutor.shutdown(); |
pierventre | 00ac250 | 2021-12-02 09:42:28 +0100 | [diff] [blame] | 279 | backgroundRoleChecker.shutdown(); |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 280 | log.info("Stopped"); |
| 281 | } |
| 282 | |
| 283 | @Override |
tom | ad2d209 | 2014-09-06 23:24:20 -0700 | [diff] [blame] | 284 | public int getDeviceCount() { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 285 | checkPermission(DEVICE_READ); |
tom | ad2d209 | 2014-09-06 23:24:20 -0700 | [diff] [blame] | 286 | return store.getDeviceCount(); |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | @Override |
mskala | 32000d3 | 2017-07-14 16:27:06 +0200 | [diff] [blame] | 290 | public int getAvailableDeviceCount() { |
| 291 | checkPermission(DEVICE_READ); |
| 292 | return store.getAvailableDeviceCount(); |
| 293 | } |
| 294 | |
| 295 | @Override |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 296 | public Iterable<Device> getDevices() { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 297 | checkPermission(DEVICE_READ); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 298 | return store.getDevices(); |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 299 | } |
| 300 | |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 301 | @Override |
Yuta HIGUCHI | f1f2ac0 | 2014-11-26 14:02:22 -0800 | [diff] [blame] | 302 | public Iterable<Device> getAvailableDevices() { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 303 | checkPermission(DEVICE_READ); |
Yuta HIGUCHI | f1f2ac0 | 2014-11-26 14:02:22 -0800 | [diff] [blame] | 304 | return store.getAvailableDevices(); |
| 305 | } |
| 306 | |
| 307 | @Override |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 308 | public Device getDevice(DeviceId deviceId) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 309 | checkPermission(DEVICE_READ); |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 310 | checkNotNull(deviceId, DEVICE_ID_NULL); |
tom | 132b58a | 2014-08-28 16:11:28 -0700 | [diff] [blame] | 311 | return store.getDevice(deviceId); |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | @Override |
tom | ad2d209 | 2014-09-06 23:24:20 -0700 | [diff] [blame] | 315 | public MastershipRole getRole(DeviceId deviceId) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 316 | checkPermission(DEVICE_READ); |
tom | ad2d209 | 2014-09-06 23:24:20 -0700 | [diff] [blame] | 317 | checkNotNull(deviceId, DEVICE_ID_NULL); |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 318 | return mastershipService.getLocalRole(deviceId); |
tom | ad2d209 | 2014-09-06 23:24:20 -0700 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | @Override |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 322 | public List<Port> getPorts(DeviceId deviceId) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 323 | checkPermission(DEVICE_READ); |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 324 | checkNotNull(deviceId, DEVICE_ID_NULL); |
tom | 132b58a | 2014-08-28 16:11:28 -0700 | [diff] [blame] | 325 | return store.getPorts(deviceId); |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | @Override |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 329 | public List<PortStatistics> getPortStatistics(DeviceId deviceId) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 330 | checkPermission(DEVICE_READ); |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 331 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 332 | return store.getPortStatistics(deviceId); |
| 333 | } |
| 334 | |
| 335 | @Override |
Dusan Pajin | 11ff4a8 | 2015-08-20 18:03:05 +0200 | [diff] [blame] | 336 | public List<PortStatistics> getPortDeltaStatistics(DeviceId deviceId) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 337 | checkPermission(DEVICE_READ); |
Dusan Pajin | 11ff4a8 | 2015-08-20 18:03:05 +0200 | [diff] [blame] | 338 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 339 | return store.getPortDeltaStatistics(deviceId); |
| 340 | } |
| 341 | |
| 342 | @Override |
Viswanath KSP | 22774cd | 2016-08-20 20:06:30 +0530 | [diff] [blame] | 343 | public PortStatistics getStatisticsForPort(DeviceId deviceId, PortNumber portNumber) { |
| 344 | checkPermission(DEVICE_READ); |
| 345 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 346 | checkNotNull(portNumber, PORT_NUMBER_NULL); |
| 347 | return store.getStatisticsForPort(deviceId, portNumber); |
| 348 | } |
| 349 | |
| 350 | @Override |
| 351 | public PortStatistics getDeltaStatisticsForPort(DeviceId deviceId, PortNumber portNumber) { |
| 352 | checkPermission(DEVICE_READ); |
| 353 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 354 | checkNotNull(portNumber, PORT_NUMBER_NULL); |
| 355 | return store.getDeltaStatisticsForPort(deviceId, portNumber); |
| 356 | } |
| 357 | |
| 358 | @Override |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 359 | public Port getPort(DeviceId deviceId, PortNumber portNumber) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 360 | checkPermission(DEVICE_READ); |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 361 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 362 | checkNotNull(portNumber, PORT_NUMBER_NULL); |
tom | 132b58a | 2014-08-28 16:11:28 -0700 | [diff] [blame] | 363 | return store.getPort(deviceId, portNumber); |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | @Override |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 367 | public boolean isAvailable(DeviceId deviceId) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 368 | checkPermission(DEVICE_READ); |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 369 | |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 370 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 371 | return store.isAvailable(deviceId); |
| 372 | } |
| 373 | |
Saurav Das | d5ec9e9 | 2017-01-17 10:40:18 -0800 | [diff] [blame] | 374 | @Override |
| 375 | public String localStatus(DeviceId deviceId) { |
| 376 | LocalStatus ls = deviceLocalStatus.get(deviceId); |
| 377 | if (ls == null) { |
| 378 | return "No Record"; |
| 379 | } |
Yuta HIGUCHI | 0c47d53 | 2017-08-18 23:16:35 -0700 | [diff] [blame] | 380 | String timeAgo = Tools.timeAgo(ls.dateTime.toEpochMilli()); |
Saurav Das | d5ec9e9 | 2017-01-17 10:40:18 -0800 | [diff] [blame] | 381 | return (ls.connected) ? "connected " + timeAgo : "disconnected " + timeAgo; |
| 382 | } |
| 383 | |
Palash Kala | 0d817b0 | 2018-03-23 18:09:45 +0900 | [diff] [blame] | 384 | private boolean isLocallyConnected(DeviceId deviceId) { |
| 385 | LocalStatus ls = deviceLocalStatus.get(deviceId); |
| 386 | if (ls == null) { |
| 387 | return false; |
| 388 | } |
| 389 | return ls.connected; |
| 390 | } |
| 391 | |
Ray Milkey | 054e23d | 2018-03-22 13:37:11 -0700 | [diff] [blame] | 392 | @Override |
| 393 | public long getLastUpdatedInstant(DeviceId deviceId) { |
| 394 | LocalStatus ls = deviceLocalStatus.get(deviceId); |
| 395 | if (ls == null) { |
| 396 | return 0; |
| 397 | } |
| 398 | return ls.dateTime.toEpochMilli(); |
| 399 | } |
| 400 | |
pierventre | eed482b | 2022-02-18 05:16:48 -0800 | [diff] [blame^] | 401 | // Returns true if the grace period is still on |
| 402 | private boolean inGracePeriod(DeviceId deviceId) { |
| 403 | LocalStatus ls = deviceLocalStatus.get(deviceId); |
| 404 | if (ls == null) { |
| 405 | // This should not be possible, unless the device is removed |
| 406 | log.warn("Not found a recent local status for {}", deviceId); |
| 407 | return true; |
| 408 | } |
| 409 | DeviceProvider provider = getProvider(deviceId); |
| 410 | return ls.connected && |
| 411 | (Instant.now().toEpochMilli() - ls.dateTime.toEpochMilli()) < provider.gracePeriod(); |
| 412 | } |
| 413 | |
Palash Kala | 0d817b0 | 2018-03-23 18:09:45 +0900 | [diff] [blame] | 414 | // Check a device for control channel connectivity |
| 415 | // and changes local-status appropriately. |
pierventre | eed482b | 2022-02-18 05:16:48 -0800 | [diff] [blame^] | 416 | private boolean isReachable(DeviceId deviceId, boolean inGracePeriod) { |
Ayaka Koshibe | 78bcbc1 | 2014-11-19 14:28:58 -0800 | [diff] [blame] | 417 | if (deviceId == null) { |
| 418 | return false; |
| 419 | } |
Yuta HIGUCHI | 5481532 | 2014-10-31 23:17:08 -0700 | [diff] [blame] | 420 | DeviceProvider provider = getProvider(deviceId); |
| 421 | if (provider != null) { |
pierventre | b2f636b | 2022-01-03 17:19:24 +0100 | [diff] [blame] | 422 | boolean reachable = probeReachability(deviceId); |
Palash Kala | 0d817b0 | 2018-03-23 18:09:45 +0900 | [diff] [blame] | 423 | if (reachable && !isLocallyConnected(deviceId)) { |
| 424 | deviceLocalStatus.put(deviceId, new LocalStatus(true, Instant.now())); |
pierventre | eed482b | 2022-02-18 05:16:48 -0800 | [diff] [blame^] | 425 | } else if (!reachable && isLocallyConnected(deviceId) && !inGracePeriod) { |
| 426 | // Do not update if the grace period is still on |
Palash Kala | 0d817b0 | 2018-03-23 18:09:45 +0900 | [diff] [blame] | 427 | deviceLocalStatus.put(deviceId, new LocalStatus(false, Instant.now())); |
| 428 | } |
| 429 | return reachable; |
Yuta HIGUCHI | 5481532 | 2014-10-31 23:17:08 -0700 | [diff] [blame] | 430 | } else { |
Yuta HIGUCHI | 72669c4 | 2014-11-13 14:48:17 -0800 | [diff] [blame] | 431 | log.debug("Provider not found for {}", deviceId); |
Ayaka Koshibe | e60d452 | 2014-10-28 15:07:00 -0700 | [diff] [blame] | 432 | return false; |
| 433 | } |
Ayaka Koshibe | e8708e3 | 2014-10-22 13:40:18 -0700 | [diff] [blame] | 434 | } |
| 435 | |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 436 | @Override |
| 437 | public void removeDevice(DeviceId deviceId) { |
| 438 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 439 | DeviceEvent event = store.removeDevice(deviceId); |
tom | 0efbb1d | 2014-09-09 11:54:28 -0700 | [diff] [blame] | 440 | if (event != null) { |
| 441 | log.info("Device {} administratively removed", deviceId); |
tom | 0efbb1d | 2014-09-09 11:54:28 -0700 | [diff] [blame] | 442 | } |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 443 | } |
| 444 | |
Thomas Vachuska | 811ea2b | 2020-02-11 10:20:10 -0800 | [diff] [blame] | 445 | @Override |
| 446 | public void removeDevicePorts(DeviceId deviceId) { |
| 447 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 448 | if (isAvailable(deviceId)) { |
| 449 | log.debug("Cannot remove ports of device {} while it is available.", deviceId); |
| 450 | return; |
| 451 | } |
| 452 | |
| 453 | List<PortDescription> portDescriptions = ImmutableList.of(); |
| 454 | List<DeviceEvent> events = store.updatePorts(getProvider(deviceId).id(), |
| 455 | deviceId, portDescriptions); |
| 456 | if (events != null) { |
| 457 | for (DeviceEvent event : events) { |
| 458 | post(event); |
| 459 | } |
| 460 | } |
| 461 | } |
| 462 | |
Jonghwan Hyun | 6ecf56d | 2017-08-01 16:07:44 -0700 | [diff] [blame] | 463 | private void handlePortRequest(InternalPortUpDownEvent event) { |
| 464 | DeviceId deviceId = event.deviceId(); |
Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 465 | checkNotNull(deviceId, DEVICE_ID_NULL); |
Sean Condon | 0bd777c | 2021-01-01 14:23:29 +0000 | [diff] [blame] | 466 | checkNotNull(event.portNumber(), PORT_NUMBER_NULL); |
Jonghwan Hyun | 6ecf56d | 2017-08-01 16:07:44 -0700 | [diff] [blame] | 467 | checkState(mastershipService.isLocalMaster(deviceId), EVENT_NON_MASTER); |
| 468 | changePortStateAtMaster(event.deviceId(), event.portNumber(), event.isEnable()); |
| 469 | } |
| 470 | |
| 471 | private void changePortStateAtMaster(DeviceId deviceId, PortNumber portNumber, |
| 472 | boolean enable) { |
Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 473 | DeviceProvider provider = getProvider(deviceId); |
| 474 | if (provider != null) { |
Jonghwan Hyun | 6ecf56d | 2017-08-01 16:07:44 -0700 | [diff] [blame] | 475 | log.info("Port {} on device {} being administratively brought {}", |
Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 476 | portNumber, deviceId, |
| 477 | (enable) ? "UP" : "DOWN"); |
| 478 | provider.changePortState(deviceId, portNumber, enable); |
| 479 | } else { |
| 480 | log.warn("Provider not found for {}", deviceId); |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | @Override |
Jonghwan Hyun | 6ecf56d | 2017-08-01 16:07:44 -0700 | [diff] [blame] | 485 | public void changePortState(DeviceId deviceId, PortNumber portNumber, |
| 486 | boolean enable) { |
| 487 | checkNotNull(deviceId, DEVICE_ID_NULL); |
Sean Condon | 0bd777c | 2021-01-01 14:23:29 +0000 | [diff] [blame] | 488 | checkNotNull(portNumber, PORT_NUMBER_NULL); |
Jonghwan Hyun | 6ecf56d | 2017-08-01 16:07:44 -0700 | [diff] [blame] | 489 | NodeId masterId = mastershipService.getMasterFor(deviceId); |
| 490 | |
Ray Milkey | 4ef245e | 2018-05-10 15:41:16 -0700 | [diff] [blame] | 491 | if (masterId == null) { |
| 492 | // No master found; device is offline |
| 493 | log.info("No master found for port state change for {}", deviceId); |
| 494 | return; |
| 495 | } |
| 496 | |
Jonghwan Hyun | 6ecf56d | 2017-08-01 16:07:44 -0700 | [diff] [blame] | 497 | if (!masterId.equals(localNodeId)) { |
| 498 | //Send the request to the master node for the device |
| 499 | log.info("Device {} is managed by {}, forwarding the request to the MASTER", |
| 500 | deviceId, masterId); |
| 501 | communicationService.unicast( |
| 502 | new InternalPortUpDownEvent(deviceId, portNumber, enable), |
| 503 | PORT_UPDOWN_SUBJECT, |
| 504 | SERIALIZER::encode, |
| 505 | masterId).whenComplete((r, error) -> { |
| 506 | if (error != null) { |
| 507 | log.warn("Failed to send packet-updown-req to {}", masterId, error); |
| 508 | } |
| 509 | }); |
| 510 | } else { |
| 511 | changePortStateAtMaster(deviceId, portNumber, enable); |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | @Override |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 516 | protected DeviceProviderService createProviderService( |
| 517 | DeviceProvider provider) { |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 518 | return new InternalDeviceProviderService(provider); |
| 519 | } |
| 520 | |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 521 | /** |
| 522 | * Checks if all the reachable devices have a valid mastership role. |
| 523 | */ |
| 524 | private void mastershipCheck() { |
| 525 | log.debug("Checking mastership"); |
| 526 | for (Device device : getDevices()) { |
| 527 | final DeviceId deviceId = device.id(); |
Ray Milkey | c710467 | 2016-08-31 12:04:34 -0700 | [diff] [blame] | 528 | MastershipRole myRole = mastershipService.getLocalRole(deviceId); |
| 529 | log.trace("Checking device {}. Current role is {}", deviceId, myRole); |
pierventre | eed482b | 2022-02-18 05:16:48 -0800 | [diff] [blame^] | 530 | |
| 531 | log.debug("Device {} local status is {}", deviceId, localStatus(deviceId)); |
| 532 | final boolean isGracePeriodOn = inGracePeriod(deviceId); |
| 533 | final boolean isReachable = isReachable(deviceId, isGracePeriodOn); |
| 534 | // Passed the grace period and it is still not reachable |
| 535 | if (!isGracePeriodOn && !isReachable) { |
Ray Milkey | c710467 | 2016-08-31 12:04:34 -0700 | [diff] [blame] | 536 | if (myRole != NONE) { |
pierventre | b2f636b | 2022-01-03 17:19:24 +0100 | [diff] [blame] | 537 | // Verify if the device is fully disconnected from the cluster |
| 538 | if (updateMastershipFor(deviceId) == null |
| 539 | && myRole == MASTER && isAvailable(deviceId)) { |
| 540 | log.info("Local Role {}, Marking unreachable device {} offline", MASTER, deviceId); |
| 541 | post(store.markOffline(deviceId)); |
helenyrwu | fd296b6 | 2016-06-22 17:43:02 -0700 | [diff] [blame] | 542 | } |
Bharath Thiruveedula | 651a7da | 2016-12-13 02:52:50 +0530 | [diff] [blame] | 543 | } else { |
pierventre | eed482b | 2022-02-18 05:16:48 -0800 | [diff] [blame^] | 544 | // Firstly get a role and then check if the device is available in the store. |
| 545 | // if it is, if this node is the master and the device is fully disconnected |
| 546 | // from the cluster mark the device offline. In principle, this condition should |
| 547 | // never be hit unless in a device removed phase for NONE mastership roles. |
pierventre | b2f636b | 2022-01-03 17:19:24 +0100 | [diff] [blame] | 548 | try { |
| 549 | mastershipService.requestRoleFor(deviceId).get(); |
| 550 | } catch (InterruptedException e) { |
| 551 | Thread.currentThread().interrupt(); |
| 552 | log.error("Interrupted waiting for Mastership", e); |
| 553 | } catch (ExecutionException e) { |
| 554 | log.error("Encountered an error waiting for Mastership", e); |
| 555 | } |
| 556 | |
| 557 | MastershipTerm term = termService.getMastershipTerm(deviceId); |
| 558 | if (updateMastershipFor(deviceId) == null && |
| 559 | term != null && localNodeId.equals(term.master()) && |
| 560 | isAvailable(deviceId)) { |
| 561 | log.info("Marking unreachable device {} offline", deviceId); |
| 562 | post(store.markOffline(deviceId)); |
Jordan Halterman | bc0308f | 2017-11-29 15:37:34 -0800 | [diff] [blame] | 563 | } |
helenyrwu | fd296b6 | 2016-06-22 17:43:02 -0700 | [diff] [blame] | 564 | } |
pierventre | b2f636b | 2022-01-03 17:19:24 +0100 | [diff] [blame] | 565 | roleToAcknowledge.remove(deviceId); |
pierventre | eed482b | 2022-02-18 05:16:48 -0800 | [diff] [blame^] | 566 | } else if (isReachable) { |
| 567 | // If this node is the master, ensure the device is marked online. |
| 568 | if (myRole == MASTER && canMarkOnline(device)) { |
| 569 | log.info("Can mark online {}", deviceId); |
| 570 | post(store.markOnline(deviceId)); |
| 571 | } |
| 572 | |
| 573 | log.info("{} is reachable - reasserting the role", deviceId); |
| 574 | |
| 575 | // Device is still reachable. It is useful for some protocols |
| 576 | // to reassert the role. Note: NONE triggers request to MastershipService |
| 577 | reassertRole(deviceId, myRole); |
| 578 | } else { |
| 579 | // Do not proceed furthermore if the grace period is still on |
| 580 | log.debug("Skipping mastership check for {}", deviceId); |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 581 | } |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 582 | } |
| 583 | } |
| 584 | |
pierventre | 00ac250 | 2021-12-02 09:42:28 +0100 | [diff] [blame] | 585 | /** |
| 586 | * Checks if all the devices have acknowledged the mastership role. |
| 587 | */ |
| 588 | private void roleCheck() { |
| 589 | log.debug("Checking role"); |
| 590 | for (Device device : getDevices()) { |
| 591 | final DeviceId deviceId = device.id(); |
| 592 | MastershipRole myRole = mastershipService.getLocalRole(deviceId); |
| 593 | log.trace("Checking device {}. Current role is {}", deviceId, myRole); |
| 594 | final AtomicBoolean exists = new AtomicBoolean(false); |
| 595 | final Long ts = roleToAcknowledge.compute(deviceId, (key, value) -> { |
| 596 | if (value == null) { |
| 597 | return null; |
| 598 | } |
| 599 | exists.set(true); |
| 600 | if (currentTimeMillis() - value < (ROLE_TIMEOUT_SECONDS * 1000)) { |
| 601 | return value; |
| 602 | } |
| 603 | return null; |
| 604 | }); |
| 605 | // Nobody applied the role recently |
| 606 | if (!exists.get()) { |
| 607 | log.trace("Role was not applied or it has been acknowledged for device {}", deviceId); |
| 608 | continue; |
| 609 | } |
| 610 | // Timeout still on |
| 611 | if (ts != null) { |
| 612 | log.debug("Timeout expires in {} ms", ((ROLE_TIMEOUT_SECONDS * 1000) - currentTimeMillis() + ts)); |
| 613 | continue; |
| 614 | } |
| 615 | if (myRole != MASTER) { |
| 616 | log.debug("Timeout is expired but current role is not MASTER ({}), nothing to do", myRole); |
| 617 | continue; |
| 618 | } |
pierventre | eed482b | 2022-02-18 05:16:48 -0800 | [diff] [blame^] | 619 | // Switch failed to acknowledge master role we asked for. |
| 620 | // Yield mastership to other instance |
pierventre | 00ac250 | 2021-12-02 09:42:28 +0100 | [diff] [blame] | 621 | log.warn("Failed to assert role onto device {}. requested={}, no response", |
| 622 | deviceId, myRole); |
pierventre | b2f636b | 2022-01-03 17:19:24 +0100 | [diff] [blame] | 623 | updateMastershipFor(deviceId); |
pierventre | 00ac250 | 2021-12-02 09:42:28 +0100 | [diff] [blame] | 624 | } |
| 625 | } |
| 626 | |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 627 | // Personalized device provider service issued to the supplied provider. |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 628 | private class InternalDeviceProviderService |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 629 | extends AbstractProviderService<DeviceProvider> |
| 630 | implements DeviceProviderService { |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 631 | |
tom | cfde062 | 2014-09-09 11:02:42 -0700 | [diff] [blame] | 632 | InternalDeviceProviderService(DeviceProvider provider) { |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 633 | super(provider); |
| 634 | } |
| 635 | |
Ray Milkey | 33306ba | 2018-09-20 13:27:25 -0700 | [diff] [blame] | 636 | /** |
| 637 | * Apply role in reaction to provider event. |
| 638 | * |
| 639 | * @param deviceId device identifier |
| 640 | * @param newRole new role to apply to the device |
| 641 | * @return true if the request was sent to provider |
| 642 | */ |
| 643 | private boolean applyRole(DeviceId deviceId, MastershipRole newRole) { |
| 644 | |
| 645 | if (newRole.equals(MastershipRole.NONE)) { |
| 646 | //no-op |
| 647 | return true; |
| 648 | } |
| 649 | |
| 650 | DeviceProvider provider = provider(); |
| 651 | if (provider == null) { |
| 652 | log.warn("Provider for {} was not found. Cannot apply role {}", |
| 653 | deviceId, newRole); |
| 654 | return false; |
| 655 | } |
pierventre | 00ac250 | 2021-12-02 09:42:28 +0100 | [diff] [blame] | 656 | // Start the timer |
| 657 | roleToAcknowledge.put(deviceId, currentTimeMillis()); |
Ray Milkey | 33306ba | 2018-09-20 13:27:25 -0700 | [diff] [blame] | 658 | provider.roleChanged(deviceId, newRole); |
| 659 | // not triggering probe when triggered by provider service event |
| 660 | return true; |
| 661 | } |
| 662 | |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 663 | @Override |
alshabib | b7b4063 | 2014-09-28 21:30:00 -0700 | [diff] [blame] | 664 | public void deviceConnected(DeviceId deviceId, |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 665 | DeviceDescription deviceDescription) { |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 666 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 667 | checkNotNull(deviceDescription, DEVICE_DESCRIPTION_NULL); |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 668 | checkValidity(); |
Yuta HIGUCHI | 24b2e2a | 2014-10-07 15:53:57 -0700 | [diff] [blame] | 669 | |
Yuta HIGUCHI | 0c47d53 | 2017-08-18 23:16:35 -0700 | [diff] [blame] | 670 | deviceLocalStatus.put(deviceId, new LocalStatus(true, Instant.now())); |
Saurav Das | d5ec9e9 | 2017-01-17 10:40:18 -0800 | [diff] [blame] | 671 | |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 672 | BasicDeviceConfig cfg = networkConfigService.getConfig(deviceId, BasicDeviceConfig.class); |
| 673 | if (!isAllowed(cfg)) { |
| 674 | log.warn("Device {} is not allowed", deviceId); |
| 675 | return; |
| 676 | } |
Andrea Campanella | 75ef9f5 | 2017-07-27 20:14:32 +0200 | [diff] [blame] | 677 | PortDescriptionsConfig portConfig = networkConfigService.getConfig(deviceId, PortDescriptionsConfig.class); |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 678 | // Generate updated description and establish my Role |
| 679 | deviceDescription = BasicDeviceOperator.combine(cfg, deviceDescription); |
jaegonkim | 1c1f0c2 | 2018-01-28 22:20:42 +0900 | [diff] [blame] | 680 | DeviceAnnotationConfig annoConfig = networkConfigService.getConfig(deviceId, DeviceAnnotationConfig.class); |
| 681 | if (annoConfig != null) { |
| 682 | deviceDescription = deviceAnnotationOp.combine(deviceId, deviceDescription, Optional.of(annoConfig)); |
| 683 | } |
| 684 | |
pier | 388ec25 | 2020-04-15 20:53:14 +0200 | [diff] [blame] | 685 | // Wait for the end of the election. sync call of requestRoleFor |
| 686 | // wait only 3s and it is not entirely safe since the leadership |
| 687 | // election timer can be higher. |
| 688 | MastershipRole role = Futures.getUnchecked(mastershipService.requestRoleFor(deviceId)); |
jaegonkim | 8eabcec | 2018-07-27 23:40:43 +0900 | [diff] [blame] | 689 | log.info("Local role is {} for {}", role, deviceId); |
pier | 388ec25 | 2020-04-15 20:53:14 +0200 | [diff] [blame] | 690 | store.createOrUpdateDevice(provider().id(), deviceId, deviceDescription); |
Ray Milkey | 33306ba | 2018-09-20 13:27:25 -0700 | [diff] [blame] | 691 | applyRole(deviceId, role); |
HIGUCHI Yuta | 11530fb | 2015-05-27 13:10:20 -0700 | [diff] [blame] | 692 | |
Andrea Campanella | 75ef9f5 | 2017-07-27 20:14:32 +0200 | [diff] [blame] | 693 | if (portConfig != null) { |
| 694 | //updating the ports if configration exists |
| 695 | List<PortDescription> complete = store.getPortDescriptions(provider().id(), deviceId) |
| 696 | .collect(Collectors.toList()); |
| 697 | complete.addAll(portConfig.portDescriptions()); |
| 698 | List<PortDescription> portDescriptions = complete.stream() |
| 699 | .map(e -> applyAllPortOps(deviceId, e)) |
| 700 | .collect(Collectors.toList()); |
| 701 | store.updatePorts(provider().id(), deviceId, portDescriptions); |
| 702 | } |
| 703 | |
helenyrwu | fd296b6 | 2016-06-22 17:43:02 -0700 | [diff] [blame] | 704 | if (deviceDescription.isDefaultAvailable()) { |
| 705 | log.info("Device {} connected", deviceId); |
| 706 | } else { |
| 707 | log.info("Device {} registered", deviceId); |
| 708 | } |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 709 | } |
| 710 | |
Yuta HIGUCHI | 6eb00cc | 2016-06-10 11:55:12 -0700 | [diff] [blame] | 711 | private PortDescription ensurePortEnabledState(PortDescription desc, boolean enabled) { |
| 712 | if (desc.isEnabled() != enabled) { |
Yuta HIGUCHI | 2b1935d | 2018-03-01 21:41:06 -0800 | [diff] [blame] | 713 | return DefaultPortDescription.builder(desc) |
| 714 | .isEnabled(enabled) |
| 715 | .build(); |
Yuta HIGUCHI | 6eb00cc | 2016-06-10 11:55:12 -0700 | [diff] [blame] | 716 | } |
| 717 | return desc; |
| 718 | } |
| 719 | |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 720 | @Override |
| 721 | public void deviceDisconnected(DeviceId deviceId) { |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 722 | checkNotNull(deviceId, DEVICE_ID_NULL); |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 723 | checkValidity(); |
pierventre | b2f636b | 2022-01-03 17:19:24 +0100 | [diff] [blame] | 724 | // Update the local status |
Yuta HIGUCHI | 0c47d53 | 2017-08-18 23:16:35 -0700 | [diff] [blame] | 725 | deviceLocalStatus.put(deviceId, new LocalStatus(false, Instant.now())); |
Andrea Campanella | b0b93ac | 2021-09-13 12:37:36 +0200 | [diff] [blame] | 726 | log.info("Device {} disconnected from this node: {}", deviceId, |
| 727 | clusterService.getLocalNode().id()); |
Ayaka Koshibe | d9f693e | 2014-09-29 18:04:54 -0700 | [diff] [blame] | 728 | |
pierventre | eed482b | 2022-02-18 05:16:48 -0800 | [diff] [blame^] | 729 | // If none can reach the device, we will continue with the disconnection logic. |
| 730 | // If there is one instance that reported device is still reachable, we hand over |
| 731 | // the mastership to it if we are the current master, otherwise if we are a backup |
| 732 | // we demote ourselves to the bottom of the backups list |
pierventre | b2f636b | 2022-01-03 17:19:24 +0100 | [diff] [blame] | 733 | if (updateMastershipFor(deviceId) == null) { |
| 734 | log.info("Device {} is fully disconnected from the cluster", deviceId); |
| 735 | List<PortDescription> descs = store.getPortDescriptions(provider().id(), deviceId) |
| 736 | .map(desc -> ensurePortEnabledState(desc, false)) |
| 737 | .collect(Collectors.toList()); |
| 738 | store.updatePorts(this.provider().id(), deviceId, descs); |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 739 | |
Madan Jampani | c6e574f | 2015-05-29 13:41:52 -0700 | [diff] [blame] | 740 | try { |
pierventre | b2f636b | 2022-01-03 17:19:24 +0100 | [diff] [blame] | 741 | if (mastershipService.isLocalMaster(deviceId)) { |
| 742 | post(store.markOffline(deviceId)); |
| 743 | } |
| 744 | } catch (IllegalStateException e) { |
| 745 | log.warn("Failed to mark {} offline", deviceId); |
| 746 | // only the MASTER should be marking off-line in normal cases, |
| 747 | // but if I was the last STANDBY connection, etc. and no one else |
| 748 | // was there to mark the device offline, this instance may need to |
| 749 | // temporarily request for Master Role and mark offline. |
| 750 | |
| 751 | //there are times when this node will correctly have mastership, BUT |
| 752 | //that isn't reflected in the ClockManager before the device disconnects. |
| 753 | //we want to let go of the device anyways, so make sure this happens. |
| 754 | |
| 755 | // FIXME: Store semantics leaking out as IllegalStateException. |
| 756 | // Consider revising store API to handle this scenario. |
| 757 | CompletableFuture<MastershipRole> roleFuture = mastershipService.requestRoleFor(deviceId); |
| 758 | roleFuture.whenComplete((role, error) -> { |
| 759 | MastershipTerm term = termService.getMastershipTerm(deviceId); |
| 760 | // TODO: Move this type of check inside device clock manager, etc. |
| 761 | if (term != null && localNodeId.equals(term.master())) { |
| 762 | log.info("Retry marking {} offline", deviceId); |
| 763 | post(store.markOffline(deviceId)); |
| 764 | } else { |
| 765 | log.info("Failed again marking {} offline. {}", deviceId, role); |
| 766 | } |
| 767 | }); |
| 768 | } finally { |
pierventre | 00ac250 | 2021-12-02 09:42:28 +0100 | [diff] [blame] | 769 | roleToAcknowledge.remove(deviceId); |
Madan Jampani | c6e574f | 2015-05-29 13:41:52 -0700 | [diff] [blame] | 770 | } |
tom | 0efbb1d | 2014-09-09 11:54:28 -0700 | [diff] [blame] | 771 | } |
pierventre | b2f636b | 2022-01-03 17:19:24 +0100 | [diff] [blame] | 772 | |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | @Override |
alshabib | b7b4063 | 2014-09-28 21:30:00 -0700 | [diff] [blame] | 776 | public void updatePorts(DeviceId deviceId, |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 777 | List<PortDescription> portDescriptions) { |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 778 | checkNotNull(deviceId, DEVICE_ID_NULL); |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 779 | checkNotNull(portDescriptions, PORT_DESC_LIST_NULL); |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 780 | checkValidity(); |
Madan Jampani | 565a66a | 2015-07-25 17:01:13 -0700 | [diff] [blame] | 781 | if (!mastershipService.isLocalMaster(deviceId)) { |
Yuta HIGUCHI | 13c0b87 | 2014-10-30 18:09:22 -0700 | [diff] [blame] | 782 | // Never been a master for this device |
| 783 | // any update will be ignored. |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 784 | log.trace("Ignoring {} port updates on standby node. {}", deviceId, portDescriptions); |
Yuta HIGUCHI | 13c0b87 | 2014-10-30 18:09:22 -0700 | [diff] [blame] | 785 | return; |
| 786 | } |
Andrea Campanella | 75ef9f5 | 2017-07-27 20:14:32 +0200 | [diff] [blame] | 787 | PortDescriptionsConfig portConfig = networkConfigService.getConfig(deviceId, PortDescriptionsConfig.class); |
| 788 | if (portConfig != null) { |
Carmelo Cascone | 1da7a4d | 2018-06-27 18:03:11 +0200 | [diff] [blame] | 789 | // Updating the ports if configuration exists (on new lists as |
| 790 | // the passed one might be immutable) |
| 791 | portDescriptions = Lists.newArrayList(portDescriptions); |
Andrea Campanella | 75ef9f5 | 2017-07-27 20:14:32 +0200 | [diff] [blame] | 792 | portDescriptions.addAll(portConfig.portDescriptions()); |
| 793 | } |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 794 | portDescriptions = portDescriptions.stream() |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 795 | .map(e -> applyAllPortOps(deviceId, e)) |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 796 | .collect(Collectors.toList()); |
Yuta HIGUCHI | 5f6739c | 2014-10-01 14:04:01 -0700 | [diff] [blame] | 797 | List<DeviceEvent> events = store.updatePorts(this.provider().id(), |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 798 | deviceId, portDescriptions); |
Thomas Vachuska | 5923b9a | 2016-01-26 10:52:57 -0800 | [diff] [blame] | 799 | if (events != null) { |
| 800 | for (DeviceEvent event : events) { |
| 801 | post(event); |
| 802 | } |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 803 | } |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | @Override |
alshabib | b7b4063 | 2014-09-28 21:30:00 -0700 | [diff] [blame] | 807 | public void portStatusChanged(DeviceId deviceId, |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 808 | PortDescription portDescription) { |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 809 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 810 | checkNotNull(portDescription, PORT_DESCRIPTION_NULL); |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 811 | checkValidity(); |
Ayaka Koshibe | b5c63a0 | 2014-10-18 18:42:27 -0700 | [diff] [blame] | 812 | |
Madan Jampani | 565a66a | 2015-07-25 17:01:13 -0700 | [diff] [blame] | 813 | if (!mastershipService.isLocalMaster(deviceId)) { |
Yuta HIGUCHI | 13c0b87 | 2014-10-30 18:09:22 -0700 | [diff] [blame] | 814 | // Never been a master for this device |
| 815 | // any update will be ignored. |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 816 | log.trace("Ignoring {} port update on standby node. {}", deviceId, |
| 817 | portDescription); |
Yuta HIGUCHI | 13c0b87 | 2014-10-30 18:09:22 -0700 | [diff] [blame] | 818 | return; |
| 819 | } |
Ray Milkey | 9ef2223 | 2016-07-14 12:42:37 -0700 | [diff] [blame] | 820 | Device device = getDevice(deviceId); |
| 821 | if (device == null) { |
| 822 | log.trace("Device not found: {}", deviceId); |
nitinanand | ddfa8c9 | 2017-03-24 16:14:23 +0530 | [diff] [blame] | 823 | return; |
Ray Milkey | 9ef2223 | 2016-07-14 12:42:37 -0700 | [diff] [blame] | 824 | } |
Andrea Campanella | 1c24fb9 | 2018-12-20 16:43:59 +0100 | [diff] [blame] | 825 | if ((Type.ROADM.equals(device.type())) || (Type.OTN.equals(device.type())) || |
| 826 | (Type.OLS.equals(device.type())) || (Type.TERMINAL_DEVICE.equals(device.type()))) { |
HIGUCHI Yuta | 6972ae6 | 2016-05-12 19:57:46 -0700 | [diff] [blame] | 827 | // FIXME This is ignoring all other info in portDescription given as input?? |
Yuta HIGUCHI | 6eb00cc | 2016-06-10 11:55:12 -0700 | [diff] [blame] | 828 | PortDescription storedPortDesc = store.getPortDescription(provider().id(), |
Simon Hunt | ffbad3b | 2017-05-16 15:37:51 -0700 | [diff] [blame] | 829 | deviceId, |
| 830 | portDescription.portNumber()); |
Yuta HIGUCHI | 6eb00cc | 2016-06-10 11:55:12 -0700 | [diff] [blame] | 831 | portDescription = ensurePortEnabledState(storedPortDesc, |
| 832 | portDescription.isEnabled()); |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 833 | } |
| 834 | |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 835 | portDescription = applyAllPortOps(deviceId, portDescription); |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 836 | final DeviceEvent event = store.updatePortStatus(this.provider().id(), |
HIGUCHI Yuta | 6972ae6 | 2016-05-12 19:57:46 -0700 | [diff] [blame] | 837 | deviceId, |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 838 | portDescription); |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 839 | if (event != null) { |
Carmelo Cascone | ab5d41e | 2019-03-06 18:02:34 -0800 | [diff] [blame] | 840 | log.info("Device {} port {} status changed (enabled={})", |
| 841 | deviceId, event.port().number(), portDescription.isEnabled()); |
tom | 0efbb1d | 2014-09-09 11:54:28 -0700 | [diff] [blame] | 842 | post(event); |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 843 | } |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 844 | } |
tom | 3f2bbd7 | 2014-09-24 12:07:58 -0700 | [diff] [blame] | 845 | |
| 846 | @Override |
Michal Mach | ce77433 | 2017-01-25 11:02:55 +0100 | [diff] [blame] | 847 | public void deletePort(DeviceId deviceId, PortDescription basePortDescription) { |
| 848 | |
| 849 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 850 | checkNotNull(basePortDescription, PORT_DESCRIPTION_NULL); |
| 851 | checkValidity(); |
| 852 | |
| 853 | if (!mastershipService.isLocalMaster(deviceId)) { |
| 854 | // Never been a master for this device |
| 855 | // any update will be ignored. |
| 856 | log.trace("Ignoring {} port update on standby node. {}", deviceId, |
| 857 | basePortDescription); |
| 858 | return; |
| 859 | } |
| 860 | |
| 861 | Device device = getDevice(deviceId); |
| 862 | if (device == null) { |
| 863 | log.trace("Device not found: {}", deviceId); |
| 864 | } |
| 865 | |
Yuta HIGUCHI | 53e4796 | 2018-03-01 23:50:48 -0800 | [diff] [blame] | 866 | PortDescription newPortDescription = DefaultPortDescription.builder(basePortDescription) |
| 867 | .isRemoved(true) |
| 868 | .build(); |
| 869 | |
Michal Mach | ce77433 | 2017-01-25 11:02:55 +0100 | [diff] [blame] | 870 | final DeviceEvent event = store.updatePortStatus(this.provider().id(), |
| 871 | deviceId, |
| 872 | newPortDescription); |
| 873 | if (event != null) { |
| 874 | log.info("Device {} port {} status changed", deviceId, event.port().number()); |
| 875 | post(event); |
| 876 | } |
| 877 | } |
| 878 | |
| 879 | @Override |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 880 | public void receivedRoleReply(DeviceId deviceId, MastershipRole requested, |
Thomas Vachuska | b17c41f | 2015-05-19 11:16:05 -0700 | [diff] [blame] | 881 | MastershipRole response) { |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 882 | // Several things can happen here: |
| 883 | // 1. request and response match |
| 884 | // 2. request and response don't match |
| 885 | // 3. MastershipRole and requested match (and 1 or 2 are true) |
| 886 | // 4. MastershipRole and requested don't match (and 1 or 2 are true) |
| 887 | // |
| 888 | // 2, 4, and 3 with case 2 are failure modes. |
| 889 | |
tom | 3f2bbd7 | 2014-09-24 12:07:58 -0700 | [diff] [blame] | 890 | // FIXME: implement response to this notification |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 891 | |
Madan Jampani | f2af771 | 2015-05-29 18:43:52 -0700 | [diff] [blame] | 892 | 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] | 893 | deviceId, requested, response); |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 894 | |
| 895 | if (requested == null && response == null) { |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 896 | // something was off with DeviceProvider, maybe check channel too? |
Shashikanth VH | 387a1ca | 2016-02-09 20:35:21 +0530 | [diff] [blame] | 897 | log.warn("Failed to assert role onto Device {}", deviceId); |
pierventre | 00ac250 | 2021-12-02 09:42:28 +0100 | [diff] [blame] | 898 | roleToAcknowledge.remove(deviceId); |
pierventre | b2f636b | 2022-01-03 17:19:24 +0100 | [diff] [blame] | 899 | updateMastershipFor(deviceId); |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 900 | return; |
Yuta HIGUCHI | cf60390 | 2014-10-07 23:04:32 -0700 | [diff] [blame] | 901 | } |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 902 | |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 903 | final MastershipRole expected = mastershipService.getLocalRole(deviceId); |
| 904 | |
| 905 | if (requested == null) { |
| 906 | // Provider is not able to reconcile role responses with |
| 907 | // requests. We assume what was requested is what we expect. |
| 908 | // This will work only if mastership doesn't change too often, |
| 909 | // and devices are left enough time to provide responses before |
| 910 | // a different role is requested. |
| 911 | requested = expected; |
| 912 | } |
| 913 | |
Thomas Vachuska | b17c41f | 2015-05-19 11:16:05 -0700 | [diff] [blame] | 914 | if (Objects.equals(requested, response)) { |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 915 | if (Objects.equals(requested, expected)) { |
pierventre | 00ac250 | 2021-12-02 09:42:28 +0100 | [diff] [blame] | 916 | // Stop the timer |
| 917 | log.info("Role has been acknowledged for device {}", deviceId); |
| 918 | roleToAcknowledge.remove(deviceId); |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 919 | } else { |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 920 | log.warn("Role mismatch on {}. Set to {}, but store demands {}", |
| 921 | deviceId, response, expected); |
HIGUCHI Yuta | 1979f55 | 2015-12-28 21:24:26 -0800 | [diff] [blame] | 922 | // roleManager got the device to comply, but doesn't agree with |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 923 | // the store; use the store's view, then try to reassert. |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 924 | backgroundService.execute(() -> reassertRole(deviceId, expected)); |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 925 | } |
| 926 | } else { |
| 927 | // we didn't get back what we asked for. Reelect someone else. |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 928 | log.warn("Failed to assert role onto device {}. requested={}, response={}", |
| 929 | deviceId, requested, response); |
Carmelo Cascone | b37bdde | 2018-06-22 17:25:46 +0200 | [diff] [blame] | 930 | if (requested == MastershipRole.MASTER) { |
pierventre | 00ac250 | 2021-12-02 09:42:28 +0100 | [diff] [blame] | 931 | // Stop the timer |
| 932 | roleToAcknowledge.remove(deviceId); |
pierventre | b2f636b | 2022-01-03 17:19:24 +0100 | [diff] [blame] | 933 | updateMastershipFor(deviceId); |
Charles Chan | 3855dc1 | 2021-01-06 01:33:03 +0000 | [diff] [blame] | 934 | } else if (requested == MastershipRole.STANDBY) { |
| 935 | // For P4RT devices, the response role will be NONE when this node is expected to be STANDBY |
| 936 | // but the stream channel is not opened correctly. |
| 937 | // Calling reassertRole will trigger the mechanism in GeneralDeviceProvider that |
| 938 | // attempts to re-establish the stream channel |
| 939 | backgroundService.execute(() -> reassertRole(deviceId, expected)); |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 940 | } |
| 941 | } |
tom | 3f2bbd7 | 2014-09-24 12:07:58 -0700 | [diff] [blame] | 942 | } |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 943 | |
| 944 | @Override |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 945 | public void updatePortStatistics(DeviceId deviceId, Collection<PortStatistics> portStatistics) { |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 946 | checkNotNull(deviceId, DEVICE_ID_NULL); |
Thomas Vachuska | b17c41f | 2015-05-19 11:16:05 -0700 | [diff] [blame] | 947 | checkNotNull(portStatistics, "Port statistics list cannot be null"); |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 948 | checkValidity(); |
| 949 | |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 950 | DeviceEvent event = store.updatePortStatistics(this.provider().id(), |
| 951 | deviceId, portStatistics); |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 952 | post(event); |
| 953 | } |
pierventre | e1f8010 | 2021-10-01 22:01:22 +0200 | [diff] [blame] | 954 | |
| 955 | @Override |
| 956 | public DeviceDescription getDeviceDescription(DeviceId deviceId) { |
| 957 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 958 | checkValidity(); |
| 959 | |
| 960 | return store.getDeviceDescription(provider().id(), deviceId); |
| 961 | } |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 962 | } |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 963 | |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 964 | // by default allowed, otherwise check flag |
| 965 | private boolean isAllowed(BasicDeviceConfig cfg) { |
| 966 | return (cfg == null || cfg.isAllowed()); |
| 967 | } |
| 968 | |
Carmelo Cascone | 0761cd3 | 2018-08-29 19:22:50 -0700 | [diff] [blame] | 969 | private boolean canMarkOnline(Device device) { |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 970 | DeviceProvider provider = getProvider(device.id()); |
| 971 | if (provider == null) { |
| 972 | log.warn("Provider for {} was not found. Cannot evaluate availability", device.id()); |
| 973 | return false; |
| 974 | } |
| 975 | return provider.isAvailable(device.id()); |
Carmelo Cascone | 0761cd3 | 2018-08-29 19:22:50 -0700 | [diff] [blame] | 976 | } |
| 977 | |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 978 | // Applies the specified role to the device; ignores NONE |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 979 | |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 980 | /** |
| 981 | * Apply role to device and send probe if MASTER. |
| 982 | * |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 983 | * @param deviceId device identifier |
| 984 | * @param newRole new role to apply to the device |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 985 | * @return true if the request was sent to provider |
| 986 | */ |
| 987 | private boolean applyRoleAndProbe(DeviceId deviceId, MastershipRole newRole) { |
| 988 | if (newRole.equals(MastershipRole.NONE)) { |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 989 | //no-op |
Yuta HIGUCHI | d26354d | 2014-10-31 14:14:38 -0700 | [diff] [blame] | 990 | return true; |
| 991 | } |
Ayaka Koshibe | 317245a | 2014-10-29 00:34:43 -0700 | [diff] [blame] | 992 | |
Ayaka Koshibe | 78bcbc1 | 2014-11-19 14:28:58 -0800 | [diff] [blame] | 993 | DeviceProvider provider = getProvider(deviceId); |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 994 | if (provider == null) { |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 995 | log.warn("Provider for {} was not found. Cannot apply role {}", deviceId, newRole); |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 996 | return false; |
| 997 | } |
pierventre | 00ac250 | 2021-12-02 09:42:28 +0100 | [diff] [blame] | 998 | // Start the timer |
| 999 | roleToAcknowledge.put(deviceId, currentTimeMillis()); |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 1000 | provider.roleChanged(deviceId, newRole); |
| 1001 | |
| 1002 | if (newRole.equals(MastershipRole.MASTER)) { |
HIGUCHI Yuta | 1979f55 | 2015-12-28 21:24:26 -0800 | [diff] [blame] | 1003 | log.debug("sent TriggerProbe({})", deviceId); |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 1004 | // only trigger event when request was sent to provider |
Ayaka Koshibe | 78bcbc1 | 2014-11-19 14:28:58 -0800 | [diff] [blame] | 1005 | provider.triggerProbe(deviceId); |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 1006 | } |
| 1007 | return true; |
| 1008 | } |
| 1009 | |
pierventre | b2f636b | 2022-01-03 17:19:24 +0100 | [diff] [blame] | 1010 | private boolean probeReachability(DeviceId deviceId) { |
| 1011 | DeviceProvider provider = getProvider(deviceId); |
| 1012 | if (provider == null) { |
| 1013 | log.warn("Provider for {} was not found. Cannot probe reachability", deviceId); |
| 1014 | return false; |
| 1015 | } |
| 1016 | return provider.isReachable(deviceId) && Tools.futureGetOrElse(provider.probeReachability(deviceId), |
| 1017 | PROBE_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS, Boolean.FALSE); |
| 1018 | } |
| 1019 | |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 1020 | /** |
Harold Huang | ff6e1e6 | 2017-11-09 16:25:36 +0800 | [diff] [blame] | 1021 | * Reassert role for specified device connected to this node. |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 1022 | * |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 1023 | * @param did device identifier |
| 1024 | * @param nextRole role to apply. If NONE is specified, |
| 1025 | * it will ask mastership service for a role and apply it. |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 1026 | */ |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 1027 | private void reassertRole(final DeviceId did, |
| 1028 | final MastershipRole nextRole) { |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 1029 | |
Jordan Halterman | 9416aea | 2017-11-17 12:40:21 -0800 | [diff] [blame] | 1030 | MastershipRole myNextRole = nextRole; |
| 1031 | if (myNextRole == NONE && upgradeService.isLocalActive()) { |
| 1032 | try { |
| 1033 | mastershipService.requestRoleFor(did).get(); |
| 1034 | MastershipTerm term = termService.getMastershipTerm(did); |
| 1035 | if (term != null && localNodeId.equals(term.master())) { |
| 1036 | myNextRole = MASTER; |
| 1037 | } else { |
| 1038 | myNextRole = STANDBY; |
| 1039 | } |
| 1040 | } catch (InterruptedException e) { |
| 1041 | Thread.currentThread().interrupt(); |
| 1042 | log.error("Interrupted waiting for Mastership", e); |
| 1043 | } catch (ExecutionException e) { |
| 1044 | log.error("Encountered an error waiting for Mastership", e); |
| 1045 | } |
| 1046 | } |
| 1047 | |
| 1048 | switch (myNextRole) { |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 1049 | case MASTER: |
| 1050 | final Device device = getDevice(did); |
Carmelo Cascone | 0761cd3 | 2018-08-29 19:22:50 -0700 | [diff] [blame] | 1051 | if (device != null && !isAvailable(did) && canMarkOnline(device)) { |
Palash Kala | 6c52606 | 2018-04-03 18:25:11 +0900 | [diff] [blame] | 1052 | post(store.markOnline(did)); |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 1053 | } |
| 1054 | // TODO: should apply role only if there is mismatch |
Jordan Halterman | 9416aea | 2017-11-17 12:40:21 -0800 | [diff] [blame] | 1055 | log.debug("Applying role {} to {}", myNextRole, did); |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 1056 | if (!applyRoleAndProbe(did, MASTER)) { |
Jordan Halterman | 9416aea | 2017-11-17 12:40:21 -0800 | [diff] [blame] | 1057 | log.warn("Unsuccessful applying role {} to {}", myNextRole, did); |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 1058 | // immediately failed to apply role |
pierventre | b2f636b | 2022-01-03 17:19:24 +0100 | [diff] [blame] | 1059 | updateMastershipFor(did); |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 1060 | // FIXME disconnect? |
| 1061 | } |
| 1062 | break; |
| 1063 | case STANDBY: |
Jordan Halterman | 9416aea | 2017-11-17 12:40:21 -0800 | [diff] [blame] | 1064 | log.debug("Applying role {} to {}", myNextRole, did); |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 1065 | if (!applyRoleAndProbe(did, STANDBY)) { |
Jordan Halterman | 9416aea | 2017-11-17 12:40:21 -0800 | [diff] [blame] | 1066 | log.warn("Unsuccessful applying role {} to {}", myNextRole, did); |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 1067 | // immediately failed to apply role |
pierventre | b2f636b | 2022-01-03 17:19:24 +0100 | [diff] [blame] | 1068 | updateMastershipFor(did); |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 1069 | // FIXME disconnect? |
| 1070 | } |
| 1071 | break; |
| 1072 | case NONE: |
Jordan Halterman | 980a8c1 | 2017-09-22 18:01:19 -0700 | [diff] [blame] | 1073 | break; |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 1074 | default: |
| 1075 | // should never reach here |
| 1076 | log.error("You didn't see anything. I did not exist."); |
| 1077 | break; |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 1078 | } |
| 1079 | } |
| 1080 | |
Madan Jampani | 328371d | 2015-05-29 14:06:27 -0700 | [diff] [blame] | 1081 | private void handleMastershipEvent(MastershipEvent event) { |
pierventre | eed482b | 2022-02-18 05:16:48 -0800 | [diff] [blame^] | 1082 | log.debug("Handling mastership event"); |
Madan Jampani | 328371d | 2015-05-29 14:06:27 -0700 | [diff] [blame] | 1083 | final DeviceId did = event.subject(); |
| 1084 | |
pierventre | b2f636b | 2022-01-03 17:19:24 +0100 | [diff] [blame] | 1085 | // myNextRole suggested by MastershipService event |
Madan Jampani | 328371d | 2015-05-29 14:06:27 -0700 | [diff] [blame] | 1086 | MastershipRole myNextRole; |
Jon Hall | 7a8bfc6 | 2016-05-26 17:59:04 -0700 | [diff] [blame] | 1087 | if (event.type() == MastershipEvent.Type.SUSPENDED) { |
| 1088 | myNextRole = NONE; // FIXME STANDBY OR NONE? |
| 1089 | } else if (localNodeId.equals(event.roleInfo().master())) { |
Madan Jampani | 328371d | 2015-05-29 14:06:27 -0700 | [diff] [blame] | 1090 | // confirm latest info |
| 1091 | MastershipTerm term = termService.getMastershipTerm(did); |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 1092 | final boolean iHaveControl = term != null && localNodeId.equals(term.master()); |
Madan Jampani | 328371d | 2015-05-29 14:06:27 -0700 | [diff] [blame] | 1093 | if (iHaveControl) { |
Madan Jampani | 328371d | 2015-05-29 14:06:27 -0700 | [diff] [blame] | 1094 | myNextRole = MASTER; |
| 1095 | } else { |
| 1096 | myNextRole = STANDBY; |
| 1097 | } |
| 1098 | } else if (event.roleInfo().backups().contains(localNodeId)) { |
| 1099 | myNextRole = STANDBY; |
| 1100 | } else { |
| 1101 | myNextRole = NONE; |
| 1102 | } |
| 1103 | |
pierventre | eed482b | 2022-02-18 05:16:48 -0800 | [diff] [blame^] | 1104 | log.debug("Device {} local status is {}", did, localStatus(did)); |
| 1105 | final boolean isGracePeriodOn = inGracePeriod(did); |
| 1106 | final boolean isReachable = isReachable(did, isGracePeriodOn); |
| 1107 | // Passed the grace period and it is still not reachable |
| 1108 | if (!isGracePeriodOn && !isReachable) { |
pierventre | b2f636b | 2022-01-03 17:19:24 +0100 | [diff] [blame] | 1109 | // device is not connected to this node, nevertheless we should get a role |
Jon Hall | a90c44c | 2017-01-24 16:02:07 -0800 | [diff] [blame] | 1110 | if (mastershipService.getLocalRole(did) == NONE) { |
| 1111 | log.debug("Node was instructed to be {} role for {}, " |
Simon Hunt | ffbad3b | 2017-05-16 15:37:51 -0700 | [diff] [blame] | 1112 | + "but this node cannot reach the device " |
pierventre | b2f636b | 2022-01-03 17:19:24 +0100 | [diff] [blame] | 1113 | + "and role is already None. Asking a new role " |
| 1114 | + "and then apply the disconnection protocol.", |
Simon Hunt | ffbad3b | 2017-05-16 15:37:51 -0700 | [diff] [blame] | 1115 | myNextRole, did); |
pierventre | b2f636b | 2022-01-03 17:19:24 +0100 | [diff] [blame] | 1116 | try { |
| 1117 | mastershipService.requestRoleFor(did).get(); |
| 1118 | } catch (InterruptedException e) { |
| 1119 | Thread.currentThread().interrupt(); |
| 1120 | log.error("Interrupted waiting for Mastership", e); |
| 1121 | } catch (ExecutionException e) { |
| 1122 | log.error("Encountered an error waiting for Mastership", e); |
| 1123 | } |
Jon Hall | a90c44c | 2017-01-24 16:02:07 -0800 | [diff] [blame] | 1124 | } else if (myNextRole != NONE) { |
Madan Jampani | 328371d | 2015-05-29 14:06:27 -0700 | [diff] [blame] | 1125 | log.warn("Node was instructed to be {} role for {}, " |
pierventre | b2f636b | 2022-01-03 17:19:24 +0100 | [diff] [blame] | 1126 | + "but this node cannot reach the device. " |
| 1127 | + "Apply the disconnection protocol.", |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 1128 | myNextRole, did); |
Madan Jampani | 328371d | 2015-05-29 14:06:27 -0700 | [diff] [blame] | 1129 | } |
pierventre | b2f636b | 2022-01-03 17:19:24 +0100 | [diff] [blame] | 1130 | // Let's put some order in the candidates list |
| 1131 | roleToAcknowledge.remove(did); |
| 1132 | updateMastershipFor(did); |
pierventre | eed482b | 2022-02-18 05:16:48 -0800 | [diff] [blame^] | 1133 | } else if (isReachable) { |
| 1134 | // Device is connected to this node - always reassert the role. |
| 1135 | // Ideally, protocols like OpenFlow would not need to reassert the |
| 1136 | // role because the instances are only identified by the role. However, |
| 1137 | // other protocols like P4RT require to provide also an election id |
| 1138 | // which maybe different over time, by reasserting the role will guarantee |
| 1139 | // that updated election ids are communicated to the devices. It should not |
| 1140 | // cost us a lot as it is equivalent to a probe. |
| 1141 | if (store.getDevice(did) != null) { |
| 1142 | log.info("{} is reachable - reasserting the role", did); |
| 1143 | reassertRole(did, myNextRole); |
| 1144 | } else { |
| 1145 | log.debug("Device is not yet/no longer in the store: {}", did); |
| 1146 | } |
Madan Jampani | 328371d | 2015-05-29 14:06:27 -0700 | [diff] [blame] | 1147 | } else { |
pierventre | eed482b | 2022-02-18 05:16:48 -0800 | [diff] [blame^] | 1148 | // Do not proceed furthermore if the grace period is still on |
| 1149 | log.debug("Skipping mastership event {}", event); |
Madan Jampani | 328371d | 2015-05-29 14:06:27 -0700 | [diff] [blame] | 1150 | } |
| 1151 | } |
| 1152 | |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 1153 | // Intercepts mastership events |
| 1154 | private class InternalMastershipListener implements MastershipListener { |
| 1155 | |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 1156 | @Override |
| 1157 | public void event(MastershipEvent event) { |
HIGUCHI Yuta | 060da9a | 2016-03-11 19:16:35 -0800 | [diff] [blame] | 1158 | backgroundService.execute(() -> { |
Madan Jampani | 328371d | 2015-05-29 14:06:27 -0700 | [diff] [blame] | 1159 | try { |
| 1160 | handleMastershipEvent(event); |
| 1161 | } catch (Exception e) { |
| 1162 | log.warn("Failed to handle {}", event, e); |
Yuta HIGUCHI | d26354d | 2014-10-31 14:14:38 -0700 | [diff] [blame] | 1163 | } |
Madan Jampani | 328371d | 2015-05-29 14:06:27 -0700 | [diff] [blame] | 1164 | }); |
Ayaka Koshibe | 317245a | 2014-10-29 00:34:43 -0700 | [diff] [blame] | 1165 | } |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 1166 | } |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame] | 1167 | |
| 1168 | // Store delegate to re-post events emitted from the store. |
Thomas Vachuska | b17c41f | 2015-05-19 11:16:05 -0700 | [diff] [blame] | 1169 | private class InternalStoreDelegate implements DeviceStoreDelegate { |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame] | 1170 | @Override |
| 1171 | public void notify(DeviceEvent event) { |
| 1172 | post(event); |
Rafał Szalecki | 9fb87f6 | 2017-12-06 15:06:09 +0100 | [diff] [blame] | 1173 | if (event.type().equals(DeviceEvent.Type.DEVICE_REMOVED)) { |
Thomas Vachuska | 5b38dc0 | 2018-05-10 15:24:40 -0700 | [diff] [blame] | 1174 | // When device is administratively removed, force disconnect. |
| 1175 | DeviceId deviceId = event.subject().id(); |
| 1176 | deviceLocalStatus.remove(deviceId); |
| 1177 | |
| 1178 | DeviceProvider provider = getProvider(deviceId); |
| 1179 | if (provider != null) { |
| 1180 | log.info("Triggering disconnect for device {}", deviceId); |
| 1181 | try { |
| 1182 | provider.triggerDisconnect(deviceId); |
| 1183 | } catch (UnsupportedOperationException e) { |
| 1184 | log.warn("Unable to trigger disconnect due to {}", e.getMessage()); |
| 1185 | } |
| 1186 | } |
Rafał Szalecki | 9fb87f6 | 2017-12-06 15:06:09 +0100 | [diff] [blame] | 1187 | } |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame] | 1188 | } |
| 1189 | } |
samuel | 738dfaf | 2015-07-11 11:08:57 +0800 | [diff] [blame] | 1190 | |
| 1191 | @Override |
| 1192 | public Iterable<Device> getDevices(Type type) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 1193 | checkPermission(DEVICE_READ); |
samuel | 738dfaf | 2015-07-11 11:08:57 +0800 | [diff] [blame] | 1194 | Set<Device> results = new HashSet<>(); |
| 1195 | Iterable<Device> devices = store.getDevices(); |
| 1196 | if (devices != null) { |
| 1197 | devices.forEach(d -> { |
| 1198 | if (type.equals(d.type())) { |
| 1199 | results.add(d); |
| 1200 | } |
| 1201 | }); |
| 1202 | } |
| 1203 | return results; |
| 1204 | } |
| 1205 | |
| 1206 | @Override |
| 1207 | public Iterable<Device> getAvailableDevices(Type type) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 1208 | checkPermission(DEVICE_READ); |
samuel | 738dfaf | 2015-07-11 11:08:57 +0800 | [diff] [blame] | 1209 | Set<Device> results = new HashSet<>(); |
| 1210 | Iterable<Device> availableDevices = store.getAvailableDevices(); |
| 1211 | if (availableDevices != null) { |
| 1212 | availableDevices.forEach(d -> { |
| 1213 | if (type.equals(d.type())) { |
| 1214 | results.add(d); |
| 1215 | } |
| 1216 | }); |
| 1217 | } |
| 1218 | return results; |
| 1219 | } |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 1220 | |
| 1221 | private class InternalNetworkConfigListener implements NetworkConfigListener { |
Ray Milkey | 69ca82a | 2019-03-28 09:24:40 -0700 | [diff] [blame] | 1222 | private DeviceId extractDeviceId(NetworkConfigEvent event) { |
| 1223 | DeviceId deviceId = null; |
debmaiti | 9553ed7 | 2019-03-18 14:27:42 +0530 | [diff] [blame] | 1224 | if (event.configClass().equals(PortAnnotationConfig.class)) { |
Ray Milkey | 397caca | 2019-04-01 16:27:50 -0700 | [diff] [blame] | 1225 | if (event.subject().getClass() == ConnectPoint.class) { |
| 1226 | deviceId = ((ConnectPoint) event.subject()).deviceId(); |
| 1227 | } |
Ray Milkey | 69ca82a | 2019-03-28 09:24:40 -0700 | [diff] [blame] | 1228 | } else if (event.subject().getClass() == DeviceId.class) { |
debmaiti | 9553ed7 | 2019-03-18 14:27:42 +0530 | [diff] [blame] | 1229 | deviceId = (DeviceId) event.subject(); |
| 1230 | } |
Ray Milkey | 69ca82a | 2019-03-28 09:24:40 -0700 | [diff] [blame] | 1231 | return deviceId; |
| 1232 | } |
| 1233 | |
| 1234 | @Override |
| 1235 | public boolean isRelevant(NetworkConfigEvent event) { |
| 1236 | DeviceId deviceId = extractDeviceId(event); |
| 1237 | |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 1238 | return (event.type() == NetworkConfigEvent.Type.CONFIG_ADDED |
debmaiti | 9553ed7 | 2019-03-18 14:27:42 +0530 | [diff] [blame] | 1239 | || event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED |
| 1240 | || event.type() == NetworkConfigEvent.Type.CONFIG_REMOVED) |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 1241 | && (event.configClass().equals(BasicDeviceConfig.class) |
Andrea Campanella | 75ef9f5 | 2017-07-27 20:14:32 +0200 | [diff] [blame] | 1242 | || portOpsIndex.containsKey(event.configClass()) |
Palash Kala | a06a616 | 2017-11-15 20:42:40 +0900 | [diff] [blame] | 1243 | || event.configClass().equals(PortDescriptionsConfig.class) |
Thomas Vachuska | f131e59 | 2018-05-07 11:52:14 -0700 | [diff] [blame] | 1244 | || event.configClass().equals(DeviceAnnotationConfig.class)) |
Ray Milkey | 69ca82a | 2019-03-28 09:24:40 -0700 | [diff] [blame] | 1245 | && deviceId != null && mastershipService.isLocalMaster(deviceId); |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 1246 | } |
| 1247 | |
| 1248 | @Override |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 1249 | public void event(NetworkConfigEvent event) { |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 1250 | DeviceEvent de = null; |
| 1251 | if (event.configClass().equals(BasicDeviceConfig.class)) { |
Thomas Vachuska | 138de8b | 2016-01-11 21:31:38 -0800 | [diff] [blame] | 1252 | log.debug("Detected device network config event {}", event.type()); |
Yuta HIGUCHI | 1d4b3aa | 2017-08-09 15:10:18 -0700 | [diff] [blame] | 1253 | DeviceId did = (DeviceId) event.subject(); |
| 1254 | DeviceProvider dp = getProvider(did); |
Simon Hunt | ffbad3b | 2017-05-16 15:37:51 -0700 | [diff] [blame] | 1255 | BasicDeviceConfig cfg = |
| 1256 | networkConfigService.getConfig(did, BasicDeviceConfig.class); |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 1257 | |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 1258 | if (!isAllowed(cfg)) { |
| 1259 | kickOutBadDevice(did); |
| 1260 | } else { |
| 1261 | Device dev = getDevice(did); |
Simon Hunt | ffbad3b | 2017-05-16 15:37:51 -0700 | [diff] [blame] | 1262 | DeviceDescription desc = |
| 1263 | (dev == null) ? null : BasicDeviceOperator.descriptionOf(dev); |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 1264 | desc = BasicDeviceOperator.combine(cfg, desc); |
Simon Hunt | ffbad3b | 2017-05-16 15:37:51 -0700 | [diff] [blame] | 1265 | if (desc != null && dp != null) { |
pier | 388ec25 | 2020-04-15 20:53:14 +0200 | [diff] [blame] | 1266 | store.createOrUpdateDevice(dp.id(), did, desc); |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 1267 | } |
| 1268 | } |
Yuta HIGUCHI | 1d4b3aa | 2017-08-09 15:10:18 -0700 | [diff] [blame] | 1269 | } else if (event.configClass().equals(PortDescriptionsConfig.class)) { |
| 1270 | DeviceId did = (DeviceId) event.subject(); |
| 1271 | DeviceProvider dp = getProvider(did); |
| 1272 | if (!event.config().isPresent() || |
| 1273 | getDevice(did) == null || dp == null) { |
| 1274 | // sanity check failed, ignore |
| 1275 | return; |
| 1276 | } |
Andrea Campanella | 75ef9f5 | 2017-07-27 20:14:32 +0200 | [diff] [blame] | 1277 | PortDescriptionsConfig portConfig = (PortDescriptionsConfig) event.config().get(); |
Yuta HIGUCHI | 1d4b3aa | 2017-08-09 15:10:18 -0700 | [diff] [blame] | 1278 | //updating the ports if configuration exists |
Andrea Campanella | 75ef9f5 | 2017-07-27 20:14:32 +0200 | [diff] [blame] | 1279 | List<PortDescription> complete = store.getPortDescriptions(dp.id(), did) |
| 1280 | .collect(Collectors.toList()); |
| 1281 | complete.addAll(portConfig.portDescriptions()); |
| 1282 | store.updatePorts(dp.id(), did, complete); |
Palash Kala | a06a616 | 2017-11-15 20:42:40 +0900 | [diff] [blame] | 1283 | } else if (event.configClass().equals(DeviceAnnotationConfig.class)) { |
| 1284 | DeviceId did = (DeviceId) event.subject(); |
| 1285 | DeviceProvider dp = getProvider(did); |
| 1286 | Device dev = getDevice(did); |
| 1287 | DeviceDescription desc = |
| 1288 | (dev == null) ? null : BasicDeviceOperator.descriptionOf(dev); |
| 1289 | Optional<Config> prevConfig = event.prevConfig(); |
Anurag Chadha | 6194639 | 2020-08-28 15:18:03 +0530 | [diff] [blame] | 1290 | if (desc != null) { // Fix for NPE due to desc being null |
| 1291 | desc = deviceAnnotationOp.combine(did, desc, prevConfig); |
| 1292 | } |
Palash Kala | a06a616 | 2017-11-15 20:42:40 +0900 | [diff] [blame] | 1293 | if (desc != null && dp != null) { |
pier | 388ec25 | 2020-04-15 20:53:14 +0200 | [diff] [blame] | 1294 | store.createOrUpdateDevice(dp.id(), did, desc); |
Palash Kala | a06a616 | 2017-11-15 20:42:40 +0900 | [diff] [blame] | 1295 | } |
Yuta HIGUCHI | 1d4b3aa | 2017-08-09 15:10:18 -0700 | [diff] [blame] | 1296 | } else if (portOpsIndex.containsKey(event.configClass())) { |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 1297 | ConnectPoint cpt = (ConnectPoint) event.subject(); |
Yuta HIGUCHI | 1d4b3aa | 2017-08-09 15:10:18 -0700 | [diff] [blame] | 1298 | DeviceId did = cpt.deviceId(); |
| 1299 | DeviceProvider dp = getProvider(did); |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 1300 | |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 1301 | // Note: assuming PortOperator can modify existing port, |
| 1302 | // but cannot add new port purely from Config. |
Simon Hunt | ffbad3b | 2017-05-16 15:37:51 -0700 | [diff] [blame] | 1303 | de = Optional.ofNullable(dp) |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 1304 | .map(provider -> store.getPortDescription(provider.id(), did, cpt.port())) |
debmaiti | 9553ed7 | 2019-03-18 14:27:42 +0530 | [diff] [blame] | 1305 | .map(desc -> applyAllPortOps(cpt, desc, event.prevConfig())) |
Simon Hunt | ffbad3b | 2017-05-16 15:37:51 -0700 | [diff] [blame] | 1306 | .map(desc -> store.updatePortStatus(dp.id(), did, desc)) |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 1307 | .orElse(null); |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 1308 | } |
| 1309 | |
| 1310 | if (de != null) { |
| 1311 | post(de); |
| 1312 | } |
| 1313 | } |
| 1314 | |
Simon Hunt | 8f60ff8 | 2017-04-24 17:19:30 -0700 | [diff] [blame] | 1315 | // removes the specified device if it exists |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 1316 | private void kickOutBadDevice(DeviceId deviceId) { |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 1317 | Device badDevice = getDevice(deviceId); |
| 1318 | if (badDevice != null) { |
| 1319 | removeDevice(deviceId); |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 1320 | } |
| 1321 | } |
| 1322 | } |
Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 1323 | |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 1324 | @Override |
| 1325 | @SafeVarargs |
| 1326 | public final void registerPortConfigOperator(PortConfigOperator portOp, |
Simon Hunt | ffbad3b | 2017-05-16 15:37:51 -0700 | [diff] [blame] | 1327 | Class<? extends Config<ConnectPoint>>... configs) { |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 1328 | checkNotNull(portOp); |
| 1329 | |
| 1330 | portOp.bindService(networkConfigService); |
| 1331 | |
| 1332 | // update both portOpsIndex and portOps |
| 1333 | synchronized (portOpsIndex) { |
| 1334 | for (Class<? extends Config<ConnectPoint>> config : configs) { |
| 1335 | portOpsIndex.put(config, portOp); |
| 1336 | } |
| 1337 | |
| 1338 | portOps.add(portOp); |
| 1339 | } |
| 1340 | |
| 1341 | // TODO: Should we be applying to all existing Ports? |
| 1342 | Tools.stream(store.getAvailableDevices()) |
Simon Hunt | ffbad3b | 2017-05-16 15:37:51 -0700 | [diff] [blame] | 1343 | .map(Device::id) |
| 1344 | .filter(mastershipService::isLocalMaster) |
| 1345 | // for each locally managed Device, update all port descriptions |
| 1346 | .map(did -> { |
| 1347 | ProviderId pid = Optional.ofNullable(getProvider(did)) |
| 1348 | .map(Provider::id) |
| 1349 | .orElse(null); |
| 1350 | if (pid == null) { |
| 1351 | log.warn("Provider not found for {}", did); |
| 1352 | return ImmutableList.<DeviceEvent>of(); |
| 1353 | } |
| 1354 | List<PortDescription> pds |
| 1355 | = store.getPortDescriptions(pid, did) |
| 1356 | .map(pdesc -> applyAllPortOps(did, pdesc)) |
| 1357 | .collect(Collectors.toList()); |
| 1358 | return store.updatePorts(pid, did, pds); |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 1359 | }) |
Simon Hunt | ffbad3b | 2017-05-16 15:37:51 -0700 | [diff] [blame] | 1360 | // ..and port port update event if necessary |
| 1361 | .forEach(evts -> evts.forEach(this::post)); |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 1362 | } |
| 1363 | |
| 1364 | @Override |
| 1365 | public void unregisterPortConfigOperator(PortConfigOperator portOp) { |
| 1366 | checkNotNull(portOp); |
| 1367 | |
| 1368 | |
| 1369 | // remove all portOp |
| 1370 | synchronized (portOpsIndex) { |
| 1371 | portOps.remove(portOp); |
| 1372 | |
| 1373 | // had to do this since COWArrayList iterator doesn't support remove |
| 1374 | portOpsIndex.keySet().forEach(key -> portOpsIndex.remove(key, portOp)); |
| 1375 | } |
| 1376 | |
| 1377 | } |
| 1378 | |
| 1379 | /** |
| 1380 | * Merges the appropriate PortConfig with the description. |
| 1381 | * |
| 1382 | * @param did ID of the Device where the port is attached |
| 1383 | * @param desc {@link PortDescription} |
| 1384 | * @return merged {@link PortDescription} |
| 1385 | */ |
| 1386 | private PortDescription applyAllPortOps(DeviceId did, PortDescription desc) { |
| 1387 | return applyAllPortOps(new ConnectPoint(did, desc.portNumber()), desc); |
| 1388 | } |
| 1389 | |
| 1390 | /** |
| 1391 | * Merges the appropriate PortConfig with the description. |
| 1392 | * |
Simon Hunt | ffbad3b | 2017-05-16 15:37:51 -0700 | [diff] [blame] | 1393 | * @param cpt ConnectPoint where the port is attached |
| 1394 | * @param desc {@link PortDescription} |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 1395 | * @return merged {@link PortDescription} |
| 1396 | */ |
| 1397 | private PortDescription applyAllPortOps(ConnectPoint cpt, PortDescription desc) { |
| 1398 | PortDescription work = desc; |
| 1399 | for (PortConfigOperator portOp : portOps) { |
| 1400 | work = portOp.combine(cpt, work); |
| 1401 | } |
Yuta HIGUCHI | 7438f5a | 2017-02-15 22:09:46 -0800 | [diff] [blame] | 1402 | return portAnnotationOp.combine(cpt, work); |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 1403 | } |
| 1404 | |
Jonghwan Hyun | 6ecf56d | 2017-08-01 16:07:44 -0700 | [diff] [blame] | 1405 | /** |
debmaiti | 9553ed7 | 2019-03-18 14:27:42 +0530 | [diff] [blame] | 1406 | * Merges the appropriate PortConfig with the description. |
| 1407 | * |
| 1408 | * @param cpt ConnectPoint where the port is attached |
| 1409 | * @param desc {@link PortDescription} |
| 1410 | * @param prevConfig previous configuration |
| 1411 | * @return merged {@link PortDescription} |
| 1412 | */ |
| 1413 | private PortDescription applyAllPortOps(ConnectPoint cpt, PortDescription desc, |
| 1414 | Optional<Config> prevConfig) { |
| 1415 | PortDescription work = desc; |
| 1416 | for (PortConfigOperator portOp : portOps) { |
| 1417 | work = portOp.combine(cpt, work, prevConfig); |
| 1418 | } |
| 1419 | return portAnnotationOp.combine(cpt, work, prevConfig); |
| 1420 | } |
| 1421 | |
| 1422 | /** |
pierventre | b2f636b | 2022-01-03 17:19:24 +0100 | [diff] [blame] | 1423 | * Handler for remote probe requests. |
| 1424 | * |
| 1425 | * @param deviceId the device to check |
| 1426 | * @return whether or not the device is reachable |
| 1427 | */ |
| 1428 | private boolean handleProbeRequest(DeviceId deviceId) { |
| 1429 | int attempt = 0; |
| 1430 | // Let's do a number of attempts |
| 1431 | while (attempt < PROBE_ATTEMPTS) { |
| 1432 | if (!probeReachability(deviceId)) { |
| 1433 | return false; |
| 1434 | } |
| 1435 | attempt++; |
| 1436 | } |
| 1437 | return true; |
| 1438 | } |
| 1439 | |
| 1440 | /** |
| 1441 | * Update the mastership for this device. If there is a node able |
| 1442 | * to reach the device and this node is the master move the |
| 1443 | * mastership to the next node still connected to this device. |
| 1444 | * If the current node is a backup, it demotes itself to the bottom |
| 1445 | * of the candidates list |
| 1446 | * |
| 1447 | * @param deviceId the device for which we have to update the mastership |
| 1448 | * @return the NodeId of any node that can reach the device, or null if |
| 1449 | * none of the ONOS instances can reach the device |
| 1450 | */ |
| 1451 | private NodeId updateMastershipFor(DeviceId deviceId) { |
| 1452 | Map<NodeId, CompletableFuture<Boolean>> probes = Maps.newHashMap(); |
| 1453 | // Request a probe only if the node is ready |
| 1454 | for (ControllerNode onosNode : clusterService.getNodes()) { |
| 1455 | if (!clusterService.getState(onosNode.id()).isReady() || localNodeId.equals(onosNode.id())) { |
| 1456 | continue; |
| 1457 | } |
| 1458 | probes.put(onosNode.id(), communicationService.sendAndReceive(deviceId, PROBE_SUBJECT, SERIALIZER::encode, |
| 1459 | SERIALIZER::decode, onosNode.id())); |
| 1460 | } |
| 1461 | |
| 1462 | // Returns the first node able to reach the device |
| 1463 | // FIXME [SDFAB-935] optimize by looking at the MastershipInfo |
| 1464 | boolean isReachable; |
| 1465 | NodeId nextMaster = null; |
| 1466 | // FIXME Should we expose timeout? Understand if there is need to signal to the caller |
| 1467 | for (Map.Entry<NodeId, CompletableFuture<Boolean>> probe : probes.entrySet()) { |
| 1468 | isReachable = Tools.futureGetOrElse(probe.getValue(), PROBE_TIMEOUT_MILLIS, |
| 1469 | TimeUnit.MILLISECONDS, Boolean.FALSE); |
| 1470 | if (isReachable) { |
| 1471 | nextMaster = probe.getKey(); |
| 1472 | } |
| 1473 | } |
| 1474 | |
| 1475 | // FIXME [SDFAB-935] optimize demote by looking at the MastershipInfo; |
| 1476 | if (nextMaster != null) { |
| 1477 | log.info("Device {} is still connected to {}", deviceId, nextMaster); |
| 1478 | MastershipRole myRole = mastershipService.getLocalRole(deviceId); |
| 1479 | if (myRole == MASTER) { |
| 1480 | log.info("Handing over the mastership of {} to next master {}", deviceId, nextMaster); |
| 1481 | mastershipAdminService.setRole(nextMaster, deviceId, MASTER); |
pierventre | eed482b | 2022-02-18 05:16:48 -0800 | [diff] [blame^] | 1482 | // Do not demote here because setRole can return before the mastership has been passed. |
| 1483 | // Current implementation promotes first the nextMaster as top of candidate list and then |
| 1484 | // transfer the leadership. We can use the BACKUP events to do demote or leverage periodic |
| 1485 | // checks. |
pierventre | b2f636b | 2022-01-03 17:19:24 +0100 | [diff] [blame] | 1486 | } else if (myRole == STANDBY) { |
| 1487 | log.info("Demote current instance to the bottom of the candidates list for {}", deviceId); |
| 1488 | mastershipAdminService.demote(localNodeId, deviceId); |
| 1489 | } else { |
| 1490 | log.debug("No valid role for {}", deviceId); |
| 1491 | } |
| 1492 | } |
| 1493 | |
| 1494 | return nextMaster; |
| 1495 | } |
| 1496 | |
pierventre | b2f636b | 2022-01-03 17:19:24 +0100 | [diff] [blame] | 1497 | /** |
Jonghwan Hyun | 6ecf56d | 2017-08-01 16:07:44 -0700 | [diff] [blame] | 1498 | * Port Enable/Disable message sent to the device's MASTER node. |
| 1499 | */ |
| 1500 | private class InternalPortUpDownEvent { |
| 1501 | private final DeviceId deviceId; |
| 1502 | private final PortNumber portNumber; |
| 1503 | private final boolean enable; |
| 1504 | |
| 1505 | protected InternalPortUpDownEvent( |
| 1506 | DeviceId deviceId, PortNumber portNumber, boolean enable) { |
| 1507 | this.deviceId = deviceId; |
| 1508 | this.portNumber = portNumber; |
| 1509 | this.enable = enable; |
| 1510 | } |
| 1511 | |
| 1512 | public DeviceId deviceId() { |
| 1513 | return deviceId; |
| 1514 | } |
| 1515 | public PortNumber portNumber() { |
| 1516 | return portNumber; |
| 1517 | } |
| 1518 | public boolean isEnable() { |
| 1519 | return enable; |
| 1520 | } |
| 1521 | |
| 1522 | protected InternalPortUpDownEvent() { |
| 1523 | this.deviceId = null; |
| 1524 | this.portNumber = null; |
| 1525 | this.enable = false; |
| 1526 | } |
| 1527 | } |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 1528 | } |