blob: a409d94a8df27e64e35f8e06c5115918fd2585d4 [file] [log] [blame]
Jian Li6322a362016-10-31 00:57:19 +09001/*
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 */
Jian Li5e505c62016-12-05 02:44:24 +090016package org.onosproject.lisp.ctl.impl;
Jian Li6322a362016-10-31 00:57:19 +090017
Jian Li834ff722016-12-13 19:43:02 +090018import com.google.common.collect.Maps;
Jian Li712ec052016-11-22 03:23:54 +090019import org.apache.felix.scr.annotations.Activate;
Jian Li6322a362016-10-31 00:57:19 +090020import org.apache.felix.scr.annotations.Component;
Jian Li712ec052016-11-22 03:23:54 +090021import org.apache.felix.scr.annotations.Deactivate;
22import org.apache.felix.scr.annotations.Modified;
23import org.apache.felix.scr.annotations.Property;
Jian Li6322a362016-10-31 00:57:19 +090024import org.apache.felix.scr.annotations.Reference;
25import org.apache.felix.scr.annotations.ReferenceCardinality;
26import org.apache.felix.scr.annotations.Service;
Jian Li712ec052016-11-22 03:23:54 +090027import org.onlab.util.Tools;
28import org.onosproject.cfg.ComponentConfigService;
Jian Li6322a362016-10-31 00:57:19 +090029import org.onosproject.core.CoreService;
Jian Li5e505c62016-12-05 02:44:24 +090030import org.onosproject.lisp.ctl.LispController;
Jian Li7ccc3a82016-12-09 01:30:56 +090031import org.onosproject.lisp.ctl.LispMessageListener;
32import org.onosproject.lisp.ctl.LispRouter;
Jian Li834ff722016-12-13 19:43:02 +090033import org.onosproject.lisp.ctl.LispRouterAgent;
Jian Lib1a8fd02016-12-27 03:55:32 +090034import org.onosproject.lisp.ctl.LispRouterFactory;
Jian Li7ccc3a82016-12-09 01:30:56 +090035import org.onosproject.lisp.ctl.LispRouterId;
36import org.onosproject.lisp.ctl.LispRouterListener;
Jian Li712ec052016-11-22 03:23:54 +090037import org.onosproject.lisp.msg.authentication.LispAuthenticationConfig;
Jian Li834ff722016-12-13 19:43:02 +090038import org.onosproject.lisp.msg.protocols.LispInfoReply;
39import org.onosproject.lisp.msg.protocols.LispInfoRequest;
40import org.onosproject.lisp.msg.protocols.LispMessage;
Jian Li712ec052016-11-22 03:23:54 +090041import org.osgi.service.component.ComponentContext;
Jian Li6322a362016-10-31 00:57:19 +090042import org.slf4j.Logger;
Jian Li6322a362016-10-31 00:57:19 +090043
Jian Li712ec052016-11-22 03:23:54 +090044import java.util.Dictionary;
Jian Li834ff722016-12-13 19:43:02 +090045import java.util.Map;
46import java.util.Set;
47import java.util.concurrent.ConcurrentMap;
48import java.util.concurrent.CopyOnWriteArraySet;
49import java.util.concurrent.ExecutorService;
Jian Li712ec052016-11-22 03:23:54 +090050
Jian Li834ff722016-12-13 19:43:02 +090051import static java.util.concurrent.Executors.newFixedThreadPool;
52import static java.util.stream.Collectors.toConcurrentMap;
Jian Li712ec052016-11-22 03:23:54 +090053import static org.onlab.util.Tools.get;
54import static org.onlab.util.Tools.getIntegerProperty;
Jian Li834ff722016-12-13 19:43:02 +090055import static org.onlab.util.Tools.groupedThreads;
56import static org.slf4j.LoggerFactory.getLogger;
Jian Li712ec052016-11-22 03:23:54 +090057
Jian Li6322a362016-10-31 00:57:19 +090058/**
59 * LISP controller initiation class.
60 */
61@Component(immediate = true)
62@Service
63public class LispControllerImpl implements LispController {
64
65 private static final String APP_ID = "org.onosproject.lisp-base";
66
Jian Li834ff722016-12-13 19:43:02 +090067 private static final Logger log = getLogger(LispControllerImpl.class);
Jian Li6322a362016-10-31 00:57:19 +090068
Jian Li712ec052016-11-22 03:23:54 +090069 private static final String DEFAULT_LISP_AUTH_KEY = "onos";
70 private static final short DEFAULT_LISP_AUTH_KEY_ID = 1;
71
Jian Li6322a362016-10-31 00:57:19 +090072 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
73 protected CoreService coreService;
74
75 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Jian Li712ec052016-11-22 03:23:54 +090076 protected ComponentConfigService cfgService;
77
78 @Property(name = "lispAuthKey", value = DEFAULT_LISP_AUTH_KEY,
79 label = "Authentication key which is used to calculate authentication " +
80 "data for LISP control message; default value is onos")
Jian Li834ff722016-12-13 19:43:02 +090081 private String lispAuthKey = DEFAULT_LISP_AUTH_KEY;
Jian Li712ec052016-11-22 03:23:54 +090082
83 @Property(name = "lispAuthKeyId", intValue = DEFAULT_LISP_AUTH_KEY_ID,
84 label = "Authentication key id which denotes the authentication method " +
85 "that ONOS uses to calculate the authentication data; " +
86 "1 denotes HMAC SHA1 encryption, 2 denotes HMAC SHA256 encryption; " +
87 "default value is 1")
Jian Li834ff722016-12-13 19:43:02 +090088 private int lispAuthKeyId = DEFAULT_LISP_AUTH_KEY_ID;
89
90 ExecutorService executorMessages =
91 newFixedThreadPool(4, groupedThreads("onos/lisp", "event-stats-%d", log));
92
93 protected LispRouterAgent agent = new DefaultLispRouterAgent();
94
95 ConcurrentMap<LispRouterId, LispRouter> connectedRouters = Maps.newConcurrentMap();
96
Jian Li0dab5962016-12-15 03:44:28 +090097 final LispAuthenticationConfig authConfig = LispAuthenticationConfig.getInstance();
98 LispControllerBootstrap bootstrap = new LispControllerBootstrap();
99
Jian Li834ff722016-12-13 19:43:02 +0900100 private Set<LispRouterListener> lispRouterListeners = new CopyOnWriteArraySet<>();
101 private Set<LispMessageListener> lispMessageListeners = new CopyOnWriteArraySet<>();
Jian Li712ec052016-11-22 03:23:54 +0900102
Jian Lib1a8fd02016-12-27 03:55:32 +0900103 private LispRouterFactory routerFactory = LispRouterFactory.getInstance();
104
Jian Li6322a362016-10-31 00:57:19 +0900105 @Activate
Jian Li712ec052016-11-22 03:23:54 +0900106 public void activate(ComponentContext context) {
Jian Li834ff722016-12-13 19:43:02 +0900107 coreService.registerApplication(APP_ID, this::cleanup);
Jian Li712ec052016-11-22 03:23:54 +0900108 cfgService.registerProperties(getClass());
Jian Li712ec052016-11-22 03:23:54 +0900109 initAuthConfig(context.getProperties());
Jian Lib1a8fd02016-12-27 03:55:32 +0900110 routerFactory.setAgent(agent);
Jian Li6322a362016-10-31 00:57:19 +0900111 bootstrap.start();
112 log.info("Started");
113 }
114
Jian Li834ff722016-12-13 19:43:02 +0900115 /**
116 * Shutdowns all listening channel and all LISP channels.
117 * Clean information about routers before deactivating.
118 */
119 private void cleanup() {
120 bootstrap.stop();
Jian Lib1a8fd02016-12-27 03:55:32 +0900121 routerFactory.cleanAgent();
Jian Li834ff722016-12-13 19:43:02 +0900122 connectedRouters.values().forEach(LispRouter::disconnectRouter);
123 connectedRouters.clear();
124 }
125
Jian Li6322a362016-10-31 00:57:19 +0900126 @Deactivate
127 public void deactivate() {
Jian Li834ff722016-12-13 19:43:02 +0900128 cleanup();
Jian Li712ec052016-11-22 03:23:54 +0900129 cfgService.unregisterProperties(getClass(), false);
Jian Li6322a362016-10-31 00:57:19 +0900130 log.info("Stopped");
131 }
Jian Li712ec052016-11-22 03:23:54 +0900132
133 @Modified
134 public void modified(ComponentContext context) {
135 readComponentConfiguration(context);
Jian Li834ff722016-12-13 19:43:02 +0900136 bootstrap.stop();
137 bootstrap.start();
Jian Li712ec052016-11-22 03:23:54 +0900138 }
139
140 /**
141 * Initializes authentication key and authentication method.
142 *
143 * @param properties a set of properties that contained in component context
144 */
145 private void initAuthConfig(Dictionary<?, ?> properties) {
146 authConfig.updateLispAuthKey(get(properties, "lispAuthKey"));
147 authConfig.updateLispAuthKeyId(getIntegerProperty(properties, "lispAuthKeyId"));
148 }
149
150 /**
151 * Extracts properties from the component configuration context.
152 *
153 * @param context the component context
154 */
155 private void readComponentConfiguration(ComponentContext context) {
156 Dictionary<?, ?> properties = context.getProperties();
157
158 String lispAuthKeyStr = Tools.get(properties, "lispAuthKey");
159 lispAuthKey = lispAuthKeyStr != null ? lispAuthKeyStr : DEFAULT_LISP_AUTH_KEY;
160 authConfig.updateLispAuthKey(lispAuthKey);
161 log.info("Configured. LISP authentication key is {}", lispAuthKey);
162
163 Integer lispAuthMethodInt = Tools.getIntegerProperty(properties, "lispAuthKeyId");
164 if (lispAuthMethodInt == null) {
165 lispAuthKeyId = DEFAULT_LISP_AUTH_KEY_ID;
166 log.info("LISP authentication method is not configured, default value is {}", lispAuthKeyId);
167 } else {
168 lispAuthKeyId = lispAuthMethodInt;
169 log.info("Configured. LISP authentication method is configured to {}", lispAuthKeyId);
170 }
171 authConfig.updateLispAuthKeyId(lispAuthKeyId);
172 }
Jian Li7ccc3a82016-12-09 01:30:56 +0900173
174 @Override
175 public Iterable<LispRouter> getRouters() {
Jian Li834ff722016-12-13 19:43:02 +0900176 return connectedRouters.values();
177 }
178
179 @Override
180 public Iterable<LispRouter> getSubscribedRouters() {
181 return connectedRouters.entrySet()
182 .stream()
183 .filter(e -> e.getValue().isSubscribed())
184 .collect(toConcurrentMap(Map.Entry::getKey,
185 Map.Entry::getValue)).values();
Jian Li7ccc3a82016-12-09 01:30:56 +0900186 }
187
188 @Override
Jian Li9b1a45b2017-01-19 13:34:31 -0800189 public LispRouter connectRouter(LispRouterId routerId) {
190 if (connectedRouters.containsKey(routerId)) {
191 log.debug("LISP router {} is already existing", routerId);
192 return connectedRouters.get(routerId);
193 } else {
194 // TODO: currently we do not consider to add LISP router from netcfg
195 log.warn("Adding router from netcfg is not supported currently");
196 return null;
197 }
198 }
199
200 @Override
201 public void disconnectRouter(LispRouterId routerId, boolean remove) {
202 if (!connectedRouters.containsKey(routerId)) {
203 log.warn("LISP router {} is not existing", routerId);
204 } else {
205 connectedRouters.get(routerId).disconnectRouter();
206 if (remove) {
207 agent.removeConnectedRouter(routerId);
208 }
209 }
210 }
211
212 @Override
Jian Li7ccc3a82016-12-09 01:30:56 +0900213 public LispRouter getRouter(LispRouterId routerId) {
Jian Li834ff722016-12-13 19:43:02 +0900214 return connectedRouters.get(routerId);
Jian Li7ccc3a82016-12-09 01:30:56 +0900215 }
216
217 @Override
218 public void addRouterListener(LispRouterListener listener) {
Jian Li834ff722016-12-13 19:43:02 +0900219 if (!lispRouterListeners.contains(listener)) {
220 lispRouterListeners.add(listener);
221 }
Jian Li7ccc3a82016-12-09 01:30:56 +0900222 }
223
224 @Override
225 public void removeRouterListener(LispRouterListener listener) {
Jian Li834ff722016-12-13 19:43:02 +0900226 lispRouterListeners.remove(listener);
Jian Li7ccc3a82016-12-09 01:30:56 +0900227 }
228
229 @Override
230 public void addMessageListener(LispMessageListener listener) {
Jian Li834ff722016-12-13 19:43:02 +0900231 if (!lispMessageListeners.contains(listener)) {
232 lispMessageListeners.add(listener);
233 }
Jian Li7ccc3a82016-12-09 01:30:56 +0900234 }
235
236 @Override
237 public void removeMessageListener(LispMessageListener listener) {
Jian Li834ff722016-12-13 19:43:02 +0900238 lispMessageListeners.remove(listener);
239 }
Jian Li7ccc3a82016-12-09 01:30:56 +0900240
Jian Li834ff722016-12-13 19:43:02 +0900241 /**
242 * Implementation of a LISP agent which is responsible for keeping track of
243 * connected LISP routers and the state in which they are in.
244 */
245 public final class DefaultLispRouterAgent implements LispRouterAgent {
246
247 private final Logger log = getLogger(DefaultLispRouterAgent.class);
248
249 /**
250 * Prevents object instantiation from external class.
251 */
252 private DefaultLispRouterAgent() {
253 }
254
255 @Override
256 public boolean addConnectedRouter(LispRouterId routerId, LispRouter router) {
257
258 if (connectedRouters.get(routerId) != null) {
259 log.error("Trying to add connectedRouter but found a previous " +
260 "value for routerId: {}", routerId);
261 return false;
262 } else {
263 log.info("Added router {}", routerId);
264 connectedRouters.put(routerId, router);
265 for (LispRouterListener listener : lispRouterListeners) {
266 listener.routerAdded(routerId);
267 }
268 return true;
269 }
270 }
271
272 @Override
273 public void removeConnectedRouter(LispRouterId routerId) {
274
275 if (connectedRouters.get(routerId) == null) {
276 log.error("Trying to remove router {} from connectedRouter " +
277 "list but no element was found", routerId);
278 } else {
279 log.info("Removed router {}", routerId);
280 connectedRouters.remove(routerId);
281 for (LispRouterListener listener : lispRouterListeners) {
282 listener.routerRemoved(routerId);
283 }
284 }
285 }
286
287 @Override
288 public void processUpstreamMessage(LispRouterId routerId, LispMessage message) {
289
290 switch (message.getType()) {
291 case LISP_MAP_REGISTER:
292 case LISP_MAP_REQUEST:
293 executorMessages.execute(
294 new LispIncomingMessageHandler(routerId, message));
295 break;
296 case LISP_INFO:
297 if (message instanceof LispInfoRequest) {
298 executorMessages.execute(
299 new LispIncomingMessageHandler(routerId, message));
300 } else {
301 log.warn("Not incoming LISP control message");
302 }
303 break;
304 default:
305 log.warn("Not incoming LISP control message");
306 break;
307 }
308 }
309
310 @Override
311 public void processDownstreamMessage(LispRouterId routerId, LispMessage message) {
312
313 switch (message.getType()) {
314 case LISP_MAP_NOTIFY:
315 case LISP_MAP_REPLY:
316 executorMessages.execute(
317 new LispOutgoingMessageHandler(routerId, message));
318 break;
319 case LISP_INFO:
320 if (message instanceof LispInfoReply) {
321 executorMessages.execute(
322 new LispOutgoingMessageHandler(routerId, message));
323 } else {
324 log.warn("Not outgoing LISP control message");
325 }
326 break;
327 default:
328 log.warn("Not outgoing LISP control message");
329 break;
330 }
331 }
332 }
333
334 /**
335 * LISP message handler.
336 */
337 protected class LispMessageHandler implements Runnable {
338
339 private final LispRouterId routerId;
340 private final LispMessage message;
341 private final boolean isIncoming;
342
343 LispMessageHandler(LispRouterId routerId,
344 LispMessage message, boolean isIncoming) {
345 this.routerId = routerId;
346 this.message = message;
347 this.isIncoming = isIncoming;
348 }
349
350 @Override
351 public void run() {
352 for (LispMessageListener listener : lispMessageListeners) {
353 if (isIncoming) {
354 listener.handleIncomingMessage(routerId, message);
355 } else {
356 listener.handleOutgoingMessage(routerId, message);
357 }
358 }
359 }
360 }
361
362 /**
363 * LISP incoming message handler.
364 */
365 protected final class LispIncomingMessageHandler
366 extends LispMessageHandler implements Runnable {
367
368 LispIncomingMessageHandler(LispRouterId routerId,
369 LispMessage message) {
370 super(routerId, message, true);
371 }
372 }
373
374 /**
375 * LISP outgoing message handler.
376 */
377 protected final class LispOutgoingMessageHandler
378 extends LispMessageHandler implements Runnable {
379
380 LispOutgoingMessageHandler(LispRouterId routerId,
381 LispMessage message) {
382 super(routerId, message, false);
383 }
Jian Li7ccc3a82016-12-09 01:30:56 +0900384 }
Jian Li6322a362016-10-31 00:57:19 +0900385}