blob: 98dd21076f8a083803407bf4cfdd2c1eb2c4da5e [file] [log] [blame]
Thomas Vachuskac40d4632015-04-09 16:55:03 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Thomas Vachuskac40d4632015-04-09 16:55:03 -07003 *
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 */
16package org.onosproject.provider.nil;
17
18import static com.google.common.base.Preconditions.checkArgument;
19
20/**
21 * Tree topology with hosts at the leaf devices.
22 */
23public 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 Vachuskaf651cc92015-04-14 16:11:44 -070058 int portOffset = 1;
Thomas Vachuskac40d4632015-04-09 16:55:03 -070059 for (int t = 1; t < tierOffset.length; t++) {
60 int child = tierOffset[t];
61 for (int parent = tierOffset[t - 1]; parent < tierOffset[t]; parent++) {
62 for (int i = 0; i < tierMultiplier[t]; i++) {
Thomas Vachuskaf651cc92015-04-14 16:11:44 -070063 createLink(parent, child, i + portOffset, 1);
Thomas Vachuskac40d4632015-04-09 16:55:03 -070064 child++;
65 }
66 }
Thomas Vachuskaf651cc92015-04-14 16:11:44 -070067 portOffset = 2; // beyond first tier, allow for up-links
Thomas Vachuskac40d4632015-04-09 16:55:03 -070068 }
69 }
70
71 @Override
72 protected void createHosts() {
73 for (int i = tierOffset[tierOffset.length - 1]; i < deviceCount; i++) {
Thomas Vachuska6c0582e2016-07-05 12:20:31 -070074 createHosts(deviceIds.get(i), 2);
Thomas Vachuskac40d4632015-04-09 16:55:03 -070075 }
76 }
77}