Jonathan Hart | f8cd052 | 2016-10-25 07:09:55 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present 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 | */ |
| 16 | |
Jonathan Hart | f4bd048 | 2017-01-27 15:11:18 -0800 | [diff] [blame] | 17 | package org.onosproject.routing; |
Jonathan Hart | f8cd052 | 2016-10-25 07:09:55 -0700 | [diff] [blame] | 18 | |
| 19 | import org.onosproject.net.DeviceId; |
| 20 | import org.onosproject.net.device.DeviceEvent; |
| 21 | import org.onosproject.net.device.DeviceListener; |
| 22 | import org.onosproject.net.device.DeviceService; |
| 23 | |
| 24 | import java.util.Map; |
Charles Chan | c6d227e | 2017-02-28 15:15:17 -0800 | [diff] [blame] | 25 | import java.util.Optional; |
Jonathan Hart | f8cd052 | 2016-10-25 07:09:55 -0700 | [diff] [blame] | 26 | import java.util.concurrent.ConcurrentHashMap; |
| 27 | |
Jonathan Hart | 249b4cf | 2017-02-03 18:02:58 -0800 | [diff] [blame] | 28 | import static com.google.common.base.Preconditions.checkNotNull; |
| 29 | |
Jonathan Hart | f8cd052 | 2016-10-25 07:09:55 -0700 | [diff] [blame] | 30 | /** |
| 31 | * Provides a means of asynchronously waiting on devices. |
| 32 | */ |
| 33 | public final class AsyncDeviceFetcher { |
| 34 | |
| 35 | private DeviceService deviceService; |
| 36 | |
| 37 | private DeviceListener listener = new InternalDeviceListener(); |
| 38 | |
Charles Chan | c6d227e | 2017-02-28 15:15:17 -0800 | [diff] [blame] | 39 | private Map<DeviceId, Runnable> onConnect = new ConcurrentHashMap<>(); |
| 40 | private Map<DeviceId, Runnable> onDisconnect = new ConcurrentHashMap<>(); |
Jonathan Hart | f8cd052 | 2016-10-25 07:09:55 -0700 | [diff] [blame] | 41 | |
| 42 | private AsyncDeviceFetcher(DeviceService deviceService) { |
Jonathan Hart | 249b4cf | 2017-02-03 18:02:58 -0800 | [diff] [blame] | 43 | this.deviceService = checkNotNull(deviceService); |
Jonathan Hart | f8cd052 | 2016-10-25 07:09:55 -0700 | [diff] [blame] | 44 | deviceService.addListener(listener); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Shuts down. |
| 49 | */ |
| 50 | public void shutdown() { |
| 51 | deviceService.removeListener(listener); |
Charles Chan | c6d227e | 2017-02-28 15:15:17 -0800 | [diff] [blame] | 52 | onConnect.clear(); |
| 53 | onDisconnect.clear(); |
Jonathan Hart | f8cd052 | 2016-10-25 07:09:55 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | /** |
Charles Chan | c6d227e | 2017-02-28 15:15:17 -0800 | [diff] [blame] | 57 | * Executes provided callback when given device connects/disconnects. |
| 58 | * @param deviceId device ID |
| 59 | * @param onConnect callback that will be executed immediately if the device |
| 60 | * is currently online, or when the device becomes online |
| 61 | * @param onDisconnect callback that will be executed when the device becomes offline |
Jonathan Hart | f8cd052 | 2016-10-25 07:09:55 -0700 | [diff] [blame] | 62 | */ |
Charles Chan | c6d227e | 2017-02-28 15:15:17 -0800 | [diff] [blame] | 63 | void registerCallback(DeviceId deviceId, Runnable onConnect, Runnable onDisconnect) { |
| 64 | if (onConnect != null) { |
Jonathan Hart | f8cd052 | 2016-10-25 07:09:55 -0700 | [diff] [blame] | 65 | if (deviceService.isAvailable(deviceId)) { |
Charles Chan | c6d227e | 2017-02-28 15:15:17 -0800 | [diff] [blame] | 66 | onConnect.run(); |
Jonathan Hart | f8cd052 | 2016-10-25 07:09:55 -0700 | [diff] [blame] | 67 | } |
Charles Chan | c6d227e | 2017-02-28 15:15:17 -0800 | [diff] [blame] | 68 | this.onConnect.put(deviceId, onConnect); |
| 69 | } |
| 70 | if (onDisconnect != null) { |
| 71 | this.onDisconnect.put(deviceId, onDisconnect); |
| 72 | } |
Jonathan Hart | f8cd052 | 2016-10-25 07:09:55 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Creates a device fetcher based on the device service. |
| 77 | * |
| 78 | * @param deviceService device service |
| 79 | * @return device fetcher |
| 80 | */ |
| 81 | public static AsyncDeviceFetcher create(DeviceService deviceService) { |
| 82 | return new AsyncDeviceFetcher(deviceService); |
| 83 | } |
| 84 | |
| 85 | private class InternalDeviceListener implements DeviceListener { |
| 86 | @Override |
| 87 | public void event(DeviceEvent event) { |
| 88 | switch (event.type()) { |
Charles Chan | c6d227e | 2017-02-28 15:15:17 -0800 | [diff] [blame] | 89 | case DEVICE_ADDED: |
| 90 | case DEVICE_AVAILABILITY_CHANGED: |
Jonathan Hart | f8cd052 | 2016-10-25 07:09:55 -0700 | [diff] [blame] | 91 | DeviceId deviceId = event.subject().id(); |
Charles Chan | c6d227e | 2017-02-28 15:15:17 -0800 | [diff] [blame] | 92 | if (deviceService.isAvailable(deviceId)) { |
| 93 | Optional.ofNullable(onConnect.get(deviceId)).ifPresent(Runnable::run); |
| 94 | } else { |
| 95 | Optional.ofNullable(onDisconnect.get(deviceId)).ifPresent(Runnable::run); |
Jonathan Hart | f8cd052 | 2016-10-25 07:09:55 -0700 | [diff] [blame] | 96 | } |
Charles Chan | c6d227e | 2017-02-28 15:15:17 -0800 | [diff] [blame] | 97 | break; |
| 98 | case DEVICE_UPDATED: |
| 99 | case DEVICE_REMOVED: |
| 100 | case DEVICE_SUSPENDED: |
| 101 | case PORT_ADDED: |
| 102 | case PORT_UPDATED: |
| 103 | case PORT_REMOVED: |
| 104 | default: |
| 105 | break; |
Jonathan Hart | f8cd052 | 2016-10-25 07:09:55 -0700 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | } |
| 109 | } |