Claudine Chiu | 579969d | 2017-10-06 14:32:18 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 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.VirtualNetworkAdminService; |
| 24 | import org.onosproject.incubator.net.virtual.VirtualNetworkService; |
| 25 | import org.onosproject.incubator.net.virtual.VirtualPort; |
| 26 | import org.onosproject.net.DeviceId; |
| 27 | import org.onosproject.net.PortNumber; |
| 28 | |
| 29 | import java.util.Set; |
| 30 | |
| 31 | import static com.google.common.base.Preconditions.checkNotNull; |
| 32 | |
| 33 | /** |
| 34 | * Administratively enables or disables state of an existing virtual port. |
| 35 | */ |
| 36 | @Command(scope = "onos", name = "vnet-port-state", |
| 37 | description = "Administratively enables or disables state of an existing virtual port.") |
| 38 | public class VirtualPortStateCommand extends AbstractShellCommand { |
| 39 | @Argument(index = 0, name = "networkId", description = "Network ID", |
| 40 | required = true, multiValued = false) |
| 41 | Long networkId = null; |
| 42 | |
| 43 | @Argument(index = 1, name = "deviceId", description = "Virtual Device ID", |
| 44 | required = true, multiValued = false) |
| 45 | String deviceId = null; |
| 46 | |
| 47 | @Argument(index = 2, name = "portNum", description = "Virtual device port number", |
| 48 | required = true, multiValued = false) |
| 49 | Integer portNum = null; |
| 50 | |
| 51 | @Argument(index = 3, name = "portState", |
| 52 | description = "Desired State. Either \"enable\" or \"disable\".", |
| 53 | required = true, multiValued = false) |
| 54 | String portState = null; |
| 55 | |
| 56 | @Override |
| 57 | protected void execute() { |
| 58 | VirtualNetworkAdminService service = get(VirtualNetworkAdminService.class); |
| 59 | |
| 60 | VirtualPort vPort = getVirtualPort(PortNumber.portNumber(portNum)); |
| 61 | checkNotNull(vPort, "The virtual Port does not exist"); |
| 62 | |
| 63 | boolean isEnabled; |
| 64 | if ("enable".equals(portState)) { |
| 65 | isEnabled = true; |
| 66 | } else if ("disable".equals(portState)) { |
| 67 | isEnabled = false; |
| 68 | } else { |
| 69 | print("State must be enable or disable"); |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | service.updatePortState(NetworkId.networkId(networkId), |
| 74 | DeviceId.deviceId(deviceId), vPort.number(), isEnabled); |
| 75 | print("Virtual port state updated."); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Returns the virtual port matching the device and port identifier. |
| 80 | * |
| 81 | * @param aPortNumber port identifier |
| 82 | * @return matching virtual port, or null. |
| 83 | */ |
| 84 | private VirtualPort getVirtualPort(PortNumber aPortNumber) { |
| 85 | VirtualNetworkService service = get(VirtualNetworkService.class); |
| 86 | Set<VirtualPort> ports = service.getVirtualPorts(NetworkId.networkId(networkId), |
| 87 | DeviceId.deviceId(deviceId)); |
| 88 | return ports.stream().filter(p -> p.number().equals(aPortNumber)) |
| 89 | .findFirst().get(); |
| 90 | } |
| 91 | } |