jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014-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 | */ |
| 16 | package org.onosproject.cli.net; |
| 17 | |
| 18 | import java.util.Collection; |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 19 | import java.util.HashSet; |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 20 | import java.util.Optional; |
| 21 | |
| 22 | import org.apache.karaf.shell.commands.Argument; |
| 23 | import org.apache.karaf.shell.commands.Command; |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 24 | import org.apache.karaf.shell.commands.Option; |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 25 | import org.onlab.packet.IpAddress; |
| 26 | import org.onosproject.cli.AbstractShellCommand; |
| 27 | import org.onosproject.core.ApplicationId; |
| 28 | import org.onosproject.core.DefaultApplicationId; |
Thomas Vachuska | bf916ea | 2015-05-20 18:24:34 -0700 | [diff] [blame] | 29 | import org.onosproject.incubator.net.tunnel.DefaultOpticalTunnelEndPoint; |
| 30 | import org.onosproject.incubator.net.tunnel.IpTunnelEndPoint; |
| 31 | import org.onosproject.incubator.net.tunnel.OpticalLogicId; |
| 32 | import org.onosproject.incubator.net.tunnel.OpticalTunnelEndPoint; |
| 33 | import org.onosproject.incubator.net.tunnel.Tunnel; |
| 34 | import org.onosproject.incubator.net.tunnel.TunnelEndPoint; |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 35 | import org.onosproject.incubator.net.tunnel.TunnelId; |
| 36 | import org.onosproject.incubator.net.tunnel.TunnelName; |
Thomas Vachuska | bf916ea | 2015-05-20 18:24:34 -0700 | [diff] [blame] | 37 | import org.onosproject.incubator.net.tunnel.TunnelService; |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 38 | import org.onosproject.net.DeviceId; |
| 39 | import org.onosproject.net.PortNumber; |
| 40 | import org.onosproject.net.provider.ProviderId; |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 41 | |
| 42 | /** |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 43 | * Borrows tunnels. It's used by consumers. |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 44 | */ |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 45 | @Command(scope = "onos", name = "tunnel-borrow", description = "Borrows tunnels. It's used by consumers.") |
Thomas Vachuska | bf916ea | 2015-05-20 18:24:34 -0700 | [diff] [blame] | 46 | public class TunnelBorrowCommand extends AbstractShellCommand { |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 47 | |
| 48 | @Argument(index = 0, name = "consumerId", |
| 49 | description = "consumer id means application id.", required = true, multiValued = false) |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 50 | String consumerId = null; |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 51 | |
| 52 | @Option(name = "-s", aliases = "--src", description = "Source tunnel point." |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 53 | + " 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." |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 55 | + " Otherwise src means IP address.", required = false, multiValued = false) |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 56 | String src = null; |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 57 | |
| 58 | @Option(name = "-d", aliases = "--dst", description = "Destination tunnel point." |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 59 | + " 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." |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 61 | + " Otherwise dst means IP address.", required = false, multiValued = false) |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 62 | String dst = null; |
| 63 | |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 64 | @Option(name = "-t", aliases = "--type", description = "The type of tunnels," |
| 65 | + " It includes MPLS, VLAN, VXLAN, GRE, ODUK, OCH", required = false, multiValued = false) |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 66 | String type = null; |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 67 | |
| 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; |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 75 | 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() { |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 81 | Collection<Tunnel> tunnelSet = null; |
| 82 | Tunnel.Type trueType = null; |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 83 | TunnelService service = get(TunnelService.class); |
| 84 | ApplicationId appId = new DefaultApplicationId(1, consumerId); |
| 85 | ProviderId producerName = new ProviderId("default", |
| 86 | "org.onosproject.provider.tunnel.default"); |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 87 | 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); |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 191 | } |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 192 | 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 | } |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 202 | 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 | |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 209 | private boolean isNull(String s) { |
| 210 | return s == null || "".equals(s); |
| 211 | } |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 212 | } |