Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [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 | |
| 19 | import org.apache.karaf.shell.commands.Argument; |
| 20 | import org.apache.karaf.shell.commands.Command; |
| 21 | import org.onosproject.cli.AbstractShellCommand; |
| 22 | import org.onosproject.incubator.net.virtual.NetworkId; |
| 23 | import org.onosproject.incubator.net.virtual.VirtualDevice; |
| 24 | import org.onosproject.incubator.net.virtual.VirtualNetworkAdminService; |
| 25 | import org.onosproject.incubator.net.virtual.VirtualNetworkService; |
Yoonseon Han | 6c60389 | 2016-09-01 11:52:21 -0700 | [diff] [blame] | 26 | import org.onosproject.net.ConnectPoint; |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 27 | import org.onosproject.net.DeviceId; |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 28 | import org.onosproject.net.PortNumber; |
| 29 | import org.onosproject.net.device.DeviceService; |
| 30 | |
| 31 | import java.util.Set; |
| 32 | |
| 33 | import static com.google.common.base.Preconditions.checkNotNull; |
| 34 | |
| 35 | /** |
| 36 | * Creates a new virtual port. |
| 37 | */ |
| 38 | @Command(scope = "onos", name = "vnet-create-port", |
| 39 | description = "Creates a new virtual port in a network.") |
| 40 | public class VirtualPortCreateCommand extends AbstractShellCommand { |
| 41 | |
| 42 | @Argument(index = 0, name = "networkId", description = "Network ID", |
| 43 | required = true, multiValued = false) |
| 44 | Long networkId = null; |
| 45 | |
| 46 | @Argument(index = 1, name = "deviceId", description = "Virtual Device ID", |
| 47 | required = true, multiValued = false) |
| 48 | String deviceId = null; |
| 49 | |
| 50 | @Argument(index = 2, name = "portNum", description = "Virtual device port number", |
| 51 | required = true, multiValued = false) |
| 52 | Integer portNum = null; |
| 53 | |
| 54 | @Argument(index = 3, name = "physDeviceId", description = "Physical Device ID", |
Yoonseon Han | 6c60389 | 2016-09-01 11:52:21 -0700 | [diff] [blame] | 55 | required = false, multiValued = false) |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 56 | String physDeviceId = null; |
| 57 | |
| 58 | @Argument(index = 4, name = "physPortNum", description = "Physical device port number", |
Yoonseon Han | 6c60389 | 2016-09-01 11:52:21 -0700 | [diff] [blame] | 59 | required = false, multiValued = false) |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 60 | Integer physPortNum = null; |
| 61 | |
| 62 | @Override |
| 63 | protected void execute() { |
| 64 | VirtualNetworkAdminService service = get(VirtualNetworkAdminService.class); |
| 65 | DeviceService deviceService = get(DeviceService.class); |
Yoonseon Han | 6c60389 | 2016-09-01 11:52:21 -0700 | [diff] [blame] | 66 | |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 67 | VirtualDevice virtualDevice = getVirtualDevice(DeviceId.deviceId(deviceId)); |
| 68 | checkNotNull(virtualDevice, "The virtual device does not exist."); |
| 69 | |
Yoonseon Han | 6c60389 | 2016-09-01 11:52:21 -0700 | [diff] [blame] | 70 | ConnectPoint realizedBy = null; |
| 71 | if (physDeviceId != null && physPortNum != null) { |
| 72 | checkNotNull(physPortNum, "The physical port does not specified."); |
| 73 | realizedBy = new ConnectPoint(DeviceId.deviceId(physDeviceId), |
| 74 | PortNumber.portNumber(physPortNum)); |
| 75 | checkNotNull(realizedBy, "The physical port does not exist."); |
| 76 | } |
| 77 | |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 78 | service.createVirtualPort(NetworkId.networkId(networkId), DeviceId.deviceId(deviceId), |
| 79 | PortNumber.portNumber(portNum), realizedBy); |
| 80 | print("Virtual port successfully created."); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Returns the virtual device matching the device identifier. |
| 85 | * |
| 86 | * @param aDeviceId device identifier |
| 87 | * @return matching virtual device, or null. |
| 88 | */ |
| 89 | private VirtualDevice getVirtualDevice(DeviceId aDeviceId) { |
| 90 | VirtualNetworkService service = get(VirtualNetworkService.class); |
| 91 | |
| 92 | Set<VirtualDevice> virtualDevices = service.getVirtualDevices(NetworkId.networkId(networkId)); |
| 93 | |
| 94 | for (VirtualDevice virtualDevice : virtualDevices) { |
| 95 | if (virtualDevice.id().equals(aDeviceId)) { |
| 96 | return virtualDevice; |
| 97 | } |
| 98 | } |
| 99 | return null; |
| 100 | } |
| 101 | } |