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