blob: 6b3a77bd114e0c7ee339a3626b7e70af472f8667 [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 */
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
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;
Yi Tsengfa394de2017-02-01 11:26:40 -080026import org.onosproject.core.GroupId;
Thomas Vachuskabf916ea2015-05-20 18:24:34 -070027import org.onosproject.incubator.net.tunnel.DefaultOpticalTunnelEndPoint;
28import org.onosproject.incubator.net.tunnel.DefaultTunnelDescription;
29import org.onosproject.incubator.net.tunnel.IpTunnelEndPoint;
30import org.onosproject.incubator.net.tunnel.OpticalLogicId;
31import org.onosproject.incubator.net.tunnel.OpticalTunnelEndPoint;
32import org.onosproject.incubator.net.tunnel.Tunnel;
33import org.onosproject.incubator.net.tunnel.TunnelDescription;
34import org.onosproject.incubator.net.tunnel.TunnelEndPoint;
35import org.onosproject.incubator.net.tunnel.TunnelId;
36import org.onosproject.incubator.net.tunnel.TunnelName;
37import org.onosproject.incubator.net.tunnel.TunnelProvider;
samuel7a5691a2015-05-23 00:36:32 +080038import org.onosproject.net.DefaultAnnotations;
39import org.onosproject.net.DeviceId;
40import org.onosproject.net.PortNumber;
41import org.onosproject.net.SparseAnnotations;
42import org.onosproject.net.provider.ProviderId;
jccd8697232015-05-05 14:42:23 +080043
44/**
45 * Supports for creating a tunnel by using IP address and optical as tunnel end
46 * point.
47 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070048@Service
samuel7a5691a2015-05-23 00:36:32 +080049@Command(scope = "onos", name = "tunnel-create",
jccd8697232015-05-05 14:42:23 +080050description = "Supports for creating a tunnel by using IP address and optical as tunnel end point now.")
samuel7a5691a2015-05-23 00:36:32 +080051public class TunnelCreateCommand extends AbstractShellCommand {
jccd8697232015-05-05 14:42:23 +080052
53 @Argument(index = 0, name = "src", description = "Source tunnel point."
54 + " Only supports for IpTunnelEndPoint and OpticalTunnelEndPoint as end point now."
samuel7a5691a2015-05-23 00:36:32 +080055 + " If creates a ODUK or OCH or VLAN type tunnel, the formatter of this argument is DeviceId-PortNumber."
jccd8697232015-05-05 14:42:23 +080056 + " Otherwise src means IP address.", required = true, multiValued = false)
57 String src = null;
58
59 @Argument(index = 1, name = "dst", description = "Destination tunnel point."
60 + " Only supports for IpTunnelEndPoint and OpticalTunnelEndPoint as end point now."
samuel7a5691a2015-05-23 00:36:32 +080061 + " If creates a ODUK or OCH or VLAN type tunnel, the formatter of this argument is DeviceId-PortNumber."
jccd8697232015-05-05 14:42:23 +080062 + " Otherwise dst means IP address.", required = true, multiValued = false)
63 String dst = null;
64 @Argument(index = 2, name = "type", description = "The type of tunnels,"
65 + " It includes MPLS, VLAN, VXLAN, GRE, ODUK, OCH", required = true, multiValued = false)
66 String type = null;
samuel7a5691a2015-05-23 00:36:32 +080067 @Option(name = "-g", aliases = "--groupId",
68 description = "Group flow table id which a tunnel match up", required = false, multiValued = false)
cheng fan7716ec92015-05-31 01:53:19 +080069 String groupId = "0";
jccd8697232015-05-05 14:42:23 +080070
samuel7a5691a2015-05-23 00:36:32 +080071 @Option(name = "-n", aliases = "--tunnelName",
jccd8697232015-05-05 14:42:23 +080072 description = "The name of tunnels", required = false, multiValued = false)
cheng fan7716ec92015-05-31 01:53:19 +080073 String tunnelName = "onos";
jccd8697232015-05-05 14:42:23 +080074
samuel7a5691a2015-05-23 00:36:32 +080075 @Option(name = "-b", aliases = "--bandwidth",
76 description = "The bandwidth attribute of tunnel", required = false, multiValued = false)
cheng fan93258c72015-06-02 23:42:32 +080077 String bandwidth = "1024";
jccd8697232015-05-05 14:42:23 +080078
79 private static final String FMT = "The tunnel identity is %s";
80
81 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070082 protected void doExecute() {
jccd8697232015-05-05 14:42:23 +080083 TunnelProvider service = get(TunnelProvider.class);
84 ProviderId producerName = new ProviderId("default",
85 "org.onosproject.provider.tunnel.default");
86 TunnelEndPoint srcPoint = null;
87 TunnelEndPoint dstPoint = null;
88 Tunnel.Type trueType = null;
89 if ("MPLS".equals(type)) {
90 trueType = Tunnel.Type.MPLS;
91 srcPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress.valueOf(src));
92 dstPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress.valueOf(dst));
93 } else if ("VLAN".equals(type)) {
94 trueType = Tunnel.Type.VLAN;
cheng fan00a406d2015-06-05 21:42:40 +080095 String[] srcArray = src.split("/");
96 String[] dstArray = dst.split("/");
samuel7a5691a2015-05-23 00:36:32 +080097 srcPoint = new DefaultOpticalTunnelEndPoint(
98 producerName,
99 Optional.of(DeviceId
100 .deviceId(srcArray[0])),
101 Optional.of(PortNumber
102 .portNumber(srcArray[1])),
103 null,
104 null,
105 OpticalLogicId
106 .logicId(0),
107 true);
108 dstPoint = new DefaultOpticalTunnelEndPoint(
109 producerName,
110 Optional.of(DeviceId
111 .deviceId(dstArray[0])),
112 Optional.of(PortNumber
113 .portNumber(dstArray[1])),
114 null,
115 null,
116 OpticalLogicId
117 .logicId(0),
118 true);
jccd8697232015-05-05 14:42:23 +0800119 } else if ("VXLAN".equals(type)) {
120 trueType = Tunnel.Type.VXLAN;
121 srcPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress.valueOf(src));
122 dstPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress.valueOf(dst));
123 } else if ("GRE".equals(type)) {
124 trueType = Tunnel.Type.GRE;
125 srcPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress.valueOf(src));
126 dstPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress.valueOf(dst));
127 } else if ("ODUK".equals(type)) {
128 trueType = Tunnel.Type.ODUK;
cheng fan00a406d2015-06-05 21:42:40 +0800129 String[] srcArray = src.split("/");
130 String[] dstArray = dst.split("/");
jccd8697232015-05-05 14:42:23 +0800131 srcPoint = new DefaultOpticalTunnelEndPoint(
132 producerName,
133 Optional.of(DeviceId
134 .deviceId(srcArray[0])),
135 Optional.of(PortNumber
136 .portNumber(srcArray[1])),
137 null,
138 OpticalTunnelEndPoint.Type.LAMBDA,
139 OpticalLogicId
140 .logicId(0),
141 true);
142 dstPoint = new DefaultOpticalTunnelEndPoint(
143 producerName,
144 Optional.of(DeviceId
145 .deviceId(dstArray[0])),
146 Optional.of(PortNumber
147 .portNumber(dstArray[1])),
148 null,
149 OpticalTunnelEndPoint.Type.LAMBDA,
150 OpticalLogicId
151 .logicId(0),
152 true);
153 } else if ("OCH".equals(type)) {
154 trueType = Tunnel.Type.OCH;
cheng fan00a406d2015-06-05 21:42:40 +0800155 String[] srcArray = src.split("/");
156 String[] dstArray = dst.split("/");
jccd8697232015-05-05 14:42:23 +0800157 srcPoint = new DefaultOpticalTunnelEndPoint(
158 producerName,
159 Optional.of(DeviceId
160 .deviceId(srcArray[0])),
161 Optional.of(PortNumber
162 .portNumber(srcArray[1])),
163 null,
samuel7a5691a2015-05-23 00:36:32 +0800164 OpticalTunnelEndPoint.Type.TIMESLOT,
jccd8697232015-05-05 14:42:23 +0800165 OpticalLogicId
166 .logicId(0),
167 true);
168 dstPoint = new DefaultOpticalTunnelEndPoint(
169 producerName,
170 Optional.of(DeviceId
171 .deviceId(dstArray[0])),
172 Optional.of(PortNumber
173 .portNumber(dstArray[1])),
174 null,
samuel7a5691a2015-05-23 00:36:32 +0800175 OpticalTunnelEndPoint.Type.TIMESLOT,
jccd8697232015-05-05 14:42:23 +0800176 OpticalLogicId
177 .logicId(0),
178 true);
179 } else {
180 print("Illegal tunnel type. Please input MPLS, VLAN, VXLAN, GRE, ODUK or OCH.");
181 return;
182 }
183
184 SparseAnnotations annotations = DefaultAnnotations
185 .builder()
cheng fan93258c72015-06-02 23:42:32 +0800186 .set("bandwidth", bandwidth == null || "".equals(bandwidth) ? "0" : bandwidth)
jccd8697232015-05-05 14:42:23 +0800187 .build();
188 TunnelDescription tunnel = new DefaultTunnelDescription(
189 null,
190 srcPoint,
191 dstPoint,
192 trueType,
Yi Tsengfa394de2017-02-01 11:26:40 -0800193 new GroupId(Integer.parseInt(groupId)),
jccd8697232015-05-05 14:42:23 +0800194 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);
cheng fan93258c72015-06-02 23:42:32 +0800200 if (tunnelId == null) {
201 error("Create tunnel failed.");
202 return;
203 }
jccd8697232015-05-05 14:42:23 +0800204 print(FMT, tunnelId.id());
205 }
206
207}