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