blob: c1579794a1a62fe1785c35127aa791737c75dd9c [file] [log] [blame]
jccd8697232015-05-05 14:42:23 +08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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
20import org.apache.karaf.shell.commands.Argument;
21import org.apache.karaf.shell.commands.Command;
samuel7a5691a2015-05-23 00:36:32 +080022import org.apache.karaf.shell.commands.Option;
jccd8697232015-05-05 14:42:23 +080023import org.onlab.packet.IpAddress;
24import org.onosproject.cli.AbstractShellCommand;
25import org.onosproject.core.ApplicationId;
26import org.onosproject.core.DefaultApplicationId;
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;
samuel7a5691a2015-05-23 00:36:32 +080031import org.onosproject.incubator.net.tunnel.Tunnel;
Thomas Vachuskabf916ea2015-05-20 18:24:34 -070032import org.onosproject.incubator.net.tunnel.TunnelEndPoint;
samuel7a5691a2015-05-23 00:36:32 +080033import org.onosproject.incubator.net.tunnel.TunnelId;
34import org.onosproject.incubator.net.tunnel.TunnelName;
Thomas Vachuskabf916ea2015-05-20 18:24:34 -070035import 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;
jccd8697232015-05-05 14:42:23 +080039
40/**
samuel7a5691a2015-05-23 00:36:32 +080041 * Returns tunnels. It's used by consumers.
jccd8697232015-05-05 14:42:23 +080042 */
samuel7a5691a2015-05-23 00:36:32 +080043@Command(scope = "onos", name = "tunnel-return",
44description = "Returns tunnels. It's used by consumers.")
Thomas Vachuskabf916ea2015-05-20 18:24:34 -070045public class TunnelReturnCommand extends AbstractShellCommand {
samuel7a5691a2015-05-23 00:36:32 +080046 @Argument(index = 0, name = "consumerId",
47 description = "consumer id means application id.", required = true, multiValued = false)
jccd8697232015-05-05 14:42:23 +080048 String consumerId = null;
samuel7a5691a2015-05-23 00:36:32 +080049
50 @Option(name = "-s", aliases = "--src", description = "Source tunnel point."
jccd8697232015-05-05 14:42:23 +080051 + " 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."
samuel7a5691a2015-05-23 00:36:32 +080053 + " Otherwise src means IP address.", required = false, multiValued = false)
jccd8697232015-05-05 14:42:23 +080054 String src = null;
samuel7a5691a2015-05-23 00:36:32 +080055
56 @Option(name = "-d", aliases = "--dst", description = "Destination tunnel point."
jccd8697232015-05-05 14:42:23 +080057 + " Only supports for IpTunnelEndPoint and OpticalTunnelEndPoint as end point now."
58 + " If deletess a ODUK or OCH type tunnel, the formatter of this argument is DeviceId-PortNumber."
samuel7a5691a2015-05-23 00:36:32 +080059 + " Otherwise dst means IP address.", required = false, multiValued = false)
jccd8697232015-05-05 14:42:23 +080060 String dst = null;
61
samuel7a5691a2015-05-23 00:36:32 +080062 @Option(name = "-t", aliases = "--type", description = "The type of tunnels,"
63 + " It includes MPLS, VLAN, VXLAN, GRE, ODUK, OCH", required = false, multiValued = false)
jccd8697232015-05-05 14:42:23 +080064 String type = null;
65
samuel7a5691a2015-05-23 00:36:32 +080066 @Option(name = "-i", aliases = "--tunnelId",
67 description = "the tunnel identity.", required = false, multiValued = false)
68 String tunnelId = null;
69
70 @Option(name = "-n", aliases = "--tunnelName",
71 description = "The name of tunnels", required = false, multiValued = false)
72 String tunnelName = null;
73
jccd8697232015-05-05 14:42:23 +080074 @Override
75 protected void execute() {
samuel7a5691a2015-05-23 00:36:32 +080076 Tunnel.Type trueType = null;
jccd8697232015-05-05 14:42:23 +080077 TunnelService service = get(TunnelService.class);
78 ApplicationId appId = new DefaultApplicationId(1, consumerId);
79 ProviderId producerName = new ProviderId("default",
80 "org.onosproject.provider.tunnel.default");
samuel7a5691a2015-05-23 00:36:32 +080081 if (!isNull(src) && !isNull(dst) && !isNull(type)) {
82 TunnelEndPoint srcPoint = null;
83 TunnelEndPoint dstPoint = null;
84 if ("MPLS".equals(type)) {
85 trueType = Tunnel.Type.MPLS;
86 srcPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress
87 .valueOf(src));
88 dstPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress
89 .valueOf(dst));
90 } else if ("VXLAN".equals(type)) {
91 trueType = Tunnel.Type.VXLAN;
92 srcPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress
93 .valueOf(src));
94 dstPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress
95 .valueOf(dst));
96 } else if ("GRE".equals(type)) {
97 trueType = Tunnel.Type.GRE;
98 srcPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress
99 .valueOf(src));
100 dstPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress
101 .valueOf(dst));
102 } else if ("VLAN".equals(type)) {
103 trueType = Tunnel.Type.VLAN;
104 String[] srcArray = src.split("-");
105 String[] dstArray = dst.split("-");
106 srcPoint = new DefaultOpticalTunnelEndPoint(
107 producerName,
108 Optional.of(DeviceId
109 .deviceId(srcArray[0])),
110 Optional.of(PortNumber
111 .portNumber(srcArray[1])),
112 null,
113 null,
114 OpticalLogicId
115 .logicId(0),
116 true);
117 dstPoint = new DefaultOpticalTunnelEndPoint(
118 producerName,
119 Optional.of(DeviceId
120 .deviceId(dstArray[0])),
121 Optional.of(PortNumber
122 .portNumber(dstArray[1])),
123 null,
124 null,
125 OpticalLogicId
126 .logicId(0),
127 true);
128 } else if ("ODUK".equals(type)) {
129 trueType = Tunnel.Type.ODUK;
130 String[] srcArray = src.split("-");
131 String[] dstArray = dst.split("-");
132 srcPoint = new DefaultOpticalTunnelEndPoint(
133 producerName,
134 Optional.of(DeviceId
135 .deviceId(srcArray[0])),
136 Optional.of(PortNumber
137 .portNumber(srcArray[1])),
138 null,
139 OpticalTunnelEndPoint.Type.LAMBDA,
140 OpticalLogicId
141 .logicId(0),
142 true);
143 dstPoint = new DefaultOpticalTunnelEndPoint(
144 producerName,
145 Optional.of(DeviceId
146 .deviceId(dstArray[0])),
147 Optional.of(PortNumber
148 .portNumber(dstArray[1])),
149 null,
150 OpticalTunnelEndPoint.Type.LAMBDA,
151 OpticalLogicId
152 .logicId(0),
153 true);
154 } else if ("OCH".equals(type)) {
155 trueType = Tunnel.Type.OCH;
156 String[] srcArray = src.split("-");
157 String[] dstArray = dst.split("-");
158 srcPoint = new DefaultOpticalTunnelEndPoint(
159 producerName,
160 Optional.of(DeviceId
161 .deviceId(srcArray[0])),
162 Optional.of(PortNumber
163 .portNumber(srcArray[1])),
164 null,
165 OpticalTunnelEndPoint.Type.TIMESLOT,
166 OpticalLogicId
167 .logicId(0),
168 true);
169 dstPoint = new DefaultOpticalTunnelEndPoint(
170 producerName,
171 Optional.of(DeviceId
172 .deviceId(dstArray[0])),
173 Optional.of(PortNumber
174 .portNumber(dstArray[1])),
175 null,
176 OpticalTunnelEndPoint.Type.TIMESLOT,
177 OpticalLogicId
178 .logicId(0),
179 true);
180 } else {
181 print("Illegal tunnel type. Please input MPLS, VLAN, VXLAN, GRE, ODUK or OCH.");
182 return;
183 }
184 service.returnTunnel(appId, srcPoint, dstPoint, trueType);
jccd8697232015-05-05 14:42:23 +0800185 }
samuel7a5691a2015-05-23 00:36:32 +0800186 if (!isNull(tunnelId)) {
187 TunnelId id = TunnelId.valueOf(tunnelId);
188 service.returnTunnel(appId, id);
189 }
190 if (!isNull(tunnelName)) {
191 TunnelName name = TunnelName.tunnelName(tunnelName);
192 service.returnTunnel(appId, name);
193 }
jccd8697232015-05-05 14:42:23 +0800194 }
samuel7a5691a2015-05-23 00:36:32 +0800195 private boolean isNull(String s) {
196 return s == null || "".equals(s);
197 }
jccd8697232015-05-05 14:42:23 +0800198}