blob: 5769a64ddaff54f54d8b894b7bcd372214c93190 [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
19import org.apache.karaf.shell.commands.Argument;
20import org.apache.karaf.shell.commands.Command;
21import org.onosproject.cli.AbstractShellCommand;
22import org.onosproject.incubator.net.virtual.NetworkId;
23import org.onosproject.incubator.net.virtual.VirtualDevice;
24import org.onosproject.incubator.net.virtual.VirtualNetworkAdminService;
25import org.onosproject.incubator.net.virtual.VirtualNetworkService;
Yoonseon Han6c603892016-09-01 11:52:21 -070026import org.onosproject.net.ConnectPoint;
Brian Stanke5df14472016-03-11 19:34:38 -050027import org.onosproject.net.DeviceId;
Brian Stanke5df14472016-03-11 19:34:38 -050028import org.onosproject.net.PortNumber;
29import org.onosproject.net.device.DeviceService;
30
31import java.util.Set;
32
33import static com.google.common.base.Preconditions.checkNotNull;
34
35/**
36 * Creates a new virtual port.
37 */
38@Command(scope = "onos", name = "vnet-create-port",
39 description = "Creates a new virtual port in a network.")
40public class VirtualPortCreateCommand extends AbstractShellCommand {
41
42 @Argument(index = 0, name = "networkId", description = "Network ID",
43 required = true, multiValued = false)
44 Long networkId = null;
45
46 @Argument(index = 1, name = "deviceId", description = "Virtual Device ID",
47 required = true, multiValued = false)
48 String deviceId = null;
49
50 @Argument(index = 2, name = "portNum", description = "Virtual device port number",
51 required = true, multiValued = false)
52 Integer portNum = null;
53
54 @Argument(index = 3, name = "physDeviceId", description = "Physical Device ID",
Yoonseon Han6c603892016-09-01 11:52:21 -070055 required = false, multiValued = false)
Brian Stanke5df14472016-03-11 19:34:38 -050056 String physDeviceId = null;
57
58 @Argument(index = 4, name = "physPortNum", description = "Physical device port number",
Yoonseon Han6c603892016-09-01 11:52:21 -070059 required = false, multiValued = false)
Brian Stanke5df14472016-03-11 19:34:38 -050060 Integer physPortNum = null;
61
62 @Override
63 protected void execute() {
64 VirtualNetworkAdminService service = get(VirtualNetworkAdminService.class);
65 DeviceService deviceService = get(DeviceService.class);
Yoonseon Han6c603892016-09-01 11:52:21 -070066
Brian Stanke5df14472016-03-11 19:34:38 -050067 VirtualDevice virtualDevice = getVirtualDevice(DeviceId.deviceId(deviceId));
68 checkNotNull(virtualDevice, "The virtual device does not exist.");
69
Yoonseon Han6c603892016-09-01 11:52:21 -070070 ConnectPoint realizedBy = null;
71 if (physDeviceId != null && physPortNum != null) {
72 checkNotNull(physPortNum, "The physical port does not specified.");
73 realizedBy = new ConnectPoint(DeviceId.deviceId(physDeviceId),
74 PortNumber.portNumber(physPortNum));
75 checkNotNull(realizedBy, "The physical port does not exist.");
76 }
77
Brian Stanke5df14472016-03-11 19:34:38 -050078 service.createVirtualPort(NetworkId.networkId(networkId), DeviceId.deviceId(deviceId),
79 PortNumber.portNumber(portNum), realizedBy);
80 print("Virtual port successfully created.");
81 }
82
83 /**
84 * Returns the virtual device matching the device identifier.
85 *
86 * @param aDeviceId device identifier
87 * @return matching virtual device, or null.
88 */
89 private VirtualDevice getVirtualDevice(DeviceId aDeviceId) {
90 VirtualNetworkService service = get(VirtualNetworkService.class);
91
92 Set<VirtualDevice> virtualDevices = service.getVirtualDevices(NetworkId.networkId(networkId));
93
94 for (VirtualDevice virtualDevice : virtualDevices) {
95 if (virtualDevice.id().equals(aDeviceId)) {
96 return virtualDevice;
97 }
98 }
99 return null;
100 }
101}