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.Optional; |
| 19 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 20 | import org.apache.karaf.shell.api.action.Argument; |
| 21 | import org.apache.karaf.shell.api.action.Command; |
| 22 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
| 23 | import org.apache.karaf.shell.api.action.Option; |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 24 | import org.onlab.packet.IpAddress; |
| 25 | import org.onosproject.cli.AbstractShellCommand; |
| 26 | import org.onosproject.core.ApplicationId; |
| 27 | import org.onosproject.core.DefaultApplicationId; |
Thomas Vachuska | bf916ea | 2015-05-20 18:24:34 -0700 | [diff] [blame] | 28 | import org.onosproject.incubator.net.tunnel.DefaultOpticalTunnelEndPoint; |
| 29 | import org.onosproject.incubator.net.tunnel.IpTunnelEndPoint; |
| 30 | import org.onosproject.incubator.net.tunnel.OpticalLogicId; |
| 31 | import org.onosproject.incubator.net.tunnel.OpticalTunnelEndPoint; |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 32 | import org.onosproject.incubator.net.tunnel.Tunnel; |
Thomas Vachuska | bf916ea | 2015-05-20 18:24:34 -0700 | [diff] [blame] | 33 | import org.onosproject.incubator.net.tunnel.TunnelEndPoint; |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 34 | import org.onosproject.incubator.net.tunnel.TunnelId; |
| 35 | import org.onosproject.incubator.net.tunnel.TunnelName; |
Thomas Vachuska | bf916ea | 2015-05-20 18:24:34 -0700 | [diff] [blame] | 36 | import org.onosproject.incubator.net.tunnel.TunnelService; |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 37 | import org.onosproject.net.DeviceId; |
| 38 | import org.onosproject.net.PortNumber; |
| 39 | import org.onosproject.net.provider.ProviderId; |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 40 | |
| 41 | /** |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 42 | * Returns tunnels. It's used by consumers. |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 43 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 44 | @Service |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 45 | @Command(scope = "onos", name = "tunnel-return", |
| 46 | description = "Returns tunnels. It's used by consumers.") |
Thomas Vachuska | bf916ea | 2015-05-20 18:24:34 -0700 | [diff] [blame] | 47 | public class TunnelReturnCommand extends AbstractShellCommand { |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 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; |
| 67 | |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 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; |
| 75 | |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 76 | @Override |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 77 | protected void doExecute() { |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 78 | Tunnel.Type trueType = null; |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 79 | TunnelService service = get(TunnelService.class); |
| 80 | ApplicationId appId = new DefaultApplicationId(1, consumerId); |
| 81 | ProviderId producerName = new ProviderId("default", |
| 82 | "org.onosproject.provider.tunnel.default"); |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 83 | if (!isNull(src) && !isNull(dst) && !isNull(type)) { |
| 84 | TunnelEndPoint srcPoint = null; |
| 85 | TunnelEndPoint dstPoint = null; |
| 86 | if ("MPLS".equals(type)) { |
| 87 | trueType = Tunnel.Type.MPLS; |
| 88 | srcPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress |
| 89 | .valueOf(src)); |
| 90 | dstPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress |
| 91 | .valueOf(dst)); |
| 92 | } else if ("VXLAN".equals(type)) { |
| 93 | trueType = Tunnel.Type.VXLAN; |
| 94 | srcPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress |
| 95 | .valueOf(src)); |
| 96 | dstPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress |
| 97 | .valueOf(dst)); |
| 98 | } else if ("GRE".equals(type)) { |
| 99 | trueType = Tunnel.Type.GRE; |
| 100 | srcPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress |
| 101 | .valueOf(src)); |
| 102 | dstPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress |
| 103 | .valueOf(dst)); |
| 104 | } else if ("VLAN".equals(type)) { |
| 105 | trueType = Tunnel.Type.VLAN; |
| 106 | String[] srcArray = src.split("-"); |
| 107 | String[] dstArray = dst.split("-"); |
| 108 | srcPoint = new DefaultOpticalTunnelEndPoint( |
| 109 | producerName, |
| 110 | Optional.of(DeviceId |
| 111 | .deviceId(srcArray[0])), |
| 112 | Optional.of(PortNumber |
| 113 | .portNumber(srcArray[1])), |
| 114 | null, |
| 115 | null, |
| 116 | OpticalLogicId |
| 117 | .logicId(0), |
| 118 | true); |
| 119 | dstPoint = new DefaultOpticalTunnelEndPoint( |
| 120 | producerName, |
| 121 | Optional.of(DeviceId |
| 122 | .deviceId(dstArray[0])), |
| 123 | Optional.of(PortNumber |
| 124 | .portNumber(dstArray[1])), |
| 125 | null, |
| 126 | null, |
| 127 | OpticalLogicId |
| 128 | .logicId(0), |
| 129 | true); |
| 130 | } else if ("ODUK".equals(type)) { |
| 131 | trueType = Tunnel.Type.ODUK; |
| 132 | String[] srcArray = src.split("-"); |
| 133 | String[] dstArray = dst.split("-"); |
| 134 | srcPoint = new DefaultOpticalTunnelEndPoint( |
| 135 | producerName, |
| 136 | Optional.of(DeviceId |
| 137 | .deviceId(srcArray[0])), |
| 138 | Optional.of(PortNumber |
| 139 | .portNumber(srcArray[1])), |
| 140 | null, |
| 141 | OpticalTunnelEndPoint.Type.LAMBDA, |
| 142 | OpticalLogicId |
| 143 | .logicId(0), |
| 144 | true); |
| 145 | dstPoint = new DefaultOpticalTunnelEndPoint( |
| 146 | producerName, |
| 147 | Optional.of(DeviceId |
| 148 | .deviceId(dstArray[0])), |
| 149 | Optional.of(PortNumber |
| 150 | .portNumber(dstArray[1])), |
| 151 | null, |
| 152 | OpticalTunnelEndPoint.Type.LAMBDA, |
| 153 | OpticalLogicId |
| 154 | .logicId(0), |
| 155 | true); |
| 156 | } else if ("OCH".equals(type)) { |
| 157 | trueType = Tunnel.Type.OCH; |
| 158 | String[] srcArray = src.split("-"); |
| 159 | String[] dstArray = dst.split("-"); |
| 160 | srcPoint = new DefaultOpticalTunnelEndPoint( |
| 161 | producerName, |
| 162 | Optional.of(DeviceId |
| 163 | .deviceId(srcArray[0])), |
| 164 | Optional.of(PortNumber |
| 165 | .portNumber(srcArray[1])), |
| 166 | null, |
| 167 | OpticalTunnelEndPoint.Type.TIMESLOT, |
| 168 | OpticalLogicId |
| 169 | .logicId(0), |
| 170 | true); |
| 171 | dstPoint = new DefaultOpticalTunnelEndPoint( |
| 172 | producerName, |
| 173 | Optional.of(DeviceId |
| 174 | .deviceId(dstArray[0])), |
| 175 | Optional.of(PortNumber |
| 176 | .portNumber(dstArray[1])), |
| 177 | null, |
| 178 | OpticalTunnelEndPoint.Type.TIMESLOT, |
| 179 | OpticalLogicId |
| 180 | .logicId(0), |
| 181 | true); |
| 182 | } else { |
| 183 | print("Illegal tunnel type. Please input MPLS, VLAN, VXLAN, GRE, ODUK or OCH."); |
| 184 | return; |
| 185 | } |
| 186 | service.returnTunnel(appId, srcPoint, dstPoint, trueType); |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 187 | } |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 188 | if (!isNull(tunnelId)) { |
| 189 | TunnelId id = TunnelId.valueOf(tunnelId); |
| 190 | service.returnTunnel(appId, id); |
| 191 | } |
| 192 | if (!isNull(tunnelName)) { |
| 193 | TunnelName name = TunnelName.tunnelName(tunnelName); |
| 194 | service.returnTunnel(appId, name); |
| 195 | } |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 196 | } |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 197 | private boolean isNull(String s) { |
| 198 | return s == null || "".equals(s); |
| 199 | } |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 200 | } |