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