blob: 4934c11015469a05f7e665728ec6121670294fe2 [file] [log] [blame]
jccd8697232015-05-05 14:42:23 +08001/*
Thomas Vachuska52f2cd12018-11-08 21:20:04 -08002 * Copyright 2018-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 */
Thomas Vachuska52f2cd12018-11-08 21:20:04 -080016package org.onosproject.incubator.net.tunnel.cli;
jccd8697232015-05-05 14:42:23 +080017
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
Ray Milkeyd84f89b2018-08-17 14:54:17 -070022import org.apache.karaf.shell.api.action.Argument;
23import org.apache.karaf.shell.api.action.Command;
24import org.apache.karaf.shell.api.action.lifecycle.Service;
25import org.apache.karaf.shell.api.action.Option;
jccd8697232015-05-05 14:42:23 +080026import org.onlab.packet.IpAddress;
27import org.onosproject.cli.AbstractShellCommand;
28import org.onosproject.core.ApplicationId;
29import org.onosproject.core.DefaultApplicationId;
Thomas Vachuskabf916ea2015-05-20 18:24:34 -070030import org.onosproject.incubator.net.tunnel.DefaultOpticalTunnelEndPoint;
31import org.onosproject.incubator.net.tunnel.IpTunnelEndPoint;
32import org.onosproject.incubator.net.tunnel.OpticalLogicId;
33import org.onosproject.incubator.net.tunnel.OpticalTunnelEndPoint;
34import org.onosproject.incubator.net.tunnel.Tunnel;
35import org.onosproject.incubator.net.tunnel.TunnelEndPoint;
samuel7a5691a2015-05-23 00:36:32 +080036import org.onosproject.incubator.net.tunnel.TunnelId;
37import org.onosproject.incubator.net.tunnel.TunnelName;
Thomas Vachuskabf916ea2015-05-20 18:24:34 -070038import org.onosproject.incubator.net.tunnel.TunnelService;
samuel7a5691a2015-05-23 00:36:32 +080039import org.onosproject.net.DeviceId;
40import org.onosproject.net.PortNumber;
41import org.onosproject.net.provider.ProviderId;
jccd8697232015-05-05 14:42:23 +080042
43/**
samuel7a5691a2015-05-23 00:36:32 +080044 * Borrows tunnels. It's used by consumers.
jccd8697232015-05-05 14:42:23 +080045 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070046@Service
samuel7a5691a2015-05-23 00:36:32 +080047@Command(scope = "onos", name = "tunnel-borrow", description = "Borrows tunnels. It's used by consumers.")
Thomas Vachuskabf916ea2015-05-20 18:24:34 -070048public class TunnelBorrowCommand extends AbstractShellCommand {
samuel7a5691a2015-05-23 00:36:32 +080049
50 @Argument(index = 0, name = "consumerId",
51 description = "consumer id means application id.", required = true, multiValued = false)
jccd8697232015-05-05 14:42:23 +080052 String consumerId = null;
samuel7a5691a2015-05-23 00:36:32 +080053
54 @Option(name = "-s", aliases = "--src", description = "Source tunnel point."
jccd8697232015-05-05 14:42:23 +080055 + " Only supports for IpTunnelEndPoint and OpticalTunnelEndPoint as end point now."
56 + " If deletess a ODUK or OCH type tunnel, the formatter of this argument is DeviceId-PortNumber."
samuel7a5691a2015-05-23 00:36:32 +080057 + " Otherwise src means IP address.", required = false, multiValued = false)
jccd8697232015-05-05 14:42:23 +080058 String src = null;
samuel7a5691a2015-05-23 00:36:32 +080059
60 @Option(name = "-d", aliases = "--dst", description = "Destination tunnel point."
jccd8697232015-05-05 14:42:23 +080061 + " Only supports for IpTunnelEndPoint and OpticalTunnelEndPoint as end point now."
62 + " If deletess a ODUK or OCH type tunnel, the formatter of this argument is DeviceId-PortNumber."
samuel7a5691a2015-05-23 00:36:32 +080063 + " Otherwise dst means IP address.", required = false, multiValued = false)
jccd8697232015-05-05 14:42:23 +080064 String dst = null;
65
samuel7a5691a2015-05-23 00:36:32 +080066 @Option(name = "-t", aliases = "--type", description = "The type of tunnels,"
67 + " It includes MPLS, VLAN, VXLAN, GRE, ODUK, OCH", required = false, multiValued = false)
jccd8697232015-05-05 14:42:23 +080068 String type = null;
samuel7a5691a2015-05-23 00:36:32 +080069
70 @Option(name = "-i", aliases = "--tunnelId",
71 description = "the tunnel identity.", required = false, multiValued = false)
72 String tunnelId = null;
73
74 @Option(name = "-n", aliases = "--tunnelName",
75 description = "The name of tunnels", required = false, multiValued = false)
76 String tunnelName = null;
jccd8697232015-05-05 14:42:23 +080077 private static final String FMT = "src=%s, dst=%s,"
78 + "type=%s, state=%s, producerName=%s, tunnelName=%s,"
79 + "groupId=%s";
80
81 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070082 protected void doExecute() {
samuel7a5691a2015-05-23 00:36:32 +080083 Collection<Tunnel> tunnelSet = null;
84 Tunnel.Type trueType = null;
jccd8697232015-05-05 14:42:23 +080085 TunnelService service = get(TunnelService.class);
86 ApplicationId appId = new DefaultApplicationId(1, consumerId);
87 ProviderId producerName = new ProviderId("default",
88 "org.onosproject.provider.tunnel.default");
samuel7a5691a2015-05-23 00:36:32 +080089 if (!isNull(src) && !isNull(dst) && !isNull(type)) {
90 TunnelEndPoint srcPoint = null;
91 TunnelEndPoint dstPoint = null;
92 if ("MPLS".equals(type)) {
93 trueType = Tunnel.Type.MPLS;
94 srcPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress
95 .valueOf(src));
96 dstPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress
97 .valueOf(dst));
98 } else if ("VXLAN".equals(type)) {
99 trueType = Tunnel.Type.VXLAN;
100 srcPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress
101 .valueOf(src));
102 dstPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress
103 .valueOf(dst));
104 } else if ("GRE".equals(type)) {
105 trueType = Tunnel.Type.GRE;
106 srcPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress
107 .valueOf(src));
108 dstPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress
109 .valueOf(dst));
110 } else if ("VLAN".equals(type)) {
111 trueType = Tunnel.Type.VLAN;
112 String[] srcArray = src.split("-");
113 String[] dstArray = dst.split("-");
114 srcPoint = new DefaultOpticalTunnelEndPoint(
115 producerName,
116 Optional.of(DeviceId
117 .deviceId(srcArray[0])),
118 Optional.of(PortNumber
119 .portNumber(srcArray[1])),
120 null,
121 null,
122 OpticalLogicId
123 .logicId(0),
124 true);
125 dstPoint = new DefaultOpticalTunnelEndPoint(
126 producerName,
127 Optional.of(DeviceId
128 .deviceId(dstArray[0])),
129 Optional.of(PortNumber
130 .portNumber(dstArray[1])),
131 null,
132 null,
133 OpticalLogicId
134 .logicId(0),
135 true);
136 } else if ("ODUK".equals(type)) {
137 trueType = Tunnel.Type.ODUK;
138 String[] srcArray = src.split("-");
139 String[] dstArray = dst.split("-");
140 srcPoint = new DefaultOpticalTunnelEndPoint(
141 producerName,
142 Optional.of(DeviceId
143 .deviceId(srcArray[0])),
144 Optional.of(PortNumber
145 .portNumber(srcArray[1])),
146 null,
147 OpticalTunnelEndPoint.Type.LAMBDA,
148 OpticalLogicId
149 .logicId(0),
150 true);
151 dstPoint = new DefaultOpticalTunnelEndPoint(
152 producerName,
153 Optional.of(DeviceId
154 .deviceId(dstArray[0])),
155 Optional.of(PortNumber
156 .portNumber(dstArray[1])),
157 null,
158 OpticalTunnelEndPoint.Type.LAMBDA,
159 OpticalLogicId
160 .logicId(0),
161 true);
162 } else if ("OCH".equals(type)) {
163 trueType = Tunnel.Type.OCH;
164 String[] srcArray = src.split("-");
165 String[] dstArray = dst.split("-");
166 srcPoint = new DefaultOpticalTunnelEndPoint(
167 producerName,
168 Optional.of(DeviceId
169 .deviceId(srcArray[0])),
170 Optional.of(PortNumber
171 .portNumber(srcArray[1])),
172 null,
173 OpticalTunnelEndPoint.Type.TIMESLOT,
174 OpticalLogicId
175 .logicId(0),
176 true);
177 dstPoint = new DefaultOpticalTunnelEndPoint(
178 producerName,
179 Optional.of(DeviceId
180 .deviceId(dstArray[0])),
181 Optional.of(PortNumber
182 .portNumber(dstArray[1])),
183 null,
184 OpticalTunnelEndPoint.Type.TIMESLOT,
185 OpticalLogicId
186 .logicId(0),
187 true);
188 } else {
189 print("Illegal tunnel type. Please input MPLS, VLAN, VXLAN, GRE, ODUK or OCH.");
190 return;
191 }
192 tunnelSet = service.borrowTunnel(appId, srcPoint, dstPoint, trueType);
jccd8697232015-05-05 14:42:23 +0800193 }
samuel7a5691a2015-05-23 00:36:32 +0800194 if (!isNull(tunnelId)) {
195 TunnelId id = TunnelId.valueOf(tunnelId);
196 Tunnel tunnel = service.borrowTunnel(appId, id);
197 tunnelSet = new HashSet<Tunnel>();
198 tunnelSet.add(tunnel);
199 }
200 if (!isNull(tunnelName)) {
201 TunnelName name = TunnelName.tunnelName(tunnelName);
202 tunnelSet = service.borrowTunnel(appId, name);
203 }
jccd8697232015-05-05 14:42:23 +0800204 for (Tunnel tunnel : tunnelSet) {
205 print(FMT, tunnel.src(), tunnel.dst(), tunnel.type(),
206 tunnel.state(), tunnel.providerId(), tunnel.tunnelName(),
207 tunnel.groupId());
208 }
209 }
210
samuel7a5691a2015-05-23 00:36:32 +0800211 private boolean isNull(String s) {
212 return s == null || "".equals(s);
213 }
jccd8697232015-05-05 14:42:23 +0800214}