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