Claudine Chiu | 25f07be | 2016-06-27 16:21:21 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.cli.net.vnet; |
| 18 | |
| 19 | import org.apache.karaf.shell.commands.Argument; |
| 20 | import org.apache.karaf.shell.commands.Command; |
| 21 | import org.apache.karaf.shell.commands.Option; |
| 22 | import org.onlab.packet.IpAddress; |
| 23 | import org.onlab.packet.MacAddress; |
| 24 | import org.onlab.packet.VlanId; |
| 25 | import org.onosproject.cli.AbstractShellCommand; |
| 26 | import org.onosproject.incubator.net.virtual.NetworkId; |
| 27 | import org.onosproject.incubator.net.virtual.VirtualNetworkAdminService; |
| 28 | import org.onosproject.net.DeviceId; |
| 29 | import org.onosproject.net.HostId; |
| 30 | import org.onosproject.net.HostLocation; |
| 31 | import org.onosproject.net.PortNumber; |
| 32 | |
| 33 | import java.util.Arrays; |
| 34 | import java.util.HashSet; |
| 35 | import 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.") |
| 42 | public 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 SHIMIZU | a09e1bb | 2016-08-01 14:25:25 -0700 | [diff] [blame] | 75 | Arrays.stream(hostIpStrings).forEach(s -> hostIps.add(IpAddress.valueOf(s))); |
Claudine Chiu | 25f07be | 2016-06-27 16:21:21 +0000 | [diff] [blame] | 76 | } |
| 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 | } |