jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
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 | |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 22 | import org.apache.karaf.shell.commands.Command; |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 23 | import org.apache.karaf.shell.commands.Option; |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 24 | import org.onlab.packet.IpAddress; |
| 25 | import org.onosproject.cli.AbstractShellCommand; |
Thomas Vachuska | bf916ea | 2015-05-20 18:24:34 -0700 | [diff] [blame] | 26 | import org.onosproject.incubator.net.tunnel.DefaultOpticalTunnelEndPoint; |
| 27 | import org.onosproject.incubator.net.tunnel.IpTunnelEndPoint; |
| 28 | import org.onosproject.incubator.net.tunnel.OpticalLogicId; |
| 29 | import org.onosproject.incubator.net.tunnel.OpticalTunnelEndPoint; |
| 30 | import org.onosproject.incubator.net.tunnel.Tunnel; |
| 31 | import org.onosproject.incubator.net.tunnel.TunnelEndPoint; |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 32 | import org.onosproject.incubator.net.tunnel.TunnelId; |
Thomas Vachuska | bf916ea | 2015-05-20 18:24:34 -0700 | [diff] [blame] | 33 | import org.onosproject.incubator.net.tunnel.TunnelService; |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 34 | import org.onosproject.net.DeviceId; |
| 35 | import org.onosproject.net.Link; |
| 36 | import org.onosproject.net.Path; |
| 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 | * Supports for querying 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 = "tunnels", description = "Supports for querying tunnels." |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 44 | + " It's used by consumers.") |
Thomas Vachuska | bf916ea | 2015-05-20 18:24:34 -0700 | [diff] [blame] | 45 | public class TunnelQueryCommand extends AbstractShellCommand { |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 46 | @Option(name = "-s", aliases = "--src", description = "Source tunnel point." |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 47 | + " Only supports for IpTunnelEndPoint and OpticalTunnelEndPoint as end point now." |
| 48 | + " 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] | 49 | + " Otherwise src means IP address.", required = false, multiValued = false) |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 50 | String src = null; |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 51 | @Option(name = "-d", aliases = "--dst", description = "Destination tunnel point." |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 52 | + " Only supports for IpTunnelEndPoint and OpticalTunnelEndPoint as end point now." |
| 53 | + " 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] | 54 | + " Otherwise dst means IP address.", required = false, multiValued = false) |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 55 | String dst = null; |
| 56 | |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 57 | @Option(name = "-t", aliases = "--type", description = "The type of tunnels," |
| 58 | + " It includes MPLS, VLAN, VXLAN, GRE, ODUK, OCH", required = false, multiValued = false) |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 59 | String type = null; |
| 60 | |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 61 | @Option(name = "-i", aliases = "--tunnelId", |
| 62 | description = "the tunnel identity.", required = false, multiValued = false) |
| 63 | String tunnelId = null; |
| 64 | |
| 65 | private static final String FMT = "tunnelId=%s, src=%s, dst=%s," |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 66 | + "type=%s, state=%s, producerName=%s, tunnelName=%s," |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 67 | + "groupId=%s, path=%s%s"; |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 68 | |
| 69 | @Override |
| 70 | protected void execute() { |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 71 | Tunnel.Type trueType = null; |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 72 | TunnelService service = get(TunnelService.class); |
| 73 | ProviderId producerName = new ProviderId("default", |
| 74 | "org.onosproject.provider.tunnel.default"); |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 75 | Collection<Tunnel> tunnelSet = null; |
| 76 | if (isNull(src) && isNull(dst) && isNull(type) && isNull(tunnelId)) { |
| 77 | tunnelSet = service.queryAllTunnels(); |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 78 | } |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 79 | |
| 80 | if (!isNull(src) && !isNull(dst) && !isNull(type)) { |
| 81 | TunnelEndPoint srcPoint = null; |
| 82 | TunnelEndPoint dstPoint = null; |
| 83 | if ("MPLS".equals(type) || "VXLAN".equals(type) |
| 84 | || "GRE".equals(type)) { |
| 85 | srcPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress |
| 86 | .valueOf(src)); |
| 87 | dstPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress |
| 88 | .valueOf(dst)); |
| 89 | } else if ("VLAN".equals(type)) { |
cheng fan | 00a406d | 2015-06-05 21:42:40 +0800 | [diff] [blame] | 90 | String[] srcArray = src.split("/"); |
| 91 | String[] dstArray = dst.split("/"); |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 92 | srcPoint = new DefaultOpticalTunnelEndPoint( |
| 93 | producerName, |
| 94 | Optional.of(DeviceId |
| 95 | .deviceId(srcArray[0])), |
| 96 | Optional.of(PortNumber |
| 97 | .portNumber(srcArray[1])), |
| 98 | null, |
| 99 | null, |
| 100 | OpticalLogicId |
| 101 | .logicId(0), |
| 102 | true); |
| 103 | dstPoint = new DefaultOpticalTunnelEndPoint( |
| 104 | producerName, |
| 105 | Optional.of(DeviceId |
| 106 | .deviceId(dstArray[0])), |
| 107 | Optional.of(PortNumber |
| 108 | .portNumber(dstArray[1])), |
| 109 | null, |
| 110 | null, |
| 111 | OpticalLogicId |
| 112 | .logicId(0), |
| 113 | true); |
| 114 | } else if ("ODUK".equals(type)) { |
cheng fan | 00a406d | 2015-06-05 21:42:40 +0800 | [diff] [blame] | 115 | String[] srcArray = src.split("/"); |
| 116 | String[] dstArray = dst.split("/"); |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 117 | srcPoint = new DefaultOpticalTunnelEndPoint( |
| 118 | producerName, |
| 119 | Optional.of(DeviceId |
| 120 | .deviceId(srcArray[0])), |
| 121 | Optional.of(PortNumber |
| 122 | .portNumber(srcArray[1])), |
| 123 | null, |
| 124 | OpticalTunnelEndPoint.Type.LAMBDA, |
| 125 | OpticalLogicId |
| 126 | .logicId(0), |
| 127 | true); |
| 128 | dstPoint = new DefaultOpticalTunnelEndPoint( |
| 129 | producerName, |
| 130 | Optional.of(DeviceId |
| 131 | .deviceId(dstArray[0])), |
| 132 | Optional.of(PortNumber |
| 133 | .portNumber(dstArray[1])), |
| 134 | null, |
| 135 | OpticalTunnelEndPoint.Type.LAMBDA, |
| 136 | OpticalLogicId |
| 137 | .logicId(0), |
| 138 | true); |
| 139 | } else if ("OCH".equals(type)) { |
cheng fan | 00a406d | 2015-06-05 21:42:40 +0800 | [diff] [blame] | 140 | String[] srcArray = src.split("/"); |
| 141 | String[] dstArray = dst.split("/"); |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 142 | srcPoint = new DefaultOpticalTunnelEndPoint( |
| 143 | producerName, |
| 144 | Optional.of(DeviceId |
| 145 | .deviceId(srcArray[0])), |
| 146 | Optional.of(PortNumber |
| 147 | .portNumber(srcArray[1])), |
| 148 | null, |
| 149 | OpticalTunnelEndPoint.Type.TIMESLOT, |
| 150 | OpticalLogicId |
| 151 | .logicId(0), |
| 152 | true); |
| 153 | dstPoint = new DefaultOpticalTunnelEndPoint( |
| 154 | producerName, |
| 155 | Optional.of(DeviceId |
| 156 | .deviceId(dstArray[0])), |
| 157 | Optional.of(PortNumber |
| 158 | .portNumber(dstArray[1])), |
| 159 | null, |
| 160 | OpticalTunnelEndPoint.Type.TIMESLOT, |
| 161 | OpticalLogicId |
| 162 | .logicId(0), |
| 163 | true); |
| 164 | } else { |
| 165 | print("Illegal tunnel type. Please input MPLS, VLAN, VXLAN, GRE, ODUK or OCH."); |
| 166 | return; |
| 167 | } |
| 168 | tunnelSet = service.queryTunnel(srcPoint, dstPoint); |
| 169 | } |
| 170 | if (!isNull(type)) { |
| 171 | if ("MPLS".equals(type)) { |
| 172 | trueType = Tunnel.Type.MPLS; |
| 173 | } else if ("VLAN".equals(type)) { |
| 174 | trueType = Tunnel.Type.VLAN; |
| 175 | } else if ("VXLAN".equals(type)) { |
| 176 | trueType = Tunnel.Type.VXLAN; |
| 177 | } else if ("GRE".equals(type)) { |
| 178 | trueType = Tunnel.Type.GRE; |
| 179 | } else if ("ODUK".equals(type)) { |
| 180 | trueType = Tunnel.Type.ODUK; |
| 181 | } else if ("OCH".equals(type)) { |
| 182 | trueType = Tunnel.Type.OCH; |
| 183 | } else { |
| 184 | print("Illegal tunnel type. Please input MPLS, VLAN, VXLAN, GRE, ODUK or OCH."); |
| 185 | return; |
| 186 | } |
| 187 | tunnelSet = service.queryTunnel(trueType); |
| 188 | } |
| 189 | if (!isNull(tunnelId)) { |
| 190 | TunnelId id = TunnelId.valueOf(tunnelId); |
| 191 | Tunnel tunnel = service.queryTunnel(id); |
| 192 | tunnelSet = new HashSet<Tunnel>(); |
| 193 | tunnelSet.add(tunnel); |
| 194 | } |
| 195 | if (tunnelSet != null) { |
| 196 | for (Tunnel tunnel : tunnelSet) { |
cheng fan | 00a406d | 2015-06-05 21:42:40 +0800 | [diff] [blame] | 197 | print(FMT, tunnel.tunnelId().id(), tunnel.src().toString(), tunnel.dst().toString(), |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 198 | tunnel.type(), tunnel.state(), tunnel.providerId(), |
| 199 | tunnel.tunnelName(), tunnel.groupId(), |
| 200 | showPath(tunnel.path()), |
| 201 | annotations(tunnel.annotations())); |
| 202 | } |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 203 | } |
| 204 | } |
| 205 | |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 206 | private String showPath(Path path) { |
| 207 | if (path == null) { |
cheng fan | 93258c7 | 2015-06-02 23:42:32 +0800 | [diff] [blame] | 208 | return "null"; |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 209 | } |
| 210 | StringBuilder builder = new StringBuilder("("); |
| 211 | for (Link link : path.links()) { |
| 212 | builder.append("(DeviceId:" + link.src().deviceId() + " Port:" |
| 213 | + link.src().port().toString()); |
| 214 | builder.append(" DeviceId:" + link.dst().deviceId() + " Port:" |
| 215 | + link.dst().port().toString() + ")"); |
| 216 | } |
| 217 | builder.append(annotations(path.annotations()) + ")"); |
| 218 | return builder.toString(); |
| 219 | } |
| 220 | |
| 221 | private boolean isNull(String s) { |
| 222 | return s == null || "".equals(s); |
| 223 | } |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 224 | } |