blob: f3ae7a4ac6645133ffcc5f478896440eda873c9c [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
2 * Copyright 2014 Open Networking Laboratory
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 */
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -070016package org.onlab.onos.store.device.impl;
17
Yuta HIGUCHI47c40882014-10-10 18:44:37 -070018import com.google.common.base.Function;
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -070019import com.google.common.collect.FluentIterable;
20import com.google.common.collect.ImmutableList;
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -070021import com.google.common.collect.Maps;
22import com.google.common.collect.Sets;
Madan Jampani47c93732014-10-06 20:46:08 -070023
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -070024import org.apache.commons.lang3.RandomUtils;
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -070025import org.apache.felix.scr.annotations.Activate;
26import org.apache.felix.scr.annotations.Component;
27import org.apache.felix.scr.annotations.Deactivate;
28import org.apache.felix.scr.annotations.Reference;
29import org.apache.felix.scr.annotations.ReferenceCardinality;
30import org.apache.felix.scr.annotations.Service;
Madan Jampani53e44e62014-10-07 12:39:51 -070031import org.onlab.onos.cluster.ClusterService;
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -070032import org.onlab.onos.cluster.ControllerNode;
33import org.onlab.onos.cluster.NodeId;
Yuta HIGUCHIe8252bb2014-10-22 09:41:01 -070034import org.onlab.onos.mastership.MastershipService;
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -070035import org.onlab.onos.net.AnnotationsUtil;
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -070036import org.onlab.onos.net.DefaultAnnotations;
37import org.onlab.onos.net.DefaultDevice;
38import org.onlab.onos.net.DefaultPort;
39import org.onlab.onos.net.Device;
40import org.onlab.onos.net.Device.Type;
41import org.onlab.onos.net.DeviceId;
42import org.onlab.onos.net.Port;
43import org.onlab.onos.net.PortNumber;
Yuta HIGUCHI093e83e2014-10-10 22:26:11 -070044import org.onlab.onos.net.device.DeviceClockService;
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -070045import org.onlab.onos.net.device.DeviceDescription;
46import org.onlab.onos.net.device.DeviceEvent;
47import org.onlab.onos.net.device.DeviceStore;
48import org.onlab.onos.net.device.DeviceStoreDelegate;
49import org.onlab.onos.net.device.PortDescription;
50import org.onlab.onos.net.provider.ProviderId;
51import org.onlab.onos.store.AbstractStore;
Yuta HIGUCHId40483d2014-10-09 15:20:30 -070052import org.onlab.onos.store.Timestamp;
Madan Jampani47c93732014-10-06 20:46:08 -070053import org.onlab.onos.store.cluster.messaging.ClusterCommunicationService;
54import org.onlab.onos.store.cluster.messaging.ClusterMessage;
55import org.onlab.onos.store.cluster.messaging.ClusterMessageHandler;
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -070056import org.onlab.onos.store.cluster.messaging.MessageSubject;
Yuta HIGUCHIeecee552014-10-16 14:09:01 -070057import org.onlab.onos.store.impl.Timestamped;
Madan Jampani53e44e62014-10-07 12:39:51 -070058import org.onlab.onos.store.serializers.KryoSerializer;
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -070059import org.onlab.onos.store.serializers.DistributedStoreSerializers;
alshabib7911a052014-10-16 17:49:37 -070060import org.onlab.packet.ChassisId;
Yuta HIGUCHI8d143d22014-10-19 23:15:09 -070061import org.onlab.util.KryoNamespace;
Yuta HIGUCHI0d6a5e62014-10-03 15:54:09 -070062import org.onlab.util.NewConcurrentHashMap;
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -070063import org.slf4j.Logger;
64
Madan Jampani47c93732014-10-06 20:46:08 -070065import java.io.IOException;
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -070066import java.util.ArrayList;
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -070067import java.util.Collection;
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -070068import java.util.Collections;
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -070069import java.util.HashMap;
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -070070import java.util.HashSet;
71import java.util.Iterator;
72import java.util.List;
73import java.util.Map;
74import java.util.Map.Entry;
75import java.util.Objects;
76import java.util.Set;
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -070077import java.util.concurrent.ConcurrentMap;
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -070078import java.util.concurrent.ScheduledExecutorService;
79import java.util.concurrent.TimeUnit;
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -070080
81import static com.google.common.base.Preconditions.checkArgument;
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -070082import static com.google.common.base.Predicates.notNull;
Yuta HIGUCHIdc7374c2014-10-10 11:11:09 -070083import static org.onlab.onos.cluster.ControllerNodeToNodeId.toNodeId;
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -070084import static org.onlab.onos.net.device.DeviceEvent.Type.*;
85import static org.slf4j.LoggerFactory.getLogger;
86import static org.apache.commons.lang3.concurrent.ConcurrentUtils.createIfAbsentUnchecked;
87import static org.onlab.onos.net.DefaultAnnotations.merge;
88import static com.google.common.base.Verify.verify;
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -070089import static org.onlab.util.Tools.namedThreads;
90import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
91import static org.onlab.onos.store.device.impl.GossipDeviceStoreMessageSubjects.DEVICE_ADVERTISE;
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -070092
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -070093// TODO: give me a better name
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -070094/**
95 * Manages inventory of infrastructure devices using gossip protocol to distribute
96 * information.
97 */
98@Component(immediate = true)
99@Service
100public class GossipDeviceStore
101 extends AbstractStore<DeviceEvent, DeviceStoreDelegate>
102 implements DeviceStore {
103
104 private final Logger log = getLogger(getClass());
105
Yuta HIGUCHIbf71dff2014-10-14 16:02:33 -0700106 private static final String DEVICE_NOT_FOUND = "Device with ID %s not found";
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700107
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700108 // innerMap is used to lock a Device, thus instance should never be replaced.
109 // collection of Description given from various providers
Yuta HIGUCHIbf71dff2014-10-14 16:02:33 -0700110 private final ConcurrentMap<DeviceId, Map<ProviderId, DeviceDescriptions>>
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700111 deviceDescs = Maps.newConcurrentMap();
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700112
113 // cache of Device and Ports generated by compositing descriptions from providers
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700114 private final ConcurrentMap<DeviceId, Device> devices = Maps.newConcurrentMap();
115 private final ConcurrentMap<DeviceId, ConcurrentMap<PortNumber, Port>> devicePorts = Maps.newConcurrentMap();
116
117 // to be updated under Device lock
118 private final Map<DeviceId, Timestamp> offline = Maps.newHashMap();
119 private final Map<DeviceId, Timestamp> removalRequest = Maps.newHashMap();
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700120
121 // available(=UP) devices
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700122 private final Set<DeviceId> availableDevices = Sets.newConcurrentHashSet();
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700123
124 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Yuta HIGUCHI093e83e2014-10-10 22:26:11 -0700125 protected DeviceClockService deviceClockService;
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700126
Madan Jampani47c93732014-10-06 20:46:08 -0700127 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
128 protected ClusterCommunicationService clusterCommunicator;
129
Madan Jampani53e44e62014-10-07 12:39:51 -0700130 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
131 protected ClusterService clusterService;
132
Yuta HIGUCHIe8252bb2014-10-22 09:41:01 -0700133 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
134 protected MastershipService mastershipService;
135
Yuta HIGUCHI47c40882014-10-10 18:44:37 -0700136 protected static final KryoSerializer SERIALIZER = new KryoSerializer() {
Yuta HIGUCHI24b2e2a2014-10-07 15:53:57 -0700137 @Override
Madan Jampani53e44e62014-10-07 12:39:51 -0700138 protected void setupKryoPool() {
Yuta HIGUCHI8d143d22014-10-19 23:15:09 -0700139 serializerPool = KryoNamespace.newBuilder()
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -0700140 .register(DistributedStoreSerializers.COMMON)
141
Madan Jampanifef9b3a2014-10-07 18:38:17 -0700142 .register(InternalDeviceEvent.class, new InternalDeviceEventSerializer())
Madan Jampani25322532014-10-08 11:20:38 -0700143 .register(InternalDeviceOfflineEvent.class, new InternalDeviceOfflineEventSerializer())
Madan Jampani3fc72ed2014-10-08 12:50:27 -0700144 .register(InternalDeviceRemovedEvent.class)
Madan Jampanifef9b3a2014-10-07 18:38:17 -0700145 .register(InternalPortEvent.class, new InternalPortEventSerializer())
146 .register(InternalPortStatusEvent.class, new InternalPortStatusEventSerializer())
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -0700147 .register(DeviceAntiEntropyAdvertisement.class)
148 .register(DeviceFragmentId.class)
149 .register(PortFragmentId.class)
Madan Jampani53e44e62014-10-07 12:39:51 -0700150 .build()
151 .populate(1);
152 }
Madan Jampani53e44e62014-10-07 12:39:51 -0700153 };
154
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -0700155 private ScheduledExecutorService executor;
156
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700157 @Activate
158 public void activate() {
Madan Jampani2206e012014-10-06 21:04:20 -0700159 clusterCommunicator.addSubscriber(
160 GossipDeviceStoreMessageSubjects.DEVICE_UPDATE, new InternalDeviceEventListener());
161 clusterCommunicator.addSubscriber(
Madan Jampani25322532014-10-08 11:20:38 -0700162 GossipDeviceStoreMessageSubjects.DEVICE_OFFLINE, new InternalDeviceOfflineEventListener());
163 clusterCommunicator.addSubscriber(
Madan Jampani3fc72ed2014-10-08 12:50:27 -0700164 GossipDeviceStoreMessageSubjects.DEVICE_REMOVED, new InternalDeviceRemovedEventListener());
165 clusterCommunicator.addSubscriber(
Madan Jampani2206e012014-10-06 21:04:20 -0700166 GossipDeviceStoreMessageSubjects.PORT_UPDATE, new InternalPortEventListener());
167 clusterCommunicator.addSubscriber(
168 GossipDeviceStoreMessageSubjects.PORT_STATUS_UPDATE, new InternalPortStatusEventListener());
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -0700169 clusterCommunicator.addSubscriber(
170 GossipDeviceStoreMessageSubjects.DEVICE_ADVERTISE, new InternalDeviceAdvertisementListener());
171
172 executor =
173 newSingleThreadScheduledExecutor(namedThreads("anti-entropy-%d"));
174
175 // TODO: Make these configurable
176 long initialDelaySec = 5;
177 long periodSec = 5;
178 // start anti-entropy thread
179 executor.scheduleAtFixedRate(new SendAdvertisementTask(),
180 initialDelaySec, periodSec, TimeUnit.SECONDS);
181
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700182 log.info("Started");
183 }
184
185 @Deactivate
186 public void deactivate() {
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -0700187
188 executor.shutdownNow();
189 try {
190 boolean timedout = executor.awaitTermination(5, TimeUnit.SECONDS);
191 if (timedout) {
192 log.error("Timeout during executor shutdown");
193 }
194 } catch (InterruptedException e) {
195 log.error("Error during executor shutdown", e);
196 }
197
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700198 deviceDescs.clear();
199 devices.clear();
200 devicePorts.clear();
201 availableDevices.clear();
202 log.info("Stopped");
203 }
204
205 @Override
206 public int getDeviceCount() {
207 return devices.size();
208 }
209
210 @Override
211 public Iterable<Device> getDevices() {
212 return Collections.unmodifiableCollection(devices.values());
213 }
214
215 @Override
216 public Device getDevice(DeviceId deviceId) {
217 return devices.get(deviceId);
218 }
219
220 @Override
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700221 public synchronized DeviceEvent createOrUpdateDevice(ProviderId providerId,
222 DeviceId deviceId,
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700223 DeviceDescription deviceDescription) {
Yuta HIGUCHI093e83e2014-10-10 22:26:11 -0700224 final Timestamp newTimestamp = deviceClockService.getTimestamp(deviceId);
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700225 final Timestamped<DeviceDescription> deltaDesc = new Timestamped<>(deviceDescription, newTimestamp);
Yuta HIGUCHI47c40882014-10-10 18:44:37 -0700226 final DeviceEvent event;
227 final Timestamped<DeviceDescription> mergedDesc;
Yuta HIGUCHIbf71dff2014-10-14 16:02:33 -0700228 synchronized (getOrCreateDeviceDescriptionsMap(deviceId)) {
Yuta HIGUCHI47c40882014-10-10 18:44:37 -0700229 event = createOrUpdateDeviceInternal(providerId, deviceId, deltaDesc);
Yuta HIGUCHIbf71dff2014-10-14 16:02:33 -0700230 mergedDesc = getOrCreateDeviceDescriptionsMap(deviceId).get(providerId).getDeviceDesc();
Yuta HIGUCHI47c40882014-10-10 18:44:37 -0700231 }
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700232 if (event != null) {
Madan Jampani47c93732014-10-06 20:46:08 -0700233 log.info("Notifying peers of a device update topology event for providerId: {} and deviceId: {}",
234 providerId, deviceId);
235 try {
Yuta HIGUCHI47c40882014-10-10 18:44:37 -0700236 notifyPeers(new InternalDeviceEvent(providerId, deviceId, mergedDesc));
Madan Jampani47c93732014-10-06 20:46:08 -0700237 } catch (IOException e) {
Madan Jampani25322532014-10-08 11:20:38 -0700238 log.error("Failed to notify peers of a device update topology event for providerId: "
Madan Jampani47c93732014-10-06 20:46:08 -0700239 + providerId + " and deviceId: " + deviceId, e);
240 }
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700241 }
242 return event;
243 }
244
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700245 private DeviceEvent createOrUpdateDeviceInternal(ProviderId providerId,
246 DeviceId deviceId,
247 Timestamped<DeviceDescription> deltaDesc) {
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700248
249 // Collection of DeviceDescriptions for a Device
Yuta HIGUCHIbf71dff2014-10-14 16:02:33 -0700250 Map<ProviderId, DeviceDescriptions> providerDescs
251 = getOrCreateDeviceDescriptionsMap(deviceId);
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700252
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700253 synchronized (providerDescs) {
254 // locking per device
255
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700256 if (isDeviceRemoved(deviceId, deltaDesc.timestamp())) {
257 log.debug("Ignoring outdated event: {}", deltaDesc);
258 return null;
259 }
260
Yuta HIGUCHIbf71dff2014-10-14 16:02:33 -0700261 DeviceDescriptions descs = getOrCreateProviderDeviceDescriptions(providerDescs, providerId, deltaDesc);
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700262
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700263 final Device oldDevice = devices.get(deviceId);
264 final Device newDevice;
265
266 if (deltaDesc == descs.getDeviceDesc() ||
267 deltaDesc.isNewer(descs.getDeviceDesc())) {
268 // on new device or valid update
269 descs.putDeviceDesc(deltaDesc);
270 newDevice = composeDevice(deviceId, providerDescs);
271 } else {
272 // outdated event, ignored.
273 return null;
274 }
275 if (oldDevice == null) {
276 // ADD
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700277 return createDevice(providerId, newDevice, deltaDesc.timestamp());
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700278 } else {
279 // UPDATE or ignore (no change or stale)
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700280 return updateDevice(providerId, oldDevice, newDevice, deltaDesc.timestamp());
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700281 }
282 }
283 }
284
285 // Creates the device and returns the appropriate event if necessary.
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700286 // Guarded by deviceDescs value (=Device lock)
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700287 private DeviceEvent createDevice(ProviderId providerId,
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700288 Device newDevice, Timestamp timestamp) {
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700289
290 // update composed device cache
291 Device oldDevice = devices.putIfAbsent(newDevice.id(), newDevice);
292 verify(oldDevice == null,
293 "Unexpected Device in cache. PID:%s [old=%s, new=%s]",
294 providerId, oldDevice, newDevice);
295
296 if (!providerId.isAncillary()) {
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700297 markOnline(newDevice.id(), timestamp);
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700298 }
299
300 return new DeviceEvent(DeviceEvent.Type.DEVICE_ADDED, newDevice, null);
301 }
302
303 // Updates the device and returns the appropriate event if necessary.
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700304 // Guarded by deviceDescs value (=Device lock)
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700305 private DeviceEvent updateDevice(ProviderId providerId,
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700306 Device oldDevice,
307 Device newDevice, Timestamp newTimestamp) {
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700308 // We allow only certain attributes to trigger update
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700309 boolean propertiesChanged =
310 !Objects.equals(oldDevice.hwVersion(), newDevice.hwVersion()) ||
311 !Objects.equals(oldDevice.swVersion(), newDevice.swVersion());
312 boolean annotationsChanged =
313 !AnnotationsUtil.isEqual(oldDevice.annotations(), newDevice.annotations());
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700314
Thomas Vachuska56dbeb12014-10-22 16:40:44 -0700315 // Primary providers can respond to all changes, but ancillary ones
316 // should respond only to annotation changes.
317 if ((providerId.isAncillary() && annotationsChanged) ||
318 (!providerId.isAncillary() && (propertiesChanged || annotationsChanged))) {
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700319 boolean replaced = devices.replace(newDevice.id(), oldDevice, newDevice);
320 if (!replaced) {
321 verify(replaced,
322 "Replacing devices cache failed. PID:%s [expected:%s, found:%s, new=%s]",
323 providerId, oldDevice, devices.get(newDevice.id())
324 , newDevice);
325 }
326 if (!providerId.isAncillary()) {
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700327 markOnline(newDevice.id(), newTimestamp);
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700328 }
329 return new DeviceEvent(DeviceEvent.Type.DEVICE_UPDATED, newDevice, null);
330 }
331
332 // Otherwise merely attempt to change availability if primary provider
333 if (!providerId.isAncillary()) {
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700334 boolean added = markOnline(newDevice.id(), newTimestamp);
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700335 return !added ? null :
336 new DeviceEvent(DEVICE_AVAILABILITY_CHANGED, newDevice, null);
337 }
338 return null;
339 }
340
341 @Override
342 public DeviceEvent markOffline(DeviceId deviceId) {
Yuta HIGUCHI093e83e2014-10-10 22:26:11 -0700343 final Timestamp timestamp = deviceClockService.getTimestamp(deviceId);
Yuta HIGUCHI47c40882014-10-10 18:44:37 -0700344 final DeviceEvent event = markOfflineInternal(deviceId, timestamp);
Madan Jampani25322532014-10-08 11:20:38 -0700345 if (event != null) {
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700346 log.info("Notifying peers of a device offline topology event for deviceId: {} {}",
347 deviceId, timestamp);
Madan Jampani25322532014-10-08 11:20:38 -0700348 try {
349 notifyPeers(new InternalDeviceOfflineEvent(deviceId, timestamp));
350 } catch (IOException e) {
351 log.error("Failed to notify peers of a device offline topology event for deviceId: {}",
352 deviceId);
353 }
354 }
355 return event;
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700356 }
357
358 private DeviceEvent markOfflineInternal(DeviceId deviceId, Timestamp timestamp) {
359
360 Map<ProviderId, DeviceDescriptions> providerDescs
Yuta HIGUCHIbf71dff2014-10-14 16:02:33 -0700361 = getOrCreateDeviceDescriptionsMap(deviceId);
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700362
363 // locking device
364 synchronized (providerDescs) {
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700365
366 // accept off-line if given timestamp is newer than
367 // the latest Timestamp from Primary provider
368 DeviceDescriptions primDescs = getPrimaryDescriptions(providerDescs);
369 Timestamp lastTimestamp = primDescs.getLatestTimestamp();
370 if (timestamp.compareTo(lastTimestamp) <= 0) {
371 // outdated event ignore
372 return null;
373 }
374
375 offline.put(deviceId, timestamp);
376
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700377 Device device = devices.get(deviceId);
378 if (device == null) {
379 return null;
380 }
381 boolean removed = availableDevices.remove(deviceId);
382 if (removed) {
383 // TODO: broadcast ... DOWN only?
384 return new DeviceEvent(DEVICE_AVAILABILITY_CHANGED, device, null);
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700385 }
386 return null;
387 }
388 }
389
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700390 /**
391 * Marks the device as available if the given timestamp is not outdated,
392 * compared to the time the device has been marked offline.
393 *
394 * @param deviceId identifier of the device
395 * @param timestamp of the event triggering this change.
396 * @return true if availability change request was accepted and changed the state
397 */
398 // Guarded by deviceDescs value (=Device lock)
399 private boolean markOnline(DeviceId deviceId, Timestamp timestamp) {
400 // accept on-line if given timestamp is newer than
401 // the latest offline request Timestamp
402 Timestamp offlineTimestamp = offline.get(deviceId);
403 if (offlineTimestamp == null ||
404 offlineTimestamp.compareTo(timestamp) < 0) {
405
406 offline.remove(deviceId);
407 return availableDevices.add(deviceId);
408 }
409 return false;
410 }
411
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700412 @Override
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700413 public synchronized List<DeviceEvent> updatePorts(ProviderId providerId,
414 DeviceId deviceId,
415 List<PortDescription> portDescriptions) {
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700416
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700417 final Timestamp newTimestamp;
418 try {
419 newTimestamp = deviceClockService.getTimestamp(deviceId);
420 } catch (IllegalStateException e) {
421 log.info("Timestamp was not available for device {}", deviceId);
422 log.debug(" discarding {}", portDescriptions);
423 // Failed to generate timestamp.
424
425 // Possible situation:
426 // Device connected and became master for short period of time,
427 // but lost mastership before this instance had the chance to
428 // retrieve term information.
429
430 // Information dropped here is expected to be recoverable by
431 // device probing after mastership change
432
433 return Collections.emptyList();
434 }
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700435 log.info("timestamp for {} {}", deviceId, newTimestamp);
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700436
Yuta HIGUCHI47c40882014-10-10 18:44:37 -0700437 final Timestamped<List<PortDescription>> timestampedInput
438 = new Timestamped<>(portDescriptions, newTimestamp);
439 final List<DeviceEvent> events;
440 final Timestamped<List<PortDescription>> merged;
441
Yuta HIGUCHIbf71dff2014-10-14 16:02:33 -0700442 synchronized (getOrCreateDeviceDescriptionsMap(deviceId)) {
Yuta HIGUCHI47c40882014-10-10 18:44:37 -0700443 events = updatePortsInternal(providerId, deviceId, timestampedInput);
Yuta HIGUCHIbf71dff2014-10-14 16:02:33 -0700444 final DeviceDescriptions descs = getOrCreateDeviceDescriptionsMap(deviceId).get(providerId);
Yuta HIGUCHI47c40882014-10-10 18:44:37 -0700445 List<PortDescription> mergedList =
446 FluentIterable.from(portDescriptions)
447 .transform(new Function<PortDescription, PortDescription>() {
448 @Override
449 public PortDescription apply(PortDescription input) {
450 // lookup merged port description
451 return descs.getPortDesc(input.portNumber()).value();
452 }
453 }).toList();
454 merged = new Timestamped<List<PortDescription>>(mergedList, newTimestamp);
455 }
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700456 if (!events.isEmpty()) {
Madan Jampani47c93732014-10-06 20:46:08 -0700457 log.info("Notifying peers of a port update topology event for providerId: {} and deviceId: {}",
458 providerId, deviceId);
459 try {
Yuta HIGUCHI47c40882014-10-10 18:44:37 -0700460 notifyPeers(new InternalPortEvent(providerId, deviceId, merged));
Madan Jampani47c93732014-10-06 20:46:08 -0700461 } catch (IOException e) {
462 log.error("Failed to notify peers of a port update topology event or providerId: "
463 + providerId + " and deviceId: " + deviceId, e);
464 }
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700465 }
466 return events;
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700467 }
468
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700469 private List<DeviceEvent> updatePortsInternal(ProviderId providerId,
470 DeviceId deviceId,
471 Timestamped<List<PortDescription>> portDescriptions) {
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700472
473 Device device = devices.get(deviceId);
474 checkArgument(device != null, DEVICE_NOT_FOUND, deviceId);
475
Yuta HIGUCHIbf71dff2014-10-14 16:02:33 -0700476 Map<ProviderId, DeviceDescriptions> descsMap = deviceDescs.get(deviceId);
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700477 checkArgument(descsMap != null, DEVICE_NOT_FOUND, deviceId);
478
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700479 List<DeviceEvent> events = new ArrayList<>();
480 synchronized (descsMap) {
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700481
482 if (isDeviceRemoved(deviceId, portDescriptions.timestamp())) {
483 log.debug("Ignoring outdated events: {}", portDescriptions);
484 return null;
485 }
486
487 DeviceDescriptions descs = descsMap.get(providerId);
488 // every provider must provide DeviceDescription.
489 checkArgument(descs != null,
490 "Device description for Device ID %s from Provider %s was not found",
491 deviceId, providerId);
492
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700493 Map<PortNumber, Port> ports = getPortMap(deviceId);
494
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700495 final Timestamp newTimestamp = portDescriptions.timestamp();
496
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700497 // Add new ports
498 Set<PortNumber> processed = new HashSet<>();
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700499 for (PortDescription portDescription : portDescriptions.value()) {
500 final PortNumber number = portDescription.portNumber();
501 processed.add(number);
502
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700503 final Port oldPort = ports.get(number);
504 final Port newPort;
505
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700506
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700507 final Timestamped<PortDescription> existingPortDesc = descs.getPortDesc(number);
508 if (existingPortDesc == null ||
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700509 newTimestamp.compareTo(existingPortDesc.timestamp()) >= 0) {
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700510 // on new port or valid update
511 // update description
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700512 descs.putPortDesc(new Timestamped<>(portDescription,
513 portDescriptions.timestamp()));
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700514 newPort = composePort(device, number, descsMap);
515 } else {
516 // outdated event, ignored.
517 continue;
518 }
519
520 events.add(oldPort == null ?
521 createPort(device, newPort, ports) :
522 updatePort(device, oldPort, newPort, ports));
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700523 }
524
525 events.addAll(pruneOldPorts(device, ports, processed));
526 }
527 return FluentIterable.from(events).filter(notNull()).toList();
528 }
529
530 // Creates a new port based on the port description adds it to the map and
531 // Returns corresponding event.
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700532 // Guarded by deviceDescs value (=Device lock)
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700533 private DeviceEvent createPort(Device device, Port newPort,
534 Map<PortNumber, Port> ports) {
535 ports.put(newPort.number(), newPort);
536 return new DeviceEvent(PORT_ADDED, device, newPort);
537 }
538
539 // Checks if the specified port requires update and if so, it replaces the
540 // existing entry in the map and returns corresponding event.
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700541 // Guarded by deviceDescs value (=Device lock)
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700542 private DeviceEvent updatePort(Device device, Port oldPort,
543 Port newPort,
544 Map<PortNumber, Port> ports) {
545 if (oldPort.isEnabled() != newPort.isEnabled() ||
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -0700546 !AnnotationsUtil.isEqual(oldPort.annotations(), newPort.annotations())) {
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700547
548 ports.put(oldPort.number(), newPort);
549 return new DeviceEvent(PORT_UPDATED, device, newPort);
550 }
551 return null;
552 }
553
554 // Prunes the specified list of ports based on which ports are in the
555 // processed list and returns list of corresponding events.
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700556 // Guarded by deviceDescs value (=Device lock)
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700557 private List<DeviceEvent> pruneOldPorts(Device device,
558 Map<PortNumber, Port> ports,
559 Set<PortNumber> processed) {
560 List<DeviceEvent> events = new ArrayList<>();
Yuta HIGUCHI02649072014-10-15 23:28:20 -0700561 Iterator<Entry<PortNumber, Port>> iterator = ports.entrySet().iterator();
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700562 while (iterator.hasNext()) {
Yuta HIGUCHI02649072014-10-15 23:28:20 -0700563 Entry<PortNumber, Port> e = iterator.next();
564 PortNumber portNumber = e.getKey();
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700565 if (!processed.contains(portNumber)) {
Yuta HIGUCHI02649072014-10-15 23:28:20 -0700566 events.add(new DeviceEvent(PORT_REMOVED, device, e.getValue()));
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700567 iterator.remove();
568 }
569 }
570 return events;
571 }
572
573 // Gets the map of ports for the specified device; if one does not already
574 // exist, it creates and registers a new one.
575 private ConcurrentMap<PortNumber, Port> getPortMap(DeviceId deviceId) {
576 return createIfAbsentUnchecked(devicePorts, deviceId,
Yuta HIGUCHI0d6a5e62014-10-03 15:54:09 -0700577 NewConcurrentHashMap.<PortNumber, Port>ifNeeded());
578 }
579
Yuta HIGUCHIbf71dff2014-10-14 16:02:33 -0700580 private Map<ProviderId, DeviceDescriptions> getOrCreateDeviceDescriptionsMap(
Yuta HIGUCHI0d6a5e62014-10-03 15:54:09 -0700581 DeviceId deviceId) {
Yuta HIGUCHIbf71dff2014-10-14 16:02:33 -0700582 Map<ProviderId, DeviceDescriptions> r;
583 r = deviceDescs.get(deviceId);
584 if (r == null) {
585 r = new HashMap<ProviderId, DeviceDescriptions>();
586 final Map<ProviderId, DeviceDescriptions> concurrentlyAdded;
587 concurrentlyAdded = deviceDescs.putIfAbsent(deviceId, r);
588 if (concurrentlyAdded != null) {
589 r = concurrentlyAdded;
590 }
591 }
592 return r;
593 }
594
595 // Guarded by deviceDescs value (=Device lock)
596 private DeviceDescriptions getOrCreateProviderDeviceDescriptions(
597 Map<ProviderId, DeviceDescriptions> device,
598 ProviderId providerId, Timestamped<DeviceDescription> deltaDesc) {
599
600 synchronized (device) {
601 DeviceDescriptions r = device.get(providerId);
602 if (r == null) {
603 r = new DeviceDescriptions(deltaDesc);
604 device.put(providerId, r);
605 }
606 return r;
607 }
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700608 }
609
610 @Override
Yuta HIGUCHI47c40882014-10-10 18:44:37 -0700611 public synchronized DeviceEvent updatePortStatus(ProviderId providerId,
612 DeviceId deviceId,
613 PortDescription portDescription) {
614
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700615 final Timestamp newTimestamp;
616 try {
617 newTimestamp = deviceClockService.getTimestamp(deviceId);
618 } catch (IllegalStateException e) {
619 log.info("Timestamp was not available for device {}", deviceId);
620 log.debug(" discarding {}", portDescription);
621 // Failed to generate timestamp. Ignoring.
622 // See updatePorts comment
623 return null;
624 }
Yuta HIGUCHI47c40882014-10-10 18:44:37 -0700625 final Timestamped<PortDescription> deltaDesc
626 = new Timestamped<>(portDescription, newTimestamp);
627 final DeviceEvent event;
628 final Timestamped<PortDescription> mergedDesc;
Yuta HIGUCHIbf71dff2014-10-14 16:02:33 -0700629 synchronized (getOrCreateDeviceDescriptionsMap(deviceId)) {
Yuta HIGUCHI47c40882014-10-10 18:44:37 -0700630 event = updatePortStatusInternal(providerId, deviceId, deltaDesc);
Yuta HIGUCHIbf71dff2014-10-14 16:02:33 -0700631 mergedDesc = getOrCreateDeviceDescriptionsMap(deviceId).get(providerId)
Yuta HIGUCHI47c40882014-10-10 18:44:37 -0700632 .getPortDesc(portDescription.portNumber());
633 }
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700634 if (event != null) {
Madan Jampani47c93732014-10-06 20:46:08 -0700635 log.info("Notifying peers of a port status update topology event for providerId: {} and deviceId: {}",
636 providerId, deviceId);
637 try {
Yuta HIGUCHI47c40882014-10-10 18:44:37 -0700638 notifyPeers(new InternalPortStatusEvent(providerId, deviceId, mergedDesc));
Madan Jampani47c93732014-10-06 20:46:08 -0700639 } catch (IOException e) {
640 log.error("Failed to notify peers of a port status update topology event or providerId: "
641 + providerId + " and deviceId: " + deviceId, e);
642 }
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700643 }
644 return event;
645 }
646
647 private DeviceEvent updatePortStatusInternal(ProviderId providerId, DeviceId deviceId,
648 Timestamped<PortDescription> deltaDesc) {
649
650 Device device = devices.get(deviceId);
651 checkArgument(device != null, DEVICE_NOT_FOUND, deviceId);
652
Yuta HIGUCHIbf71dff2014-10-14 16:02:33 -0700653 Map<ProviderId, DeviceDescriptions> descsMap = deviceDescs.get(deviceId);
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700654 checkArgument(descsMap != null, DEVICE_NOT_FOUND, deviceId);
655
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700656 synchronized (descsMap) {
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700657
658 if (isDeviceRemoved(deviceId, deltaDesc.timestamp())) {
659 log.debug("Ignoring outdated event: {}", deltaDesc);
660 return null;
661 }
662
663 DeviceDescriptions descs = descsMap.get(providerId);
664 // assuming all providers must to give DeviceDescription
Yuta HIGUCHIbf71dff2014-10-14 16:02:33 -0700665 verify(descs != null,
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700666 "Device description for Device ID %s from Provider %s was not found",
667 deviceId, providerId);
668
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700669 ConcurrentMap<PortNumber, Port> ports = getPortMap(deviceId);
670 final PortNumber number = deltaDesc.value().portNumber();
671 final Port oldPort = ports.get(number);
672 final Port newPort;
673
674 final Timestamped<PortDescription> existingPortDesc = descs.getPortDesc(number);
675 if (existingPortDesc == null ||
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700676 deltaDesc.isNewer(existingPortDesc)) {
677 // on new port or valid update
678 // update description
679 descs.putPortDesc(deltaDesc);
680 newPort = composePort(device, number, descsMap);
681 } else {
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -0700682 // same or outdated event, ignored.
683 log.trace("ignore same or outdated {} >= {}", existingPortDesc, deltaDesc);
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700684 return null;
685 }
686
687 if (oldPort == null) {
688 return createPort(device, newPort, ports);
689 } else {
690 return updatePort(device, oldPort, newPort, ports);
691 }
692 }
693 }
694
695 @Override
696 public List<Port> getPorts(DeviceId deviceId) {
697 Map<PortNumber, Port> ports = devicePorts.get(deviceId);
698 if (ports == null) {
699 return Collections.emptyList();
700 }
701 return ImmutableList.copyOf(ports.values());
702 }
703
704 @Override
705 public Port getPort(DeviceId deviceId, PortNumber portNumber) {
706 Map<PortNumber, Port> ports = devicePorts.get(deviceId);
707 return ports == null ? null : ports.get(portNumber);
708 }
709
710 @Override
711 public boolean isAvailable(DeviceId deviceId) {
712 return availableDevices.contains(deviceId);
713 }
714
715 @Override
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700716 public synchronized DeviceEvent removeDevice(DeviceId deviceId) {
Yuta HIGUCHIe8252bb2014-10-22 09:41:01 -0700717 final NodeId master = mastershipService.getMasterFor(deviceId);
718 if (!clusterService.getLocalNode().id().equals(master)) {
Thomas Vachuska444eda62014-10-28 13:09:42 -0700719 log.info("Removal of device {} requested on non master node", deviceId);
Yuta HIGUCHIe8252bb2014-10-22 09:41:01 -0700720 // FIXME silently ignoring. Should be forwarding or broadcasting to
721 // master.
722 return null;
723 }
724
Yuta HIGUCHI093e83e2014-10-10 22:26:11 -0700725 Timestamp timestamp = deviceClockService.getTimestamp(deviceId);
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700726 DeviceEvent event = removeDeviceInternal(deviceId, timestamp);
Madan Jampani3fc72ed2014-10-08 12:50:27 -0700727 if (event != null) {
728 log.info("Notifying peers of a device removed topology event for deviceId: {}",
729 deviceId);
730 try {
731 notifyPeers(new InternalDeviceRemovedEvent(deviceId, timestamp));
732 } catch (IOException e) {
733 log.error("Failed to notify peers of a device removed topology event for deviceId: {}",
734 deviceId);
735 }
736 }
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700737 return event;
738 }
739
740 private DeviceEvent removeDeviceInternal(DeviceId deviceId,
741 Timestamp timestamp) {
742
Yuta HIGUCHIbf71dff2014-10-14 16:02:33 -0700743 Map<ProviderId, DeviceDescriptions> descs = getOrCreateDeviceDescriptionsMap(deviceId);
Yuta HIGUCHI0d6a5e62014-10-03 15:54:09 -0700744 synchronized (descs) {
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700745 // accept removal request if given timestamp is newer than
746 // the latest Timestamp from Primary provider
747 DeviceDescriptions primDescs = getPrimaryDescriptions(descs);
748 Timestamp lastTimestamp = primDescs.getLatestTimestamp();
749 if (timestamp.compareTo(lastTimestamp) <= 0) {
750 // outdated event ignore
751 return null;
752 }
753 removalRequest.put(deviceId, timestamp);
754
Yuta HIGUCHI0d6a5e62014-10-03 15:54:09 -0700755 Device device = devices.remove(deviceId);
756 // should DEVICE_REMOVED carry removed ports?
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700757 Map<PortNumber, Port> ports = devicePorts.get(deviceId);
758 if (ports != null) {
759 ports.clear();
760 }
761 markOfflineInternal(deviceId, timestamp);
Yuta HIGUCHI0d6a5e62014-10-03 15:54:09 -0700762 descs.clear();
763 return device == null ? null :
764 new DeviceEvent(DEVICE_REMOVED, device, null);
765 }
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700766 }
767
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -0700768 /**
769 * Checks if given timestamp is superseded by removal request
770 * with more recent timestamp.
771 *
772 * @param deviceId identifier of a device
773 * @param timestampToCheck timestamp of an event to check
774 * @return true if device is already removed
775 */
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700776 private boolean isDeviceRemoved(DeviceId deviceId, Timestamp timestampToCheck) {
777 Timestamp removalTimestamp = removalRequest.get(deviceId);
778 if (removalTimestamp != null &&
779 removalTimestamp.compareTo(timestampToCheck) >= 0) {
780 // removalRequest is more recent
781 return true;
782 }
783 return false;
784 }
785
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700786 /**
787 * Returns a Device, merging description given from multiple Providers.
788 *
789 * @param deviceId device identifier
790 * @param providerDescs Collection of Descriptions from multiple providers
791 * @return Device instance
792 */
793 private Device composeDevice(DeviceId deviceId,
Yuta HIGUCHI47c40882014-10-10 18:44:37 -0700794 Map<ProviderId, DeviceDescriptions> providerDescs) {
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700795
Thomas Vachuska444eda62014-10-28 13:09:42 -0700796 checkArgument(!providerDescs.isEmpty(), "No device descriptions supplied");
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700797
798 ProviderId primary = pickPrimaryPID(providerDescs);
799
800 DeviceDescriptions desc = providerDescs.get(primary);
801
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700802 final DeviceDescription base = desc.getDeviceDesc().value();
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700803 Type type = base.type();
804 String manufacturer = base.manufacturer();
805 String hwVersion = base.hwVersion();
806 String swVersion = base.swVersion();
807 String serialNumber = base.serialNumber();
alshabib7911a052014-10-16 17:49:37 -0700808 ChassisId chassisId = base.chassisId();
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700809 DefaultAnnotations annotations = DefaultAnnotations.builder().build();
810 annotations = merge(annotations, base.annotations());
811
812 for (Entry<ProviderId, DeviceDescriptions> e : providerDescs.entrySet()) {
813 if (e.getKey().equals(primary)) {
814 continue;
815 }
816 // TODO: should keep track of Description timestamp
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -0700817 // and only merge conflicting keys when timestamp is newer.
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700818 // Currently assuming there will never be a key conflict between
819 // providers
820
821 // annotation merging. not so efficient, should revisit later
822 annotations = merge(annotations, e.getValue().getDeviceDesc().value().annotations());
823 }
824
825 return new DefaultDevice(primary, deviceId , type, manufacturer,
alshabib7911a052014-10-16 17:49:37 -0700826 hwVersion, swVersion, serialNumber,
827 chassisId, annotations);
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700828 }
829
830 /**
831 * Returns a Port, merging description given from multiple Providers.
832 *
833 * @param device device the port is on
834 * @param number port number
Yuta HIGUCHIbf71dff2014-10-14 16:02:33 -0700835 * @param descsMap Collection of Descriptions from multiple providers
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700836 * @return Port instance
837 */
838 private Port composePort(Device device, PortNumber number,
Yuta HIGUCHIbf71dff2014-10-14 16:02:33 -0700839 Map<ProviderId, DeviceDescriptions> descsMap) {
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700840
Yuta HIGUCHIbf71dff2014-10-14 16:02:33 -0700841 ProviderId primary = pickPrimaryPID(descsMap);
842 DeviceDescriptions primDescs = descsMap.get(primary);
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700843 // if no primary, assume not enabled
844 // TODO: revisit this default port enabled/disabled behavior
845 boolean isEnabled = false;
846 DefaultAnnotations annotations = DefaultAnnotations.builder().build();
847
848 final Timestamped<PortDescription> portDesc = primDescs.getPortDesc(number);
849 if (portDesc != null) {
850 isEnabled = portDesc.value().isEnabled();
851 annotations = merge(annotations, portDesc.value().annotations());
852 }
853
Yuta HIGUCHIbf71dff2014-10-14 16:02:33 -0700854 for (Entry<ProviderId, DeviceDescriptions> e : descsMap.entrySet()) {
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700855 if (e.getKey().equals(primary)) {
856 continue;
857 }
858 // TODO: should keep track of Description timestamp
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -0700859 // and only merge conflicting keys when timestamp is newer.
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700860 // Currently assuming there will never be a key conflict between
861 // providers
862
863 // annotation merging. not so efficient, should revisit later
864 final Timestamped<PortDescription> otherPortDesc = e.getValue().getPortDesc(number);
865 if (otherPortDesc != null) {
866 annotations = merge(annotations, otherPortDesc.value().annotations());
867 }
868 }
869
870 return new DefaultPort(device, number, isEnabled, annotations);
871 }
872
873 /**
874 * @return primary ProviderID, or randomly chosen one if none exists
875 */
876 private ProviderId pickPrimaryPID(
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700877 Map<ProviderId, DeviceDescriptions> providerDescs) {
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700878 ProviderId fallBackPrimary = null;
879 for (Entry<ProviderId, DeviceDescriptions> e : providerDescs.entrySet()) {
880 if (!e.getKey().isAncillary()) {
881 return e.getKey();
882 } else if (fallBackPrimary == null) {
883 // pick randomly as a fallback in case there is no primary
884 fallBackPrimary = e.getKey();
885 }
886 }
887 return fallBackPrimary;
888 }
889
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700890 private DeviceDescriptions getPrimaryDescriptions(
891 Map<ProviderId, DeviceDescriptions> providerDescs) {
892 ProviderId pid = pickPrimaryPID(providerDescs);
893 return providerDescs.get(pid);
894 }
895
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -0700896 // TODO: should we be throwing exception?
897 private void unicastMessage(NodeId recipient, MessageSubject subject, Object event) throws IOException {
898 ClusterMessage message = new ClusterMessage(
899 clusterService.getLocalNode().id(),
900 subject,
901 SERIALIZER.encode(event));
902 clusterCommunicator.unicast(message, recipient);
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700903 }
904
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -0700905 // TODO: should we be throwing exception?
906 private void broadcastMessage(MessageSubject subject, Object event) throws IOException {
907 ClusterMessage message = new ClusterMessage(
908 clusterService.getLocalNode().id(),
909 subject,
910 SERIALIZER.encode(event));
911 clusterCommunicator.broadcast(message);
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700912 }
Madan Jampani47c93732014-10-06 20:46:08 -0700913
914 private void notifyPeers(InternalDeviceEvent event) throws IOException {
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -0700915 broadcastMessage(GossipDeviceStoreMessageSubjects.DEVICE_UPDATE, event);
Madan Jampani47c93732014-10-06 20:46:08 -0700916 }
917
Madan Jampani25322532014-10-08 11:20:38 -0700918 private void notifyPeers(InternalDeviceOfflineEvent event) throws IOException {
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -0700919 broadcastMessage(GossipDeviceStoreMessageSubjects.DEVICE_OFFLINE, event);
Madan Jampani25322532014-10-08 11:20:38 -0700920 }
921
Madan Jampani3fc72ed2014-10-08 12:50:27 -0700922 private void notifyPeers(InternalDeviceRemovedEvent event) throws IOException {
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -0700923 broadcastMessage(GossipDeviceStoreMessageSubjects.DEVICE_REMOVED, event);
Madan Jampani3fc72ed2014-10-08 12:50:27 -0700924 }
925
Madan Jampani47c93732014-10-06 20:46:08 -0700926 private void notifyPeers(InternalPortEvent event) throws IOException {
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -0700927 broadcastMessage(GossipDeviceStoreMessageSubjects.PORT_UPDATE, event);
Madan Jampani47c93732014-10-06 20:46:08 -0700928 }
929
930 private void notifyPeers(InternalPortStatusEvent event) throws IOException {
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -0700931 broadcastMessage(GossipDeviceStoreMessageSubjects.PORT_STATUS_UPDATE, event);
932 }
933
934 private void notifyPeer(NodeId recipient, InternalDeviceEvent event) {
935 try {
936 unicastMessage(recipient, GossipDeviceStoreMessageSubjects.DEVICE_UPDATE, event);
937 } catch (IOException e) {
938 log.error("Failed to send" + event + " to " + recipient, e);
939 }
940 }
941
942 private void notifyPeer(NodeId recipient, InternalDeviceOfflineEvent event) {
943 try {
944 unicastMessage(recipient, GossipDeviceStoreMessageSubjects.DEVICE_OFFLINE, event);
945 } catch (IOException e) {
946 log.error("Failed to send" + event + " to " + recipient, e);
947 }
948 }
949
950 private void notifyPeer(NodeId recipient, InternalDeviceRemovedEvent event) {
951 try {
952 unicastMessage(recipient, GossipDeviceStoreMessageSubjects.DEVICE_REMOVED, event);
953 } catch (IOException e) {
954 log.error("Failed to send" + event + " to " + recipient, e);
955 }
956 }
957
958 private void notifyPeer(NodeId recipient, InternalPortEvent event) {
959 try {
960 unicastMessage(recipient, GossipDeviceStoreMessageSubjects.PORT_UPDATE, event);
961 } catch (IOException e) {
962 log.error("Failed to send" + event + " to " + recipient, e);
963 }
964 }
965
966 private void notifyPeer(NodeId recipient, InternalPortStatusEvent event) {
967 try {
968 unicastMessage(recipient, GossipDeviceStoreMessageSubjects.PORT_STATUS_UPDATE, event);
969 } catch (IOException e) {
970 log.error("Failed to send" + event + " to " + recipient, e);
971 }
972 }
973
974 private DeviceAntiEntropyAdvertisement createAdvertisement() {
975 final NodeId self = clusterService.getLocalNode().id();
976
Yuta HIGUCHIbf71dff2014-10-14 16:02:33 -0700977 final int numDevices = deviceDescs.size();
978 Map<DeviceFragmentId, Timestamp> adDevices = new HashMap<>(numDevices);
979 final int portsPerDevice = 8; // random factor to minimize reallocation
980 Map<PortFragmentId, Timestamp> adPorts = new HashMap<>(numDevices * portsPerDevice);
981 Map<DeviceId, Timestamp> adOffline = new HashMap<>(numDevices);
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -0700982
Yuta HIGUCHIbf71dff2014-10-14 16:02:33 -0700983 for (Entry<DeviceId, Map<ProviderId, DeviceDescriptions>>
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -0700984 provs : deviceDescs.entrySet()) {
985
Yuta HIGUCHIbf71dff2014-10-14 16:02:33 -0700986 // for each Device...
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -0700987 final DeviceId deviceId = provs.getKey();
Yuta HIGUCHIbf71dff2014-10-14 16:02:33 -0700988 final Map<ProviderId, DeviceDescriptions> devDescs = provs.getValue();
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -0700989 synchronized (devDescs) {
990
Yuta HIGUCHIbf71dff2014-10-14 16:02:33 -0700991 // send device offline timestamp
992 Timestamp lOffline = this.offline.get(deviceId);
993 if (lOffline != null) {
994 adOffline.put(deviceId, lOffline);
995 }
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -0700996
997 for (Entry<ProviderId, DeviceDescriptions>
998 prov : devDescs.entrySet()) {
999
Yuta HIGUCHIbf71dff2014-10-14 16:02:33 -07001000 // for each Provider Descriptions...
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -07001001 final ProviderId provId = prov.getKey();
1002 final DeviceDescriptions descs = prov.getValue();
1003
Yuta HIGUCHIbf71dff2014-10-14 16:02:33 -07001004 adDevices.put(new DeviceFragmentId(deviceId, provId),
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -07001005 descs.getDeviceDesc().timestamp());
1006
1007 for (Entry<PortNumber, Timestamped<PortDescription>>
1008 portDesc : descs.getPortDescs().entrySet()) {
1009
1010 final PortNumber number = portDesc.getKey();
Yuta HIGUCHIbf71dff2014-10-14 16:02:33 -07001011 adPorts.put(new PortFragmentId(deviceId, provId, number),
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -07001012 portDesc.getValue().timestamp());
1013 }
1014 }
1015 }
1016 }
1017
Yuta HIGUCHIbf71dff2014-10-14 16:02:33 -07001018 return new DeviceAntiEntropyAdvertisement(self, adDevices, adPorts, adOffline);
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -07001019 }
1020
1021 /**
1022 * Responds to anti-entropy advertisement message.
1023 * <P>
1024 * Notify sender about out-dated information using regular replication message.
1025 * Send back advertisement to sender if not in sync.
1026 *
1027 * @param advertisement to respond to
1028 */
1029 private void handleAdvertisement(DeviceAntiEntropyAdvertisement advertisement) {
1030
1031 final NodeId sender = advertisement.sender();
1032
1033 Map<DeviceFragmentId, Timestamp> devAds = new HashMap<>(advertisement.deviceFingerPrints());
1034 Map<PortFragmentId, Timestamp> portAds = new HashMap<>(advertisement.ports());
1035 Map<DeviceId, Timestamp> offlineAds = new HashMap<>(advertisement.offline());
1036
1037 // Fragments to request
1038 Collection<DeviceFragmentId> reqDevices = new ArrayList<>();
1039 Collection<PortFragmentId> reqPorts = new ArrayList<>();
1040
Yuta HIGUCHIbf71dff2014-10-14 16:02:33 -07001041 for (Entry<DeviceId, Map<ProviderId, DeviceDescriptions>> de : deviceDescs.entrySet()) {
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -07001042 final DeviceId deviceId = de.getKey();
1043 final Map<ProviderId, DeviceDescriptions> lDevice = de.getValue();
1044
1045 synchronized (lDevice) {
1046 // latestTimestamp across provider
1047 // Note: can be null initially
1048 Timestamp localLatest = offline.get(deviceId);
1049
1050 // handle device Ads
1051 for (Entry<ProviderId, DeviceDescriptions> prov : lDevice.entrySet()) {
1052 final ProviderId provId = prov.getKey();
1053 final DeviceDescriptions lDeviceDescs = prov.getValue();
1054
1055 final DeviceFragmentId devFragId = new DeviceFragmentId(deviceId, provId);
1056
1057
1058 Timestamped<DeviceDescription> lProvDevice = lDeviceDescs.getDeviceDesc();
1059 Timestamp advDevTimestamp = devAds.get(devFragId);
1060
1061 if (advDevTimestamp == null || lProvDevice.isNewer(advDevTimestamp)) {
1062 // remote does not have it or outdated, suggest
1063 notifyPeer(sender, new InternalDeviceEvent(provId, deviceId, lProvDevice));
1064 } else if (!lProvDevice.timestamp().equals(advDevTimestamp)) {
1065 // local is outdated, request
1066 reqDevices.add(devFragId);
1067 }
1068
1069 // handle port Ads
1070 for (Entry<PortNumber, Timestamped<PortDescription>>
1071 pe : lDeviceDescs.getPortDescs().entrySet()) {
1072
1073 final PortNumber num = pe.getKey();
1074 final Timestamped<PortDescription> lPort = pe.getValue();
1075
1076 final PortFragmentId portFragId = new PortFragmentId(deviceId, provId, num);
1077
1078 Timestamp advPortTimestamp = portAds.get(portFragId);
Yuta HIGUCHIec76bfe2014-10-09 20:17:07 -07001079 if (advPortTimestamp == null || lPort.isNewer(advPortTimestamp)) {
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -07001080 // remote does not have it or outdated, suggest
1081 notifyPeer(sender, new InternalPortStatusEvent(provId, deviceId, lPort));
1082 } else if (!lPort.timestamp().equals(advPortTimestamp)) {
1083 // local is outdated, request
1084 log.trace("need update {} < {}", lPort.timestamp(), advPortTimestamp);
1085 reqPorts.add(portFragId);
1086 }
1087
1088 // remove port Ad already processed
1089 portAds.remove(portFragId);
1090 } // end local port loop
1091
1092 // remove device Ad already processed
1093 devAds.remove(devFragId);
1094
1095 // find latest and update
1096 final Timestamp providerLatest = lDeviceDescs.getLatestTimestamp();
1097 if (localLatest == null ||
1098 providerLatest.compareTo(localLatest) > 0) {
1099 localLatest = providerLatest;
1100 }
1101 } // end local provider loop
1102
1103 // checking if remote timestamp is more recent.
1104 Timestamp rOffline = offlineAds.get(deviceId);
1105 if (rOffline != null &&
1106 rOffline.compareTo(localLatest) > 0) {
1107 // remote offline timestamp suggests that the
1108 // device is off-line
1109 markOfflineInternal(deviceId, rOffline);
1110 }
1111
1112 Timestamp lOffline = offline.get(deviceId);
1113 if (lOffline != null && rOffline == null) {
1114 // locally offline, but remote is online, suggest offline
1115 notifyPeer(sender, new InternalDeviceOfflineEvent(deviceId, lOffline));
1116 }
1117
1118 // remove device offline Ad already processed
1119 offlineAds.remove(deviceId);
1120 } // end local device loop
1121 } // device lock
1122
1123 // If there is any Ads left, request them
1124 log.trace("Ads left {}, {}", devAds, portAds);
1125 reqDevices.addAll(devAds.keySet());
1126 reqPorts.addAll(portAds.keySet());
1127
1128 if (reqDevices.isEmpty() && reqPorts.isEmpty()) {
1129 log.trace("Nothing to request to remote peer {}", sender);
1130 return;
1131 }
1132
1133 log.info("Need to sync {} {}", reqDevices, reqPorts);
1134
1135 // 2-way Anti-Entropy for now
1136 try {
1137 unicastMessage(sender, DEVICE_ADVERTISE, createAdvertisement());
1138 } catch (IOException e) {
1139 log.error("Failed to send response advertisement to " + sender, e);
1140 }
1141
1142// Sketch of 3-way Anti-Entropy
1143// DeviceAntiEntropyRequest request = new DeviceAntiEntropyRequest(self, reqDevices, reqPorts);
1144// ClusterMessage message = new ClusterMessage(
1145// clusterService.getLocalNode().id(),
1146// GossipDeviceStoreMessageSubjects.DEVICE_REQUEST,
1147// SERIALIZER.encode(request));
1148//
1149// try {
1150// clusterCommunicator.unicast(message, advertisement.sender());
1151// } catch (IOException e) {
1152// log.error("Failed to send advertisement reply to "
1153// + advertisement.sender(), e);
1154// }
Madan Jampani47c93732014-10-06 20:46:08 -07001155 }
1156
Madan Jampani255a58b2014-10-09 12:08:20 -07001157 private void notifyDelegateIfNotNull(DeviceEvent event) {
1158 if (event != null) {
1159 notifyDelegate(event);
1160 }
1161 }
1162
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -07001163 private final class SendAdvertisementTask implements Runnable {
1164
1165 @Override
1166 public void run() {
1167 if (Thread.currentThread().isInterrupted()) {
1168 log.info("Interrupted, quitting");
1169 return;
1170 }
1171
1172 try {
1173 final NodeId self = clusterService.getLocalNode().id();
1174 Set<ControllerNode> nodes = clusterService.getNodes();
1175
1176 ImmutableList<NodeId> nodeIds = FluentIterable.from(nodes)
1177 .transform(toNodeId())
1178 .toList();
1179
1180 if (nodeIds.size() == 1 && nodeIds.get(0).equals(self)) {
Yuta HIGUCHI37083082014-10-13 10:38:38 -07001181 log.debug("No other peers in the cluster.");
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -07001182 return;
1183 }
1184
1185 NodeId peer;
1186 do {
1187 int idx = RandomUtils.nextInt(0, nodeIds.size());
1188 peer = nodeIds.get(idx);
1189 } while (peer.equals(self));
1190
1191 DeviceAntiEntropyAdvertisement ad = createAdvertisement();
1192
1193 if (Thread.currentThread().isInterrupted()) {
1194 log.info("Interrupted, quitting");
1195 return;
1196 }
1197
1198 try {
1199 unicastMessage(peer, DEVICE_ADVERTISE, ad);
1200 } catch (IOException e) {
Yuta HIGUCHI78f3a0a2014-10-16 17:24:20 -07001201 log.debug("Failed to send anti-entropy advertisement to {}", peer);
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -07001202 return;
1203 }
1204 } catch (Exception e) {
1205 // catch all Exception to avoid Scheduled task being suppressed.
1206 log.error("Exception thrown while sending advertisement", e);
1207 }
1208 }
1209 }
1210
Madan Jampani47c93732014-10-06 20:46:08 -07001211 private class InternalDeviceEventListener implements ClusterMessageHandler {
1212 @Override
1213 public void handle(ClusterMessage message) {
Madan Jampani25322532014-10-08 11:20:38 -07001214
Madan Jampani47c93732014-10-06 20:46:08 -07001215 log.info("Received device update event from peer: {}", message.sender());
Madan Jampani53e44e62014-10-07 12:39:51 -07001216 InternalDeviceEvent event = (InternalDeviceEvent) SERIALIZER.decode(message.payload());
Madan Jampani25322532014-10-08 11:20:38 -07001217
Madan Jampani47c93732014-10-06 20:46:08 -07001218 ProviderId providerId = event.providerId();
1219 DeviceId deviceId = event.deviceId();
1220 Timestamped<DeviceDescription> deviceDescription = event.deviceDescription();
Madan Jampani25322532014-10-08 11:20:38 -07001221
Madan Jampani255a58b2014-10-09 12:08:20 -07001222 notifyDelegateIfNotNull(createOrUpdateDeviceInternal(providerId, deviceId, deviceDescription));
Madan Jampani47c93732014-10-06 20:46:08 -07001223 }
1224 }
1225
Madan Jampani25322532014-10-08 11:20:38 -07001226 private class InternalDeviceOfflineEventListener implements ClusterMessageHandler {
1227 @Override
1228 public void handle(ClusterMessage message) {
1229
1230 log.info("Received device offline event from peer: {}", message.sender());
1231 InternalDeviceOfflineEvent event = (InternalDeviceOfflineEvent) SERIALIZER.decode(message.payload());
1232
1233 DeviceId deviceId = event.deviceId();
1234 Timestamp timestamp = event.timestamp();
1235
Madan Jampani255a58b2014-10-09 12:08:20 -07001236 notifyDelegateIfNotNull(markOfflineInternal(deviceId, timestamp));
Madan Jampani25322532014-10-08 11:20:38 -07001237 }
1238 }
1239
Madan Jampani3fc72ed2014-10-08 12:50:27 -07001240 private class InternalDeviceRemovedEventListener implements ClusterMessageHandler {
1241 @Override
1242 public void handle(ClusterMessage message) {
1243
1244 log.info("Received device removed event from peer: {}", message.sender());
1245 InternalDeviceRemovedEvent event = (InternalDeviceRemovedEvent) SERIALIZER.decode(message.payload());
1246
1247 DeviceId deviceId = event.deviceId();
1248 Timestamp timestamp = event.timestamp();
1249
Madan Jampani255a58b2014-10-09 12:08:20 -07001250 notifyDelegateIfNotNull(removeDeviceInternal(deviceId, timestamp));
Madan Jampani3fc72ed2014-10-08 12:50:27 -07001251 }
1252 }
1253
Madan Jampani47c93732014-10-06 20:46:08 -07001254 private class InternalPortEventListener implements ClusterMessageHandler {
1255 @Override
1256 public void handle(ClusterMessage message) {
1257
1258 log.info("Received port update event from peer: {}", message.sender());
Madan Jampani53e44e62014-10-07 12:39:51 -07001259 InternalPortEvent event = (InternalPortEvent) SERIALIZER.decode(message.payload());
Madan Jampani47c93732014-10-06 20:46:08 -07001260
1261 ProviderId providerId = event.providerId();
1262 DeviceId deviceId = event.deviceId();
1263 Timestamped<List<PortDescription>> portDescriptions = event.portDescriptions();
1264
Madan Jampani255a58b2014-10-09 12:08:20 -07001265 notifyDelegate(updatePortsInternal(providerId, deviceId, portDescriptions));
Madan Jampani47c93732014-10-06 20:46:08 -07001266 }
1267 }
1268
1269 private class InternalPortStatusEventListener implements ClusterMessageHandler {
1270 @Override
1271 public void handle(ClusterMessage message) {
1272
1273 log.info("Received port status update event from peer: {}", message.sender());
Madan Jampani53e44e62014-10-07 12:39:51 -07001274 InternalPortStatusEvent event = (InternalPortStatusEvent) SERIALIZER.decode(message.payload());
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -07001275 log.info("{}", event);
Madan Jampani47c93732014-10-06 20:46:08 -07001276
1277 ProviderId providerId = event.providerId();
1278 DeviceId deviceId = event.deviceId();
1279 Timestamped<PortDescription> portDescription = event.portDescription();
1280
Madan Jampani255a58b2014-10-09 12:08:20 -07001281 notifyDelegateIfNotNull(updatePortStatusInternal(providerId, deviceId, portDescription));
Madan Jampani47c93732014-10-06 20:46:08 -07001282 }
1283 }
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -07001284
1285 private final class InternalDeviceAdvertisementListener
1286 implements ClusterMessageHandler {
1287
1288 @Override
1289 public void handle(ClusterMessage message) {
Yuta HIGUCHI9a0a1d12014-10-13 22:38:02 -07001290 log.debug("Received Device Anti-Entropy advertisement from peer: {}", message.sender());
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -07001291 DeviceAntiEntropyAdvertisement advertisement = SERIALIZER.decode(message.payload());
1292 handleAdvertisement(advertisement);
1293 }
1294 }
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -07001295}