blob: e053622114b0a05e442687b8928b7d849dd67f61 [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 */
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -070016package org.onosproject.segmentrouting.grouphandler;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080017
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
sanghob35a6192015-04-01 13:05:26 -0700141 Set<DeviceId> tmp = new HashSet<DeviceId>();
142 tmp.addAll(updatedNeighbors);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800143 tmp.remove(impactedNeighbor);
144 Set<Set<DeviceId>> tmpPowerSet = getPowerSetOfNeighbors(tmp);
145
146 // Compute the impacted neighbor sets
147 powerSet.removeAll(tmpPowerSet);
148
149 powerSet = filterEdgeRouterOnlyPairings(powerSet);
150 Set<NeighborSet> nsSet = new HashSet<NeighborSet>();
151 for (Set<DeviceId> combo : powerSet) {
152 if (combo.isEmpty()) {
153 continue;
154 }
155 NeighborSet ns = new NeighborSet(combo);
156 log.debug("createGroupsAtTransitRouter: sw {} combo {} ns {}",
157 deviceId, combo, ns);
158 nsSet.add(ns);
159 }
160 log.debug("computeImpactedNeighborsetForPortEvent: The neighborset with label "
161 + "for sw {} is {}", deviceId, nsSet);
162
163 return nsSet;
164 }
165
166 private Set<Set<DeviceId>> filterEdgeRouterOnlyPairings(Set<Set<DeviceId>> sets) {
167 Set<Set<DeviceId>> fiteredSets = new HashSet<Set<DeviceId>>();
168 for (Set<DeviceId> deviceSubSet : sets) {
169 if (deviceSubSet.size() > 1) {
170 boolean avoidEdgeRouterPairing = true;
171 for (DeviceId device : deviceSubSet) {
172 if (!deviceConfig.isEdgeDevice(device)) {
173 avoidEdgeRouterPairing = false;
174 break;
175 }
176 }
177 if (!avoidEdgeRouterPairing) {
178 fiteredSets.add(deviceSubSet);
179 }
180 } else {
181 fiteredSets.add(deviceSubSet);
182 }
183 }
184 return fiteredSets;
185 }
186}