blob: 0c2757cd943ccfb2d8bebaba9562a5fc84b2142d [file] [log] [blame]
Brian Stanke5df14472016-03-11 19:34:38 -05001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
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;
26import org.onosproject.net.DefaultAnnotations;
27import org.onosproject.net.DefaultDevice;
28import org.onosproject.net.DefaultPort;
29import org.onosproject.net.Device;
30import org.onosproject.net.DeviceId;
31import org.onosproject.net.Port;
32import 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 */
42@Command(scope = "onos", name = "vnet-create-port",
43 description = "Creates a new virtual port in a network.")
44public class VirtualPortCreateCommand extends AbstractShellCommand {
45
46 @Argument(index = 0, name = "networkId", description = "Network ID",
47 required = true, multiValued = false)
48 Long networkId = null;
49
50 @Argument(index = 1, name = "deviceId", description = "Virtual Device ID",
51 required = true, multiValued = false)
52 String deviceId = null;
53
54 @Argument(index = 2, name = "portNum", description = "Virtual device port number",
55 required = true, multiValued = false)
56 Integer portNum = null;
57
58 @Argument(index = 3, name = "physDeviceId", description = "Physical Device ID",
59 required = true, multiValued = false)
60 String physDeviceId = null;
61
62 @Argument(index = 4, name = "physPortNum", description = "Physical device port number",
63 required = true, multiValued = false)
64 Integer physPortNum = null;
65
66 @Override
67 protected void execute() {
68 VirtualNetworkAdminService service = get(VirtualNetworkAdminService.class);
69 DeviceService deviceService = get(DeviceService.class);
70 VirtualDevice virtualDevice = getVirtualDevice(DeviceId.deviceId(deviceId));
71 checkNotNull(virtualDevice, "The virtual device does not exist.");
72
73 DefaultAnnotations annotations = DefaultAnnotations.builder().build();
74 Device physDevice = new DefaultDevice(null, DeviceId.deviceId(physDeviceId),
75 null, null, null, null, null, null, annotations);
76 Port realizedBy = new DefaultPort(physDevice, PortNumber.portNumber(physPortNum), true);
77 service.createVirtualPort(NetworkId.networkId(networkId), DeviceId.deviceId(deviceId),
78 PortNumber.portNumber(portNum), realizedBy);
79 print("Virtual port successfully created.");
80 }
81
82 /**
83 * Returns the virtual device matching the device identifier.
84 *
85 * @param aDeviceId device identifier
86 * @return matching virtual device, or null.
87 */
88 private VirtualDevice getVirtualDevice(DeviceId aDeviceId) {
89 VirtualNetworkService service = get(VirtualNetworkService.class);
90
91 Set<VirtualDevice> virtualDevices = service.getVirtualDevices(NetworkId.networkId(networkId));
92
93 for (VirtualDevice virtualDevice : virtualDevices) {
94 if (virtualDevice.id().equals(aDeviceId)) {
95 return virtualDevice;
96 }
97 }
98 return null;
99 }
100}