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