2 * Copyright 2015 Open Networking Laboratory
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onosproject.segmentrouting.grouphandler;
18 import java.util.HashSet;
21 import org.onosproject.core.ApplicationId;
22 import org.onosproject.net.DeviceId;
23 import org.onosproject.net.Link;
24 import org.onosproject.net.flowobjective.FlowObjectiveService;
25 import org.onosproject.net.link.LinkService;
26 import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
27 import org.onosproject.segmentrouting.config.DeviceProperties;
28 import org.onosproject.store.service.EventuallyConsistentMap;
31 * Default ECMP group handler creation module for a transit device.
32 * This component creates a set of ECMP groups for every neighbor
33 * that this device is connected to.
34 * For example, consider a network of 4 devices: D0 (Segment ID: 100),
35 * D1 (Segment ID: 101), D2 (Segment ID: 102) and D3 (Segment ID: 103),
36 * where D0 and D3 are edge devices and D1 and D2 are transit devices.
37 * Assume transit device D1 is connected to 2 neighbors (D0 and D3 ).
38 * The following groups will be created in D1:
39 * 1) all ports to D0 + with no label push,
40 * 2) all ports to D3 + with no label push,
42 public class DefaultTransitGroupHandler extends DefaultGroupHandler {
44 protected DefaultTransitGroupHandler(DeviceId deviceId,
46 DeviceProperties config,
47 LinkService linkService,
48 FlowObjectiveService flowObjService,
49 EventuallyConsistentMap<
50 NeighborSetNextObjectiveStoreKey,
51 Integer> nsNextObjStore,
52 EventuallyConsistentMap<SubnetNextObjectiveStoreKey,
53 Integer> subnetNextObjStore) {
54 super(deviceId, appId, config, linkService, flowObjService,
55 nsNextObjStore, subnetNextObjStore);
59 public void createGroups() {
60 Set<DeviceId> neighbors = devicePortMap.keySet();
61 if (neighbors == null || neighbors.isEmpty()) {
65 // Create all possible Neighbor sets from this router
66 // NOTE: Avoid any pairings of edge routers only
67 Set<Set<DeviceId>> sets = getPowerSetOfNeighbors(neighbors);
68 sets = filterEdgeRouterOnlyPairings(sets);
69 log.debug("createGroupsAtTransitRouter: The size of neighbor powerset "
70 + "for sw {} is {}", deviceId, sets.size());
71 Set<NeighborSet> nsSet = new HashSet<>();
72 for (Set<DeviceId> combo : sets) {
73 if (combo.isEmpty()) {
76 NeighborSet ns = new NeighborSet(combo);
77 log.debug("createGroupsAtTransitRouter: sw {} combo {} ns {}",
81 log.debug("createGroupsAtTransitRouter: The neighborset with label "
82 + "for sw {} is {}", deviceId, nsSet);
84 //createGroupsFromNeighborsets(nsSet);
88 protected void newNeighbor(Link newNeighborLink) {
89 log.debug("New Neighbor: Updating groups for "
90 + "transit device {}", deviceId);
91 // Recompute neighbor power set
92 addNeighborAtPort(newNeighborLink.dst().deviceId(),
93 newNeighborLink.src().port());
94 // Compute new neighbor sets due to the addition of new neighbor
95 Set<NeighborSet> nsSet = computeImpactedNeighborsetForPortEvent(
96 newNeighborLink.dst().deviceId(),
97 devicePortMap.keySet());
98 //createGroupsFromNeighborsets(nsSet);
102 protected void newPortToExistingNeighbor(Link newNeighborLink) {
103 /*log.debug("New port to existing neighbor: Updating "
104 + "groups for transit device {}", deviceId);
105 addNeighborAtPort(newNeighborLink.dst().deviceId(),
106 newNeighborLink.src().port());
107 Set<NeighborSet> nsSet = computeImpactedNeighborsetForPortEvent(
108 newNeighborLink.dst().deviceId(),
109 devicePortMap.keySet());
110 for (NeighborSet ns : nsSet) {
111 // Create the new bucket to be updated
112 TrafficTreatment.Builder tBuilder =
113 DefaultTrafficTreatment.builder();
114 tBuilder.setOutput(newNeighborLink.src().port())
115 .setEthDst(deviceConfig.getDeviceMac(
116 newNeighborLink.dst().deviceId()))
117 .setEthSrc(nodeMacAddr);
118 if (ns.getEdgeLabel() != NeighborSet.NO_EDGE_LABEL) {
121 mplsLabel(ns.getEdgeLabel()));
125 Integer nextId = deviceNextObjectiveIds.get(ns);
126 if (nextId != null) {
127 NextObjective.Builder nextObjBuilder = DefaultNextObjective
128 .builder().withId(nextId)
129 .withType(NextObjective.Type.HASHED).fromApp(appId);
131 nextObjBuilder.addTreatment(tBuilder.build());
133 NextObjective nextObjective = nextObjBuilder.add();
134 flowObjectiveService.next(deviceId, nextObjective);
140 protected Set<NeighborSet> computeImpactedNeighborsetForPortEvent(
141 DeviceId impactedNeighbor,
142 Set<DeviceId> updatedNeighbors) {
143 Set<Set<DeviceId>> powerSet = getPowerSetOfNeighbors(updatedNeighbors);
145 Set<DeviceId> tmp = new HashSet<>();
146 tmp.addAll(updatedNeighbors);
147 tmp.remove(impactedNeighbor);
148 Set<Set<DeviceId>> tmpPowerSet = getPowerSetOfNeighbors(tmp);
150 // Compute the impacted neighbor sets
151 powerSet.removeAll(tmpPowerSet);
153 powerSet = filterEdgeRouterOnlyPairings(powerSet);
154 Set<NeighborSet> nsSet = new HashSet<>();
155 for (Set<DeviceId> combo : powerSet) {
156 if (combo.isEmpty()) {
159 NeighborSet ns = new NeighborSet(combo);
160 log.debug("createGroupsAtTransitRouter: sw {} combo {} ns {}",
161 deviceId, combo, ns);
164 log.debug("computeImpactedNeighborsetForPortEvent: The neighborset with label "
165 + "for sw {} is {}", deviceId, nsSet);
170 private Set<Set<DeviceId>> filterEdgeRouterOnlyPairings(Set<Set<DeviceId>> sets) {
171 Set<Set<DeviceId>> fiteredSets = new HashSet<>();
172 for (Set<DeviceId> deviceSubSet : sets) {
173 if (deviceSubSet.size() > 1) {
174 boolean avoidEdgeRouterPairing = true;
175 for (DeviceId device : deviceSubSet) {
178 isEdge = deviceConfig.isEdgeDevice(device);
179 } catch (DeviceConfigNotFoundException e) {
180 log.warn(e.getMessage() + " Skipping filterEdgeRouterOnlyPairings on this device.");
185 avoidEdgeRouterPairing = false;
189 if (!avoidEdgeRouterPairing) {
190 fiteredSets.add(deviceSubSet);
193 fiteredSets.add(deviceSubSet);