Brian O'Connor | 14796d2 | 2016-04-09 02:13:23 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0947d7e | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Brian O'Connor | 14796d2 | 2016-04-09 02:13:23 -0700 | [diff] [blame] | 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 | */ |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 16 | package org.onosproject.segmentrouting; |
| 17 | |
| 18 | import com.google.common.collect.ImmutableSet; |
Andrea Campanella | 60ce222 | 2018-04-30 11:48:55 +0200 | [diff] [blame] | 19 | import com.google.common.collect.Sets; |
| 20 | import org.onlab.packet.IpPrefix; |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 21 | import org.onlab.packet.MacAddress; |
| 22 | import org.onosproject.net.DeviceId; |
| 23 | import org.onosproject.net.PortNumber; |
| 24 | import org.onosproject.net.config.NetworkConfigEvent; |
| 25 | import org.onosproject.net.device.DeviceService; |
| 26 | import org.onosproject.net.flow.criteria.Criteria; |
| 27 | import org.onosproject.net.flowobjective.DefaultFilteringObjective; |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 28 | import org.onosproject.net.flowobjective.DefaultObjectiveContext; |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 29 | import org.onosproject.net.flowobjective.FilteringObjective; |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 30 | import org.onosproject.net.flowobjective.ObjectiveContext; |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 31 | import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException; |
| 32 | import org.onosproject.segmentrouting.config.SegmentRoutingAppConfig; |
| 33 | import org.slf4j.Logger; |
| 34 | import org.slf4j.LoggerFactory; |
| 35 | |
| 36 | import java.util.HashSet; |
| 37 | import java.util.Set; |
| 38 | |
| 39 | /** |
Charles Chan | 82f1997 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 40 | * Handles Segment Routing app config events. |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 41 | */ |
Charles Chan | 82f1997 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 42 | public class AppConfigHandler { |
| 43 | private static final Logger log = LoggerFactory.getLogger(AppConfigHandler.class); |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 44 | private final SegmentRoutingManager srManager; |
| 45 | private final DeviceService deviceService; |
| 46 | |
| 47 | /** |
Charles Chan | 82f1997 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 48 | * Constructs Segment Routing App Config Handler. |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 49 | * |
| 50 | * @param srManager instance of {@link SegmentRoutingManager} |
| 51 | */ |
Charles Chan | 82f1997 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 52 | public AppConfigHandler(SegmentRoutingManager srManager) { |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 53 | this.srManager = srManager; |
| 54 | this.deviceService = srManager.deviceService; |
| 55 | } |
| 56 | |
| 57 | /** |
Charles Chan | 82f1997 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 58 | * Processes Segment Routing App Config added event. |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 59 | * |
| 60 | * @param event network config added event |
| 61 | */ |
Charles Chan | 82f1997 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 62 | protected void processAppConfigAdded(NetworkConfigEvent event) { |
Pier Ventre | 7a78de2 | 2016-10-31 15:00:01 -0700 | [diff] [blame] | 63 | log.info("Processing AppConfig CONFIG_ADDED"); |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 64 | SegmentRoutingAppConfig config = (SegmentRoutingAppConfig) event.config().get(); |
| 65 | deviceService.getAvailableDevices().forEach(device -> { |
| 66 | populateVRouter(device.id(), getMacAddresses(config)); |
Andrea Campanella | 60ce222 | 2018-04-30 11:48:55 +0200 | [diff] [blame] | 67 | config.blackholeIPs().forEach(ipPrefix -> { |
| 68 | srManager.routingRulePopulator.populateDefaultRouteBlackhole(device.id(), ipPrefix); |
| 69 | }); |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 70 | }); |
| 71 | } |
| 72 | |
| 73 | /** |
Charles Chan | 82f1997 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 74 | * Processes Segment Routing App Config updated event. |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 75 | * |
| 76 | * @param event network config updated event |
| 77 | */ |
Charles Chan | 82f1997 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 78 | protected void processAppConfigUpdated(NetworkConfigEvent event) { |
Pier Ventre | 7a78de2 | 2016-10-31 15:00:01 -0700 | [diff] [blame] | 79 | log.info("Processing AppConfig CONFIG_UPDATED"); |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 80 | SegmentRoutingAppConfig config = (SegmentRoutingAppConfig) event.config().get(); |
| 81 | SegmentRoutingAppConfig prevConfig = (SegmentRoutingAppConfig) event.prevConfig().get(); |
| 82 | deviceService.getAvailableDevices().forEach(device -> { |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 83 | Set<MacAddress> macAddresses = new HashSet<>(getMacAddresses(config)); |
| 84 | Set<MacAddress> prevMacAddresses = new HashSet<>(getMacAddresses(prevConfig)); |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 85 | // Avoid removing and re-adding unchanged MAC addresses since |
| 86 | // FlowObjective does not guarantee the execution order. |
| 87 | Set<MacAddress> sameMacAddresses = new HashSet<>(macAddresses); |
| 88 | sameMacAddresses.retainAll(prevMacAddresses); |
| 89 | macAddresses.removeAll(sameMacAddresses); |
| 90 | prevMacAddresses.removeAll(sameMacAddresses); |
| 91 | |
| 92 | revokeVRouter(device.id(), prevMacAddresses); |
| 93 | populateVRouter(device.id(), macAddresses); |
Andrea Campanella | 60ce222 | 2018-04-30 11:48:55 +0200 | [diff] [blame] | 94 | Set<IpPrefix> toRemove = Sets.difference(prevConfig.blackholeIPs(), config.blackholeIPs()); |
| 95 | toRemove.forEach(ipPrefix -> { |
| 96 | srManager.routingRulePopulator.removeDefaultRouteBlackhole(device.id(), ipPrefix); |
| 97 | }); |
| 98 | Set<IpPrefix> toAdd = Sets.difference(config.blackholeIPs(), prevConfig.blackholeIPs()); |
| 99 | toAdd.forEach(ipPrefix -> { |
| 100 | srManager.routingRulePopulator.populateDefaultRouteBlackhole(device.id(), ipPrefix); |
| 101 | }); |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 102 | }); |
| 103 | |
| 104 | } |
| 105 | |
| 106 | /** |
Charles Chan | 82f1997 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 107 | * Processes Segment Routing App Config removed event. |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 108 | * |
| 109 | * @param event network config removed event |
| 110 | */ |
Charles Chan | 82f1997 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 111 | protected void processAppConfigRemoved(NetworkConfigEvent event) { |
Pier Ventre | 7a78de2 | 2016-10-31 15:00:01 -0700 | [diff] [blame] | 112 | log.info("Processing AppConfig CONFIG_REMOVED"); |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 113 | SegmentRoutingAppConfig prevConfig = (SegmentRoutingAppConfig) event.prevConfig().get(); |
| 114 | deviceService.getAvailableDevices().forEach(device -> { |
| 115 | revokeVRouter(device.id(), getMacAddresses(prevConfig)); |
Andrea Campanella | 60ce222 | 2018-04-30 11:48:55 +0200 | [diff] [blame] | 116 | prevConfig.blackholeIPs().forEach(ipPrefix -> { |
| 117 | srManager.routingRulePopulator.removeDefaultRouteBlackhole(device.id(), ipPrefix); |
| 118 | }); |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 119 | }); |
| 120 | } |
| 121 | |
| 122 | /** |
Andrea Campanella | 60ce222 | 2018-04-30 11:48:55 +0200 | [diff] [blame] | 123 | * Populates initial vRouter and blackhole rules. |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 124 | * |
| 125 | * @param deviceId device ID |
| 126 | */ |
Charles Chan | debfea3 | 2016-10-24 14:52:01 -0700 | [diff] [blame] | 127 | public void init(DeviceId deviceId) { |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 128 | SegmentRoutingAppConfig config = |
| 129 | srManager.cfgService.getConfig(srManager.appId, SegmentRoutingAppConfig.class); |
| 130 | populateVRouter(deviceId, getMacAddresses(config)); |
Andrea Campanella | 346c3d6 | 2018-05-10 19:18:48 +0200 | [diff] [blame] | 131 | if (config != null) { |
| 132 | config.blackholeIPs().forEach(ipPrefix -> { |
| 133 | srManager.routingRulePopulator.populateDefaultRouteBlackhole(deviceId, ipPrefix); |
| 134 | }); |
| 135 | } |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | private void populateVRouter(DeviceId deviceId, Set<MacAddress> pendingAdd) { |
| 139 | if (!isEdge(deviceId)) { |
| 140 | return; |
| 141 | } |
| 142 | getVRouterFlowObjBuilders(pendingAdd).forEach(foBuilder -> { |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 143 | ObjectiveContext context = new DefaultObjectiveContext( |
| 144 | (objective) -> log.debug("vRouterMac filter for {} populated", pendingAdd), |
| 145 | (objective, error) -> |
| 146 | log.warn("Failed to populate vRouterMac filter for {}: {}", pendingAdd, error)); |
| 147 | srManager.flowObjectiveService.filter(deviceId, foBuilder.add(context)); |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 148 | }); |
| 149 | } |
| 150 | |
| 151 | private void revokeVRouter(DeviceId deviceId, Set<MacAddress> pendingRemove) { |
| 152 | if (!isEdge(deviceId)) { |
| 153 | return; |
| 154 | } |
| 155 | getVRouterFlowObjBuilders(pendingRemove).forEach(foBuilder -> { |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 156 | ObjectiveContext context = new DefaultObjectiveContext( |
| 157 | (objective) -> log.debug("vRouterMac filter for {} revoked", pendingRemove), |
| 158 | (objective, error) -> |
| 159 | log.warn("Failed to revoke vRouterMac filter for {}: {}", pendingRemove, error)); |
| 160 | srManager.flowObjectiveService.filter(deviceId, foBuilder.remove(context)); |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 161 | }); |
| 162 | } |
| 163 | |
| 164 | private Set<FilteringObjective.Builder> getVRouterFlowObjBuilders(Set<MacAddress> macAddresses) { |
| 165 | ImmutableSet.Builder<FilteringObjective.Builder> setBuilder = ImmutableSet.builder(); |
| 166 | macAddresses.forEach(macAddress -> { |
| 167 | FilteringObjective.Builder fobuilder = DefaultFilteringObjective.builder(); |
| 168 | fobuilder.withKey(Criteria.matchInPort(PortNumber.ANY)) |
| 169 | .addCondition(Criteria.matchEthDst(macAddress)) |
| 170 | .permit() |
| 171 | .withPriority(SegmentRoutingService.DEFAULT_PRIORITY) |
| 172 | .fromApp(srManager.appId); |
| 173 | setBuilder.add(fobuilder); |
| 174 | }); |
| 175 | return setBuilder.build(); |
| 176 | } |
| 177 | |
| 178 | private Set<MacAddress> getMacAddresses(SegmentRoutingAppConfig config) { |
| 179 | if (config == null) { |
| 180 | return ImmutableSet.of(); |
| 181 | } |
Charles Chan | 1dd21a5 | 2016-03-07 00:49:33 -0800 | [diff] [blame] | 182 | return ImmutableSet.copyOf(config.vRouterMacs()); |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | private boolean isEdge(DeviceId deviceId) { |
| 186 | try { |
| 187 | if (srManager.deviceConfiguration.isEdgeDevice(deviceId)) { |
| 188 | return true; |
| 189 | } |
Andrea Campanella | 60ce222 | 2018-04-30 11:48:55 +0200 | [diff] [blame] | 190 | } catch (DeviceConfigNotFoundException e) { |
| 191 | log.warn("Device configuration for {} is not present.", deviceId); |
| 192 | } |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 193 | return false; |
| 194 | } |
| 195 | } |