blob: 6cd218e5c48dfdf5caa62efa8717cc79ccbd2bf3 [file] [log] [blame]
samuel7a5691a2015-05-23 00:36:32 +08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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
21import org.apache.karaf.shell.commands.Command;
22import org.apache.karaf.shell.commands.Option;
23import org.onlab.packet.IpAddress;
24import org.onosproject.cli.AbstractShellCommand;
25import org.onosproject.incubator.net.tunnel.DefaultOpticalTunnelEndPoint;
26import org.onosproject.incubator.net.tunnel.DefaultTunnelDescription;
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.TunnelDescription;
32import org.onosproject.incubator.net.tunnel.TunnelEndPoint;
33import org.onosproject.incubator.net.tunnel.TunnelId;
34import org.onosproject.incubator.net.tunnel.TunnelProvider;
cheng fan00a406d2015-06-05 21:42:40 +080035import org.onosproject.incubator.net.tunnel.TunnelService;
samuel7a5691a2015-05-23 00:36:32 +080036import org.onosproject.net.DeviceId;
37import org.onosproject.net.PortNumber;
38import org.onosproject.net.provider.ProviderId;
39
40/**
41 * Supports for removing tunnels. It's used by producers.
42 */
43@Command(scope = "onos", name = "tunnel-remove", description = "Supports for removing tunnels. It's used by producers.")
44public class TunnelRemoveCommand extends AbstractShellCommand {
45 @Option(name = "-s", aliases = "--src", description = "Source tunnel point."
46 + " Only supports for IpTunnelEndPoint and OpticalTunnelEndPoint as end point now."
47 + " If deletess a ODUK or OCH type tunnel, the formatter of this argument is DeviceId-PortNumber."
48 + " Otherwise src means IP address.", required = false, multiValued = false)
49 String src = null;
50 @Option(name = "-d", aliases = "--dst", description = "Destination tunnel point."
51 + " Only supports for IpTunnelEndPoint and OpticalTunnelEndPoint as end point now."
52 + " If deletess a ODUK or OCH type tunnel, the formatter of this argument is DeviceId-PortNumber."
53 + " Otherwise dst means IP address.", required = false, multiValued = false)
54 String dst = null;
55
56 @Option(name = "-t", aliases = "--type", description = "The type of tunnels,"
57 + " It includes MPLS, VLAN, VXLAN, GRE, ODUK, OCH", required = false, multiValued = false)
58 String type = null;
59
60 @Option(name = "-i", aliases = "--tunnelId",
61 description = "the tunnel identity.", required = false, multiValued = false)
62 String tunnelId = null;
63
64 @Override
65 protected void execute() {
66 TunnelDescription tunnel = null;
67 TunnelProvider service = get(TunnelProvider.class);
68 ProviderId producerName = new ProviderId("default",
69 "org.onosproject.provider.tunnel.default");
70 if (!isNull(src) && !isNull(dst) && !isNull(type)) {
71 TunnelEndPoint srcPoint = null;
72 TunnelEndPoint dstPoint = null;
73 Tunnel.Type trueType = null;
74 if ("MPLS".equals(type)) {
75 trueType = Tunnel.Type.MPLS;
76 srcPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress
77 .valueOf(src));
78 dstPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress
79 .valueOf(dst));
80 } else if ("VLAN".equals(type)) {
81 trueType = Tunnel.Type.VLAN;
82 srcPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress
83 .valueOf(src));
84 dstPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress
85 .valueOf(dst));
86 } else if ("VXLAN".equals(type)) {
87 trueType = Tunnel.Type.VXLAN;
88 srcPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress
89 .valueOf(src));
90 dstPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress
91 .valueOf(dst));
92 } else if ("GRE".equals(type)) {
93 trueType = Tunnel.Type.GRE;
94 srcPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress
95 .valueOf(src));
96 dstPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress
97 .valueOf(dst));
98 } else if ("ODUK".equals(type)) {
99 trueType = Tunnel.Type.ODUK;
cheng fan00a406d2015-06-05 21:42:40 +0800100 String[] srcArray = src.split("/");
101 String[] dstArray = dst.split("/");
samuel7a5691a2015-05-23 00:36:32 +0800102 srcPoint = new DefaultOpticalTunnelEndPoint(
103 producerName,
104 Optional.of(DeviceId
105 .deviceId(srcArray[0])),
106 Optional.of(PortNumber
107 .portNumber(srcArray[1])),
108 null,
109 OpticalTunnelEndPoint.Type.LAMBDA,
110 OpticalLogicId
111 .logicId(0),
112 true);
113 dstPoint = new DefaultOpticalTunnelEndPoint(
114 producerName,
115 Optional.of(DeviceId
116 .deviceId(dstArray[0])),
117 Optional.of(PortNumber
118 .portNumber(dstArray[1])),
119 null,
120 OpticalTunnelEndPoint.Type.LAMBDA,
121 OpticalLogicId
122 .logicId(0),
123 true);
124 } else if ("OCH".equals(type)) {
125 trueType = Tunnel.Type.OCH;
cheng fan00a406d2015-06-05 21:42:40 +0800126 String[] srcArray = src.split("/");
127 String[] dstArray = dst.split("/");
samuel7a5691a2015-05-23 00:36:32 +0800128 srcPoint = new DefaultOpticalTunnelEndPoint(
129 producerName,
130 Optional.of(DeviceId
131 .deviceId(srcArray[0])),
132 Optional.of(PortNumber
133 .portNumber(srcArray[1])),
134 null,
135 OpticalTunnelEndPoint.Type.LAMBDA,
136 OpticalLogicId
137 .logicId(0),
138 true);
139 dstPoint = new DefaultOpticalTunnelEndPoint(
140 producerName,
141 Optional.of(DeviceId
142 .deviceId(dstArray[0])),
143 Optional.of(PortNumber
144 .portNumber(dstArray[1])),
145 null,
146 OpticalTunnelEndPoint.Type.LAMBDA,
147 OpticalLogicId
148 .logicId(0),
149 true);
150 } else {
151 print("Illegal tunnel type. Please input MPLS, VLAN, VXLAN, GRE, ODUK or OCH.");
152 return;
153 }
154
155 tunnel = new DefaultTunnelDescription(null, srcPoint, dstPoint,
156 trueType, null, producerName,
157 null, null);
158 service.tunnelRemoved(tunnel);
cheng fan00a406d2015-06-05 21:42:40 +0800159 return;
samuel7a5691a2015-05-23 00:36:32 +0800160 }
161 if (!isNull(tunnelId)) {
162 TunnelId id = TunnelId.valueOf(tunnelId);
163 tunnel = new DefaultTunnelDescription(id, null, null, null, null,
164 producerName, null, null);
165 service.tunnelRemoved(tunnel);
cheng fan00a406d2015-06-05 21:42:40 +0800166 return;
167 }
168
169 if (!isNull(type)) {
170 Tunnel.Type trueType = null;
171 Collection<Tunnel> tunnelSet = null;
172 TunnelService tunnelService = get(TunnelService.class);
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 = tunnelService.queryTunnel(trueType);
190 if (tunnelSet != null) {
191 for (Tunnel tunnelTemp : tunnelSet) {
192 tunnel = new DefaultTunnelDescription(tunnelTemp.tunnelId(), null, null, null, null,
193 producerName, null, null);
194 service.tunnelRemoved(tunnel);
195 }
196 }
samuel7a5691a2015-05-23 00:36:32 +0800197 }
198 }
199
200 private boolean isNull(String s) {
201 return s == null || "".equals(s);
202 }
203}