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; |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 19 | import com.google.common.collect.Lists; |
| 20 | import org.onlab.packet.ChassisId; |
| 21 | import org.onlab.packet.IpAddress; |
| 22 | import org.onlab.packet.MacAddress; |
| 23 | import org.onlab.packet.VlanId; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 24 | import org.onosproject.net.ConnectPoint; |
| 25 | import org.onosproject.net.DefaultAnnotations; |
| 26 | import org.onosproject.net.Device; |
| 27 | import org.onosproject.net.DeviceId; |
| 28 | import org.onosproject.net.Host; |
| 29 | import org.onosproject.net.HostId; |
| 30 | import org.onosproject.net.HostLocation; |
| 31 | import org.onosproject.net.Link; |
| 32 | import org.onosproject.net.MastershipRole; |
| 33 | import org.onosproject.net.Port; |
| 34 | import org.onosproject.net.SparseAnnotations; |
| 35 | import org.onosproject.net.device.DefaultDeviceDescription; |
| 36 | import org.onosproject.net.device.DefaultPortDescription; |
| 37 | import org.onosproject.net.device.DeviceDescription; |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 38 | import org.onosproject.net.device.DeviceEvent; |
| 39 | import org.onosproject.net.device.DeviceListener; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 40 | import org.onosproject.net.device.DeviceProvider; |
| 41 | import org.onosproject.net.device.DeviceProviderRegistry; |
| 42 | import org.onosproject.net.device.DeviceProviderService; |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 43 | import org.onosproject.net.device.DeviceService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 44 | import org.onosproject.net.device.PortDescription; |
| 45 | import org.onosproject.net.host.DefaultHostDescription; |
| 46 | import org.onosproject.net.host.HostProvider; |
| 47 | import org.onosproject.net.host.HostProviderRegistry; |
| 48 | import org.onosproject.net.host.HostProviderService; |
| 49 | import org.onosproject.net.link.DefaultLinkDescription; |
| 50 | import org.onosproject.net.link.LinkProvider; |
| 51 | import org.onosproject.net.link.LinkProviderRegistry; |
| 52 | import org.onosproject.net.link.LinkProviderService; |
| 53 | import org.onosproject.net.provider.ProviderId; |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 54 | import org.slf4j.Logger; |
| 55 | import org.slf4j.LoggerFactory; |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 56 | |
| 57 | import java.net.URI; |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 58 | import java.util.ArrayList; |
Thomas Vachuska | ece59ee | 2014-11-19 19:06:11 -0800 | [diff] [blame] | 59 | import java.util.HashSet; |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 60 | import java.util.Iterator; |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 61 | import java.util.List; |
Thomas Vachuska | ece59ee | 2014-11-19 19:06:11 -0800 | [diff] [blame] | 62 | import java.util.Set; |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 63 | import java.util.concurrent.CountDownLatch; |
| 64 | import java.util.concurrent.TimeUnit; |
| 65 | import java.util.stream.Collectors; |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 66 | |
| 67 | import static com.google.common.base.Preconditions.checkNotNull; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 68 | import static org.onosproject.net.DeviceId.deviceId; |
| 69 | import static org.onosproject.net.PortNumber.portNumber; |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 70 | import static org.onosproject.net.device.DeviceEvent.Type.DEVICE_ADDED; |
| 71 | import static org.onosproject.net.device.DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED; |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 72 | |
| 73 | /** |
| 74 | * Provider of devices and links parsed from a JSON configuration structure. |
| 75 | */ |
| 76 | class ConfigProvider implements DeviceProvider, LinkProvider, HostProvider { |
| 77 | |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 78 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 79 | |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 80 | private static final ProviderId PID = |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 81 | new ProviderId("cfg", "org.onosproject.rest", true); |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 82 | |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 83 | private static final String UNKNOWN = "unknown"; |
| 84 | |
| 85 | private CountDownLatch deviceLatch; |
| 86 | |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 87 | private final JsonNode cfg; |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 88 | private final DeviceService deviceService; |
| 89 | |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 90 | private final DeviceProviderRegistry deviceProviderRegistry; |
| 91 | private final LinkProviderRegistry linkProviderRegistry; |
| 92 | private final HostProviderRegistry hostProviderRegistry; |
| 93 | |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 94 | private DeviceProviderService deviceProviderService; |
| 95 | private LinkProviderService linkProviderService; |
| 96 | private HostProviderService hostProviderService; |
| 97 | |
| 98 | private DeviceListener deviceEventCounter = new DeviceEventCounter(); |
| 99 | private List<ConnectPoint> connectPoints = Lists.newArrayList(); |
| 100 | |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 101 | /** |
| 102 | * Creates a new configuration provider. |
| 103 | * |
| 104 | * @param cfg JSON configuration |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 105 | * @param deviceService device service |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 106 | * @param deviceProviderRegistry device provider registry |
| 107 | * @param linkProviderRegistry link provider registry |
| 108 | * @param hostProviderRegistry host provider registry |
| 109 | */ |
| 110 | ConfigProvider(JsonNode cfg, |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 111 | DeviceService deviceService, |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 112 | DeviceProviderRegistry deviceProviderRegistry, |
| 113 | LinkProviderRegistry linkProviderRegistry, |
| 114 | HostProviderRegistry hostProviderRegistry) { |
| 115 | this.cfg = checkNotNull(cfg, "Configuration cannot be null"); |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 116 | this.deviceService = checkNotNull(deviceService, "Device service cannot be null"); |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 117 | this.deviceProviderRegistry = checkNotNull(deviceProviderRegistry, "Device provider registry cannot be null"); |
| 118 | this.linkProviderRegistry = checkNotNull(linkProviderRegistry, "Link provider registry cannot be null"); |
| 119 | this.hostProviderRegistry = checkNotNull(hostProviderRegistry, "Host provider registry cannot be null"); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Parses the given JSON and provides links as configured. |
| 124 | */ |
| 125 | void parse() { |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 126 | try { |
| 127 | register(); |
| 128 | parseDevices(); |
| 129 | parseLinks(); |
| 130 | parseHosts(); |
| 131 | addMissingPorts(); |
| 132 | } finally { |
| 133 | unregister(); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | private void register() { |
| 138 | deviceProviderService = deviceProviderRegistry.register(this); |
| 139 | linkProviderService = linkProviderRegistry.register(this); |
| 140 | hostProviderService = hostProviderRegistry.register(this); |
| 141 | } |
| 142 | |
| 143 | private void unregister() { |
| 144 | deviceProviderRegistry.unregister(this); |
| 145 | linkProviderRegistry.unregister(this); |
| 146 | hostProviderRegistry.unregister(this); |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | // Parses the given JSON and provides devices. |
| 150 | private void parseDevices() { |
| 151 | try { |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 152 | JsonNode nodes = cfg.get("devices"); |
| 153 | if (nodes != null) { |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 154 | prepareForDeviceEvents(nodes.size()); |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 155 | for (JsonNode node : nodes) { |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 156 | parseDevice(node); |
Thomas Vachuska | 3b84c86 | 2015-04-28 12:09:48 -0700 | [diff] [blame] | 157 | |
| 158 | // FIXME: hack to make sure device attributes take |
| 159 | // This will be fixed when GossipDeviceStore uses ECM |
| 160 | parseDevice(node); |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 161 | } |
| 162 | } |
| 163 | } finally { |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 164 | waitForDeviceEvents(); |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | |
| 168 | // Parses the given node with device data and supplies the device. |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 169 | private void parseDevice(JsonNode node) { |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 170 | URI uri = URI.create(get(node, "uri")); |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 171 | Device.Type type = Device.Type.valueOf(get(node, "type", "SWITCH")); |
| 172 | String mfr = get(node, "mfr", UNKNOWN); |
| 173 | String hw = get(node, "hw", UNKNOWN); |
| 174 | String sw = get(node, "sw", UNKNOWN); |
| 175 | String serial = get(node, "serial", UNKNOWN); |
| 176 | ChassisId cid = new ChassisId(get(node, "mac", "000000000000")); |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 177 | SparseAnnotations annotations = annotations(node.get("annotations")); |
| 178 | |
| 179 | DeviceDescription desc = |
| 180 | new DefaultDeviceDescription(uri, type, mfr, hw, sw, serial, |
| 181 | cid, annotations); |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 182 | DeviceId deviceId = deviceId(uri); |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 183 | deviceProviderService.deviceConnected(deviceId, desc); |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 184 | |
| 185 | JsonNode ports = node.get("ports"); |
| 186 | if (ports != null) { |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 187 | parsePorts(deviceId, ports); |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | |
| 191 | // Parses the given node with list of device ports. |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 192 | private void parsePorts(DeviceId deviceId, JsonNode nodes) { |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 193 | List<PortDescription> ports = new ArrayList<>(); |
| 194 | for (JsonNode node : nodes) { |
| 195 | ports.add(parsePort(node)); |
| 196 | } |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 197 | deviceProviderService.updatePorts(deviceId, ports); |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | // Parses the given node with port information. |
| 201 | private PortDescription parsePort(JsonNode node) { |
| 202 | Port.Type type = Port.Type.valueOf(node.path("type").asText("COPPER")); |
| 203 | return new DefaultPortDescription(portNumber(node.path("port").asLong(0)), |
| 204 | node.path("enabled").asBoolean(true), |
| 205 | type, node.path("speed").asLong(1_000)); |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | // Parses the given JSON and provides links as configured. |
| 209 | private void parseLinks() { |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 210 | JsonNode nodes = cfg.get("links"); |
| 211 | if (nodes != null) { |
| 212 | for (JsonNode node : nodes) { |
| 213 | parseLink(node, false); |
| 214 | if (!node.has("halfplex")) { |
| 215 | parseLink(node, true); |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 216 | } |
| 217 | } |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 218 | } |
| 219 | } |
| 220 | |
| 221 | // Parses the given node with link data and supplies the link. |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 222 | private void parseLink(JsonNode node, boolean reverse) { |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 223 | ConnectPoint src = connectPoint(get(node, "src")); |
| 224 | ConnectPoint dst = connectPoint(get(node, "dst")); |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 225 | Link.Type type = Link.Type.valueOf(get(node, "type", "DIRECT")); |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 226 | SparseAnnotations annotations = annotations(node.get("annotations")); |
| 227 | |
| 228 | DefaultLinkDescription desc = reverse ? |
| 229 | new DefaultLinkDescription(dst, src, type, annotations) : |
| 230 | new DefaultLinkDescription(src, dst, type, annotations); |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 231 | linkProviderService.linkDetected(desc); |
| 232 | |
| 233 | connectPoints.add(src); |
| 234 | connectPoints.add(dst); |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | // Parses the given JSON and provides hosts as configured. |
| 238 | private void parseHosts() { |
| 239 | try { |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 240 | JsonNode nodes = cfg.get("hosts"); |
| 241 | if (nodes != null) { |
| 242 | for (JsonNode node : nodes) { |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 243 | parseHost(node); |
Thomas Vachuska | 3b84c86 | 2015-04-28 12:09:48 -0700 | [diff] [blame] | 244 | |
| 245 | // FIXME: hack to make sure host attributes take |
| 246 | // This will be fixed when GossipHostStore uses ECM |
| 247 | parseHost(node); |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | } finally { |
| 251 | hostProviderRegistry.unregister(this); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | // Parses the given node with host data and supplies the host. |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 256 | private void parseHost(JsonNode node) { |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 257 | MacAddress mac = MacAddress.valueOf(get(node, "mac")); |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 258 | VlanId vlanId = VlanId.vlanId((short) node.get("vlan").asInt(VlanId.UNTAGGED)); |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 259 | HostId hostId = HostId.hostId(mac, vlanId); |
| 260 | SparseAnnotations annotations = annotations(node.get("annotations")); |
| 261 | HostLocation location = new HostLocation(connectPoint(get(node, "location")), 0); |
Thomas Vachuska | ece59ee | 2014-11-19 19:06:11 -0800 | [diff] [blame] | 262 | |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 263 | String[] ipStrings = get(node, "ip", "").split(","); |
Thomas Vachuska | ece59ee | 2014-11-19 19:06:11 -0800 | [diff] [blame] | 264 | Set<IpAddress> ips = new HashSet<>(); |
| 265 | for (String ip : ipStrings) { |
| 266 | ips.add(IpAddress.valueOf(ip.trim())); |
| 267 | } |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 268 | |
| 269 | DefaultHostDescription desc = |
Thomas Vachuska | ece59ee | 2014-11-19 19:06:11 -0800 | [diff] [blame] | 270 | new DefaultHostDescription(mac, vlanId, location, ips, annotations); |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 271 | hostProviderService.hostDetected(hostId, desc); |
| 272 | |
| 273 | connectPoints.add(location); |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 274 | } |
| 275 | |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 276 | // Adds any missing device ports for configured links and host locations. |
| 277 | private void addMissingPorts() { |
| 278 | deviceService.getDevices().forEach(this::addMissingPorts); |
| 279 | } |
| 280 | |
| 281 | // Adds any missing device ports. |
| 282 | private void addMissingPorts(Device device) { |
| 283 | List<Port> ports = deviceService.getPorts(device.id()); |
| 284 | Set<ConnectPoint> existing = ports.stream() |
| 285 | .map(p -> new ConnectPoint(device.id(), p.number())) |
| 286 | .collect(Collectors.toSet()); |
| 287 | Set<ConnectPoint> missing = connectPoints.stream() |
| 288 | .filter(cp -> !existing.contains(cp)) |
| 289 | .collect(Collectors.toSet()); |
| 290 | |
| 291 | if (!missing.isEmpty()) { |
| 292 | List<PortDescription> newPorts = Lists.newArrayList(); |
| 293 | ports.forEach(p -> newPorts.add(description(p))); |
| 294 | missing.forEach(cp -> newPorts.add(description(cp))); |
| 295 | deviceProviderService.updatePorts(device.id(), newPorts); |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | // Creates a port description from the specified port. |
| 300 | private PortDescription description(Port p) { |
| 301 | return new DefaultPortDescription(p.number(), p.isEnabled(), p.type(), p.portSpeed()); |
| 302 | } |
| 303 | |
| 304 | // Creates a port description from the specified connection point. |
| 305 | private PortDescription description(ConnectPoint cp) { |
| 306 | return new DefaultPortDescription(cp.port(), true); |
| 307 | } |
| 308 | |
| 309 | |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 310 | // Produces set of annotations from the given JSON node. |
| 311 | private SparseAnnotations annotations(JsonNode node) { |
| 312 | if (node == null) { |
| 313 | return null; |
| 314 | } |
| 315 | |
| 316 | DefaultAnnotations.Builder builder = DefaultAnnotations.builder(); |
| 317 | Iterator<String> it = node.fieldNames(); |
| 318 | while (it.hasNext()) { |
| 319 | String k = it.next(); |
| 320 | builder.set(k, node.get(k).asText()); |
| 321 | } |
| 322 | return builder.build(); |
| 323 | } |
| 324 | |
| 325 | // Produces a connection point from the specified uri/port text. |
| 326 | private ConnectPoint connectPoint(String text) { |
| 327 | int i = text.lastIndexOf("/"); |
| 328 | return new ConnectPoint(deviceId(text.substring(0, i)), |
| 329 | portNumber(text.substring(i + 1))); |
| 330 | } |
| 331 | |
| 332 | // Returns string form of the named property in the given JSON object. |
| 333 | private String get(JsonNode node, String name) { |
| 334 | return node.path(name).asText(); |
| 335 | } |
| 336 | |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 337 | // Returns string form of the named property in the given JSON object. |
| 338 | private String get(JsonNode node, String name, String defaultValue) { |
| 339 | return node.path(name).asText(defaultValue); |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | @Override |
Yuta HIGUCHI | 5481532 | 2014-10-31 23:17:08 -0700 | [diff] [blame] | 343 | public void roleChanged(DeviceId device, MastershipRole newRole) { |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 344 | deviceProviderService.receivedRoleReply(device, newRole, newRole); |
| 345 | } |
| 346 | |
| 347 | @Override |
| 348 | public void triggerProbe(DeviceId deviceId) { |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | @Override |
| 352 | public void triggerProbe(Host host) { |
| 353 | } |
| 354 | |
| 355 | @Override |
| 356 | public ProviderId id() { |
| 357 | return PID; |
| 358 | } |
Ayaka Koshibe | e60d452 | 2014-10-28 15:07:00 -0700 | [diff] [blame] | 359 | |
| 360 | @Override |
Yuta HIGUCHI | 5481532 | 2014-10-31 23:17:08 -0700 | [diff] [blame] | 361 | public boolean isReachable(DeviceId device) { |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 362 | return true; |
Ayaka Koshibe | e60d452 | 2014-10-28 15:07:00 -0700 | [diff] [blame] | 363 | } |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 364 | |
| 365 | /** |
| 366 | * Prepares to count device added/available/removed events. |
| 367 | * |
| 368 | * @param count number of events to count |
| 369 | */ |
| 370 | protected void prepareForDeviceEvents(int count) { |
| 371 | deviceLatch = new CountDownLatch(count); |
| 372 | deviceService.addListener(deviceEventCounter); |
| 373 | } |
| 374 | |
| 375 | /** |
| 376 | * Waits for all expected device added/available/removed events. |
| 377 | */ |
| 378 | protected void waitForDeviceEvents() { |
| 379 | try { |
| 380 | deviceLatch.await(2, TimeUnit.SECONDS); |
| 381 | } catch (InterruptedException e) { |
| 382 | log.warn("Device events did not arrive in time"); |
| 383 | } |
| 384 | deviceService.removeListener(deviceEventCounter); |
| 385 | } |
| 386 | |
| 387 | // Counts down number of device added/available/removed events. |
| 388 | private class DeviceEventCounter implements DeviceListener { |
| 389 | @Override |
| 390 | public void event(DeviceEvent event) { |
| 391 | DeviceEvent.Type type = event.type(); |
| 392 | if (type == DEVICE_ADDED || type == DEVICE_AVAILABILITY_CHANGED) { |
| 393 | deviceLatch.countDown(); |
| 394 | } |
| 395 | } |
| 396 | } |
| 397 | |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 398 | } |