blob: 8d683cbfbec245b876bc7f1e1d8cb5188300cd4b [file] [log] [blame]
samuel7a5691a2015-05-23 00:36:32 +08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
samuel7a5691a2015-05-23 00:36:32 +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 */
cheng fan7716ec92015-05-31 01:53:19 +080016package org.onosproject.cli.net;
samuel7a5691a2015-05-23 00:36:32 +080017
cheng fan00a406d2015-06-05 21:42:40 +080018import java.util.Collection;
samuel7a5691a2015-05-23 00:36:32 +080019import java.util.Optional;
20
Ray Milkeyd84f89b2018-08-17 14:54:17 -070021import org.apache.karaf.shell.api.action.Command;
22import org.apache.karaf.shell.api.action.lifecycle.Service;
23import org.apache.karaf.shell.api.action.Option;
samuel7a5691a2015-05-23 00:36:32 +080024import org.onlab.packet.IpAddress;
25import org.onosproject.cli.AbstractShellCommand;
26import org.onosproject.incubator.net.tunnel.DefaultOpticalTunnelEndPoint;
27import org.onosproject.incubator.net.tunnel.DefaultTunnelDescription;
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.TunnelDescription;
33import org.onosproject.incubator.net.tunnel.TunnelEndPoint;
34import org.onosproject.incubator.net.tunnel.TunnelId;
35import org.onosproject.incubator.net.tunnel.TunnelProvider;
cheng fan00a406d2015-06-05 21:42:40 +080036import org.onosproject.incubator.net.tunnel.TunnelService;
samuel7a5691a2015-05-23 00:36:32 +080037import org.onosproject.net.DeviceId;
38import org.onosproject.net.PortNumber;
39import org.onosproject.net.provider.ProviderId;
40
41/**
42 * Supports for removing tunnels. It's used by producers.
43 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070044@Service
samuel7a5691a2015-05-23 00:36:32 +080045@Command(scope = "onos", name = "tunnel-remove", description = "Supports for removing tunnels. It's used by producers.")
46public class TunnelRemoveCommand extends AbstractShellCommand {
47 @Option(name = "-s", aliases = "--src", description = "Source tunnel point."
48 + " Only supports for IpTunnelEndPoint and OpticalTunnelEndPoint as end point now."
49 + " If deletess a ODUK or OCH type tunnel, the formatter of this argument is DeviceId-PortNumber."
50 + " Otherwise src means IP address.", required = false, multiValued = false)
51 String src = null;
52 @Option(name = "-d", aliases = "--dst", description = "Destination tunnel point."
53 + " Only supports for IpTunnelEndPoint and OpticalTunnelEndPoint as end point now."
54 + " If deletess a ODUK or OCH type tunnel, the formatter of this argument is DeviceId-PortNumber."
55 + " Otherwise dst means IP address.", required = false, multiValued = false)
56 String dst = null;
57
58 @Option(name = "-t", aliases = "--type", description = "The type of tunnels,"
59 + " It includes MPLS, VLAN, VXLAN, GRE, ODUK, OCH", required = false, multiValued = false)
60 String type = null;
61
62 @Option(name = "-i", aliases = "--tunnelId",
63 description = "the tunnel identity.", required = false, multiValued = false)
64 String tunnelId = null;
65
66 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070067 protected void doExecute() {
samuel7a5691a2015-05-23 00:36:32 +080068 TunnelDescription tunnel = null;
69 TunnelProvider service = get(TunnelProvider.class);
70 ProviderId producerName = new ProviderId("default",
71 "org.onosproject.provider.tunnel.default");
72 if (!isNull(src) && !isNull(dst) && !isNull(type)) {
73 TunnelEndPoint srcPoint = null;
74 TunnelEndPoint dstPoint = null;
75 Tunnel.Type trueType = null;
76 if ("MPLS".equals(type)) {
77 trueType = Tunnel.Type.MPLS;
78 srcPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress
79 .valueOf(src));
80 dstPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress
81 .valueOf(dst));
82 } else if ("VLAN".equals(type)) {
83 trueType = Tunnel.Type.VLAN;
84 srcPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress
85 .valueOf(src));
86 dstPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress
87 .valueOf(dst));
88 } else if ("VXLAN".equals(type)) {
89 trueType = Tunnel.Type.VXLAN;
90 srcPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress
91 .valueOf(src));
92 dstPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress
93 .valueOf(dst));
94 } else if ("GRE".equals(type)) {
95 trueType = Tunnel.Type.GRE;
96 srcPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress
97 .valueOf(src));
98 dstPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress
99 .valueOf(dst));
100 } else if ("ODUK".equals(type)) {
101 trueType = Tunnel.Type.ODUK;
cheng fan00a406d2015-06-05 21:42:40 +0800102 String[] srcArray = src.split("/");
103 String[] dstArray = dst.split("/");
samuel7a5691a2015-05-23 00:36:32 +0800104 srcPoint = new DefaultOpticalTunnelEndPoint(
105 producerName,
106 Optional.of(DeviceId
107 .deviceId(srcArray[0])),
108 Optional.of(PortNumber
109 .portNumber(srcArray[1])),
110 null,
111 OpticalTunnelEndPoint.Type.LAMBDA,
112 OpticalLogicId
113 .logicId(0),
114 true);
115 dstPoint = new DefaultOpticalTunnelEndPoint(
116 producerName,
117 Optional.of(DeviceId
118 .deviceId(dstArray[0])),
119 Optional.of(PortNumber
120 .portNumber(dstArray[1])),
121 null,
122 OpticalTunnelEndPoint.Type.LAMBDA,
123 OpticalLogicId
124 .logicId(0),
125 true);
126 } else if ("OCH".equals(type)) {
127 trueType = Tunnel.Type.OCH;
cheng fan00a406d2015-06-05 21:42:40 +0800128 String[] srcArray = src.split("/");
129 String[] dstArray = dst.split("/");
samuel7a5691a2015-05-23 00:36:32 +0800130 srcPoint = new DefaultOpticalTunnelEndPoint(
131 producerName,
132 Optional.of(DeviceId
133 .deviceId(srcArray[0])),
134 Optional.of(PortNumber
135 .portNumber(srcArray[1])),
136 null,
137 OpticalTunnelEndPoint.Type.LAMBDA,
138 OpticalLogicId
139 .logicId(0),
140 true);
141 dstPoint = new DefaultOpticalTunnelEndPoint(
142 producerName,
143 Optional.of(DeviceId
144 .deviceId(dstArray[0])),
145 Optional.of(PortNumber
146 .portNumber(dstArray[1])),
147 null,
148 OpticalTunnelEndPoint.Type.LAMBDA,
149 OpticalLogicId
150 .logicId(0),
151 true);
152 } else {
153 print("Illegal tunnel type. Please input MPLS, VLAN, VXLAN, GRE, ODUK or OCH.");
154 return;
155 }
156
157 tunnel = new DefaultTunnelDescription(null, srcPoint, dstPoint,
158 trueType, null, producerName,
159 null, null);
160 service.tunnelRemoved(tunnel);
cheng fan00a406d2015-06-05 21:42:40 +0800161 return;
samuel7a5691a2015-05-23 00:36:32 +0800162 }
163 if (!isNull(tunnelId)) {
164 TunnelId id = TunnelId.valueOf(tunnelId);
165 tunnel = new DefaultTunnelDescription(id, null, null, null, null,
166 producerName, null, null);
167 service.tunnelRemoved(tunnel);
cheng fan00a406d2015-06-05 21:42:40 +0800168 return;
169 }
170
171 if (!isNull(type)) {
172 Tunnel.Type trueType = null;
173 Collection<Tunnel> tunnelSet = null;
174 TunnelService tunnelService = get(TunnelService.class);
175 if ("MPLS".equals(type)) {
176 trueType = Tunnel.Type.MPLS;
177 } else if ("VLAN".equals(type)) {
178 trueType = Tunnel.Type.VLAN;
179 } else if ("VXLAN".equals(type)) {
180 trueType = Tunnel.Type.VXLAN;
181 } else if ("GRE".equals(type)) {
182 trueType = Tunnel.Type.GRE;
183 } else if ("ODUK".equals(type)) {
184 trueType = Tunnel.Type.ODUK;
185 } else if ("OCH".equals(type)) {
186 trueType = Tunnel.Type.OCH;
187 } else {
188 print("Illegal tunnel type. Please input MPLS, VLAN, VXLAN, GRE, ODUK or OCH.");
189 return;
190 }
191 tunnelSet = tunnelService.queryTunnel(trueType);
192 if (tunnelSet != null) {
193 for (Tunnel tunnelTemp : tunnelSet) {
194 tunnel = new DefaultTunnelDescription(tunnelTemp.tunnelId(), null, null, null, null,
195 producerName, null, null);
196 service.tunnelRemoved(tunnel);
197 }
198 }
samuel7a5691a2015-05-23 00:36:32 +0800199 }
200 }
201
202 private boolean isNull(String s) {
203 return s == null || "".equals(s);
204 }
205}