Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.segmentrouting.grouphandler; |
| 17 | |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 18 | import java.util.HashSet; |
| 19 | import java.util.Set; |
| 20 | |
| 21 | import org.onlab.packet.MplsLabel; |
| 22 | import org.onosproject.core.ApplicationId; |
| 23 | import org.onosproject.net.DeviceId; |
| 24 | import org.onosproject.net.Link; |
| 25 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 26 | import org.onosproject.net.flow.TrafficTreatment; |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 27 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 28 | import org.onosproject.net.link.LinkService; |
| 29 | |
| 30 | /** |
| 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, |
| 41 | */ |
| 42 | public class DefaultTransitGroupHandler extends DefaultGroupHandler { |
| 43 | |
| 44 | protected DefaultTransitGroupHandler(DeviceId deviceId, |
| 45 | ApplicationId appId, |
| 46 | DeviceProperties config, |
| 47 | LinkService linkService, |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 48 | FlowObjectiveService flowObjService) { |
| 49 | super(deviceId, appId, config, linkService, flowObjService); |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | @Override |
| 53 | public void createGroups() { |
| 54 | Set<DeviceId> neighbors = devicePortMap.keySet(); |
| 55 | if (neighbors == null || neighbors.isEmpty()) { |
| 56 | return; |
| 57 | } |
| 58 | |
| 59 | // Create all possible Neighbor sets from this router |
| 60 | // NOTE: Avoid any pairings of edge routers only |
| 61 | Set<Set<DeviceId>> sets = getPowerSetOfNeighbors(neighbors); |
| 62 | sets = filterEdgeRouterOnlyPairings(sets); |
| 63 | log.debug("createGroupsAtTransitRouter: The size of neighbor powerset " |
| 64 | + "for sw {} is {}", deviceId, sets.size()); |
| 65 | Set<NeighborSet> nsSet = new HashSet<NeighborSet>(); |
| 66 | for (Set<DeviceId> combo : sets) { |
| 67 | if (combo.isEmpty()) { |
| 68 | continue; |
| 69 | } |
| 70 | NeighborSet ns = new NeighborSet(combo); |
| 71 | log.debug("createGroupsAtTransitRouter: sw {} combo {} ns {}", |
| 72 | deviceId, combo, ns); |
| 73 | nsSet.add(ns); |
| 74 | } |
| 75 | log.debug("createGroupsAtTransitRouter: The neighborset with label " |
| 76 | + "for sw {} is {}", deviceId, nsSet); |
| 77 | |
| 78 | createGroupsFromNeighborsets(nsSet); |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | protected void newNeighbor(Link newNeighborLink) { |
| 83 | log.debug("New Neighbor: Updating groups for " |
| 84 | + "transit device {}", deviceId); |
| 85 | // Recompute neighbor power set |
| 86 | addNeighborAtPort(newNeighborLink.dst().deviceId(), |
| 87 | newNeighborLink.src().port()); |
| 88 | // Compute new neighbor sets due to the addition of new neighbor |
| 89 | Set<NeighborSet> nsSet = computeImpactedNeighborsetForPortEvent( |
| 90 | newNeighborLink.dst().deviceId(), |
| 91 | devicePortMap.keySet()); |
| 92 | createGroupsFromNeighborsets(nsSet); |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | protected void newPortToExistingNeighbor(Link newNeighborLink) { |
| 97 | log.debug("New port to existing neighbor: Updating " |
| 98 | + "groups for transit device {}", deviceId); |
| 99 | addNeighborAtPort(newNeighborLink.dst().deviceId(), |
| 100 | newNeighborLink.src().port()); |
| 101 | Set<NeighborSet> nsSet = computeImpactedNeighborsetForPortEvent( |
| 102 | newNeighborLink.dst().deviceId(), |
| 103 | devicePortMap.keySet()); |
| 104 | for (NeighborSet ns : nsSet) { |
| 105 | // Create the new bucket to be updated |
| 106 | TrafficTreatment.Builder tBuilder = |
| 107 | DefaultTrafficTreatment.builder(); |
| 108 | tBuilder.setOutput(newNeighborLink.src().port()) |
| 109 | .setEthDst(deviceConfig.getDeviceMac( |
| 110 | newNeighborLink.dst().deviceId())) |
| 111 | .setEthSrc(nodeMacAddr); |
| 112 | if (ns.getEdgeLabel() != NeighborSet.NO_EDGE_LABEL) { |
| 113 | tBuilder.pushMpls() |
| 114 | .setMpls(MplsLabel. |
| 115 | mplsLabel(ns.getEdgeLabel())); |
| 116 | } |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 117 | /*GroupBucket updatedBucket = DefaultGroupBucket. |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 118 | createSelectGroupBucket(tBuilder.build()); |
| 119 | GroupBuckets updatedBuckets = new GroupBuckets( |
| 120 | Arrays.asList(updatedBucket)); |
| 121 | log.debug("newPortToExistingNeighborAtEdgeRouter: " |
| 122 | + "groupService.addBucketsToGroup for neighborset{}", ns); |
| 123 | groupService.addBucketsToGroup(deviceId, |
| 124 | getGroupKey(ns), |
| 125 | updatedBuckets, |
| 126 | getGroupKey(ns), |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 127 | appId);*/ |
| 128 | //TODO: Use nextObjective APIs to update the next objective |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | protected Set<NeighborSet> computeImpactedNeighborsetForPortEvent( |
| 134 | DeviceId impactedNeighbor, |
| 135 | Set<DeviceId> updatedNeighbors) { |
| 136 | Set<Set<DeviceId>> powerSet = getPowerSetOfNeighbors(updatedNeighbors); |
| 137 | |
| 138 | Set<DeviceId> tmp = new HashSet<DeviceId>(); |
| 139 | tmp.addAll(updatedNeighbors); |
| 140 | tmp.remove(impactedNeighbor); |
| 141 | Set<Set<DeviceId>> tmpPowerSet = getPowerSetOfNeighbors(tmp); |
| 142 | |
| 143 | // Compute the impacted neighbor sets |
| 144 | powerSet.removeAll(tmpPowerSet); |
| 145 | |
| 146 | powerSet = filterEdgeRouterOnlyPairings(powerSet); |
| 147 | Set<NeighborSet> nsSet = new HashSet<NeighborSet>(); |
| 148 | for (Set<DeviceId> combo : powerSet) { |
| 149 | if (combo.isEmpty()) { |
| 150 | continue; |
| 151 | } |
| 152 | NeighborSet ns = new NeighborSet(combo); |
| 153 | log.debug("createGroupsAtTransitRouter: sw {} combo {} ns {}", |
| 154 | deviceId, combo, ns); |
| 155 | nsSet.add(ns); |
| 156 | } |
| 157 | log.debug("computeImpactedNeighborsetForPortEvent: The neighborset with label " |
| 158 | + "for sw {} is {}", deviceId, nsSet); |
| 159 | |
| 160 | return nsSet; |
| 161 | } |
| 162 | |
| 163 | private Set<Set<DeviceId>> filterEdgeRouterOnlyPairings(Set<Set<DeviceId>> sets) { |
| 164 | Set<Set<DeviceId>> fiteredSets = new HashSet<Set<DeviceId>>(); |
| 165 | for (Set<DeviceId> deviceSubSet : sets) { |
| 166 | if (deviceSubSet.size() > 1) { |
| 167 | boolean avoidEdgeRouterPairing = true; |
| 168 | for (DeviceId device : deviceSubSet) { |
| 169 | if (!deviceConfig.isEdgeDevice(device)) { |
| 170 | avoidEdgeRouterPairing = false; |
| 171 | break; |
| 172 | } |
| 173 | } |
| 174 | if (!avoidEdgeRouterPairing) { |
| 175 | fiteredSets.add(deviceSubSet); |
| 176 | } |
| 177 | } else { |
| 178 | fiteredSets.add(deviceSubSet); |
| 179 | } |
| 180 | } |
| 181 | return fiteredSets; |
| 182 | } |
| 183 | } |