blob: 248a233601461a86d962dec3cb2ccfad87d8ef02 [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 Hart6be70952016-02-12 21:11:26 -080062 public static final int DEFAULT_HELLO_INTERVAL = 30; // seconds
63
64 private static final int DEFAULT_TASK_PERIOD_MS = 250;
Jonathan Hart54119bb2016-02-06 18:48:27 -080065
66 // Create a Scheduled Executor service for recurring tasks
67 private final ScheduledExecutorService scheduledExecutorService =
Rusty Eddy4d5a92f2016-01-25 17:12:14 -080068 Executors.newScheduledThreadPool(1);
69
Jonathan Hart6be70952016-02-12 21:11:26 -080070 private final long initialHelloDelay = 1000;
Rusty Eddy4d5a92f2016-01-25 17:12:14 -080071
Jonathan Hart6be70952016-02-12 21:11:26 -080072 private final long pimHelloPeriod = DEFAULT_TASK_PERIOD_MS;
Rusty Eddy4ae5aa82015-12-15 12:58:27 -080073
Jonathan Hart6be70952016-02-12 21:11:26 -080074 private final int timeoutTaskPeriod = DEFAULT_TASK_PERIOD_MS;
Jonathan Hart54119bb2016-02-06 18:48:27 -080075
Jonathan Hart36fd31e2016-01-28 15:55:31 -080076 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
77 protected PacketService packetService;
78
Jonathan Hart5af5f142016-01-28 18:45:27 -080079 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
80 protected NetworkConfigRegistry networkConfig;
81
82 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
83 protected InterfaceService interfaceService;
84
Rusty Eddy4ae5aa82015-12-15 12:58:27 -080085 // Store PIM Interfaces in a map key'd by ConnectPoint
86 private final Map<ConnectPoint, PIMInterface> pimInterfaces = Maps.newConcurrentMap();
87
Jonathan Hart5af5f142016-01-28 18:45:27 -080088 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 Eddy4ae5aa82015-12-15 12:58:27 -0800104 @Activate
105 public void activate() {
Jonathan Hart5af5f142016-01-28 18:45:27 -0800106 networkConfig.registerConfigFactory(pimConfigFactory);
Rusty Eddy4ae5aa82015-12-15 12:58:27 -0800107
Jonathan Hart5af5f142016-01-28 18:45:27 -0800108 // 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 Eddy4ae5aa82015-12-15 12:58:27 -0800114 }
Rusty Eddy4d5a92f2016-01-25 17:12:14 -0800115
Jonathan Hart5af5f142016-01-28 18:45:27 -0800116 networkConfig.addListener(configListener);
117 interfaceService.addListener(interfaceListener);
118
Rusty Eddy4d5a92f2016-01-25 17:12:14 -0800119 // Schedule the periodic hello sender.
Jonathan Hart54119bb2016-02-06 18:48:27 -0800120 scheduledExecutorService.scheduleAtFixedRate(
121 SafeRecurringTask.wrap(
122 () -> pimInterfaces.values().forEach(PIMInterface::sendHello)),
Jonathan Hart6be70952016-02-12 21:11:26 -0800123 initialHelloDelay, pimHelloPeriod, TimeUnit.MILLISECONDS);
Jonathan Hart54119bb2016-02-06 18:48:27 -0800124
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 Hart5af5f142016-01-28 18:45:27 -0800130
131 log.info("Started");
Rusty Eddy4ae5aa82015-12-15 12:58:27 -0800132 }
133
134 @Deactivate
135 public void deactivate() {
Jonathan Hart5af5f142016-01-28 18:45:27 -0800136 interfaceService.removeListener(interfaceListener);
137 networkConfig.removeListener(configListener);
138 networkConfig.unregisterConfigFactory(pimConfigFactory);
Rusty Eddy4d5a92f2016-01-25 17:12:14 -0800139
140 // Shutdown the periodic hello task.
Jonathan Hart54119bb2016-02-06 18:48:27 -0800141 scheduledExecutorService.shutdown();
Rusty Eddy4d5a92f2016-01-25 17:12:14 -0800142
Rusty Eddy4ae5aa82015-12-15 12:58:27 -0800143 log.info("Stopped");
144 }
145
146 /**
Rusty Eddy4ae5aa82015-12-15 12:58:27 -0800147 * 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 Hart5af5f142016-01-28 18:45:27 -0800160
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
Jonathan Hart6be70952016-02-12 21:11:26 -0800196 if (config.getHelloInterval().isPresent()) {
197 builder.withHelloInterval(config.getHelloInterval().get());
198 }
Jonathan Hart5af5f142016-01-28 18:45:27 -0800199 if (config.getHoldTime().isPresent()) {
200 builder.withHoldTime(config.getHoldTime().get());
201 }
202 if (config.getPriority().isPresent()) {
203 builder.withPriority(config.getPriority().get());
204 }
205 if (config.getPropagationDelay().isPresent()) {
206 builder.withPropagationDelay(config.getPropagationDelay().get());
207 }
208 if (config.getOverrideInterval().isPresent()) {
209 builder.withOverrideInterval(config.getOverrideInterval().get());
210 }
211
212 return builder.build();
213 }
214
215 /**
216 * Listener for network config events.
217 */
218 private class InternalNetworkConfigListener implements NetworkConfigListener {
219
220 @Override
221 public void event(NetworkConfigEvent event) {
222 if (event.configClass() != PIM_INTERFACE_CONFIG_CLASS) {
223 return;
224 }
225
226 switch (event.type()) {
227 case CONFIG_REGISTERED:
228 case CONFIG_UNREGISTERED:
229 break;
230 case CONFIG_ADDED:
231 case CONFIG_UPDATED:
232 ConnectPoint cp = (ConnectPoint) event.subject();
233 PimInterfaceConfig config = networkConfig.getConfig(
234 cp, PIM_INTERFACE_CONFIG_CLASS);
235
236 updateInterface(config);
237 break;
238 case CONFIG_REMOVED:
239 removeInterface((ConnectPoint) event.subject());
240 break;
241 default:
242 break;
243 }
244 }
245 }
246
247 /**
248 * Listener for interface events.
249 */
250 private class InternalInterfaceListener implements InterfaceListener {
251
252 @Override
253 public void event(InterfaceEvent event) {
254 switch (event.type()) {
255 case INTERFACE_ADDED:
256 PimInterfaceConfig config = networkConfig.getConfig(
257 event.subject().connectPoint(), PIM_INTERFACE_CONFIG_CLASS);
258
259 if (config != null) {
260 updateInterface(config);
261 }
262 break;
263 case INTERFACE_UPDATED:
264 break;
265 case INTERFACE_REMOVED:
266 removeInterface(event.subject().connectPoint());
267 break;
268 default:
269 break;
270
271 }
272 }
273 }
Rusty Eddy4ae5aa82015-12-15 12:58:27 -0800274}