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