jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 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 static com.google.common.base.Preconditions.checkArgument; |
| 19 | |
| 20 | import java.util.Optional; |
| 21 | |
| 22 | import org.apache.karaf.shell.commands.Argument; |
| 23 | import org.apache.karaf.shell.commands.Command; |
| 24 | import org.onlab.packet.IpAddress; |
| 25 | import org.onosproject.cli.AbstractShellCommand; |
| 26 | import org.onosproject.core.DefaultGroupId; |
| 27 | import org.onosproject.net.DefaultAnnotations; |
| 28 | import org.onosproject.net.DeviceId; |
| 29 | import org.onosproject.net.PortNumber; |
| 30 | import org.onosproject.net.SparseAnnotations; |
| 31 | import org.onosproject.net.provider.ProviderId; |
| 32 | import org.onosproject.net.tunnel.DefaultOpticalTunnelEndPoint; |
| 33 | import org.onosproject.net.tunnel.DefaultTunnelDescription; |
| 34 | import org.onosproject.net.tunnel.IpTunnelEndPoint; |
| 35 | import org.onosproject.net.tunnel.OpticalLogicId; |
| 36 | import org.onosproject.net.tunnel.OpticalTunnelEndPoint; |
| 37 | import org.onosproject.net.tunnel.Tunnel; |
| 38 | import org.onosproject.net.tunnel.TunnelDescription; |
| 39 | import org.onosproject.net.tunnel.TunnelEndPoint; |
| 40 | import org.onosproject.net.tunnel.TunnelId; |
| 41 | import org.onosproject.net.tunnel.TunnelName; |
| 42 | import org.onosproject.net.tunnel.TunnelProvider; |
| 43 | |
| 44 | /** |
| 45 | * Supports for creating a tunnel by using IP address and optical as tunnel end |
| 46 | * point. |
| 47 | */ |
| 48 | @Command(scope = "onos", name = "create-tunnels", |
| 49 | description = "Supports for creating a tunnel by using IP address and optical as tunnel end point now.") |
| 50 | public class CreateTunnelCommand extends AbstractShellCommand { |
| 51 | |
| 52 | @Argument(index = 0, name = "src", description = "Source tunnel point." |
| 53 | + " Only supports for IpTunnelEndPoint and OpticalTunnelEndPoint as end point now." |
| 54 | + " If creates a ODUK or OCH type tunnel, the formatter of this argument is DeviceId-PortNumber." |
| 55 | + " Otherwise src means IP address.", required = true, multiValued = false) |
| 56 | String src = null; |
| 57 | |
| 58 | @Argument(index = 1, name = "dst", description = "Destination tunnel point." |
| 59 | + " Only supports for IpTunnelEndPoint and OpticalTunnelEndPoint as end point now." |
| 60 | + " If creates a ODUK or OCH type tunnel, the formatter of this argument is DeviceId-PortNumber." |
| 61 | + " Otherwise dst means IP address.", required = true, multiValued = false) |
| 62 | String dst = null; |
| 63 | @Argument(index = 2, name = "type", description = "The type of tunnels," |
| 64 | + " It includes MPLS, VLAN, VXLAN, GRE, ODUK, OCH", required = true, multiValued = false) |
| 65 | String type = null; |
| 66 | @Argument(index = 3, name = "groupId", |
| 67 | description = "Group flow table id which a tunnel match up", required = true, multiValued = false) |
| 68 | String groupId = null; |
| 69 | |
| 70 | @Argument(index = 4, name = "tunnelName", |
| 71 | description = "The name of tunnels", required = false, multiValued = false) |
| 72 | String tunnelName = null; |
| 73 | |
| 74 | @Argument(index = 5, name = "bandWith", |
| 75 | description = "The bandWith attribute of tunnel", required = false, multiValued = false) |
| 76 | String bandWith = null; |
| 77 | |
| 78 | private static final String FMT = "The tunnel identity is %s"; |
| 79 | |
| 80 | @Override |
| 81 | protected void execute() { |
| 82 | TunnelProvider service = get(TunnelProvider.class); |
| 83 | ProviderId producerName = new ProviderId("default", |
| 84 | "org.onosproject.provider.tunnel.default"); |
| 85 | TunnelEndPoint srcPoint = null; |
| 86 | TunnelEndPoint dstPoint = null; |
| 87 | Tunnel.Type trueType = null; |
| 88 | if ("MPLS".equals(type)) { |
| 89 | trueType = Tunnel.Type.MPLS; |
| 90 | srcPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress.valueOf(src)); |
| 91 | dstPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress.valueOf(dst)); |
| 92 | } else if ("VLAN".equals(type)) { |
| 93 | trueType = Tunnel.Type.VLAN; |
| 94 | srcPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress.valueOf(src)); |
| 95 | dstPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress.valueOf(dst)); |
| 96 | } else if ("VXLAN".equals(type)) { |
| 97 | trueType = Tunnel.Type.VXLAN; |
| 98 | srcPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress.valueOf(src)); |
| 99 | dstPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress.valueOf(dst)); |
| 100 | } else if ("GRE".equals(type)) { |
| 101 | trueType = Tunnel.Type.GRE; |
| 102 | srcPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress.valueOf(src)); |
| 103 | dstPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress.valueOf(dst)); |
| 104 | } else if ("ODUK".equals(type)) { |
| 105 | trueType = Tunnel.Type.ODUK; |
| 106 | String[] srcArray = src.split("||"); |
| 107 | checkArgument(srcArray.length < 2, "Illegal src formatter."); |
| 108 | String[] dstArray = dst.split("||"); |
| 109 | checkArgument(dstArray.length < 2, "Illegal dst formatter."); |
| 110 | srcPoint = new DefaultOpticalTunnelEndPoint( |
| 111 | producerName, |
| 112 | Optional.of(DeviceId |
| 113 | .deviceId(srcArray[0])), |
| 114 | Optional.of(PortNumber |
| 115 | .portNumber(srcArray[1])), |
| 116 | null, |
| 117 | OpticalTunnelEndPoint.Type.LAMBDA, |
| 118 | OpticalLogicId |
| 119 | .logicId(0), |
| 120 | true); |
| 121 | dstPoint = new DefaultOpticalTunnelEndPoint( |
| 122 | producerName, |
| 123 | Optional.of(DeviceId |
| 124 | .deviceId(dstArray[0])), |
| 125 | Optional.of(PortNumber |
| 126 | .portNumber(dstArray[1])), |
| 127 | null, |
| 128 | OpticalTunnelEndPoint.Type.LAMBDA, |
| 129 | OpticalLogicId |
| 130 | .logicId(0), |
| 131 | true); |
| 132 | } else if ("OCH".equals(type)) { |
| 133 | trueType = Tunnel.Type.OCH; |
| 134 | String[] srcArray = src.split("-"); |
| 135 | String[] dstArray = dst.split("-"); |
| 136 | srcPoint = new DefaultOpticalTunnelEndPoint( |
| 137 | producerName, |
| 138 | Optional.of(DeviceId |
| 139 | .deviceId(srcArray[0])), |
| 140 | Optional.of(PortNumber |
| 141 | .portNumber(srcArray[1])), |
| 142 | null, |
| 143 | OpticalTunnelEndPoint.Type.LAMBDA, |
| 144 | OpticalLogicId |
| 145 | .logicId(0), |
| 146 | true); |
| 147 | dstPoint = new DefaultOpticalTunnelEndPoint( |
| 148 | producerName, |
| 149 | Optional.of(DeviceId |
| 150 | .deviceId(dstArray[0])), |
| 151 | Optional.of(PortNumber |
| 152 | .portNumber(dstArray[1])), |
| 153 | null, |
| 154 | OpticalTunnelEndPoint.Type.LAMBDA, |
| 155 | OpticalLogicId |
| 156 | .logicId(0), |
| 157 | true); |
| 158 | } else { |
| 159 | print("Illegal tunnel type. Please input MPLS, VLAN, VXLAN, GRE, ODUK or OCH."); |
| 160 | return; |
| 161 | } |
| 162 | |
| 163 | SparseAnnotations annotations = DefaultAnnotations |
| 164 | .builder() |
| 165 | .set("bandWith", bandWith == null && "".equals(bandWith) ? "0" : bandWith) |
| 166 | .build(); |
| 167 | TunnelDescription tunnel = new DefaultTunnelDescription( |
| 168 | null, |
| 169 | srcPoint, |
| 170 | dstPoint, |
| 171 | trueType, |
| 172 | new DefaultGroupId( |
| 173 | Integer.valueOf(groupId) |
| 174 | .intValue()), |
| 175 | producerName, |
| 176 | TunnelName |
| 177 | .tunnelName(tunnelName), |
| 178 | annotations); |
| 179 | TunnelId tunnelId = service.tunnelAdded(tunnel); |
| 180 | print(FMT, tunnelId.id()); |
| 181 | } |
| 182 | |
| 183 | } |