Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [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.ui.impl.topo.model; |
| 18 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableList; |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 20 | import com.google.common.collect.ImmutableSet; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 21 | import org.onlab.packet.IpAddress; |
| 22 | import org.onlab.packet.MacAddress; |
| 23 | import org.onlab.packet.VlanId; |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 24 | import org.onosproject.cluster.ClusterService; |
| 25 | import org.onosproject.cluster.ClusterServiceAdapter; |
| 26 | import org.onosproject.cluster.ControllerNode; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 27 | import org.onosproject.cluster.DefaultControllerNode; |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 28 | import org.onosproject.cluster.NodeId; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 29 | import org.onosproject.cluster.RoleInfo; |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 30 | import org.onosproject.mastership.MastershipService; |
| 31 | import org.onosproject.mastership.MastershipServiceAdapter; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 32 | import org.onosproject.net.ConnectPoint; |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 33 | import org.onosproject.net.DefaultAnnotations; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 34 | import org.onosproject.net.DefaultDevice; |
| 35 | import org.onosproject.net.DefaultHost; |
| 36 | import org.onosproject.net.DefaultLink; |
| 37 | import org.onosproject.net.Device; |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 38 | import org.onosproject.net.DeviceId; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 39 | import org.onosproject.net.Host; |
| 40 | import org.onosproject.net.HostId; |
| 41 | import org.onosproject.net.HostLocation; |
| 42 | import org.onosproject.net.Link; |
| 43 | import org.onosproject.net.PortNumber; |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 44 | import org.onosproject.net.device.DeviceService; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 45 | import org.onosproject.net.device.DeviceServiceAdapter; |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 46 | import org.onosproject.net.flow.FlowRuleService; |
| 47 | import org.onosproject.net.host.HostService; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 48 | import org.onosproject.net.host.HostServiceAdapter; |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 49 | import org.onosproject.net.intent.IntentService; |
| 50 | import org.onosproject.net.link.LinkService; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 51 | import org.onosproject.net.link.LinkServiceAdapter; |
| 52 | import org.onosproject.net.provider.ProviderId; |
| 53 | import org.onosproject.net.region.DefaultRegion; |
| 54 | import org.onosproject.net.region.Region; |
| 55 | import org.onosproject.net.region.RegionId; |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 56 | import org.onosproject.net.region.RegionService; |
Simon Hunt | 4f4ffc3 | 2016-08-03 18:30:47 -0700 | [diff] [blame] | 57 | import org.onosproject.ui.UiTopoLayoutService; |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 58 | import org.onosproject.ui.impl.AbstractUiImplTest; |
| 59 | import org.onosproject.ui.model.ServiceBundle; |
Simon Hunt | 4f4ffc3 | 2016-08-03 18:30:47 -0700 | [diff] [blame] | 60 | import org.onosproject.ui.model.topo.UiTopoLayout; |
| 61 | import org.onosproject.ui.model.topo.UiTopoLayoutId; |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 62 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 63 | import java.util.ArrayList; |
| 64 | import java.util.Collections; |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 65 | import java.util.HashMap; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 66 | import java.util.HashSet; |
| 67 | import java.util.List; |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 68 | import java.util.Map; |
| 69 | import java.util.Set; |
| 70 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 71 | import static org.onosproject.cluster.NodeId.nodeId; |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 72 | import static org.onosproject.net.DeviceId.deviceId; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 73 | import static org.onosproject.net.HostId.hostId; |
| 74 | import static org.onosproject.net.PortNumber.portNumber; |
Simon Hunt | 4f4ffc3 | 2016-08-03 18:30:47 -0700 | [diff] [blame] | 75 | import static org.onosproject.ui.model.topo.UiTopoLayoutId.layoutId; |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 76 | |
| 77 | /** |
| 78 | * Base class for model test classes. |
| 79 | */ |
| 80 | abstract class AbstractTopoModelTest extends AbstractUiImplTest { |
| 81 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 82 | /* |
| 83 | Our mock environment: |
| 84 | |
| 85 | Three controllers: C1, C2, C3 |
| 86 | |
| 87 | Nine devices: D1 .. D9 |
| 88 | |
| 89 | D4 ---+ +--- D7 |
| 90 | | | |
| 91 | D5 --- D1 --- D2 --- D3 --- D8 |
| 92 | | | |
| 93 | D6 ---+ +--- D9 |
| 94 | |
| 95 | Twelve hosts (two per D4 ... D9) H4a, H4b, H5a, H5b, ... |
| 96 | |
Simon Hunt | 4f4ffc3 | 2016-08-03 18:30:47 -0700 | [diff] [blame] | 97 | Layouts: |
| 98 | LROOT : (default) |
| 99 | +-- L1 : R1 |
| 100 | +-- L2 : R2 |
| 101 | +-- L3 : R3 |
| 102 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 103 | Regions: |
| 104 | R1 : D1, D2, D3 |
| 105 | R2 : D4, D5, D6 |
| 106 | R3 : D7, D8, D9 |
| 107 | |
| 108 | Mastership: |
| 109 | C1 : D1, D2, D3 |
| 110 | C2 : D4, D5, D6 |
| 111 | C3 : D7, D8, D9 |
| 112 | |
| 113 | Roles: (backups) |
| 114 | C1 -> C2, C3 |
| 115 | C2 -> C1, C3 |
| 116 | C3 -> C1, C2 |
| 117 | */ |
| 118 | |
| 119 | protected static final String C1 = "C1"; |
| 120 | protected static final String C2 = "C2"; |
| 121 | protected static final String C3 = "C3"; |
| 122 | |
| 123 | protected static final NodeId CNID_1 = nodeId(C1); |
| 124 | protected static final NodeId CNID_2 = nodeId(C2); |
| 125 | protected static final NodeId CNID_3 = nodeId(C3); |
| 126 | |
| 127 | protected static final ControllerNode CNODE_1 = cnode(CNID_1, "10.0.0.1"); |
| 128 | protected static final ControllerNode CNODE_2 = cnode(CNID_2, "10.0.0.2"); |
| 129 | protected static final ControllerNode CNODE_3 = cnode(CNID_3, "10.0.0.3"); |
| 130 | |
| 131 | protected static final String R1 = "R1"; |
| 132 | protected static final String R2 = "R2"; |
| 133 | protected static final String R3 = "R3"; |
| 134 | |
| 135 | protected static final Set<NodeId> SET_C1 = ImmutableSet.of(CNID_1); |
| 136 | protected static final Set<NodeId> SET_C2 = ImmutableSet.of(CNID_2); |
| 137 | protected static final Set<NodeId> SET_C3 = ImmutableSet.of(CNID_3); |
| 138 | |
| 139 | protected static final Region REGION_1 = |
| 140 | region(R1, Region.Type.METRO, ImmutableList.of(SET_C1, SET_C2)); |
| 141 | protected static final Region REGION_2 = |
| 142 | region(R2, Region.Type.CAMPUS, ImmutableList.of(SET_C2, SET_C1)); |
| 143 | protected static final Region REGION_3 = |
| 144 | region(R3, Region.Type.CAMPUS, ImmutableList.of(SET_C3, SET_C1)); |
| 145 | |
| 146 | protected static final Set<Region> REGION_SET = |
| 147 | ImmutableSet.of(REGION_1, REGION_2, REGION_3); |
| 148 | |
Simon Hunt | 4f4ffc3 | 2016-08-03 18:30:47 -0700 | [diff] [blame] | 149 | protected static final String LROOT = "LROOT"; |
| 150 | protected static final String L1 = "L1"; |
| 151 | protected static final String L2 = "L2"; |
| 152 | protected static final String L3 = "L3"; |
| 153 | |
| 154 | protected static final UiTopoLayout LAYOUT_ROOT = layout(LROOT, null, null); |
| 155 | protected static final UiTopoLayout LAYOUT_1 = layout(L1, REGION_1, LROOT); |
| 156 | protected static final UiTopoLayout LAYOUT_2 = layout(L2, REGION_2, LROOT); |
| 157 | protected static final UiTopoLayout LAYOUT_3 = layout(L3, REGION_3, LROOT); |
| 158 | |
| 159 | protected static final Set<UiTopoLayout> LAYOUT_SET = |
| 160 | ImmutableSet.of(LAYOUT_ROOT, LAYOUT_1, LAYOUT_2, LAYOUT_3); |
| 161 | protected static final Set<UiTopoLayout> ROOT_KIDS = |
| 162 | ImmutableSet.of(LAYOUT_1, LAYOUT_2, LAYOUT_3); |
| 163 | protected static final Set<UiTopoLayout> PEERS_OF_1 = |
| 164 | ImmutableSet.of(LAYOUT_2, LAYOUT_3); |
| 165 | protected static final Set<UiTopoLayout> PEERS_OF_2 = |
| 166 | ImmutableSet.of(LAYOUT_1, LAYOUT_3); |
| 167 | protected static final Set<UiTopoLayout> PEERS_OF_3 = |
| 168 | ImmutableSet.of(LAYOUT_1, LAYOUT_2); |
| 169 | |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 170 | protected static final String D1 = "d1"; |
| 171 | protected static final String D2 = "d2"; |
| 172 | protected static final String D3 = "d3"; |
| 173 | protected static final String D4 = "d4"; |
| 174 | protected static final String D5 = "d5"; |
| 175 | protected static final String D6 = "d6"; |
| 176 | protected static final String D7 = "d7"; |
| 177 | protected static final String D8 = "d8"; |
| 178 | protected static final String D9 = "d9"; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 179 | |
| 180 | protected static final String MFR = "Mfr"; |
| 181 | protected static final String HW = "h/w"; |
| 182 | protected static final String SW = "s/w"; |
| 183 | protected static final String SERIAL = "ser123"; |
| 184 | |
| 185 | protected static final DeviceId DEVID_1 = deviceId(D1); |
| 186 | protected static final DeviceId DEVID_2 = deviceId(D2); |
| 187 | protected static final DeviceId DEVID_3 = deviceId(D3); |
| 188 | protected static final DeviceId DEVID_4 = deviceId(D4); |
| 189 | protected static final DeviceId DEVID_5 = deviceId(D5); |
| 190 | protected static final DeviceId DEVID_6 = deviceId(D6); |
| 191 | protected static final DeviceId DEVID_7 = deviceId(D7); |
| 192 | protected static final DeviceId DEVID_8 = deviceId(D8); |
| 193 | protected static final DeviceId DEVID_9 = deviceId(D9); |
| 194 | |
| 195 | protected static final Device DEV_1 = device(D1); |
| 196 | protected static final Device DEV_2 = device(D2); |
| 197 | protected static final Device DEV_3 = device(D3); |
| 198 | protected static final Device DEV_4 = device(D4); |
| 199 | protected static final Device DEV_5 = device(D5); |
| 200 | protected static final Device DEV_6 = device(D6); |
| 201 | protected static final Device DEV_7 = device(D7); |
| 202 | protected static final Device DEV_8 = device(D8); |
| 203 | protected static final Device DEV_9 = device(D9); |
| 204 | |
| 205 | protected static final List<Device> ALL_DEVS = |
| 206 | ImmutableList.of( |
| 207 | DEV_1, DEV_2, DEV_3, |
| 208 | DEV_4, DEV_5, DEV_6, |
| 209 | DEV_7, DEV_8, DEV_9 |
| 210 | ); |
| 211 | |
| 212 | private static final Set<DeviceId> DEVS_TRUNK = |
| 213 | ImmutableSet.of(DEVID_1, DEVID_2, DEVID_3); |
| 214 | |
| 215 | private static final Set<DeviceId> DEVS_LEFT = |
| 216 | ImmutableSet.of(DEVID_4, DEVID_5, DEVID_6); |
| 217 | |
| 218 | private static final Set<DeviceId> DEVS_RIGHT = |
| 219 | ImmutableSet.of(DEVID_7, DEVID_8, DEVID_9); |
| 220 | |
| 221 | private static final String[][] LINK_CONNECT_DATA = { |
| 222 | {D1, "12", D2, "21"}, |
| 223 | {D2, "23", D3, "32"}, |
| 224 | {D4, "41", D1, "14"}, |
| 225 | {D5, "51", D1, "15"}, |
| 226 | {D6, "61", D1, "16"}, |
| 227 | {D7, "73", D3, "37"}, |
| 228 | {D8, "83", D3, "38"}, |
| 229 | {D9, "93", D3, "39"}, |
| 230 | }; |
| 231 | |
| 232 | private static final String HOST_MAC_PREFIX = "aa:00:00:00:00:"; |
| 233 | |
| 234 | /** |
| 235 | * Returns IP address instance for given string. |
| 236 | * |
| 237 | * @param s string |
| 238 | * @return IP address |
| 239 | */ |
| 240 | protected static IpAddress ip(String s) { |
| 241 | return IpAddress.valueOf(s); |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * Returns controller node instance for given ID and IP. |
| 246 | * |
| 247 | * @param id identifier |
| 248 | * @param ip IP address |
| 249 | * @return controller node instance |
| 250 | */ |
| 251 | protected static ControllerNode cnode(NodeId id, String ip) { |
| 252 | return new DefaultControllerNode(id, ip(ip)); |
| 253 | } |
| 254 | |
| 255 | /** |
Simon Hunt | 4f4ffc3 | 2016-08-03 18:30:47 -0700 | [diff] [blame] | 256 | * Returns UI topology layout instance with the specified parameters. |
| 257 | * |
| 258 | * @param layoutId the layout ID |
| 259 | * @param region the backing region |
| 260 | * @param parentId the parent layout ID |
| 261 | * @return layout instance |
| 262 | */ |
| 263 | protected static UiTopoLayout layout(String layoutId, Region region, |
| 264 | String parentId) { |
| 265 | UiTopoLayoutId pid = parentId == null |
| 266 | ? UiTopoLayoutId.DEFAULT_ID : layoutId(parentId); |
Simon Hunt | d0fa284 | 2016-10-24 18:04:05 -0700 | [diff] [blame] | 267 | return new UiTopoLayout(layoutId(layoutId)).region(region).parent(pid); |
Simon Hunt | 4f4ffc3 | 2016-08-03 18:30:47 -0700 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | /** |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 271 | * Returns a region instance with specified parameters. |
| 272 | * |
| 273 | * @param id region id |
| 274 | * @param type region type |
| 275 | * @param masters ordered list of master sets |
| 276 | * @return region instance |
| 277 | */ |
| 278 | protected static Region region(String id, Region.Type type, |
| 279 | List<Set<NodeId>> masters) { |
| 280 | return new DefaultRegion(RegionId.regionId(id), "Region-" + id, |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 281 | type, DefaultAnnotations.EMPTY, masters); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | /** |
| 285 | * Returns device with given ID. |
| 286 | * |
| 287 | * @param id device ID |
| 288 | * @return device instance |
| 289 | */ |
| 290 | protected static Device device(String id) { |
| 291 | return new DefaultDevice(ProviderId.NONE, deviceId(id), |
| 292 | Device.Type.SWITCH, MFR, HW, SW, SERIAL, null); |
| 293 | } |
| 294 | |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 295 | /** |
| 296 | * Returns canned results. |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 297 | * <p> |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 298 | * At some future point, we may make this "programmable", so that |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 299 | * its state can be changed over the course of a unit test. |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 300 | */ |
| 301 | protected static final ServiceBundle MOCK_SERVICES = |
| 302 | new ServiceBundle() { |
| 303 | @Override |
Simon Hunt | 4f4ffc3 | 2016-08-03 18:30:47 -0700 | [diff] [blame] | 304 | public UiTopoLayoutService layout() { |
| 305 | return MOCK_LAYOUT; |
| 306 | } |
| 307 | |
| 308 | @Override |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 309 | public ClusterService cluster() { |
| 310 | return MOCK_CLUSTER; |
| 311 | } |
| 312 | |
| 313 | @Override |
| 314 | public MastershipService mastership() { |
| 315 | return MOCK_MASTER; |
| 316 | } |
| 317 | |
| 318 | @Override |
| 319 | public RegionService region() { |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 320 | return MOCK_REGION; |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | @Override |
| 324 | public DeviceService device() { |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 325 | return MOCK_DEVICE; |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | @Override |
| 329 | public LinkService link() { |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 330 | return MOCK_LINK; |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | @Override |
| 334 | public HostService host() { |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 335 | return MOCK_HOST; |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | @Override |
| 339 | public IntentService intent() { |
| 340 | return null; |
| 341 | } |
| 342 | |
| 343 | @Override |
| 344 | public FlowRuleService flow() { |
| 345 | return null; |
| 346 | } |
| 347 | }; |
| 348 | |
| 349 | private static final ClusterService MOCK_CLUSTER = new MockClusterService(); |
| 350 | private static final MastershipService MOCK_MASTER = new MockMasterService(); |
Simon Hunt | 4f4ffc3 | 2016-08-03 18:30:47 -0700 | [diff] [blame] | 351 | private static final UiTopoLayoutService MOCK_LAYOUT = new MockLayoutService(); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 352 | private static final RegionService MOCK_REGION = new MockRegionService(); |
| 353 | private static final DeviceService MOCK_DEVICE = new MockDeviceService(); |
| 354 | private static final LinkService MOCK_LINK = new MockLinkService(); |
| 355 | private static final HostService MOCK_HOST = new MockHostService(); |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 356 | |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 357 | |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 358 | private static class MockClusterService extends ClusterServiceAdapter { |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 359 | private final Map<NodeId, ControllerNode> nodes = new HashMap<>(); |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 360 | private final Map<NodeId, ControllerNode.State> states = new HashMap<>(); |
| 361 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 362 | MockClusterService() { |
| 363 | nodes.put(CNODE_1.id(), CNODE_1); |
| 364 | nodes.put(CNODE_2.id(), CNODE_2); |
| 365 | nodes.put(CNODE_3.id(), CNODE_3); |
| 366 | |
| 367 | states.put(CNODE_1.id(), ControllerNode.State.READY); |
| 368 | states.put(CNODE_2.id(), ControllerNode.State.ACTIVE); |
| 369 | states.put(CNODE_3.id(), ControllerNode.State.ACTIVE); |
| 370 | } |
| 371 | |
| 372 | @Override |
| 373 | public Set<ControllerNode> getNodes() { |
| 374 | return ImmutableSet.copyOf(nodes.values()); |
| 375 | } |
| 376 | |
| 377 | @Override |
| 378 | public ControllerNode getNode(NodeId nodeId) { |
| 379 | return nodes.get(nodeId); |
| 380 | } |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 381 | |
| 382 | @Override |
| 383 | public ControllerNode.State getState(NodeId nodeId) { |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 384 | return states.get(nodeId); |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 385 | } |
| 386 | } |
| 387 | |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 388 | |
| 389 | private static class MockMasterService extends MastershipServiceAdapter { |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 390 | private final Map<NodeId, Set<DeviceId>> masterOf = new HashMap<>(); |
| 391 | |
| 392 | MockMasterService() { |
| 393 | masterOf.put(CNODE_1.id(), DEVS_TRUNK); |
| 394 | masterOf.put(CNODE_2.id(), DEVS_LEFT); |
| 395 | masterOf.put(CNODE_3.id(), DEVS_RIGHT); |
| 396 | } |
| 397 | |
| 398 | @Override |
| 399 | public NodeId getMasterFor(DeviceId deviceId) { |
| 400 | if (DEVS_TRUNK.contains(deviceId)) { |
| 401 | return CNID_1; |
| 402 | } |
| 403 | if (DEVS_LEFT.contains(deviceId)) { |
| 404 | return CNID_2; |
| 405 | } |
| 406 | if (DEVS_RIGHT.contains(deviceId)) { |
| 407 | return CNID_3; |
| 408 | } |
| 409 | return null; |
| 410 | } |
| 411 | |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 412 | @Override |
| 413 | public Set<DeviceId> getDevicesOf(NodeId nodeId) { |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 414 | return masterOf.get(nodeId); |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 415 | } |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 416 | |
| 417 | @Override |
| 418 | public RoleInfo getNodesFor(DeviceId deviceId) { |
| 419 | NodeId master = null; |
| 420 | List<NodeId> backups = new ArrayList<>(); |
| 421 | |
| 422 | if (DEVS_TRUNK.contains(deviceId)) { |
| 423 | master = CNID_1; |
| 424 | backups.add(CNID_2); |
| 425 | backups.add(CNID_3); |
| 426 | } else if (DEVS_LEFT.contains(deviceId)) { |
| 427 | master = CNID_2; |
| 428 | backups.add(CNID_1); |
| 429 | backups.add(CNID_3); |
| 430 | } else if (DEVS_RIGHT.contains(deviceId)) { |
| 431 | master = CNID_3; |
| 432 | backups.add(CNID_1); |
| 433 | backups.add(CNID_2); |
| 434 | } |
| 435 | return new RoleInfo(master, backups); |
| 436 | } |
| 437 | } |
| 438 | |
Simon Hunt | 4f4ffc3 | 2016-08-03 18:30:47 -0700 | [diff] [blame] | 439 | // TODO: consider implementing UiTopoLayoutServiceAdapter and extending that here |
| 440 | private static class MockLayoutService implements UiTopoLayoutService { |
| 441 | private final Map<UiTopoLayoutId, UiTopoLayout> map = new HashMap<>(); |
| 442 | private final Map<UiTopoLayoutId, Set<UiTopoLayout>> peers = new HashMap<>(); |
| 443 | private final Map<RegionId, UiTopoLayout> byRegion = new HashMap<>(); |
| 444 | |
| 445 | MockLayoutService() { |
| 446 | map.put(LAYOUT_ROOT.id(), LAYOUT_ROOT); |
| 447 | map.put(LAYOUT_1.id(), LAYOUT_1); |
| 448 | map.put(LAYOUT_2.id(), LAYOUT_2); |
| 449 | map.put(LAYOUT_3.id(), LAYOUT_3); |
| 450 | |
| 451 | peers.put(LAYOUT_ROOT.id(), ImmutableSet.of()); |
| 452 | peers.put(LAYOUT_1.id(), ImmutableSet.of(LAYOUT_2, LAYOUT_3)); |
| 453 | peers.put(LAYOUT_2.id(), ImmutableSet.of(LAYOUT_1, LAYOUT_3)); |
| 454 | peers.put(LAYOUT_3.id(), ImmutableSet.of(LAYOUT_1, LAYOUT_2)); |
| 455 | |
| 456 | byRegion.put(REGION_1.id(), LAYOUT_1); |
| 457 | byRegion.put(REGION_2.id(), LAYOUT_2); |
| 458 | byRegion.put(REGION_3.id(), LAYOUT_3); |
| 459 | } |
| 460 | |
| 461 | @Override |
| 462 | public UiTopoLayout getRootLayout() { |
| 463 | return LAYOUT_ROOT; |
| 464 | } |
| 465 | |
| 466 | @Override |
| 467 | public Set<UiTopoLayout> getLayouts() { |
| 468 | return LAYOUT_SET; |
| 469 | } |
| 470 | |
| 471 | @Override |
| 472 | public boolean addLayout(UiTopoLayout layout) { |
| 473 | return false; |
| 474 | } |
| 475 | |
| 476 | @Override |
| 477 | public UiTopoLayout getLayout(UiTopoLayoutId layoutId) { |
| 478 | return map.get(layoutId); |
| 479 | } |
| 480 | |
| 481 | @Override |
| 482 | public UiTopoLayout getLayout(RegionId regionId) { |
| 483 | return byRegion.get(regionId); |
| 484 | } |
| 485 | |
| 486 | @Override |
| 487 | public Set<UiTopoLayout> getPeerLayouts(UiTopoLayoutId layoutId) { |
| 488 | return peers.get(layoutId); |
| 489 | } |
| 490 | |
| 491 | @Override |
| 492 | public Set<UiTopoLayout> getChildren(UiTopoLayoutId layoutId) { |
| 493 | return LAYOUT_ROOT.id().equals(layoutId) |
| 494 | ? ROOT_KIDS |
| 495 | : Collections.emptySet(); |
| 496 | } |
| 497 | |
| 498 | @Override |
| 499 | public boolean removeLayout(UiTopoLayout layout) { |
| 500 | return false; |
| 501 | } |
| 502 | } |
| 503 | |
Steven Burrows | 19e6e4f | 2016-10-05 13:27:07 -0500 | [diff] [blame] | 504 | private static class MockRegionService extends RegionServiceAdapter { |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 505 | |
| 506 | private final Map<RegionId, Region> lookup = new HashMap<>(); |
| 507 | |
| 508 | MockRegionService() { |
| 509 | lookup.put(REGION_1.id(), REGION_1); |
| 510 | lookup.put(REGION_2.id(), REGION_2); |
| 511 | lookup.put(REGION_3.id(), REGION_3); |
| 512 | } |
| 513 | |
| 514 | @Override |
| 515 | public Set<Region> getRegions() { |
| 516 | return REGION_SET; |
| 517 | } |
| 518 | |
| 519 | @Override |
| 520 | public Region getRegion(RegionId regionId) { |
| 521 | return lookup.get(regionId); |
| 522 | } |
| 523 | |
| 524 | @Override |
| 525 | public Region getRegionForDevice(DeviceId deviceId) { |
| 526 | if (DEVS_TRUNK.contains(deviceId)) { |
| 527 | return REGION_1; |
| 528 | } |
| 529 | if (DEVS_LEFT.contains(deviceId)) { |
| 530 | return REGION_2; |
| 531 | } |
| 532 | if (DEVS_RIGHT.contains(deviceId)) { |
| 533 | return REGION_3; |
| 534 | } |
| 535 | return null; |
| 536 | } |
| 537 | |
| 538 | @Override |
| 539 | public Set<DeviceId> getRegionDevices(RegionId regionId) { |
| 540 | if (REGION_1.id().equals(regionId)) { |
| 541 | return DEVS_TRUNK; |
| 542 | } |
| 543 | if (REGION_2.id().equals(regionId)) { |
| 544 | return DEVS_LEFT; |
| 545 | } |
| 546 | if (REGION_3.id().equals(regionId)) { |
| 547 | return DEVS_RIGHT; |
| 548 | } |
| 549 | return Collections.emptySet(); |
| 550 | } |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | |
| 554 | private static class MockDeviceService extends DeviceServiceAdapter { |
| 555 | private final Map<DeviceId, Device> devices = new HashMap<>(); |
| 556 | |
| 557 | MockDeviceService() { |
| 558 | for (Device dev : ALL_DEVS) { |
| 559 | devices.put(dev.id(), dev); |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | @Override |
| 564 | public int getDeviceCount() { |
| 565 | return devices.size(); |
| 566 | } |
| 567 | |
| 568 | @Override |
| 569 | public Iterable<Device> getDevices() { |
| 570 | return ImmutableList.copyOf(devices.values()); |
| 571 | } |
| 572 | |
| 573 | @Override |
| 574 | public Device getDevice(DeviceId deviceId) { |
| 575 | return devices.get(deviceId); |
| 576 | } |
| 577 | |
| 578 | } |
| 579 | |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 580 | /** |
| 581 | * Synthesizes a pair of unidirectional links between two devices. The |
| 582 | * string array should be of the form: |
| 583 | * <pre> |
| 584 | * { "device-A-id", "device-A-port", "device-B-id", "device-B-port" } |
| 585 | * </pre> |
| 586 | * |
| 587 | * @param linkPairData device ids and ports |
| 588 | * @return pair of synthesized links |
| 589 | */ |
| 590 | protected static List<Link> makeLinkPair(String[] linkPairData) { |
| 591 | DeviceId devA = deviceId(linkPairData[0]); |
| 592 | PortNumber portA = portNumber(Long.valueOf(linkPairData[1])); |
| 593 | DeviceId devB = deviceId(linkPairData[2]); |
| 594 | PortNumber portB = portNumber(Long.valueOf(linkPairData[3])); |
| 595 | |
| 596 | Link linkA = DefaultLink.builder() |
| 597 | .providerId(ProviderId.NONE) |
| 598 | .type(Link.Type.DIRECT) |
| 599 | .src(new ConnectPoint(devA, portA)) |
| 600 | .dst(new ConnectPoint(devB, portB)) |
| 601 | .build(); |
| 602 | |
| 603 | Link linkB = DefaultLink.builder() |
| 604 | .providerId(ProviderId.NONE) |
| 605 | .type(Link.Type.DIRECT) |
| 606 | .src(new ConnectPoint(devB, portB)) |
| 607 | .dst(new ConnectPoint(devA, portA)) |
| 608 | .build(); |
| 609 | |
| 610 | return ImmutableList.of(linkA, linkB); |
| 611 | } |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 612 | |
| 613 | private static class MockLinkService extends LinkServiceAdapter { |
| 614 | private final Set<Link> links = new HashSet<>(); |
| 615 | |
| 616 | MockLinkService() { |
| 617 | for (String[] linkPair : LINK_CONNECT_DATA) { |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 618 | links.addAll(makeLinkPair(linkPair)); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 619 | } |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | @Override |
| 623 | public int getLinkCount() { |
| 624 | return links.size(); |
| 625 | } |
| 626 | |
| 627 | @Override |
| 628 | public Iterable<Link> getLinks() { |
| 629 | return ImmutableSet.copyOf(links); |
| 630 | } |
| 631 | |
| 632 | // TODO: possibly fill out other methods if we find the model uses them |
| 633 | } |
| 634 | |
| 635 | |
Simon Hunt | eae81ee | 2016-05-19 12:33:22 -0700 | [diff] [blame] | 636 | /** |
| 637 | * Creates a default host connected at the given edge device and port. Note |
| 638 | * that an identifying hex character ("a" - "f") should be supplied. This |
| 639 | * will be included in the MAC address of the host (and equivalent value |
| 640 | * as last byte in IP address). |
| 641 | * |
| 642 | * @param device edge device |
| 643 | * @param port port number |
| 644 | * @param hexChar identifying hex character |
| 645 | * @return host connected at that location |
| 646 | */ |
| 647 | protected static Host createHost(Device device, int port, String hexChar) { |
| 648 | DeviceId deviceId = device.id(); |
| 649 | String devNum = deviceId.toString().substring(1); |
| 650 | |
| 651 | MacAddress mac = MacAddress.valueOf(HOST_MAC_PREFIX + devNum + hexChar); |
| 652 | HostId hostId = hostId(String.format("%s/-1", mac)); |
| 653 | |
| 654 | int ipByte = Integer.valueOf(hexChar, 16); |
| 655 | if (ipByte < 10 || ipByte > 15) { |
| 656 | throw new IllegalArgumentException("hexChar must be a-f"); |
| 657 | } |
| 658 | HostLocation loc = new HostLocation(deviceId, portNumber(port), 0); |
| 659 | |
| 660 | IpAddress ip = ip("10." + devNum + ".0." + ipByte); |
| 661 | |
| 662 | return new DefaultHost(ProviderId.NONE, hostId, mac, VlanId.NONE, |
| 663 | loc, ImmutableSet.of(ip)); |
| 664 | } |
| 665 | |
| 666 | /** |
| 667 | * Creates a pair of hosts connected to the specified device. |
| 668 | * |
| 669 | * @param d edge device |
| 670 | * @return pair of hosts |
| 671 | */ |
| 672 | protected static List<Host> createHostPair(Device d) { |
| 673 | List<Host> hosts = new ArrayList<>(); |
| 674 | hosts.add(createHost(d, 101, "a")); |
| 675 | hosts.add(createHost(d, 102, "b")); |
| 676 | return hosts; |
| 677 | } |
| 678 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 679 | private static class MockHostService extends HostServiceAdapter { |
| 680 | private final Map<HostId, Host> hosts = new HashMap<>(); |
| 681 | |
| 682 | MockHostService() { |
| 683 | for (Device d : ALL_DEVS) { |
Simon Hunt | eae81ee | 2016-05-19 12:33:22 -0700 | [diff] [blame] | 684 | for (Host h : createHostPair(d)) { |
| 685 | hosts.put(h.id(), h); |
| 686 | } |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 687 | } |
| 688 | } |
| 689 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 690 | @Override |
| 691 | public int getHostCount() { |
| 692 | return hosts.size(); |
| 693 | } |
| 694 | |
| 695 | @Override |
| 696 | public Iterable<Host> getHosts() { |
| 697 | return ImmutableSet.copyOf(hosts.values()); |
| 698 | } |
| 699 | |
| 700 | @Override |
| 701 | public Host getHost(HostId hostId) { |
| 702 | return hosts.get(hostId); |
| 703 | } |
| 704 | |
| 705 | // TODO: possibly fill out other methods, should the model require them |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | } |