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