Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.rest; |
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 | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | } finally { |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame^] | 160 | waitForDeviceEvents(); |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 161 | } |
| 162 | } |
| 163 | |
| 164 | // Parses the given node with device data and supplies the device. |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame^] | 165 | private void parseDevice(JsonNode node) { |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 166 | URI uri = URI.create(get(node, "uri")); |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame^] | 167 | Device.Type type = Device.Type.valueOf(get(node, "type", "SWITCH")); |
| 168 | String mfr = get(node, "mfr", UNKNOWN); |
| 169 | String hw = get(node, "hw", UNKNOWN); |
| 170 | String sw = get(node, "sw", UNKNOWN); |
| 171 | String serial = get(node, "serial", UNKNOWN); |
| 172 | ChassisId cid = new ChassisId(get(node, "mac", "000000000000")); |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 173 | SparseAnnotations annotations = annotations(node.get("annotations")); |
| 174 | |
| 175 | DeviceDescription desc = |
| 176 | new DefaultDeviceDescription(uri, type, mfr, hw, sw, serial, |
| 177 | cid, annotations); |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 178 | DeviceId deviceId = deviceId(uri); |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame^] | 179 | deviceProviderService.deviceConnected(deviceId, desc); |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 180 | |
| 181 | JsonNode ports = node.get("ports"); |
| 182 | if (ports != null) { |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame^] | 183 | parsePorts(deviceId, ports); |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | |
| 187 | // Parses the given node with list of device ports. |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame^] | 188 | private void parsePorts(DeviceId deviceId, JsonNode nodes) { |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 189 | List<PortDescription> ports = new ArrayList<>(); |
| 190 | for (JsonNode node : nodes) { |
| 191 | ports.add(parsePort(node)); |
| 192 | } |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame^] | 193 | deviceProviderService.updatePorts(deviceId, ports); |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | // Parses the given node with port information. |
| 197 | private PortDescription parsePort(JsonNode node) { |
| 198 | Port.Type type = Port.Type.valueOf(node.path("type").asText("COPPER")); |
| 199 | return new DefaultPortDescription(portNumber(node.path("port").asLong(0)), |
| 200 | node.path("enabled").asBoolean(true), |
| 201 | type, node.path("speed").asLong(1_000)); |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | // Parses the given JSON and provides links as configured. |
| 205 | private void parseLinks() { |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame^] | 206 | JsonNode nodes = cfg.get("links"); |
| 207 | if (nodes != null) { |
| 208 | for (JsonNode node : nodes) { |
| 209 | parseLink(node, false); |
| 210 | if (!node.has("halfplex")) { |
| 211 | parseLink(node, true); |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 212 | } |
| 213 | } |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 214 | } |
| 215 | } |
| 216 | |
| 217 | // Parses the given node with link data and supplies the link. |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame^] | 218 | private void parseLink(JsonNode node, boolean reverse) { |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 219 | ConnectPoint src = connectPoint(get(node, "src")); |
| 220 | ConnectPoint dst = connectPoint(get(node, "dst")); |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame^] | 221 | Link.Type type = Link.Type.valueOf(get(node, "type", "DIRECT")); |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 222 | SparseAnnotations annotations = annotations(node.get("annotations")); |
| 223 | |
| 224 | DefaultLinkDescription desc = reverse ? |
| 225 | new DefaultLinkDescription(dst, src, type, annotations) : |
| 226 | new DefaultLinkDescription(src, dst, type, annotations); |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame^] | 227 | linkProviderService.linkDetected(desc); |
| 228 | |
| 229 | connectPoints.add(src); |
| 230 | connectPoints.add(dst); |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | // Parses the given JSON and provides hosts as configured. |
| 234 | private void parseHosts() { |
| 235 | try { |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 236 | JsonNode nodes = cfg.get("hosts"); |
| 237 | if (nodes != null) { |
| 238 | for (JsonNode node : nodes) { |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame^] | 239 | parseHost(node); |
| 240 | parseHost(node); // FIXME: hack to make sure host positions take |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 241 | } |
| 242 | } |
| 243 | } finally { |
| 244 | hostProviderRegistry.unregister(this); |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | // Parses the given node with host data and supplies the host. |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame^] | 249 | private void parseHost(JsonNode node) { |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 250 | MacAddress mac = MacAddress.valueOf(get(node, "mac")); |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame^] | 251 | VlanId vlanId = VlanId.vlanId((short) node.get("vlan").asInt(VlanId.UNTAGGED)); |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 252 | HostId hostId = HostId.hostId(mac, vlanId); |
| 253 | SparseAnnotations annotations = annotations(node.get("annotations")); |
| 254 | HostLocation location = new HostLocation(connectPoint(get(node, "location")), 0); |
Thomas Vachuska | ece59ee | 2014-11-19 19:06:11 -0800 | [diff] [blame] | 255 | |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame^] | 256 | String[] ipStrings = get(node, "ip", "").split(","); |
Thomas Vachuska | ece59ee | 2014-11-19 19:06:11 -0800 | [diff] [blame] | 257 | Set<IpAddress> ips = new HashSet<>(); |
| 258 | for (String ip : ipStrings) { |
| 259 | ips.add(IpAddress.valueOf(ip.trim())); |
| 260 | } |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 261 | |
| 262 | DefaultHostDescription desc = |
Thomas Vachuska | ece59ee | 2014-11-19 19:06:11 -0800 | [diff] [blame] | 263 | new DefaultHostDescription(mac, vlanId, location, ips, annotations); |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame^] | 264 | hostProviderService.hostDetected(hostId, desc); |
| 265 | |
| 266 | connectPoints.add(location); |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 267 | } |
| 268 | |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame^] | 269 | // Adds any missing device ports for configured links and host locations. |
| 270 | private void addMissingPorts() { |
| 271 | deviceService.getDevices().forEach(this::addMissingPorts); |
| 272 | } |
| 273 | |
| 274 | // Adds any missing device ports. |
| 275 | private void addMissingPorts(Device device) { |
| 276 | List<Port> ports = deviceService.getPorts(device.id()); |
| 277 | Set<ConnectPoint> existing = ports.stream() |
| 278 | .map(p -> new ConnectPoint(device.id(), p.number())) |
| 279 | .collect(Collectors.toSet()); |
| 280 | Set<ConnectPoint> missing = connectPoints.stream() |
| 281 | .filter(cp -> !existing.contains(cp)) |
| 282 | .collect(Collectors.toSet()); |
| 283 | |
| 284 | if (!missing.isEmpty()) { |
| 285 | List<PortDescription> newPorts = Lists.newArrayList(); |
| 286 | ports.forEach(p -> newPorts.add(description(p))); |
| 287 | missing.forEach(cp -> newPorts.add(description(cp))); |
| 288 | deviceProviderService.updatePorts(device.id(), newPorts); |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | // Creates a port description from the specified port. |
| 293 | private PortDescription description(Port p) { |
| 294 | return new DefaultPortDescription(p.number(), p.isEnabled(), p.type(), p.portSpeed()); |
| 295 | } |
| 296 | |
| 297 | // Creates a port description from the specified connection point. |
| 298 | private PortDescription description(ConnectPoint cp) { |
| 299 | return new DefaultPortDescription(cp.port(), true); |
| 300 | } |
| 301 | |
| 302 | |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 303 | // Produces set of annotations from the given JSON node. |
| 304 | private SparseAnnotations annotations(JsonNode node) { |
| 305 | if (node == null) { |
| 306 | return null; |
| 307 | } |
| 308 | |
| 309 | DefaultAnnotations.Builder builder = DefaultAnnotations.builder(); |
| 310 | Iterator<String> it = node.fieldNames(); |
| 311 | while (it.hasNext()) { |
| 312 | String k = it.next(); |
| 313 | builder.set(k, node.get(k).asText()); |
| 314 | } |
| 315 | return builder.build(); |
| 316 | } |
| 317 | |
| 318 | // Produces a connection point from the specified uri/port text. |
| 319 | private ConnectPoint connectPoint(String text) { |
| 320 | int i = text.lastIndexOf("/"); |
| 321 | return new ConnectPoint(deviceId(text.substring(0, i)), |
| 322 | portNumber(text.substring(i + 1))); |
| 323 | } |
| 324 | |
| 325 | // Returns string form of the named property in the given JSON object. |
| 326 | private String get(JsonNode node, String name) { |
| 327 | return node.path(name).asText(); |
| 328 | } |
| 329 | |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame^] | 330 | // Returns string form of the named property in the given JSON object. |
| 331 | private String get(JsonNode node, String name, String defaultValue) { |
| 332 | return node.path(name).asText(defaultValue); |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | @Override |
Yuta HIGUCHI | 5481532 | 2014-10-31 23:17:08 -0700 | [diff] [blame] | 336 | public void roleChanged(DeviceId device, MastershipRole newRole) { |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame^] | 337 | deviceProviderService.receivedRoleReply(device, newRole, newRole); |
| 338 | } |
| 339 | |
| 340 | @Override |
| 341 | public void triggerProbe(DeviceId deviceId) { |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | @Override |
| 345 | public void triggerProbe(Host host) { |
| 346 | } |
| 347 | |
| 348 | @Override |
| 349 | public ProviderId id() { |
| 350 | return PID; |
| 351 | } |
Ayaka Koshibe | e60d452 | 2014-10-28 15:07:00 -0700 | [diff] [blame] | 352 | |
| 353 | @Override |
Yuta HIGUCHI | 5481532 | 2014-10-31 23:17:08 -0700 | [diff] [blame] | 354 | public boolean isReachable(DeviceId device) { |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame^] | 355 | return true; |
Ayaka Koshibe | e60d452 | 2014-10-28 15:07:00 -0700 | [diff] [blame] | 356 | } |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame^] | 357 | |
| 358 | /** |
| 359 | * Prepares to count device added/available/removed events. |
| 360 | * |
| 361 | * @param count number of events to count |
| 362 | */ |
| 363 | protected void prepareForDeviceEvents(int count) { |
| 364 | deviceLatch = new CountDownLatch(count); |
| 365 | deviceService.addListener(deviceEventCounter); |
| 366 | } |
| 367 | |
| 368 | /** |
| 369 | * Waits for all expected device added/available/removed events. |
| 370 | */ |
| 371 | protected void waitForDeviceEvents() { |
| 372 | try { |
| 373 | deviceLatch.await(2, TimeUnit.SECONDS); |
| 374 | } catch (InterruptedException e) { |
| 375 | log.warn("Device events did not arrive in time"); |
| 376 | } |
| 377 | deviceService.removeListener(deviceEventCounter); |
| 378 | } |
| 379 | |
| 380 | // Counts down number of device added/available/removed events. |
| 381 | private class DeviceEventCounter implements DeviceListener { |
| 382 | @Override |
| 383 | public void event(DeviceEvent event) { |
| 384 | DeviceEvent.Type type = event.type(); |
| 385 | if (type == DEVICE_ADDED || type == DEVICE_AVAILABILITY_CHANGED) { |
| 386 | deviceLatch.countDown(); |
| 387 | } |
| 388 | } |
| 389 | } |
| 390 | |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 391 | } |