Thomas Vachuska | d821c75 | 2018-02-26 11:33:35 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 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.provider.nil.cli; |
| 18 | |
| 19 | import org.apache.karaf.shell.commands.Argument; |
| 20 | import org.apache.karaf.shell.commands.Command; |
| 21 | import org.onlab.packet.IpAddress; |
| 22 | import org.onosproject.net.ConnectPoint; |
| 23 | import org.onosproject.net.HostId; |
| 24 | import org.onosproject.net.HostLocation; |
| 25 | import org.onosproject.provider.nil.CustomTopologySimulator; |
| 26 | import org.onosproject.provider.nil.NullProviders; |
| 27 | import org.onosproject.provider.nil.TopologySimulator; |
| 28 | |
| 29 | import java.util.List; |
| 30 | |
| 31 | /** |
| 32 | * Adds a simulated end-station host to the custom topology simulation. |
| 33 | */ |
| 34 | @Command(scope = "onos", name = "null-create-hosts", |
| 35 | description = "Adds a simulated end-station host to the custom topology simulation") |
| 36 | public class CreateNullHosts extends CreateNullEntity { |
| 37 | |
| 38 | @Argument(index = 0, name = "deviceName", description = "Name of device where hosts are attached", |
| 39 | required = true) |
| 40 | String deviceName = null; |
| 41 | |
| 42 | @Argument(index = 1, name = "hostIpPattern", description = "Host IP pattern", |
| 43 | required = true) |
| 44 | String hostIpPattern = null; |
| 45 | |
| 46 | @Argument(index = 2, name = "hostCount", description = "Number of hosts to create", |
| 47 | required = true) |
| 48 | int hostCount = 0; |
| 49 | |
| 50 | @Override |
| 51 | protected void execute() { |
| 52 | NullProviders service = get(NullProviders.class); |
| 53 | |
| 54 | TopologySimulator simulator = service.currentSimulator(); |
| 55 | if (!validateSimulator(simulator)) { |
| 56 | return; |
| 57 | } |
| 58 | |
| 59 | CustomTopologySimulator sim = (CustomTopologySimulator) simulator; |
| 60 | |
| 61 | List<ConnectPoint> points = findAvailablePorts(sim.deviceId(deviceName)); |
| 62 | String pattern = hostIpPattern.replace("*", "%d"); |
| 63 | for (int h = 0; h < hostCount; h++) { |
| 64 | HostLocation location = new HostLocation(points.get(h), System.currentTimeMillis()); |
| 65 | IpAddress ip = IpAddress.valueOf(String.format(pattern, h)); |
| 66 | HostId id = sim.nextHostId(); |
| 67 | sim.createHost(id, location, ip); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | } |