blob: 1b5bb5091e02302f35284e26b08ba1c7ddb4eb33 [file] [log] [blame]
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -08001/*
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.grouphandler;
17
18import java.util.Arrays;
19import java.util.HashSet;
20import java.util.Set;
21
sangho32a59322015-02-17 12:07:41 -080022import org.onlab.packet.MplsLabel;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080023import org.onosproject.core.ApplicationId;
24import org.onosproject.net.DeviceId;
25import org.onosproject.net.Link;
26import org.onosproject.net.flow.DefaultTrafficTreatment;
27import org.onosproject.net.flow.TrafficTreatment;
28import org.onosproject.net.group.DefaultGroupBucket;
29import org.onosproject.net.group.GroupBucket;
30import org.onosproject.net.group.GroupBuckets;
31import org.onosproject.net.group.GroupService;
32import org.onosproject.net.link.LinkService;
33
34/**
35 * Default ECMP group handler creation module for a transit device.
36 * This component creates a set of ECMP groups for every neighbor
37 * that this device is connected to.
38 * For example, consider a network of 4 devices: D0 (Segment ID: 100),
39 * D1 (Segment ID: 101), D2 (Segment ID: 102) and D3 (Segment ID: 103),
40 * where D0 and D3 are edge devices and D1 and D2 are transit devices.
41 * Assume transit device D1 is connected to 2 neighbors (D0 and D3 ).
42 * The following groups will be created in D1:
43 * 1) all ports to D0 + with no label push,
44 * 2) all ports to D3 + with no label push,
45 */
46public class DefaultTransitGroupHandler extends DefaultGroupHandler {
47
48 protected DefaultTransitGroupHandler(DeviceId deviceId,
49 ApplicationId appId,
50 DeviceProperties config,
51 LinkService linkService,
52 GroupService groupService) {
53 super(deviceId, appId, config, linkService, groupService);
54 }
55
56 @Override
57 public void createGroups() {
58 Set<DeviceId> neighbors = devicePortMap.keySet();
59 if (neighbors == null || neighbors.isEmpty()) {
60 return;
61 }
62
63 // Create all possible Neighbor sets from this router
64 // NOTE: Avoid any pairings of edge routers only
65 Set<Set<DeviceId>> sets = getPowerSetOfNeighbors(neighbors);
66 sets = filterEdgeRouterOnlyPairings(sets);
67 log.debug("createGroupsAtTransitRouter: The size of neighbor powerset "
68 + "for sw {} is {}", deviceId, sets.size());
69 Set<NeighborSet> nsSet = new HashSet<NeighborSet>();
70 for (Set<DeviceId> combo : sets) {
71 if (combo.isEmpty()) {
72 continue;
73 }
74 NeighborSet ns = new NeighborSet(combo);
75 log.debug("createGroupsAtTransitRouter: sw {} combo {} ns {}",
76 deviceId, combo, ns);
77 nsSet.add(ns);
78 }
79 log.debug("createGroupsAtTransitRouter: The neighborset with label "
80 + "for sw {} is {}", deviceId, nsSet);
81
82 createGroupsFromNeighborsets(nsSet);
83 }
84
85 @Override
86 protected void newNeighbor(Link newNeighborLink) {
87 log.debug("New Neighbor: Updating groups for "
88 + "transit device {}", deviceId);
89 // Recompute neighbor power set
90 addNeighborAtPort(newNeighborLink.dst().deviceId(),
91 newNeighborLink.src().port());
92 // Compute new neighbor sets due to the addition of new neighbor
93 Set<NeighborSet> nsSet = computeImpactedNeighborsetForPortEvent(
94 newNeighborLink.dst().deviceId(),
95 devicePortMap.keySet());
96 createGroupsFromNeighborsets(nsSet);
97 }
98
99 @Override
100 protected void newPortToExistingNeighbor(Link newNeighborLink) {
101 log.debug("New port to existing neighbor: Updating "
102 + "groups for transit device {}", deviceId);
103 addNeighborAtPort(newNeighborLink.dst().deviceId(),
104 newNeighborLink.src().port());
105 Set<NeighborSet> nsSet = computeImpactedNeighborsetForPortEvent(
106 newNeighborLink.dst().deviceId(),
107 devicePortMap.keySet());
108 for (NeighborSet ns : nsSet) {
109 // Create the new bucket to be updated
110 TrafficTreatment.Builder tBuilder =
111 DefaultTrafficTreatment.builder();
112 tBuilder.setOutput(newNeighborLink.src().port())
113 .setEthDst(deviceConfig.getDeviceMac(
114 newNeighborLink.dst().deviceId()))
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700115 .setEthSrc(nodeMacAddr);
116 if (ns.getEdgeLabel() != NeighborSet.NO_EDGE_LABEL) {
117 tBuilder.pushMpls()
118 .setMpls(MplsLabel.
119 mplsLabel(ns.getEdgeLabel()));
120 }
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800121 GroupBucket updatedBucket = DefaultGroupBucket.
122 createSelectGroupBucket(tBuilder.build());
123 GroupBuckets updatedBuckets = new GroupBuckets(
124 Arrays.asList(updatedBucket));
125 log.debug("newPortToExistingNeighborAtEdgeRouter: "
126 + "groupService.addBucketsToGroup for neighborset{}", ns);
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700127 groupService.addBucketsToGroup(deviceId,
128 getGroupKey(ns),
129 updatedBuckets,
130 getGroupKey(ns),
131 appId);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800132 }
133 }
134
135 @Override
136 protected Set<NeighborSet> computeImpactedNeighborsetForPortEvent(
137 DeviceId impactedNeighbor,
138 Set<DeviceId> updatedNeighbors) {
139 Set<Set<DeviceId>> powerSet = getPowerSetOfNeighbors(updatedNeighbors);
140
141 Set<DeviceId> tmp = updatedNeighbors;
142 tmp.remove(impactedNeighbor);
143 Set<Set<DeviceId>> tmpPowerSet = getPowerSetOfNeighbors(tmp);
144
145 // Compute the impacted neighbor sets
146 powerSet.removeAll(tmpPowerSet);
147
148 powerSet = filterEdgeRouterOnlyPairings(powerSet);
149 Set<NeighborSet> nsSet = new HashSet<NeighborSet>();
150 for (Set<DeviceId> combo : powerSet) {
151 if (combo.isEmpty()) {
152 continue;
153 }
154 NeighborSet ns = new NeighborSet(combo);
155 log.debug("createGroupsAtTransitRouter: sw {} combo {} ns {}",
156 deviceId, combo, ns);
157 nsSet.add(ns);
158 }
159 log.debug("computeImpactedNeighborsetForPortEvent: The neighborset with label "
160 + "for sw {} is {}", deviceId, nsSet);
161
162 return nsSet;
163 }
164
165 private Set<Set<DeviceId>> filterEdgeRouterOnlyPairings(Set<Set<DeviceId>> sets) {
166 Set<Set<DeviceId>> fiteredSets = new HashSet<Set<DeviceId>>();
167 for (Set<DeviceId> deviceSubSet : sets) {
168 if (deviceSubSet.size() > 1) {
169 boolean avoidEdgeRouterPairing = true;
170 for (DeviceId device : deviceSubSet) {
171 if (!deviceConfig.isEdgeDevice(device)) {
172 avoidEdgeRouterPairing = false;
173 break;
174 }
175 }
176 if (!avoidEdgeRouterPairing) {
177 fiteredSets.add(deviceSubSet);
178 }
179 } else {
180 fiteredSets.add(deviceSubSet);
181 }
182 }
183 return fiteredSets;
184 }
185}