jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
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 | */ |
| 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 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 22 | import org.apache.karaf.shell.api.action.Argument; |
| 23 | import org.apache.karaf.shell.api.action.Command; |
| 24 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
| 25 | import org.apache.karaf.shell.api.action.Option; |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 26 | import org.onlab.packet.IpAddress; |
| 27 | import org.onosproject.cli.AbstractShellCommand; |
| 28 | import org.onosproject.core.ApplicationId; |
| 29 | import org.onosproject.core.DefaultApplicationId; |
Thomas Vachuska | bf916ea | 2015-05-20 18:24:34 -0700 | [diff] [blame] | 30 | import org.onosproject.incubator.net.tunnel.DefaultOpticalTunnelEndPoint; |
| 31 | import org.onosproject.incubator.net.tunnel.IpTunnelEndPoint; |
| 32 | import org.onosproject.incubator.net.tunnel.OpticalLogicId; |
| 33 | import org.onosproject.incubator.net.tunnel.OpticalTunnelEndPoint; |
| 34 | import org.onosproject.incubator.net.tunnel.Tunnel; |
| 35 | import org.onosproject.incubator.net.tunnel.TunnelEndPoint; |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 36 | import org.onosproject.incubator.net.tunnel.TunnelId; |
| 37 | import org.onosproject.incubator.net.tunnel.TunnelName; |
Thomas Vachuska | bf916ea | 2015-05-20 18:24:34 -0700 | [diff] [blame] | 38 | import org.onosproject.incubator.net.tunnel.TunnelService; |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 39 | import org.onosproject.net.DeviceId; |
| 40 | import org.onosproject.net.PortNumber; |
| 41 | import org.onosproject.net.provider.ProviderId; |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 42 | |
| 43 | /** |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 44 | * Borrows tunnels. It's used by consumers. |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 45 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 46 | @Service |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 47 | @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] | 48 | public class TunnelBorrowCommand extends AbstractShellCommand { |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 49 | |
| 50 | @Argument(index = 0, name = "consumerId", |
| 51 | description = "consumer id means application id.", required = true, multiValued = false) |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 52 | String consumerId = null; |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 53 | |
| 54 | @Option(name = "-s", aliases = "--src", description = "Source tunnel point." |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 55 | + " 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." |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 57 | + " Otherwise src means IP address.", required = false, multiValued = false) |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 58 | String src = null; |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 59 | |
| 60 | @Option(name = "-d", aliases = "--dst", description = "Destination tunnel point." |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 61 | + " 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." |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 63 | + " Otherwise dst means IP address.", required = false, multiValued = false) |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 64 | String dst = null; |
| 65 | |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 66 | @Option(name = "-t", aliases = "--type", description = "The type of tunnels," |
| 67 | + " It includes MPLS, VLAN, VXLAN, GRE, ODUK, OCH", required = false, multiValued = false) |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 68 | String type = null; |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 69 | |
| 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; |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 77 | 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 Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 82 | protected void doExecute() { |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 83 | Collection<Tunnel> tunnelSet = null; |
| 84 | Tunnel.Type trueType = null; |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 85 | TunnelService service = get(TunnelService.class); |
| 86 | ApplicationId appId = new DefaultApplicationId(1, consumerId); |
| 87 | ProviderId producerName = new ProviderId("default", |
| 88 | "org.onosproject.provider.tunnel.default"); |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 89 | 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); |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 193 | } |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 194 | 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 | } |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 204 | 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 | |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 211 | private boolean isNull(String s) { |
| 212 | return s == null || "".equals(s); |
| 213 | } |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 214 | } |