blob: 3974f171359864331c6f2d143c1c976e312dadd3 [file] [log] [blame]
Thomas Vachuska9252bc32014-10-23 02:33:25 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska9252bc32014-10-23 02:33:25 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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 Vachuska9252bc32014-10-23 02:33:25 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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 Vachuska9252bc32014-10-23 02:33:25 -070015 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.rest;
Thomas Vachuska9252bc32014-10-23 02:33:25 -070017
18import com.fasterxml.jackson.databind.JsonNode;
Thomas Vachuskac40d4632015-04-09 16:55:03 -070019import com.google.common.collect.Lists;
20import org.onlab.packet.ChassisId;
21import org.onlab.packet.IpAddress;
22import org.onlab.packet.MacAddress;
23import org.onlab.packet.VlanId;
Brian O'Connorabafb502014-12-02 22:26:20 -080024import org.onosproject.net.ConnectPoint;
25import org.onosproject.net.DefaultAnnotations;
26import org.onosproject.net.Device;
27import org.onosproject.net.DeviceId;
28import org.onosproject.net.Host;
29import org.onosproject.net.HostId;
30import org.onosproject.net.HostLocation;
31import org.onosproject.net.Link;
32import org.onosproject.net.MastershipRole;
33import org.onosproject.net.Port;
34import org.onosproject.net.SparseAnnotations;
35import org.onosproject.net.device.DefaultDeviceDescription;
36import org.onosproject.net.device.DefaultPortDescription;
37import org.onosproject.net.device.DeviceDescription;
Thomas Vachuskac40d4632015-04-09 16:55:03 -070038import org.onosproject.net.device.DeviceEvent;
39import org.onosproject.net.device.DeviceListener;
Brian O'Connorabafb502014-12-02 22:26:20 -080040import org.onosproject.net.device.DeviceProvider;
41import org.onosproject.net.device.DeviceProviderRegistry;
42import org.onosproject.net.device.DeviceProviderService;
Thomas Vachuskac40d4632015-04-09 16:55:03 -070043import org.onosproject.net.device.DeviceService;
Brian O'Connorabafb502014-12-02 22:26:20 -080044import org.onosproject.net.device.PortDescription;
45import org.onosproject.net.host.DefaultHostDescription;
46import org.onosproject.net.host.HostProvider;
47import org.onosproject.net.host.HostProviderRegistry;
48import org.onosproject.net.host.HostProviderService;
49import org.onosproject.net.link.DefaultLinkDescription;
50import org.onosproject.net.link.LinkProvider;
51import org.onosproject.net.link.LinkProviderRegistry;
52import org.onosproject.net.link.LinkProviderService;
53import org.onosproject.net.provider.ProviderId;
Thomas Vachuskac40d4632015-04-09 16:55:03 -070054import org.slf4j.Logger;
55import org.slf4j.LoggerFactory;
Thomas Vachuska9252bc32014-10-23 02:33:25 -070056
57import java.net.URI;
Thomas Vachuskad16ce182014-10-29 17:25:29 -070058import java.util.ArrayList;
Thomas Vachuskaece59ee2014-11-19 19:06:11 -080059import java.util.HashSet;
Thomas Vachuska9252bc32014-10-23 02:33:25 -070060import java.util.Iterator;
Thomas Vachuskad16ce182014-10-29 17:25:29 -070061import java.util.List;
Thomas Vachuskaece59ee2014-11-19 19:06:11 -080062import java.util.Set;
Thomas Vachuskac40d4632015-04-09 16:55:03 -070063import java.util.concurrent.CountDownLatch;
64import java.util.concurrent.TimeUnit;
65import java.util.stream.Collectors;
Thomas Vachuska9252bc32014-10-23 02:33:25 -070066
67import static com.google.common.base.Preconditions.checkNotNull;
Brian O'Connorabafb502014-12-02 22:26:20 -080068import static org.onosproject.net.DeviceId.deviceId;
69import static org.onosproject.net.PortNumber.portNumber;
Thomas Vachuskac40d4632015-04-09 16:55:03 -070070import static org.onosproject.net.device.DeviceEvent.Type.DEVICE_ADDED;
71import static org.onosproject.net.device.DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED;
Thomas Vachuska9252bc32014-10-23 02:33:25 -070072
73/**
74 * Provider of devices and links parsed from a JSON configuration structure.
75 */
76class ConfigProvider implements DeviceProvider, LinkProvider, HostProvider {
77
Thomas Vachuskac40d4632015-04-09 16:55:03 -070078 private final Logger log = LoggerFactory.getLogger(getClass());
79
Thomas Vachuska9252bc32014-10-23 02:33:25 -070080 private static final ProviderId PID =
Brian O'Connorabafb502014-12-02 22:26:20 -080081 new ProviderId("cfg", "org.onosproject.rest", true);
Thomas Vachuska9252bc32014-10-23 02:33:25 -070082
Thomas Vachuskac40d4632015-04-09 16:55:03 -070083 private static final String UNKNOWN = "unknown";
84
85 private CountDownLatch deviceLatch;
86
Thomas Vachuska9252bc32014-10-23 02:33:25 -070087 private final JsonNode cfg;
Thomas Vachuskac40d4632015-04-09 16:55:03 -070088 private final DeviceService deviceService;
89
Thomas Vachuska9252bc32014-10-23 02:33:25 -070090 private final DeviceProviderRegistry deviceProviderRegistry;
91 private final LinkProviderRegistry linkProviderRegistry;
92 private final HostProviderRegistry hostProviderRegistry;
93
Thomas Vachuskac40d4632015-04-09 16:55:03 -070094 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 Vachuska9252bc32014-10-23 02:33:25 -0700101 /**
102 * Creates a new configuration provider.
103 *
104 * @param cfg JSON configuration
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700105 * @param deviceService device service
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700106 * @param deviceProviderRegistry device provider registry
107 * @param linkProviderRegistry link provider registry
108 * @param hostProviderRegistry host provider registry
109 */
110 ConfigProvider(JsonNode cfg,
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700111 DeviceService deviceService,
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700112 DeviceProviderRegistry deviceProviderRegistry,
113 LinkProviderRegistry linkProviderRegistry,
114 HostProviderRegistry hostProviderRegistry) {
115 this.cfg = checkNotNull(cfg, "Configuration cannot be null");
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700116 this.deviceService = checkNotNull(deviceService, "Device service cannot be null");
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700117 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 Vachuskac40d4632015-04-09 16:55:03 -0700126 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 Vachuska9252bc32014-10-23 02:33:25 -0700147 }
148
149 // Parses the given JSON and provides devices.
150 private void parseDevices() {
151 try {
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700152 JsonNode nodes = cfg.get("devices");
153 if (nodes != null) {
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700154 prepareForDeviceEvents(nodes.size());
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700155 for (JsonNode node : nodes) {
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700156 parseDevice(node);
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700157 }
158 }
159 } finally {
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700160 waitForDeviceEvents();
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700161 }
162 }
163
164 // Parses the given node with device data and supplies the device.
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700165 private void parseDevice(JsonNode node) {
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700166 URI uri = URI.create(get(node, "uri"));
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700167 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 Vachuska9252bc32014-10-23 02:33:25 -0700173 SparseAnnotations annotations = annotations(node.get("annotations"));
174
175 DeviceDescription desc =
176 new DefaultDeviceDescription(uri, type, mfr, hw, sw, serial,
177 cid, annotations);
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700178 DeviceId deviceId = deviceId(uri);
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700179 deviceProviderService.deviceConnected(deviceId, desc);
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700180
181 JsonNode ports = node.get("ports");
182 if (ports != null) {
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700183 parsePorts(deviceId, ports);
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700184 }
185 }
186
187 // Parses the given node with list of device ports.
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700188 private void parsePorts(DeviceId deviceId, JsonNode nodes) {
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700189 List<PortDescription> ports = new ArrayList<>();
190 for (JsonNode node : nodes) {
191 ports.add(parsePort(node));
192 }
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700193 deviceProviderService.updatePorts(deviceId, ports);
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700194 }
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 Vachuska9252bc32014-10-23 02:33:25 -0700202 }
203
204 // Parses the given JSON and provides links as configured.
205 private void parseLinks() {
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700206 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 Vachuska9252bc32014-10-23 02:33:25 -0700212 }
213 }
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700214 }
215 }
216
217 // Parses the given node with link data and supplies the link.
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700218 private void parseLink(JsonNode node, boolean reverse) {
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700219 ConnectPoint src = connectPoint(get(node, "src"));
220 ConnectPoint dst = connectPoint(get(node, "dst"));
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700221 Link.Type type = Link.Type.valueOf(get(node, "type", "DIRECT"));
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700222 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 Vachuskac40d4632015-04-09 16:55:03 -0700227 linkProviderService.linkDetected(desc);
228
229 connectPoints.add(src);
230 connectPoints.add(dst);
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700231 }
232
233 // Parses the given JSON and provides hosts as configured.
234 private void parseHosts() {
235 try {
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700236 JsonNode nodes = cfg.get("hosts");
237 if (nodes != null) {
238 for (JsonNode node : nodes) {
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700239 parseHost(node);
240 parseHost(node); // FIXME: hack to make sure host positions take
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700241 }
242 }
243 } finally {
244 hostProviderRegistry.unregister(this);
245 }
246 }
247
248 // Parses the given node with host data and supplies the host.
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700249 private void parseHost(JsonNode node) {
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700250 MacAddress mac = MacAddress.valueOf(get(node, "mac"));
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700251 VlanId vlanId = VlanId.vlanId((short) node.get("vlan").asInt(VlanId.UNTAGGED));
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700252 HostId hostId = HostId.hostId(mac, vlanId);
253 SparseAnnotations annotations = annotations(node.get("annotations"));
254 HostLocation location = new HostLocation(connectPoint(get(node, "location")), 0);
Thomas Vachuskaece59ee2014-11-19 19:06:11 -0800255
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700256 String[] ipStrings = get(node, "ip", "").split(",");
Thomas Vachuskaece59ee2014-11-19 19:06:11 -0800257 Set<IpAddress> ips = new HashSet<>();
258 for (String ip : ipStrings) {
259 ips.add(IpAddress.valueOf(ip.trim()));
260 }
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700261
262 DefaultHostDescription desc =
Thomas Vachuskaece59ee2014-11-19 19:06:11 -0800263 new DefaultHostDescription(mac, vlanId, location, ips, annotations);
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700264 hostProviderService.hostDetected(hostId, desc);
265
266 connectPoints.add(location);
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700267 }
268
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700269 // 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 Vachuska9252bc32014-10-23 02:33:25 -0700303 // 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 Vachuskac40d4632015-04-09 16:55:03 -0700330 // 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 Vachuska9252bc32014-10-23 02:33:25 -0700333 }
334
335 @Override
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700336 public void roleChanged(DeviceId device, MastershipRole newRole) {
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700337 deviceProviderService.receivedRoleReply(device, newRole, newRole);
338 }
339
340 @Override
341 public void triggerProbe(DeviceId deviceId) {
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700342 }
343
344 @Override
345 public void triggerProbe(Host host) {
346 }
347
348 @Override
349 public ProviderId id() {
350 return PID;
351 }
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700352
353 @Override
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700354 public boolean isReachable(DeviceId device) {
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700355 return true;
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700356 }
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700357
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 Vachuska9252bc32014-10-23 02:33:25 -0700391}