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; |
| 25 | import java.util.concurrent.CompletableFuture; |
| 26 | import java.util.concurrent.ConcurrentHashMap; |
| 27 | |
| 28 | /** |
| 29 | * Provides a means of asynchronously waiting on devices. |
| 30 | */ |
| 31 | public final class AsyncDeviceFetcher { |
| 32 | |
| 33 | private DeviceService deviceService; |
| 34 | |
| 35 | private DeviceListener listener = new InternalDeviceListener(); |
| 36 | |
| 37 | private Map<DeviceId, CompletableFuture<DeviceId>> devices = new ConcurrentHashMap(); |
| 38 | |
| 39 | private AsyncDeviceFetcher(DeviceService deviceService) { |
| 40 | this.deviceService = deviceService; |
| 41 | deviceService.addListener(listener); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Shuts down. |
| 46 | */ |
| 47 | public void shutdown() { |
| 48 | deviceService.removeListener(listener); |
| 49 | devices.clear(); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Returns a completable future that completes when the device is available |
| 54 | * for the first time. |
| 55 | * |
| 56 | * @param deviceId ID of the device |
| 57 | * @return completable future |
| 58 | */ |
| 59 | public CompletableFuture<DeviceId> getDevice(DeviceId deviceId) { |
| 60 | CompletableFuture<DeviceId> future = new CompletableFuture<>(); |
| 61 | return devices.computeIfAbsent(deviceId, deviceId1 -> { |
| 62 | if (deviceService.isAvailable(deviceId)) { |
| 63 | future.complete(deviceId); |
| 64 | } |
| 65 | return future; |
| 66 | }); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Creates a device fetcher based on the device service. |
| 71 | * |
| 72 | * @param deviceService device service |
| 73 | * @return device fetcher |
| 74 | */ |
| 75 | public static AsyncDeviceFetcher create(DeviceService deviceService) { |
| 76 | return new AsyncDeviceFetcher(deviceService); |
| 77 | } |
| 78 | |
| 79 | private class InternalDeviceListener implements DeviceListener { |
| 80 | @Override |
| 81 | public void event(DeviceEvent event) { |
| 82 | switch (event.type()) { |
| 83 | case DEVICE_ADDED: |
| 84 | case DEVICE_AVAILABILITY_CHANGED: |
| 85 | if (deviceService.isAvailable(event.subject().id())) { |
| 86 | DeviceId deviceId = event.subject().id(); |
| 87 | CompletableFuture<DeviceId> future = devices.get(deviceId); |
| 88 | if (future != null) { |
| 89 | future.complete(deviceId); |
| 90 | } |
| 91 | } |
| 92 | break; |
| 93 | case DEVICE_UPDATED: |
| 94 | case DEVICE_REMOVED: |
| 95 | case DEVICE_SUSPENDED: |
| 96 | case PORT_ADDED: |
| 97 | case PORT_UPDATED: |
| 98 | case PORT_REMOVED: |
| 99 | default: |
| 100 | break; |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | } |