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