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