blob: 488972c1ecbe5ccbf60de7b3688c7fc073401115 [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;
Ray Milkey0068fd02018-10-11 15:45:39 -070021import org.apache.karaf.shell.api.action.Completion;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070022import org.apache.karaf.shell.api.action.lifecycle.Service;
Yoonseon Han6c603892016-09-01 11:52:21 -070023import org.onosproject.cli.AbstractShellCommand;
Ray Milkey0068fd02018-10-11 15:45:39 -070024import org.onosproject.cli.net.DeviceIdCompleter;
25import org.onosproject.cli.net.PortNumberCompleter;
Yoonseon Han6c603892016-09-01 11:52:21 -070026import org.onosproject.incubator.net.virtual.NetworkId;
27import org.onosproject.incubator.net.virtual.VirtualNetworkAdminService;
28import org.onosproject.incubator.net.virtual.VirtualNetworkService;
29import org.onosproject.incubator.net.virtual.VirtualPort;
30import org.onosproject.net.ConnectPoint;
31import org.onosproject.net.DeviceId;
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 * Binds an existing virtual port with a physical port.
41 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070042@Service
Yoonseon Han6c603892016-09-01 11:52:21 -070043@Command(scope = "onos", name = "vnet-bind-port",
44 description = "Binds an existing virtual port with a physical port.")
45public class VirtualPortBindCommand extends AbstractShellCommand {
46 @Argument(index = 0, name = "networkId", description = "Network ID",
47 required = true, multiValued = false)
Ray Milkey0068fd02018-10-11 15:45:39 -070048 @Completion(VirtualNetworkCompleter.class)
Yoonseon Han6c603892016-09-01 11:52:21 -070049 Long networkId = null;
50
51 @Argument(index = 1, name = "deviceId", description = "Virtual Device ID",
52 required = true, multiValued = false)
Ray Milkey0068fd02018-10-11 15:45:39 -070053 @Completion(VirtualDeviceCompleter.class)
Yoonseon Han6c603892016-09-01 11:52:21 -070054 String deviceId = null;
55
56 @Argument(index = 2, name = "portNum", description = "Virtual device port number",
57 required = true, multiValued = false)
Ray Milkey0068fd02018-10-11 15:45:39 -070058 @Completion(VirtualPortCompleter.class)
Yoonseon Han6c603892016-09-01 11:52:21 -070059 Integer portNum = null;
60
61 @Argument(index = 3, name = "physDeviceId", description = "Physical Device ID",
62 required = true, multiValued = false)
Ray Milkey0068fd02018-10-11 15:45:39 -070063 @Completion(DeviceIdCompleter.class)
Yoonseon Han6c603892016-09-01 11:52:21 -070064 String physDeviceId = null;
65
66 @Argument(index = 4, name = "physPortNum", description = "Physical device port number",
67 required = true, multiValued = false)
Ray Milkey0068fd02018-10-11 15:45:39 -070068 @Completion(PortNumberCompleter.class)
Yoonseon Han6c603892016-09-01 11:52:21 -070069 Integer physPortNum = null;
70
71 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070072 protected void doExecute() {
Yoonseon Han6c603892016-09-01 11:52:21 -070073 VirtualNetworkAdminService service = get(VirtualNetworkAdminService.class);
74 DeviceService deviceService = get(DeviceService.class);
75
76 VirtualPort vPort = getVirtualPort(PortNumber.portNumber(portNum));
77 checkNotNull(vPort, "The virtual Port does not exist");
78
79 ConnectPoint realizedBy = new ConnectPoint(DeviceId.deviceId(physDeviceId),
80 PortNumber.portNumber(physPortNum));
81 service.bindVirtualPort(NetworkId.networkId(networkId), DeviceId.deviceId(deviceId),
82 PortNumber.portNumber(portNum), realizedBy);
83 print("Virtual port is successfully bound.");
84 }
85
86 /**
87 * Returns the virtual port matching the device and port identifier.
88 *
89 * @param aPortNumber port identifier
90 * @return matching virtual port, or null.
91 */
92 private VirtualPort getVirtualPort(PortNumber aPortNumber) {
93 VirtualNetworkService service = get(VirtualNetworkService.class);
94 Set<VirtualPort> ports = service.getVirtualPorts(NetworkId.networkId(networkId),
95 DeviceId.deviceId(deviceId));
Ray Milkey88cc3432017-03-30 17:19:08 -070096 return ports.stream().filter(p -> p.number().equals(aPortNumber))
Yoonseon Han6c603892016-09-01 11:52:21 -070097 .findFirst().get();
98 }
99}