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