Thomas Vachuska | 5c6ed22 | 2016-06-29 11:02:22 +0200 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Thomas Vachuska | 5c6ed22 | 2016-06-29 11:02:22 +0200 | [diff] [blame] | 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; |
| 18 | |
| 19 | import com.google.common.collect.Maps; |
Thomas Vachuska | 4407a7e | 2016-07-02 12:38:01 +0200 | [diff] [blame] | 20 | import org.onlab.packet.IpAddress; |
Thomas Vachuska | 5c6ed22 | 2016-06-29 11:02:22 +0200 | [diff] [blame] | 21 | import org.onlab.packet.MacAddress; |
| 22 | import org.onlab.packet.VlanId; |
| 23 | import org.onosproject.net.Device; |
| 24 | import org.onosproject.net.DeviceId; |
| 25 | import org.onosproject.net.HostId; |
Thomas Vachuska | 4407a7e | 2016-07-02 12:38:01 +0200 | [diff] [blame] | 26 | import org.onosproject.net.HostLocation; |
| 27 | import org.onosproject.net.host.DefaultHostDescription; |
Thomas Vachuska | 5c6ed22 | 2016-06-29 11:02:22 +0200 | [diff] [blame] | 28 | |
| 29 | import java.util.Map; |
Thomas Vachuska | b1906d2 | 2018-02-14 16:50:16 -0800 | [diff] [blame] | 30 | import java.util.Set; |
Thomas Vachuska | 5c6ed22 | 2016-06-29 11:02:22 +0200 | [diff] [blame] | 31 | |
| 32 | import static org.onlab.util.Tools.toHex; |
| 33 | import static org.onosproject.provider.nil.NullProviders.SCHEME; |
| 34 | |
| 35 | /** |
| 36 | * Custom topology defined by a concise language. |
| 37 | */ |
| 38 | public class CustomTopologySimulator extends TopologySimulator { |
| 39 | |
| 40 | private int nextDeviceId = 0; |
| 41 | private int nextHostId = 0; |
| 42 | |
| 43 | private Map<String, DeviceId> nameToId = Maps.newConcurrentMap(); |
| 44 | |
| 45 | /** |
| 46 | * Returns the next device id. |
| 47 | * |
| 48 | * @return the next device id |
| 49 | */ |
| 50 | public DeviceId nextDeviceId() { |
| 51 | return DeviceId.deviceId(SCHEME + ":" + toHex(++nextDeviceId)); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Returns the next host id. |
| 56 | * |
| 57 | * @return the next host id |
| 58 | */ |
| 59 | public HostId nextHostId() { |
Thomas Vachuska | 83da9b7 | 2016-07-02 12:52:23 +0200 | [diff] [blame] | 60 | return HostId.hostId(MacAddress.valueOf(++nextHostId), VlanId.NONE); |
Thomas Vachuska | 5c6ed22 | 2016-06-29 11:02:22 +0200 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Returns the identifier of the device with the specified alias. |
| 65 | * |
| 66 | * @param name device name |
| 67 | * @return device identifier |
| 68 | */ |
| 69 | public DeviceId deviceId(String name) { |
| 70 | return nameToId.get(name); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Creates simulated device. |
| 75 | * |
| 76 | * @param id device identifier |
| 77 | * @param name device name |
| 78 | * @param type device type |
| 79 | * @param portCount number of device ports |
| 80 | */ |
| 81 | public void createDevice(DeviceId id, String name, Device.Type type, int portCount) { |
Andreas Pantelopoulos | f28c056 | 2016-11-12 14:12:05 +0100 | [diff] [blame] | 82 | int chassisId = Integer.parseInt(id.uri().getSchemeSpecificPart(), 16); |
Thomas Vachuska | 5c6ed22 | 2016-06-29 11:02:22 +0200 | [diff] [blame] | 83 | createDevice(id, chassisId, type, portCount); |
| 84 | nameToId.put(name, id); |
| 85 | } |
| 86 | |
Thomas Vachuska | 4407a7e | 2016-07-02 12:38:01 +0200 | [diff] [blame] | 87 | /** |
Thomas Vachuska | 7b1fadc | 2018-09-27 11:20:41 -0700 | [diff] [blame] | 88 | * Creates simulated device. |
| 89 | * |
| 90 | * @param id device identifier |
| 91 | * @param name device name |
| 92 | * @param type device type |
| 93 | * @param hw hardware revision |
| 94 | * @param sw software revision |
| 95 | * @param portCount number of device ports |
| 96 | */ |
| 97 | public void createDevice(DeviceId id, String name, Device.Type type, |
| 98 | String hw, String sw, int portCount) { |
| 99 | int chassisId = Integer.parseInt(id.uri().getSchemeSpecificPart(), 16); |
| 100 | createDevice(id, chassisId, type, hw, sw, portCount); |
| 101 | nameToId.put(name, id); |
| 102 | } |
| 103 | |
| 104 | /** |
Thomas Vachuska | 4407a7e | 2016-07-02 12:38:01 +0200 | [diff] [blame] | 105 | * Creates a simulated host. |
| 106 | * |
| 107 | * @param hostId host identifier |
| 108 | * @param location host location |
| 109 | * @param hostIp host IP address |
| 110 | */ |
| 111 | public void createHost(HostId hostId, HostLocation location, IpAddress hostIp) { |
| 112 | DefaultHostDescription description = |
| 113 | new DefaultHostDescription(hostId.mac(), hostId.vlanId(), location, hostIp); |
| 114 | hostProviderService.hostDetected(hostId, description, false); |
| 115 | } |
| 116 | |
Thomas Vachuska | b1906d2 | 2018-02-14 16:50:16 -0800 | [diff] [blame] | 117 | /** |
| 118 | * Creates a simulated multi-homed host. |
| 119 | * |
| 120 | * @param hostId host identifier |
| 121 | * @param locations host locations |
| 122 | * @param hostIps host IP addresses |
| 123 | */ |
| 124 | public void createHost(HostId hostId, Set<HostLocation> locations, Set<IpAddress> hostIps) { |
| 125 | DefaultHostDescription description = |
| 126 | new DefaultHostDescription(hostId.mac(), hostId.vlanId(), locations, hostIps, false); |
| 127 | hostProviderService.hostDetected(hostId, description, false); |
| 128 | } |
| 129 | |
Thomas Vachuska | 5c6ed22 | 2016-06-29 11:02:22 +0200 | [diff] [blame] | 130 | @Override |
| 131 | protected void createDevices() { |
| 132 | } |
| 133 | |
| 134 | @Override |
| 135 | protected void createLinks() { |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | protected void createHosts() { |
| 140 | } |
Simon Hunt | b356728 | 2017-04-07 22:59:28 -0700 | [diff] [blame] | 141 | |
Thomas Vachuska | cab29d2 | 2018-02-21 15:47:29 -0800 | [diff] [blame] | 142 | @Override |
| 143 | public void tearDownTopology() { |
| 144 | super.tearDownTopology(); |
Simon Hunt | b356728 | 2017-04-07 22:59:28 -0700 | [diff] [blame] | 145 | nextDeviceId = 0; |
| 146 | nextHostId = 0; |
Thomas Vachuska | cab29d2 | 2018-02-21 15:47:29 -0800 | [diff] [blame] | 147 | nameToId.clear(); |
Simon Hunt | b356728 | 2017-04-07 22:59:28 -0700 | [diff] [blame] | 148 | } |
Thomas Vachuska | cab29d2 | 2018-02-21 15:47:29 -0800 | [diff] [blame] | 149 | |
Thomas Vachuska | 5c6ed22 | 2016-06-29 11:02:22 +0200 | [diff] [blame] | 150 | } |