blob: 2eb0a56f1a444180a2c86ab509276aaabbb6120e [file] [log] [blame]
Yoonseon Han6c603892016-09-01 11:52:21 -07001/*
2 * Copyright 2016-present Open Networking Laboratory
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
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.VirtualNetworkAdminService;
24import org.onosproject.incubator.net.virtual.VirtualNetworkService;
25import org.onosproject.incubator.net.virtual.VirtualPort;
26import org.onosproject.net.ConnectPoint;
27import org.onosproject.net.DeviceId;
28import 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 * Binds an existing virtual port with a physical port.
37 */
38@Command(scope = "onos", name = "vnet-bind-port",
39 description = "Binds an existing virtual port with a physical port.")
40public class VirtualPortBindCommand extends AbstractShellCommand {
41 @Argument(index = 0, name = "networkId", description = "Network ID",
42 required = true, multiValued = false)
43 Long networkId = null;
44
45 @Argument(index = 1, name = "deviceId", description = "Virtual Device ID",
46 required = true, multiValued = false)
47 String deviceId = null;
48
49 @Argument(index = 2, name = "portNum", description = "Virtual device port number",
50 required = true, multiValued = false)
51 Integer portNum = null;
52
53 @Argument(index = 3, name = "physDeviceId", description = "Physical Device ID",
54 required = true, multiValued = false)
55 String physDeviceId = null;
56
57 @Argument(index = 4, name = "physPortNum", description = "Physical device port number",
58 required = true, multiValued = false)
59 Integer physPortNum = null;
60
61 @Override
62 protected void execute() {
63 VirtualNetworkAdminService service = get(VirtualNetworkAdminService.class);
64 DeviceService deviceService = get(DeviceService.class);
65
66 VirtualPort vPort = getVirtualPort(PortNumber.portNumber(portNum));
67 checkNotNull(vPort, "The virtual Port does not exist");
68
69 ConnectPoint realizedBy = new ConnectPoint(DeviceId.deviceId(physDeviceId),
70 PortNumber.portNumber(physPortNum));
71 service.bindVirtualPort(NetworkId.networkId(networkId), DeviceId.deviceId(deviceId),
72 PortNumber.portNumber(portNum), realizedBy);
73 print("Virtual port is successfully bound.");
74 }
75
76 /**
77 * Returns the virtual port matching the device and port identifier.
78 *
79 * @param aPortNumber port identifier
80 * @return matching virtual port, or null.
81 */
82 private VirtualPort getVirtualPort(PortNumber aPortNumber) {
83 VirtualNetworkService service = get(VirtualNetworkService.class);
84 Set<VirtualPort> ports = service.getVirtualPorts(NetworkId.networkId(networkId),
85 DeviceId.deviceId(deviceId));
86 return ports.stream().filter(p->p.number().equals(aPortNumber))
87 .findFirst().get();
88 }
89}