blob: 54a5a1b861a7c4f2aca97850fa93df1f1cae31c4 [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.Collection;
samuel7a5691a2015-05-23 00:36:32 +080019import java.util.HashSet;
jccd8697232015-05-05 14:42:23 +080020import java.util.Optional;
21
22import org.apache.karaf.shell.commands.Argument;
23import org.apache.karaf.shell.commands.Command;
samuel7a5691a2015-05-23 00:36:32 +080024import org.apache.karaf.shell.commands.Option;
jccd8697232015-05-05 14:42:23 +080025import org.onlab.packet.IpAddress;
26import org.onosproject.cli.AbstractShellCommand;
27import org.onosproject.core.ApplicationId;
28import org.onosproject.core.DefaultApplicationId;
Thomas Vachuskabf916ea2015-05-20 18:24:34 -070029import org.onosproject.incubator.net.tunnel.DefaultOpticalTunnelEndPoint;
30import org.onosproject.incubator.net.tunnel.IpTunnelEndPoint;
31import org.onosproject.incubator.net.tunnel.OpticalLogicId;
32import org.onosproject.incubator.net.tunnel.OpticalTunnelEndPoint;
33import org.onosproject.incubator.net.tunnel.Tunnel;
34import org.onosproject.incubator.net.tunnel.TunnelEndPoint;
samuel7a5691a2015-05-23 00:36:32 +080035import org.onosproject.incubator.net.tunnel.TunnelId;
36import org.onosproject.incubator.net.tunnel.TunnelName;
Thomas Vachuskabf916ea2015-05-20 18:24:34 -070037import org.onosproject.incubator.net.tunnel.TunnelService;
samuel7a5691a2015-05-23 00:36:32 +080038import org.onosproject.net.DeviceId;
39import org.onosproject.net.PortNumber;
40import org.onosproject.net.provider.ProviderId;
jccd8697232015-05-05 14:42:23 +080041
42/**
samuel7a5691a2015-05-23 00:36:32 +080043 * Borrows tunnels. It's used by consumers.
jccd8697232015-05-05 14:42:23 +080044 */
samuel7a5691a2015-05-23 00:36:32 +080045@Command(scope = "onos", name = "tunnel-borrow", description = "Borrows tunnels. It's used by consumers.")
Thomas Vachuskabf916ea2015-05-20 18:24:34 -070046public class TunnelBorrowCommand extends AbstractShellCommand {
samuel7a5691a2015-05-23 00:36:32 +080047
48 @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;
samuel7a5691a2015-05-23 00:36:32 +080067
68 @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;
jccd8697232015-05-05 14:42:23 +080075 private static final String FMT = "src=%s, dst=%s,"
76 + "type=%s, state=%s, producerName=%s, tunnelName=%s,"
77 + "groupId=%s";
78
79 @Override
80 protected void execute() {
samuel7a5691a2015-05-23 00:36:32 +080081 Collection<Tunnel> tunnelSet = null;
82 Tunnel.Type trueType = null;
jccd8697232015-05-05 14:42:23 +080083 TunnelService service = get(TunnelService.class);
84 ApplicationId appId = new DefaultApplicationId(1, consumerId);
85 ProviderId producerName = new ProviderId("default",
86 "org.onosproject.provider.tunnel.default");
samuel7a5691a2015-05-23 00:36:32 +080087 if (!isNull(src) && !isNull(dst) && !isNull(type)) {
88 TunnelEndPoint srcPoint = null;
89 TunnelEndPoint dstPoint = null;
90 if ("MPLS".equals(type)) {
91 trueType = Tunnel.Type.MPLS;
92 srcPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress
93 .valueOf(src));
94 dstPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress
95 .valueOf(dst));
96 } else if ("VXLAN".equals(type)) {
97 trueType = Tunnel.Type.VXLAN;
98 srcPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress
99 .valueOf(src));
100 dstPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress
101 .valueOf(dst));
102 } else if ("GRE".equals(type)) {
103 trueType = Tunnel.Type.GRE;
104 srcPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress
105 .valueOf(src));
106 dstPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress
107 .valueOf(dst));
108 } else if ("VLAN".equals(type)) {
109 trueType = Tunnel.Type.VLAN;
110 String[] srcArray = src.split("-");
111 String[] dstArray = dst.split("-");
112 srcPoint = new DefaultOpticalTunnelEndPoint(
113 producerName,
114 Optional.of(DeviceId
115 .deviceId(srcArray[0])),
116 Optional.of(PortNumber
117 .portNumber(srcArray[1])),
118 null,
119 null,
120 OpticalLogicId
121 .logicId(0),
122 true);
123 dstPoint = new DefaultOpticalTunnelEndPoint(
124 producerName,
125 Optional.of(DeviceId
126 .deviceId(dstArray[0])),
127 Optional.of(PortNumber
128 .portNumber(dstArray[1])),
129 null,
130 null,
131 OpticalLogicId
132 .logicId(0),
133 true);
134 } else if ("ODUK".equals(type)) {
135 trueType = Tunnel.Type.ODUK;
136 String[] srcArray = src.split("-");
137 String[] dstArray = dst.split("-");
138 srcPoint = new DefaultOpticalTunnelEndPoint(
139 producerName,
140 Optional.of(DeviceId
141 .deviceId(srcArray[0])),
142 Optional.of(PortNumber
143 .portNumber(srcArray[1])),
144 null,
145 OpticalTunnelEndPoint.Type.LAMBDA,
146 OpticalLogicId
147 .logicId(0),
148 true);
149 dstPoint = new DefaultOpticalTunnelEndPoint(
150 producerName,
151 Optional.of(DeviceId
152 .deviceId(dstArray[0])),
153 Optional.of(PortNumber
154 .portNumber(dstArray[1])),
155 null,
156 OpticalTunnelEndPoint.Type.LAMBDA,
157 OpticalLogicId
158 .logicId(0),
159 true);
160 } else if ("OCH".equals(type)) {
161 trueType = Tunnel.Type.OCH;
162 String[] srcArray = src.split("-");
163 String[] dstArray = dst.split("-");
164 srcPoint = new DefaultOpticalTunnelEndPoint(
165 producerName,
166 Optional.of(DeviceId
167 .deviceId(srcArray[0])),
168 Optional.of(PortNumber
169 .portNumber(srcArray[1])),
170 null,
171 OpticalTunnelEndPoint.Type.TIMESLOT,
172 OpticalLogicId
173 .logicId(0),
174 true);
175 dstPoint = new DefaultOpticalTunnelEndPoint(
176 producerName,
177 Optional.of(DeviceId
178 .deviceId(dstArray[0])),
179 Optional.of(PortNumber
180 .portNumber(dstArray[1])),
181 null,
182 OpticalTunnelEndPoint.Type.TIMESLOT,
183 OpticalLogicId
184 .logicId(0),
185 true);
186 } else {
187 print("Illegal tunnel type. Please input MPLS, VLAN, VXLAN, GRE, ODUK or OCH.");
188 return;
189 }
190 tunnelSet = service.borrowTunnel(appId, srcPoint, dstPoint, trueType);
jccd8697232015-05-05 14:42:23 +0800191 }
samuel7a5691a2015-05-23 00:36:32 +0800192 if (!isNull(tunnelId)) {
193 TunnelId id = TunnelId.valueOf(tunnelId);
194 Tunnel tunnel = service.borrowTunnel(appId, id);
195 tunnelSet = new HashSet<Tunnel>();
196 tunnelSet.add(tunnel);
197 }
198 if (!isNull(tunnelName)) {
199 TunnelName name = TunnelName.tunnelName(tunnelName);
200 tunnelSet = service.borrowTunnel(appId, name);
201 }
jccd8697232015-05-05 14:42:23 +0800202 for (Tunnel tunnel : tunnelSet) {
203 print(FMT, tunnel.src(), tunnel.dst(), tunnel.type(),
204 tunnel.state(), tunnel.providerId(), tunnel.tunnelName(),
205 tunnel.groupId());
206 }
207 }
208
samuel7a5691a2015-05-23 00:36:32 +0800209 private boolean isNull(String s) {
210 return s == null || "".equals(s);
211 }
jccd8697232015-05-05 14:42:23 +0800212}