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