sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame^] | 1 | package org.onosproject.segmentrouting; |
| 2 | |
| 3 | import org.onlab.packet.Ip4Address; |
| 4 | import org.onlab.packet.MacAddress; |
| 5 | import org.onosproject.grouphandler.DeviceProperties; |
| 6 | import org.onosproject.net.DeviceId; |
| 7 | import org.slf4j.Logger; |
| 8 | import org.slf4j.LoggerFactory; |
| 9 | |
| 10 | import java.util.Arrays; |
| 11 | import java.util.HashMap; |
| 12 | import java.util.List; |
| 13 | import java.util.Map; |
| 14 | |
| 15 | public class DeviceConfiguration implements DeviceProperties { |
| 16 | |
| 17 | private static final Logger log = LoggerFactory |
| 18 | .getLogger(DeviceConfiguration.class); |
| 19 | private final List<Integer> allSegmentIds = |
| 20 | Arrays.asList(101, 102, 103, 104, 105, 106); |
| 21 | private HashMap<DeviceId, Integer> deviceSegmentIdMap = |
| 22 | new HashMap<DeviceId, Integer>() { |
| 23 | { |
| 24 | put(DeviceId.deviceId("of:0000000000000001"), 101); |
| 25 | put(DeviceId.deviceId("of:0000000000000002"), 102); |
| 26 | put(DeviceId.deviceId("of:0000000000000003"), 103); |
| 27 | put(DeviceId.deviceId("of:0000000000000004"), 104); |
| 28 | put(DeviceId.deviceId("of:0000000000000005"), 105); |
| 29 | put(DeviceId.deviceId("of:0000000000000006"), 106); |
| 30 | } |
| 31 | }; |
| 32 | private final HashMap<DeviceId, MacAddress> deviceMacMap = |
| 33 | new HashMap<DeviceId, MacAddress>() { |
| 34 | { |
| 35 | put(DeviceId.deviceId("of:0000000000000001"), |
| 36 | MacAddress.valueOf("00:00:00:00:00:01")); |
| 37 | put(DeviceId.deviceId("of:0000000000000002"), |
| 38 | MacAddress.valueOf("00:00:00:00:00:02")); |
| 39 | put(DeviceId.deviceId("of:0000000000000003"), |
| 40 | MacAddress.valueOf("00:00:00:00:00:03")); |
| 41 | put(DeviceId.deviceId("of:0000000000000004"), |
| 42 | MacAddress.valueOf("00:00:00:00:00:04")); |
| 43 | put(DeviceId.deviceId("of:0000000000000005"), |
| 44 | MacAddress.valueOf("00:00:00:00:00:05")); |
| 45 | put(DeviceId.deviceId("of:0000000000000006"), |
| 46 | MacAddress.valueOf("00:00:00:00:00:06")); |
| 47 | } |
| 48 | }; |
| 49 | |
| 50 | private final HashMap<DeviceId, Ip4Address> deviceIpMap = |
| 51 | new HashMap<DeviceId, Ip4Address>() { |
| 52 | { |
| 53 | put(DeviceId.deviceId("of:0000000000000001"), |
| 54 | Ip4Address.valueOf("192.168.0.1")); |
| 55 | put(DeviceId.deviceId("of:0000000000000002"), |
| 56 | Ip4Address.valueOf("192.168.0.2")); |
| 57 | put(DeviceId.deviceId("of:0000000000000003"), |
| 58 | Ip4Address.valueOf("192.168.0.3")); |
| 59 | put(DeviceId.deviceId("of:0000000000000004"), |
| 60 | Ip4Address.valueOf("192.168.0.4")); |
| 61 | put(DeviceId.deviceId("of:0000000000000005"), |
| 62 | Ip4Address.valueOf("192.168.0.5")); |
| 63 | put(DeviceId.deviceId("of:0000000000000006"), |
| 64 | Ip4Address.valueOf("192.168.0.6")); |
| 65 | } |
| 66 | }; |
| 67 | |
| 68 | @Override |
| 69 | public int getSegmentId(DeviceId deviceId) { |
| 70 | if (deviceSegmentIdMap.get(deviceId) != null) { |
| 71 | log.debug("getSegmentId for device{} is {}", |
| 72 | deviceId, |
| 73 | deviceSegmentIdMap.get(deviceId)); |
| 74 | return deviceSegmentIdMap.get(deviceId); |
| 75 | } else { |
| 76 | throw new IllegalStateException(); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | |
| 81 | @Override |
| 82 | public MacAddress getDeviceMac(DeviceId deviceId) { |
| 83 | if (deviceMacMap.get(deviceId) != null) { |
| 84 | log.debug("getDeviceMac for device{} is {}", |
| 85 | deviceId, |
| 86 | deviceMacMap.get(deviceId)); |
| 87 | return deviceMacMap.get(deviceId); |
| 88 | } else { |
| 89 | throw new IllegalStateException(); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | |
| 94 | @Override |
| 95 | public boolean isEdgeDevice(DeviceId deviceId) { |
| 96 | if (deviceId.equals(DeviceId.deviceId("of:0000000000000001")) |
| 97 | || deviceId.equals(DeviceId.deviceId("of:0000000000000006"))) { |
| 98 | return true; |
| 99 | } |
| 100 | |
| 101 | return false; |
| 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public List<Integer> getAllDeviceSegmentIds() { |
| 106 | return allSegmentIds; |
| 107 | } |
| 108 | |
| 109 | |
| 110 | /** |
| 111 | * Returns Segment ID for the router with the MAC address given. |
| 112 | * |
| 113 | * @param targetMac Mac address for the router |
| 114 | * @return Segment ID for the router with the MAC address |
| 115 | */ |
| 116 | public int getSegmentId(MacAddress targetMac) { |
| 117 | for (Map.Entry<DeviceId, MacAddress> entry: deviceMacMap.entrySet()) { |
| 118 | if (entry.getValue().equals(targetMac)) { |
| 119 | return deviceSegmentIdMap.get(entry.getKey()); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | return -1; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Returns Segment ID for the router withe IP address given. |
| 128 | * |
| 129 | * @param targetAddress IP address of the router |
| 130 | * @return Segment ID for the router with the IP address |
| 131 | */ |
| 132 | public int getSegmentId(Ip4Address targetAddress) { |
| 133 | for (Map.Entry<DeviceId, Ip4Address> entry: deviceIpMap.entrySet()) { |
| 134 | if (entry.getValue().equals(targetAddress)) { |
| 135 | return deviceSegmentIdMap.get(entry.getKey()); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | return -1; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Returns Router IP address for the router with the device ID given. |
| 144 | * |
| 145 | * @param deviceId device ID of the router |
| 146 | * @return IP address of the router |
| 147 | */ |
| 148 | public Ip4Address getRouterIp(DeviceId deviceId) { |
| 149 | if (deviceIpMap.get(deviceId) != null) { |
| 150 | log.debug("getDeviceIp for device{} is {}", |
| 151 | deviceId, |
| 152 | deviceIpMap.get(deviceId)); |
| 153 | return deviceIpMap.get(deviceId); |
| 154 | } else { |
| 155 | throw new IllegalStateException(); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Returns the Device ID of the router with the Segment ID given. |
| 161 | * |
| 162 | * @param sid Segment ID of the router |
| 163 | * @return Device ID of the router |
| 164 | */ |
| 165 | public DeviceId getDeviceId(int sid) { |
| 166 | for (Map.Entry<DeviceId, Integer> entry: deviceSegmentIdMap.entrySet()) { |
| 167 | if (entry.getValue() == sid) { |
| 168 | return entry.getKey(); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | return null; |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Returns the Device ID of the router with the IP address given. |
| 177 | * |
| 178 | * @param ipAddress IP address of the router |
| 179 | * @return Device ID of the router |
| 180 | */ |
| 181 | public DeviceId getDeviceId(Ip4Address ipAddress) { |
| 182 | for (Map.Entry<DeviceId, Ip4Address> entry: deviceIpMap.entrySet()) { |
| 183 | if (entry.getValue().equals(ipAddress)) { |
| 184 | return entry.getKey(); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | return null; |
| 189 | } |
| 190 | } |