blob: 063f6e5ef96501584dd9d764e19eedec2ebde8ef [file] [log] [blame]
Ray Milkey19ffea32015-01-28 10:03:06 -08001/*
2 * Copyright 2015 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 */
16package org.onosproject.rest;
17
18import java.util.Set;
19
20import javax.ws.rs.GET;
21import javax.ws.rs.Path;
22import javax.ws.rs.PathParam;
23import javax.ws.rs.Produces;
24import javax.ws.rs.core.MediaType;
25import javax.ws.rs.core.Response;
26
27import org.onosproject.net.DeviceId;
28import org.onosproject.net.ElementId;
29import org.onosproject.net.HostId;
30import org.onosproject.net.topology.PathService;
31
32import com.fasterxml.jackson.databind.node.ObjectNode;
33
34/**
35 * REST resource for interacting with path calculations.
36 */
37@Path("paths")
38public class PathsWebResource extends AbstractWebResource {
39
40 // Host id format is 00:00:00:00:00:01/-1
41 private static final int VLAN_SEPARATOR_OFFSET = 17;
42 private static final int FIRST_MAC_ADDRESS_SEPARATOR_OFFSET = 2;
43 private static final int SECOND_MAC_ADDRESS_SEPARATOR_OFFSET = 5;
44 private static final int THIRD_MAC_ADDRESS_SEPARATOR_OFFSET = 8;
45
46 /**
47 * Determines if the id appears to be the id of a host.
48 *
49 * @param id id string
50 * @return HostId if the id is valid, null otherwise
51 */
52 private HostId isHostId(String id) {
53
54 if (id.length() < VLAN_SEPARATOR_OFFSET + 1 ||
55 id.charAt(FIRST_MAC_ADDRESS_SEPARATOR_OFFSET) != ':' ||
56 id.charAt(SECOND_MAC_ADDRESS_SEPARATOR_OFFSET) != ':' ||
57 id.charAt(THIRD_MAC_ADDRESS_SEPARATOR_OFFSET) != ':' ||
58 id.charAt(VLAN_SEPARATOR_OFFSET) != '/') {
59 return null;
60 }
61
62 return HostId.hostId(id);
63 }
64
65 /**
66 * Gets the paths between two elements.
67 *
68 * @param src source
69 * @param dst destination
70 * @return path data
71 */
72 @GET
73 @Produces(MediaType.APPLICATION_JSON)
74 @Path("{src}/{dst}")
75 public Response getPath(@PathParam("src") String src,
76 @PathParam("dst") String dst) {
77 PathService pathService = get(PathService.class);
78
79 ElementId srcElement = isHostId(src);
80 ElementId dstElement = isHostId(dst);
81
82 if (srcElement == null) {
83 // Doesn't look like a host, assume it is a device
84 srcElement = DeviceId.deviceId(src);
85 }
86
87 if (dstElement == null) {
88 // Doesn't look like a host, assume it is a device
89 dstElement = DeviceId.deviceId(dst);
90 }
91
92 Set<org.onosproject.net.Path> paths =
93 pathService.getPaths(srcElement, dstElement);
94 ObjectNode root =
95 encodeArray(org.onosproject.net.Path.class, "paths", paths);
96 return ok(root).build();
97 }
98
99}