blob: eb20b9556bc1d782c5f188c5899dc08325034e07 [file] [log] [blame]
jccd8697232015-05-05 14:42:23 +08001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
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;
jccd8697232015-05-05 14:42:23 +080017
jccd8697232015-05-05 14:42:23 +080018import 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.DefaultGroupId;
Thomas Vachuskabf916ea2015-05-20 18:24:34 -070026import org.onosproject.incubator.net.tunnel.DefaultOpticalTunnelEndPoint;
27import org.onosproject.incubator.net.tunnel.DefaultTunnelDescription;
28import org.onosproject.incubator.net.tunnel.IpTunnelEndPoint;
29import org.onosproject.incubator.net.tunnel.OpticalLogicId;
30import org.onosproject.incubator.net.tunnel.OpticalTunnelEndPoint;
31import org.onosproject.incubator.net.tunnel.Tunnel;
32import org.onosproject.incubator.net.tunnel.TunnelDescription;
33import org.onosproject.incubator.net.tunnel.TunnelEndPoint;
34import org.onosproject.incubator.net.tunnel.TunnelId;
35import org.onosproject.incubator.net.tunnel.TunnelName;
36import org.onosproject.incubator.net.tunnel.TunnelProvider;
samuel7a5691a2015-05-23 00:36:32 +080037import org.onosproject.net.DefaultAnnotations;
38import org.onosproject.net.DeviceId;
39import org.onosproject.net.PortNumber;
40import org.onosproject.net.SparseAnnotations;
41import org.onosproject.net.provider.ProviderId;
jccd8697232015-05-05 14:42:23 +080042
43/**
44 * Supports for creating a tunnel by using IP address and optical as tunnel end
45 * point.
46 */
samuel7a5691a2015-05-23 00:36:32 +080047@Command(scope = "onos", name = "tunnel-create",
jccd8697232015-05-05 14:42:23 +080048description = "Supports for creating a tunnel by using IP address and optical as tunnel end point now.")
samuel7a5691a2015-05-23 00:36:32 +080049public class TunnelCreateCommand extends AbstractShellCommand {
jccd8697232015-05-05 14:42:23 +080050
51 @Argument(index = 0, name = "src", description = "Source tunnel point."
52 + " Only supports for IpTunnelEndPoint and OpticalTunnelEndPoint as end point now."
samuel7a5691a2015-05-23 00:36:32 +080053 + " If creates a ODUK or OCH or VLAN type tunnel, the formatter of this argument is DeviceId-PortNumber."
jccd8697232015-05-05 14:42:23 +080054 + " Otherwise src means IP address.", required = true, multiValued = false)
55 String src = null;
56
57 @Argument(index = 1, name = "dst", description = "Destination tunnel point."
58 + " Only supports for IpTunnelEndPoint and OpticalTunnelEndPoint as end point now."
samuel7a5691a2015-05-23 00:36:32 +080059 + " If creates a ODUK or OCH or VLAN type tunnel, the formatter of this argument is DeviceId-PortNumber."
jccd8697232015-05-05 14:42:23 +080060 + " Otherwise dst means IP address.", required = true, multiValued = false)
61 String dst = null;
62 @Argument(index = 2, name = "type", description = "The type of tunnels,"
63 + " It includes MPLS, VLAN, VXLAN, GRE, ODUK, OCH", required = true, multiValued = false)
64 String type = null;
samuel7a5691a2015-05-23 00:36:32 +080065 @Option(name = "-g", aliases = "--groupId",
66 description = "Group flow table id which a tunnel match up", required = false, multiValued = false)
cheng fan7716ec92015-05-31 01:53:19 +080067 String groupId = "0";
jccd8697232015-05-05 14:42:23 +080068
samuel7a5691a2015-05-23 00:36:32 +080069 @Option(name = "-n", aliases = "--tunnelName",
jccd8697232015-05-05 14:42:23 +080070 description = "The name of tunnels", required = false, multiValued = false)
cheng fan7716ec92015-05-31 01:53:19 +080071 String tunnelName = "onos";
jccd8697232015-05-05 14:42:23 +080072
samuel7a5691a2015-05-23 00:36:32 +080073 @Option(name = "-b", aliases = "--bandwidth",
74 description = "The bandwidth attribute of tunnel", required = false, multiValued = false)
75 String bandwidth = null;
jccd8697232015-05-05 14:42:23 +080076
77 private static final String FMT = "The tunnel identity is %s";
78
79 @Override
80 protected void execute() {
81 TunnelProvider service = get(TunnelProvider.class);
82 ProviderId producerName = new ProviderId("default",
83 "org.onosproject.provider.tunnel.default");
84 TunnelEndPoint srcPoint = null;
85 TunnelEndPoint dstPoint = null;
86 Tunnel.Type trueType = null;
87 if ("MPLS".equals(type)) {
88 trueType = Tunnel.Type.MPLS;
89 srcPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress.valueOf(src));
90 dstPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress.valueOf(dst));
91 } else if ("VLAN".equals(type)) {
92 trueType = Tunnel.Type.VLAN;
samuel7a5691a2015-05-23 00:36:32 +080093 String[] srcArray = src.split("-");
94 String[] dstArray = dst.split("-");
95 srcPoint = new DefaultOpticalTunnelEndPoint(
96 producerName,
97 Optional.of(DeviceId
98 .deviceId(srcArray[0])),
99 Optional.of(PortNumber
100 .portNumber(srcArray[1])),
101 null,
102 null,
103 OpticalLogicId
104 .logicId(0),
105 true);
106 dstPoint = new DefaultOpticalTunnelEndPoint(
107 producerName,
108 Optional.of(DeviceId
109 .deviceId(dstArray[0])),
110 Optional.of(PortNumber
111 .portNumber(dstArray[1])),
112 null,
113 null,
114 OpticalLogicId
115 .logicId(0),
116 true);
jccd8697232015-05-05 14:42:23 +0800117 } else if ("VXLAN".equals(type)) {
118 trueType = Tunnel.Type.VXLAN;
119 srcPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress.valueOf(src));
120 dstPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress.valueOf(dst));
121 } else if ("GRE".equals(type)) {
122 trueType = Tunnel.Type.GRE;
123 srcPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress.valueOf(src));
124 dstPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress.valueOf(dst));
125 } else if ("ODUK".equals(type)) {
126 trueType = Tunnel.Type.ODUK;
samuel7a5691a2015-05-23 00:36:32 +0800127 String[] srcArray = src.split("-");
128 String[] dstArray = dst.split("-");
jccd8697232015-05-05 14:42:23 +0800129 srcPoint = new DefaultOpticalTunnelEndPoint(
130 producerName,
131 Optional.of(DeviceId
132 .deviceId(srcArray[0])),
133 Optional.of(PortNumber
134 .portNumber(srcArray[1])),
135 null,
136 OpticalTunnelEndPoint.Type.LAMBDA,
137 OpticalLogicId
138 .logicId(0),
139 true);
140 dstPoint = new DefaultOpticalTunnelEndPoint(
141 producerName,
142 Optional.of(DeviceId
143 .deviceId(dstArray[0])),
144 Optional.of(PortNumber
145 .portNumber(dstArray[1])),
146 null,
147 OpticalTunnelEndPoint.Type.LAMBDA,
148 OpticalLogicId
149 .logicId(0),
150 true);
151 } else if ("OCH".equals(type)) {
152 trueType = Tunnel.Type.OCH;
153 String[] srcArray = src.split("-");
154 String[] dstArray = dst.split("-");
155 srcPoint = new DefaultOpticalTunnelEndPoint(
156 producerName,
157 Optional.of(DeviceId
158 .deviceId(srcArray[0])),
159 Optional.of(PortNumber
160 .portNumber(srcArray[1])),
161 null,
samuel7a5691a2015-05-23 00:36:32 +0800162 OpticalTunnelEndPoint.Type.TIMESLOT,
jccd8697232015-05-05 14:42:23 +0800163 OpticalLogicId
164 .logicId(0),
165 true);
166 dstPoint = new DefaultOpticalTunnelEndPoint(
167 producerName,
168 Optional.of(DeviceId
169 .deviceId(dstArray[0])),
170 Optional.of(PortNumber
171 .portNumber(dstArray[1])),
172 null,
samuel7a5691a2015-05-23 00:36:32 +0800173 OpticalTunnelEndPoint.Type.TIMESLOT,
jccd8697232015-05-05 14:42:23 +0800174 OpticalLogicId
175 .logicId(0),
176 true);
177 } else {
178 print("Illegal tunnel type. Please input MPLS, VLAN, VXLAN, GRE, ODUK or OCH.");
179 return;
180 }
181
182 SparseAnnotations annotations = DefaultAnnotations
183 .builder()
samuel7a5691a2015-05-23 00:36:32 +0800184 .set("bandwidth", bandwidth == null && "".equals(bandwidth) ? "0" : bandwidth)
jccd8697232015-05-05 14:42:23 +0800185 .build();
186 TunnelDescription tunnel = new DefaultTunnelDescription(
187 null,
188 srcPoint,
189 dstPoint,
190 trueType,
191 new DefaultGroupId(
192 Integer.valueOf(groupId)
193 .intValue()),
194 producerName,
195 TunnelName
196 .tunnelName(tunnelName),
samuel7a5691a2015-05-23 00:36:32 +0800197 null,
jccd8697232015-05-05 14:42:23 +0800198 annotations);
199 TunnelId tunnelId = service.tunnelAdded(tunnel);
200 print(FMT, tunnelId.id());
201 }
202
203}