blob: 5449277c8adb24380b102d21657382e1aa2ca733 [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() ||
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700546 oldPort.type() != newPort.type() ||
547 oldPort.portSpeed() != newPort.portSpeed() ||
548 !AnnotationsUtil.isEqual(oldPort.annotations(), newPort.annotations())) {
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700549 ports.put(oldPort.number(), newPort);
550 return new DeviceEvent(PORT_UPDATED, device, newPort);
551 }
552 return null;
553 }
554
555 // Prunes the specified list of ports based on which ports are in the
556 // processed list and returns list of corresponding events.
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700557 // Guarded by deviceDescs value (=Device lock)
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700558 private List<DeviceEvent> pruneOldPorts(Device device,
559 Map<PortNumber, Port> ports,
560 Set<PortNumber> processed) {
561 List<DeviceEvent> events = new ArrayList<>();
Yuta HIGUCHI02649072014-10-15 23:28:20 -0700562 Iterator<Entry<PortNumber, Port>> iterator = ports.entrySet().iterator();
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700563 while (iterator.hasNext()) {
Yuta HIGUCHI02649072014-10-15 23:28:20 -0700564 Entry<PortNumber, Port> e = iterator.next();
565 PortNumber portNumber = e.getKey();
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700566 if (!processed.contains(portNumber)) {
Yuta HIGUCHI02649072014-10-15 23:28:20 -0700567 events.add(new DeviceEvent(PORT_REMOVED, device, e.getValue()));
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700568 iterator.remove();
569 }
570 }
571 return events;
572 }
573
574 // Gets the map of ports for the specified device; if one does not already
575 // exist, it creates and registers a new one.
576 private ConcurrentMap<PortNumber, Port> getPortMap(DeviceId deviceId) {
577 return createIfAbsentUnchecked(devicePorts, deviceId,
Yuta HIGUCHI0d6a5e62014-10-03 15:54:09 -0700578 NewConcurrentHashMap.<PortNumber, Port>ifNeeded());
579 }
580
Yuta HIGUCHIbf71dff2014-10-14 16:02:33 -0700581 private Map<ProviderId, DeviceDescriptions> getOrCreateDeviceDescriptionsMap(
Yuta HIGUCHI0d6a5e62014-10-03 15:54:09 -0700582 DeviceId deviceId) {
Yuta HIGUCHIbf71dff2014-10-14 16:02:33 -0700583 Map<ProviderId, DeviceDescriptions> r;
584 r = deviceDescs.get(deviceId);
585 if (r == null) {
586 r = new HashMap<ProviderId, DeviceDescriptions>();
587 final Map<ProviderId, DeviceDescriptions> concurrentlyAdded;
588 concurrentlyAdded = deviceDescs.putIfAbsent(deviceId, r);
589 if (concurrentlyAdded != null) {
590 r = concurrentlyAdded;
591 }
592 }
593 return r;
594 }
595
596 // Guarded by deviceDescs value (=Device lock)
597 private DeviceDescriptions getOrCreateProviderDeviceDescriptions(
598 Map<ProviderId, DeviceDescriptions> device,
599 ProviderId providerId, Timestamped<DeviceDescription> deltaDesc) {
600
601 synchronized (device) {
602 DeviceDescriptions r = device.get(providerId);
603 if (r == null) {
604 r = new DeviceDescriptions(deltaDesc);
605 device.put(providerId, r);
606 }
607 return r;
608 }
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700609 }
610
611 @Override
Yuta HIGUCHI47c40882014-10-10 18:44:37 -0700612 public synchronized DeviceEvent updatePortStatus(ProviderId providerId,
613 DeviceId deviceId,
614 PortDescription portDescription) {
615
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700616 final Timestamp newTimestamp;
617 try {
618 newTimestamp = deviceClockService.getTimestamp(deviceId);
619 } catch (IllegalStateException e) {
620 log.info("Timestamp was not available for device {}", deviceId);
621 log.debug(" discarding {}", portDescription);
622 // Failed to generate timestamp. Ignoring.
623 // See updatePorts comment
624 return null;
625 }
Yuta HIGUCHI47c40882014-10-10 18:44:37 -0700626 final Timestamped<PortDescription> deltaDesc
627 = new Timestamped<>(portDescription, newTimestamp);
628 final DeviceEvent event;
629 final Timestamped<PortDescription> mergedDesc;
Yuta HIGUCHIbf71dff2014-10-14 16:02:33 -0700630 synchronized (getOrCreateDeviceDescriptionsMap(deviceId)) {
Yuta HIGUCHI47c40882014-10-10 18:44:37 -0700631 event = updatePortStatusInternal(providerId, deviceId, deltaDesc);
Yuta HIGUCHIbf71dff2014-10-14 16:02:33 -0700632 mergedDesc = getOrCreateDeviceDescriptionsMap(deviceId).get(providerId)
Yuta HIGUCHI47c40882014-10-10 18:44:37 -0700633 .getPortDesc(portDescription.portNumber());
634 }
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700635 if (event != null) {
Madan Jampani47c93732014-10-06 20:46:08 -0700636 log.info("Notifying peers of a port status update topology event for providerId: {} and deviceId: {}",
637 providerId, deviceId);
638 try {
Yuta HIGUCHI47c40882014-10-10 18:44:37 -0700639 notifyPeers(new InternalPortStatusEvent(providerId, deviceId, mergedDesc));
Madan Jampani47c93732014-10-06 20:46:08 -0700640 } catch (IOException e) {
641 log.error("Failed to notify peers of a port status update topology event or providerId: "
642 + providerId + " and deviceId: " + deviceId, e);
643 }
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700644 }
645 return event;
646 }
647
648 private DeviceEvent updatePortStatusInternal(ProviderId providerId, DeviceId deviceId,
649 Timestamped<PortDescription> deltaDesc) {
650
651 Device device = devices.get(deviceId);
652 checkArgument(device != null, DEVICE_NOT_FOUND, deviceId);
653
Yuta HIGUCHIbf71dff2014-10-14 16:02:33 -0700654 Map<ProviderId, DeviceDescriptions> descsMap = deviceDescs.get(deviceId);
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700655 checkArgument(descsMap != null, DEVICE_NOT_FOUND, deviceId);
656
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700657 synchronized (descsMap) {
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700658
659 if (isDeviceRemoved(deviceId, deltaDesc.timestamp())) {
660 log.debug("Ignoring outdated event: {}", deltaDesc);
661 return null;
662 }
663
664 DeviceDescriptions descs = descsMap.get(providerId);
665 // assuming all providers must to give DeviceDescription
Yuta HIGUCHIbf71dff2014-10-14 16:02:33 -0700666 verify(descs != null,
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700667 "Device description for Device ID %s from Provider %s was not found",
668 deviceId, providerId);
669
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700670 ConcurrentMap<PortNumber, Port> ports = getPortMap(deviceId);
671 final PortNumber number = deltaDesc.value().portNumber();
672 final Port oldPort = ports.get(number);
673 final Port newPort;
674
675 final Timestamped<PortDescription> existingPortDesc = descs.getPortDesc(number);
676 if (existingPortDesc == null ||
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700677 deltaDesc.isNewer(existingPortDesc)) {
678 // on new port or valid update
679 // update description
680 descs.putPortDesc(deltaDesc);
681 newPort = composePort(device, number, descsMap);
682 } else {
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -0700683 // same or outdated event, ignored.
684 log.trace("ignore same or outdated {} >= {}", existingPortDesc, deltaDesc);
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700685 return null;
686 }
687
688 if (oldPort == null) {
689 return createPort(device, newPort, ports);
690 } else {
691 return updatePort(device, oldPort, newPort, ports);
692 }
693 }
694 }
695
696 @Override
697 public List<Port> getPorts(DeviceId deviceId) {
698 Map<PortNumber, Port> ports = devicePorts.get(deviceId);
699 if (ports == null) {
700 return Collections.emptyList();
701 }
702 return ImmutableList.copyOf(ports.values());
703 }
704
705 @Override
706 public Port getPort(DeviceId deviceId, PortNumber portNumber) {
707 Map<PortNumber, Port> ports = devicePorts.get(deviceId);
708 return ports == null ? null : ports.get(portNumber);
709 }
710
711 @Override
712 public boolean isAvailable(DeviceId deviceId) {
713 return availableDevices.contains(deviceId);
714 }
715
716 @Override
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700717 public synchronized DeviceEvent removeDevice(DeviceId deviceId) {
Yuta HIGUCHIe8252bb2014-10-22 09:41:01 -0700718 final NodeId master = mastershipService.getMasterFor(deviceId);
719 if (!clusterService.getLocalNode().id().equals(master)) {
Thomas Vachuska444eda62014-10-28 13:09:42 -0700720 log.info("Removal of device {} requested on non master node", deviceId);
Yuta HIGUCHIe8252bb2014-10-22 09:41:01 -0700721 // FIXME silently ignoring. Should be forwarding or broadcasting to
722 // master.
723 return null;
724 }
725
Yuta HIGUCHI093e83e2014-10-10 22:26:11 -0700726 Timestamp timestamp = deviceClockService.getTimestamp(deviceId);
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700727 DeviceEvent event = removeDeviceInternal(deviceId, timestamp);
Madan Jampani3fc72ed2014-10-08 12:50:27 -0700728 if (event != null) {
729 log.info("Notifying peers of a device removed topology event for deviceId: {}",
730 deviceId);
731 try {
732 notifyPeers(new InternalDeviceRemovedEvent(deviceId, timestamp));
733 } catch (IOException e) {
734 log.error("Failed to notify peers of a device removed topology event for deviceId: {}",
735 deviceId);
736 }
737 }
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700738 return event;
739 }
740
741 private DeviceEvent removeDeviceInternal(DeviceId deviceId,
742 Timestamp timestamp) {
743
Yuta HIGUCHIbf71dff2014-10-14 16:02:33 -0700744 Map<ProviderId, DeviceDescriptions> descs = getOrCreateDeviceDescriptionsMap(deviceId);
Yuta HIGUCHI0d6a5e62014-10-03 15:54:09 -0700745 synchronized (descs) {
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700746 // accept removal request if given timestamp is newer than
747 // the latest Timestamp from Primary provider
748 DeviceDescriptions primDescs = getPrimaryDescriptions(descs);
749 Timestamp lastTimestamp = primDescs.getLatestTimestamp();
750 if (timestamp.compareTo(lastTimestamp) <= 0) {
751 // outdated event ignore
752 return null;
753 }
754 removalRequest.put(deviceId, timestamp);
755
Yuta HIGUCHI0d6a5e62014-10-03 15:54:09 -0700756 Device device = devices.remove(deviceId);
757 // should DEVICE_REMOVED carry removed ports?
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700758 Map<PortNumber, Port> ports = devicePorts.get(deviceId);
759 if (ports != null) {
760 ports.clear();
761 }
762 markOfflineInternal(deviceId, timestamp);
Yuta HIGUCHI0d6a5e62014-10-03 15:54:09 -0700763 descs.clear();
764 return device == null ? null :
765 new DeviceEvent(DEVICE_REMOVED, device, null);
766 }
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700767 }
768
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -0700769 /**
770 * Checks if given timestamp is superseded by removal request
771 * with more recent timestamp.
772 *
773 * @param deviceId identifier of a device
774 * @param timestampToCheck timestamp of an event to check
775 * @return true if device is already removed
776 */
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700777 private boolean isDeviceRemoved(DeviceId deviceId, Timestamp timestampToCheck) {
778 Timestamp removalTimestamp = removalRequest.get(deviceId);
779 if (removalTimestamp != null &&
780 removalTimestamp.compareTo(timestampToCheck) >= 0) {
781 // removalRequest is more recent
782 return true;
783 }
784 return false;
785 }
786
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700787 /**
788 * Returns a Device, merging description given from multiple Providers.
789 *
790 * @param deviceId device identifier
791 * @param providerDescs Collection of Descriptions from multiple providers
792 * @return Device instance
793 */
794 private Device composeDevice(DeviceId deviceId,
Yuta HIGUCHI47c40882014-10-10 18:44:37 -0700795 Map<ProviderId, DeviceDescriptions> providerDescs) {
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700796
Thomas Vachuska444eda62014-10-28 13:09:42 -0700797 checkArgument(!providerDescs.isEmpty(), "No device descriptions supplied");
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700798
799 ProviderId primary = pickPrimaryPID(providerDescs);
800
801 DeviceDescriptions desc = providerDescs.get(primary);
802
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700803 final DeviceDescription base = desc.getDeviceDesc().value();
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700804 Type type = base.type();
805 String manufacturer = base.manufacturer();
806 String hwVersion = base.hwVersion();
807 String swVersion = base.swVersion();
808 String serialNumber = base.serialNumber();
alshabib7911a052014-10-16 17:49:37 -0700809 ChassisId chassisId = base.chassisId();
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700810 DefaultAnnotations annotations = DefaultAnnotations.builder().build();
811 annotations = merge(annotations, base.annotations());
812
813 for (Entry<ProviderId, DeviceDescriptions> e : providerDescs.entrySet()) {
814 if (e.getKey().equals(primary)) {
815 continue;
816 }
817 // TODO: should keep track of Description timestamp
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -0700818 // and only merge conflicting keys when timestamp is newer.
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700819 // Currently assuming there will never be a key conflict between
820 // providers
821
822 // annotation merging. not so efficient, should revisit later
823 annotations = merge(annotations, e.getValue().getDeviceDesc().value().annotations());
824 }
825
826 return new DefaultDevice(primary, deviceId , type, manufacturer,
alshabib7911a052014-10-16 17:49:37 -0700827 hwVersion, swVersion, serialNumber,
828 chassisId, annotations);
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700829 }
830
831 /**
832 * Returns a Port, merging description given from multiple Providers.
833 *
834 * @param device device the port is on
835 * @param number port number
Yuta HIGUCHIbf71dff2014-10-14 16:02:33 -0700836 * @param descsMap Collection of Descriptions from multiple providers
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700837 * @return Port instance
838 */
839 private Port composePort(Device device, PortNumber number,
Yuta HIGUCHIbf71dff2014-10-14 16:02:33 -0700840 Map<ProviderId, DeviceDescriptions> descsMap) {
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700841
Yuta HIGUCHIbf71dff2014-10-14 16:02:33 -0700842 ProviderId primary = pickPrimaryPID(descsMap);
843 DeviceDescriptions primDescs = descsMap.get(primary);
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700844 // if no primary, assume not enabled
845 // TODO: revisit this default port enabled/disabled behavior
846 boolean isEnabled = false;
847 DefaultAnnotations annotations = DefaultAnnotations.builder().build();
848
849 final Timestamped<PortDescription> portDesc = primDescs.getPortDesc(number);
850 if (portDesc != null) {
851 isEnabled = portDesc.value().isEnabled();
852 annotations = merge(annotations, portDesc.value().annotations());
853 }
854
Yuta HIGUCHIbf71dff2014-10-14 16:02:33 -0700855 for (Entry<ProviderId, DeviceDescriptions> e : descsMap.entrySet()) {
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700856 if (e.getKey().equals(primary)) {
857 continue;
858 }
859 // TODO: should keep track of Description timestamp
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -0700860 // and only merge conflicting keys when timestamp is newer.
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700861 // Currently assuming there will never be a key conflict between
862 // providers
863
864 // annotation merging. not so efficient, should revisit later
865 final Timestamped<PortDescription> otherPortDesc = e.getValue().getPortDesc(number);
866 if (otherPortDesc != null) {
867 annotations = merge(annotations, otherPortDesc.value().annotations());
868 }
869 }
870
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700871 return portDesc == null ?
872 new DefaultPort(device, number, false, annotations) :
873 new DefaultPort(device, number, isEnabled, portDesc.value().type(),
874 portDesc.value().portSpeed(), annotations);
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700875 }
876
877 /**
878 * @return primary ProviderID, or randomly chosen one if none exists
879 */
880 private ProviderId pickPrimaryPID(
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700881 Map<ProviderId, DeviceDescriptions> providerDescs) {
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700882 ProviderId fallBackPrimary = null;
883 for (Entry<ProviderId, DeviceDescriptions> e : providerDescs.entrySet()) {
884 if (!e.getKey().isAncillary()) {
885 return e.getKey();
886 } else if (fallBackPrimary == null) {
887 // pick randomly as a fallback in case there is no primary
888 fallBackPrimary = e.getKey();
889 }
890 }
891 return fallBackPrimary;
892 }
893
Yuta HIGUCHIc35efac2014-10-06 14:43:53 -0700894 private DeviceDescriptions getPrimaryDescriptions(
895 Map<ProviderId, DeviceDescriptions> providerDescs) {
896 ProviderId pid = pickPrimaryPID(providerDescs);
897 return providerDescs.get(pid);
898 }
899
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -0700900 // TODO: should we be throwing exception?
901 private void unicastMessage(NodeId recipient, MessageSubject subject, Object event) throws IOException {
902 ClusterMessage message = new ClusterMessage(
903 clusterService.getLocalNode().id(),
904 subject,
905 SERIALIZER.encode(event));
906 clusterCommunicator.unicast(message, recipient);
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700907 }
908
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -0700909 // TODO: should we be throwing exception?
910 private void broadcastMessage(MessageSubject subject, Object event) throws IOException {
911 ClusterMessage message = new ClusterMessage(
912 clusterService.getLocalNode().id(),
913 subject,
914 SERIALIZER.encode(event));
915 clusterCommunicator.broadcast(message);
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700916 }
Madan Jampani47c93732014-10-06 20:46:08 -0700917
918 private void notifyPeers(InternalDeviceEvent event) throws IOException {
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -0700919 broadcastMessage(GossipDeviceStoreMessageSubjects.DEVICE_UPDATE, event);
Madan Jampani47c93732014-10-06 20:46:08 -0700920 }
921
Madan Jampani25322532014-10-08 11:20:38 -0700922 private void notifyPeers(InternalDeviceOfflineEvent event) throws IOException {
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -0700923 broadcastMessage(GossipDeviceStoreMessageSubjects.DEVICE_OFFLINE, event);
Madan Jampani25322532014-10-08 11:20:38 -0700924 }
925
Madan Jampani3fc72ed2014-10-08 12:50:27 -0700926 private void notifyPeers(InternalDeviceRemovedEvent event) throws IOException {
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -0700927 broadcastMessage(GossipDeviceStoreMessageSubjects.DEVICE_REMOVED, event);
Madan Jampani3fc72ed2014-10-08 12:50:27 -0700928 }
929
Madan Jampani47c93732014-10-06 20:46:08 -0700930 private void notifyPeers(InternalPortEvent event) throws IOException {
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -0700931 broadcastMessage(GossipDeviceStoreMessageSubjects.PORT_UPDATE, event);
Madan Jampani47c93732014-10-06 20:46:08 -0700932 }
933
934 private void notifyPeers(InternalPortStatusEvent event) throws IOException {
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -0700935 broadcastMessage(GossipDeviceStoreMessageSubjects.PORT_STATUS_UPDATE, event);
936 }
937
938 private void notifyPeer(NodeId recipient, InternalDeviceEvent event) {
939 try {
940 unicastMessage(recipient, GossipDeviceStoreMessageSubjects.DEVICE_UPDATE, event);
941 } catch (IOException e) {
942 log.error("Failed to send" + event + " to " + recipient, e);
943 }
944 }
945
946 private void notifyPeer(NodeId recipient, InternalDeviceOfflineEvent event) {
947 try {
948 unicastMessage(recipient, GossipDeviceStoreMessageSubjects.DEVICE_OFFLINE, event);
949 } catch (IOException e) {
950 log.error("Failed to send" + event + " to " + recipient, e);
951 }
952 }
953
954 private void notifyPeer(NodeId recipient, InternalDeviceRemovedEvent event) {
955 try {
956 unicastMessage(recipient, GossipDeviceStoreMessageSubjects.DEVICE_REMOVED, event);
957 } catch (IOException e) {
958 log.error("Failed to send" + event + " to " + recipient, e);
959 }
960 }
961
962 private void notifyPeer(NodeId recipient, InternalPortEvent event) {
963 try {
964 unicastMessage(recipient, GossipDeviceStoreMessageSubjects.PORT_UPDATE, event);
965 } catch (IOException e) {
966 log.error("Failed to send" + event + " to " + recipient, e);
967 }
968 }
969
970 private void notifyPeer(NodeId recipient, InternalPortStatusEvent event) {
971 try {
972 unicastMessage(recipient, GossipDeviceStoreMessageSubjects.PORT_STATUS_UPDATE, event);
973 } catch (IOException e) {
974 log.error("Failed to send" + event + " to " + recipient, e);
975 }
976 }
977
978 private DeviceAntiEntropyAdvertisement createAdvertisement() {
979 final NodeId self = clusterService.getLocalNode().id();
980
Yuta HIGUCHIbf71dff2014-10-14 16:02:33 -0700981 final int numDevices = deviceDescs.size();
982 Map<DeviceFragmentId, Timestamp> adDevices = new HashMap<>(numDevices);
983 final int portsPerDevice = 8; // random factor to minimize reallocation
984 Map<PortFragmentId, Timestamp> adPorts = new HashMap<>(numDevices * portsPerDevice);
985 Map<DeviceId, Timestamp> adOffline = new HashMap<>(numDevices);
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -0700986
Yuta HIGUCHIbf71dff2014-10-14 16:02:33 -0700987 for (Entry<DeviceId, Map<ProviderId, DeviceDescriptions>>
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -0700988 provs : deviceDescs.entrySet()) {
989
Yuta HIGUCHIbf71dff2014-10-14 16:02:33 -0700990 // for each Device...
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -0700991 final DeviceId deviceId = provs.getKey();
Yuta HIGUCHIbf71dff2014-10-14 16:02:33 -0700992 final Map<ProviderId, DeviceDescriptions> devDescs = provs.getValue();
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -0700993 synchronized (devDescs) {
994
Yuta HIGUCHIbf71dff2014-10-14 16:02:33 -0700995 // send device offline timestamp
996 Timestamp lOffline = this.offline.get(deviceId);
997 if (lOffline != null) {
998 adOffline.put(deviceId, lOffline);
999 }
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -07001000
1001 for (Entry<ProviderId, DeviceDescriptions>
1002 prov : devDescs.entrySet()) {
1003
Yuta HIGUCHIbf71dff2014-10-14 16:02:33 -07001004 // for each Provider Descriptions...
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -07001005 final ProviderId provId = prov.getKey();
1006 final DeviceDescriptions descs = prov.getValue();
1007
Yuta HIGUCHIbf71dff2014-10-14 16:02:33 -07001008 adDevices.put(new DeviceFragmentId(deviceId, provId),
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -07001009 descs.getDeviceDesc().timestamp());
1010
1011 for (Entry<PortNumber, Timestamped<PortDescription>>
1012 portDesc : descs.getPortDescs().entrySet()) {
1013
1014 final PortNumber number = portDesc.getKey();
Yuta HIGUCHIbf71dff2014-10-14 16:02:33 -07001015 adPorts.put(new PortFragmentId(deviceId, provId, number),
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -07001016 portDesc.getValue().timestamp());
1017 }
1018 }
1019 }
1020 }
1021
Yuta HIGUCHIbf71dff2014-10-14 16:02:33 -07001022 return new DeviceAntiEntropyAdvertisement(self, adDevices, adPorts, adOffline);
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -07001023 }
1024
1025 /**
1026 * Responds to anti-entropy advertisement message.
1027 * <P>
1028 * Notify sender about out-dated information using regular replication message.
1029 * Send back advertisement to sender if not in sync.
1030 *
1031 * @param advertisement to respond to
1032 */
1033 private void handleAdvertisement(DeviceAntiEntropyAdvertisement advertisement) {
1034
1035 final NodeId sender = advertisement.sender();
1036
1037 Map<DeviceFragmentId, Timestamp> devAds = new HashMap<>(advertisement.deviceFingerPrints());
1038 Map<PortFragmentId, Timestamp> portAds = new HashMap<>(advertisement.ports());
1039 Map<DeviceId, Timestamp> offlineAds = new HashMap<>(advertisement.offline());
1040
1041 // Fragments to request
1042 Collection<DeviceFragmentId> reqDevices = new ArrayList<>();
1043 Collection<PortFragmentId> reqPorts = new ArrayList<>();
1044
Yuta HIGUCHIbf71dff2014-10-14 16:02:33 -07001045 for (Entry<DeviceId, Map<ProviderId, DeviceDescriptions>> de : deviceDescs.entrySet()) {
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -07001046 final DeviceId deviceId = de.getKey();
1047 final Map<ProviderId, DeviceDescriptions> lDevice = de.getValue();
1048
1049 synchronized (lDevice) {
1050 // latestTimestamp across provider
1051 // Note: can be null initially
1052 Timestamp localLatest = offline.get(deviceId);
1053
1054 // handle device Ads
1055 for (Entry<ProviderId, DeviceDescriptions> prov : lDevice.entrySet()) {
1056 final ProviderId provId = prov.getKey();
1057 final DeviceDescriptions lDeviceDescs = prov.getValue();
1058
1059 final DeviceFragmentId devFragId = new DeviceFragmentId(deviceId, provId);
1060
1061
1062 Timestamped<DeviceDescription> lProvDevice = lDeviceDescs.getDeviceDesc();
1063 Timestamp advDevTimestamp = devAds.get(devFragId);
1064
1065 if (advDevTimestamp == null || lProvDevice.isNewer(advDevTimestamp)) {
1066 // remote does not have it or outdated, suggest
1067 notifyPeer(sender, new InternalDeviceEvent(provId, deviceId, lProvDevice));
1068 } else if (!lProvDevice.timestamp().equals(advDevTimestamp)) {
1069 // local is outdated, request
1070 reqDevices.add(devFragId);
1071 }
1072
1073 // handle port Ads
1074 for (Entry<PortNumber, Timestamped<PortDescription>>
1075 pe : lDeviceDescs.getPortDescs().entrySet()) {
1076
1077 final PortNumber num = pe.getKey();
1078 final Timestamped<PortDescription> lPort = pe.getValue();
1079
1080 final PortFragmentId portFragId = new PortFragmentId(deviceId, provId, num);
1081
1082 Timestamp advPortTimestamp = portAds.get(portFragId);
Yuta HIGUCHIec76bfe2014-10-09 20:17:07 -07001083 if (advPortTimestamp == null || lPort.isNewer(advPortTimestamp)) {
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -07001084 // remote does not have it or outdated, suggest
1085 notifyPeer(sender, new InternalPortStatusEvent(provId, deviceId, lPort));
1086 } else if (!lPort.timestamp().equals(advPortTimestamp)) {
1087 // local is outdated, request
1088 log.trace("need update {} < {}", lPort.timestamp(), advPortTimestamp);
1089 reqPorts.add(portFragId);
1090 }
1091
1092 // remove port Ad already processed
1093 portAds.remove(portFragId);
1094 } // end local port loop
1095
1096 // remove device Ad already processed
1097 devAds.remove(devFragId);
1098
1099 // find latest and update
1100 final Timestamp providerLatest = lDeviceDescs.getLatestTimestamp();
1101 if (localLatest == null ||
1102 providerLatest.compareTo(localLatest) > 0) {
1103 localLatest = providerLatest;
1104 }
1105 } // end local provider loop
1106
1107 // checking if remote timestamp is more recent.
1108 Timestamp rOffline = offlineAds.get(deviceId);
1109 if (rOffline != null &&
1110 rOffline.compareTo(localLatest) > 0) {
1111 // remote offline timestamp suggests that the
1112 // device is off-line
1113 markOfflineInternal(deviceId, rOffline);
1114 }
1115
1116 Timestamp lOffline = offline.get(deviceId);
1117 if (lOffline != null && rOffline == null) {
1118 // locally offline, but remote is online, suggest offline
1119 notifyPeer(sender, new InternalDeviceOfflineEvent(deviceId, lOffline));
1120 }
1121
1122 // remove device offline Ad already processed
1123 offlineAds.remove(deviceId);
1124 } // end local device loop
1125 } // device lock
1126
1127 // If there is any Ads left, request them
1128 log.trace("Ads left {}, {}", devAds, portAds);
1129 reqDevices.addAll(devAds.keySet());
1130 reqPorts.addAll(portAds.keySet());
1131
1132 if (reqDevices.isEmpty() && reqPorts.isEmpty()) {
1133 log.trace("Nothing to request to remote peer {}", sender);
1134 return;
1135 }
1136
1137 log.info("Need to sync {} {}", reqDevices, reqPorts);
1138
1139 // 2-way Anti-Entropy for now
1140 try {
1141 unicastMessage(sender, DEVICE_ADVERTISE, createAdvertisement());
1142 } catch (IOException e) {
1143 log.error("Failed to send response advertisement to " + sender, e);
1144 }
1145
1146// Sketch of 3-way Anti-Entropy
1147// DeviceAntiEntropyRequest request = new DeviceAntiEntropyRequest(self, reqDevices, reqPorts);
1148// ClusterMessage message = new ClusterMessage(
1149// clusterService.getLocalNode().id(),
1150// GossipDeviceStoreMessageSubjects.DEVICE_REQUEST,
1151// SERIALIZER.encode(request));
1152//
1153// try {
1154// clusterCommunicator.unicast(message, advertisement.sender());
1155// } catch (IOException e) {
1156// log.error("Failed to send advertisement reply to "
1157// + advertisement.sender(), e);
1158// }
Madan Jampani47c93732014-10-06 20:46:08 -07001159 }
1160
Madan Jampani255a58b2014-10-09 12:08:20 -07001161 private void notifyDelegateIfNotNull(DeviceEvent event) {
1162 if (event != null) {
1163 notifyDelegate(event);
1164 }
1165 }
1166
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -07001167 private final class SendAdvertisementTask implements Runnable {
1168
1169 @Override
1170 public void run() {
1171 if (Thread.currentThread().isInterrupted()) {
1172 log.info("Interrupted, quitting");
1173 return;
1174 }
1175
1176 try {
1177 final NodeId self = clusterService.getLocalNode().id();
1178 Set<ControllerNode> nodes = clusterService.getNodes();
1179
1180 ImmutableList<NodeId> nodeIds = FluentIterable.from(nodes)
1181 .transform(toNodeId())
1182 .toList();
1183
1184 if (nodeIds.size() == 1 && nodeIds.get(0).equals(self)) {
Yuta HIGUCHI37083082014-10-13 10:38:38 -07001185 log.debug("No other peers in the cluster.");
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -07001186 return;
1187 }
1188
1189 NodeId peer;
1190 do {
1191 int idx = RandomUtils.nextInt(0, nodeIds.size());
1192 peer = nodeIds.get(idx);
1193 } while (peer.equals(self));
1194
1195 DeviceAntiEntropyAdvertisement ad = createAdvertisement();
1196
1197 if (Thread.currentThread().isInterrupted()) {
1198 log.info("Interrupted, quitting");
1199 return;
1200 }
1201
1202 try {
1203 unicastMessage(peer, DEVICE_ADVERTISE, ad);
1204 } catch (IOException e) {
Yuta HIGUCHI78f3a0a2014-10-16 17:24:20 -07001205 log.debug("Failed to send anti-entropy advertisement to {}", peer);
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -07001206 return;
1207 }
1208 } catch (Exception e) {
1209 // catch all Exception to avoid Scheduled task being suppressed.
1210 log.error("Exception thrown while sending advertisement", e);
1211 }
1212 }
1213 }
1214
Madan Jampani47c93732014-10-06 20:46:08 -07001215 private class InternalDeviceEventListener implements ClusterMessageHandler {
1216 @Override
1217 public void handle(ClusterMessage message) {
Madan Jampani25322532014-10-08 11:20:38 -07001218
Madan Jampani47c93732014-10-06 20:46:08 -07001219 log.info("Received device update event from peer: {}", message.sender());
Madan Jampani53e44e62014-10-07 12:39:51 -07001220 InternalDeviceEvent event = (InternalDeviceEvent) SERIALIZER.decode(message.payload());
Madan Jampani25322532014-10-08 11:20:38 -07001221
Madan Jampani47c93732014-10-06 20:46:08 -07001222 ProviderId providerId = event.providerId();
1223 DeviceId deviceId = event.deviceId();
1224 Timestamped<DeviceDescription> deviceDescription = event.deviceDescription();
Madan Jampani25322532014-10-08 11:20:38 -07001225
Madan Jampani255a58b2014-10-09 12:08:20 -07001226 notifyDelegateIfNotNull(createOrUpdateDeviceInternal(providerId, deviceId, deviceDescription));
Madan Jampani47c93732014-10-06 20:46:08 -07001227 }
1228 }
1229
Madan Jampani25322532014-10-08 11:20:38 -07001230 private class InternalDeviceOfflineEventListener implements ClusterMessageHandler {
1231 @Override
1232 public void handle(ClusterMessage message) {
1233
1234 log.info("Received device offline event from peer: {}", message.sender());
1235 InternalDeviceOfflineEvent event = (InternalDeviceOfflineEvent) SERIALIZER.decode(message.payload());
1236
1237 DeviceId deviceId = event.deviceId();
1238 Timestamp timestamp = event.timestamp();
1239
Madan Jampani255a58b2014-10-09 12:08:20 -07001240 notifyDelegateIfNotNull(markOfflineInternal(deviceId, timestamp));
Madan Jampani25322532014-10-08 11:20:38 -07001241 }
1242 }
1243
Madan Jampani3fc72ed2014-10-08 12:50:27 -07001244 private class InternalDeviceRemovedEventListener implements ClusterMessageHandler {
1245 @Override
1246 public void handle(ClusterMessage message) {
1247
1248 log.info("Received device removed event from peer: {}", message.sender());
1249 InternalDeviceRemovedEvent event = (InternalDeviceRemovedEvent) SERIALIZER.decode(message.payload());
1250
1251 DeviceId deviceId = event.deviceId();
1252 Timestamp timestamp = event.timestamp();
1253
Madan Jampani255a58b2014-10-09 12:08:20 -07001254 notifyDelegateIfNotNull(removeDeviceInternal(deviceId, timestamp));
Madan Jampani3fc72ed2014-10-08 12:50:27 -07001255 }
1256 }
1257
Madan Jampani47c93732014-10-06 20:46:08 -07001258 private class InternalPortEventListener implements ClusterMessageHandler {
1259 @Override
1260 public void handle(ClusterMessage message) {
1261
1262 log.info("Received port update event from peer: {}", message.sender());
Madan Jampani53e44e62014-10-07 12:39:51 -07001263 InternalPortEvent event = (InternalPortEvent) SERIALIZER.decode(message.payload());
Madan Jampani47c93732014-10-06 20:46:08 -07001264
1265 ProviderId providerId = event.providerId();
1266 DeviceId deviceId = event.deviceId();
1267 Timestamped<List<PortDescription>> portDescriptions = event.portDescriptions();
1268
Madan Jampani255a58b2014-10-09 12:08:20 -07001269 notifyDelegate(updatePortsInternal(providerId, deviceId, portDescriptions));
Madan Jampani47c93732014-10-06 20:46:08 -07001270 }
1271 }
1272
1273 private class InternalPortStatusEventListener implements ClusterMessageHandler {
1274 @Override
1275 public void handle(ClusterMessage message) {
1276
1277 log.info("Received port status update event from peer: {}", message.sender());
Madan Jampani53e44e62014-10-07 12:39:51 -07001278 InternalPortStatusEvent event = (InternalPortStatusEvent) SERIALIZER.decode(message.payload());
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -07001279 log.info("{}", event);
Madan Jampani47c93732014-10-06 20:46:08 -07001280
1281 ProviderId providerId = event.providerId();
1282 DeviceId deviceId = event.deviceId();
1283 Timestamped<PortDescription> portDescription = event.portDescription();
1284
Madan Jampani255a58b2014-10-09 12:08:20 -07001285 notifyDelegateIfNotNull(updatePortStatusInternal(providerId, deviceId, portDescription));
Madan Jampani47c93732014-10-06 20:46:08 -07001286 }
1287 }
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -07001288
1289 private final class InternalDeviceAdvertisementListener
1290 implements ClusterMessageHandler {
1291
1292 @Override
1293 public void handle(ClusterMessage message) {
Yuta HIGUCHI9a0a1d12014-10-13 22:38:02 -07001294 log.debug("Received Device Anti-Entropy advertisement from peer: {}", message.sender());
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -07001295 DeviceAntiEntropyAdvertisement advertisement = SERIALIZER.decode(message.payload());
1296 handleAdvertisement(advertisement);
1297 }
1298 }
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -07001299}