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