blob: 1aff4561f483572d2c7c595060d4b70badcb2fbc [file] [log] [blame]
jccd8697232015-05-05 14:42:23 +08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
jccd8697232015-05-05 14:42:23 +08003 *
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.cli.net;
17
18import java.util.Collection;
samuel7a5691a2015-05-23 00:36:32 +080019import java.util.HashSet;
jccd8697232015-05-05 14:42:23 +080020import java.util.Optional;
21
jccd8697232015-05-05 14:42:23 +080022import org.apache.karaf.shell.commands.Command;
samuel7a5691a2015-05-23 00:36:32 +080023import org.apache.karaf.shell.commands.Option;
jccd8697232015-05-05 14:42:23 +080024import org.onlab.packet.IpAddress;
25import org.onosproject.cli.AbstractShellCommand;
Thomas Vachuskabf916ea2015-05-20 18:24:34 -070026import org.onosproject.incubator.net.tunnel.DefaultOpticalTunnelEndPoint;
27import org.onosproject.incubator.net.tunnel.IpTunnelEndPoint;
28import org.onosproject.incubator.net.tunnel.OpticalLogicId;
29import org.onosproject.incubator.net.tunnel.OpticalTunnelEndPoint;
30import org.onosproject.incubator.net.tunnel.Tunnel;
31import org.onosproject.incubator.net.tunnel.TunnelEndPoint;
samuel7a5691a2015-05-23 00:36:32 +080032import org.onosproject.incubator.net.tunnel.TunnelId;
Thomas Vachuskabf916ea2015-05-20 18:24:34 -070033import org.onosproject.incubator.net.tunnel.TunnelService;
samuel7a5691a2015-05-23 00:36:32 +080034import org.onosproject.net.DeviceId;
35import org.onosproject.net.Link;
36import org.onosproject.net.Path;
37import org.onosproject.net.PortNumber;
38import org.onosproject.net.provider.ProviderId;
jccd8697232015-05-05 14:42:23 +080039
40/**
samuel7a5691a2015-05-23 00:36:32 +080041 * Supports for querying tunnels. It's used by consumers.
jccd8697232015-05-05 14:42:23 +080042 */
samuel7a5691a2015-05-23 00:36:32 +080043@Command(scope = "onos", name = "tunnels", description = "Supports for querying tunnels."
jccd8697232015-05-05 14:42:23 +080044 + " It's used by consumers.")
Thomas Vachuskabf916ea2015-05-20 18:24:34 -070045public class TunnelQueryCommand extends AbstractShellCommand {
samuel7a5691a2015-05-23 00:36:32 +080046 @Option(name = "-s", aliases = "--src", description = "Source tunnel point."
jccd8697232015-05-05 14:42:23 +080047 + " Only supports for IpTunnelEndPoint and OpticalTunnelEndPoint as end point now."
48 + " If deletess a ODUK or OCH type tunnel, the formatter of this argument is DeviceId-PortNumber."
samuel7a5691a2015-05-23 00:36:32 +080049 + " Otherwise src means IP address.", required = false, multiValued = false)
jccd8697232015-05-05 14:42:23 +080050 String src = null;
samuel7a5691a2015-05-23 00:36:32 +080051 @Option(name = "-d", aliases = "--dst", description = "Destination tunnel point."
jccd8697232015-05-05 14:42:23 +080052 + " Only supports for IpTunnelEndPoint and OpticalTunnelEndPoint as end point now."
53 + " If deletess a ODUK or OCH type tunnel, the formatter of this argument is DeviceId-PortNumber."
samuel7a5691a2015-05-23 00:36:32 +080054 + " Otherwise dst means IP address.", required = false, multiValued = false)
jccd8697232015-05-05 14:42:23 +080055 String dst = null;
56
samuel7a5691a2015-05-23 00:36:32 +080057 @Option(name = "-t", aliases = "--type", description = "The type of tunnels,"
58 + " It includes MPLS, VLAN, VXLAN, GRE, ODUK, OCH", required = false, multiValued = false)
jccd8697232015-05-05 14:42:23 +080059 String type = null;
60
samuel7a5691a2015-05-23 00:36:32 +080061 @Option(name = "-i", aliases = "--tunnelId",
62 description = "the tunnel identity.", required = false, multiValued = false)
63 String tunnelId = null;
64
65 private static final String FMT = "tunnelId=%s, src=%s, dst=%s,"
jccd8697232015-05-05 14:42:23 +080066 + "type=%s, state=%s, producerName=%s, tunnelName=%s,"
samuel7a5691a2015-05-23 00:36:32 +080067 + "groupId=%s, path=%s%s";
jccd8697232015-05-05 14:42:23 +080068
69 @Override
70 protected void execute() {
samuel7a5691a2015-05-23 00:36:32 +080071 Tunnel.Type trueType = null;
jccd8697232015-05-05 14:42:23 +080072 TunnelService service = get(TunnelService.class);
73 ProviderId producerName = new ProviderId("default",
74 "org.onosproject.provider.tunnel.default");
samuel7a5691a2015-05-23 00:36:32 +080075 Collection<Tunnel> tunnelSet = null;
76 if (isNull(src) && isNull(dst) && isNull(type) && isNull(tunnelId)) {
77 tunnelSet = service.queryAllTunnels();
jccd8697232015-05-05 14:42:23 +080078 }
samuel7a5691a2015-05-23 00:36:32 +080079
80 if (!isNull(src) && !isNull(dst) && !isNull(type)) {
81 TunnelEndPoint srcPoint = null;
82 TunnelEndPoint dstPoint = null;
83 if ("MPLS".equals(type) || "VXLAN".equals(type)
84 || "GRE".equals(type)) {
85 srcPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress
86 .valueOf(src));
87 dstPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress
88 .valueOf(dst));
89 } else if ("VLAN".equals(type)) {
cheng fan00a406d2015-06-05 21:42:40 +080090 String[] srcArray = src.split("/");
91 String[] dstArray = dst.split("/");
samuel7a5691a2015-05-23 00:36:32 +080092 srcPoint = new DefaultOpticalTunnelEndPoint(
93 producerName,
94 Optional.of(DeviceId
95 .deviceId(srcArray[0])),
96 Optional.of(PortNumber
97 .portNumber(srcArray[1])),
98 null,
99 null,
100 OpticalLogicId
101 .logicId(0),
102 true);
103 dstPoint = new DefaultOpticalTunnelEndPoint(
104 producerName,
105 Optional.of(DeviceId
106 .deviceId(dstArray[0])),
107 Optional.of(PortNumber
108 .portNumber(dstArray[1])),
109 null,
110 null,
111 OpticalLogicId
112 .logicId(0),
113 true);
114 } else if ("ODUK".equals(type)) {
cheng fan00a406d2015-06-05 21:42:40 +0800115 String[] srcArray = src.split("/");
116 String[] dstArray = dst.split("/");
samuel7a5691a2015-05-23 00:36:32 +0800117 srcPoint = new DefaultOpticalTunnelEndPoint(
118 producerName,
119 Optional.of(DeviceId
120 .deviceId(srcArray[0])),
121 Optional.of(PortNumber
122 .portNumber(srcArray[1])),
123 null,
124 OpticalTunnelEndPoint.Type.LAMBDA,
125 OpticalLogicId
126 .logicId(0),
127 true);
128 dstPoint = new DefaultOpticalTunnelEndPoint(
129 producerName,
130 Optional.of(DeviceId
131 .deviceId(dstArray[0])),
132 Optional.of(PortNumber
133 .portNumber(dstArray[1])),
134 null,
135 OpticalTunnelEndPoint.Type.LAMBDA,
136 OpticalLogicId
137 .logicId(0),
138 true);
139 } else if ("OCH".equals(type)) {
cheng fan00a406d2015-06-05 21:42:40 +0800140 String[] srcArray = src.split("/");
141 String[] dstArray = dst.split("/");
samuel7a5691a2015-05-23 00:36:32 +0800142 srcPoint = new DefaultOpticalTunnelEndPoint(
143 producerName,
144 Optional.of(DeviceId
145 .deviceId(srcArray[0])),
146 Optional.of(PortNumber
147 .portNumber(srcArray[1])),
148 null,
149 OpticalTunnelEndPoint.Type.TIMESLOT,
150 OpticalLogicId
151 .logicId(0),
152 true);
153 dstPoint = new DefaultOpticalTunnelEndPoint(
154 producerName,
155 Optional.of(DeviceId
156 .deviceId(dstArray[0])),
157 Optional.of(PortNumber
158 .portNumber(dstArray[1])),
159 null,
160 OpticalTunnelEndPoint.Type.TIMESLOT,
161 OpticalLogicId
162 .logicId(0),
163 true);
164 } else {
165 print("Illegal tunnel type. Please input MPLS, VLAN, VXLAN, GRE, ODUK or OCH.");
166 return;
167 }
168 tunnelSet = service.queryTunnel(srcPoint, dstPoint);
169 }
170 if (!isNull(type)) {
171 if ("MPLS".equals(type)) {
172 trueType = Tunnel.Type.MPLS;
173 } else if ("VLAN".equals(type)) {
174 trueType = Tunnel.Type.VLAN;
175 } else if ("VXLAN".equals(type)) {
176 trueType = Tunnel.Type.VXLAN;
177 } else if ("GRE".equals(type)) {
178 trueType = Tunnel.Type.GRE;
179 } else if ("ODUK".equals(type)) {
180 trueType = Tunnel.Type.ODUK;
181 } else if ("OCH".equals(type)) {
182 trueType = Tunnel.Type.OCH;
183 } else {
184 print("Illegal tunnel type. Please input MPLS, VLAN, VXLAN, GRE, ODUK or OCH.");
185 return;
186 }
187 tunnelSet = service.queryTunnel(trueType);
188 }
189 if (!isNull(tunnelId)) {
190 TunnelId id = TunnelId.valueOf(tunnelId);
191 Tunnel tunnel = service.queryTunnel(id);
192 tunnelSet = new HashSet<Tunnel>();
193 tunnelSet.add(tunnel);
194 }
195 if (tunnelSet != null) {
196 for (Tunnel tunnel : tunnelSet) {
cheng fan00a406d2015-06-05 21:42:40 +0800197 print(FMT, tunnel.tunnelId().id(), tunnel.src().toString(), tunnel.dst().toString(),
samuel7a5691a2015-05-23 00:36:32 +0800198 tunnel.type(), tunnel.state(), tunnel.providerId(),
199 tunnel.tunnelName(), tunnel.groupId(),
200 showPath(tunnel.path()),
201 annotations(tunnel.annotations()));
202 }
jccd8697232015-05-05 14:42:23 +0800203 }
204 }
205
samuel7a5691a2015-05-23 00:36:32 +0800206 private String showPath(Path path) {
207 if (path == null) {
cheng fan93258c72015-06-02 23:42:32 +0800208 return "null";
samuel7a5691a2015-05-23 00:36:32 +0800209 }
210 StringBuilder builder = new StringBuilder("(");
211 for (Link link : path.links()) {
212 builder.append("(DeviceId:" + link.src().deviceId() + " Port:"
213 + link.src().port().toString());
214 builder.append(" DeviceId:" + link.dst().deviceId() + " Port:"
215 + link.dst().port().toString() + ")");
216 }
217 builder.append(annotations(path.annotations()) + ")");
218 return builder.toString();
219 }
220
221 private boolean isNull(String s) {
222 return s == null || "".equals(s);
223 }
jccd8697232015-05-05 14:42:23 +0800224}