blob: 64aafa4726444ea9c339354daea2d77db6664276 [file] [log] [blame]
Thomas Vachuskac40d4632015-04-09 16:55:03 -07001/*
2 * Copyright 2015 Open Networking Laboratory
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 */
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 Vachuska8e038eb2016-02-23 23:28:23 -080058
Thomas Vachuskaf651cc92015-04-14 16:11:44 -070059 int portOffset = 1;
Thomas Vachuskac40d4632015-04-09 16:55:03 -070060 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 Vachuskaf651cc92015-04-14 16:11:44 -070064 createLink(parent, child, i + portOffset, 1);
Thomas Vachuskac40d4632015-04-09 16:55:03 -070065 child++;
66 }
67 }
Thomas Vachuskaf651cc92015-04-14 16:11:44 -070068 portOffset = 2; // beyond first tier, allow for up-links
Thomas Vachuskac40d4632015-04-09 16:55:03 -070069 }
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}