blob: 505e8c30f7665ccfe7c8e6cdf5e0e1d2c540dc6f [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;
21import org.apache.karaf.shell.api.action.lifecycle.Service;
22import org.apache.karaf.shell.api.action.Option;
Claudine Chiu25f07be2016-06-27 16:21:21 +000023import org.onlab.packet.IpAddress;
24import org.onlab.packet.MacAddress;
25import org.onlab.packet.VlanId;
26import org.onosproject.cli.AbstractShellCommand;
27import org.onosproject.incubator.net.virtual.NetworkId;
28import org.onosproject.incubator.net.virtual.VirtualNetworkAdminService;
29import org.onosproject.net.DeviceId;
30import org.onosproject.net.HostId;
31import org.onosproject.net.HostLocation;
32import org.onosproject.net.PortNumber;
33
34import java.util.Arrays;
35import java.util.HashSet;
36import java.util.Set;
37
38/**
39 * Creates a new virtual host.
40 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070041@Service
Claudine Chiu25f07be2016-06-27 16:21:21 +000042@Command(scope = "onos", name = "vnet-create-host",
43 description = "Creates a new virtual host in a network.")
44public class VirtualHostCreateCommand 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 = "mac", description = "Mac address",
51 required = true, multiValued = false)
52 String mac = null;
53
54 @Argument(index = 2, name = "vlan", description = "Vlan",
55 required = true, multiValued = false)
56 short vlan;
57
58 @Argument(index = 3, name = "hostLocationDeviceId", description = "Host location device ID",
59 required = true, multiValued = false)
60 String hostLocationDeviceId;
61
62 @Argument(index = 4, name = "hostLocationPortNumber", description = "Host location port number",
63 required = true, multiValued = false)
64 long hostLocationPortNumber;
65
66 // ip addresses
67 @Option(name = "--hostIp", description = "Host IP addresses. Can be specified multiple times.",
68 required = false, multiValued = true)
69 protected String[] hostIpStrings;
70
71 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070072 protected void doExecute() {
Claudine Chiu25f07be2016-06-27 16:21:21 +000073 VirtualNetworkAdminService service = get(VirtualNetworkAdminService.class);
74
75 Set<IpAddress> hostIps = new HashSet<>();
76 if (hostIpStrings != null) {
Sho SHIMIZUa09e1bb2016-08-01 14:25:25 -070077 Arrays.stream(hostIpStrings).forEach(s -> hostIps.add(IpAddress.valueOf(s)));
Claudine Chiu25f07be2016-06-27 16:21:21 +000078 }
79 HostLocation hostLocation = new HostLocation(DeviceId.deviceId(hostLocationDeviceId),
80 PortNumber.portNumber(hostLocationPortNumber),
81 System.currentTimeMillis());
82 MacAddress macAddress = MacAddress.valueOf(mac);
83 VlanId vlanId = VlanId.vlanId(vlan);
84 service.createVirtualHost(NetworkId.networkId(networkId),
85 HostId.hostId(macAddress, vlanId), macAddress, vlanId,
86 hostLocation, hostIps);
87 print("Virtual host successfully created.");
88 }
89}