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