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; |
| 30 | |
| 31 | import static org.onlab.util.Tools.toHex; |
| 32 | import static org.onosproject.provider.nil.NullProviders.SCHEME; |
| 33 | |
| 34 | /** |
| 35 | * Custom topology defined by a concise language. |
| 36 | */ |
| 37 | public class CustomTopologySimulator extends TopologySimulator { |
| 38 | |
| 39 | private int nextDeviceId = 0; |
| 40 | private int nextHostId = 0; |
| 41 | |
| 42 | private Map<String, DeviceId> nameToId = Maps.newConcurrentMap(); |
| 43 | |
| 44 | /** |
| 45 | * Returns the next device id. |
| 46 | * |
| 47 | * @return the next device id |
| 48 | */ |
| 49 | public DeviceId nextDeviceId() { |
| 50 | return DeviceId.deviceId(SCHEME + ":" + toHex(++nextDeviceId)); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Returns the next host id. |
| 55 | * |
| 56 | * @return the next host id |
| 57 | */ |
| 58 | public HostId nextHostId() { |
Thomas Vachuska | 83da9b7 | 2016-07-02 12:52:23 +0200 | [diff] [blame] | 59 | return HostId.hostId(MacAddress.valueOf(++nextHostId), VlanId.NONE); |
Thomas Vachuska | 5c6ed22 | 2016-06-29 11:02:22 +0200 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Returns the identifier of the device with the specified alias. |
| 64 | * |
| 65 | * @param name device name |
| 66 | * @return device identifier |
| 67 | */ |
| 68 | public DeviceId deviceId(String name) { |
| 69 | return nameToId.get(name); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Creates simulated device. |
| 74 | * |
| 75 | * @param id device identifier |
| 76 | * @param name device name |
| 77 | * @param type device type |
| 78 | * @param portCount number of device ports |
| 79 | */ |
| 80 | public void createDevice(DeviceId id, String name, Device.Type type, int portCount) { |
Andreas Pantelopoulos | f28c056 | 2016-11-12 14:12:05 +0100 | [diff] [blame] | 81 | int chassisId = Integer.parseInt(id.uri().getSchemeSpecificPart(), 16); |
Thomas Vachuska | 5c6ed22 | 2016-06-29 11:02:22 +0200 | [diff] [blame] | 82 | createDevice(id, chassisId, type, portCount); |
| 83 | nameToId.put(name, id); |
| 84 | } |
| 85 | |
Thomas Vachuska | 4407a7e | 2016-07-02 12:38:01 +0200 | [diff] [blame] | 86 | /** |
| 87 | * Creates a simulated host. |
| 88 | * |
| 89 | * @param hostId host identifier |
| 90 | * @param location host location |
| 91 | * @param hostIp host IP address |
| 92 | */ |
| 93 | public void createHost(HostId hostId, HostLocation location, IpAddress hostIp) { |
| 94 | DefaultHostDescription description = |
| 95 | new DefaultHostDescription(hostId.mac(), hostId.vlanId(), location, hostIp); |
| 96 | hostProviderService.hostDetected(hostId, description, false); |
| 97 | } |
| 98 | |
Thomas Vachuska | 5c6ed22 | 2016-06-29 11:02:22 +0200 | [diff] [blame] | 99 | @Override |
| 100 | protected void createDevices() { |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | protected void createLinks() { |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | protected void createHosts() { |
| 109 | } |
Simon Hunt | b356728 | 2017-04-07 22:59:28 -0700 | [diff] [blame] | 110 | |
| 111 | /** |
| 112 | * Resets the device and host ID seeds to the default values. That is, the |
| 113 | * next assigned values will start from 1 again. |
| 114 | */ |
| 115 | public void resetIdSeeds() { |
| 116 | nextDeviceId = 0; |
| 117 | nextHostId = 0; |
| 118 | } |
Thomas Vachuska | 5c6ed22 | 2016-06-29 11:02:22 +0200 | [diff] [blame] | 119 | } |