Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 Open Networking Laboratory |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 15 | */ |
Jonathan Hart | 9bb32ab | 2015-05-05 18:17:31 -0700 | [diff] [blame] | 16 | package org.onosproject.rest.resources; |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
Ayaka Koshibe | 74b5527 | 2015-05-28 15:16:04 -0700 | [diff] [blame] | 19 | import com.google.common.base.Strings; |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 20 | import com.google.common.collect.Lists; |
Ayaka Koshibe | 74b5527 | 2015-05-28 15:16:04 -0700 | [diff] [blame] | 21 | import com.google.common.collect.Maps; |
Ayaka Koshibe | ae54173 | 2015-05-19 13:37:27 -0700 | [diff] [blame] | 22 | |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 23 | import org.onlab.packet.ChassisId; |
| 24 | import org.onlab.packet.IpAddress; |
| 25 | import org.onlab.packet.MacAddress; |
| 26 | import org.onlab.packet.VlanId; |
Ayaka Koshibe | ae54173 | 2015-05-19 13:37:27 -0700 | [diff] [blame] | 27 | import org.onlab.util.Frequency; |
Sho SHIMIZU | 70e76d8 | 2016-01-05 16:26:26 -0800 | [diff] [blame] | 28 | import org.onlab.util.Spectrum; |
Ayaka Koshibe | 74b5527 | 2015-05-28 15:16:04 -0700 | [diff] [blame] | 29 | import org.onosproject.net.AnnotationKeys; |
Ayaka Koshibe | ae54173 | 2015-05-19 13:37:27 -0700 | [diff] [blame] | 30 | import org.onosproject.net.ChannelSpacing; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 31 | import org.onosproject.net.ConnectPoint; |
| 32 | import org.onosproject.net.DefaultAnnotations; |
| 33 | import org.onosproject.net.Device; |
| 34 | import org.onosproject.net.DeviceId; |
Ayaka Koshibe | ae54173 | 2015-05-19 13:37:27 -0700 | [diff] [blame] | 35 | import org.onosproject.net.GridType; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 36 | import org.onosproject.net.Host; |
| 37 | import org.onosproject.net.HostId; |
| 38 | import org.onosproject.net.HostLocation; |
| 39 | import org.onosproject.net.Link; |
| 40 | import org.onosproject.net.MastershipRole; |
Ayaka Koshibe | ae54173 | 2015-05-19 13:37:27 -0700 | [diff] [blame] | 41 | import org.onosproject.net.OchPort; |
| 42 | import org.onosproject.net.OchSignal; |
| 43 | import org.onosproject.net.OduCltPort; |
| 44 | import org.onosproject.net.OduSignalType; |
| 45 | import org.onosproject.net.OmsPort; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 46 | import org.onosproject.net.Port; |
Ayaka Koshibe | 74b5527 | 2015-05-28 15:16:04 -0700 | [diff] [blame] | 47 | import org.onosproject.net.PortNumber; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 48 | import org.onosproject.net.SparseAnnotations; |
| 49 | import org.onosproject.net.device.DefaultDeviceDescription; |
| 50 | import org.onosproject.net.device.DefaultPortDescription; |
| 51 | import org.onosproject.net.device.DeviceDescription; |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 52 | import org.onosproject.net.device.DeviceEvent; |
| 53 | import org.onosproject.net.device.DeviceListener; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 54 | import org.onosproject.net.device.DeviceProvider; |
| 55 | import org.onosproject.net.device.DeviceProviderRegistry; |
| 56 | import org.onosproject.net.device.DeviceProviderService; |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 57 | import org.onosproject.net.device.DeviceService; |
Ayaka Koshibe | ae54173 | 2015-05-19 13:37:27 -0700 | [diff] [blame] | 58 | import org.onosproject.net.device.OchPortDescription; |
| 59 | import org.onosproject.net.device.OduCltPortDescription; |
| 60 | import org.onosproject.net.device.OmsPortDescription; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 61 | import org.onosproject.net.device.PortDescription; |
| 62 | import org.onosproject.net.host.DefaultHostDescription; |
| 63 | import org.onosproject.net.host.HostProvider; |
| 64 | import org.onosproject.net.host.HostProviderRegistry; |
| 65 | import org.onosproject.net.host.HostProviderService; |
| 66 | import org.onosproject.net.link.DefaultLinkDescription; |
| 67 | import org.onosproject.net.link.LinkProvider; |
| 68 | import org.onosproject.net.link.LinkProviderRegistry; |
| 69 | import org.onosproject.net.link.LinkProviderService; |
| 70 | import org.onosproject.net.provider.ProviderId; |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 71 | import org.slf4j.Logger; |
| 72 | import org.slf4j.LoggerFactory; |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 73 | |
| 74 | import java.net.URI; |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 75 | import java.util.ArrayList; |
Thomas Vachuska | ece59ee | 2014-11-19 19:06:11 -0800 | [diff] [blame] | 76 | import java.util.HashSet; |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 77 | import java.util.Iterator; |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 78 | import java.util.List; |
Ayaka Koshibe | 74b5527 | 2015-05-28 15:16:04 -0700 | [diff] [blame] | 79 | import java.util.Map; |
Thomas Vachuska | ece59ee | 2014-11-19 19:06:11 -0800 | [diff] [blame] | 80 | import java.util.Set; |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 81 | import java.util.concurrent.CountDownLatch; |
| 82 | import java.util.concurrent.TimeUnit; |
| 83 | import java.util.stream.Collectors; |
Sho SHIMIZU | bb825d7 | 2015-06-11 10:56:32 -0700 | [diff] [blame] | 84 | import java.util.stream.Stream; |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 85 | |
| 86 | import static com.google.common.base.Preconditions.checkNotNull; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 87 | import static org.onosproject.net.DeviceId.deviceId; |
| 88 | import static org.onosproject.net.PortNumber.portNumber; |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 89 | import static org.onosproject.net.device.DeviceEvent.Type.DEVICE_ADDED; |
| 90 | import static org.onosproject.net.device.DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED; |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 91 | |
| 92 | /** |
| 93 | * Provider of devices and links parsed from a JSON configuration structure. |
Thomas Vachuska | f9879c5 | 2016-01-15 11:12:19 -0800 | [diff] [blame] | 94 | * |
| 95 | * @deprecated in 1.5.0 (Falcon) |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 96 | */ |
Thomas Vachuska | f9879c5 | 2016-01-15 11:12:19 -0800 | [diff] [blame] | 97 | @Deprecated |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 98 | class ConfigProvider implements DeviceProvider, LinkProvider, HostProvider { |
| 99 | |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 100 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 101 | |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 102 | private static final ProviderId PID = |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 103 | new ProviderId("cfg", "org.onosproject.rest", true); |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 104 | |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 105 | private static final String UNKNOWN = "unknown"; |
| 106 | |
Ayaka Koshibe | ae54173 | 2015-05-19 13:37:27 -0700 | [diff] [blame] | 107 | // C-band has 4.4 THz (4,400 GHz) total bandwidth |
Sho SHIMIZU | 00762ee | 2016-01-05 16:32:24 -0800 | [diff] [blame] | 108 | private static final Frequency TOTAL = Frequency.ofGHz(4_400); |
Ayaka Koshibe | ae54173 | 2015-05-19 13:37:27 -0700 | [diff] [blame] | 109 | |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 110 | private CountDownLatch deviceLatch; |
| 111 | |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 112 | private final JsonNode cfg; |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 113 | private final DeviceService deviceService; |
| 114 | |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 115 | private final DeviceProviderRegistry deviceProviderRegistry; |
| 116 | private final LinkProviderRegistry linkProviderRegistry; |
| 117 | private final HostProviderRegistry hostProviderRegistry; |
| 118 | |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 119 | private DeviceProviderService deviceProviderService; |
| 120 | private LinkProviderService linkProviderService; |
| 121 | private HostProviderService hostProviderService; |
| 122 | |
| 123 | private DeviceListener deviceEventCounter = new DeviceEventCounter(); |
| 124 | private List<ConnectPoint> connectPoints = Lists.newArrayList(); |
Ayaka Koshibe | 74b5527 | 2015-05-28 15:16:04 -0700 | [diff] [blame] | 125 | private Map<ConnectPoint, PortDescription> descriptions = Maps.newHashMap(); |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 126 | |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 127 | /** |
| 128 | * Creates a new configuration provider. |
| 129 | * |
| 130 | * @param cfg JSON configuration |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 131 | * @param deviceService device service |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 132 | * @param deviceProviderRegistry device provider registry |
| 133 | * @param linkProviderRegistry link provider registry |
| 134 | * @param hostProviderRegistry host provider registry |
| 135 | */ |
| 136 | ConfigProvider(JsonNode cfg, |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 137 | DeviceService deviceService, |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 138 | DeviceProviderRegistry deviceProviderRegistry, |
| 139 | LinkProviderRegistry linkProviderRegistry, |
| 140 | HostProviderRegistry hostProviderRegistry) { |
| 141 | this.cfg = checkNotNull(cfg, "Configuration cannot be null"); |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 142 | this.deviceService = checkNotNull(deviceService, "Device service cannot be null"); |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 143 | this.deviceProviderRegistry = checkNotNull(deviceProviderRegistry, "Device provider registry cannot be null"); |
| 144 | this.linkProviderRegistry = checkNotNull(linkProviderRegistry, "Link provider registry cannot be null"); |
| 145 | this.hostProviderRegistry = checkNotNull(hostProviderRegistry, "Host provider registry cannot be null"); |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Parses the given JSON and provides links as configured. |
| 150 | */ |
| 151 | void parse() { |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 152 | try { |
| 153 | register(); |
| 154 | parseDevices(); |
| 155 | parseLinks(); |
| 156 | parseHosts(); |
| 157 | addMissingPorts(); |
| 158 | } finally { |
| 159 | unregister(); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | private void register() { |
| 164 | deviceProviderService = deviceProviderRegistry.register(this); |
| 165 | linkProviderService = linkProviderRegistry.register(this); |
| 166 | hostProviderService = hostProviderRegistry.register(this); |
| 167 | } |
| 168 | |
| 169 | private void unregister() { |
| 170 | deviceProviderRegistry.unregister(this); |
| 171 | linkProviderRegistry.unregister(this); |
| 172 | hostProviderRegistry.unregister(this); |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | // Parses the given JSON and provides devices. |
| 176 | private void parseDevices() { |
| 177 | try { |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 178 | JsonNode nodes = cfg.get("devices"); |
| 179 | if (nodes != null) { |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 180 | prepareForDeviceEvents(nodes.size()); |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 181 | for (JsonNode node : nodes) { |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 182 | parseDevice(node); |
Thomas Vachuska | 3b84c86 | 2015-04-28 12:09:48 -0700 | [diff] [blame] | 183 | |
| 184 | // FIXME: hack to make sure device attributes take |
| 185 | // This will be fixed when GossipDeviceStore uses ECM |
| 186 | parseDevice(node); |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | } finally { |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 190 | waitForDeviceEvents(); |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | |
| 194 | // Parses the given node with device data and supplies the device. |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 195 | private void parseDevice(JsonNode node) { |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 196 | URI uri = URI.create(get(node, "uri")); |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 197 | Device.Type type = Device.Type.valueOf(get(node, "type", "SWITCH")); |
| 198 | String mfr = get(node, "mfr", UNKNOWN); |
| 199 | String hw = get(node, "hw", UNKNOWN); |
| 200 | String sw = get(node, "sw", UNKNOWN); |
| 201 | String serial = get(node, "serial", UNKNOWN); |
| 202 | ChassisId cid = new ChassisId(get(node, "mac", "000000000000")); |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 203 | SparseAnnotations annotations = annotations(node.get("annotations")); |
| 204 | |
| 205 | DeviceDescription desc = |
| 206 | new DefaultDeviceDescription(uri, type, mfr, hw, sw, serial, |
| 207 | cid, annotations); |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 208 | DeviceId deviceId = deviceId(uri); |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 209 | deviceProviderService.deviceConnected(deviceId, desc); |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 210 | |
| 211 | JsonNode ports = node.get("ports"); |
| 212 | if (ports != null) { |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 213 | parsePorts(deviceId, ports); |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 214 | } |
| 215 | } |
| 216 | |
| 217 | // Parses the given node with list of device ports. |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 218 | private void parsePorts(DeviceId deviceId, JsonNode nodes) { |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 219 | List<PortDescription> ports = new ArrayList<>(); |
| 220 | for (JsonNode node : nodes) { |
Marc De Leenheer | 723f553 | 2015-06-03 20:16:17 -0700 | [diff] [blame] | 221 | ports.add(parsePort(deviceId, node)); |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 222 | } |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 223 | deviceProviderService.updatePorts(deviceId, ports); |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | // Parses the given node with port information. |
Marc De Leenheer | 723f553 | 2015-06-03 20:16:17 -0700 | [diff] [blame] | 227 | private PortDescription parsePort(DeviceId deviceId, JsonNode node) { |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 228 | Port.Type type = Port.Type.valueOf(node.path("type").asText("COPPER")); |
Marc De Leenheer | 723f553 | 2015-06-03 20:16:17 -0700 | [diff] [blame] | 229 | // TL1-based ports have a name |
| 230 | PortNumber port = null; |
| 231 | if (node.has("name")) { |
| 232 | for (Port p : deviceService.getPorts(deviceId)) { |
| 233 | if (p.number().name().equals(node.get("name").asText())) { |
| 234 | port = p.number(); |
| 235 | break; |
| 236 | } |
| 237 | } |
| 238 | } else { |
| 239 | port = portNumber(node.path("port").asLong(0)); |
| 240 | } |
| 241 | |
Marc De Leenheer | c973308 | 2015-06-04 12:22:38 -0700 | [diff] [blame] | 242 | if (port == null) { |
| 243 | log.error("Cannot find port given in node {}", node); |
| 244 | return null; |
| 245 | } |
| 246 | |
Ayaka Koshibe | 74b5527 | 2015-05-28 15:16:04 -0700 | [diff] [blame] | 247 | String portName = Strings.emptyToNull(port.name()); |
| 248 | SparseAnnotations annotations = null; |
| 249 | if (portName != null) { |
| 250 | annotations = DefaultAnnotations.builder() |
| 251 | .set(AnnotationKeys.PORT_NAME, portName).build(); |
| 252 | } |
Ayaka Koshibe | ae54173 | 2015-05-19 13:37:27 -0700 | [diff] [blame] | 253 | switch (type) { |
| 254 | case COPPER: |
Ayaka Koshibe | 74b5527 | 2015-05-28 15:16:04 -0700 | [diff] [blame] | 255 | return new DefaultPortDescription(port, node.path("enabled").asBoolean(true), |
| 256 | type, node.path("speed").asLong(1_000), |
| 257 | annotations); |
Ayaka Koshibe | ae54173 | 2015-05-19 13:37:27 -0700 | [diff] [blame] | 258 | case FIBER: |
| 259 | // Currently, assume OMS when FIBER. Provide sane defaults. |
Marc De Leenheer | fc913dd | 2015-07-30 16:04:55 -0700 | [diff] [blame] | 260 | annotations = annotations(node.get("annotations")); |
Ayaka Koshibe | 74b5527 | 2015-05-28 15:16:04 -0700 | [diff] [blame] | 261 | return new OmsPortDescription(port, node.path("enabled").asBoolean(true), |
Sho SHIMIZU | 70e76d8 | 2016-01-05 16:26:26 -0800 | [diff] [blame] | 262 | Spectrum.CENTER_FREQUENCY, Spectrum.CENTER_FREQUENCY.add(TOTAL), |
Ayaka Koshibe | 74b5527 | 2015-05-28 15:16:04 -0700 | [diff] [blame] | 263 | Frequency.ofGHz(100), annotations); |
Marc De Leenheer | 723f553 | 2015-06-03 20:16:17 -0700 | [diff] [blame] | 264 | case ODUCLT: |
| 265 | annotations = annotations(node.get("annotations")); |
| 266 | OduCltPort oduCltPort = (OduCltPort) deviceService.getPort(deviceId, port); |
| 267 | return new OduCltPortDescription(port, node.path("enabled").asBoolean(true), |
| 268 | oduCltPort.signalType(), annotations); |
| 269 | case OCH: |
| 270 | annotations = annotations(node.get("annotations")); |
| 271 | OchPort ochPort = (OchPort) deviceService.getPort(deviceId, port); |
| 272 | return new OchPortDescription(port, node.path("enabled").asBoolean(true), |
| 273 | ochPort.signalType(), ochPort.isTunable(), |
| 274 | ochPort.lambda(), annotations); |
| 275 | case OMS: |
| 276 | annotations = annotations(node.get("annotations")); |
| 277 | OmsPort omsPort = (OmsPort) deviceService.getPort(deviceId, port); |
| 278 | return new OmsPortDescription(port, node.path("enabled").asBoolean(true), |
| 279 | omsPort.minFrequency(), omsPort.maxFrequency(), omsPort.grid(), annotations); |
Ayaka Koshibe | ae54173 | 2015-05-19 13:37:27 -0700 | [diff] [blame] | 280 | default: |
| 281 | log.warn("{}: Unsupported Port Type"); |
| 282 | } |
Ayaka Koshibe | 74b5527 | 2015-05-28 15:16:04 -0700 | [diff] [blame] | 283 | return new DefaultPortDescription(port, node.path("enabled").asBoolean(true), |
| 284 | type, node.path("speed").asLong(1_000), |
| 285 | annotations); |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | // Parses the given JSON and provides links as configured. |
| 289 | private void parseLinks() { |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 290 | JsonNode nodes = cfg.get("links"); |
| 291 | if (nodes != null) { |
| 292 | for (JsonNode node : nodes) { |
| 293 | parseLink(node, false); |
| 294 | if (!node.has("halfplex")) { |
| 295 | parseLink(node, true); |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 296 | } |
| 297 | } |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 298 | } |
| 299 | } |
| 300 | |
| 301 | // Parses the given node with link data and supplies the link. |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 302 | private void parseLink(JsonNode node, boolean reverse) { |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 303 | ConnectPoint src = connectPoint(get(node, "src")); |
| 304 | ConnectPoint dst = connectPoint(get(node, "dst")); |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 305 | Link.Type type = Link.Type.valueOf(get(node, "type", "DIRECT")); |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 306 | SparseAnnotations annotations = annotations(node.get("annotations")); |
Ayaka Koshibe | ae54173 | 2015-05-19 13:37:27 -0700 | [diff] [blame] | 307 | // take annotations to update optical ports with correct attributes. |
| 308 | updatePorts(src, dst, annotations); |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 309 | DefaultLinkDescription desc = reverse ? |
| 310 | new DefaultLinkDescription(dst, src, type, annotations) : |
| 311 | new DefaultLinkDescription(src, dst, type, annotations); |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 312 | linkProviderService.linkDetected(desc); |
| 313 | |
| 314 | connectPoints.add(src); |
| 315 | connectPoints.add(dst); |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 316 | } |
| 317 | |
Ayaka Koshibe | ae54173 | 2015-05-19 13:37:27 -0700 | [diff] [blame] | 318 | private void updatePorts(ConnectPoint src, ConnectPoint dst, SparseAnnotations annotations) { |
Ayaka Koshibe | ae54173 | 2015-05-19 13:37:27 -0700 | [diff] [blame] | 319 | final String linkType = annotations.value("optical.type"); |
| 320 | if ("cross-connect".equals(linkType)) { |
| 321 | String value = annotations.value("bandwidth").trim(); |
| 322 | try { |
| 323 | double bw = Double.parseDouble(value); |
Ayaka Koshibe | 74b5527 | 2015-05-28 15:16:04 -0700 | [diff] [blame] | 324 | updateOchPort(bw, src, dst); |
Ayaka Koshibe | ae54173 | 2015-05-19 13:37:27 -0700 | [diff] [blame] | 325 | } catch (NumberFormatException e) { |
| 326 | log.warn("Invalid bandwidth ({}), can't configure port(s)", value); |
| 327 | return; |
| 328 | } |
| 329 | } else if ("WDM".equals(linkType)) { |
| 330 | String value = annotations.value("optical.waves").trim(); |
| 331 | try { |
| 332 | int numChls = Integer.parseInt(value); |
Jonathan Hart | d9df7bd | 2015-11-10 17:10:25 -0800 | [diff] [blame] | 333 | updateOmsPorts(numChls, src, dst); |
Ayaka Koshibe | ae54173 | 2015-05-19 13:37:27 -0700 | [diff] [blame] | 334 | } catch (NumberFormatException e) { |
| 335 | log.warn("Invalid channel ({}), can't configure port(s)", value); |
| 336 | return; |
| 337 | } |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | // uses 'bandwidth' annotation to determine the channel spacing. |
Ayaka Koshibe | 74b5527 | 2015-05-28 15:16:04 -0700 | [diff] [blame] | 342 | private void updateOchPort(double bw, ConnectPoint srcCp, ConnectPoint dstCp) { |
| 343 | Device src = deviceService.getDevice(srcCp.deviceId()); |
| 344 | Device dst = deviceService.getDevice(dstCp.deviceId()); |
| 345 | // bandwidth in MHz (assuming Hz - linc is not clear if that or Mb). |
Ayaka Koshibe | ae54173 | 2015-05-19 13:37:27 -0700 | [diff] [blame] | 346 | Frequency spacing = Frequency.ofMHz(bw); |
| 347 | // channel bandwidth is smaller than smallest standard channel spacing. |
| 348 | ChannelSpacing chsp = null; |
| 349 | if (spacing.compareTo(ChannelSpacing.CHL_6P25GHZ.frequency()) <= 0) { |
| 350 | chsp = ChannelSpacing.CHL_6P25GHZ; |
| 351 | } |
| 352 | for (int i = 1; i < ChannelSpacing.values().length; i++) { |
| 353 | Frequency val = ChannelSpacing.values()[i].frequency(); |
| 354 | // pick the next highest or equal channel interval. |
| 355 | if (val.isLessThan(spacing)) { |
| 356 | chsp = ChannelSpacing.values()[i - 1]; |
| 357 | break; |
| 358 | } |
| 359 | } |
| 360 | if (chsp == null) { |
| 361 | log.warn("Invalid channel spacing ({}), can't configure port(s)", spacing); |
| 362 | return; |
| 363 | } |
| 364 | OchSignal signal = new OchSignal(GridType.DWDM, chsp, 1, 1); |
| 365 | if (src.type() == Device.Type.ROADM) { |
Ayaka Koshibe | 74b5527 | 2015-05-28 15:16:04 -0700 | [diff] [blame] | 366 | PortDescription portDesc = new OchPortDescription(srcCp.port(), true, |
Ayaka Koshibe | ae54173 | 2015-05-19 13:37:27 -0700 | [diff] [blame] | 367 | OduSignalType.ODU4, true, signal); |
Ayaka Koshibe | 74b5527 | 2015-05-28 15:16:04 -0700 | [diff] [blame] | 368 | descriptions.put(srcCp, portDesc); |
| 369 | deviceProviderService.portStatusChanged(srcCp.deviceId(), portDesc); |
Ayaka Koshibe | ae54173 | 2015-05-19 13:37:27 -0700 | [diff] [blame] | 370 | } |
| 371 | if (dst.type() == Device.Type.ROADM) { |
Ayaka Koshibe | 74b5527 | 2015-05-28 15:16:04 -0700 | [diff] [blame] | 372 | PortDescription portDesc = new OchPortDescription(dstCp.port(), true, |
Ayaka Koshibe | ae54173 | 2015-05-19 13:37:27 -0700 | [diff] [blame] | 373 | OduSignalType.ODU4, true, signal); |
Ayaka Koshibe | 74b5527 | 2015-05-28 15:16:04 -0700 | [diff] [blame] | 374 | descriptions.put(dstCp, portDesc); |
| 375 | deviceProviderService.portStatusChanged(dstCp.deviceId(), portDesc); |
Ayaka Koshibe | ae54173 | 2015-05-19 13:37:27 -0700 | [diff] [blame] | 376 | } |
| 377 | } |
| 378 | |
Jonathan Hart | d9df7bd | 2015-11-10 17:10:25 -0800 | [diff] [blame] | 379 | private void updateOmsPorts(int numChls, ConnectPoint srcCp, ConnectPoint dstCp) { |
Ayaka Koshibe | 74b5527 | 2015-05-28 15:16:04 -0700 | [diff] [blame] | 380 | // round down to largest slot that allows numChl channels to fit into C band range |
Ayaka Koshibe | ae54173 | 2015-05-19 13:37:27 -0700 | [diff] [blame] | 381 | ChannelSpacing chl = null; |
| 382 | Frequency perChl = TOTAL.floorDivision(numChls); |
| 383 | for (int i = 0; i < ChannelSpacing.values().length; i++) { |
| 384 | Frequency val = ChannelSpacing.values()[i].frequency(); |
| 385 | if (val.isLessThan(perChl)) { |
| 386 | chl = ChannelSpacing.values()[i]; |
| 387 | break; |
| 388 | } |
| 389 | } |
| 390 | if (chl == null) { |
| 391 | chl = ChannelSpacing.CHL_6P25GHZ; |
| 392 | } |
| 393 | |
| 394 | // if true, there was less channels than can be tightly packed. |
Sho SHIMIZU | f6501c0 | 2015-06-10 19:30:24 -0700 | [diff] [blame] | 395 | Frequency grid = chl.frequency(); |
Ayaka Koshibe | ae54173 | 2015-05-19 13:37:27 -0700 | [diff] [blame] | 396 | // say Linc's 1st slot starts at CENTER and goes up from there. |
Sho SHIMIZU | 70e76d8 | 2016-01-05 16:26:26 -0800 | [diff] [blame] | 397 | Frequency min = Spectrum.CENTER_FREQUENCY.add(grid); |
| 398 | Frequency max = Spectrum.CENTER_FREQUENCY.add(grid.multiply(numChls)); |
Ayaka Koshibe | ae54173 | 2015-05-19 13:37:27 -0700 | [diff] [blame] | 399 | |
Ayaka Koshibe | 74b5527 | 2015-05-28 15:16:04 -0700 | [diff] [blame] | 400 | PortDescription srcPortDesc = new OmsPortDescription(srcCp.port(), true, min, max, grid); |
| 401 | PortDescription dstPortDesc = new OmsPortDescription(dstCp.port(), true, min, max, grid); |
| 402 | descriptions.put(srcCp, srcPortDesc); |
| 403 | descriptions.put(dstCp, dstPortDesc); |
| 404 | deviceProviderService.portStatusChanged(srcCp.deviceId(), srcPortDesc); |
| 405 | deviceProviderService.portStatusChanged(dstCp.deviceId(), dstPortDesc); |
Ayaka Koshibe | ae54173 | 2015-05-19 13:37:27 -0700 | [diff] [blame] | 406 | } |
| 407 | |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 408 | // Parses the given JSON and provides hosts as configured. |
| 409 | private void parseHosts() { |
| 410 | try { |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 411 | JsonNode nodes = cfg.get("hosts"); |
| 412 | if (nodes != null) { |
| 413 | for (JsonNode node : nodes) { |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 414 | parseHost(node); |
Thomas Vachuska | 3b84c86 | 2015-04-28 12:09:48 -0700 | [diff] [blame] | 415 | |
| 416 | // FIXME: hack to make sure host attributes take |
| 417 | // This will be fixed when GossipHostStore uses ECM |
| 418 | parseHost(node); |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 419 | } |
| 420 | } |
| 421 | } finally { |
| 422 | hostProviderRegistry.unregister(this); |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | // Parses the given node with host data and supplies the host. |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 427 | private void parseHost(JsonNode node) { |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 428 | MacAddress mac = MacAddress.valueOf(get(node, "mac")); |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 429 | VlanId vlanId = VlanId.vlanId((short) node.get("vlan").asInt(VlanId.UNTAGGED)); |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 430 | HostId hostId = HostId.hostId(mac, vlanId); |
| 431 | SparseAnnotations annotations = annotations(node.get("annotations")); |
| 432 | HostLocation location = new HostLocation(connectPoint(get(node, "location")), 0); |
Thomas Vachuska | ece59ee | 2014-11-19 19:06:11 -0800 | [diff] [blame] | 433 | |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 434 | String[] ipStrings = get(node, "ip", "").split(","); |
Thomas Vachuska | ece59ee | 2014-11-19 19:06:11 -0800 | [diff] [blame] | 435 | Set<IpAddress> ips = new HashSet<>(); |
| 436 | for (String ip : ipStrings) { |
| 437 | ips.add(IpAddress.valueOf(ip.trim())); |
| 438 | } |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 439 | |
| 440 | DefaultHostDescription desc = |
Thomas Vachuska | ece59ee | 2014-11-19 19:06:11 -0800 | [diff] [blame] | 441 | new DefaultHostDescription(mac, vlanId, location, ips, annotations); |
Ray Milkey | dc08344 | 2016-02-22 11:27:57 -0800 | [diff] [blame] | 442 | hostProviderService.hostDetected(hostId, desc, false); |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 443 | |
| 444 | connectPoints.add(location); |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 445 | } |
| 446 | |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 447 | // Adds any missing device ports for configured links and host locations. |
| 448 | private void addMissingPorts() { |
| 449 | deviceService.getDevices().forEach(this::addMissingPorts); |
| 450 | } |
| 451 | |
| 452 | // Adds any missing device ports. |
| 453 | private void addMissingPorts(Device device) { |
Jonathan Hart | eb431c1 | 2015-07-27 10:43:50 -0700 | [diff] [blame] | 454 | try { |
| 455 | List<Port> ports = deviceService.getPorts(device.id()); |
| 456 | Set<ConnectPoint> existing = ports.stream() |
| 457 | .map(p -> new ConnectPoint(device.id(), p.number())) |
| 458 | .collect(Collectors.toSet()); |
| 459 | Set<ConnectPoint> missing = connectPoints.stream() |
| 460 | .filter(cp -> cp.deviceId().equals(device.id())) |
| 461 | .filter(cp -> !existing.contains(cp)) |
| 462 | .collect(Collectors.toSet()); |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 463 | |
Jonathan Hart | eb431c1 | 2015-07-27 10:43:50 -0700 | [diff] [blame] | 464 | if (!missing.isEmpty()) { |
| 465 | List<PortDescription> newPorts = Stream.concat( |
| 466 | ports.stream().map(this::description), |
| 467 | missing.stream().map(this::description) |
| 468 | ).collect(Collectors.toList()); |
| 469 | deviceProviderService.updatePorts(device.id(), newPorts); |
| 470 | } |
| 471 | } catch (IllegalArgumentException e) { |
| 472 | log.warn("Error pushing ports: {}", e.getMessage()); |
| 473 | } |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | // Creates a port description from the specified port. |
| 477 | private PortDescription description(Port p) { |
Ayaka Koshibe | ae54173 | 2015-05-19 13:37:27 -0700 | [diff] [blame] | 478 | switch (p.type()) { |
| 479 | case OMS: |
| 480 | OmsPort op = (OmsPort) p; |
| 481 | return new OmsPortDescription( |
| 482 | op.number(), op.isEnabled(), op.minFrequency(), op.maxFrequency(), op.grid()); |
| 483 | case OCH: |
| 484 | OchPort ochp = (OchPort) p; |
| 485 | return new OchPortDescription( |
| 486 | ochp.number(), ochp.isEnabled(), ochp.signalType(), ochp.isTunable(), ochp.lambda()); |
| 487 | case ODUCLT: |
| 488 | OduCltPort odup = (OduCltPort) p; |
| 489 | return new OduCltPortDescription( |
| 490 | odup.number(), odup.isEnabled(), odup.signalType()); |
| 491 | default: |
| 492 | return new DefaultPortDescription(p.number(), p.isEnabled(), p.type(), p.portSpeed()); |
| 493 | } |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 494 | } |
| 495 | |
Ayaka Koshibe | 74b5527 | 2015-05-28 15:16:04 -0700 | [diff] [blame] | 496 | // Creates a port description from the specified connection point if none created earlier. |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 497 | private PortDescription description(ConnectPoint cp) { |
Ayaka Koshibe | 74b5527 | 2015-05-28 15:16:04 -0700 | [diff] [blame] | 498 | PortDescription saved = descriptions.get(cp); |
| 499 | if (saved != null) { |
| 500 | return saved; |
| 501 | } |
Ayaka Koshibe | ae54173 | 2015-05-19 13:37:27 -0700 | [diff] [blame] | 502 | Port p = deviceService.getPort(cp.deviceId(), cp.port()); |
| 503 | if (p == null) { |
| 504 | return new DefaultPortDescription(cp.port(), true); |
| 505 | } |
| 506 | return description(p); |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 507 | } |
| 508 | |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 509 | // Produces set of annotations from the given JSON node. |
| 510 | private SparseAnnotations annotations(JsonNode node) { |
| 511 | if (node == null) { |
Sho SHIMIZU | a3d67cd | 2015-06-04 11:46:48 -0700 | [diff] [blame] | 512 | return DefaultAnnotations.EMPTY; |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | DefaultAnnotations.Builder builder = DefaultAnnotations.builder(); |
| 516 | Iterator<String> it = node.fieldNames(); |
| 517 | while (it.hasNext()) { |
| 518 | String k = it.next(); |
| 519 | builder.set(k, node.get(k).asText()); |
| 520 | } |
| 521 | return builder.build(); |
| 522 | } |
| 523 | |
| 524 | // Produces a connection point from the specified uri/port text. |
| 525 | private ConnectPoint connectPoint(String text) { |
| 526 | int i = text.lastIndexOf("/"); |
Ayaka Koshibe | c10d151 | 2015-06-04 15:17:58 -0700 | [diff] [blame] | 527 | String portName = text.substring(i + 1); |
| 528 | DeviceId deviceId = deviceId(text.substring(0, i)); |
| 529 | |
Ayaka Koshibe | c10d151 | 2015-06-04 15:17:58 -0700 | [diff] [blame] | 530 | for (Port port : deviceService.getPorts(deviceId)) { |
| 531 | PortNumber pn = port.number(); |
| 532 | if (pn.name().equals(portName)) { |
| 533 | return new ConnectPoint(deviceId, pn); |
| 534 | } |
| 535 | } |
Sho SHIMIZU | ccf40c7 | 2015-06-11 16:55:20 -0700 | [diff] [blame] | 536 | |
| 537 | long portNum; |
| 538 | try { |
| 539 | portNum = Long.parseLong(portName); |
| 540 | } catch (NumberFormatException e) { |
| 541 | portNum = 0; |
| 542 | } |
| 543 | |
Ayaka Koshibe | c10d151 | 2015-06-04 15:17:58 -0700 | [diff] [blame] | 544 | return new ConnectPoint(deviceId, portNumber(portNum, portName)); |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | // Returns string form of the named property in the given JSON object. |
| 548 | private String get(JsonNode node, String name) { |
| 549 | return node.path(name).asText(); |
| 550 | } |
| 551 | |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 552 | // Returns string form of the named property in the given JSON object. |
| 553 | private String get(JsonNode node, String name, String defaultValue) { |
| 554 | return node.path(name).asText(defaultValue); |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | @Override |
Yuta HIGUCHI | 5481532 | 2014-10-31 23:17:08 -0700 | [diff] [blame] | 558 | public void roleChanged(DeviceId device, MastershipRole newRole) { |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 559 | deviceProviderService.receivedRoleReply(device, newRole, newRole); |
| 560 | } |
| 561 | |
| 562 | @Override |
| 563 | public void triggerProbe(DeviceId deviceId) { |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | @Override |
| 567 | public void triggerProbe(Host host) { |
| 568 | } |
| 569 | |
| 570 | @Override |
| 571 | public ProviderId id() { |
| 572 | return PID; |
| 573 | } |
Ayaka Koshibe | e60d452 | 2014-10-28 15:07:00 -0700 | [diff] [blame] | 574 | |
| 575 | @Override |
Yuta HIGUCHI | 5481532 | 2014-10-31 23:17:08 -0700 | [diff] [blame] | 576 | public boolean isReachable(DeviceId device) { |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 577 | return true; |
Ayaka Koshibe | e60d452 | 2014-10-28 15:07:00 -0700 | [diff] [blame] | 578 | } |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 579 | |
| 580 | /** |
| 581 | * Prepares to count device added/available/removed events. |
| 582 | * |
| 583 | * @param count number of events to count |
| 584 | */ |
| 585 | protected void prepareForDeviceEvents(int count) { |
| 586 | deviceLatch = new CountDownLatch(count); |
| 587 | deviceService.addListener(deviceEventCounter); |
| 588 | } |
| 589 | |
| 590 | /** |
| 591 | * Waits for all expected device added/available/removed events. |
| 592 | */ |
| 593 | protected void waitForDeviceEvents() { |
| 594 | try { |
| 595 | deviceLatch.await(2, TimeUnit.SECONDS); |
| 596 | } catch (InterruptedException e) { |
| 597 | log.warn("Device events did not arrive in time"); |
| 598 | } |
| 599 | deviceService.removeListener(deviceEventCounter); |
| 600 | } |
| 601 | |
| 602 | // Counts down number of device added/available/removed events. |
| 603 | private class DeviceEventCounter implements DeviceListener { |
| 604 | @Override |
| 605 | public void event(DeviceEvent event) { |
| 606 | DeviceEvent.Type type = event.type(); |
| 607 | if (type == DEVICE_ADDED || type == DEVICE_AVAILABILITY_CHANGED) { |
| 608 | deviceLatch.countDown(); |
| 609 | } |
| 610 | } |
| 611 | } |
| 612 | |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 613 | } |