blob: 17dc4fe7a588fae7333ae551b2e1636dba75f58c [file] [log] [blame]
Simon Hunt4e942932017-03-06 19:09:25 -08001/*
2 * Copyright 2017-present Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onosproject.cordsupport;
18
19import com.fasterxml.jackson.databind.ObjectMapper;
20import com.fasterxml.jackson.databind.node.ArrayNode;
21import com.fasterxml.jackson.databind.node.ObjectNode;
22import org.onosproject.net.Device;
23import org.onosproject.net.Host;
24import org.onosproject.net.Link;
25import org.onosproject.net.device.DeviceService;
26import org.onosproject.net.host.HostService;
27import org.onosproject.net.link.LinkService;
28import org.onosproject.rest.AbstractWebResource;
29
30import javax.ws.rs.GET;
31import javax.ws.rs.Path;
32import javax.ws.rs.core.Response;
33import java.util.HashSet;
34import java.util.Set;
35
36import static javax.ws.rs.core.MediaType.APPLICATION_JSON_TYPE;
37
38/**
39 * CORD support REST implementation.
40 */
41@Path("topo")
42public class CordSupportWebResource extends AbstractWebResource {
43
44 private static final String TILDE = "~";
45
46 private final ObjectMapper mapper = new ObjectMapper();
47 private final Set<String> seenLinks = new HashSet<>();
48
49 @GET
50 public Response queryTopology() {
51 ObjectNode root = mapper.createObjectNode();
52
53 addDevices(root);
54 addHosts(root);
55 addLinks(root);
56
57 return Response.ok(root.toString(), APPLICATION_JSON_TYPE).build();
58 }
59
60 private void addDevices(ObjectNode root) {
61 ArrayNode devices = createArray(root, "devices");
62 get(DeviceService.class).getDevices()
63 .forEach(dev -> devices.add(json(dev)));
64 }
65
66 private void addHosts(ObjectNode root) {
67 ArrayNode hosts = createArray(root, "hosts");
68 get(HostService.class).getHosts()
69 .forEach(host -> hosts.add(json(host)));
70 }
71
72 private void addLinks(ObjectNode root) {
73 ArrayNode links = createArray(root, "links");
74 seenLinks.clear();
75 get(LinkService.class).getLinks()
76 .forEach(link -> {
77 String canon = canonicalRep(link);
78 if (!seenLinks.contains(canon)) {
79 seenLinks.add(canon);
80 links.add(json(link));
81 }
82 });
83 }
84
85 private String canonicalRep(Link link) {
86 String a = link.src().toString();
87 String b = link.dst().toString();
88 return a.compareTo(b) < 0 ? a + TILDE + b : b + TILDE + a;
89 }
90
91 private ArrayNode createArray(ObjectNode root, String key) {
92 ArrayNode result = mapper.createArrayNode();
93 root.set(key, result);
94 return result;
95 }
96
97 private ObjectNode json(Device device) {
98 return mapper.createObjectNode()
99 .put("id", device.id().toString())
100 .put("type", device.type().toString());
101 }
102
103 private ObjectNode json(Host host) {
104 return mapper.createObjectNode()
105 .put("id", host.id().toString())
106 .put("location", host.location().toString());
107 }
108
109 private ObjectNode json(Link link) {
110 return mapper.createObjectNode()
111 .put("canon", canonicalRep(link))
112 .put("src", link.src().toString())
113 .put("dst", link.dst().toString());
114 }
115
116}