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