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