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