blob: b580c688f3fdedaf9b2b2c3d1b9c628e6e0afe5c [file] [log] [blame]
Claudine Chiu25f07be2016-06-27 16:21:21 +00001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Claudine Chiu25f07be2016-06-27 16:21:21 +00003 *
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;
23import org.apache.karaf.shell.api.action.Option;
Claudine Chiu25f07be2016-06-27 16:21:21 +000024import org.onlab.packet.IpAddress;
25import org.onlab.packet.MacAddress;
26import org.onlab.packet.VlanId;
27import org.onosproject.cli.AbstractShellCommand;
28import org.onosproject.incubator.net.virtual.NetworkId;
29import org.onosproject.incubator.net.virtual.VirtualNetworkAdminService;
30import org.onosproject.net.DeviceId;
31import org.onosproject.net.HostId;
32import org.onosproject.net.HostLocation;
33import org.onosproject.net.PortNumber;
34
35import java.util.Arrays;
36import java.util.HashSet;
37import java.util.Set;
38
39/**
40 * Creates a new virtual host.
41 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070042@Service
Claudine Chiu25f07be2016-06-27 16:21:21 +000043@Command(scope = "onos", name = "vnet-create-host",
44 description = "Creates a new virtual host in a network.")
45public class VirtualHostCreateCommand extends AbstractShellCommand {
46
47 @Argument(index = 0, name = "networkId", description = "Network ID",
48 required = true, multiValued = false)
Ray Milkey0068fd02018-10-11 15:45:39 -070049 @Completion(VirtualNetworkCompleter.class)
Claudine Chiu25f07be2016-06-27 16:21:21 +000050 Long networkId = null;
51
52 @Argument(index = 1, name = "mac", description = "Mac address",
53 required = true, multiValued = false)
54 String mac = null;
55
56 @Argument(index = 2, name = "vlan", description = "Vlan",
57 required = true, multiValued = false)
58 short vlan;
59
60 @Argument(index = 3, name = "hostLocationDeviceId", description = "Host location device ID",
61 required = true, multiValued = false)
62 String hostLocationDeviceId;
63
64 @Argument(index = 4, name = "hostLocationPortNumber", description = "Host location port number",
65 required = true, multiValued = false)
66 long hostLocationPortNumber;
67
68 // ip addresses
69 @Option(name = "--hostIp", description = "Host IP addresses. Can be specified multiple times.",
70 required = false, multiValued = true)
71 protected String[] hostIpStrings;
72
73 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070074 protected void doExecute() {
Claudine Chiu25f07be2016-06-27 16:21:21 +000075 VirtualNetworkAdminService service = get(VirtualNetworkAdminService.class);
76
77 Set<IpAddress> hostIps = new HashSet<>();
78 if (hostIpStrings != null) {
Sho SHIMIZUa09e1bb2016-08-01 14:25:25 -070079 Arrays.stream(hostIpStrings).forEach(s -> hostIps.add(IpAddress.valueOf(s)));
Claudine Chiu25f07be2016-06-27 16:21:21 +000080 }
81 HostLocation hostLocation = new HostLocation(DeviceId.deviceId(hostLocationDeviceId),
82 PortNumber.portNumber(hostLocationPortNumber),
83 System.currentTimeMillis());
84 MacAddress macAddress = MacAddress.valueOf(mac);
85 VlanId vlanId = VlanId.vlanId(vlan);
86 service.createVirtualHost(NetworkId.networkId(networkId),
87 HostId.hostId(macAddress, vlanId), macAddress, vlanId,
88 hostLocation, hostIps);
89 print("Virtual host successfully created.");
90 }
91}