Yoonseon Han | 6c60389 | 2016-09-01 11:52:21 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Yoonseon Han | 6c60389 | 2016-09-01 11:52:21 -0700 | [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 | |
| 17 | package org.onosproject.cli.net.vnet; |
| 18 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 19 | import org.apache.karaf.shell.api.action.Argument; |
| 20 | import org.apache.karaf.shell.api.action.Command; |
Ray Milkey | 0068fd0 | 2018-10-11 15:45:39 -0700 | [diff] [blame] | 21 | import org.apache.karaf.shell.api.action.Completion; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 22 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
Yoonseon Han | 6c60389 | 2016-09-01 11:52:21 -0700 | [diff] [blame] | 23 | import org.onosproject.cli.AbstractShellCommand; |
Ray Milkey | 0068fd0 | 2018-10-11 15:45:39 -0700 | [diff] [blame] | 24 | import org.onosproject.cli.net.DeviceIdCompleter; |
| 25 | import org.onosproject.cli.net.PortNumberCompleter; |
Yoonseon Han | 6c60389 | 2016-09-01 11:52:21 -0700 | [diff] [blame] | 26 | import org.onosproject.incubator.net.virtual.NetworkId; |
| 27 | import org.onosproject.incubator.net.virtual.VirtualNetworkAdminService; |
| 28 | import org.onosproject.incubator.net.virtual.VirtualNetworkService; |
| 29 | import org.onosproject.incubator.net.virtual.VirtualPort; |
| 30 | import org.onosproject.net.ConnectPoint; |
| 31 | import org.onosproject.net.DeviceId; |
| 32 | import org.onosproject.net.PortNumber; |
| 33 | import org.onosproject.net.device.DeviceService; |
| 34 | |
| 35 | import java.util.Set; |
| 36 | |
| 37 | import static com.google.common.base.Preconditions.checkNotNull; |
| 38 | |
| 39 | /** |
| 40 | * Binds an existing virtual port with a physical port. |
| 41 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 42 | @Service |
Yoonseon Han | 6c60389 | 2016-09-01 11:52:21 -0700 | [diff] [blame] | 43 | @Command(scope = "onos", name = "vnet-bind-port", |
| 44 | description = "Binds an existing virtual port with a physical port.") |
| 45 | public class VirtualPortBindCommand extends AbstractShellCommand { |
| 46 | @Argument(index = 0, name = "networkId", description = "Network ID", |
| 47 | required = true, multiValued = false) |
Ray Milkey | 0068fd0 | 2018-10-11 15:45:39 -0700 | [diff] [blame] | 48 | @Completion(VirtualNetworkCompleter.class) |
Yoonseon Han | 6c60389 | 2016-09-01 11:52:21 -0700 | [diff] [blame] | 49 | Long networkId = null; |
| 50 | |
| 51 | @Argument(index = 1, name = "deviceId", description = "Virtual Device ID", |
| 52 | required = true, multiValued = false) |
Ray Milkey | 0068fd0 | 2018-10-11 15:45:39 -0700 | [diff] [blame] | 53 | @Completion(VirtualDeviceCompleter.class) |
Yoonseon Han | 6c60389 | 2016-09-01 11:52:21 -0700 | [diff] [blame] | 54 | String deviceId = null; |
| 55 | |
| 56 | @Argument(index = 2, name = "portNum", description = "Virtual device port number", |
| 57 | required = true, multiValued = false) |
Ray Milkey | 0068fd0 | 2018-10-11 15:45:39 -0700 | [diff] [blame] | 58 | @Completion(VirtualPortCompleter.class) |
Yoonseon Han | 6c60389 | 2016-09-01 11:52:21 -0700 | [diff] [blame] | 59 | Integer portNum = null; |
| 60 | |
| 61 | @Argument(index = 3, name = "physDeviceId", description = "Physical Device ID", |
| 62 | required = true, multiValued = false) |
Ray Milkey | 0068fd0 | 2018-10-11 15:45:39 -0700 | [diff] [blame] | 63 | @Completion(DeviceIdCompleter.class) |
Yoonseon Han | 6c60389 | 2016-09-01 11:52:21 -0700 | [diff] [blame] | 64 | String physDeviceId = null; |
| 65 | |
| 66 | @Argument(index = 4, name = "physPortNum", description = "Physical device port number", |
| 67 | required = true, multiValued = false) |
Ray Milkey | 0068fd0 | 2018-10-11 15:45:39 -0700 | [diff] [blame] | 68 | @Completion(PortNumberCompleter.class) |
Yoonseon Han | 6c60389 | 2016-09-01 11:52:21 -0700 | [diff] [blame] | 69 | Integer physPortNum = null; |
| 70 | |
| 71 | @Override |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 72 | protected void doExecute() { |
Yoonseon Han | 6c60389 | 2016-09-01 11:52:21 -0700 | [diff] [blame] | 73 | VirtualNetworkAdminService service = get(VirtualNetworkAdminService.class); |
| 74 | DeviceService deviceService = get(DeviceService.class); |
| 75 | |
| 76 | VirtualPort vPort = getVirtualPort(PortNumber.portNumber(portNum)); |
| 77 | checkNotNull(vPort, "The virtual Port does not exist"); |
| 78 | |
| 79 | ConnectPoint realizedBy = new ConnectPoint(DeviceId.deviceId(physDeviceId), |
| 80 | PortNumber.portNumber(physPortNum)); |
| 81 | service.bindVirtualPort(NetworkId.networkId(networkId), DeviceId.deviceId(deviceId), |
| 82 | PortNumber.portNumber(portNum), realizedBy); |
| 83 | print("Virtual port is successfully bound."); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Returns the virtual port matching the device and port identifier. |
| 88 | * |
| 89 | * @param aPortNumber port identifier |
| 90 | * @return matching virtual port, or null. |
| 91 | */ |
| 92 | private VirtualPort getVirtualPort(PortNumber aPortNumber) { |
| 93 | VirtualNetworkService service = get(VirtualNetworkService.class); |
| 94 | Set<VirtualPort> ports = service.getVirtualPorts(NetworkId.networkId(networkId), |
| 95 | DeviceId.deviceId(deviceId)); |
Ray Milkey | 88cc343 | 2017-03-30 17:19:08 -0700 | [diff] [blame] | 96 | return ports.stream().filter(p -> p.number().equals(aPortNumber)) |
Yoonseon Han | 6c60389 | 2016-09-01 11:52:21 -0700 | [diff] [blame] | 97 | .findFirst().get(); |
| 98 | } |
| 99 | } |