blob: e9b7763b970539129d930ae1b7cceb88abf799cf [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;
21import org.apache.karaf.shell.api.action.lifecycle.Service;
Brian Stanke5df14472016-03-11 19:34:38 -050022import org.onosproject.cli.AbstractShellCommand;
23import org.onosproject.incubator.net.virtual.NetworkId;
24import org.onosproject.incubator.net.virtual.VirtualDevice;
25import org.onosproject.incubator.net.virtual.VirtualNetworkAdminService;
26import org.onosproject.incubator.net.virtual.VirtualNetworkService;
Yoonseon Han6c603892016-09-01 11:52:21 -070027import org.onosproject.net.ConnectPoint;
Brian Stanke5df14472016-03-11 19:34:38 -050028import org.onosproject.net.DeviceId;
Brian Stanke5df14472016-03-11 19:34:38 -050029import org.onosproject.net.PortNumber;
30import org.onosproject.net.device.DeviceService;
31
32import java.util.Set;
33
34import static com.google.common.base.Preconditions.checkNotNull;
35
36/**
37 * Creates a new virtual port.
38 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070039@Service
Brian Stanke5df14472016-03-11 19:34:38 -050040@Command(scope = "onos", name = "vnet-create-port",
41 description = "Creates a new virtual port in a network.")
42public class VirtualPortCreateCommand extends AbstractShellCommand {
43
44 @Argument(index = 0, name = "networkId", description = "Network ID",
45 required = true, multiValued = false)
46 Long networkId = null;
47
48 @Argument(index = 1, name = "deviceId", description = "Virtual Device ID",
49 required = true, multiValued = false)
50 String deviceId = null;
51
52 @Argument(index = 2, name = "portNum", description = "Virtual device port number",
53 required = true, multiValued = false)
54 Integer portNum = null;
55
56 @Argument(index = 3, name = "physDeviceId", description = "Physical Device ID",
Yoonseon Han6c603892016-09-01 11:52:21 -070057 required = false, multiValued = false)
Brian Stanke5df14472016-03-11 19:34:38 -050058 String physDeviceId = null;
59
60 @Argument(index = 4, name = "physPortNum", description = "Physical device port number",
Yoonseon Han6c603892016-09-01 11:52:21 -070061 required = false, multiValued = false)
Brian Stanke5df14472016-03-11 19:34:38 -050062 Integer physPortNum = null;
63
64 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070065 protected void doExecute() {
Brian Stanke5df14472016-03-11 19:34:38 -050066 VirtualNetworkAdminService service = get(VirtualNetworkAdminService.class);
67 DeviceService deviceService = get(DeviceService.class);
Yoonseon Han6c603892016-09-01 11:52:21 -070068
Brian Stanke5df14472016-03-11 19:34:38 -050069 VirtualDevice virtualDevice = getVirtualDevice(DeviceId.deviceId(deviceId));
70 checkNotNull(virtualDevice, "The virtual device does not exist.");
71
Yoonseon Han6c603892016-09-01 11:52:21 -070072 ConnectPoint realizedBy = null;
73 if (physDeviceId != null && physPortNum != null) {
74 checkNotNull(physPortNum, "The physical port does not specified.");
75 realizedBy = new ConnectPoint(DeviceId.deviceId(physDeviceId),
76 PortNumber.portNumber(physPortNum));
77 checkNotNull(realizedBy, "The physical port does not exist.");
78 }
79
Brian Stanke5df14472016-03-11 19:34:38 -050080 service.createVirtualPort(NetworkId.networkId(networkId), DeviceId.deviceId(deviceId),
81 PortNumber.portNumber(portNum), realizedBy);
82 print("Virtual port successfully created.");
83 }
84
85 /**
86 * Returns the virtual device matching the device identifier.
87 *
88 * @param aDeviceId device identifier
89 * @return matching virtual device, or null.
90 */
91 private VirtualDevice getVirtualDevice(DeviceId aDeviceId) {
92 VirtualNetworkService service = get(VirtualNetworkService.class);
93
94 Set<VirtualDevice> virtualDevices = service.getVirtualDevices(NetworkId.networkId(networkId));
95
96 for (VirtualDevice virtualDevice : virtualDevices) {
97 if (virtualDevice.id().equals(aDeviceId)) {
98 return virtualDevice;
99 }
100 }
101 return null;
102 }
103}