blob: 76dd752df0dcd369e458d0958cb21e276980750d [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
Ray Milkeyd84f89b2018-08-17 14:54:17 -070022import org.apache.karaf.shell.api.action.Command;
23import org.apache.karaf.shell.api.action.lifecycle.Service;
24import org.apache.karaf.shell.api.action.Option;
jccd8697232015-05-05 14:42:23 +080025import org.onlab.packet.IpAddress;
26import org.onosproject.cli.AbstractShellCommand;
Thomas Vachuskabf916ea2015-05-20 18:24:34 -070027import org.onosproject.incubator.net.tunnel.DefaultOpticalTunnelEndPoint;
28import org.onosproject.incubator.net.tunnel.IpTunnelEndPoint;
29import org.onosproject.incubator.net.tunnel.OpticalLogicId;
30import org.onosproject.incubator.net.tunnel.OpticalTunnelEndPoint;
31import org.onosproject.incubator.net.tunnel.Tunnel;
32import org.onosproject.incubator.net.tunnel.TunnelEndPoint;
samuel7a5691a2015-05-23 00:36:32 +080033import org.onosproject.incubator.net.tunnel.TunnelId;
Thomas Vachuskabf916ea2015-05-20 18:24:34 -070034import org.onosproject.incubator.net.tunnel.TunnelService;
samuel7a5691a2015-05-23 00:36:32 +080035import org.onosproject.net.DeviceId;
36import org.onosproject.net.Link;
37import org.onosproject.net.Path;
38import org.onosproject.net.PortNumber;
39import org.onosproject.net.provider.ProviderId;
jccd8697232015-05-05 14:42:23 +080040
41/**
samuel7a5691a2015-05-23 00:36:32 +080042 * Supports for querying tunnels. It's used by consumers.
jccd8697232015-05-05 14:42:23 +080043 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070044@Service
samuel7a5691a2015-05-23 00:36:32 +080045@Command(scope = "onos", name = "tunnels", description = "Supports for querying tunnels."
jccd8697232015-05-05 14:42:23 +080046 + " It's used by consumers.")
Thomas Vachuskabf916ea2015-05-20 18:24:34 -070047public class TunnelQueryCommand extends AbstractShellCommand {
samuel7a5691a2015-05-23 00:36:32 +080048 @Option(name = "-s", aliases = "--src", description = "Source tunnel point."
jccd8697232015-05-05 14:42:23 +080049 + " Only supports for IpTunnelEndPoint and OpticalTunnelEndPoint as end point now."
50 + " If deletess a ODUK or OCH type tunnel, the formatter of this argument is DeviceId-PortNumber."
samuel7a5691a2015-05-23 00:36:32 +080051 + " Otherwise src means IP address.", required = false, multiValued = false)
jccd8697232015-05-05 14:42:23 +080052 String src = null;
samuel7a5691a2015-05-23 00:36:32 +080053 @Option(name = "-d", aliases = "--dst", description = "Destination tunnel point."
jccd8697232015-05-05 14:42:23 +080054 + " Only supports for IpTunnelEndPoint and OpticalTunnelEndPoint as end point now."
55 + " If deletess a ODUK or OCH type tunnel, the formatter of this argument is DeviceId-PortNumber."
samuel7a5691a2015-05-23 00:36:32 +080056 + " Otherwise dst means IP address.", required = false, multiValued = false)
jccd8697232015-05-05 14:42:23 +080057 String dst = null;
58
samuel7a5691a2015-05-23 00:36:32 +080059 @Option(name = "-t", aliases = "--type", description = "The type of tunnels,"
60 + " It includes MPLS, VLAN, VXLAN, GRE, ODUK, OCH", required = false, multiValued = false)
jccd8697232015-05-05 14:42:23 +080061 String type = null;
62
samuel7a5691a2015-05-23 00:36:32 +080063 @Option(name = "-i", aliases = "--tunnelId",
64 description = "the tunnel identity.", required = false, multiValued = false)
65 String tunnelId = null;
66
67 private static final String FMT = "tunnelId=%s, src=%s, dst=%s,"
jccd8697232015-05-05 14:42:23 +080068 + "type=%s, state=%s, producerName=%s, tunnelName=%s,"
samuel7a5691a2015-05-23 00:36:32 +080069 + "groupId=%s, path=%s%s";
jccd8697232015-05-05 14:42:23 +080070
71 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070072 protected void doExecute() {
samuel7a5691a2015-05-23 00:36:32 +080073 Tunnel.Type trueType = null;
jccd8697232015-05-05 14:42:23 +080074 TunnelService service = get(TunnelService.class);
75 ProviderId producerName = new ProviderId("default",
76 "org.onosproject.provider.tunnel.default");
samuel7a5691a2015-05-23 00:36:32 +080077 Collection<Tunnel> tunnelSet = null;
78 if (isNull(src) && isNull(dst) && isNull(type) && isNull(tunnelId)) {
79 tunnelSet = service.queryAllTunnels();
jccd8697232015-05-05 14:42:23 +080080 }
samuel7a5691a2015-05-23 00:36:32 +080081
82 if (!isNull(src) && !isNull(dst) && !isNull(type)) {
83 TunnelEndPoint srcPoint = null;
84 TunnelEndPoint dstPoint = null;
85 if ("MPLS".equals(type) || "VXLAN".equals(type)
86 || "GRE".equals(type)) {
87 srcPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress
88 .valueOf(src));
89 dstPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress
90 .valueOf(dst));
91 } else if ("VLAN".equals(type)) {
cheng fan00a406d2015-06-05 21:42:40 +080092 String[] srcArray = src.split("/");
93 String[] dstArray = dst.split("/");
samuel7a5691a2015-05-23 00:36:32 +080094 srcPoint = new DefaultOpticalTunnelEndPoint(
95 producerName,
96 Optional.of(DeviceId
97 .deviceId(srcArray[0])),
98 Optional.of(PortNumber
99 .portNumber(srcArray[1])),
100 null,
101 null,
102 OpticalLogicId
103 .logicId(0),
104 true);
105 dstPoint = new DefaultOpticalTunnelEndPoint(
106 producerName,
107 Optional.of(DeviceId
108 .deviceId(dstArray[0])),
109 Optional.of(PortNumber
110 .portNumber(dstArray[1])),
111 null,
112 null,
113 OpticalLogicId
114 .logicId(0),
115 true);
116 } else if ("ODUK".equals(type)) {
cheng fan00a406d2015-06-05 21:42:40 +0800117 String[] srcArray = src.split("/");
118 String[] dstArray = dst.split("/");
samuel7a5691a2015-05-23 00:36:32 +0800119 srcPoint = new DefaultOpticalTunnelEndPoint(
120 producerName,
121 Optional.of(DeviceId
122 .deviceId(srcArray[0])),
123 Optional.of(PortNumber
124 .portNumber(srcArray[1])),
125 null,
126 OpticalTunnelEndPoint.Type.LAMBDA,
127 OpticalLogicId
128 .logicId(0),
129 true);
130 dstPoint = new DefaultOpticalTunnelEndPoint(
131 producerName,
132 Optional.of(DeviceId
133 .deviceId(dstArray[0])),
134 Optional.of(PortNumber
135 .portNumber(dstArray[1])),
136 null,
137 OpticalTunnelEndPoint.Type.LAMBDA,
138 OpticalLogicId
139 .logicId(0),
140 true);
141 } else if ("OCH".equals(type)) {
cheng fan00a406d2015-06-05 21:42:40 +0800142 String[] srcArray = src.split("/");
143 String[] dstArray = dst.split("/");
samuel7a5691a2015-05-23 00:36:32 +0800144 srcPoint = new DefaultOpticalTunnelEndPoint(
145 producerName,
146 Optional.of(DeviceId
147 .deviceId(srcArray[0])),
148 Optional.of(PortNumber
149 .portNumber(srcArray[1])),
150 null,
151 OpticalTunnelEndPoint.Type.TIMESLOT,
152 OpticalLogicId
153 .logicId(0),
154 true);
155 dstPoint = new DefaultOpticalTunnelEndPoint(
156 producerName,
157 Optional.of(DeviceId
158 .deviceId(dstArray[0])),
159 Optional.of(PortNumber
160 .portNumber(dstArray[1])),
161 null,
162 OpticalTunnelEndPoint.Type.TIMESLOT,
163 OpticalLogicId
164 .logicId(0),
165 true);
166 } else {
167 print("Illegal tunnel type. Please input MPLS, VLAN, VXLAN, GRE, ODUK or OCH.");
168 return;
169 }
170 tunnelSet = service.queryTunnel(srcPoint, dstPoint);
171 }
172 if (!isNull(type)) {
173 if ("MPLS".equals(type)) {
174 trueType = Tunnel.Type.MPLS;
175 } else if ("VLAN".equals(type)) {
176 trueType = Tunnel.Type.VLAN;
177 } else if ("VXLAN".equals(type)) {
178 trueType = Tunnel.Type.VXLAN;
179 } else if ("GRE".equals(type)) {
180 trueType = Tunnel.Type.GRE;
181 } else if ("ODUK".equals(type)) {
182 trueType = Tunnel.Type.ODUK;
183 } else if ("OCH".equals(type)) {
184 trueType = Tunnel.Type.OCH;
185 } else {
186 print("Illegal tunnel type. Please input MPLS, VLAN, VXLAN, GRE, ODUK or OCH.");
187 return;
188 }
189 tunnelSet = service.queryTunnel(trueType);
190 }
191 if (!isNull(tunnelId)) {
192 TunnelId id = TunnelId.valueOf(tunnelId);
193 Tunnel tunnel = service.queryTunnel(id);
194 tunnelSet = new HashSet<Tunnel>();
195 tunnelSet.add(tunnel);
196 }
197 if (tunnelSet != null) {
198 for (Tunnel tunnel : tunnelSet) {
cheng fan00a406d2015-06-05 21:42:40 +0800199 print(FMT, tunnel.tunnelId().id(), tunnel.src().toString(), tunnel.dst().toString(),
samuel7a5691a2015-05-23 00:36:32 +0800200 tunnel.type(), tunnel.state(), tunnel.providerId(),
201 tunnel.tunnelName(), tunnel.groupId(),
202 showPath(tunnel.path()),
203 annotations(tunnel.annotations()));
204 }
jccd8697232015-05-05 14:42:23 +0800205 }
206 }
207
samuel7a5691a2015-05-23 00:36:32 +0800208 private String showPath(Path path) {
209 if (path == null) {
cheng fan93258c72015-06-02 23:42:32 +0800210 return "null";
samuel7a5691a2015-05-23 00:36:32 +0800211 }
212 StringBuilder builder = new StringBuilder("(");
213 for (Link link : path.links()) {
214 builder.append("(DeviceId:" + link.src().deviceId() + " Port:"
215 + link.src().port().toString());
216 builder.append(" DeviceId:" + link.dst().deviceId() + " Port:"
217 + link.dst().port().toString() + ")");
218 }
219 builder.append(annotations(path.annotations()) + ")");
220 return builder.toString();
221 }
222
223 private boolean isNull(String s) {
224 return s == null || "".equals(s);
225 }
jccd8697232015-05-05 14:42:23 +0800226}