blob: d6205e56ff907d28e22db7d0c36da61f5c9de6db [file] [log] [blame]
Rusty Eddy4ae5aa82015-12-15 12:58:27 -08001/*
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 */
16package org.onosproject.pim.impl;
17
Jonathan Hart5af5f142016-01-28 18:45:27 -080018import com.google.common.collect.ImmutableSet;
Rusty Eddy4ae5aa82015-12-15 12:58:27 -080019import com.google.common.collect.Maps;
20import org.apache.felix.scr.annotations.Activate;
21import org.apache.felix.scr.annotations.Component;
22import org.apache.felix.scr.annotations.Deactivate;
23import org.apache.felix.scr.annotations.Reference;
24import org.apache.felix.scr.annotations.ReferenceCardinality;
25import org.apache.felix.scr.annotations.Service;
Jonathan Hart54119bb2016-02-06 18:48:27 -080026import org.onlab.util.SafeRecurringTask;
Rusty Eddy4ae5aa82015-12-15 12:58:27 -080027import org.onosproject.incubator.net.intf.Interface;
Jonathan Hart5af5f142016-01-28 18:45:27 -080028import org.onosproject.incubator.net.intf.InterfaceEvent;
29import org.onosproject.incubator.net.intf.InterfaceListener;
Rusty Eddy4ae5aa82015-12-15 12:58:27 -080030import org.onosproject.incubator.net.intf.InterfaceService;
31import org.onosproject.net.ConnectPoint;
Jonathan Hart5af5f142016-01-28 18:45:27 -080032import org.onosproject.net.config.ConfigFactory;
33import org.onosproject.net.config.NetworkConfigEvent;
34import org.onosproject.net.config.NetworkConfigListener;
35import org.onosproject.net.config.NetworkConfigRegistry;
36import org.onosproject.net.config.basics.SubjectFactories;
Jonathan Hart36fd31e2016-01-28 15:55:31 -080037import org.onosproject.net.packet.PacketService;
Rusty Eddy4ae5aa82015-12-15 12:58:27 -080038import org.slf4j.Logger;
Jonathan Hart5af5f142016-01-28 18:45:27 -080039
Rusty Eddy4ae5aa82015-12-15 12:58:27 -080040import java.util.Map;
Jonathan Hart5af5f142016-01-28 18:45:27 -080041import java.util.Set;
Rusty Eddy4d5a92f2016-01-25 17:12:14 -080042import java.util.concurrent.Executors;
43import java.util.concurrent.ScheduledExecutorService;
44import java.util.concurrent.TimeUnit;
Rusty Eddy4ae5aa82015-12-15 12:58:27 -080045
46import 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
55public class PIMInterfaceManager implements PIMInterfaceService {
56
57 private final Logger log = getLogger(getClass());
58
Jonathan Hart5af5f142016-01-28 18:45:27 -080059 private static final Class<PimInterfaceConfig> PIM_INTERFACE_CONFIG_CLASS = PimInterfaceConfig.class;
60 private static final String PIM_INTERFACE_CONFIG_KEY = "pimInterface";
Rusty Eddy4ae5aa82015-12-15 12:58:27 -080061
Jonathan Hart54119bb2016-02-06 18:48:27 -080062 private static final int DEFAULT_TIMEOUT_TASK_PERIOD_MS = 250;
63
64 // Create a Scheduled Executor service for recurring tasks
65 private final ScheduledExecutorService scheduledExecutorService =
Rusty Eddy4d5a92f2016-01-25 17:12:14 -080066 Executors.newScheduledThreadPool(1);
67
68 // Wait for a bout 3 seconds before sending the initial hello messages.
69 // TODO: make this tunnable.
Jonathan Hart5af5f142016-01-28 18:45:27 -080070 private final long initialHelloDelay = 3;
Rusty Eddy4d5a92f2016-01-25 17:12:14 -080071
72 // Send PIM hello packets: 30 seconds.
Jonathan Hart5af5f142016-01-28 18:45:27 -080073 private final long pimHelloPeriod = 30;
Rusty Eddy4ae5aa82015-12-15 12:58:27 -080074
Jonathan Hart54119bb2016-02-06 18:48:27 -080075 private final int timeoutTaskPeriod = DEFAULT_TIMEOUT_TASK_PERIOD_MS;
76
Jonathan Hart36fd31e2016-01-28 15:55:31 -080077 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
78 protected PacketService packetService;
79
Jonathan Hart5af5f142016-01-28 18:45:27 -080080 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
81 protected NetworkConfigRegistry networkConfig;
82
83 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
84 protected InterfaceService interfaceService;
85
Rusty Eddy4ae5aa82015-12-15 12:58:27 -080086 // Store PIM Interfaces in a map key'd by ConnectPoint
87 private final Map<ConnectPoint, PIMInterface> pimInterfaces = Maps.newConcurrentMap();
88
Jonathan Hart5af5f142016-01-28 18:45:27 -080089 private final InternalNetworkConfigListener configListener =
90 new InternalNetworkConfigListener();
91 private final InternalInterfaceListener interfaceListener =
92 new InternalInterfaceListener();
93
94 private final ConfigFactory<ConnectPoint, PimInterfaceConfig> pimConfigFactory
95 = new ConfigFactory<ConnectPoint, PimInterfaceConfig>(
96 SubjectFactories.CONNECT_POINT_SUBJECT_FACTORY, PIM_INTERFACE_CONFIG_CLASS,
97 PIM_INTERFACE_CONFIG_KEY) {
98
99 @Override
100 public PimInterfaceConfig createConfig() {
101 return new PimInterfaceConfig();
102 }
103 };
104
Rusty Eddy4ae5aa82015-12-15 12:58:27 -0800105 @Activate
106 public void activate() {
Jonathan Hart5af5f142016-01-28 18:45:27 -0800107 networkConfig.registerConfigFactory(pimConfigFactory);
Rusty Eddy4ae5aa82015-12-15 12:58:27 -0800108
Jonathan Hart5af5f142016-01-28 18:45:27 -0800109 // Create PIM Interfaces for each of the existing configured interfaces.
110 Set<ConnectPoint> subjects = networkConfig.getSubjects(
111 ConnectPoint.class, PIM_INTERFACE_CONFIG_CLASS);
112 for (ConnectPoint cp : subjects) {
113 PimInterfaceConfig config = networkConfig.getConfig(cp, PIM_INTERFACE_CONFIG_CLASS);
114 updateInterface(config);
Rusty Eddy4ae5aa82015-12-15 12:58:27 -0800115 }
Rusty Eddy4d5a92f2016-01-25 17:12:14 -0800116
Jonathan Hart5af5f142016-01-28 18:45:27 -0800117 networkConfig.addListener(configListener);
118 interfaceService.addListener(interfaceListener);
119
Rusty Eddy4d5a92f2016-01-25 17:12:14 -0800120 // Schedule the periodic hello sender.
Jonathan Hart54119bb2016-02-06 18:48:27 -0800121 scheduledExecutorService.scheduleAtFixedRate(
122 SafeRecurringTask.wrap(
123 () -> pimInterfaces.values().forEach(PIMInterface::sendHello)),
124 initialHelloDelay, pimHelloPeriod, TimeUnit.SECONDS);
125
126 // Schedule task to periodically time out expired neighbors
127 scheduledExecutorService.scheduleAtFixedRate(
128 SafeRecurringTask.wrap(
129 () -> pimInterfaces.values().forEach(PIMInterface::checkNeighborTimeouts)),
130 0, timeoutTaskPeriod, TimeUnit.MILLISECONDS);
Jonathan Hart5af5f142016-01-28 18:45:27 -0800131
132 log.info("Started");
Rusty Eddy4ae5aa82015-12-15 12:58:27 -0800133 }
134
135 @Deactivate
136 public void deactivate() {
Jonathan Hart5af5f142016-01-28 18:45:27 -0800137 interfaceService.removeListener(interfaceListener);
138 networkConfig.removeListener(configListener);
139 networkConfig.unregisterConfigFactory(pimConfigFactory);
Rusty Eddy4d5a92f2016-01-25 17:12:14 -0800140
141 // Shutdown the periodic hello task.
Jonathan Hart54119bb2016-02-06 18:48:27 -0800142 scheduledExecutorService.shutdown();
Rusty Eddy4d5a92f2016-01-25 17:12:14 -0800143
Rusty Eddy4ae5aa82015-12-15 12:58:27 -0800144 log.info("Stopped");
145 }
146
147 /**
Rusty Eddy4ae5aa82015-12-15 12:58:27 -0800148 * Return the PIMInterface that corresponds to the given ConnectPoint.
149 *
150 * @param cp The ConnectPoint we want to get the PIMInterface for
151 * @return The PIMInterface if it exists, NULL if it does not exist.
152 */
153 @Override
154 public PIMInterface getPIMInterface(ConnectPoint cp) {
155 PIMInterface pi = pimInterfaces.getOrDefault(cp, null);
156 if (pi == null) {
157 log.warn("We have been asked for an Interface we don't have: " + cp.toString());
158 }
159 return pi;
160 }
Jonathan Hart5af5f142016-01-28 18:45:27 -0800161
162 @Override
163 public Set<PIMInterface> getPimInterfaces() {
164 return ImmutableSet.copyOf(pimInterfaces.values());
165 }
166
167 private void updateInterface(PimInterfaceConfig config) {
168 ConnectPoint cp = config.subject();
169
170 if (!config.isEnabled()) {
171 removeInterface(cp);
172 return;
173 }
174
175 String intfName = config.getInterfaceName();
176 Interface intf = interfaceService.getInterfaceByName(cp, intfName);
177
178 if (intf == null) {
179 log.debug("Interface configuration missing: {}", config.getInterfaceName());
180 return;
181 }
182
183
184 log.debug("Updating Interface for " + intf.connectPoint().toString());
185 pimInterfaces.computeIfAbsent(cp, k -> buildPimInterface(config, intf));
186 }
187
188 private void removeInterface(ConnectPoint cp) {
189 pimInterfaces.remove(cp);
190 }
191
192 private PIMInterface buildPimInterface(PimInterfaceConfig config, Interface intf) {
193 PIMInterface.Builder builder = PIMInterface.builder()
194 .withPacketService(packetService)
195 .withInterface(intf);
196
197 if (config.getHoldTime().isPresent()) {
198 builder.withHoldTime(config.getHoldTime().get());
199 }
200 if (config.getPriority().isPresent()) {
201 builder.withPriority(config.getPriority().get());
202 }
203 if (config.getPropagationDelay().isPresent()) {
204 builder.withPropagationDelay(config.getPropagationDelay().get());
205 }
206 if (config.getOverrideInterval().isPresent()) {
207 builder.withOverrideInterval(config.getOverrideInterval().get());
208 }
209
210 return builder.build();
211 }
212
213 /**
214 * Listener for network config events.
215 */
216 private class InternalNetworkConfigListener implements NetworkConfigListener {
217
218 @Override
219 public void event(NetworkConfigEvent event) {
220 if (event.configClass() != PIM_INTERFACE_CONFIG_CLASS) {
221 return;
222 }
223
224 switch (event.type()) {
225 case CONFIG_REGISTERED:
226 case CONFIG_UNREGISTERED:
227 break;
228 case CONFIG_ADDED:
229 case CONFIG_UPDATED:
230 ConnectPoint cp = (ConnectPoint) event.subject();
231 PimInterfaceConfig config = networkConfig.getConfig(
232 cp, PIM_INTERFACE_CONFIG_CLASS);
233
234 updateInterface(config);
235 break;
236 case CONFIG_REMOVED:
237 removeInterface((ConnectPoint) event.subject());
238 break;
239 default:
240 break;
241 }
242 }
243 }
244
245 /**
246 * Listener for interface events.
247 */
248 private class InternalInterfaceListener implements InterfaceListener {
249
250 @Override
251 public void event(InterfaceEvent event) {
252 switch (event.type()) {
253 case INTERFACE_ADDED:
254 PimInterfaceConfig config = networkConfig.getConfig(
255 event.subject().connectPoint(), PIM_INTERFACE_CONFIG_CLASS);
256
257 if (config != null) {
258 updateInterface(config);
259 }
260 break;
261 case INTERFACE_UPDATED:
262 break;
263 case INTERFACE_REMOVED:
264 removeInterface(event.subject().connectPoint());
265 break;
266 default:
267 break;
268
269 }
270 }
271 }
Rusty Eddy4ae5aa82015-12-15 12:58:27 -0800272}