Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [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.pim.impl; |
| 17 | |
Jonathan Hart | 5af5f14 | 2016-01-28 18:45:27 -0800 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableSet; |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 19 | import com.google.common.collect.Maps; |
| 20 | import org.apache.felix.scr.annotations.Activate; |
| 21 | import org.apache.felix.scr.annotations.Component; |
| 22 | import org.apache.felix.scr.annotations.Deactivate; |
| 23 | import org.apache.felix.scr.annotations.Reference; |
| 24 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 25 | import org.apache.felix.scr.annotations.Service; |
Jonathan Hart | 54119bb | 2016-02-06 18:48:27 -0800 | [diff] [blame] | 26 | import org.onlab.util.SafeRecurringTask; |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 27 | import org.onosproject.incubator.net.intf.Interface; |
Jonathan Hart | 5af5f14 | 2016-01-28 18:45:27 -0800 | [diff] [blame] | 28 | import org.onosproject.incubator.net.intf.InterfaceEvent; |
| 29 | import org.onosproject.incubator.net.intf.InterfaceListener; |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 30 | import org.onosproject.incubator.net.intf.InterfaceService; |
| 31 | import org.onosproject.net.ConnectPoint; |
Jonathan Hart | 5af5f14 | 2016-01-28 18:45:27 -0800 | [diff] [blame] | 32 | import org.onosproject.net.config.ConfigFactory; |
| 33 | import org.onosproject.net.config.NetworkConfigEvent; |
| 34 | import org.onosproject.net.config.NetworkConfigListener; |
| 35 | import org.onosproject.net.config.NetworkConfigRegistry; |
| 36 | import org.onosproject.net.config.basics.SubjectFactories; |
Jonathan Hart | 36fd31e | 2016-01-28 15:55:31 -0800 | [diff] [blame] | 37 | import org.onosproject.net.packet.PacketService; |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 38 | import org.slf4j.Logger; |
Jonathan Hart | 5af5f14 | 2016-01-28 18:45:27 -0800 | [diff] [blame] | 39 | |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 40 | import java.util.Map; |
Jonathan Hart | 5af5f14 | 2016-01-28 18:45:27 -0800 | [diff] [blame] | 41 | import java.util.Set; |
Rusty Eddy | 4d5a92f | 2016-01-25 17:12:14 -0800 | [diff] [blame] | 42 | import java.util.concurrent.Executors; |
| 43 | import java.util.concurrent.ScheduledExecutorService; |
| 44 | import java.util.concurrent.TimeUnit; |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 45 | |
| 46 | import static org.slf4j.LoggerFactory.getLogger; |
| 47 | |
| 48 | /** |
| 49 | * Manages PIMInterfaces. |
| 50 | * |
| 51 | * TODO: Do we need to add a ServiceListener? |
| 52 | */ |
| 53 | @Component(immediate = true) |
| 54 | @Service |
| 55 | public class PIMInterfaceManager implements PIMInterfaceService { |
| 56 | |
| 57 | private final Logger log = getLogger(getClass()); |
| 58 | |
Jonathan Hart | 5af5f14 | 2016-01-28 18:45:27 -0800 | [diff] [blame] | 59 | private static final Class<PimInterfaceConfig> PIM_INTERFACE_CONFIG_CLASS = PimInterfaceConfig.class; |
| 60 | private static final String PIM_INTERFACE_CONFIG_KEY = "pimInterface"; |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 61 | |
Jonathan Hart | 6be7095 | 2016-02-12 21:11:26 -0800 | [diff] [blame] | 62 | public static final int DEFAULT_HELLO_INTERVAL = 30; // seconds |
| 63 | |
| 64 | private static final int DEFAULT_TASK_PERIOD_MS = 250; |
Jonathan Hart | 54119bb | 2016-02-06 18:48:27 -0800 | [diff] [blame] | 65 | |
| 66 | // Create a Scheduled Executor service for recurring tasks |
| 67 | private final ScheduledExecutorService scheduledExecutorService = |
Rusty Eddy | 4d5a92f | 2016-01-25 17:12:14 -0800 | [diff] [blame] | 68 | Executors.newScheduledThreadPool(1); |
| 69 | |
Jonathan Hart | 6be7095 | 2016-02-12 21:11:26 -0800 | [diff] [blame] | 70 | private final long initialHelloDelay = 1000; |
Rusty Eddy | 4d5a92f | 2016-01-25 17:12:14 -0800 | [diff] [blame] | 71 | |
Jonathan Hart | 6be7095 | 2016-02-12 21:11:26 -0800 | [diff] [blame] | 72 | private final long pimHelloPeriod = DEFAULT_TASK_PERIOD_MS; |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 73 | |
Jonathan Hart | 6be7095 | 2016-02-12 21:11:26 -0800 | [diff] [blame] | 74 | private final int timeoutTaskPeriod = DEFAULT_TASK_PERIOD_MS; |
Jonathan Hart | 54119bb | 2016-02-06 18:48:27 -0800 | [diff] [blame] | 75 | |
Jonathan Hart | 36fd31e | 2016-01-28 15:55:31 -0800 | [diff] [blame] | 76 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 77 | protected PacketService packetService; |
| 78 | |
Jonathan Hart | 5af5f14 | 2016-01-28 18:45:27 -0800 | [diff] [blame] | 79 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 80 | protected NetworkConfigRegistry networkConfig; |
| 81 | |
| 82 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 83 | protected InterfaceService interfaceService; |
| 84 | |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 85 | // Store PIM Interfaces in a map key'd by ConnectPoint |
| 86 | private final Map<ConnectPoint, PIMInterface> pimInterfaces = Maps.newConcurrentMap(); |
| 87 | |
Jonathan Hart | 5af5f14 | 2016-01-28 18:45:27 -0800 | [diff] [blame] | 88 | private final InternalNetworkConfigListener configListener = |
| 89 | new InternalNetworkConfigListener(); |
| 90 | private final InternalInterfaceListener interfaceListener = |
| 91 | new InternalInterfaceListener(); |
| 92 | |
| 93 | private final ConfigFactory<ConnectPoint, PimInterfaceConfig> pimConfigFactory |
| 94 | = new ConfigFactory<ConnectPoint, PimInterfaceConfig>( |
| 95 | SubjectFactories.CONNECT_POINT_SUBJECT_FACTORY, PIM_INTERFACE_CONFIG_CLASS, |
| 96 | PIM_INTERFACE_CONFIG_KEY) { |
| 97 | |
| 98 | @Override |
| 99 | public PimInterfaceConfig createConfig() { |
| 100 | return new PimInterfaceConfig(); |
| 101 | } |
| 102 | }; |
| 103 | |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 104 | @Activate |
| 105 | public void activate() { |
Jonathan Hart | 5af5f14 | 2016-01-28 18:45:27 -0800 | [diff] [blame] | 106 | networkConfig.registerConfigFactory(pimConfigFactory); |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 107 | |
Jonathan Hart | 5af5f14 | 2016-01-28 18:45:27 -0800 | [diff] [blame] | 108 | // Create PIM Interfaces for each of the existing configured interfaces. |
| 109 | Set<ConnectPoint> subjects = networkConfig.getSubjects( |
| 110 | ConnectPoint.class, PIM_INTERFACE_CONFIG_CLASS); |
| 111 | for (ConnectPoint cp : subjects) { |
| 112 | PimInterfaceConfig config = networkConfig.getConfig(cp, PIM_INTERFACE_CONFIG_CLASS); |
| 113 | updateInterface(config); |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 114 | } |
Rusty Eddy | 4d5a92f | 2016-01-25 17:12:14 -0800 | [diff] [blame] | 115 | |
Jonathan Hart | 5af5f14 | 2016-01-28 18:45:27 -0800 | [diff] [blame] | 116 | networkConfig.addListener(configListener); |
| 117 | interfaceService.addListener(interfaceListener); |
| 118 | |
Rusty Eddy | 4d5a92f | 2016-01-25 17:12:14 -0800 | [diff] [blame] | 119 | // Schedule the periodic hello sender. |
Jonathan Hart | 54119bb | 2016-02-06 18:48:27 -0800 | [diff] [blame] | 120 | scheduledExecutorService.scheduleAtFixedRate( |
| 121 | SafeRecurringTask.wrap( |
| 122 | () -> pimInterfaces.values().forEach(PIMInterface::sendHello)), |
Jonathan Hart | 6be7095 | 2016-02-12 21:11:26 -0800 | [diff] [blame] | 123 | initialHelloDelay, pimHelloPeriod, TimeUnit.MILLISECONDS); |
Jonathan Hart | 54119bb | 2016-02-06 18:48:27 -0800 | [diff] [blame] | 124 | |
| 125 | // Schedule task to periodically time out expired neighbors |
| 126 | scheduledExecutorService.scheduleAtFixedRate( |
| 127 | SafeRecurringTask.wrap( |
| 128 | () -> pimInterfaces.values().forEach(PIMInterface::checkNeighborTimeouts)), |
| 129 | 0, timeoutTaskPeriod, TimeUnit.MILLISECONDS); |
Jonathan Hart | 5af5f14 | 2016-01-28 18:45:27 -0800 | [diff] [blame] | 130 | |
| 131 | log.info("Started"); |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | @Deactivate |
| 135 | public void deactivate() { |
Jonathan Hart | 5af5f14 | 2016-01-28 18:45:27 -0800 | [diff] [blame] | 136 | interfaceService.removeListener(interfaceListener); |
| 137 | networkConfig.removeListener(configListener); |
| 138 | networkConfig.unregisterConfigFactory(pimConfigFactory); |
Rusty Eddy | 4d5a92f | 2016-01-25 17:12:14 -0800 | [diff] [blame] | 139 | |
| 140 | // Shutdown the periodic hello task. |
Jonathan Hart | 54119bb | 2016-02-06 18:48:27 -0800 | [diff] [blame] | 141 | scheduledExecutorService.shutdown(); |
Rusty Eddy | 4d5a92f | 2016-01-25 17:12:14 -0800 | [diff] [blame] | 142 | |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 143 | log.info("Stopped"); |
| 144 | } |
| 145 | |
| 146 | /** |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 147 | * Return the PIMInterface that corresponds to the given ConnectPoint. |
| 148 | * |
| 149 | * @param cp The ConnectPoint we want to get the PIMInterface for |
| 150 | * @return The PIMInterface if it exists, NULL if it does not exist. |
| 151 | */ |
| 152 | @Override |
| 153 | public PIMInterface getPIMInterface(ConnectPoint cp) { |
| 154 | PIMInterface pi = pimInterfaces.getOrDefault(cp, null); |
| 155 | if (pi == null) { |
| 156 | log.warn("We have been asked for an Interface we don't have: " + cp.toString()); |
| 157 | } |
| 158 | return pi; |
| 159 | } |
Jonathan Hart | 5af5f14 | 2016-01-28 18:45:27 -0800 | [diff] [blame] | 160 | |
| 161 | @Override |
| 162 | public Set<PIMInterface> getPimInterfaces() { |
| 163 | return ImmutableSet.copyOf(pimInterfaces.values()); |
| 164 | } |
| 165 | |
| 166 | private void updateInterface(PimInterfaceConfig config) { |
| 167 | ConnectPoint cp = config.subject(); |
| 168 | |
| 169 | if (!config.isEnabled()) { |
| 170 | removeInterface(cp); |
| 171 | return; |
| 172 | } |
| 173 | |
| 174 | String intfName = config.getInterfaceName(); |
| 175 | Interface intf = interfaceService.getInterfaceByName(cp, intfName); |
| 176 | |
| 177 | if (intf == null) { |
| 178 | log.debug("Interface configuration missing: {}", config.getInterfaceName()); |
| 179 | return; |
| 180 | } |
| 181 | |
| 182 | |
| 183 | log.debug("Updating Interface for " + intf.connectPoint().toString()); |
| 184 | pimInterfaces.computeIfAbsent(cp, k -> buildPimInterface(config, intf)); |
| 185 | } |
| 186 | |
| 187 | private void removeInterface(ConnectPoint cp) { |
| 188 | pimInterfaces.remove(cp); |
| 189 | } |
| 190 | |
| 191 | private PIMInterface buildPimInterface(PimInterfaceConfig config, Interface intf) { |
| 192 | PIMInterface.Builder builder = PIMInterface.builder() |
| 193 | .withPacketService(packetService) |
| 194 | .withInterface(intf); |
| 195 | |
Sho SHIMIZU | ef7e290 | 2016-02-12 18:38:29 -0800 | [diff] [blame] | 196 | config.getHelloInterval().ifPresent(builder::withHelloInterval); |
| 197 | config.getHoldTime().ifPresent(builder::withHoldTime); |
| 198 | config.getPriority().ifPresent(builder::withPriority); |
| 199 | config.getPropagationDelay().ifPresent(builder::withPropagationDelay); |
| 200 | config.getOverrideInterval().ifPresent(builder::withOverrideInterval); |
Jonathan Hart | 5af5f14 | 2016-01-28 18:45:27 -0800 | [diff] [blame] | 201 | |
| 202 | return builder.build(); |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Listener for network config events. |
| 207 | */ |
| 208 | private class InternalNetworkConfigListener implements NetworkConfigListener { |
| 209 | |
| 210 | @Override |
| 211 | public void event(NetworkConfigEvent event) { |
| 212 | if (event.configClass() != PIM_INTERFACE_CONFIG_CLASS) { |
| 213 | return; |
| 214 | } |
| 215 | |
| 216 | switch (event.type()) { |
| 217 | case CONFIG_REGISTERED: |
| 218 | case CONFIG_UNREGISTERED: |
| 219 | break; |
| 220 | case CONFIG_ADDED: |
| 221 | case CONFIG_UPDATED: |
| 222 | ConnectPoint cp = (ConnectPoint) event.subject(); |
| 223 | PimInterfaceConfig config = networkConfig.getConfig( |
| 224 | cp, PIM_INTERFACE_CONFIG_CLASS); |
| 225 | |
| 226 | updateInterface(config); |
| 227 | break; |
| 228 | case CONFIG_REMOVED: |
| 229 | removeInterface((ConnectPoint) event.subject()); |
| 230 | break; |
| 231 | default: |
| 232 | break; |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Listener for interface events. |
| 239 | */ |
| 240 | private class InternalInterfaceListener implements InterfaceListener { |
| 241 | |
| 242 | @Override |
| 243 | public void event(InterfaceEvent event) { |
| 244 | switch (event.type()) { |
| 245 | case INTERFACE_ADDED: |
| 246 | PimInterfaceConfig config = networkConfig.getConfig( |
| 247 | event.subject().connectPoint(), PIM_INTERFACE_CONFIG_CLASS); |
| 248 | |
| 249 | if (config != null) { |
| 250 | updateInterface(config); |
| 251 | } |
| 252 | break; |
| 253 | case INTERFACE_UPDATED: |
| 254 | break; |
| 255 | case INTERFACE_REMOVED: |
| 256 | removeInterface(event.subject().connectPoint()); |
| 257 | break; |
| 258 | default: |
| 259 | break; |
| 260 | |
| 261 | } |
| 262 | } |
| 263 | } |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 264 | } |