blob: cab46f92d5155ca76afccf86ed0bca672e5fdeab [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.Optional;
19
Ray Milkeyd84f89b2018-08-17 14:54:17 -070020import org.apache.karaf.shell.api.action.Argument;
21import org.apache.karaf.shell.api.action.Command;
22import org.apache.karaf.shell.api.action.lifecycle.Service;
23import org.apache.karaf.shell.api.action.Option;
jccd8697232015-05-05 14:42:23 +080024import org.onlab.packet.IpAddress;
25import org.onosproject.cli.AbstractShellCommand;
26import org.onosproject.core.ApplicationId;
27import org.onosproject.core.DefaultApplicationId;
Thomas Vachuskabf916ea2015-05-20 18:24:34 -070028import org.onosproject.incubator.net.tunnel.DefaultOpticalTunnelEndPoint;
29import org.onosproject.incubator.net.tunnel.IpTunnelEndPoint;
30import org.onosproject.incubator.net.tunnel.OpticalLogicId;
31import org.onosproject.incubator.net.tunnel.OpticalTunnelEndPoint;
samuel7a5691a2015-05-23 00:36:32 +080032import org.onosproject.incubator.net.tunnel.Tunnel;
Thomas Vachuskabf916ea2015-05-20 18:24:34 -070033import org.onosproject.incubator.net.tunnel.TunnelEndPoint;
samuel7a5691a2015-05-23 00:36:32 +080034import org.onosproject.incubator.net.tunnel.TunnelId;
35import org.onosproject.incubator.net.tunnel.TunnelName;
Thomas Vachuskabf916ea2015-05-20 18:24:34 -070036import 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;
jccd8697232015-05-05 14:42:23 +080040
41/**
samuel7a5691a2015-05-23 00:36:32 +080042 * Returns 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 = "tunnel-return",
46description = "Returns tunnels. It's used by consumers.")
Thomas Vachuskabf916ea2015-05-20 18:24:34 -070047public class TunnelReturnCommand extends AbstractShellCommand {
samuel7a5691a2015-05-23 00:36:32 +080048 @Argument(index = 0, name = "consumerId",
49 description = "consumer id means application id.", required = true, multiValued = false)
jccd8697232015-05-05 14:42:23 +080050 String consumerId = null;
samuel7a5691a2015-05-23 00:36:32 +080051
52 @Option(name = "-s", aliases = "--src", description = "Source tunnel point."
jccd8697232015-05-05 14:42:23 +080053 + " 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."
samuel7a5691a2015-05-23 00:36:32 +080055 + " Otherwise src means IP address.", required = false, multiValued = false)
jccd8697232015-05-05 14:42:23 +080056 String src = null;
samuel7a5691a2015-05-23 00:36:32 +080057
58 @Option(name = "-d", aliases = "--dst", description = "Destination tunnel point."
jccd8697232015-05-05 14:42:23 +080059 + " Only supports for IpTunnelEndPoint and OpticalTunnelEndPoint as end point now."
60 + " If deletess a ODUK or OCH type tunnel, the formatter of this argument is DeviceId-PortNumber."
samuel7a5691a2015-05-23 00:36:32 +080061 + " Otherwise dst means IP address.", required = false, multiValued = false)
jccd8697232015-05-05 14:42:23 +080062 String dst = null;
63
samuel7a5691a2015-05-23 00:36:32 +080064 @Option(name = "-t", aliases = "--type", description = "The type of tunnels,"
65 + " It includes MPLS, VLAN, VXLAN, GRE, ODUK, OCH", required = false, multiValued = false)
jccd8697232015-05-05 14:42:23 +080066 String type = null;
67
samuel7a5691a2015-05-23 00:36:32 +080068 @Option(name = "-i", aliases = "--tunnelId",
69 description = "the tunnel identity.", required = false, multiValued = false)
70 String tunnelId = null;
71
72 @Option(name = "-n", aliases = "--tunnelName",
73 description = "The name of tunnels", required = false, multiValued = false)
74 String tunnelName = null;
75
jccd8697232015-05-05 14:42:23 +080076 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070077 protected void doExecute() {
samuel7a5691a2015-05-23 00:36:32 +080078 Tunnel.Type trueType = null;
jccd8697232015-05-05 14:42:23 +080079 TunnelService service = get(TunnelService.class);
80 ApplicationId appId = new DefaultApplicationId(1, consumerId);
81 ProviderId producerName = new ProviderId("default",
82 "org.onosproject.provider.tunnel.default");
samuel7a5691a2015-05-23 00:36:32 +080083 if (!isNull(src) && !isNull(dst) && !isNull(type)) {
84 TunnelEndPoint srcPoint = null;
85 TunnelEndPoint dstPoint = null;
86 if ("MPLS".equals(type)) {
87 trueType = Tunnel.Type.MPLS;
88 srcPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress
89 .valueOf(src));
90 dstPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress
91 .valueOf(dst));
92 } else if ("VXLAN".equals(type)) {
93 trueType = Tunnel.Type.VXLAN;
94 srcPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress
95 .valueOf(src));
96 dstPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress
97 .valueOf(dst));
98 } else if ("GRE".equals(type)) {
99 trueType = Tunnel.Type.GRE;
100 srcPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress
101 .valueOf(src));
102 dstPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress
103 .valueOf(dst));
104 } else if ("VLAN".equals(type)) {
105 trueType = Tunnel.Type.VLAN;
106 String[] srcArray = src.split("-");
107 String[] dstArray = dst.split("-");
108 srcPoint = new DefaultOpticalTunnelEndPoint(
109 producerName,
110 Optional.of(DeviceId
111 .deviceId(srcArray[0])),
112 Optional.of(PortNumber
113 .portNumber(srcArray[1])),
114 null,
115 null,
116 OpticalLogicId
117 .logicId(0),
118 true);
119 dstPoint = new DefaultOpticalTunnelEndPoint(
120 producerName,
121 Optional.of(DeviceId
122 .deviceId(dstArray[0])),
123 Optional.of(PortNumber
124 .portNumber(dstArray[1])),
125 null,
126 null,
127 OpticalLogicId
128 .logicId(0),
129 true);
130 } else if ("ODUK".equals(type)) {
131 trueType = Tunnel.Type.ODUK;
132 String[] srcArray = src.split("-");
133 String[] dstArray = dst.split("-");
134 srcPoint = new DefaultOpticalTunnelEndPoint(
135 producerName,
136 Optional.of(DeviceId
137 .deviceId(srcArray[0])),
138 Optional.of(PortNumber
139 .portNumber(srcArray[1])),
140 null,
141 OpticalTunnelEndPoint.Type.LAMBDA,
142 OpticalLogicId
143 .logicId(0),
144 true);
145 dstPoint = new DefaultOpticalTunnelEndPoint(
146 producerName,
147 Optional.of(DeviceId
148 .deviceId(dstArray[0])),
149 Optional.of(PortNumber
150 .portNumber(dstArray[1])),
151 null,
152 OpticalTunnelEndPoint.Type.LAMBDA,
153 OpticalLogicId
154 .logicId(0),
155 true);
156 } else if ("OCH".equals(type)) {
157 trueType = Tunnel.Type.OCH;
158 String[] srcArray = src.split("-");
159 String[] dstArray = dst.split("-");
160 srcPoint = new DefaultOpticalTunnelEndPoint(
161 producerName,
162 Optional.of(DeviceId
163 .deviceId(srcArray[0])),
164 Optional.of(PortNumber
165 .portNumber(srcArray[1])),
166 null,
167 OpticalTunnelEndPoint.Type.TIMESLOT,
168 OpticalLogicId
169 .logicId(0),
170 true);
171 dstPoint = new DefaultOpticalTunnelEndPoint(
172 producerName,
173 Optional.of(DeviceId
174 .deviceId(dstArray[0])),
175 Optional.of(PortNumber
176 .portNumber(dstArray[1])),
177 null,
178 OpticalTunnelEndPoint.Type.TIMESLOT,
179 OpticalLogicId
180 .logicId(0),
181 true);
182 } else {
183 print("Illegal tunnel type. Please input MPLS, VLAN, VXLAN, GRE, ODUK or OCH.");
184 return;
185 }
186 service.returnTunnel(appId, srcPoint, dstPoint, trueType);
jccd8697232015-05-05 14:42:23 +0800187 }
samuel7a5691a2015-05-23 00:36:32 +0800188 if (!isNull(tunnelId)) {
189 TunnelId id = TunnelId.valueOf(tunnelId);
190 service.returnTunnel(appId, id);
191 }
192 if (!isNull(tunnelName)) {
193 TunnelName name = TunnelName.tunnelName(tunnelName);
194 service.returnTunnel(appId, name);
195 }
jccd8697232015-05-05 14:42:23 +0800196 }
samuel7a5691a2015-05-23 00:36:32 +0800197 private boolean isNull(String s) {
198 return s == null || "".equals(s);
199 }
jccd8697232015-05-05 14:42:23 +0800200}