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.store.device.impl; |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 17 | |
Ray Milkey | 2bf5ea7 | 2017-06-01 09:03:34 -0700 | [diff] [blame] | 18 | import java.io.IOException; |
| 19 | import java.util.ArrayList; |
| 20 | import java.util.Collection; |
| 21 | import java.util.Collections; |
| 22 | import java.util.HashMap; |
| 23 | import java.util.HashSet; |
| 24 | import java.util.Iterator; |
| 25 | import java.util.List; |
| 26 | import java.util.Map; |
| 27 | import java.util.Map.Entry; |
| 28 | import java.util.Objects; |
| 29 | import java.util.Optional; |
| 30 | import java.util.Set; |
| 31 | import java.util.concurrent.ConcurrentHashMap; |
| 32 | import java.util.concurrent.ConcurrentMap; |
| 33 | import java.util.concurrent.ExecutorService; |
| 34 | import java.util.concurrent.ScheduledExecutorService; |
| 35 | import java.util.concurrent.TimeUnit; |
| 36 | import java.util.function.Consumer; |
| 37 | import java.util.stream.Stream; |
| 38 | |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 39 | import org.apache.commons.lang3.RandomUtils; |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 40 | import org.apache.felix.scr.annotations.Activate; |
| 41 | import org.apache.felix.scr.annotations.Component; |
| 42 | import org.apache.felix.scr.annotations.Deactivate; |
| 43 | import org.apache.felix.scr.annotations.Reference; |
| 44 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 45 | import org.apache.felix.scr.annotations.Service; |
Marc De Leenheer | b473b9d | 2015-02-06 15:21:03 -0800 | [diff] [blame] | 46 | import org.onlab.packet.ChassisId; |
| 47 | import org.onlab.util.KryoNamespace; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 48 | import org.onosproject.cluster.ClusterService; |
| 49 | import org.onosproject.cluster.ControllerNode; |
| 50 | import org.onosproject.cluster.NodeId; |
| 51 | import org.onosproject.mastership.MastershipService; |
| 52 | import org.onosproject.mastership.MastershipTerm; |
| 53 | import org.onosproject.mastership.MastershipTermService; |
Marc De Leenheer | 88194c3 | 2015-05-29 22:10:59 -0700 | [diff] [blame] | 54 | import org.onosproject.net.Annotations; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 55 | import org.onosproject.net.AnnotationsUtil; |
| 56 | import org.onosproject.net.DefaultAnnotations; |
| 57 | import org.onosproject.net.DefaultDevice; |
| 58 | import org.onosproject.net.DefaultPort; |
| 59 | import org.onosproject.net.Device; |
| 60 | import org.onosproject.net.Device.Type; |
| 61 | import org.onosproject.net.DeviceId; |
| 62 | import org.onosproject.net.MastershipRole; |
| 63 | import org.onosproject.net.Port; |
| 64 | import org.onosproject.net.PortNumber; |
Thomas Vachuska | fdbc4c2 | 2015-05-29 15:53:01 -0700 | [diff] [blame] | 65 | import org.onosproject.net.device.DefaultPortStatistics; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 66 | import org.onosproject.net.device.DeviceClockService; |
| 67 | import org.onosproject.net.device.DeviceDescription; |
| 68 | import org.onosproject.net.device.DeviceEvent; |
| 69 | import org.onosproject.net.device.DeviceStore; |
| 70 | import org.onosproject.net.device.DeviceStoreDelegate; |
| 71 | import org.onosproject.net.device.PortDescription; |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 72 | import org.onosproject.net.device.PortStatistics; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 73 | import org.onosproject.net.provider.ProviderId; |
| 74 | import org.onosproject.store.AbstractStore; |
| 75 | import org.onosproject.store.Timestamp; |
| 76 | import org.onosproject.store.cluster.messaging.ClusterCommunicationService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 77 | import org.onosproject.store.cluster.messaging.MessageSubject; |
Jordan Halterman | ebedbb5 | 2017-08-08 15:57:50 -0700 | [diff] [blame] | 78 | import org.onosproject.store.impl.MastershipBasedTimestamp; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 79 | import org.onosproject.store.impl.Timestamped; |
Thomas Vachuska | fdbc4c2 | 2015-05-29 15:53:01 -0700 | [diff] [blame] | 80 | import org.onosproject.store.serializers.KryoNamespaces; |
Brian O'Connor | 6de2e20 | 2015-05-21 14:30:41 -0700 | [diff] [blame] | 81 | import org.onosproject.store.serializers.custom.DistributedStoreSerializers; |
Thomas Vachuska | fdbc4c2 | 2015-05-29 15:53:01 -0700 | [diff] [blame] | 82 | import org.onosproject.store.service.EventuallyConsistentMap; |
| 83 | import org.onosproject.store.service.EventuallyConsistentMapEvent; |
| 84 | import org.onosproject.store.service.EventuallyConsistentMapListener; |
| 85 | import org.onosproject.store.service.MultiValuedTimestamp; |
Jordan Halterman | c6c6ef2 | 2017-08-20 17:11:41 -0700 | [diff] [blame] | 86 | import org.onosproject.store.service.Serializer; |
Thomas Vachuska | fdbc4c2 | 2015-05-29 15:53:01 -0700 | [diff] [blame] | 87 | import org.onosproject.store.service.StorageService; |
| 88 | import org.onosproject.store.service.WallClockTimestamp; |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 89 | import org.slf4j.Logger; |
| 90 | |
Ray Milkey | 2bf5ea7 | 2017-06-01 09:03:34 -0700 | [diff] [blame] | 91 | import com.google.common.collect.FluentIterable; |
| 92 | import com.google.common.collect.ImmutableList; |
| 93 | import com.google.common.collect.Maps; |
| 94 | import com.google.common.collect.Sets; |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 95 | |
| 96 | import static com.google.common.base.Preconditions.checkArgument; |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 97 | import static com.google.common.base.Predicates.notNull; |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 98 | import static com.google.common.base.Verify.verify; |
HIGUCHI Yuta | 060da9a | 2016-03-11 19:16:35 -0800 | [diff] [blame] | 99 | import static java.util.concurrent.Executors.newCachedThreadPool; |
Marc De Leenheer | b473b9d | 2015-02-06 15:21:03 -0800 | [diff] [blame] | 100 | import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor; |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 101 | import static org.onlab.util.Tools.groupedThreads; |
Yuta HIGUCHI | 0658627 | 2014-11-25 14:27:03 -0800 | [diff] [blame] | 102 | import static org.onlab.util.Tools.minPriority; |
Marc De Leenheer | b473b9d | 2015-02-06 15:21:03 -0800 | [diff] [blame] | 103 | import static org.onosproject.cluster.ControllerNodeToNodeId.toNodeId; |
Ray Milkey | 9ef2223 | 2016-07-14 12:42:37 -0700 | [diff] [blame] | 104 | import static org.onosproject.net.device.DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED; |
| 105 | import static org.onosproject.net.device.DeviceEvent.Type.PORT_ADDED; |
| 106 | import static org.onosproject.net.device.DeviceEvent.Type.PORT_REMOVED; |
| 107 | import static org.onosproject.net.device.DeviceEvent.Type.PORT_STATS_UPDATED; |
| 108 | import static org.onosproject.net.device.DeviceEvent.Type.PORT_UPDATED; |
| 109 | import static org.onosproject.store.device.impl.GossipDeviceStoreMessageSubjects.DEVICE_ADVERTISE; |
Jonathan Hart | 46ab5cc | 2016-09-15 15:42:39 -0700 | [diff] [blame] | 110 | import static org.onosproject.store.device.impl.GossipDeviceStoreMessageSubjects.DEVICE_OFFLINE; |
| 111 | import static org.onosproject.store.device.impl.GossipDeviceStoreMessageSubjects.DEVICE_REMOVED; |
Ray Milkey | 9ef2223 | 2016-07-14 12:42:37 -0700 | [diff] [blame] | 112 | import static org.onosproject.store.device.impl.GossipDeviceStoreMessageSubjects.DEVICE_REMOVE_REQ; |
Jonathan Hart | 46ab5cc | 2016-09-15 15:42:39 -0700 | [diff] [blame] | 113 | import static org.onosproject.store.device.impl.GossipDeviceStoreMessageSubjects.DEVICE_UPDATE; |
Jonathan Hart | 46ab5cc | 2016-09-15 15:42:39 -0700 | [diff] [blame] | 114 | import static org.onosproject.store.device.impl.GossipDeviceStoreMessageSubjects.PORT_STATUS_UPDATE; |
| 115 | import static org.onosproject.store.device.impl.GossipDeviceStoreMessageSubjects.PORT_UPDATE; |
Thomas Vachuska | fdbc4c2 | 2015-05-29 15:53:01 -0700 | [diff] [blame] | 116 | import static org.onosproject.store.service.EventuallyConsistentMapEvent.Type.PUT; |
Marc De Leenheer | b473b9d | 2015-02-06 15:21:03 -0800 | [diff] [blame] | 117 | import static org.slf4j.LoggerFactory.getLogger; |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 118 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 119 | /** |
| 120 | * Manages inventory of infrastructure devices using gossip protocol to distribute |
| 121 | * information. |
| 122 | */ |
| 123 | @Component(immediate = true) |
| 124 | @Service |
| 125 | public class GossipDeviceStore |
| 126 | extends AbstractStore<DeviceEvent, DeviceStoreDelegate> |
| 127 | implements DeviceStore { |
| 128 | |
| 129 | private final Logger log = getLogger(getClass()); |
| 130 | |
Yuta HIGUCHI | bf71dff | 2014-10-14 16:02:33 -0700 | [diff] [blame] | 131 | private static final String DEVICE_NOT_FOUND = "Device with ID %s not found"; |
Marc De Leenheer | b473b9d | 2015-02-06 15:21:03 -0800 | [diff] [blame] | 132 | // Timeout in milliseconds to process device or ports on remote master node |
| 133 | private static final int REMOTE_MASTER_TIMEOUT = 1000; |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 134 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 135 | // innerMap is used to lock a Device, thus instance should never be replaced. |
| 136 | // collection of Description given from various providers |
Yuta HIGUCHI | bf71dff | 2014-10-14 16:02:33 -0700 | [diff] [blame] | 137 | private final ConcurrentMap<DeviceId, Map<ProviderId, DeviceDescriptions>> |
Thomas Vachuska | fdbc4c2 | 2015-05-29 15:53:01 -0700 | [diff] [blame] | 138 | deviceDescs = Maps.newConcurrentMap(); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 139 | |
| 140 | // cache of Device and Ports generated by compositing descriptions from providers |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 141 | private final ConcurrentMap<DeviceId, Device> devices = Maps.newConcurrentMap(); |
| 142 | private final ConcurrentMap<DeviceId, ConcurrentMap<PortNumber, Port>> devicePorts = Maps.newConcurrentMap(); |
Thomas Vachuska | fdbc4c2 | 2015-05-29 15:53:01 -0700 | [diff] [blame] | 143 | |
| 144 | private EventuallyConsistentMap<DeviceId, Map<PortNumber, PortStatistics>> devicePortStats; |
Dusan Pajin | 11ff4a8 | 2015-08-20 18:03:05 +0200 | [diff] [blame] | 145 | private EventuallyConsistentMap<DeviceId, Map<PortNumber, PortStatistics>> devicePortDeltaStats; |
Thomas Vachuska | fdbc4c2 | 2015-05-29 15:53:01 -0700 | [diff] [blame] | 146 | private final EventuallyConsistentMapListener<DeviceId, Map<PortNumber, PortStatistics>> |
| 147 | portStatsListener = new InternalPortStatsListener(); |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 148 | |
| 149 | // to be updated under Device lock |
| 150 | private final Map<DeviceId, Timestamp> offline = Maps.newHashMap(); |
| 151 | private final Map<DeviceId, Timestamp> removalRequest = Maps.newHashMap(); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 152 | |
| 153 | // available(=UP) devices |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 154 | private final Set<DeviceId> availableDevices = Sets.newConcurrentHashSet(); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 155 | |
| 156 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Yuta HIGUCHI | 093e83e | 2014-10-10 22:26:11 -0700 | [diff] [blame] | 157 | protected DeviceClockService deviceClockService; |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 158 | |
Madan Jampani | 47c9373 | 2014-10-06 20:46:08 -0700 | [diff] [blame] | 159 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Thomas Vachuska | fdbc4c2 | 2015-05-29 15:53:01 -0700 | [diff] [blame] | 160 | protected StorageService storageService; |
| 161 | |
| 162 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Madan Jampani | 47c9373 | 2014-10-06 20:46:08 -0700 | [diff] [blame] | 163 | protected ClusterCommunicationService clusterCommunicator; |
| 164 | |
Madan Jampani | 53e44e6 | 2014-10-07 12:39:51 -0700 | [diff] [blame] | 165 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 166 | protected ClusterService clusterService; |
| 167 | |
Yuta HIGUCHI | e8252bb | 2014-10-22 09:41:01 -0700 | [diff] [blame] | 168 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 169 | protected MastershipService mastershipService; |
| 170 | |
Yuta HIGUCHI | bcac499 | 2014-11-22 19:27:57 -0800 | [diff] [blame] | 171 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 172 | protected MastershipTermService termService; |
| 173 | |
Jordan Halterman | ebedbb5 | 2017-08-08 15:57:50 -0700 | [diff] [blame] | 174 | private static final Timestamp DEFAULT_TIMESTAMP = new MastershipBasedTimestamp(0, 0); |
Yuta HIGUCHI | bcac499 | 2014-11-22 19:27:57 -0800 | [diff] [blame] | 175 | |
Jordan Halterman | c6c6ef2 | 2017-08-20 17:11:41 -0700 | [diff] [blame] | 176 | protected static final Serializer SERIALIZER = Serializer.using(KryoNamespace.newBuilder() |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 177 | .register(DistributedStoreSerializers.STORE_COMMON) |
| 178 | .nextId(DistributedStoreSerializers.STORE_CUSTOM_BEGIN) |
| 179 | .register(new InternalDeviceEventSerializer(), InternalDeviceEvent.class) |
| 180 | .register(new InternalDeviceOfflineEventSerializer(), InternalDeviceOfflineEvent.class) |
Madan Jampani | 3fc72ed | 2014-10-08 12:50:27 -0700 | [diff] [blame] | 181 | .register(InternalDeviceRemovedEvent.class) |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 182 | .register(new InternalPortEventSerializer(), InternalPortEvent.class) |
| 183 | .register(new InternalPortStatusEventSerializer(), InternalPortStatusEvent.class) |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 184 | .register(DeviceAntiEntropyAdvertisement.class) |
| 185 | .register(DeviceFragmentId.class) |
| 186 | .register(PortFragmentId.class) |
HIGUCHI Yuta | e729065 | 2016-05-18 11:29:01 -0700 | [diff] [blame] | 187 | .build("GossipDevice")); |
Madan Jampani | 53e44e6 | 2014-10-07 12:39:51 -0700 | [diff] [blame] | 188 | |
Yuta HIGUCHI | 80d5659 | 2014-11-25 15:11:13 -0800 | [diff] [blame] | 189 | private ExecutorService executor; |
| 190 | |
Yuta HIGUCHI | 0658627 | 2014-11-25 14:27:03 -0800 | [diff] [blame] | 191 | private ScheduledExecutorService backgroundExecutor; |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 192 | |
Yuta HIGUCHI | 6593489 | 2014-12-04 17:47:44 -0800 | [diff] [blame] | 193 | // TODO make these anti-entropy parameters configurable |
| 194 | private long initialDelaySec = 5; |
| 195 | private long periodSec = 5; |
| 196 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 197 | @Activate |
| 198 | public void activate() { |
HIGUCHI Yuta | 060da9a | 2016-03-11 19:16:35 -0800 | [diff] [blame] | 199 | executor = newCachedThreadPool(groupedThreads("onos/device", "fg-%d", log)); |
Yuta HIGUCHI | 80d5659 | 2014-11-25 15:11:13 -0800 | [diff] [blame] | 200 | |
Yuta HIGUCHI | 0658627 | 2014-11-25 14:27:03 -0800 | [diff] [blame] | 201 | backgroundExecutor = |
HIGUCHI Yuta | 060da9a | 2016-03-11 19:16:35 -0800 | [diff] [blame] | 202 | newSingleThreadScheduledExecutor(minPriority(groupedThreads("onos/device", "bg-%d", log))); |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 203 | |
Jonathan Hart | 46ab5cc | 2016-09-15 15:42:39 -0700 | [diff] [blame] | 204 | addSubscriber(DEVICE_UPDATE, this::handleDeviceEvent); |
| 205 | addSubscriber(DEVICE_OFFLINE, this::handleDeviceOfflineEvent); |
| 206 | addSubscriber(DEVICE_REMOVE_REQ, this::handleRemoveRequest); |
| 207 | addSubscriber(DEVICE_REMOVED, this::handleDeviceRemovedEvent); |
| 208 | addSubscriber(PORT_UPDATE, this::handlePortEvent); |
| 209 | addSubscriber(PORT_STATUS_UPDATE, this::handlePortStatusEvent); |
| 210 | addSubscriber(DEVICE_ADVERTISE, this::handleDeviceAdvertisement); |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 211 | |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 212 | // start anti-entropy thread |
Yuta HIGUCHI | 0658627 | 2014-11-25 14:27:03 -0800 | [diff] [blame] | 213 | backgroundExecutor.scheduleAtFixedRate(new SendAdvertisementTask(), |
Thomas Vachuska | fdbc4c2 | 2015-05-29 15:53:01 -0700 | [diff] [blame] | 214 | initialDelaySec, periodSec, TimeUnit.SECONDS); |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 215 | |
Thomas Vachuska | fdbc4c2 | 2015-05-29 15:53:01 -0700 | [diff] [blame] | 216 | // Create a distributed map for port stats. |
| 217 | KryoNamespace.Builder deviceDataSerializer = KryoNamespace.newBuilder() |
| 218 | .register(KryoNamespaces.API) |
HIGUCHI Yuta | 03666a3 | 2016-05-18 11:49:09 -0700 | [diff] [blame] | 219 | .nextId(KryoNamespaces.BEGIN_USER_CUSTOM_ID) |
| 220 | .register(MultiValuedTimestamp.class); |
Thomas Vachuska | fdbc4c2 | 2015-05-29 15:53:01 -0700 | [diff] [blame] | 221 | |
| 222 | devicePortStats = storageService.<DeviceId, Map<PortNumber, PortStatistics>>eventuallyConsistentMapBuilder() |
| 223 | .withName("port-stats") |
| 224 | .withSerializer(deviceDataSerializer) |
| 225 | .withAntiEntropyPeriod(5, TimeUnit.SECONDS) |
Madan Jampani | bcf1a48 | 2015-06-24 19:05:56 -0700 | [diff] [blame] | 226 | .withTimestampProvider((k, v) -> new WallClockTimestamp()) |
Thomas Vachuska | fdbc4c2 | 2015-05-29 15:53:01 -0700 | [diff] [blame] | 227 | .withTombstonesDisabled() |
| 228 | .build(); |
Dusan Pajin | 11ff4a8 | 2015-08-20 18:03:05 +0200 | [diff] [blame] | 229 | devicePortDeltaStats = storageService.<DeviceId, Map<PortNumber, PortStatistics>> |
| 230 | eventuallyConsistentMapBuilder() |
| 231 | .withName("port-stats-delta") |
| 232 | .withSerializer(deviceDataSerializer) |
| 233 | .withAntiEntropyPeriod(5, TimeUnit.SECONDS) |
| 234 | .withTimestampProvider((k, v) -> new WallClockTimestamp()) |
| 235 | .withTombstonesDisabled() |
| 236 | .build(); |
Thomas Vachuska | fdbc4c2 | 2015-05-29 15:53:01 -0700 | [diff] [blame] | 237 | devicePortStats.addListener(portStatsListener); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 238 | log.info("Started"); |
| 239 | } |
| 240 | |
Jonathan Hart | 46ab5cc | 2016-09-15 15:42:39 -0700 | [diff] [blame] | 241 | private <M> void addSubscriber(MessageSubject subject, Consumer<M> handler) { |
| 242 | clusterCommunicator.addSubscriber(subject, SERIALIZER::decode, handler, executor); |
| 243 | } |
| 244 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 245 | @Deactivate |
| 246 | public void deactivate() { |
Frank Wang | e0eb5ce | 2016-07-01 18:21:25 +0800 | [diff] [blame] | 247 | devicePortStats.removeListener(portStatsListener); |
Madan Jampani | 632f16b | 2015-08-11 12:42:59 -0700 | [diff] [blame] | 248 | devicePortStats.destroy(); |
Dusan Pajin | 11ff4a8 | 2015-08-20 18:03:05 +0200 | [diff] [blame] | 249 | devicePortDeltaStats.destroy(); |
Yuta HIGUCHI | 80d5659 | 2014-11-25 15:11:13 -0800 | [diff] [blame] | 250 | executor.shutdownNow(); |
| 251 | |
Yuta HIGUCHI | 0658627 | 2014-11-25 14:27:03 -0800 | [diff] [blame] | 252 | backgroundExecutor.shutdownNow(); |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 253 | try { |
Yuta HIGUCHI | c578359 | 2014-12-05 11:13:29 -0800 | [diff] [blame] | 254 | if (!backgroundExecutor.awaitTermination(5, TimeUnit.SECONDS)) { |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 255 | log.error("Timeout during executor shutdown"); |
| 256 | } |
| 257 | } catch (InterruptedException e) { |
| 258 | log.error("Error during executor shutdown", e); |
| 259 | } |
| 260 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 261 | deviceDescs.clear(); |
| 262 | devices.clear(); |
| 263 | devicePorts.clear(); |
| 264 | availableDevices.clear(); |
Jonathan Hart | 46ab5cc | 2016-09-15 15:42:39 -0700 | [diff] [blame] | 265 | clusterCommunicator.removeSubscriber(DEVICE_UPDATE); |
| 266 | clusterCommunicator.removeSubscriber(DEVICE_OFFLINE); |
| 267 | clusterCommunicator.removeSubscriber(DEVICE_REMOVE_REQ); |
| 268 | clusterCommunicator.removeSubscriber(DEVICE_REMOVED); |
| 269 | clusterCommunicator.removeSubscriber(PORT_UPDATE); |
| 270 | clusterCommunicator.removeSubscriber(PORT_STATUS_UPDATE); |
| 271 | clusterCommunicator.removeSubscriber(DEVICE_ADVERTISE); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 272 | log.info("Stopped"); |
| 273 | } |
| 274 | |
| 275 | @Override |
| 276 | public int getDeviceCount() { |
| 277 | return devices.size(); |
| 278 | } |
| 279 | |
| 280 | @Override |
mskala | 0d0c683 | 2017-07-12 11:21:23 +0200 | [diff] [blame] | 281 | public int getAvailableDeviceCount() { |
| 282 | return availableDevices.size(); |
| 283 | } |
| 284 | |
| 285 | @Override |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 286 | public Iterable<Device> getDevices() { |
| 287 | return Collections.unmodifiableCollection(devices.values()); |
| 288 | } |
| 289 | |
| 290 | @Override |
Yuta HIGUCHI | f1f2ac0 | 2014-11-26 14:02:22 -0800 | [diff] [blame] | 291 | public Iterable<Device> getAvailableDevices() { |
| 292 | return FluentIterable.from(getDevices()) |
Sho SHIMIZU | 06a6c9f | 2015-06-12 14:49:06 -0700 | [diff] [blame] | 293 | .filter(input -> isAvailable(input.id())); |
Yuta HIGUCHI | f1f2ac0 | 2014-11-26 14:02:22 -0800 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | @Override |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 297 | public Device getDevice(DeviceId deviceId) { |
| 298 | return devices.get(deviceId); |
| 299 | } |
| 300 | |
| 301 | @Override |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 302 | public synchronized DeviceEvent createOrUpdateDevice(ProviderId providerId, |
Thomas Vachuska | fdbc4c2 | 2015-05-29 15:53:01 -0700 | [diff] [blame] | 303 | DeviceId deviceId, |
| 304 | DeviceDescription deviceDescription) { |
Marc De Leenheer | b473b9d | 2015-02-06 15:21:03 -0800 | [diff] [blame] | 305 | NodeId localNode = clusterService.getLocalNode().id(); |
| 306 | NodeId deviceNode = mastershipService.getMasterFor(deviceId); |
Jordan Halterman | 4ec25fc | 2017-12-19 14:56:08 -0800 | [diff] [blame] | 307 | boolean isMaster = localNode.equals(deviceNode); |
Marc De Leenheer | b473b9d | 2015-02-06 15:21:03 -0800 | [diff] [blame] | 308 | |
| 309 | // Process device update only if we're the master, |
| 310 | // otherwise signal the actual master. |
| 311 | DeviceEvent deviceEvent = null; |
Marc De Leenheer | b473b9d | 2015-02-06 15:21:03 -0800 | [diff] [blame] | 312 | |
Jordan Halterman | ebedbb5 | 2017-08-08 15:57:50 -0700 | [diff] [blame] | 313 | // If this node is the master for the device, acquire a new timestamp. Otherwise, |
| 314 | // use a 0,0 or tombstone timestamp to create the device if it doesn't already exist. |
Jordan Halterman | 4ec25fc | 2017-12-19 14:56:08 -0800 | [diff] [blame] | 315 | Timestamp newTimestamp; |
| 316 | try { |
| 317 | newTimestamp = isMaster |
| 318 | ? deviceClockService.getTimestamp(deviceId) |
| 319 | : removalRequest.getOrDefault(deviceId, DEFAULT_TIMESTAMP); |
| 320 | } catch (IllegalStateException e) { |
Yuta HIGUCHI | 1edc36b | 2018-01-24 23:39:06 -0800 | [diff] [blame] | 321 | newTimestamp = removalRequest.getOrDefault(deviceId, DEFAULT_TIMESTAMP); |
Jordan Halterman | 4ec25fc | 2017-12-19 14:56:08 -0800 | [diff] [blame] | 322 | isMaster = false; |
| 323 | } |
Jordan Halterman | ebedbb5 | 2017-08-08 15:57:50 -0700 | [diff] [blame] | 324 | final Timestamped<DeviceDescription> deltaDesc = new Timestamped<>(deviceDescription, newTimestamp); |
| 325 | final Timestamped<DeviceDescription> mergedDesc; |
| 326 | final Map<ProviderId, DeviceDescriptions> device = getOrCreateDeviceDescriptionsMap(deviceId); |
Marc De Leenheer | b473b9d | 2015-02-06 15:21:03 -0800 | [diff] [blame] | 327 | |
Jordan Halterman | ebedbb5 | 2017-08-08 15:57:50 -0700 | [diff] [blame] | 328 | synchronized (device) { |
| 329 | deviceEvent = createOrUpdateDeviceInternal(providerId, deviceId, deltaDesc); |
Jordan Halterman | 8a0b397 | 2017-08-15 16:14:18 -0700 | [diff] [blame] | 330 | if (deviceEvent == null) { |
| 331 | return null; |
| 332 | } |
Jordan Halterman | ebedbb5 | 2017-08-08 15:57:50 -0700 | [diff] [blame] | 333 | mergedDesc = device.get(providerId).getDeviceDesc(); |
| 334 | } |
Marc De Leenheer | b473b9d | 2015-02-06 15:21:03 -0800 | [diff] [blame] | 335 | |
Jordan Halterman | ebedbb5 | 2017-08-08 15:57:50 -0700 | [diff] [blame] | 336 | // If this node is the master for the device, update peers. |
| 337 | if (isMaster) { |
Jordan Halterman | 8a0b397 | 2017-08-15 16:14:18 -0700 | [diff] [blame] | 338 | log.debug("Notifying peers of a device update topology event for providerId: {} and deviceId: {}", |
| 339 | providerId, deviceId); |
| 340 | notifyPeers(new InternalDeviceEvent(providerId, deviceId, mergedDesc)); |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 341 | } |
Marc De Leenheer | b473b9d | 2015-02-06 15:21:03 -0800 | [diff] [blame] | 342 | |
| 343 | return deviceEvent; |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 344 | } |
| 345 | |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 346 | private DeviceEvent createOrUpdateDeviceInternal(ProviderId providerId, |
Thomas Vachuska | fdbc4c2 | 2015-05-29 15:53:01 -0700 | [diff] [blame] | 347 | DeviceId deviceId, |
| 348 | Timestamped<DeviceDescription> deltaDesc) { |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 349 | |
| 350 | // Collection of DeviceDescriptions for a Device |
Yuta HIGUCHI | 04c6b3f | 2014-11-07 14:47:01 -0800 | [diff] [blame] | 351 | Map<ProviderId, DeviceDescriptions> device |
Thomas Vachuska | fdbc4c2 | 2015-05-29 15:53:01 -0700 | [diff] [blame] | 352 | = getOrCreateDeviceDescriptionsMap(deviceId); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 353 | |
Yuta HIGUCHI | 04c6b3f | 2014-11-07 14:47:01 -0800 | [diff] [blame] | 354 | synchronized (device) { |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 355 | // locking per device |
| 356 | |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 357 | if (isDeviceRemoved(deviceId, deltaDesc.timestamp())) { |
| 358 | log.debug("Ignoring outdated event: {}", deltaDesc); |
| 359 | return null; |
| 360 | } |
| 361 | |
Yuta HIGUCHI | 04c6b3f | 2014-11-07 14:47:01 -0800 | [diff] [blame] | 362 | DeviceDescriptions descs = getOrCreateProviderDeviceDescriptions(device, providerId, deltaDesc); |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 363 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 364 | final Device oldDevice = devices.get(deviceId); |
| 365 | final Device newDevice; |
| 366 | |
| 367 | if (deltaDesc == descs.getDeviceDesc() || |
Thomas Vachuska | fdbc4c2 | 2015-05-29 15:53:01 -0700 | [diff] [blame] | 368 | deltaDesc.isNewer(descs.getDeviceDesc())) { |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 369 | // on new device or valid update |
| 370 | descs.putDeviceDesc(deltaDesc); |
Yuta HIGUCHI | 04c6b3f | 2014-11-07 14:47:01 -0800 | [diff] [blame] | 371 | newDevice = composeDevice(deviceId, device); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 372 | } else { |
| 373 | // outdated event, ignored. |
| 374 | return null; |
| 375 | } |
| 376 | if (oldDevice == null) { |
helenyrwu | fd296b6 | 2016-06-22 17:43:02 -0700 | [diff] [blame] | 377 | // REGISTER |
| 378 | if (!deltaDesc.value().isDefaultAvailable()) { |
Jordan Halterman | 5b78dc8 | 2017-04-19 07:55:48 -0700 | [diff] [blame] | 379 | return registerDevice(providerId, newDevice, deltaDesc.timestamp()); |
helenyrwu | fd296b6 | 2016-06-22 17:43:02 -0700 | [diff] [blame] | 380 | } |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 381 | // ADD |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 382 | return createDevice(providerId, newDevice, deltaDesc.timestamp()); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 383 | } else { |
| 384 | // UPDATE or ignore (no change or stale) |
helenyrwu | fd296b6 | 2016-06-22 17:43:02 -0700 | [diff] [blame] | 385 | return updateDevice(providerId, oldDevice, newDevice, deltaDesc.timestamp(), |
| 386 | deltaDesc.value().isDefaultAvailable()); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 387 | } |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | // Creates the device and returns the appropriate event if necessary. |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 392 | // Guarded by deviceDescs value (=Device lock) |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 393 | private DeviceEvent createDevice(ProviderId providerId, |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 394 | Device newDevice, Timestamp timestamp) { |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 395 | |
| 396 | // update composed device cache |
| 397 | Device oldDevice = devices.putIfAbsent(newDevice.id(), newDevice); |
| 398 | verify(oldDevice == null, |
Thomas Vachuska | fdbc4c2 | 2015-05-29 15:53:01 -0700 | [diff] [blame] | 399 | "Unexpected Device in cache. PID:%s [old=%s, new=%s]", |
| 400 | providerId, oldDevice, newDevice); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 401 | |
| 402 | if (!providerId.isAncillary()) { |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 403 | markOnline(newDevice.id(), timestamp); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 404 | } |
| 405 | |
Jordan Halterman | ebedbb5 | 2017-08-08 15:57:50 -0700 | [diff] [blame] | 406 | log.debug("Device {} added", newDevice.id()); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 407 | return new DeviceEvent(DeviceEvent.Type.DEVICE_ADDED, newDevice, null); |
| 408 | } |
| 409 | |
| 410 | // Updates the device and returns the appropriate event if necessary. |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 411 | // Guarded by deviceDescs value (=Device lock) |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 412 | private DeviceEvent updateDevice(ProviderId providerId, |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 413 | Device oldDevice, |
helenyrwu | fd296b6 | 2016-06-22 17:43:02 -0700 | [diff] [blame] | 414 | Device newDevice, Timestamp newTimestamp, |
| 415 | boolean forceAvailable) { |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 416 | // We allow only certain attributes to trigger update |
Thomas Vachuska | 56dbeb1 | 2014-10-22 16:40:44 -0700 | [diff] [blame] | 417 | boolean propertiesChanged = |
| 418 | !Objects.equals(oldDevice.hwVersion(), newDevice.hwVersion()) || |
Thomas Vachuska | daaa42d | 2015-04-21 16:21:37 -0700 | [diff] [blame] | 419 | !Objects.equals(oldDevice.swVersion(), newDevice.swVersion()) || |
mskala | 4b2081a | 2017-06-12 16:39:50 +0200 | [diff] [blame] | 420 | !Objects.equals(oldDevice.providerId(), newDevice.providerId()) || |
| 421 | !Objects.equals(oldDevice.chassisId(), newDevice.chassisId()); |
Thomas Vachuska | 56dbeb1 | 2014-10-22 16:40:44 -0700 | [diff] [blame] | 422 | boolean annotationsChanged = |
| 423 | !AnnotationsUtil.isEqual(oldDevice.annotations(), newDevice.annotations()); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 424 | |
Thomas Vachuska | 56dbeb1 | 2014-10-22 16:40:44 -0700 | [diff] [blame] | 425 | // Primary providers can respond to all changes, but ancillary ones |
| 426 | // should respond only to annotation changes. |
alshabib | dc5d8bd | 2015-11-02 15:41:29 -0800 | [diff] [blame] | 427 | DeviceEvent event = null; |
Thomas Vachuska | 56dbeb1 | 2014-10-22 16:40:44 -0700 | [diff] [blame] | 428 | if ((providerId.isAncillary() && annotationsChanged) || |
| 429 | (!providerId.isAncillary() && (propertiesChanged || annotationsChanged))) { |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 430 | boolean replaced = devices.replace(newDevice.id(), oldDevice, newDevice); |
| 431 | if (!replaced) { |
| 432 | verify(replaced, |
Thomas Vachuska | fdbc4c2 | 2015-05-29 15:53:01 -0700 | [diff] [blame] | 433 | "Replacing devices cache failed. PID:%s [expected:%s, found:%s, new=%s]", |
Jian Li | 68c4fc4 | 2016-01-11 16:07:03 -0800 | [diff] [blame] | 434 | providerId, oldDevice, devices.get(newDevice.id()), newDevice); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 435 | } |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 436 | |
Jordan Halterman | ebedbb5 | 2017-08-08 15:57:50 -0700 | [diff] [blame] | 437 | log.debug("Device {} updated", newDevice.id()); |
alshabib | dc5d8bd | 2015-11-02 15:41:29 -0800 | [diff] [blame] | 438 | event = new DeviceEvent(DeviceEvent.Type.DEVICE_UPDATED, newDevice, null); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 439 | } |
alshabib | dc5d8bd | 2015-11-02 15:41:29 -0800 | [diff] [blame] | 440 | |
helenyrwu | fd296b6 | 2016-06-22 17:43:02 -0700 | [diff] [blame] | 441 | if (!providerId.isAncillary() && forceAvailable) { |
alshabib | dc5d8bd | 2015-11-02 15:41:29 -0800 | [diff] [blame] | 442 | boolean wasOnline = availableDevices.contains(newDevice.id()); |
| 443 | markOnline(newDevice.id(), newTimestamp); |
| 444 | if (!wasOnline) { |
| 445 | notifyDelegateIfNotNull(new DeviceEvent(DEVICE_AVAILABILITY_CHANGED, newDevice, null)); |
| 446 | } |
| 447 | } |
| 448 | return event; |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 449 | } |
| 450 | |
Jordan Halterman | 5b78dc8 | 2017-04-19 07:55:48 -0700 | [diff] [blame] | 451 | private DeviceEvent registerDevice(ProviderId providerId, Device newDevice, Timestamp newTimestamp) { |
helenyrwu | fd296b6 | 2016-06-22 17:43:02 -0700 | [diff] [blame] | 452 | // update composed device cache |
| 453 | Device oldDevice = devices.putIfAbsent(newDevice.id(), newDevice); |
| 454 | verify(oldDevice == null, |
| 455 | "Unexpected Device in cache. PID:%s [old=%s, new=%s]", |
| 456 | providerId, oldDevice, newDevice); |
| 457 | |
| 458 | if (!providerId.isAncillary()) { |
Jordan Halterman | 5b78dc8 | 2017-04-19 07:55:48 -0700 | [diff] [blame] | 459 | markOffline(newDevice.id(), newTimestamp); |
helenyrwu | fd296b6 | 2016-06-22 17:43:02 -0700 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | return new DeviceEvent(DeviceEvent.Type.DEVICE_ADDED, newDevice, null); |
| 463 | } |
| 464 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 465 | @Override |
| 466 | public DeviceEvent markOffline(DeviceId deviceId) { |
Jordan Halterman | 5b78dc8 | 2017-04-19 07:55:48 -0700 | [diff] [blame] | 467 | return markOffline(deviceId, deviceClockService.getTimestamp(deviceId)); |
| 468 | } |
| 469 | |
| 470 | private DeviceEvent markOffline(DeviceId deviceId, Timestamp timestamp) { |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 471 | final DeviceEvent event = markOfflineInternal(deviceId, timestamp); |
Madan Jampani | 2532253 | 2014-10-08 11:20:38 -0700 | [diff] [blame] | 472 | if (event != null) { |
Madan Jampani | f2af771 | 2015-05-29 18:43:52 -0700 | [diff] [blame] | 473 | log.debug("Notifying peers of a device offline topology event for deviceId: {} {}", |
Thomas Vachuska | fdbc4c2 | 2015-05-29 15:53:01 -0700 | [diff] [blame] | 474 | deviceId, timestamp); |
Jonathan Hart | 7d656f4 | 2015-01-27 14:07:23 -0800 | [diff] [blame] | 475 | notifyPeers(new InternalDeviceOfflineEvent(deviceId, timestamp)); |
Madan Jampani | 2532253 | 2014-10-08 11:20:38 -0700 | [diff] [blame] | 476 | } |
| 477 | return event; |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | private DeviceEvent markOfflineInternal(DeviceId deviceId, Timestamp timestamp) { |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 481 | Map<ProviderId, DeviceDescriptions> providerDescs |
Thomas Vachuska | fdbc4c2 | 2015-05-29 15:53:01 -0700 | [diff] [blame] | 482 | = getOrCreateDeviceDescriptionsMap(deviceId); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 483 | |
| 484 | // locking device |
| 485 | synchronized (providerDescs) { |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 486 | // accept off-line if given timestamp is newer than |
| 487 | // the latest Timestamp from Primary provider |
| 488 | DeviceDescriptions primDescs = getPrimaryDescriptions(providerDescs); |
jaegonkim | 3b5d416 | 2018-01-13 10:52:02 +0900 | [diff] [blame] | 489 | if (primDescs != null) { |
| 490 | Timestamp lastTimestamp = primDescs.getLatestTimestamp(); |
| 491 | if (lastTimestamp == null) { |
| 492 | lastTimestamp = deviceClockService.getTimestamp(deviceId); |
| 493 | } |
| 494 | if (timestamp.compareTo(lastTimestamp) <= 0) { |
| 495 | // outdated event ignore |
| 496 | return null; |
| 497 | } |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | offline.put(deviceId, timestamp); |
| 501 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 502 | Device device = devices.get(deviceId); |
| 503 | if (device == null) { |
| 504 | return null; |
| 505 | } |
| 506 | boolean removed = availableDevices.remove(deviceId); |
| 507 | if (removed) { |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 508 | return new DeviceEvent(DEVICE_AVAILABILITY_CHANGED, device, null); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 509 | } |
| 510 | return null; |
| 511 | } |
| 512 | } |
| 513 | |
Yuta HIGUCHI | c2e6815 | 2016-08-16 13:47:36 -0700 | [diff] [blame] | 514 | @Override |
helenyrwu | fd296b6 | 2016-06-22 17:43:02 -0700 | [diff] [blame] | 515 | public boolean markOnline(DeviceId deviceId) { |
| 516 | if (devices.containsKey(deviceId)) { |
| 517 | final Timestamp timestamp = deviceClockService.getTimestamp(deviceId); |
| 518 | Map<?, ?> deviceLock = getOrCreateDeviceDescriptionsMap(deviceId); |
| 519 | synchronized (deviceLock) { |
| 520 | if (markOnline(deviceId, timestamp)) { |
| 521 | notifyDelegate(new DeviceEvent(DEVICE_AVAILABILITY_CHANGED, getDevice(deviceId), null)); |
| 522 | return true; |
| 523 | } else { |
| 524 | return false; |
| 525 | } |
| 526 | } |
| 527 | } |
| 528 | log.warn("Device {} does not exist in store", deviceId); |
| 529 | return false; |
| 530 | |
| 531 | } |
| 532 | |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 533 | /** |
| 534 | * Marks the device as available if the given timestamp is not outdated, |
| 535 | * compared to the time the device has been marked offline. |
| 536 | * |
Thomas Vachuska | fdbc4c2 | 2015-05-29 15:53:01 -0700 | [diff] [blame] | 537 | * @param deviceId identifier of the device |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 538 | * @param timestamp of the event triggering this change. |
| 539 | * @return true if availability change request was accepted and changed the state |
| 540 | */ |
| 541 | // Guarded by deviceDescs value (=Device lock) |
| 542 | private boolean markOnline(DeviceId deviceId, Timestamp timestamp) { |
| 543 | // accept on-line if given timestamp is newer than |
| 544 | // the latest offline request Timestamp |
| 545 | Timestamp offlineTimestamp = offline.get(deviceId); |
| 546 | if (offlineTimestamp == null || |
Thomas Vachuska | fdbc4c2 | 2015-05-29 15:53:01 -0700 | [diff] [blame] | 547 | offlineTimestamp.compareTo(timestamp) < 0) { |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 548 | |
| 549 | offline.remove(deviceId); |
| 550 | return availableDevices.add(deviceId); |
| 551 | } |
| 552 | return false; |
| 553 | } |
| 554 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 555 | @Override |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 556 | public synchronized List<DeviceEvent> updatePorts(ProviderId providerId, |
Thomas Vachuska | fdbc4c2 | 2015-05-29 15:53:01 -0700 | [diff] [blame] | 557 | DeviceId deviceId, |
| 558 | List<PortDescription> portDescriptions) { |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 559 | |
Marc De Leenheer | b473b9d | 2015-02-06 15:21:03 -0800 | [diff] [blame] | 560 | NodeId localNode = clusterService.getLocalNode().id(); |
| 561 | // TODO: It might be negligible, but this will have negative impact to topology discovery performance, |
| 562 | // since it will trigger distributed store read. |
| 563 | // Also, it'll probably be better if side-way communication happened on ConfigurationProvider, etc. |
| 564 | // outside Device subsystem. so that we don't have to modify both Device and Link stores. |
| 565 | // If we don't care much about topology performance, then it might be OK. |
| 566 | NodeId deviceNode = mastershipService.getMasterFor(deviceId); |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame] | 567 | |
Marc De Leenheer | b473b9d | 2015-02-06 15:21:03 -0800 | [diff] [blame] | 568 | // Process port update only if we're the master of the device, |
| 569 | // otherwise signal the actual master. |
| 570 | List<DeviceEvent> deviceEvents = null; |
| 571 | if (localNode.equals(deviceNode)) { |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame] | 572 | |
Marc De Leenheer | b473b9d | 2015-02-06 15:21:03 -0800 | [diff] [blame] | 573 | final Timestamp newTimestamp; |
| 574 | try { |
| 575 | newTimestamp = deviceClockService.getTimestamp(deviceId); |
| 576 | } catch (IllegalStateException e) { |
| 577 | log.info("Timestamp was not available for device {}", deviceId); |
| 578 | log.debug(" discarding {}", portDescriptions); |
| 579 | // Failed to generate timestamp. |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame] | 580 | |
Marc De Leenheer | b473b9d | 2015-02-06 15:21:03 -0800 | [diff] [blame] | 581 | // Possible situation: |
| 582 | // Device connected and became master for short period of time, |
| 583 | // but lost mastership before this instance had the chance to |
| 584 | // retrieve term information. |
| 585 | |
| 586 | // Information dropped here is expected to be recoverable by |
| 587 | // device probing after mastership change |
| 588 | |
| 589 | return Collections.emptyList(); |
| 590 | } |
| 591 | log.debug("timestamp for {} {}", deviceId, newTimestamp); |
| 592 | |
| 593 | final Timestamped<List<PortDescription>> timestampedInput |
| 594 | = new Timestamped<>(portDescriptions, newTimestamp); |
| 595 | final Timestamped<List<PortDescription>> merged; |
| 596 | |
| 597 | final Map<ProviderId, DeviceDescriptions> device = getOrCreateDeviceDescriptionsMap(deviceId); |
| 598 | |
| 599 | synchronized (device) { |
| 600 | deviceEvents = updatePortsInternal(providerId, deviceId, timestampedInput); |
| 601 | final DeviceDescriptions descs = device.get(providerId); |
| 602 | List<PortDescription> mergedList = |
| 603 | FluentIterable.from(portDescriptions) |
Sho SHIMIZU | 7462641 | 2015-09-11 11:46:27 -0700 | [diff] [blame] | 604 | .transform(input -> |
| 605 | // lookup merged port description |
| 606 | descs.getPortDesc(input.portNumber()).value() |
| 607 | ).toList(); |
Sho SHIMIZU | a0fda21 | 2015-06-10 19:15:38 -0700 | [diff] [blame] | 608 | merged = new Timestamped<>(mergedList, newTimestamp); |
Marc De Leenheer | b473b9d | 2015-02-06 15:21:03 -0800 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | if (!deviceEvents.isEmpty()) { |
Madan Jampani | f2af771 | 2015-05-29 18:43:52 -0700 | [diff] [blame] | 612 | log.debug("Notifying peers of a ports update topology event for providerId: {} and deviceId: {}", |
Thomas Vachuska | fdbc4c2 | 2015-05-29 15:53:01 -0700 | [diff] [blame] | 613 | providerId, deviceId); |
Marc De Leenheer | b473b9d | 2015-02-06 15:21:03 -0800 | [diff] [blame] | 614 | notifyPeers(new InternalPortEvent(providerId, deviceId, merged)); |
| 615 | } |
| 616 | |
| 617 | } else { |
Ray Milkey | 2bf5ea7 | 2017-06-01 09:03:34 -0700 | [diff] [blame] | 618 | return Collections.emptyList(); |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame] | 619 | } |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 620 | |
Ayaka Koshibe | eeb9510 | 2015-02-26 16:31:49 -0800 | [diff] [blame] | 621 | return deviceEvents == null ? Collections.emptyList() : deviceEvents; |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 622 | } |
| 623 | |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 624 | private List<DeviceEvent> updatePortsInternal(ProviderId providerId, |
Thomas Vachuska | fdbc4c2 | 2015-05-29 15:53:01 -0700 | [diff] [blame] | 625 | DeviceId deviceId, |
| 626 | Timestamped<List<PortDescription>> portDescriptions) { |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 627 | |
| 628 | Device device = devices.get(deviceId); |
Ray Milkey | 9ef2223 | 2016-07-14 12:42:37 -0700 | [diff] [blame] | 629 | if (device == null) { |
| 630 | log.debug("Device is no longer valid: {}", deviceId); |
| 631 | return Collections.emptyList(); |
| 632 | } |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 633 | |
Yuta HIGUCHI | bf71dff | 2014-10-14 16:02:33 -0700 | [diff] [blame] | 634 | Map<ProviderId, DeviceDescriptions> descsMap = deviceDescs.get(deviceId); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 635 | checkArgument(descsMap != null, DEVICE_NOT_FOUND, deviceId); |
| 636 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 637 | List<DeviceEvent> events = new ArrayList<>(); |
| 638 | synchronized (descsMap) { |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 639 | |
| 640 | if (isDeviceRemoved(deviceId, portDescriptions.timestamp())) { |
| 641 | log.debug("Ignoring outdated events: {}", portDescriptions); |
Sho SHIMIZU | 7b7eabc | 2015-06-10 20:30:19 -0700 | [diff] [blame] | 642 | return Collections.emptyList(); |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 643 | } |
| 644 | |
| 645 | DeviceDescriptions descs = descsMap.get(providerId); |
| 646 | // every provider must provide DeviceDescription. |
| 647 | checkArgument(descs != null, |
Thomas Vachuska | fdbc4c2 | 2015-05-29 15:53:01 -0700 | [diff] [blame] | 648 | "Device description for Device ID %s from Provider %s was not found", |
| 649 | deviceId, providerId); |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 650 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 651 | Map<PortNumber, Port> ports = getPortMap(deviceId); |
| 652 | |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 653 | final Timestamp newTimestamp = portDescriptions.timestamp(); |
| 654 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 655 | // Add new ports |
| 656 | Set<PortNumber> processed = new HashSet<>(); |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 657 | for (PortDescription portDescription : portDescriptions.value()) { |
| 658 | final PortNumber number = portDescription.portNumber(); |
| 659 | processed.add(number); |
| 660 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 661 | final Port oldPort = ports.get(number); |
| 662 | final Port newPort; |
| 663 | |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 664 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 665 | final Timestamped<PortDescription> existingPortDesc = descs.getPortDesc(number); |
| 666 | if (existingPortDesc == null || |
Thomas Vachuska | fdbc4c2 | 2015-05-29 15:53:01 -0700 | [diff] [blame] | 667 | newTimestamp.compareTo(existingPortDesc.timestamp()) >= 0) { |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 668 | // on new port or valid update |
| 669 | // update description |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 670 | descs.putPortDesc(new Timestamped<>(portDescription, |
Thomas Vachuska | fdbc4c2 | 2015-05-29 15:53:01 -0700 | [diff] [blame] | 671 | portDescriptions.timestamp())); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 672 | newPort = composePort(device, number, descsMap); |
| 673 | } else { |
| 674 | // outdated event, ignored. |
| 675 | continue; |
| 676 | } |
| 677 | |
| 678 | events.add(oldPort == null ? |
| 679 | createPort(device, newPort, ports) : |
| 680 | updatePort(device, oldPort, newPort, ports)); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | events.addAll(pruneOldPorts(device, ports, processed)); |
| 684 | } |
| 685 | return FluentIterable.from(events).filter(notNull()).toList(); |
| 686 | } |
| 687 | |
| 688 | // Creates a new port based on the port description adds it to the map and |
| 689 | // Returns corresponding event. |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 690 | // Guarded by deviceDescs value (=Device lock) |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 691 | private DeviceEvent createPort(Device device, Port newPort, |
| 692 | Map<PortNumber, Port> ports) { |
| 693 | ports.put(newPort.number(), newPort); |
| 694 | return new DeviceEvent(PORT_ADDED, device, newPort); |
| 695 | } |
| 696 | |
| 697 | // Checks if the specified port requires update and if so, it replaces the |
| 698 | // existing entry in the map and returns corresponding event. |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 699 | // Guarded by deviceDescs value (=Device lock) |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 700 | private DeviceEvent updatePort(Device device, Port oldPort, |
| 701 | Port newPort, |
| 702 | Map<PortNumber, Port> ports) { |
Michal Mach | ce77433 | 2017-01-25 11:02:55 +0100 | [diff] [blame] | 703 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 704 | if (oldPort.isEnabled() != newPort.isEnabled() || |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 705 | oldPort.type() != newPort.type() || |
| 706 | oldPort.portSpeed() != newPort.portSpeed() || |
| 707 | !AnnotationsUtil.isEqual(oldPort.annotations(), newPort.annotations())) { |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 708 | ports.put(oldPort.number(), newPort); |
| 709 | return new DeviceEvent(PORT_UPDATED, device, newPort); |
| 710 | } |
| 711 | return null; |
| 712 | } |
| 713 | |
Michal Mach | ce77433 | 2017-01-25 11:02:55 +0100 | [diff] [blame] | 714 | private DeviceEvent removePort(DeviceId deviceId, PortNumber portNumber) { |
| 715 | |
| 716 | log.info("Deleted port: " + deviceId.toString() + "/" + portNumber.toString()); |
| 717 | Port deletedPort = devicePorts.get(deviceId).remove(portNumber); |
| 718 | |
| 719 | return new DeviceEvent(PORT_REMOVED, getDevice(deviceId), deletedPort); |
| 720 | } |
| 721 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 722 | // Prunes the specified list of ports based on which ports are in the |
| 723 | // processed list and returns list of corresponding events. |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 724 | // Guarded by deviceDescs value (=Device lock) |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 725 | private List<DeviceEvent> pruneOldPorts(Device device, |
| 726 | Map<PortNumber, Port> ports, |
| 727 | Set<PortNumber> processed) { |
| 728 | List<DeviceEvent> events = new ArrayList<>(); |
Yuta HIGUCHI | 0264907 | 2014-10-15 23:28:20 -0700 | [diff] [blame] | 729 | Iterator<Entry<PortNumber, Port>> iterator = ports.entrySet().iterator(); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 730 | while (iterator.hasNext()) { |
Yuta HIGUCHI | 0264907 | 2014-10-15 23:28:20 -0700 | [diff] [blame] | 731 | Entry<PortNumber, Port> e = iterator.next(); |
| 732 | PortNumber portNumber = e.getKey(); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 733 | if (!processed.contains(portNumber)) { |
Yuta HIGUCHI | 0264907 | 2014-10-15 23:28:20 -0700 | [diff] [blame] | 734 | events.add(new DeviceEvent(PORT_REMOVED, device, e.getValue())); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 735 | iterator.remove(); |
| 736 | } |
| 737 | } |
| 738 | return events; |
| 739 | } |
| 740 | |
| 741 | // Gets the map of ports for the specified device; if one does not already |
| 742 | // exist, it creates and registers a new one. |
| 743 | private ConcurrentMap<PortNumber, Port> getPortMap(DeviceId deviceId) { |
Yuta HIGUCHI | c2e6815 | 2016-08-16 13:47:36 -0700 | [diff] [blame] | 744 | return devicePorts.computeIfAbsent(deviceId, k -> new ConcurrentHashMap<>()); |
Yuta HIGUCHI | 0d6a5e6 | 2014-10-03 15:54:09 -0700 | [diff] [blame] | 745 | } |
| 746 | |
Yuta HIGUCHI | bf71dff | 2014-10-14 16:02:33 -0700 | [diff] [blame] | 747 | private Map<ProviderId, DeviceDescriptions> getOrCreateDeviceDescriptionsMap( |
Yuta HIGUCHI | 0d6a5e6 | 2014-10-03 15:54:09 -0700 | [diff] [blame] | 748 | DeviceId deviceId) { |
Yuta HIGUCHI | bf71dff | 2014-10-14 16:02:33 -0700 | [diff] [blame] | 749 | Map<ProviderId, DeviceDescriptions> r; |
| 750 | r = deviceDescs.get(deviceId); |
| 751 | if (r == null) { |
Sho SHIMIZU | a0fda21 | 2015-06-10 19:15:38 -0700 | [diff] [blame] | 752 | r = new HashMap<>(); |
Yuta HIGUCHI | bf71dff | 2014-10-14 16:02:33 -0700 | [diff] [blame] | 753 | final Map<ProviderId, DeviceDescriptions> concurrentlyAdded; |
| 754 | concurrentlyAdded = deviceDescs.putIfAbsent(deviceId, r); |
| 755 | if (concurrentlyAdded != null) { |
| 756 | r = concurrentlyAdded; |
| 757 | } |
| 758 | } |
| 759 | return r; |
| 760 | } |
| 761 | |
| 762 | // Guarded by deviceDescs value (=Device lock) |
| 763 | private DeviceDescriptions getOrCreateProviderDeviceDescriptions( |
| 764 | Map<ProviderId, DeviceDescriptions> device, |
| 765 | ProviderId providerId, Timestamped<DeviceDescription> deltaDesc) { |
Yuta HIGUCHI | bf71dff | 2014-10-14 16:02:33 -0700 | [diff] [blame] | 766 | synchronized (device) { |
| 767 | DeviceDescriptions r = device.get(providerId); |
| 768 | if (r == null) { |
| 769 | r = new DeviceDescriptions(deltaDesc); |
| 770 | device.put(providerId, r); |
| 771 | } |
| 772 | return r; |
| 773 | } |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | @Override |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 777 | public synchronized DeviceEvent updatePortStatus(ProviderId providerId, |
| 778 | DeviceId deviceId, |
| 779 | PortDescription portDescription) { |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame] | 780 | final Timestamp newTimestamp; |
| 781 | try { |
| 782 | newTimestamp = deviceClockService.getTimestamp(deviceId); |
| 783 | } catch (IllegalStateException e) { |
| 784 | log.info("Timestamp was not available for device {}", deviceId); |
| 785 | log.debug(" discarding {}", portDescription); |
| 786 | // Failed to generate timestamp. Ignoring. |
| 787 | // See updatePorts comment |
| 788 | return null; |
| 789 | } |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 790 | final Timestamped<PortDescription> deltaDesc |
Thomas Vachuska | fdbc4c2 | 2015-05-29 15:53:01 -0700 | [diff] [blame] | 791 | = new Timestamped<>(portDescription, newTimestamp); |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 792 | final DeviceEvent event; |
| 793 | final Timestamped<PortDescription> mergedDesc; |
Yuta HIGUCHI | 04c6b3f | 2014-11-07 14:47:01 -0800 | [diff] [blame] | 794 | final Map<ProviderId, DeviceDescriptions> device = getOrCreateDeviceDescriptionsMap(deviceId); |
| 795 | synchronized (device) { |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 796 | event = updatePortStatusInternal(providerId, deviceId, deltaDesc); |
Yuta HIGUCHI | 04c6b3f | 2014-11-07 14:47:01 -0800 | [diff] [blame] | 797 | mergedDesc = device.get(providerId) |
Thomas Vachuska | fdbc4c2 | 2015-05-29 15:53:01 -0700 | [diff] [blame] | 798 | .getPortDesc(portDescription.portNumber()); |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 799 | } |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 800 | if (event != null) { |
Madan Jampani | f2af771 | 2015-05-29 18:43:52 -0700 | [diff] [blame] | 801 | log.debug("Notifying peers of a port status update topology event for providerId: {} and deviceId: {}", |
Thomas Vachuska | fdbc4c2 | 2015-05-29 15:53:01 -0700 | [diff] [blame] | 802 | providerId, deviceId); |
Jonathan Hart | 7d656f4 | 2015-01-27 14:07:23 -0800 | [diff] [blame] | 803 | notifyPeers(new InternalPortStatusEvent(providerId, deviceId, mergedDesc)); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 804 | } |
| 805 | return event; |
| 806 | } |
| 807 | |
| 808 | private DeviceEvent updatePortStatusInternal(ProviderId providerId, DeviceId deviceId, |
Thomas Vachuska | fdbc4c2 | 2015-05-29 15:53:01 -0700 | [diff] [blame] | 809 | Timestamped<PortDescription> deltaDesc) { |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 810 | Device device = devices.get(deviceId); |
| 811 | checkArgument(device != null, DEVICE_NOT_FOUND, deviceId); |
| 812 | |
Yuta HIGUCHI | bf71dff | 2014-10-14 16:02:33 -0700 | [diff] [blame] | 813 | Map<ProviderId, DeviceDescriptions> descsMap = deviceDescs.get(deviceId); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 814 | checkArgument(descsMap != null, DEVICE_NOT_FOUND, deviceId); |
| 815 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 816 | synchronized (descsMap) { |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 817 | |
| 818 | if (isDeviceRemoved(deviceId, deltaDesc.timestamp())) { |
| 819 | log.debug("Ignoring outdated event: {}", deltaDesc); |
| 820 | return null; |
| 821 | } |
| 822 | |
| 823 | DeviceDescriptions descs = descsMap.get(providerId); |
| 824 | // assuming all providers must to give DeviceDescription |
Yuta HIGUCHI | bf71dff | 2014-10-14 16:02:33 -0700 | [diff] [blame] | 825 | verify(descs != null, |
Thomas Vachuska | fdbc4c2 | 2015-05-29 15:53:01 -0700 | [diff] [blame] | 826 | "Device description for Device ID %s from Provider %s was not found", |
| 827 | deviceId, providerId); |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 828 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 829 | ConcurrentMap<PortNumber, Port> ports = getPortMap(deviceId); |
| 830 | final PortNumber number = deltaDesc.value().portNumber(); |
| 831 | final Port oldPort = ports.get(number); |
| 832 | final Port newPort; |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 833 | final Timestamped<PortDescription> existingPortDesc = descs.getPortDesc(number); |
Michal Mach | ce77433 | 2017-01-25 11:02:55 +0100 | [diff] [blame] | 834 | boolean toDelete = false; |
| 835 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 836 | if (existingPortDesc == null || |
Thomas Vachuska | fdbc4c2 | 2015-05-29 15:53:01 -0700 | [diff] [blame] | 837 | deltaDesc.isNewer(existingPortDesc)) { |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 838 | // on new port or valid update |
| 839 | // update description |
| 840 | descs.putPortDesc(deltaDesc); |
| 841 | newPort = composePort(device, number, descsMap); |
Michal Mach | ce77433 | 2017-01-25 11:02:55 +0100 | [diff] [blame] | 842 | toDelete = deltaDesc.value().isRemoved(); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 843 | } else { |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 844 | // same or outdated event, ignored. |
| 845 | log.trace("ignore same or outdated {} >= {}", existingPortDesc, deltaDesc); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 846 | return null; |
| 847 | } |
| 848 | |
| 849 | if (oldPort == null) { |
| 850 | return createPort(device, newPort, ports); |
| 851 | } else { |
Michal Mach | ce77433 | 2017-01-25 11:02:55 +0100 | [diff] [blame] | 852 | return toDelete ? removePort(deviceId, number) : updatePort(device, oldPort, newPort, ports); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 853 | } |
| 854 | } |
| 855 | } |
| 856 | |
| 857 | @Override |
| 858 | public List<Port> getPorts(DeviceId deviceId) { |
| 859 | Map<PortNumber, Port> ports = devicePorts.get(deviceId); |
| 860 | if (ports == null) { |
| 861 | return Collections.emptyList(); |
| 862 | } |
| 863 | return ImmutableList.copyOf(ports.values()); |
| 864 | } |
| 865 | |
| 866 | @Override |
Yuta HIGUCHI | 6eb00cc | 2016-06-10 11:55:12 -0700 | [diff] [blame] | 867 | public Stream<PortDescription> getPortDescriptions(ProviderId pid, |
| 868 | DeviceId deviceId) { |
| 869 | Map<ProviderId, DeviceDescriptions> descs = this.deviceDescs.get(deviceId); |
| 870 | if (descs == null) { |
| 871 | return null; |
| 872 | } |
| 873 | // inner-Map(=descs) is HashMap, thus requires synchronization even for reads |
| 874 | final Optional<DeviceDescriptions> devDescs; |
| 875 | synchronized (descs) { |
| 876 | devDescs = Optional.ofNullable(descs.get(pid)); |
| 877 | } |
| 878 | // DeviceDescriptions is concurrent access-safe |
| 879 | return devDescs |
| 880 | .map(dd -> dd.getPortDescs().values().stream() |
| 881 | .map(Timestamped::value)) |
| 882 | .orElse(Stream.empty()); |
| 883 | } |
| 884 | |
| 885 | @Override |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 886 | public DeviceEvent updatePortStatistics(ProviderId providerId, DeviceId deviceId, |
Dusan Pajin | 11ff4a8 | 2015-08-20 18:03:05 +0200 | [diff] [blame] | 887 | Collection<PortStatistics> newStatsCollection) { |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 888 | |
Dusan Pajin | 11ff4a8 | 2015-08-20 18:03:05 +0200 | [diff] [blame] | 889 | Map<PortNumber, PortStatistics> prvStatsMap = devicePortStats.get(deviceId); |
| 890 | Map<PortNumber, PortStatistics> newStatsMap = Maps.newHashMap(); |
| 891 | Map<PortNumber, PortStatistics> deltaStatsMap = Maps.newHashMap(); |
| 892 | |
| 893 | if (prvStatsMap != null) { |
| 894 | for (PortStatistics newStats : newStatsCollection) { |
| 895 | PortNumber port = PortNumber.portNumber(newStats.port()); |
| 896 | PortStatistics prvStats = prvStatsMap.get(port); |
| 897 | DefaultPortStatistics.Builder builder = DefaultPortStatistics.builder(); |
| 898 | PortStatistics deltaStats = builder.build(); |
| 899 | if (prvStats != null) { |
| 900 | deltaStats = calcDeltaStats(deviceId, prvStats, newStats); |
| 901 | } |
| 902 | deltaStatsMap.put(port, deltaStats); |
| 903 | newStatsMap.put(port, newStats); |
| 904 | } |
| 905 | } else { |
| 906 | for (PortStatistics newStats : newStatsCollection) { |
| 907 | PortNumber port = PortNumber.portNumber(newStats.port()); |
| 908 | newStatsMap.put(port, newStats); |
| 909 | } |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 910 | } |
Dusan Pajin | 11ff4a8 | 2015-08-20 18:03:05 +0200 | [diff] [blame] | 911 | devicePortDeltaStats.put(deviceId, deltaStatsMap); |
| 912 | devicePortStats.put(deviceId, newStatsMap); |
Dusan Pajin | 517e223 | 2015-08-24 16:50:11 +0200 | [diff] [blame] | 913 | // DeviceEvent returns null because of InternalPortStatsListener usage |
| 914 | return null; |
Dusan Pajin | 11ff4a8 | 2015-08-20 18:03:05 +0200 | [diff] [blame] | 915 | } |
| 916 | |
| 917 | /** |
| 918 | * Calculate delta statistics by subtracting previous from new statistics. |
| 919 | * |
Madan Jampani | f97edc1 | 2015-08-31 14:41:01 -0700 | [diff] [blame] | 920 | * @param deviceId device identifier |
| 921 | * @param prvStats previous port statistics |
| 922 | * @param newStats new port statistics |
Dusan Pajin | 11ff4a8 | 2015-08-20 18:03:05 +0200 | [diff] [blame] | 923 | * @return PortStatistics |
| 924 | */ |
| 925 | public PortStatistics calcDeltaStats(DeviceId deviceId, PortStatistics prvStats, PortStatistics newStats) { |
| 926 | // calculate time difference |
| 927 | long deltaStatsSec, deltaStatsNano; |
| 928 | if (newStats.durationNano() < prvStats.durationNano()) { |
| 929 | deltaStatsNano = newStats.durationNano() - prvStats.durationNano() + TimeUnit.SECONDS.toNanos(1); |
| 930 | deltaStatsSec = newStats.durationSec() - prvStats.durationSec() - 1L; |
| 931 | } else { |
| 932 | deltaStatsNano = newStats.durationNano() - prvStats.durationNano(); |
| 933 | deltaStatsSec = newStats.durationSec() - prvStats.durationSec(); |
| 934 | } |
| 935 | DefaultPortStatistics.Builder builder = DefaultPortStatistics.builder(); |
| 936 | DefaultPortStatistics deltaStats = builder.setDeviceId(deviceId) |
| 937 | .setPort(newStats.port()) |
| 938 | .setPacketsReceived(newStats.packetsReceived() - prvStats.packetsReceived()) |
| 939 | .setPacketsSent(newStats.packetsSent() - prvStats.packetsSent()) |
| 940 | .setBytesReceived(newStats.bytesReceived() - prvStats.bytesReceived()) |
| 941 | .setBytesSent(newStats.bytesSent() - prvStats.bytesSent()) |
| 942 | .setPacketsRxDropped(newStats.packetsRxDropped() - prvStats.packetsRxDropped()) |
| 943 | .setPacketsTxDropped(newStats.packetsTxDropped() - prvStats.packetsTxDropped()) |
| 944 | .setPacketsRxErrors(newStats.packetsRxErrors() - prvStats.packetsRxErrors()) |
| 945 | .setPacketsTxErrors(newStats.packetsTxErrors() - prvStats.packetsTxErrors()) |
| 946 | .setDurationSec(deltaStatsSec) |
| 947 | .setDurationNano(deltaStatsNano) |
| 948 | .build(); |
| 949 | return deltaStats; |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 950 | } |
| 951 | |
| 952 | @Override |
| 953 | public List<PortStatistics> getPortStatistics(DeviceId deviceId) { |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 954 | Map<PortNumber, PortStatistics> portStats = devicePortStats.get(deviceId); |
| 955 | if (portStats == null) { |
| 956 | return Collections.emptyList(); |
| 957 | } |
| 958 | return ImmutableList.copyOf(portStats.values()); |
| 959 | } |
| 960 | |
| 961 | @Override |
Viswanath KSP | 22774cd | 2016-08-20 20:06:30 +0530 | [diff] [blame] | 962 | public PortStatistics getStatisticsForPort(DeviceId deviceId, PortNumber portNumber) { |
| 963 | Map<PortNumber, PortStatistics> portStatsMap = devicePortStats.get(deviceId); |
| 964 | if (portStatsMap == null) { |
| 965 | return null; |
| 966 | } |
| 967 | PortStatistics portStats = portStatsMap.get(portNumber); |
| 968 | return portStats; |
| 969 | } |
| 970 | |
| 971 | @Override |
Dusan Pajin | 11ff4a8 | 2015-08-20 18:03:05 +0200 | [diff] [blame] | 972 | public List<PortStatistics> getPortDeltaStatistics(DeviceId deviceId) { |
| 973 | Map<PortNumber, PortStatistics> portStats = devicePortDeltaStats.get(deviceId); |
| 974 | if (portStats == null) { |
| 975 | return Collections.emptyList(); |
| 976 | } |
| 977 | return ImmutableList.copyOf(portStats.values()); |
| 978 | } |
| 979 | |
| 980 | @Override |
Viswanath KSP | 22774cd | 2016-08-20 20:06:30 +0530 | [diff] [blame] | 981 | public PortStatistics getDeltaStatisticsForPort(DeviceId deviceId, PortNumber portNumber) { |
| 982 | Map<PortNumber, PortStatistics> portStatsMap = devicePortDeltaStats.get(deviceId); |
| 983 | if (portStatsMap == null) { |
| 984 | return null; |
| 985 | } |
| 986 | PortStatistics portStats = portStatsMap.get(portNumber); |
| 987 | return portStats; |
| 988 | } |
| 989 | |
| 990 | @Override |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 991 | public Port getPort(DeviceId deviceId, PortNumber portNumber) { |
| 992 | Map<PortNumber, Port> ports = devicePorts.get(deviceId); |
| 993 | return ports == null ? null : ports.get(portNumber); |
| 994 | } |
| 995 | |
| 996 | @Override |
Yuta HIGUCHI | 6eb00cc | 2016-06-10 11:55:12 -0700 | [diff] [blame] | 997 | public PortDescription getPortDescription(ProviderId pid, |
| 998 | DeviceId deviceId, |
| 999 | PortNumber portNumber) { |
| 1000 | Map<ProviderId, DeviceDescriptions> descs = this.deviceDescs.get(deviceId); |
| 1001 | if (descs == null) { |
| 1002 | return null; |
| 1003 | } |
| 1004 | // inner-Map(=descs) is HashMap, thus requires synchronization even for reads |
| 1005 | final Optional<DeviceDescriptions> devDescs; |
| 1006 | synchronized (descs) { |
| 1007 | devDescs = Optional.ofNullable(descs.get(pid)); |
| 1008 | } |
| 1009 | // DeviceDescriptions is concurrent access-safe |
| 1010 | return devDescs |
| 1011 | .map(deviceDescriptions -> deviceDescriptions.getPortDesc(portNumber)) |
| 1012 | .map(Timestamped::value) |
| 1013 | .orElse(null); |
| 1014 | } |
| 1015 | |
| 1016 | @Override |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 1017 | public boolean isAvailable(DeviceId deviceId) { |
| 1018 | return availableDevices.contains(deviceId); |
| 1019 | } |
| 1020 | |
| 1021 | @Override |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 1022 | public synchronized DeviceEvent removeDevice(DeviceId deviceId) { |
Yuta HIGUCHI | 53afd5b | 2014-11-03 18:03:08 -0800 | [diff] [blame] | 1023 | final NodeId myId = clusterService.getLocalNode().id(); |
| 1024 | NodeId master = mastershipService.getMasterFor(deviceId); |
| 1025 | |
| 1026 | // if there exist a master, forward |
| 1027 | // if there is no master, try to become one and process |
| 1028 | |
| 1029 | boolean relinquishAtEnd = false; |
| 1030 | if (master == null) { |
| 1031 | final MastershipRole myRole = mastershipService.getLocalRole(deviceId); |
| 1032 | if (myRole != MastershipRole.NONE) { |
| 1033 | relinquishAtEnd = true; |
| 1034 | } |
Yuta HIGUCHI | 1a01272 | 2014-11-20 15:21:41 -0800 | [diff] [blame] | 1035 | log.debug("Temporarily requesting role for {} to remove", deviceId); |
Yuta HIGUCHI | 53afd5b | 2014-11-03 18:03:08 -0800 | [diff] [blame] | 1036 | mastershipService.requestRoleFor(deviceId); |
Yuta HIGUCHI | 53afd5b | 2014-11-03 18:03:08 -0800 | [diff] [blame] | 1037 | MastershipTerm term = termService.getMastershipTerm(deviceId); |
Madan Jampani | 7cdf3f1 | 2015-05-12 23:18:05 -0700 | [diff] [blame] | 1038 | if (term != null && myId.equals(term.master())) { |
Yuta HIGUCHI | 53afd5b | 2014-11-03 18:03:08 -0800 | [diff] [blame] | 1039 | master = myId; |
| 1040 | } |
Yuta HIGUCHI | e8252bb | 2014-10-22 09:41:01 -0700 | [diff] [blame] | 1041 | } |
| 1042 | |
Jordan Halterman | ebedbb5 | 2017-08-08 15:57:50 -0700 | [diff] [blame] | 1043 | boolean isMaster = myId.equals(master); |
| 1044 | |
| 1045 | // If this node is not the master, forward the request. |
| 1046 | if (!isMaster) { |
Yuta HIGUCHI | 1a01272 | 2014-11-20 15:21:41 -0800 | [diff] [blame] | 1047 | log.debug("{} has control of {}, forwarding remove request", |
Thomas Vachuska | fdbc4c2 | 2015-05-29 15:53:01 -0700 | [diff] [blame] | 1048 | master, deviceId); |
Yuta HIGUCHI | 53afd5b | 2014-11-03 18:03:08 -0800 | [diff] [blame] | 1049 | |
Brian O'Connor | 5eb77c8 | 2015-03-02 18:09:39 -0800 | [diff] [blame] | 1050 | // TODO check unicast return value |
Madan Jampani | 2bfa94c | 2015-04-11 05:03:49 -0700 | [diff] [blame] | 1051 | clusterCommunicator.unicast(deviceId, DEVICE_REMOVE_REQ, SERIALIZER::encode, master); |
Brian O'Connor | 5eb77c8 | 2015-03-02 18:09:39 -0800 | [diff] [blame] | 1052 | /* error log: |
| 1053 | log.error("Failed to forward {} remove request to {}", deviceId, master, e); |
| 1054 | */ |
Yuta HIGUCHI | 53afd5b | 2014-11-03 18:03:08 -0800 | [diff] [blame] | 1055 | } |
| 1056 | |
Jordan Halterman | ebedbb5 | 2017-08-08 15:57:50 -0700 | [diff] [blame] | 1057 | // If this node is the master, get a timestamp. Otherwise, default to the current device timestamp. |
| 1058 | Timestamp timestamp = isMaster ? deviceClockService.getTimestamp(deviceId) : null; |
Yuta HIGUCHI | 53afd5b | 2014-11-03 18:03:08 -0800 | [diff] [blame] | 1059 | |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 1060 | DeviceEvent event = removeDeviceInternal(deviceId, timestamp); |
Jordan Halterman | ebedbb5 | 2017-08-08 15:57:50 -0700 | [diff] [blame] | 1061 | |
| 1062 | // If this node is the master, update peers. |
| 1063 | if (isMaster && event != null) { |
Yuta HIGUCHI | 1a01272 | 2014-11-20 15:21:41 -0800 | [diff] [blame] | 1064 | log.debug("Notifying peers of a device removed topology event for deviceId: {}", |
Thomas Vachuska | fdbc4c2 | 2015-05-29 15:53:01 -0700 | [diff] [blame] | 1065 | deviceId); |
Jonathan Hart | 7d656f4 | 2015-01-27 14:07:23 -0800 | [diff] [blame] | 1066 | notifyPeers(new InternalDeviceRemovedEvent(deviceId, timestamp)); |
Madan Jampani | 3fc72ed | 2014-10-08 12:50:27 -0700 | [diff] [blame] | 1067 | } |
Jordan Halterman | ebedbb5 | 2017-08-08 15:57:50 -0700 | [diff] [blame] | 1068 | |
| 1069 | // Relinquish mastership if acquired to remove the device. |
Yuta HIGUCHI | 53afd5b | 2014-11-03 18:03:08 -0800 | [diff] [blame] | 1070 | if (relinquishAtEnd) { |
Yuta HIGUCHI | 1a01272 | 2014-11-20 15:21:41 -0800 | [diff] [blame] | 1071 | log.debug("Relinquishing temporary role acquired for {}", deviceId); |
Yuta HIGUCHI | 53afd5b | 2014-11-03 18:03:08 -0800 | [diff] [blame] | 1072 | mastershipService.relinquishMastership(deviceId); |
| 1073 | } |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 1074 | return event; |
| 1075 | } |
| 1076 | |
Jordan Halterman | ebedbb5 | 2017-08-08 15:57:50 -0700 | [diff] [blame] | 1077 | private DeviceEvent removeDeviceInternal(DeviceId deviceId, Timestamp timestamp) { |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 1078 | |
Yuta HIGUCHI | bf71dff | 2014-10-14 16:02:33 -0700 | [diff] [blame] | 1079 | Map<ProviderId, DeviceDescriptions> descs = getOrCreateDeviceDescriptionsMap(deviceId); |
Yuta HIGUCHI | 0d6a5e6 | 2014-10-03 15:54:09 -0700 | [diff] [blame] | 1080 | synchronized (descs) { |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 1081 | // accept removal request if given timestamp is newer than |
| 1082 | // the latest Timestamp from Primary provider |
| 1083 | DeviceDescriptions primDescs = getPrimaryDescriptions(descs); |
Thomas Vachuska | 710293f | 2015-11-13 12:29:31 -0800 | [diff] [blame] | 1084 | if (primDescs == null) { |
| 1085 | return null; |
| 1086 | } |
| 1087 | |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 1088 | Timestamp lastTimestamp = primDescs.getLatestTimestamp(); |
Jordan Halterman | ebedbb5 | 2017-08-08 15:57:50 -0700 | [diff] [blame] | 1089 | |
| 1090 | // If no timestamp is set, default the timestamp to the last timestamp for the device. |
| 1091 | if (timestamp == null) { |
| 1092 | timestamp = lastTimestamp; |
| 1093 | } |
| 1094 | |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 1095 | if (timestamp.compareTo(lastTimestamp) <= 0) { |
| 1096 | // outdated event ignore |
| 1097 | return null; |
| 1098 | } |
| 1099 | removalRequest.put(deviceId, timestamp); |
| 1100 | |
Yuta HIGUCHI | 0d6a5e6 | 2014-10-03 15:54:09 -0700 | [diff] [blame] | 1101 | Device device = devices.remove(deviceId); |
| 1102 | // should DEVICE_REMOVED carry removed ports? |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 1103 | Map<PortNumber, Port> ports = devicePorts.get(deviceId); |
| 1104 | if (ports != null) { |
| 1105 | ports.clear(); |
| 1106 | } |
| 1107 | markOfflineInternal(deviceId, timestamp); |
Yuta HIGUCHI | 0d6a5e6 | 2014-10-03 15:54:09 -0700 | [diff] [blame] | 1108 | descs.clear(); |
| 1109 | return device == null ? null : |
Dusan Pajin | 11ff4a8 | 2015-08-20 18:03:05 +0200 | [diff] [blame] | 1110 | new DeviceEvent(DeviceEvent.Type.DEVICE_REMOVED, device, null); |
Yuta HIGUCHI | 0d6a5e6 | 2014-10-03 15:54:09 -0700 | [diff] [blame] | 1111 | } |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 1112 | } |
| 1113 | |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 1114 | /** |
| 1115 | * Checks if given timestamp is superseded by removal request |
| 1116 | * with more recent timestamp. |
| 1117 | * |
Thomas Vachuska | fdbc4c2 | 2015-05-29 15:53:01 -0700 | [diff] [blame] | 1118 | * @param deviceId identifier of a device |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 1119 | * @param timestampToCheck timestamp of an event to check |
| 1120 | * @return true if device is already removed |
| 1121 | */ |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 1122 | private boolean isDeviceRemoved(DeviceId deviceId, Timestamp timestampToCheck) { |
| 1123 | Timestamp removalTimestamp = removalRequest.get(deviceId); |
| 1124 | if (removalTimestamp != null && |
Jordan Halterman | 8a0b397 | 2017-08-15 16:14:18 -0700 | [diff] [blame] | 1125 | removalTimestamp.compareTo(timestampToCheck) > 0) { |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 1126 | // removalRequest is more recent |
| 1127 | return true; |
| 1128 | } |
| 1129 | return false; |
| 1130 | } |
| 1131 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 1132 | /** |
| 1133 | * Returns a Device, merging description given from multiple Providers. |
| 1134 | * |
Thomas Vachuska | fdbc4c2 | 2015-05-29 15:53:01 -0700 | [diff] [blame] | 1135 | * @param deviceId device identifier |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 1136 | * @param providerDescs Collection of Descriptions from multiple providers |
| 1137 | * @return Device instance |
| 1138 | */ |
| 1139 | private Device composeDevice(DeviceId deviceId, |
Thomas Vachuska | fdbc4c2 | 2015-05-29 15:53:01 -0700 | [diff] [blame] | 1140 | Map<ProviderId, DeviceDescriptions> providerDescs) { |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 1141 | |
Thomas Vachuska | 444eda6 | 2014-10-28 13:09:42 -0700 | [diff] [blame] | 1142 | checkArgument(!providerDescs.isEmpty(), "No device descriptions supplied"); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 1143 | |
Jonathan Hart | d9df7bd | 2015-11-10 17:10:25 -0800 | [diff] [blame] | 1144 | ProviderId primary = pickPrimaryPid(providerDescs); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 1145 | |
| 1146 | DeviceDescriptions desc = providerDescs.get(primary); |
| 1147 | |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 1148 | final DeviceDescription base = desc.getDeviceDesc().value(); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 1149 | Type type = base.type(); |
| 1150 | String manufacturer = base.manufacturer(); |
| 1151 | String hwVersion = base.hwVersion(); |
| 1152 | String swVersion = base.swVersion(); |
| 1153 | String serialNumber = base.serialNumber(); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 1154 | ChassisId chassisId = base.chassisId(); |
Yuta HIGUCHI | 9eed0b1 | 2017-06-07 11:59:18 -0700 | [diff] [blame] | 1155 | DefaultAnnotations.Builder annotations = DefaultAnnotations.builder(); |
| 1156 | annotations.putAll(base.annotations()); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 1157 | |
| 1158 | for (Entry<ProviderId, DeviceDescriptions> e : providerDescs.entrySet()) { |
| 1159 | if (e.getKey().equals(primary)) { |
| 1160 | continue; |
| 1161 | } |
Yuta HIGUCHI | 6593489 | 2014-12-04 17:47:44 -0800 | [diff] [blame] | 1162 | // Note: should keep track of Description timestamp in the future |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 1163 | // and only merge conflicting keys when timestamp is newer. |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 1164 | // Currently assuming there will never be a key conflict between |
| 1165 | // providers |
| 1166 | |
| 1167 | // annotation merging. not so efficient, should revisit later |
Yuta HIGUCHI | 9eed0b1 | 2017-06-07 11:59:18 -0700 | [diff] [blame] | 1168 | annotations.putAll(e.getValue().getDeviceDesc().value().annotations()); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 1169 | } |
| 1170 | |
Thomas Vachuska | fdbc4c2 | 2015-05-29 15:53:01 -0700 | [diff] [blame] | 1171 | return new DefaultDevice(primary, deviceId, type, manufacturer, |
| 1172 | hwVersion, swVersion, serialNumber, |
Yuta HIGUCHI | 9eed0b1 | 2017-06-07 11:59:18 -0700 | [diff] [blame] | 1173 | chassisId, annotations.build()); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 1174 | } |
| 1175 | |
Marc De Leenheer | 88194c3 | 2015-05-29 22:10:59 -0700 | [diff] [blame] | 1176 | private Port buildTypedPort(Device device, PortNumber number, boolean isEnabled, |
| 1177 | PortDescription description, Annotations annotations) { |
Marc De Leenheer | 88194c3 | 2015-05-29 22:10:59 -0700 | [diff] [blame] | 1178 | return new DefaultPort(device, number, isEnabled, description.type(), |
| 1179 | description.portSpeed(), annotations); |
Marc De Leenheer | 88194c3 | 2015-05-29 22:10:59 -0700 | [diff] [blame] | 1180 | } |
| 1181 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 1182 | /** |
| 1183 | * Returns a Port, merging description given from multiple Providers. |
| 1184 | * |
Thomas Vachuska | fdbc4c2 | 2015-05-29 15:53:01 -0700 | [diff] [blame] | 1185 | * @param device device the port is on |
| 1186 | * @param number port number |
Yuta HIGUCHI | bf71dff | 2014-10-14 16:02:33 -0700 | [diff] [blame] | 1187 | * @param descsMap Collection of Descriptions from multiple providers |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 1188 | * @return Port instance |
| 1189 | */ |
| 1190 | private Port composePort(Device device, PortNumber number, |
Thomas Vachuska | fdbc4c2 | 2015-05-29 15:53:01 -0700 | [diff] [blame] | 1191 | Map<ProviderId, DeviceDescriptions> descsMap) { |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 1192 | |
Jonathan Hart | d9df7bd | 2015-11-10 17:10:25 -0800 | [diff] [blame] | 1193 | ProviderId primary = pickPrimaryPid(descsMap); |
Yuta HIGUCHI | bf71dff | 2014-10-14 16:02:33 -0700 | [diff] [blame] | 1194 | DeviceDescriptions primDescs = descsMap.get(primary); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 1195 | // if no primary, assume not enabled |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 1196 | boolean isEnabled = false; |
Yuta HIGUCHI | 9eed0b1 | 2017-06-07 11:59:18 -0700 | [diff] [blame] | 1197 | DefaultAnnotations.Builder annotations = DefaultAnnotations.builder(); |
Ayaka Koshibe | ae54173 | 2015-05-19 13:37:27 -0700 | [diff] [blame] | 1198 | Timestamp newest = null; |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 1199 | final Timestamped<PortDescription> portDesc = primDescs.getPortDesc(number); |
| 1200 | if (portDesc != null) { |
| 1201 | isEnabled = portDesc.value().isEnabled(); |
Yuta HIGUCHI | 9eed0b1 | 2017-06-07 11:59:18 -0700 | [diff] [blame] | 1202 | annotations.putAll(portDesc.value().annotations()); |
Ayaka Koshibe | ae54173 | 2015-05-19 13:37:27 -0700 | [diff] [blame] | 1203 | newest = portDesc.timestamp(); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 1204 | } |
Ayaka Koshibe | ae54173 | 2015-05-19 13:37:27 -0700 | [diff] [blame] | 1205 | Port updated = null; |
Yuta HIGUCHI | bf71dff | 2014-10-14 16:02:33 -0700 | [diff] [blame] | 1206 | for (Entry<ProviderId, DeviceDescriptions> e : descsMap.entrySet()) { |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 1207 | if (e.getKey().equals(primary)) { |
| 1208 | continue; |
| 1209 | } |
Yuta HIGUCHI | 6593489 | 2014-12-04 17:47:44 -0800 | [diff] [blame] | 1210 | // Note: should keep track of Description timestamp in the future |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 1211 | // and only merge conflicting keys when timestamp is newer. |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 1212 | // Currently assuming there will never be a key conflict between |
| 1213 | // providers |
| 1214 | |
| 1215 | // annotation merging. not so efficient, should revisit later |
| 1216 | final Timestamped<PortDescription> otherPortDesc = e.getValue().getPortDesc(number); |
| 1217 | if (otherPortDesc != null) { |
Ayaka Koshibe | ae54173 | 2015-05-19 13:37:27 -0700 | [diff] [blame] | 1218 | if (newest != null && newest.isNewerThan(otherPortDesc.timestamp())) { |
| 1219 | continue; |
| 1220 | } |
Yuta HIGUCHI | 9eed0b1 | 2017-06-07 11:59:18 -0700 | [diff] [blame] | 1221 | annotations.putAll(otherPortDesc.value().annotations()); |
Ayaka Koshibe | 74b5527 | 2015-05-28 15:16:04 -0700 | [diff] [blame] | 1222 | PortDescription other = otherPortDesc.value(); |
Yuta HIGUCHI | 9eed0b1 | 2017-06-07 11:59:18 -0700 | [diff] [blame] | 1223 | updated = buildTypedPort(device, number, isEnabled, other, annotations.build()); |
Ayaka Koshibe | ae54173 | 2015-05-19 13:37:27 -0700 | [diff] [blame] | 1224 | newest = otherPortDesc.timestamp(); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 1225 | } |
| 1226 | } |
Marc De Leenheer | 4b18a23 | 2015-04-30 11:58:20 -0700 | [diff] [blame] | 1227 | if (portDesc == null) { |
Yuta HIGUCHI | 9eed0b1 | 2017-06-07 11:59:18 -0700 | [diff] [blame] | 1228 | return updated == null ? new DefaultPort(device, number, false, annotations.build()) : updated; |
Marc De Leenheer | 4b18a23 | 2015-04-30 11:58:20 -0700 | [diff] [blame] | 1229 | } |
Ayaka Koshibe | 74b5527 | 2015-05-28 15:16:04 -0700 | [diff] [blame] | 1230 | PortDescription current = portDesc.value(); |
| 1231 | return updated == null |
Yuta HIGUCHI | 9eed0b1 | 2017-06-07 11:59:18 -0700 | [diff] [blame] | 1232 | ? buildTypedPort(device, number, isEnabled, current, annotations.build()) |
Ayaka Koshibe | 74b5527 | 2015-05-28 15:16:04 -0700 | [diff] [blame] | 1233 | : updated; |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 1234 | } |
| 1235 | |
| 1236 | /** |
| 1237 | * @return primary ProviderID, or randomly chosen one if none exists |
| 1238 | */ |
Jonathan Hart | d9df7bd | 2015-11-10 17:10:25 -0800 | [diff] [blame] | 1239 | private ProviderId pickPrimaryPid( |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 1240 | Map<ProviderId, DeviceDescriptions> providerDescs) { |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 1241 | ProviderId fallBackPrimary = null; |
| 1242 | for (Entry<ProviderId, DeviceDescriptions> e : providerDescs.entrySet()) { |
| 1243 | if (!e.getKey().isAncillary()) { |
| 1244 | return e.getKey(); |
| 1245 | } else if (fallBackPrimary == null) { |
| 1246 | // pick randomly as a fallback in case there is no primary |
| 1247 | fallBackPrimary = e.getKey(); |
| 1248 | } |
| 1249 | } |
| 1250 | return fallBackPrimary; |
| 1251 | } |
| 1252 | |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 1253 | private DeviceDescriptions getPrimaryDescriptions( |
Thomas Vachuska | fdbc4c2 | 2015-05-29 15:53:01 -0700 | [diff] [blame] | 1254 | Map<ProviderId, DeviceDescriptions> providerDescs) { |
Jonathan Hart | d9df7bd | 2015-11-10 17:10:25 -0800 | [diff] [blame] | 1255 | ProviderId pid = pickPrimaryPid(providerDescs); |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 1256 | return providerDescs.get(pid); |
| 1257 | } |
| 1258 | |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 1259 | private void unicastMessage(NodeId recipient, MessageSubject subject, Object event) throws IOException { |
Madan Jampani | 2bfa94c | 2015-04-11 05:03:49 -0700 | [diff] [blame] | 1260 | clusterCommunicator.unicast(event, subject, SERIALIZER::encode, recipient); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 1261 | } |
| 1262 | |
Jonathan Hart | 7d656f4 | 2015-01-27 14:07:23 -0800 | [diff] [blame] | 1263 | private void broadcastMessage(MessageSubject subject, Object event) { |
Madan Jampani | 2bfa94c | 2015-04-11 05:03:49 -0700 | [diff] [blame] | 1264 | clusterCommunicator.broadcast(event, subject, SERIALIZER::encode); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 1265 | } |
Madan Jampani | 47c9373 | 2014-10-06 20:46:08 -0700 | [diff] [blame] | 1266 | |
Jonathan Hart | 7d656f4 | 2015-01-27 14:07:23 -0800 | [diff] [blame] | 1267 | private void notifyPeers(InternalDeviceEvent event) { |
Jonathan Hart | 46ab5cc | 2016-09-15 15:42:39 -0700 | [diff] [blame] | 1268 | broadcastMessage(DEVICE_UPDATE, event); |
Madan Jampani | 47c9373 | 2014-10-06 20:46:08 -0700 | [diff] [blame] | 1269 | } |
| 1270 | |
Jonathan Hart | 7d656f4 | 2015-01-27 14:07:23 -0800 | [diff] [blame] | 1271 | private void notifyPeers(InternalDeviceOfflineEvent event) { |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 1272 | broadcastMessage(GossipDeviceStoreMessageSubjects.DEVICE_OFFLINE, event); |
Madan Jampani | 2532253 | 2014-10-08 11:20:38 -0700 | [diff] [blame] | 1273 | } |
| 1274 | |
Jonathan Hart | 7d656f4 | 2015-01-27 14:07:23 -0800 | [diff] [blame] | 1275 | private void notifyPeers(InternalDeviceRemovedEvent event) { |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 1276 | broadcastMessage(GossipDeviceStoreMessageSubjects.DEVICE_REMOVED, event); |
Madan Jampani | 3fc72ed | 2014-10-08 12:50:27 -0700 | [diff] [blame] | 1277 | } |
| 1278 | |
Jonathan Hart | 7d656f4 | 2015-01-27 14:07:23 -0800 | [diff] [blame] | 1279 | private void notifyPeers(InternalPortEvent event) { |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 1280 | broadcastMessage(GossipDeviceStoreMessageSubjects.PORT_UPDATE, event); |
Madan Jampani | 47c9373 | 2014-10-06 20:46:08 -0700 | [diff] [blame] | 1281 | } |
| 1282 | |
Jonathan Hart | 7d656f4 | 2015-01-27 14:07:23 -0800 | [diff] [blame] | 1283 | private void notifyPeers(InternalPortStatusEvent event) { |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 1284 | broadcastMessage(GossipDeviceStoreMessageSubjects.PORT_STATUS_UPDATE, event); |
| 1285 | } |
| 1286 | |
| 1287 | private void notifyPeer(NodeId recipient, InternalDeviceEvent event) { |
| 1288 | try { |
Jonathan Hart | 46ab5cc | 2016-09-15 15:42:39 -0700 | [diff] [blame] | 1289 | unicastMessage(recipient, DEVICE_UPDATE, event); |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 1290 | } catch (IOException e) { |
| 1291 | log.error("Failed to send" + event + " to " + recipient, e); |
| 1292 | } |
| 1293 | } |
| 1294 | |
| 1295 | private void notifyPeer(NodeId recipient, InternalDeviceOfflineEvent event) { |
| 1296 | try { |
| 1297 | unicastMessage(recipient, GossipDeviceStoreMessageSubjects.DEVICE_OFFLINE, event); |
| 1298 | } catch (IOException e) { |
| 1299 | log.error("Failed to send" + event + " to " + recipient, e); |
| 1300 | } |
| 1301 | } |
| 1302 | |
| 1303 | private void notifyPeer(NodeId recipient, InternalDeviceRemovedEvent event) { |
| 1304 | try { |
| 1305 | unicastMessage(recipient, GossipDeviceStoreMessageSubjects.DEVICE_REMOVED, event); |
| 1306 | } catch (IOException e) { |
| 1307 | log.error("Failed to send" + event + " to " + recipient, e); |
| 1308 | } |
| 1309 | } |
| 1310 | |
| 1311 | private void notifyPeer(NodeId recipient, InternalPortEvent event) { |
| 1312 | try { |
| 1313 | unicastMessage(recipient, GossipDeviceStoreMessageSubjects.PORT_UPDATE, event); |
| 1314 | } catch (IOException e) { |
| 1315 | log.error("Failed to send" + event + " to " + recipient, e); |
| 1316 | } |
| 1317 | } |
| 1318 | |
| 1319 | private void notifyPeer(NodeId recipient, InternalPortStatusEvent event) { |
| 1320 | try { |
| 1321 | unicastMessage(recipient, GossipDeviceStoreMessageSubjects.PORT_STATUS_UPDATE, event); |
| 1322 | } catch (IOException e) { |
| 1323 | log.error("Failed to send" + event + " to " + recipient, e); |
| 1324 | } |
| 1325 | } |
| 1326 | |
| 1327 | private DeviceAntiEntropyAdvertisement createAdvertisement() { |
| 1328 | final NodeId self = clusterService.getLocalNode().id(); |
| 1329 | |
Yuta HIGUCHI | bf71dff | 2014-10-14 16:02:33 -0700 | [diff] [blame] | 1330 | final int numDevices = deviceDescs.size(); |
| 1331 | Map<DeviceFragmentId, Timestamp> adDevices = new HashMap<>(numDevices); |
| 1332 | final int portsPerDevice = 8; // random factor to minimize reallocation |
| 1333 | Map<PortFragmentId, Timestamp> adPorts = new HashMap<>(numDevices * portsPerDevice); |
| 1334 | Map<DeviceId, Timestamp> adOffline = new HashMap<>(numDevices); |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 1335 | |
Yuta HIGUCHI | b6cfac3 | 2014-11-25 13:37:27 -0800 | [diff] [blame] | 1336 | deviceDescs.forEach((deviceId, devDescs) -> { |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 1337 | |
Yuta HIGUCHI | bf71dff | 2014-10-14 16:02:33 -0700 | [diff] [blame] | 1338 | // for each Device... |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 1339 | synchronized (devDescs) { |
| 1340 | |
Yuta HIGUCHI | bf71dff | 2014-10-14 16:02:33 -0700 | [diff] [blame] | 1341 | // send device offline timestamp |
| 1342 | Timestamp lOffline = this.offline.get(deviceId); |
| 1343 | if (lOffline != null) { |
| 1344 | adOffline.put(deviceId, lOffline); |
| 1345 | } |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 1346 | |
| 1347 | for (Entry<ProviderId, DeviceDescriptions> |
| 1348 | prov : devDescs.entrySet()) { |
| 1349 | |
Yuta HIGUCHI | bf71dff | 2014-10-14 16:02:33 -0700 | [diff] [blame] | 1350 | // for each Provider Descriptions... |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 1351 | final ProviderId provId = prov.getKey(); |
| 1352 | final DeviceDescriptions descs = prov.getValue(); |
| 1353 | |
Yuta HIGUCHI | bf71dff | 2014-10-14 16:02:33 -0700 | [diff] [blame] | 1354 | adDevices.put(new DeviceFragmentId(deviceId, provId), |
Thomas Vachuska | fdbc4c2 | 2015-05-29 15:53:01 -0700 | [diff] [blame] | 1355 | descs.getDeviceDesc().timestamp()); |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 1356 | |
| 1357 | for (Entry<PortNumber, Timestamped<PortDescription>> |
| 1358 | portDesc : descs.getPortDescs().entrySet()) { |
| 1359 | |
| 1360 | final PortNumber number = portDesc.getKey(); |
Yuta HIGUCHI | bf71dff | 2014-10-14 16:02:33 -0700 | [diff] [blame] | 1361 | adPorts.put(new PortFragmentId(deviceId, provId, number), |
Thomas Vachuska | fdbc4c2 | 2015-05-29 15:53:01 -0700 | [diff] [blame] | 1362 | portDesc.getValue().timestamp()); |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 1363 | } |
| 1364 | } |
| 1365 | } |
Yuta HIGUCHI | b6cfac3 | 2014-11-25 13:37:27 -0800 | [diff] [blame] | 1366 | }); |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 1367 | |
Yuta HIGUCHI | bf71dff | 2014-10-14 16:02:33 -0700 | [diff] [blame] | 1368 | return new DeviceAntiEntropyAdvertisement(self, adDevices, adPorts, adOffline); |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 1369 | } |
| 1370 | |
| 1371 | /** |
| 1372 | * Responds to anti-entropy advertisement message. |
HIGUCHI Yuta | 67023a2 | 2016-03-28 13:35:44 -0700 | [diff] [blame] | 1373 | * <p> |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 1374 | * Notify sender about out-dated information using regular replication message. |
| 1375 | * Send back advertisement to sender if not in sync. |
| 1376 | * |
| 1377 | * @param advertisement to respond to |
| 1378 | */ |
| 1379 | private void handleAdvertisement(DeviceAntiEntropyAdvertisement advertisement) { |
| 1380 | |
| 1381 | final NodeId sender = advertisement.sender(); |
| 1382 | |
| 1383 | Map<DeviceFragmentId, Timestamp> devAds = new HashMap<>(advertisement.deviceFingerPrints()); |
| 1384 | Map<PortFragmentId, Timestamp> portAds = new HashMap<>(advertisement.ports()); |
| 1385 | Map<DeviceId, Timestamp> offlineAds = new HashMap<>(advertisement.offline()); |
| 1386 | |
| 1387 | // Fragments to request |
| 1388 | Collection<DeviceFragmentId> reqDevices = new ArrayList<>(); |
| 1389 | Collection<PortFragmentId> reqPorts = new ArrayList<>(); |
| 1390 | |
Yuta HIGUCHI | bf71dff | 2014-10-14 16:02:33 -0700 | [diff] [blame] | 1391 | for (Entry<DeviceId, Map<ProviderId, DeviceDescriptions>> de : deviceDescs.entrySet()) { |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 1392 | final DeviceId deviceId = de.getKey(); |
| 1393 | final Map<ProviderId, DeviceDescriptions> lDevice = de.getValue(); |
| 1394 | |
| 1395 | synchronized (lDevice) { |
| 1396 | // latestTimestamp across provider |
| 1397 | // Note: can be null initially |
| 1398 | Timestamp localLatest = offline.get(deviceId); |
| 1399 | |
| 1400 | // handle device Ads |
| 1401 | for (Entry<ProviderId, DeviceDescriptions> prov : lDevice.entrySet()) { |
| 1402 | final ProviderId provId = prov.getKey(); |
| 1403 | final DeviceDescriptions lDeviceDescs = prov.getValue(); |
| 1404 | |
| 1405 | final DeviceFragmentId devFragId = new DeviceFragmentId(deviceId, provId); |
| 1406 | |
| 1407 | |
| 1408 | Timestamped<DeviceDescription> lProvDevice = lDeviceDescs.getDeviceDesc(); |
| 1409 | Timestamp advDevTimestamp = devAds.get(devFragId); |
| 1410 | |
Jonathan Hart | 403ea93 | 2015-02-20 16:23:00 -0800 | [diff] [blame] | 1411 | if (advDevTimestamp == null || lProvDevice.isNewerThan( |
| 1412 | advDevTimestamp)) { |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 1413 | // remote does not have it or outdated, suggest |
| 1414 | notifyPeer(sender, new InternalDeviceEvent(provId, deviceId, lProvDevice)); |
| 1415 | } else if (!lProvDevice.timestamp().equals(advDevTimestamp)) { |
| 1416 | // local is outdated, request |
| 1417 | reqDevices.add(devFragId); |
| 1418 | } |
| 1419 | |
| 1420 | // handle port Ads |
| 1421 | for (Entry<PortNumber, Timestamped<PortDescription>> |
| 1422 | pe : lDeviceDescs.getPortDescs().entrySet()) { |
| 1423 | |
| 1424 | final PortNumber num = pe.getKey(); |
| 1425 | final Timestamped<PortDescription> lPort = pe.getValue(); |
| 1426 | |
| 1427 | final PortFragmentId portFragId = new PortFragmentId(deviceId, provId, num); |
| 1428 | |
| 1429 | Timestamp advPortTimestamp = portAds.get(portFragId); |
Jonathan Hart | 403ea93 | 2015-02-20 16:23:00 -0800 | [diff] [blame] | 1430 | if (advPortTimestamp == null || lPort.isNewerThan( |
| 1431 | advPortTimestamp)) { |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 1432 | // remote does not have it or outdated, suggest |
| 1433 | notifyPeer(sender, new InternalPortStatusEvent(provId, deviceId, lPort)); |
| 1434 | } else if (!lPort.timestamp().equals(advPortTimestamp)) { |
| 1435 | // local is outdated, request |
| 1436 | log.trace("need update {} < {}", lPort.timestamp(), advPortTimestamp); |
| 1437 | reqPorts.add(portFragId); |
| 1438 | } |
| 1439 | |
| 1440 | // remove port Ad already processed |
| 1441 | portAds.remove(portFragId); |
| 1442 | } // end local port loop |
| 1443 | |
| 1444 | // remove device Ad already processed |
| 1445 | devAds.remove(devFragId); |
| 1446 | |
| 1447 | // find latest and update |
| 1448 | final Timestamp providerLatest = lDeviceDescs.getLatestTimestamp(); |
| 1449 | if (localLatest == null || |
Thomas Vachuska | fdbc4c2 | 2015-05-29 15:53:01 -0700 | [diff] [blame] | 1450 | providerLatest.compareTo(localLatest) > 0) { |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 1451 | localLatest = providerLatest; |
| 1452 | } |
| 1453 | } // end local provider loop |
| 1454 | |
| 1455 | // checking if remote timestamp is more recent. |
| 1456 | Timestamp rOffline = offlineAds.get(deviceId); |
jaegonkim | 3b5d416 | 2018-01-13 10:52:02 +0900 | [diff] [blame] | 1457 | if (localLatest == null || (rOffline != null && rOffline.compareTo(localLatest) > 0)) { |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 1458 | // remote offline timestamp suggests that the |
| 1459 | // device is off-line |
| 1460 | markOfflineInternal(deviceId, rOffline); |
| 1461 | } |
| 1462 | |
| 1463 | Timestamp lOffline = offline.get(deviceId); |
| 1464 | if (lOffline != null && rOffline == null) { |
| 1465 | // locally offline, but remote is online, suggest offline |
| 1466 | notifyPeer(sender, new InternalDeviceOfflineEvent(deviceId, lOffline)); |
| 1467 | } |
| 1468 | |
| 1469 | // remove device offline Ad already processed |
| 1470 | offlineAds.remove(deviceId); |
| 1471 | } // end local device loop |
| 1472 | } // device lock |
| 1473 | |
| 1474 | // If there is any Ads left, request them |
| 1475 | log.trace("Ads left {}, {}", devAds, portAds); |
| 1476 | reqDevices.addAll(devAds.keySet()); |
| 1477 | reqPorts.addAll(portAds.keySet()); |
| 1478 | |
| 1479 | if (reqDevices.isEmpty() && reqPorts.isEmpty()) { |
| 1480 | log.trace("Nothing to request to remote peer {}", sender); |
| 1481 | return; |
| 1482 | } |
| 1483 | |
Yuta HIGUCHI | 1a01272 | 2014-11-20 15:21:41 -0800 | [diff] [blame] | 1484 | log.debug("Need to sync {} {}", reqDevices, reqPorts); |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 1485 | |
| 1486 | // 2-way Anti-Entropy for now |
| 1487 | try { |
| 1488 | unicastMessage(sender, DEVICE_ADVERTISE, createAdvertisement()); |
| 1489 | } catch (IOException e) { |
| 1490 | log.error("Failed to send response advertisement to " + sender, e); |
| 1491 | } |
| 1492 | |
| 1493 | // Sketch of 3-way Anti-Entropy |
| 1494 | // DeviceAntiEntropyRequest request = new DeviceAntiEntropyRequest(self, reqDevices, reqPorts); |
| 1495 | // ClusterMessage message = new ClusterMessage( |
| 1496 | // clusterService.getLocalNode().id(), |
| 1497 | // GossipDeviceStoreMessageSubjects.DEVICE_REQUEST, |
| 1498 | // SERIALIZER.encode(request)); |
| 1499 | // |
| 1500 | // try { |
| 1501 | // clusterCommunicator.unicast(message, advertisement.sender()); |
| 1502 | // } catch (IOException e) { |
| 1503 | // log.error("Failed to send advertisement reply to " |
| 1504 | // + advertisement.sender(), e); |
| 1505 | // } |
Madan Jampani | 47c9373 | 2014-10-06 20:46:08 -0700 | [diff] [blame] | 1506 | } |
| 1507 | |
Madan Jampani | 255a58b | 2014-10-09 12:08:20 -0700 | [diff] [blame] | 1508 | private void notifyDelegateIfNotNull(DeviceEvent event) { |
| 1509 | if (event != null) { |
| 1510 | notifyDelegate(event); |
| 1511 | } |
| 1512 | } |
| 1513 | |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 1514 | private final class SendAdvertisementTask implements Runnable { |
| 1515 | |
| 1516 | @Override |
| 1517 | public void run() { |
| 1518 | if (Thread.currentThread().isInterrupted()) { |
Yuta HIGUCHI | 1a01272 | 2014-11-20 15:21:41 -0800 | [diff] [blame] | 1519 | log.debug("Interrupted, quitting"); |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 1520 | return; |
| 1521 | } |
| 1522 | |
| 1523 | try { |
| 1524 | final NodeId self = clusterService.getLocalNode().id(); |
| 1525 | Set<ControllerNode> nodes = clusterService.getNodes(); |
| 1526 | |
| 1527 | ImmutableList<NodeId> nodeIds = FluentIterable.from(nodes) |
| 1528 | .transform(toNodeId()) |
| 1529 | .toList(); |
| 1530 | |
| 1531 | if (nodeIds.size() == 1 && nodeIds.get(0).equals(self)) { |
Yuta HIGUCHI | faf9e1c | 2014-11-20 00:31:29 -0800 | [diff] [blame] | 1532 | log.trace("No other peers in the cluster."); |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 1533 | return; |
| 1534 | } |
| 1535 | |
| 1536 | NodeId peer; |
| 1537 | do { |
| 1538 | int idx = RandomUtils.nextInt(0, nodeIds.size()); |
| 1539 | peer = nodeIds.get(idx); |
| 1540 | } while (peer.equals(self)); |
| 1541 | |
| 1542 | DeviceAntiEntropyAdvertisement ad = createAdvertisement(); |
| 1543 | |
| 1544 | if (Thread.currentThread().isInterrupted()) { |
Yuta HIGUCHI | 1a01272 | 2014-11-20 15:21:41 -0800 | [diff] [blame] | 1545 | log.debug("Interrupted, quitting"); |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 1546 | return; |
| 1547 | } |
| 1548 | |
| 1549 | try { |
| 1550 | unicastMessage(peer, DEVICE_ADVERTISE, ad); |
| 1551 | } catch (IOException e) { |
Yuta HIGUCHI | 78f3a0a | 2014-10-16 17:24:20 -0700 | [diff] [blame] | 1552 | log.debug("Failed to send anti-entropy advertisement to {}", peer); |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 1553 | return; |
| 1554 | } |
| 1555 | } catch (Exception e) { |
| 1556 | // catch all Exception to avoid Scheduled task being suppressed. |
| 1557 | log.error("Exception thrown while sending advertisement", e); |
| 1558 | } |
| 1559 | } |
| 1560 | } |
| 1561 | |
Jonathan Hart | 46ab5cc | 2016-09-15 15:42:39 -0700 | [diff] [blame] | 1562 | private void handleDeviceEvent(InternalDeviceEvent event) { |
| 1563 | ProviderId providerId = event.providerId(); |
| 1564 | DeviceId deviceId = event.deviceId(); |
| 1565 | Timestamped<DeviceDescription> deviceDescription = event.deviceDescription(); |
Madan Jampani | 2532253 | 2014-10-08 11:20:38 -0700 | [diff] [blame] | 1566 | |
Jonathan Hart | 46ab5cc | 2016-09-15 15:42:39 -0700 | [diff] [blame] | 1567 | try { |
| 1568 | notifyDelegateIfNotNull(createOrUpdateDeviceInternal(providerId, deviceId, |
| 1569 | deviceDescription)); |
| 1570 | } catch (Exception e) { |
| 1571 | log.warn("Exception thrown handling device update", e); |
Madan Jampani | 47c9373 | 2014-10-06 20:46:08 -0700 | [diff] [blame] | 1572 | } |
| 1573 | } |
| 1574 | |
Jonathan Hart | 46ab5cc | 2016-09-15 15:42:39 -0700 | [diff] [blame] | 1575 | private void handleDeviceOfflineEvent(InternalDeviceOfflineEvent event) { |
| 1576 | DeviceId deviceId = event.deviceId(); |
| 1577 | Timestamp timestamp = event.timestamp(); |
Madan Jampani | 2532253 | 2014-10-08 11:20:38 -0700 | [diff] [blame] | 1578 | |
Jonathan Hart | 46ab5cc | 2016-09-15 15:42:39 -0700 | [diff] [blame] | 1579 | try { |
| 1580 | notifyDelegateIfNotNull(markOfflineInternal(deviceId, timestamp)); |
| 1581 | } catch (Exception e) { |
| 1582 | log.warn("Exception thrown handling device offline", e); |
Madan Jampani | 2532253 | 2014-10-08 11:20:38 -0700 | [diff] [blame] | 1583 | } |
| 1584 | } |
| 1585 | |
Jonathan Hart | 46ab5cc | 2016-09-15 15:42:39 -0700 | [diff] [blame] | 1586 | private void handleRemoveRequest(DeviceId did) { |
| 1587 | try { |
Jayasree Ghosh | 7d96d6a | 2017-02-27 18:35:32 +0530 | [diff] [blame] | 1588 | DeviceEvent event = removeDevice(did); |
| 1589 | notifyDelegateIfNotNull(event); |
Jonathan Hart | 46ab5cc | 2016-09-15 15:42:39 -0700 | [diff] [blame] | 1590 | } catch (Exception e) { |
| 1591 | log.warn("Exception thrown handling device remove", e); |
Yuta HIGUCHI | 53afd5b | 2014-11-03 18:03:08 -0800 | [diff] [blame] | 1592 | } |
| 1593 | } |
| 1594 | |
Jonathan Hart | 46ab5cc | 2016-09-15 15:42:39 -0700 | [diff] [blame] | 1595 | private void handleDeviceRemovedEvent(InternalDeviceRemovedEvent event) { |
| 1596 | DeviceId deviceId = event.deviceId(); |
| 1597 | Timestamp timestamp = event.timestamp(); |
Madan Jampani | 3fc72ed | 2014-10-08 12:50:27 -0700 | [diff] [blame] | 1598 | |
Jonathan Hart | 46ab5cc | 2016-09-15 15:42:39 -0700 | [diff] [blame] | 1599 | try { |
| 1600 | notifyDelegateIfNotNull(removeDeviceInternal(deviceId, timestamp)); |
| 1601 | } catch (Exception e) { |
| 1602 | log.warn("Exception thrown handling device removed", e); |
Madan Jampani | 3fc72ed | 2014-10-08 12:50:27 -0700 | [diff] [blame] | 1603 | } |
| 1604 | } |
| 1605 | |
Jonathan Hart | 46ab5cc | 2016-09-15 15:42:39 -0700 | [diff] [blame] | 1606 | private void handlePortEvent(InternalPortEvent event) { |
| 1607 | ProviderId providerId = event.providerId(); |
| 1608 | DeviceId deviceId = event.deviceId(); |
| 1609 | Timestamped<List<PortDescription>> portDescriptions = event.portDescriptions(); |
Madan Jampani | 47c9373 | 2014-10-06 20:46:08 -0700 | [diff] [blame] | 1610 | |
Jonathan Hart | 46ab5cc | 2016-09-15 15:42:39 -0700 | [diff] [blame] | 1611 | if (getDevice(deviceId) == null) { |
| 1612 | log.debug("{} not found on this node yet, ignoring.", deviceId); |
| 1613 | // Note: dropped information will be recovered by anti-entropy |
| 1614 | return; |
| 1615 | } |
Madan Jampani | 47c9373 | 2014-10-06 20:46:08 -0700 | [diff] [blame] | 1616 | |
Jonathan Hart | 46ab5cc | 2016-09-15 15:42:39 -0700 | [diff] [blame] | 1617 | try { |
| 1618 | notifyDelegate(updatePortsInternal(providerId, deviceId, portDescriptions)); |
| 1619 | } catch (Exception e) { |
| 1620 | log.warn("Exception thrown handling port update", e); |
Madan Jampani | 47c9373 | 2014-10-06 20:46:08 -0700 | [diff] [blame] | 1621 | } |
| 1622 | } |
| 1623 | |
Jonathan Hart | 46ab5cc | 2016-09-15 15:42:39 -0700 | [diff] [blame] | 1624 | private void handlePortStatusEvent(InternalPortStatusEvent event) { |
| 1625 | ProviderId providerId = event.providerId(); |
| 1626 | DeviceId deviceId = event.deviceId(); |
| 1627 | Timestamped<PortDescription> portDescription = event.portDescription(); |
Madan Jampani | 47c9373 | 2014-10-06 20:46:08 -0700 | [diff] [blame] | 1628 | |
Jonathan Hart | 46ab5cc | 2016-09-15 15:42:39 -0700 | [diff] [blame] | 1629 | if (getDevice(deviceId) == null) { |
| 1630 | log.debug("{} not found on this node yet, ignoring.", deviceId); |
| 1631 | // Note: dropped information will be recovered by anti-entropy |
| 1632 | return; |
| 1633 | } |
Madan Jampani | 47c9373 | 2014-10-06 20:46:08 -0700 | [diff] [blame] | 1634 | |
Jonathan Hart | 46ab5cc | 2016-09-15 15:42:39 -0700 | [diff] [blame] | 1635 | try { |
| 1636 | notifyDelegateIfNotNull(updatePortStatusInternal(providerId, deviceId, portDescription)); |
| 1637 | } catch (Exception e) { |
| 1638 | log.warn("Exception thrown handling port update", e); |
Madan Jampani | 47c9373 | 2014-10-06 20:46:08 -0700 | [diff] [blame] | 1639 | } |
| 1640 | } |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 1641 | |
Jonathan Hart | 46ab5cc | 2016-09-15 15:42:39 -0700 | [diff] [blame] | 1642 | private void handleDeviceAdvertisement(DeviceAntiEntropyAdvertisement advertisement) { |
| 1643 | try { |
| 1644 | handleAdvertisement(advertisement); |
| 1645 | } catch (Exception e) { |
| 1646 | log.warn("Exception thrown handling Device advertisements.", e); |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 1647 | } |
| 1648 | } |
Marc De Leenheer | b473b9d | 2015-02-06 15:21:03 -0800 | [diff] [blame] | 1649 | |
Thomas Vachuska | fdbc4c2 | 2015-05-29 15:53:01 -0700 | [diff] [blame] | 1650 | private class InternalPortStatsListener |
| 1651 | implements EventuallyConsistentMapListener<DeviceId, Map<PortNumber, PortStatistics>> { |
| 1652 | @Override |
| 1653 | public void event(EventuallyConsistentMapEvent<DeviceId, Map<PortNumber, PortStatistics>> event) { |
| 1654 | if (event.type() == PUT) { |
| 1655 | Device device = devices.get(event.key()); |
| 1656 | if (device != null) { |
Thomas Vachuska | d4955ae | 2016-08-23 14:56:37 -0700 | [diff] [blame] | 1657 | notifyDelegate(new DeviceEvent(PORT_STATS_UPDATED, device)); |
Thomas Vachuska | fdbc4c2 | 2015-05-29 15:53:01 -0700 | [diff] [blame] | 1658 | } |
| 1659 | } |
| 1660 | } |
| 1661 | } |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 1662 | } |