Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -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 | package org.onosproject.provider.nil; |
| 17 | |
| 18 | import static com.google.common.base.Preconditions.checkArgument; |
| 19 | |
| 20 | /** |
| 21 | * Tree topology with hosts at the leaf devices. |
| 22 | */ |
| 23 | public class TreeTopologySimulator extends TopologySimulator { |
| 24 | |
| 25 | private int[] tierMultiplier; |
| 26 | private int[] tierDeviceCount; |
| 27 | private int[] tierOffset; |
| 28 | |
| 29 | @Override |
| 30 | protected void processTopoShape(String shape) { |
| 31 | super.processTopoShape(shape); |
| 32 | tierOffset = new int[topoShape.length]; |
| 33 | tierMultiplier = new int[topoShape.length]; |
| 34 | tierDeviceCount = new int[topoShape.length]; |
| 35 | |
| 36 | deviceCount = 1; |
| 37 | |
| 38 | tierOffset[0] = 0; |
| 39 | tierMultiplier[0] = 1; |
| 40 | tierDeviceCount[0] = deviceCount; |
| 41 | |
| 42 | for (int i = 1; i < topoShape.length; i++) { |
| 43 | tierOffset[i] = deviceCount; |
| 44 | tierMultiplier[i] = Integer.parseInt(topoShape[i]); |
| 45 | tierDeviceCount[i] = tierDeviceCount[i - 1] * tierMultiplier[i]; |
| 46 | deviceCount = deviceCount + tierDeviceCount[i]; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | @Override |
| 51 | public void setUpTopology() { |
| 52 | checkArgument(tierDeviceCount.length > 0, "There must be at least one tree tier"); |
| 53 | super.setUpTopology(); |
| 54 | } |
| 55 | |
| 56 | @Override |
| 57 | protected void createLinks() { |
Thomas Vachuska | 8e038eb | 2016-02-23 23:28:23 -0800 | [diff] [blame] | 58 | |
Thomas Vachuska | f651cc9 | 2015-04-14 16:11:44 -0700 | [diff] [blame] | 59 | int portOffset = 1; |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 60 | for (int t = 1; t < tierOffset.length; t++) { |
| 61 | int child = tierOffset[t]; |
| 62 | for (int parent = tierOffset[t - 1]; parent < tierOffset[t]; parent++) { |
| 63 | for (int i = 0; i < tierMultiplier[t]; i++) { |
Thomas Vachuska | f651cc9 | 2015-04-14 16:11:44 -0700 | [diff] [blame] | 64 | createLink(parent, child, i + portOffset, 1); |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 65 | child++; |
| 66 | } |
| 67 | } |
Thomas Vachuska | f651cc9 | 2015-04-14 16:11:44 -0700 | [diff] [blame] | 68 | portOffset = 2; // beyond first tier, allow for up-links |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | protected void createHosts() { |
| 74 | for (int i = tierOffset[tierOffset.length - 1]; i < deviceCount; i++) { |
| 75 | createHosts(deviceIds.get(i), hostCount); |
| 76 | } |
| 77 | } |
| 78 | } |