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.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.ApplicationId; |
| 26 | import org.onosproject.core.DefaultApplicationId; |
Thomas Vachuska | bf916ea | 2015-05-20 18:24:34 -0700 | [diff] [blame] | 27 | import org.onosproject.incubator.net.tunnel.DefaultOpticalTunnelEndPoint; |
| 28 | import org.onosproject.incubator.net.tunnel.IpTunnelEndPoint; |
| 29 | import org.onosproject.incubator.net.tunnel.OpticalLogicId; |
| 30 | import org.onosproject.incubator.net.tunnel.OpticalTunnelEndPoint; |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 31 | import org.onosproject.incubator.net.tunnel.Tunnel; |
Thomas Vachuska | bf916ea | 2015-05-20 18:24:34 -0700 | [diff] [blame] | 32 | import org.onosproject.incubator.net.tunnel.TunnelEndPoint; |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 33 | import org.onosproject.incubator.net.tunnel.TunnelId; |
| 34 | import org.onosproject.incubator.net.tunnel.TunnelName; |
Thomas Vachuska | bf916ea | 2015-05-20 18:24:34 -0700 | [diff] [blame] | 35 | import org.onosproject.incubator.net.tunnel.TunnelService; |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 36 | import org.onosproject.net.DeviceId; |
| 37 | import org.onosproject.net.PortNumber; |
| 38 | import org.onosproject.net.provider.ProviderId; |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 39 | |
| 40 | /** |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 41 | * Returns tunnels. It's used by consumers. |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 42 | */ |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 43 | @Command(scope = "onos", name = "tunnel-return", |
| 44 | description = "Returns tunnels. It's used by consumers.") |
Thomas Vachuska | bf916ea | 2015-05-20 18:24:34 -0700 | [diff] [blame] | 45 | public class TunnelReturnCommand extends AbstractShellCommand { |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 46 | @Argument(index = 0, name = "consumerId", |
| 47 | description = "consumer id means application id.", required = true, multiValued = false) |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 48 | String consumerId = null; |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 49 | |
| 50 | @Option(name = "-s", aliases = "--src", description = "Source tunnel point." |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 51 | + " Only supports for IpTunnelEndPoint and OpticalTunnelEndPoint as end point now." |
| 52 | + " 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] | 53 | + " Otherwise src means IP address.", required = false, multiValued = false) |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 54 | String src = null; |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 55 | |
| 56 | @Option(name = "-d", aliases = "--dst", description = "Destination tunnel point." |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 57 | + " Only supports for IpTunnelEndPoint and OpticalTunnelEndPoint as end point now." |
| 58 | + " 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] | 59 | + " Otherwise dst means IP address.", required = false, multiValued = false) |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 60 | String dst = null; |
| 61 | |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 62 | @Option(name = "-t", aliases = "--type", description = "The type of tunnels," |
| 63 | + " It includes MPLS, VLAN, VXLAN, GRE, ODUK, OCH", required = false, multiValued = false) |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 64 | String type = null; |
| 65 | |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 66 | @Option(name = "-i", aliases = "--tunnelId", |
| 67 | description = "the tunnel identity.", required = false, multiValued = false) |
| 68 | String tunnelId = null; |
| 69 | |
| 70 | @Option(name = "-n", aliases = "--tunnelName", |
| 71 | description = "The name of tunnels", required = false, multiValued = false) |
| 72 | String tunnelName = null; |
| 73 | |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 74 | @Override |
| 75 | protected void execute() { |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 76 | Tunnel.Type trueType = null; |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 77 | TunnelService service = get(TunnelService.class); |
| 78 | ApplicationId appId = new DefaultApplicationId(1, consumerId); |
| 79 | ProviderId producerName = new ProviderId("default", |
| 80 | "org.onosproject.provider.tunnel.default"); |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 81 | if (!isNull(src) && !isNull(dst) && !isNull(type)) { |
| 82 | TunnelEndPoint srcPoint = null; |
| 83 | TunnelEndPoint dstPoint = null; |
| 84 | if ("MPLS".equals(type)) { |
| 85 | trueType = Tunnel.Type.MPLS; |
| 86 | srcPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress |
| 87 | .valueOf(src)); |
| 88 | dstPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress |
| 89 | .valueOf(dst)); |
| 90 | } else if ("VXLAN".equals(type)) { |
| 91 | trueType = Tunnel.Type.VXLAN; |
| 92 | srcPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress |
| 93 | .valueOf(src)); |
| 94 | dstPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress |
| 95 | .valueOf(dst)); |
| 96 | } else if ("GRE".equals(type)) { |
| 97 | trueType = Tunnel.Type.GRE; |
| 98 | srcPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress |
| 99 | .valueOf(src)); |
| 100 | dstPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress |
| 101 | .valueOf(dst)); |
| 102 | } else if ("VLAN".equals(type)) { |
| 103 | trueType = Tunnel.Type.VLAN; |
| 104 | String[] srcArray = src.split("-"); |
| 105 | String[] dstArray = dst.split("-"); |
| 106 | srcPoint = new DefaultOpticalTunnelEndPoint( |
| 107 | producerName, |
| 108 | Optional.of(DeviceId |
| 109 | .deviceId(srcArray[0])), |
| 110 | Optional.of(PortNumber |
| 111 | .portNumber(srcArray[1])), |
| 112 | null, |
| 113 | null, |
| 114 | OpticalLogicId |
| 115 | .logicId(0), |
| 116 | true); |
| 117 | dstPoint = new DefaultOpticalTunnelEndPoint( |
| 118 | producerName, |
| 119 | Optional.of(DeviceId |
| 120 | .deviceId(dstArray[0])), |
| 121 | Optional.of(PortNumber |
| 122 | .portNumber(dstArray[1])), |
| 123 | null, |
| 124 | null, |
| 125 | OpticalLogicId |
| 126 | .logicId(0), |
| 127 | true); |
| 128 | } else if ("ODUK".equals(type)) { |
| 129 | trueType = Tunnel.Type.ODUK; |
| 130 | String[] srcArray = src.split("-"); |
| 131 | String[] dstArray = dst.split("-"); |
| 132 | srcPoint = new DefaultOpticalTunnelEndPoint( |
| 133 | producerName, |
| 134 | Optional.of(DeviceId |
| 135 | .deviceId(srcArray[0])), |
| 136 | Optional.of(PortNumber |
| 137 | .portNumber(srcArray[1])), |
| 138 | null, |
| 139 | OpticalTunnelEndPoint.Type.LAMBDA, |
| 140 | OpticalLogicId |
| 141 | .logicId(0), |
| 142 | true); |
| 143 | dstPoint = new DefaultOpticalTunnelEndPoint( |
| 144 | producerName, |
| 145 | Optional.of(DeviceId |
| 146 | .deviceId(dstArray[0])), |
| 147 | Optional.of(PortNumber |
| 148 | .portNumber(dstArray[1])), |
| 149 | null, |
| 150 | OpticalTunnelEndPoint.Type.LAMBDA, |
| 151 | OpticalLogicId |
| 152 | .logicId(0), |
| 153 | true); |
| 154 | } else if ("OCH".equals(type)) { |
| 155 | trueType = Tunnel.Type.OCH; |
| 156 | String[] srcArray = src.split("-"); |
| 157 | String[] dstArray = dst.split("-"); |
| 158 | srcPoint = new DefaultOpticalTunnelEndPoint( |
| 159 | producerName, |
| 160 | Optional.of(DeviceId |
| 161 | .deviceId(srcArray[0])), |
| 162 | Optional.of(PortNumber |
| 163 | .portNumber(srcArray[1])), |
| 164 | null, |
| 165 | OpticalTunnelEndPoint.Type.TIMESLOT, |
| 166 | OpticalLogicId |
| 167 | .logicId(0), |
| 168 | true); |
| 169 | dstPoint = new DefaultOpticalTunnelEndPoint( |
| 170 | producerName, |
| 171 | Optional.of(DeviceId |
| 172 | .deviceId(dstArray[0])), |
| 173 | Optional.of(PortNumber |
| 174 | .portNumber(dstArray[1])), |
| 175 | null, |
| 176 | OpticalTunnelEndPoint.Type.TIMESLOT, |
| 177 | OpticalLogicId |
| 178 | .logicId(0), |
| 179 | true); |
| 180 | } else { |
| 181 | print("Illegal tunnel type. Please input MPLS, VLAN, VXLAN, GRE, ODUK or OCH."); |
| 182 | return; |
| 183 | } |
| 184 | service.returnTunnel(appId, srcPoint, dstPoint, trueType); |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 185 | } |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 186 | if (!isNull(tunnelId)) { |
| 187 | TunnelId id = TunnelId.valueOf(tunnelId); |
| 188 | service.returnTunnel(appId, id); |
| 189 | } |
| 190 | if (!isNull(tunnelName)) { |
| 191 | TunnelName name = TunnelName.tunnelName(tunnelName); |
| 192 | service.returnTunnel(appId, name); |
| 193 | } |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 194 | } |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 195 | private boolean isNull(String s) { |
| 196 | return s == null || "".equals(s); |
| 197 | } |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 198 | } |