blob: 99872ac0cf92fed03dbe33d2ea2b4a6784c5b9f3 [file] [log] [blame]
Thomas Vachuska7d693f52014-10-21 19:17:57 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska7d693f52014-10-21 19:17:57 -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 Vachuska7d693f52014-10-21 19:17:57 -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 Vachuska7d693f52014-10-21 19:17:57 -070015 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.cli.net;
tom6d2a43e2014-09-08 01:50:20 -070017
tom32085cf2014-10-16 00:04:33 -070018import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.ObjectMapper;
20import com.fasterxml.jackson.databind.node.ArrayNode;
21import com.fasterxml.jackson.databind.node.ObjectNode;
tom6d2a43e2014-09-08 01:50:20 -070022import org.apache.karaf.shell.commands.Argument;
23import org.apache.karaf.shell.commands.Command;
Brian O'Connorabafb502014-12-02 22:26:20 -080024import org.onosproject.cli.AbstractShellCommand;
25import org.onosproject.net.ConnectPoint;
Pavlin Radoslavovaab51a32014-12-08 11:07:38 -080026import org.onosproject.net.HostId;
Brian O'Connorabafb502014-12-02 22:26:20 -080027import org.onosproject.net.Link;
28import org.onosproject.net.link.LinkService;
tom6d2a43e2014-09-08 01:50:20 -070029
Brian O'Connorabafb502014-12-02 22:26:20 -080030import static org.onosproject.net.DeviceId.deviceId;
tom6d2a43e2014-09-08 01:50:20 -070031
32/**
33 * Lists all infrastructure links.
34 */
35@Command(scope = "onos", name = "links",
36 description = "Lists all infrastructure links")
37public class LinksListCommand extends AbstractShellCommand {
38
Thomas Vachuskae4cebaf2014-11-15 18:49:34 -080039 private static final String FMT = "src=%s/%s, dst=%s/%s, type=%s, state=%s%s";
tom9eb57fb2014-09-11 19:42:38 -070040 private static final String COMPACT = "%s/%s-%s/%s";
tom6d2a43e2014-09-08 01:50:20 -070041
tomc290a122014-09-08 14:27:13 -070042 @Argument(index = 0, name = "uri", description = "Device ID",
tom6d2a43e2014-09-08 01:50:20 -070043 required = false, multiValued = false)
tomc290a122014-09-08 14:27:13 -070044 String uri = null;
tom6d2a43e2014-09-08 01:50:20 -070045
tom6d2a43e2014-09-08 01:50:20 -070046 @Override
tom0872a172014-09-23 11:24:26 -070047 protected void execute() {
tomcaf3bf72014-09-23 13:20:53 -070048 LinkService service = get(LinkService.class);
tomc290a122014-09-08 14:27:13 -070049 Iterable<Link> links = uri != null ?
50 service.getDeviceLinks(deviceId(uri)) : service.getLinks();
tom32085cf2014-10-16 00:04:33 -070051 if (outputJson()) {
52 print("%s", json(links));
53 } else {
54 for (Link link : links) {
55 print(linkString(link));
56 }
tom6d2a43e2014-09-08 01:50:20 -070057 }
tom6d2a43e2014-09-08 01:50:20 -070058 }
tom13cb4852014-09-11 12:44:17 -070059
60 /**
tom32085cf2014-10-16 00:04:33 -070061 * Produces a JSON array containing the specified links.
62 *
63 * @param links collection of links
64 * @return JSON array
65 */
66 public static JsonNode json(Iterable<Link> links) {
67 ObjectMapper mapper = new ObjectMapper();
68 ArrayNode result = mapper.createArrayNode();
69 for (Link link : links) {
70 result.add(json(mapper, link));
71 }
72 return result;
73 }
74
75 /**
76 * Produces a JSON object for the specified link.
77 *
78 * @param mapper object mapper
79 * @param link link to encode
80 * @return JSON object
81 */
82 public static ObjectNode json(ObjectMapper mapper, Link link) {
83 ObjectNode result = mapper.createObjectNode();
84 result.set("src", json(mapper, link.src()));
tomc65fa112014-10-16 07:48:47 -070085 result.set("dst", json(mapper, link.dst()));
Thomas Vachuskae4cebaf2014-11-15 18:49:34 -080086 result.put("type", link.type().toString());
87 result.put("state", link.state().toString());
Thomas Vachuska944cb6c2014-10-22 17:05:42 -070088 result.set("annotations", annotations(mapper, link.annotations()));
tom32085cf2014-10-16 00:04:33 -070089 return result;
90 }
91
92 /**
Pavlin Radoslavovaab51a32014-12-08 11:07:38 -080093 * Produces a JSON object for the specified host ID.
94 *
95 * @param mapper object mapper
96 * @param hostId host ID to encode
97 * @return JSON object
98 */
99 public static ObjectNode json(ObjectMapper mapper, HostId hostId) {
100 return mapper.createObjectNode()
101 .put("mac", hostId.mac().toString())
102 .put("vlanId", hostId.vlanId().toString());
103 }
104
105 /**
tom32085cf2014-10-16 00:04:33 -0700106 * Produces a JSON object for the specified connect point.
107 *
108 * @param mapper object mapper
109 * @param connectPoint connection point to encode
110 * @return JSON object
111 */
112 public static ObjectNode json(ObjectMapper mapper, ConnectPoint connectPoint) {
113 return mapper.createObjectNode()
114 .put("device", connectPoint.deviceId().toString())
115 .put("port", connectPoint.port().toString());
116 }
117
118 /**
tom613d8142014-09-11 15:09:37 -0700119 * Returns a formatted string representing the given link.
tom13cb4852014-09-11 12:44:17 -0700120 *
121 * @param link infrastructure link
tom613d8142014-09-11 15:09:37 -0700122 * @return formatted link string
tom13cb4852014-09-11 12:44:17 -0700123 */
124 public static String linkString(Link link) {
125 return String.format(FMT, link.src().deviceId(), link.src().port(),
Thomas Vachuskae4cebaf2014-11-15 18:49:34 -0800126 link.dst().deviceId(), link.dst().port(),
127 link.type(), link.state(),
Thomas Vachuska944cb6c2014-10-22 17:05:42 -0700128 annotations(link.annotations()));
tom13cb4852014-09-11 12:44:17 -0700129 }
tom9eb57fb2014-09-11 19:42:38 -0700130
131 /**
132 * Returns a compact string representing the given link.
133 *
134 * @param link infrastructure link
135 * @return formatted link string
136 */
137 public static String compactLinkString(Link link) {
138 return String.format(COMPACT, link.src().deviceId(), link.src().port(),
139 link.dst().deviceId(), link.dst().port());
140 }
141
tom6d2a43e2014-09-08 01:50:20 -0700142}