3cb73aba8cf23f5ca67fd15b857ceab37797cf6d
[onosfw.git] /
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
18 import java.util.HashSet;
19 import java.util.Set;
20
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.store.service.EventuallyConsistentMap;
27
28 /**
29  * Default ECMP group handler creation module for a transit device.
30  * This component creates a set of ECMP groups for every neighbor
31  * that this device is connected to.
32  * For example, consider a network of 4 devices: D0 (Segment ID: 100),
33  * D1 (Segment ID: 101), D2 (Segment ID: 102) and D3 (Segment ID: 103),
34  * where D0 and D3 are edge devices and D1 and D2 are transit devices.
35  * Assume transit device D1 is connected to 2 neighbors (D0 and D3 ).
36  * The following groups will be created in D1:
37  * 1) all ports to D0 + with no label push,
38  * 2) all ports to D3 + with no label push,
39  */
40 public class DefaultTransitGroupHandler extends DefaultGroupHandler {
41
42     protected DefaultTransitGroupHandler(DeviceId deviceId,
43                                   ApplicationId appId,
44                                   DeviceProperties config,
45                                   LinkService linkService,
46                                   FlowObjectiveService flowObjService,
47                                   EventuallyConsistentMap<
48                                   NeighborSetNextObjectiveStoreKey,
49                                   Integer> nsNextObjStore) {
50         super(deviceId, appId, config, linkService, flowObjService, nsNextObjStore);
51     }
52
53     @Override
54     public void createGroups() {
55         Set<DeviceId> neighbors = devicePortMap.keySet();
56         if (neighbors == null || neighbors.isEmpty()) {
57             return;
58         }
59
60         // Create all possible Neighbor sets from this router
61         // NOTE: Avoid any pairings of edge routers only
62         Set<Set<DeviceId>> sets = getPowerSetOfNeighbors(neighbors);
63         sets = filterEdgeRouterOnlyPairings(sets);
64         log.debug("createGroupsAtTransitRouter: The size of neighbor powerset "
65                 + "for sw {} is {}", deviceId, sets.size());
66         Set<NeighborSet> nsSet = new HashSet<>();
67         for (Set<DeviceId> combo : sets) {
68             if (combo.isEmpty()) {
69                 continue;
70             }
71             NeighborSet ns = new NeighborSet(combo);
72             log.debug("createGroupsAtTransitRouter: sw {} combo {} ns {}",
73                       deviceId, combo, ns);
74             nsSet.add(ns);
75         }
76         log.debug("createGroupsAtTransitRouter: The neighborset with label "
77                 + "for sw {} is {}", deviceId, nsSet);
78
79         createGroupsFromNeighborsets(nsSet);
80     }
81
82     @Override
83     protected void newNeighbor(Link newNeighborLink) {
84         log.debug("New Neighbor: Updating groups for "
85                 + "transit device {}", deviceId);
86         // Recompute neighbor power set
87         addNeighborAtPort(newNeighborLink.dst().deviceId(),
88                           newNeighborLink.src().port());
89         // Compute new neighbor sets due to the addition of new neighbor
90         Set<NeighborSet> nsSet = computeImpactedNeighborsetForPortEvent(
91                                              newNeighborLink.dst().deviceId(),
92                                              devicePortMap.keySet());
93         createGroupsFromNeighborsets(nsSet);
94     }
95
96     @Override
97     protected void newPortToExistingNeighbor(Link newNeighborLink) {
98         /*log.debug("New port to existing neighbor: Updating "
99                 + "groups for transit device {}", deviceId);
100         addNeighborAtPort(newNeighborLink.dst().deviceId(),
101                           newNeighborLink.src().port());
102         Set<NeighborSet> nsSet = computeImpactedNeighborsetForPortEvent(
103                                               newNeighborLink.dst().deviceId(),
104                                               devicePortMap.keySet());
105         for (NeighborSet ns : nsSet) {
106             // Create the new bucket to be updated
107             TrafficTreatment.Builder tBuilder =
108                     DefaultTrafficTreatment.builder();
109             tBuilder.setOutput(newNeighborLink.src().port())
110                     .setEthDst(deviceConfig.getDeviceMac(
111                           newNeighborLink.dst().deviceId()))
112                     .setEthSrc(nodeMacAddr);
113             if (ns.getEdgeLabel() != NeighborSet.NO_EDGE_LABEL) {
114                 tBuilder.pushMpls()
115                         .setMpls(MplsLabel.
116                                  mplsLabel(ns.getEdgeLabel()));
117             }
118
119
120             Integer nextId = deviceNextObjectiveIds.get(ns);
121             if (nextId != null) {
122                 NextObjective.Builder nextObjBuilder = DefaultNextObjective
123                         .builder().withId(nextId)
124                         .withType(NextObjective.Type.HASHED).fromApp(appId);
125
126                 nextObjBuilder.addTreatment(tBuilder.build());
127
128                 NextObjective nextObjective = nextObjBuilder.add();
129                 flowObjectiveService.next(deviceId, nextObjective);
130             }
131         }*/
132     }
133
134     @Override
135     protected Set<NeighborSet> computeImpactedNeighborsetForPortEvent(
136                                             DeviceId impactedNeighbor,
137                                             Set<DeviceId> updatedNeighbors) {
138         Set<Set<DeviceId>> powerSet = getPowerSetOfNeighbors(updatedNeighbors);
139
140         Set<DeviceId> tmp = new HashSet<>();
141         tmp.addAll(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<>();
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<>();
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 }