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