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