blob: 105840e87a4ad5ddf1b7e23de3837960d3e6508c [file] [log] [blame]
Thomas Vachuska0d933862018-04-06 00:29:30 -07001/*
2 * Copyright 2018-present Open Networking Foundation
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
17package org.onosproject.layout;
18
19import com.google.common.collect.HashMultiset;
20import com.google.common.collect.Multiset;
21import com.google.common.collect.Sets;
22import org.onosproject.net.ConnectPoint;
23import org.onosproject.net.Device;
24import org.onosproject.net.DeviceId;
25import org.onosproject.net.Host;
26import org.onosproject.net.HostId;
27import org.onosproject.utils.Comparators;
28
29import java.util.List;
30import java.util.Set;
31import java.util.stream.Collectors;
32
33/**
34 * Arranges access network according to roles assigned to devices and hosts.
35 */
36public class AccessNetworkLayout extends LayoutAlgorithm {
37
38 private static final double COMPUTE_Y = -400.0;
39 private static final double SERVICE_Y = -200.0;
40 private static final double SPINE_Y = 0.0;
41 private static final double AGGREGATION_Y = +200.0;
42 private static final double ACCESS_Y = +400.0;
43 private static final double HOSTS_Y = +700.0;
44 private static final double GATEWAY_X = 900.0;
45
46 private static final int HOSTS_PER_ROW = 6;
Thomas Vachuska3fd1cee2018-04-25 19:07:06 -040047 private static final int COMPUTE_PER_ROW = 12;
48
Thomas Vachuska0d933862018-04-06 00:29:30 -070049 private static final double ROW_GAP = 70;
Thomas Vachuska3fd1cee2018-04-25 19:07:06 -040050 private static final double COMPUTE_ROW_GAP = -120;
51 private static final double COL_GAP = 54;
Thomas Vachuska0d933862018-04-06 00:29:30 -070052 private static final double COMPUTE_OFFSET = 400.0;
53 private static final double GATEWAY_GAP = 200.0;
54 private static final double GATEWAY_OFFSET = -200.0;
55
Thomas Vachuska3fd1cee2018-04-25 19:07:06 -040056 private int spine, aggregation, accessLeaf, serviceLeaf, gateway;
Thomas Vachuska0d933862018-04-06 00:29:30 -070057
58 @Override
59 protected boolean classify(Device device) {
60 if (!super.classify(device)) {
61 String role;
62
63 // Does the device have any hosts attached? If not, it's a spine
64 if (hostService.getConnectedHosts(device.id()).isEmpty()) {
65 // Does the device have any aggregate links to other devices?
66 Multiset<DeviceId> destinations = HashMultiset.create();
67 linkService.getDeviceEgressLinks(device.id()).stream()
68 .map(l -> l.dst().deviceId()).forEach(destinations::add);
69
70 // If yes, it's the main spine; otherwise it's an aggregate spine
71 role = destinations.entrySet().stream().anyMatch(e -> e.getCount() > 1) ?
72 SPINE : AGGREGATION;
73 } else {
74 // Does the device have any multi-home hosts attached?
75 // If yes, it's a service leaf; otherwise it's an access leaf
76 role = hostService.getConnectedHosts(device.id()).stream()
77 .map(Host::locations).anyMatch(s -> s.size() > 1) ?
78 LEAF : ACCESS;
79 }
80 deviceCategories.put(role, device.id());
81 }
82 return true;
83 }
84
85 @Override
86 protected boolean classify(Host host) {
87 if (!super.classify(host)) {
88 // Is the host attached to an access leaf?
89 // If so, it's an access host; otherwise it's a service host or gateway
90 String role = host.locations().stream().map(ConnectPoint::deviceId)
91 .anyMatch(d -> deviceCategories.get(ACCESS)
92 .contains(deviceService.getDevice(d).id())) ?
93 ACCESS : COMPUTE;
94 hostCategories.put(role, host.id());
95 }
96 return true;
97 }
98
99 @Override
100 public void apply() {
101 placeSpines();
102 placeServiceLeavesAndHosts();
103 placeAccessLeavesAndHosts();
104 }
105
106 private void placeSpines() {
107 spine = 1;
108 List<DeviceId> spines = deviceCategories.get(SPINE);
109 spines.stream().sorted(Comparators.ELEMENT_ID_COMPARATOR)
110 .forEach(d -> place(d, c(spine++, spines.size()), SPINE_Y));
111 }
112
113 private void placeServiceLeavesAndHosts() {
114 List<DeviceId> leaves = deviceCategories.get(LEAF);
115 List<HostId> computes = hostCategories.get(COMPUTE);
116 List<HostId> gateways = hostCategories.get(GATEWAY);
117 Set<HostId> placed = Sets.newHashSet();
118
119 serviceLeaf = 1;
120 leaves.stream().sorted(Comparators.ELEMENT_ID_COMPARATOR).forEach(id -> {
121 gateway = 1;
122 place(id, c(serviceLeaf++, leaves.size()), SERVICE_Y);
123
124 List<HostId> gwHosts = hostService.getConnectedHosts(id).stream()
125 .map(Host::id)
126 .filter(gateways::contains)
127 .filter(hid -> !placed.contains(hid))
128 .sorted(Comparators.ELEMENT_ID_COMPARATOR)
129 .collect(Collectors.toList());
130
131 gwHosts.forEach(hid -> {
132 place(hid, serviceLeaf <= 2 ? -GATEWAY_X : GATEWAY_X,
133 c(gateway++, gwHosts.size(), GATEWAY_GAP, GATEWAY_OFFSET));
134 placed.add(hid);
135 });
136
Thomas Vachuska0d933862018-04-06 00:29:30 -0700137 List<HostId> hosts = hostService.getConnectedHosts(id).stream()
138 .map(Host::id)
139 .filter(computes::contains)
140 .filter(hid -> !placed.contains(hid))
141 .sorted(Comparators.ELEMENT_ID_COMPARATOR)
142 .collect(Collectors.toList());
143
Thomas Vachuska3fd1cee2018-04-25 19:07:06 -0400144 placeHostBlock(hosts, serviceLeaf <= 2 ? -COMPUTE_OFFSET : COMPUTE_OFFSET,
145 COMPUTE_Y, COMPUTE_PER_ROW, COMPUTE_ROW_GAP,
146 serviceLeaf <= 2 ? -COL_GAP : COL_GAP);
147 placed.addAll(hosts);
Thomas Vachuska0d933862018-04-06 00:29:30 -0700148 });
149 }
150
151 private void placeAccessLeavesAndHosts() {
152 List<DeviceId> spines = deviceCategories.get(AGGREGATION);
153 List<DeviceId> leaves = deviceCategories.get(ACCESS);
154 Set<DeviceId> placed = Sets.newHashSet();
155
156 aggregation = 1;
157 accessLeaf = 1;
Thomas Vachuska834cc082018-04-24 22:39:22 -0400158 if (spines.isEmpty()) {
Thomas Vachuska3fd1cee2018-04-25 19:07:06 -0400159 leaves.stream().sorted(Comparators.ELEMENT_ID_COMPARATOR)
160 .forEach(lid -> placeAccessLeafAndHosts(lid, leaves.size(), placed));
Thomas Vachuska834cc082018-04-24 22:39:22 -0400161 } else {
162 spines.stream().sorted(Comparators.ELEMENT_ID_COMPARATOR).forEach(id -> {
163 place(id, c(aggregation++, spines.size()), AGGREGATION_Y);
164 linkService.getDeviceEgressLinks(id).stream()
165 .map(l -> l.dst().deviceId())
166 .filter(leaves::contains)
167 .filter(lid -> !placed.contains(lid))
168 .sorted(Comparators.ELEMENT_ID_COMPARATOR)
169 .forEach(lid -> placeAccessLeafAndHosts(lid, leaves.size(), placed));
170 });
171 }
172 }
173
174 private void placeAccessLeafAndHosts(DeviceId leafId, int leafCount, Set<DeviceId> placed) {
175 double x = c(accessLeaf++, leafCount);
176 place(leafId, x, ACCESS_Y);
177 placed.add(leafId);
178 placeHostBlock(hostService.getConnectedHosts(leafId).stream()
179 .map(Host::id)
180 .sorted(Comparators.ELEMENT_ID_COMPARATOR)
181 .collect(Collectors.toList()), x, HOSTS_Y,
182 HOSTS_PER_ROW, ROW_GAP, COL_GAP);
Thomas Vachuska0d933862018-04-06 00:29:30 -0700183 }
184
185}