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