samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
samuel | 7a5691a | 2015-05-23 00:36:32 +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; |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 17 | |
cheng fan | 00a406d | 2015-06-05 21:42:40 +0800 | [diff] [blame] | 18 | import java.util.Collection; |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 19 | import java.util.Optional; |
| 20 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 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; |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 24 | import org.onlab.packet.IpAddress; |
| 25 | import org.onosproject.cli.AbstractShellCommand; |
| 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.TunnelProvider; |
cheng fan | 00a406d | 2015-06-05 21:42:40 +0800 | [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; |
| 40 | |
| 41 | /** |
| 42 | * Supports for removing tunnels. It's used by producers. |
| 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-remove", description = "Supports for removing tunnels. It's used by producers.") |
| 46 | public class TunnelRemoveCommand extends AbstractShellCommand { |
| 47 | @Option(name = "-s", aliases = "--src", description = "Source tunnel point." |
| 48 | + " Only supports for IpTunnelEndPoint and OpticalTunnelEndPoint as end point now." |
| 49 | + " If deletess a ODUK or OCH type tunnel, the formatter of this argument is DeviceId-PortNumber." |
| 50 | + " Otherwise src means IP address.", required = false, multiValued = false) |
| 51 | String src = null; |
| 52 | @Option(name = "-d", aliases = "--dst", description = "Destination tunnel point." |
| 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." |
| 55 | + " Otherwise dst means IP address.", required = false, multiValued = false) |
| 56 | String dst = null; |
| 57 | |
| 58 | @Option(name = "-t", aliases = "--type", description = "The type of tunnels," |
| 59 | + " It includes MPLS, VLAN, VXLAN, GRE, ODUK, OCH", required = false, multiValued = false) |
| 60 | String type = null; |
| 61 | |
| 62 | @Option(name = "-i", aliases = "--tunnelId", |
| 63 | description = "the tunnel identity.", required = false, multiValued = false) |
| 64 | String tunnelId = null; |
| 65 | |
| 66 | @Override |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 67 | protected void doExecute() { |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 68 | TunnelDescription tunnel = null; |
| 69 | TunnelProvider service = get(TunnelProvider.class); |
| 70 | ProviderId producerName = new ProviderId("default", |
| 71 | "org.onosproject.provider.tunnel.default"); |
| 72 | if (!isNull(src) && !isNull(dst) && !isNull(type)) { |
| 73 | TunnelEndPoint srcPoint = null; |
| 74 | TunnelEndPoint dstPoint = null; |
| 75 | Tunnel.Type trueType = null; |
| 76 | if ("MPLS".equals(type)) { |
| 77 | trueType = Tunnel.Type.MPLS; |
| 78 | srcPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress |
| 79 | .valueOf(src)); |
| 80 | dstPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress |
| 81 | .valueOf(dst)); |
| 82 | } else if ("VLAN".equals(type)) { |
| 83 | trueType = Tunnel.Type.VLAN; |
| 84 | srcPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress |
| 85 | .valueOf(src)); |
| 86 | dstPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress |
| 87 | .valueOf(dst)); |
| 88 | } else if ("VXLAN".equals(type)) { |
| 89 | trueType = Tunnel.Type.VXLAN; |
| 90 | srcPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress |
| 91 | .valueOf(src)); |
| 92 | dstPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress |
| 93 | .valueOf(dst)); |
| 94 | } else if ("GRE".equals(type)) { |
| 95 | trueType = Tunnel.Type.GRE; |
| 96 | srcPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress |
| 97 | .valueOf(src)); |
| 98 | dstPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress |
| 99 | .valueOf(dst)); |
| 100 | } else if ("ODUK".equals(type)) { |
| 101 | trueType = Tunnel.Type.ODUK; |
cheng fan | 00a406d | 2015-06-05 21:42:40 +0800 | [diff] [blame] | 102 | String[] srcArray = src.split("/"); |
| 103 | String[] dstArray = dst.split("/"); |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 104 | srcPoint = new DefaultOpticalTunnelEndPoint( |
| 105 | producerName, |
| 106 | Optional.of(DeviceId |
| 107 | .deviceId(srcArray[0])), |
| 108 | Optional.of(PortNumber |
| 109 | .portNumber(srcArray[1])), |
| 110 | null, |
| 111 | OpticalTunnelEndPoint.Type.LAMBDA, |
| 112 | OpticalLogicId |
| 113 | .logicId(0), |
| 114 | true); |
| 115 | dstPoint = new DefaultOpticalTunnelEndPoint( |
| 116 | producerName, |
| 117 | Optional.of(DeviceId |
| 118 | .deviceId(dstArray[0])), |
| 119 | Optional.of(PortNumber |
| 120 | .portNumber(dstArray[1])), |
| 121 | null, |
| 122 | OpticalTunnelEndPoint.Type.LAMBDA, |
| 123 | OpticalLogicId |
| 124 | .logicId(0), |
| 125 | true); |
| 126 | } else if ("OCH".equals(type)) { |
| 127 | trueType = Tunnel.Type.OCH; |
cheng fan | 00a406d | 2015-06-05 21:42:40 +0800 | [diff] [blame] | 128 | String[] srcArray = src.split("/"); |
| 129 | String[] dstArray = dst.split("/"); |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 130 | srcPoint = new DefaultOpticalTunnelEndPoint( |
| 131 | producerName, |
| 132 | Optional.of(DeviceId |
| 133 | .deviceId(srcArray[0])), |
| 134 | Optional.of(PortNumber |
| 135 | .portNumber(srcArray[1])), |
| 136 | null, |
| 137 | OpticalTunnelEndPoint.Type.LAMBDA, |
| 138 | OpticalLogicId |
| 139 | .logicId(0), |
| 140 | true); |
| 141 | dstPoint = new DefaultOpticalTunnelEndPoint( |
| 142 | producerName, |
| 143 | Optional.of(DeviceId |
| 144 | .deviceId(dstArray[0])), |
| 145 | Optional.of(PortNumber |
| 146 | .portNumber(dstArray[1])), |
| 147 | null, |
| 148 | OpticalTunnelEndPoint.Type.LAMBDA, |
| 149 | OpticalLogicId |
| 150 | .logicId(0), |
| 151 | true); |
| 152 | } else { |
| 153 | print("Illegal tunnel type. Please input MPLS, VLAN, VXLAN, GRE, ODUK or OCH."); |
| 154 | return; |
| 155 | } |
| 156 | |
| 157 | tunnel = new DefaultTunnelDescription(null, srcPoint, dstPoint, |
| 158 | trueType, null, producerName, |
| 159 | null, null); |
| 160 | service.tunnelRemoved(tunnel); |
cheng fan | 00a406d | 2015-06-05 21:42:40 +0800 | [diff] [blame] | 161 | return; |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 162 | } |
| 163 | if (!isNull(tunnelId)) { |
| 164 | TunnelId id = TunnelId.valueOf(tunnelId); |
| 165 | tunnel = new DefaultTunnelDescription(id, null, null, null, null, |
| 166 | producerName, null, null); |
| 167 | service.tunnelRemoved(tunnel); |
cheng fan | 00a406d | 2015-06-05 21:42:40 +0800 | [diff] [blame] | 168 | return; |
| 169 | } |
| 170 | |
| 171 | if (!isNull(type)) { |
| 172 | Tunnel.Type trueType = null; |
| 173 | Collection<Tunnel> tunnelSet = null; |
| 174 | TunnelService tunnelService = get(TunnelService.class); |
| 175 | if ("MPLS".equals(type)) { |
| 176 | trueType = Tunnel.Type.MPLS; |
| 177 | } else if ("VLAN".equals(type)) { |
| 178 | trueType = Tunnel.Type.VLAN; |
| 179 | } else if ("VXLAN".equals(type)) { |
| 180 | trueType = Tunnel.Type.VXLAN; |
| 181 | } else if ("GRE".equals(type)) { |
| 182 | trueType = Tunnel.Type.GRE; |
| 183 | } else if ("ODUK".equals(type)) { |
| 184 | trueType = Tunnel.Type.ODUK; |
| 185 | } else if ("OCH".equals(type)) { |
| 186 | trueType = Tunnel.Type.OCH; |
| 187 | } else { |
| 188 | print("Illegal tunnel type. Please input MPLS, VLAN, VXLAN, GRE, ODUK or OCH."); |
| 189 | return; |
| 190 | } |
| 191 | tunnelSet = tunnelService.queryTunnel(trueType); |
| 192 | if (tunnelSet != null) { |
| 193 | for (Tunnel tunnelTemp : tunnelSet) { |
| 194 | tunnel = new DefaultTunnelDescription(tunnelTemp.tunnelId(), null, null, null, null, |
| 195 | producerName, null, null); |
| 196 | service.tunnelRemoved(tunnel); |
| 197 | } |
| 198 | } |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | |
| 202 | private boolean isNull(String s) { |
| 203 | return s == null || "".equals(s); |
| 204 | } |
| 205 | } |