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