jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 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 fan | 7716ec9 | 2015-05-31 01:53:19 +0800 | [diff] [blame] | 16 | package org.onosproject.cli.net; |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 17 | |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 18 | import java.util.Optional; |
| 19 | |
| 20 | import org.apache.karaf.shell.commands.Argument; |
| 21 | import org.apache.karaf.shell.commands.Command; |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 22 | import org.apache.karaf.shell.commands.Option; |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 23 | import org.onlab.packet.IpAddress; |
| 24 | import org.onosproject.cli.AbstractShellCommand; |
| 25 | import org.onosproject.core.DefaultGroupId; |
Thomas Vachuska | bf916ea | 2015-05-20 18:24:34 -0700 | [diff] [blame] | 26 | import org.onosproject.incubator.net.tunnel.DefaultOpticalTunnelEndPoint; |
| 27 | import org.onosproject.incubator.net.tunnel.DefaultTunnelDescription; |
| 28 | import org.onosproject.incubator.net.tunnel.IpTunnelEndPoint; |
| 29 | import org.onosproject.incubator.net.tunnel.OpticalLogicId; |
| 30 | import org.onosproject.incubator.net.tunnel.OpticalTunnelEndPoint; |
| 31 | import org.onosproject.incubator.net.tunnel.Tunnel; |
| 32 | import org.onosproject.incubator.net.tunnel.TunnelDescription; |
| 33 | import org.onosproject.incubator.net.tunnel.TunnelEndPoint; |
| 34 | import org.onosproject.incubator.net.tunnel.TunnelId; |
| 35 | import org.onosproject.incubator.net.tunnel.TunnelName; |
| 36 | import org.onosproject.incubator.net.tunnel.TunnelProvider; |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 37 | import org.onosproject.net.DefaultAnnotations; |
| 38 | import org.onosproject.net.DeviceId; |
| 39 | import org.onosproject.net.PortNumber; |
| 40 | import org.onosproject.net.SparseAnnotations; |
| 41 | import org.onosproject.net.provider.ProviderId; |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 42 | |
| 43 | /** |
| 44 | * Supports for creating a tunnel by using IP address and optical as tunnel end |
| 45 | * point. |
| 46 | */ |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 47 | @Command(scope = "onos", name = "tunnel-create", |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 48 | description = "Supports for creating a tunnel by using IP address and optical as tunnel end point now.") |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 49 | public class TunnelCreateCommand extends AbstractShellCommand { |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 50 | |
| 51 | @Argument(index = 0, name = "src", description = "Source tunnel point." |
| 52 | + " Only supports for IpTunnelEndPoint and OpticalTunnelEndPoint as end point now." |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 53 | + " If creates a ODUK or OCH or VLAN type tunnel, the formatter of this argument is DeviceId-PortNumber." |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 54 | + " 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." |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 59 | + " If creates a ODUK or OCH or VLAN type tunnel, the formatter of this argument is DeviceId-PortNumber." |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 60 | + " 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; |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 65 | @Option(name = "-g", aliases = "--groupId", |
| 66 | description = "Group flow table id which a tunnel match up", required = false, multiValued = false) |
cheng fan | 7716ec9 | 2015-05-31 01:53:19 +0800 | [diff] [blame] | 67 | String groupId = "0"; |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 68 | |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 69 | @Option(name = "-n", aliases = "--tunnelName", |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 70 | description = "The name of tunnels", required = false, multiValued = false) |
cheng fan | 7716ec9 | 2015-05-31 01:53:19 +0800 | [diff] [blame] | 71 | String tunnelName = "onos"; |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 72 | |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 73 | @Option(name = "-b", aliases = "--bandwidth", |
| 74 | description = "The bandwidth attribute of tunnel", required = false, multiValued = false) |
cheng fan | 93258c7 | 2015-06-02 23:42:32 +0800 | [diff] [blame] | 75 | String bandwidth = "1024"; |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 76 | |
| 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; |
cheng fan | 00a406d | 2015-06-05 21:42:40 +0800 | [diff] [blame] | 93 | String[] srcArray = src.split("/"); |
| 94 | String[] dstArray = dst.split("/"); |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 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); |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 117 | } 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; |
cheng fan | 00a406d | 2015-06-05 21:42:40 +0800 | [diff] [blame] | 127 | String[] srcArray = src.split("/"); |
| 128 | String[] dstArray = dst.split("/"); |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 129 | 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; |
cheng fan | 00a406d | 2015-06-05 21:42:40 +0800 | [diff] [blame] | 153 | String[] srcArray = src.split("/"); |
| 154 | String[] dstArray = dst.split("/"); |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 155 | srcPoint = new DefaultOpticalTunnelEndPoint( |
| 156 | producerName, |
| 157 | Optional.of(DeviceId |
| 158 | .deviceId(srcArray[0])), |
| 159 | Optional.of(PortNumber |
| 160 | .portNumber(srcArray[1])), |
| 161 | null, |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 162 | OpticalTunnelEndPoint.Type.TIMESLOT, |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 163 | 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, |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 173 | OpticalTunnelEndPoint.Type.TIMESLOT, |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 174 | 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() |
cheng fan | 93258c7 | 2015-06-02 23:42:32 +0800 | [diff] [blame] | 184 | .set("bandwidth", bandwidth == null || "".equals(bandwidth) ? "0" : bandwidth) |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 185 | .build(); |
| 186 | TunnelDescription tunnel = new DefaultTunnelDescription( |
| 187 | null, |
| 188 | srcPoint, |
| 189 | dstPoint, |
| 190 | trueType, |
HIGUCHI Yuta | 4a6a70a | 2015-09-04 14:42:10 -0700 | [diff] [blame] | 191 | new DefaultGroupId(Integer.parseInt(groupId)), |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 192 | producerName, |
| 193 | TunnelName |
| 194 | .tunnelName(tunnelName), |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 195 | null, |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 196 | annotations); |
| 197 | TunnelId tunnelId = service.tunnelAdded(tunnel); |
cheng fan | 93258c7 | 2015-06-02 23:42:32 +0800 | [diff] [blame] | 198 | if (tunnelId == null) { |
| 199 | error("Create tunnel failed."); |
| 200 | return; |
| 201 | } |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 202 | print(FMT, tunnelId.id()); |
| 203 | } |
| 204 | |
| 205 | } |