samuel | 7a5691a | 2015-05-23 00:36:32 +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.provider.tunnel.cli; |
| 17 | |
| 18 | import java.util.Optional; |
| 19 | |
| 20 | import org.apache.karaf.shell.commands.Command; |
| 21 | import org.apache.karaf.shell.commands.Option; |
| 22 | import org.onlab.packet.IpAddress; |
| 23 | import org.onosproject.cli.AbstractShellCommand; |
| 24 | import org.onosproject.incubator.net.tunnel.DefaultOpticalTunnelEndPoint; |
| 25 | import org.onosproject.incubator.net.tunnel.DefaultTunnelDescription; |
| 26 | import org.onosproject.incubator.net.tunnel.IpTunnelEndPoint; |
| 27 | import org.onosproject.incubator.net.tunnel.OpticalLogicId; |
| 28 | import org.onosproject.incubator.net.tunnel.OpticalTunnelEndPoint; |
| 29 | import org.onosproject.incubator.net.tunnel.Tunnel; |
| 30 | import org.onosproject.incubator.net.tunnel.TunnelDescription; |
| 31 | import org.onosproject.incubator.net.tunnel.TunnelEndPoint; |
| 32 | import org.onosproject.incubator.net.tunnel.TunnelId; |
| 33 | import org.onosproject.incubator.net.tunnel.TunnelProvider; |
| 34 | import org.onosproject.net.DeviceId; |
| 35 | import org.onosproject.net.PortNumber; |
| 36 | import org.onosproject.net.provider.ProviderId; |
| 37 | |
| 38 | /** |
| 39 | * Supports for removing tunnels. It's used by producers. |
| 40 | */ |
| 41 | @Command(scope = "onos", name = "tunnel-remove", description = "Supports for removing tunnels. It's used by producers.") |
| 42 | public class TunnelRemoveCommand extends AbstractShellCommand { |
| 43 | @Option(name = "-s", aliases = "--src", description = "Source tunnel point." |
| 44 | + " Only supports for IpTunnelEndPoint and OpticalTunnelEndPoint as end point now." |
| 45 | + " If deletess a ODUK or OCH type tunnel, the formatter of this argument is DeviceId-PortNumber." |
| 46 | + " Otherwise src means IP address.", required = false, multiValued = false) |
| 47 | String src = null; |
| 48 | @Option(name = "-d", aliases = "--dst", description = "Destination tunnel point." |
| 49 | + " Only supports for IpTunnelEndPoint and OpticalTunnelEndPoint as end point now." |
| 50 | + " If deletess a ODUK or OCH type tunnel, the formatter of this argument is DeviceId-PortNumber." |
| 51 | + " Otherwise dst means IP address.", required = false, multiValued = false) |
| 52 | String dst = null; |
| 53 | |
| 54 | @Option(name = "-t", aliases = "--type", description = "The type of tunnels," |
| 55 | + " It includes MPLS, VLAN, VXLAN, GRE, ODUK, OCH", required = false, multiValued = false) |
| 56 | String type = null; |
| 57 | |
| 58 | @Option(name = "-i", aliases = "--tunnelId", |
| 59 | description = "the tunnel identity.", required = false, multiValued = false) |
| 60 | String tunnelId = null; |
| 61 | |
| 62 | @Override |
| 63 | protected void execute() { |
| 64 | TunnelDescription tunnel = null; |
| 65 | TunnelProvider service = get(TunnelProvider.class); |
| 66 | ProviderId producerName = new ProviderId("default", |
| 67 | "org.onosproject.provider.tunnel.default"); |
| 68 | if (!isNull(src) && !isNull(dst) && !isNull(type)) { |
| 69 | TunnelEndPoint srcPoint = null; |
| 70 | TunnelEndPoint dstPoint = null; |
| 71 | Tunnel.Type trueType = null; |
| 72 | if ("MPLS".equals(type)) { |
| 73 | trueType = Tunnel.Type.MPLS; |
| 74 | srcPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress |
| 75 | .valueOf(src)); |
| 76 | dstPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress |
| 77 | .valueOf(dst)); |
| 78 | } else if ("VLAN".equals(type)) { |
| 79 | trueType = Tunnel.Type.VLAN; |
| 80 | srcPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress |
| 81 | .valueOf(src)); |
| 82 | dstPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress |
| 83 | .valueOf(dst)); |
| 84 | } else if ("VXLAN".equals(type)) { |
| 85 | trueType = Tunnel.Type.VXLAN; |
| 86 | srcPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress |
| 87 | .valueOf(src)); |
| 88 | dstPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress |
| 89 | .valueOf(dst)); |
| 90 | } else if ("GRE".equals(type)) { |
| 91 | trueType = Tunnel.Type.GRE; |
| 92 | srcPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress |
| 93 | .valueOf(src)); |
| 94 | dstPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress |
| 95 | .valueOf(dst)); |
| 96 | } else if ("ODUK".equals(type)) { |
| 97 | trueType = Tunnel.Type.ODUK; |
| 98 | String[] srcArray = src.split("-"); |
| 99 | String[] dstArray = dst.split("-"); |
| 100 | srcPoint = new DefaultOpticalTunnelEndPoint( |
| 101 | producerName, |
| 102 | Optional.of(DeviceId |
| 103 | .deviceId(srcArray[0])), |
| 104 | Optional.of(PortNumber |
| 105 | .portNumber(srcArray[1])), |
| 106 | null, |
| 107 | OpticalTunnelEndPoint.Type.LAMBDA, |
| 108 | OpticalLogicId |
| 109 | .logicId(0), |
| 110 | true); |
| 111 | dstPoint = new DefaultOpticalTunnelEndPoint( |
| 112 | producerName, |
| 113 | Optional.of(DeviceId |
| 114 | .deviceId(dstArray[0])), |
| 115 | Optional.of(PortNumber |
| 116 | .portNumber(dstArray[1])), |
| 117 | null, |
| 118 | OpticalTunnelEndPoint.Type.LAMBDA, |
| 119 | OpticalLogicId |
| 120 | .logicId(0), |
| 121 | true); |
| 122 | } else if ("OCH".equals(type)) { |
| 123 | trueType = Tunnel.Type.OCH; |
| 124 | String[] srcArray = src.split("-"); |
| 125 | String[] dstArray = dst.split("-"); |
| 126 | srcPoint = new DefaultOpticalTunnelEndPoint( |
| 127 | producerName, |
| 128 | Optional.of(DeviceId |
| 129 | .deviceId(srcArray[0])), |
| 130 | Optional.of(PortNumber |
| 131 | .portNumber(srcArray[1])), |
| 132 | null, |
| 133 | OpticalTunnelEndPoint.Type.LAMBDA, |
| 134 | OpticalLogicId |
| 135 | .logicId(0), |
| 136 | true); |
| 137 | dstPoint = new DefaultOpticalTunnelEndPoint( |
| 138 | producerName, |
| 139 | Optional.of(DeviceId |
| 140 | .deviceId(dstArray[0])), |
| 141 | Optional.of(PortNumber |
| 142 | .portNumber(dstArray[1])), |
| 143 | null, |
| 144 | OpticalTunnelEndPoint.Type.LAMBDA, |
| 145 | OpticalLogicId |
| 146 | .logicId(0), |
| 147 | true); |
| 148 | } else { |
| 149 | print("Illegal tunnel type. Please input MPLS, VLAN, VXLAN, GRE, ODUK or OCH."); |
| 150 | return; |
| 151 | } |
| 152 | |
| 153 | tunnel = new DefaultTunnelDescription(null, srcPoint, dstPoint, |
| 154 | trueType, null, producerName, |
| 155 | null, null); |
| 156 | service.tunnelRemoved(tunnel); |
| 157 | } |
| 158 | if (!isNull(tunnelId)) { |
| 159 | TunnelId id = TunnelId.valueOf(tunnelId); |
| 160 | tunnel = new DefaultTunnelDescription(id, null, null, null, null, |
| 161 | producerName, null, null); |
| 162 | service.tunnelRemoved(tunnel); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | private boolean isNull(String s) { |
| 167 | return s == null || "".equals(s); |
| 168 | } |
| 169 | } |