blob: 190de0fd734f6b28329b46a9d937d2afe955ab2d [file] [log] [blame]
Brian Stanke5df14472016-03-11 19:34:38 -05001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Brian Stanke5df14472016-03-11 19:34:38 -05003 *
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;
Brian Stanke5df14472016-03-11 19:34:38 -050023import org.onosproject.cli.AbstractShellCommand;
Ray Milkey0068fd02018-10-11 15:45:39 -070024import org.onosproject.cli.net.DeviceIdCompleter;
25import org.onosproject.cli.net.PortNumberCompleter;
Brian Stanke5df14472016-03-11 19:34:38 -050026import org.onosproject.incubator.net.virtual.NetworkId;
27import org.onosproject.incubator.net.virtual.VirtualDevice;
28import org.onosproject.incubator.net.virtual.VirtualNetworkAdminService;
29import org.onosproject.incubator.net.virtual.VirtualNetworkService;
Yoonseon Han6c603892016-09-01 11:52:21 -070030import org.onosproject.net.ConnectPoint;
Brian Stanke5df14472016-03-11 19:34:38 -050031import org.onosproject.net.DeviceId;
Brian Stanke5df14472016-03-11 19:34:38 -050032import org.onosproject.net.PortNumber;
33import org.onosproject.net.device.DeviceService;
34
35import java.util.Set;
36
37import static com.google.common.base.Preconditions.checkNotNull;
38
39/**
40 * Creates a new virtual port.
41 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070042@Service
Brian Stanke5df14472016-03-11 19:34:38 -050043@Command(scope = "onos", name = "vnet-create-port",
44 description = "Creates a new virtual port in a network.")
45public class VirtualPortCreateCommand extends AbstractShellCommand {
46
47 @Argument(index = 0, name = "networkId", description = "Network ID",
48 required = true, multiValued = false)
Ray Milkey0068fd02018-10-11 15:45:39 -070049 @Completion(VirtualNetworkCompleter.class)
Brian Stanke5df14472016-03-11 19:34:38 -050050 Long networkId = null;
51
52 @Argument(index = 1, name = "deviceId", description = "Virtual Device ID",
53 required = true, multiValued = false)
Ray Milkey0068fd02018-10-11 15:45:39 -070054 @Completion(VirtualDeviceCompleter.class)
Brian Stanke5df14472016-03-11 19:34:38 -050055 String deviceId = null;
56
57 @Argument(index = 2, name = "portNum", description = "Virtual device port number",
58 required = true, multiValued = false)
59 Integer portNum = null;
60
61 @Argument(index = 3, name = "physDeviceId", description = "Physical Device ID",
Yoonseon Han6c603892016-09-01 11:52:21 -070062 required = false, multiValued = false)
Ray Milkey0068fd02018-10-11 15:45:39 -070063 @Completion(DeviceIdCompleter.class)
Brian Stanke5df14472016-03-11 19:34:38 -050064 String physDeviceId = null;
65
66 @Argument(index = 4, name = "physPortNum", description = "Physical device port number",
Yoonseon Han6c603892016-09-01 11:52:21 -070067 required = false, multiValued = false)
Ray Milkey0068fd02018-10-11 15:45:39 -070068 @Completion(PortNumberCompleter.class)
Brian Stanke5df14472016-03-11 19:34:38 -050069 Integer physPortNum = null;
70
71 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070072 protected void doExecute() {
Brian Stanke5df14472016-03-11 19:34:38 -050073 VirtualNetworkAdminService service = get(VirtualNetworkAdminService.class);
74 DeviceService deviceService = get(DeviceService.class);
Yoonseon Han6c603892016-09-01 11:52:21 -070075
Brian Stanke5df14472016-03-11 19:34:38 -050076 VirtualDevice virtualDevice = getVirtualDevice(DeviceId.deviceId(deviceId));
77 checkNotNull(virtualDevice, "The virtual device does not exist.");
78
Yoonseon Han6c603892016-09-01 11:52:21 -070079 ConnectPoint realizedBy = null;
80 if (physDeviceId != null && physPortNum != null) {
81 checkNotNull(physPortNum, "The physical port does not specified.");
82 realizedBy = new ConnectPoint(DeviceId.deviceId(physDeviceId),
83 PortNumber.portNumber(physPortNum));
84 checkNotNull(realizedBy, "The physical port does not exist.");
85 }
86
Brian Stanke5df14472016-03-11 19:34:38 -050087 service.createVirtualPort(NetworkId.networkId(networkId), DeviceId.deviceId(deviceId),
88 PortNumber.portNumber(portNum), realizedBy);
89 print("Virtual port successfully created.");
90 }
91
92 /**
93 * Returns the virtual device matching the device identifier.
94 *
95 * @param aDeviceId device identifier
96 * @return matching virtual device, or null.
97 */
98 private VirtualDevice getVirtualDevice(DeviceId aDeviceId) {
99 VirtualNetworkService service = get(VirtualNetworkService.class);
100
101 Set<VirtualDevice> virtualDevices = service.getVirtualDevices(NetworkId.networkId(networkId));
102
103 for (VirtualDevice virtualDevice : virtualDevices) {
104 if (virtualDevice.id().equals(aDeviceId)) {
105 return virtualDevice;
106 }
107 }
108 return null;
109 }
110}