blob: 565d76b8e0af013e9d9f23720a2a45c3f68c9e6c [file] [log] [blame]
alshabib3b1eadc2016-02-01 17:57:00 -08001/*
2 * Copyright 2015-2016 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.cordmcast;
17
Jonathan Hartc3f84eb2016-02-19 12:44:36 -080018import com.fasterxml.jackson.databind.ObjectMapper;
19import com.fasterxml.jackson.databind.node.ArrayNode;
Jonathan Hart28271642016-02-10 16:13:54 -080020import com.fasterxml.jackson.databind.node.ObjectNode;
Jonathan Hartc3f84eb2016-02-19 12:44:36 -080021import com.google.common.collect.Lists;
alshabib3b1eadc2016-02-01 17:57:00 -080022import com.google.common.collect.Maps;
Jonathan Hart28271642016-02-10 16:13:54 -080023import com.sun.jersey.api.client.Client;
24import com.sun.jersey.api.client.WebResource;
25import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;
alshabib3b1eadc2016-02-01 17:57:00 -080026import org.apache.felix.scr.annotations.Activate;
27import org.apache.felix.scr.annotations.Component;
28import org.apache.felix.scr.annotations.Deactivate;
Jonathan Hart28271642016-02-10 16:13:54 -080029import org.apache.felix.scr.annotations.Modified;
30import org.apache.felix.scr.annotations.Property;
alshabib3b1eadc2016-02-01 17:57:00 -080031import org.apache.felix.scr.annotations.Reference;
32import org.apache.felix.scr.annotations.ReferenceCardinality;
33import org.onlab.packet.Ethernet;
alshabib3b1eadc2016-02-01 17:57:00 -080034import org.onlab.packet.IpAddress;
35import org.onlab.packet.VlanId;
Jonathan Hart28271642016-02-10 16:13:54 -080036import org.onosproject.cfg.ComponentConfigService;
37import org.onosproject.codec.CodecService;
Jonathan Hartc3f84eb2016-02-19 12:44:36 -080038import org.onosproject.codec.JsonCodec;
alshabib3b1eadc2016-02-01 17:57:00 -080039import org.onosproject.core.ApplicationId;
40import org.onosproject.core.CoreService;
41import org.onosproject.net.ConnectPoint;
42import org.onosproject.net.flow.DefaultTrafficSelector;
43import org.onosproject.net.flow.DefaultTrafficTreatment;
44import org.onosproject.net.flow.TrafficSelector;
45import org.onosproject.net.flowobjective.DefaultForwardingObjective;
46import org.onosproject.net.flowobjective.DefaultNextObjective;
47import org.onosproject.net.flowobjective.FlowObjectiveService;
48import org.onosproject.net.flowobjective.ForwardingObjective;
49import org.onosproject.net.flowobjective.NextObjective;
50import org.onosproject.net.flowobjective.Objective;
51import org.onosproject.net.flowobjective.ObjectiveContext;
52import org.onosproject.net.flowobjective.ObjectiveError;
alshabib3b1eadc2016-02-01 17:57:00 -080053import org.onosproject.net.mcast.McastEvent;
54import org.onosproject.net.mcast.McastListener;
Jonathan Hart28271642016-02-10 16:13:54 -080055import org.onosproject.net.mcast.McastRoute;
alshabib3b1eadc2016-02-01 17:57:00 -080056import org.onosproject.net.mcast.McastRouteInfo;
57import org.onosproject.net.mcast.MulticastRouteService;
Jonathan Hart28271642016-02-10 16:13:54 -080058import org.onosproject.rest.AbstractWebResource;
59import org.osgi.service.component.ComponentContext;
alshabib3b1eadc2016-02-01 17:57:00 -080060import org.slf4j.Logger;
61
Jonathan Hartc3f84eb2016-02-19 12:44:36 -080062import javax.ws.rs.core.MediaType;
63import java.io.IOException;
Jonathan Hart28271642016-02-10 16:13:54 -080064import java.util.Dictionary;
Jonathan Hartc3f84eb2016-02-19 12:44:36 -080065import java.util.List;
alshabib3b1eadc2016-02-01 17:57:00 -080066import java.util.Map;
alshabibfc1cb032016-02-17 15:37:56 -080067import java.util.Properties;
Jonathan Hartc3f84eb2016-02-19 12:44:36 -080068import java.util.Set;
Jonathan Hart28271642016-02-10 16:13:54 -080069import java.util.concurrent.atomic.AtomicBoolean;
alshabib3b1eadc2016-02-01 17:57:00 -080070import java.util.concurrent.atomic.AtomicInteger;
71
Jonathan Hartc3f84eb2016-02-19 12:44:36 -080072import static com.google.common.base.Preconditions.checkNotNull;
alshabibfc1cb032016-02-17 15:37:56 -080073import static com.google.common.base.Strings.isNullOrEmpty;
Jonathan Hart28271642016-02-10 16:13:54 -080074import static com.google.common.net.MediaType.JSON_UTF_8;
alshabibfc1cb032016-02-17 15:37:56 -080075import static org.onlab.util.Tools.get;
alshabib3b1eadc2016-02-01 17:57:00 -080076import static org.slf4j.LoggerFactory.getLogger;
77
78/**
Jonathan Hartc3f84eb2016-02-19 12:44:36 -080079 * CORD multicast provisioning application. Operates by listening to
Jonathan Hart28271642016-02-10 16:13:54 -080080 * events on the multicast rib and provisioning groups to program multicast
alshabib3b1eadc2016-02-01 17:57:00 -080081 * flows on the dataplane.
82 */
83@Component(immediate = true)
84public class CordMcast {
85
86 private static final int DEFAULT_PRIORITY = 1000;
87 private static final short DEFAULT_MCAST_VLAN = 4000;
alshabibfc1cb032016-02-17 15:37:56 -080088 private static final String DEFAULT_SYNC_HOST = "10.90.0.8:8181";
89 private static final String DEFAULT_USER = "karaf";
90 private static final String DEFAULT_PASSWORD = "karaf";
91
alshabib3b1eadc2016-02-01 17:57:00 -080092 private final Logger log = getLogger(getClass());
93
94 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
95 protected MulticastRouteService mcastService;
96
97 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
alshabib3b1eadc2016-02-01 17:57:00 -080098 protected FlowObjectiveService flowObjectiveService;
99
100 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
101 protected CoreService coreService;
102
Jonathan Hart28271642016-02-10 16:13:54 -0800103 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
104 protected CodecService codecService;
105
106 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
107 protected ComponentConfigService componentConfigService;
108
alshabib3b1eadc2016-02-01 17:57:00 -0800109 protected McastListener listener = new InternalMulticastListener();
110
alshabib3b1eadc2016-02-01 17:57:00 -0800111 //TODO: move this to a ec map
112 private Map<IpAddress, Integer> groups = Maps.newConcurrentMap();
113
114 //TODO: move this to distributed atomic long
115 private AtomicInteger channels = new AtomicInteger(0);
116
117 private ApplicationId appId;
118
alshabibfc1cb032016-02-17 15:37:56 -0800119 @Property(name = "mcastVlan", intValue = DEFAULT_MCAST_VLAN,
120 label = "VLAN for multicast traffic")
121 private int mcastVlan = DEFAULT_MCAST_VLAN;
alshabib3b1eadc2016-02-01 17:57:00 -0800122
alshabibfc1cb032016-02-17 15:37:56 -0800123 @Property(name = "vlanEnabled", boolValue = false,
124 label = "Use vlan for multicast traffic")
125 private boolean vlanEnabled = false;
126
127 @Property(name = "priority", intValue = DEFAULT_PRIORITY,
128 label = "Priority for multicast rules")
alshabib3b1eadc2016-02-01 17:57:00 -0800129 private int priority = DEFAULT_PRIORITY;
130
alshabibfc1cb032016-02-17 15:37:56 -0800131 @Property(name = "syncHost", value = DEFAULT_SYNC_HOST,
Jonathan Hart28271642016-02-10 16:13:54 -0800132 label = "host:port to synchronize routes to")
alshabibfc1cb032016-02-17 15:37:56 -0800133 private String syncHost = DEFAULT_SYNC_HOST;
Jonathan Hart28271642016-02-10 16:13:54 -0800134
135 @Property(name = "username", value = DEFAULT_USER,
136 label = "Username for REST password authentication")
137 private String user = DEFAULT_USER;
138
139 @Property(name = "password", value = DEFAULT_PASSWORD,
140 label = "Password for REST authentication")
141 private String password = DEFAULT_PASSWORD;
142
143 private String fabricOnosUrl;
144
alshabib3b1eadc2016-02-01 17:57:00 -0800145 @Activate
Jonathan Hart435ffc42016-02-19 10:32:05 -0800146 public void activate(ComponentContext context) {
Jonathan Hartc3f84eb2016-02-19 12:44:36 -0800147 componentConfigService.registerProperties(getClass());
Jonathan Hart435ffc42016-02-19 10:32:05 -0800148 modified(context);
149
alshabib3b1eadc2016-02-01 17:57:00 -0800150 appId = coreService.registerApplication("org.onosproject.cordmcast");
Jonathan Hart28271642016-02-10 16:13:54 -0800151
152 fabricOnosUrl = "http://" + syncHost + "/onos/v1/mcast";
153
Jonathan Hartc3f84eb2016-02-19 12:44:36 -0800154 clearRemoteRoutes();
155
156 mcastService.addListener(listener);
157
158 for (McastRoute route : mcastService.getRoutes()) {
159 Set<ConnectPoint> sinks = mcastService.fetchSinks(route);
160 if (!sinks.isEmpty()) {
161 sinks.forEach(s -> provisionGroup(route, s));
162 }
163 }
164
alshabib3b1eadc2016-02-01 17:57:00 -0800165 log.info("Started");
166 }
167
168 @Deactivate
169 public void deactivate() {
Jonathan Hartc3f84eb2016-02-19 12:44:36 -0800170 componentConfigService.unregisterProperties(getClass(), false);
alshabib3b1eadc2016-02-01 17:57:00 -0800171 mcastService.removeListener(listener);
172 log.info("Stopped");
173 }
174
Jonathan Hart28271642016-02-10 16:13:54 -0800175 @Modified
176 public void modified(ComponentContext context) {
alshabibfc1cb032016-02-17 15:37:56 -0800177 Dictionary<?, ?> properties = context != null ? context.getProperties() : new Properties();
178
alshabibfc1cb032016-02-17 15:37:56 -0800179 try {
180 String s = get(properties, "username");
181 user = isNullOrEmpty(s) ? DEFAULT_USER : s.trim();
182
183 s = get(properties, "password");
184 password = isNullOrEmpty(s) ? DEFAULT_PASSWORD : s.trim();
185
186 s = get(properties, "mcastVlan");
187 mcastVlan = isNullOrEmpty(s) ? DEFAULT_MCAST_VLAN : Short.parseShort(s.trim());
188
189 s = get(properties, "vlanEnabled");
190 vlanEnabled = isNullOrEmpty(s) || Boolean.parseBoolean(s.trim());
191
192 s = get(properties, "priority");
193 priority = isNullOrEmpty(s) ? DEFAULT_PRIORITY : Integer.parseInt(s.trim());
194
195 s = get(properties, syncHost);
196 syncHost = isNullOrEmpty(s) ? DEFAULT_SYNC_HOST : s.trim();
197 } catch (Exception e) {
198 user = DEFAULT_USER;
199 password = DEFAULT_PASSWORD;
200 syncHost = DEFAULT_SYNC_HOST;
201 mcastVlan = DEFAULT_MCAST_VLAN;
202 vlanEnabled = false;
203 priority = DEFAULT_PRIORITY;
204 }
Jonathan Hart28271642016-02-10 16:13:54 -0800205 }
206
alshabib3b1eadc2016-02-01 17:57:00 -0800207 private class InternalMulticastListener implements McastListener {
208 @Override
209 public void event(McastEvent event) {
Jonathan Hartc3f84eb2016-02-19 12:44:36 -0800210 McastRouteInfo info = event.subject();
alshabib3b1eadc2016-02-01 17:57:00 -0800211 switch (event.type()) {
212 case ROUTE_ADDED:
213 break;
214 case ROUTE_REMOVED:
215 break;
216 case SOURCE_ADDED:
217 break;
218 case SINK_ADDED:
Jonathan Hartc3f84eb2016-02-19 12:44:36 -0800219 if (!info.sink().isPresent()) {
220 log.warn("No sink given after sink added event: {}", info);
221 return;
222 }
223 provisionGroup(info.route(), info.sink().get());
alshabib3b1eadc2016-02-01 17:57:00 -0800224 break;
225 case SINK_REMOVED:
alshabibfc1cb032016-02-17 15:37:56 -0800226 unprovisionGroup(event.subject());
alshabib3b1eadc2016-02-01 17:57:00 -0800227 break;
228 default:
229 log.warn("Unknown mcast event {}", event.type());
230 }
231 }
232 }
233
alshabibfc1cb032016-02-17 15:37:56 -0800234 private void unprovisionGroup(McastRouteInfo info) {
Jonathan Hart718c0452016-02-18 15:56:22 -0800235 if (info.sinks().isEmpty()) {
Jonathan Hartc3f84eb2016-02-19 12:44:36 -0800236 removeRemoteRoute(info.route());
Jonathan Hart718c0452016-02-18 15:56:22 -0800237 }
238
alshabibfc1cb032016-02-17 15:37:56 -0800239 if (!info.sink().isPresent()) {
240 log.warn("No sink given after sink removed event: {}", info);
241 return;
242 }
243 ConnectPoint loc = info.sink().get();
244
245 NextObjective next = DefaultNextObjective.builder()
246 .fromApp(appId)
247 .addTreatment(DefaultTrafficTreatment.builder().setOutput(loc.port()).build())
248 .withType(NextObjective.Type.BROADCAST)
249 .withId(groups.get(info.route().group()))
250 .removeFromExisting(new ObjectiveContext() {
251 @Override
252 public void onSuccess(Objective objective) {
253 //TODO: change to debug
254 log.info("Next Objective {} installed", objective.id());
255 }
256
257 @Override
258 public void onError(Objective objective, ObjectiveError error) {
259 //TODO: change to debug
260 log.info("Next Objective {} failed, because {}",
261 objective.id(),
262 error);
263 }
264 });
265
266 flowObjectiveService.next(loc.deviceId(), next);
alshabibfc1cb032016-02-17 15:37:56 -0800267 }
268
Jonathan Hartc3f84eb2016-02-19 12:44:36 -0800269 private void provisionGroup(McastRoute route, ConnectPoint sink) {
270 checkNotNull(route, "Route cannot be null");
271 checkNotNull(sink, "Sink cannot be null");
alshabib3b1eadc2016-02-01 17:57:00 -0800272
Jonathan Hart28271642016-02-10 16:13:54 -0800273 final AtomicBoolean sync = new AtomicBoolean(false);
alshabib3b1eadc2016-02-01 17:57:00 -0800274
Jonathan Hartc3f84eb2016-02-19 12:44:36 -0800275 Integer nextId = groups.computeIfAbsent(route.group(), (g) -> {
Jonathan Hart28271642016-02-10 16:13:54 -0800276 Integer id = allocateId();
alshabib3b1eadc2016-02-01 17:57:00 -0800277
alshabibfc1cb032016-02-17 15:37:56 -0800278 NextObjective next = DefaultNextObjective.builder()
279 .fromApp(appId)
Jonathan Hartc3f84eb2016-02-19 12:44:36 -0800280 .addTreatment(DefaultTrafficTreatment.builder().setOutput(sink.port()).build())
alshabibfc1cb032016-02-17 15:37:56 -0800281 .withType(NextObjective.Type.BROADCAST)
282 .withId(id)
283 .add(new ObjectiveContext() {
284 @Override
285 public void onSuccess(Objective objective) {
286 //TODO: change to debug
287 log.info("Next Objective {} installed", objective.id());
288 }
289
290 @Override
291 public void onError(Objective objective, ObjectiveError error) {
292 //TODO: change to debug
293 log.info("Next Objective {} failed, because {}",
294 objective.id(),
295 error);
296 }
297 });
298
Jonathan Hartc3f84eb2016-02-19 12:44:36 -0800299 flowObjectiveService.next(sink.deviceId(), next);
alshabibfc1cb032016-02-17 15:37:56 -0800300
301 TrafficSelector.Builder mcast = DefaultTrafficSelector.builder()
alshabib3b1eadc2016-02-01 17:57:00 -0800302 .matchEthType(Ethernet.TYPE_IPV4)
alshabibfc1cb032016-02-17 15:37:56 -0800303 .matchIPDst(g.toIpPrefix());
304
alshabibfc1cb032016-02-17 15:37:56 -0800305 if (vlanEnabled) {
306 mcast.matchVlanId(VlanId.vlanId((short) mcastVlan));
307 }
alshabib3b1eadc2016-02-01 17:57:00 -0800308
alshabib3b1eadc2016-02-01 17:57:00 -0800309 ForwardingObjective fwd = DefaultForwardingObjective.builder()
310 .fromApp(appId)
311 .nextStep(id)
312 .makePermanent()
313 .withFlag(ForwardingObjective.Flag.VERSATILE)
314 .withPriority(priority)
alshabibfc1cb032016-02-17 15:37:56 -0800315 .withSelector(mcast.build())
alshabib3b1eadc2016-02-01 17:57:00 -0800316 .add(new ObjectiveContext() {
317 @Override
318 public void onSuccess(Objective objective) {
319 //TODO: change to debug
320 log.info("Forwarding objective installed {}", objective);
321 }
322
323 @Override
324 public void onError(Objective objective, ObjectiveError error) {
325 //TODO: change to debug
326 log.info("Forwarding objective failed {}", objective);
327 }
328 });
329
Jonathan Hartc3f84eb2016-02-19 12:44:36 -0800330 flowObjectiveService.forward(sink.deviceId(), fwd);
alshabib3b1eadc2016-02-01 17:57:00 -0800331
Jonathan Hart28271642016-02-10 16:13:54 -0800332 sync.set(true);
333
alshabib3b1eadc2016-02-01 17:57:00 -0800334 return id;
335 });
336
alshabibfc1cb032016-02-17 15:37:56 -0800337 if (!sync.get()) {
338 NextObjective next = DefaultNextObjective.builder()
339 .fromApp(appId)
Jonathan Hartc3f84eb2016-02-19 12:44:36 -0800340 .addTreatment(DefaultTrafficTreatment.builder().setOutput(sink.port()).build())
alshabibfc1cb032016-02-17 15:37:56 -0800341 .withType(NextObjective.Type.BROADCAST)
342 .withId(nextId)
343 .addToExisting(new ObjectiveContext() {
344 @Override
345 public void onSuccess(Objective objective) {
346 //TODO: change to debug
347 log.info("Next Objective {} installed", objective.id());
348 }
alshabib3b1eadc2016-02-01 17:57:00 -0800349
alshabibfc1cb032016-02-17 15:37:56 -0800350 @Override
351 public void onError(Objective objective, ObjectiveError error) {
352 //TODO: change to debug
353 log.info("Next Objective {} failed, because {}",
354 objective.id(),
355 error);
356 }
357 });
alshabib3b1eadc2016-02-01 17:57:00 -0800358
Jonathan Hartc3f84eb2016-02-19 12:44:36 -0800359 flowObjectiveService.next(sink.deviceId(), next);
alshabibfc1cb032016-02-17 15:37:56 -0800360 }
Jonathan Hart28271642016-02-10 16:13:54 -0800361
Jonathan Hartc3f84eb2016-02-19 12:44:36 -0800362 addRemoteRoute(route);
alshabib3b1eadc2016-02-01 17:57:00 -0800363 }
364
Jonathan Hartc3f84eb2016-02-19 12:44:36 -0800365 private void addRemoteRoute(McastRoute route) {
366 checkNotNull(route);
Jonathan Hart28271642016-02-10 16:13:54 -0800367 if (syncHost == null) {
368 log.warn("No host configured for synchronization; route will be dropped");
369 return;
370 }
371
Jonathan Hartc3f84eb2016-02-19 12:44:36 -0800372 log.debug("Sending route to other ONOS: {}", route);
Jonathan Hart28271642016-02-10 16:13:54 -0800373
374 WebResource.Builder builder = getClientBuilder(fabricOnosUrl);
375
376 ObjectNode json = codecService.getCodec(McastRoute.class)
Jonathan Hartc3f84eb2016-02-19 12:44:36 -0800377 .encode(route, new AbstractWebResource());
Jonathan Hart28271642016-02-10 16:13:54 -0800378 builder.post(json.toString());
alshabib3b1eadc2016-02-01 17:57:00 -0800379 }
Jonathan Hart28271642016-02-10 16:13:54 -0800380
Jonathan Hartc3f84eb2016-02-19 12:44:36 -0800381 private void removeRemoteRoute(McastRoute route) {
Jonathan Hart718c0452016-02-18 15:56:22 -0800382 if (syncHost == null) {
383 log.warn("No host configured for synchronization; route will be dropped");
384 return;
385 }
386
Jonathan Hartc3f84eb2016-02-19 12:44:36 -0800387 log.debug("Removing route from other ONOS: {}", route);
Jonathan Hart718c0452016-02-18 15:56:22 -0800388
389 WebResource.Builder builder = getClientBuilder(fabricOnosUrl);
390
391 ObjectNode json = codecService.getCodec(McastRoute.class)
Jonathan Hartc3f84eb2016-02-19 12:44:36 -0800392 .encode(route, new AbstractWebResource());
Jonathan Hart718c0452016-02-18 15:56:22 -0800393 builder.delete(json.toString());
394 }
395
Jonathan Hartc3f84eb2016-02-19 12:44:36 -0800396 private void clearRemoteRoutes() {
397 if (syncHost == null) {
398 log.warn("No host configured for synchronization");
399 return;
400 }
401
402 log.debug("Clearing remote multicast routes");
403
404 WebResource.Builder builder = getClientBuilder(fabricOnosUrl);
405
406 String response = builder
407 .accept(MediaType.APPLICATION_JSON_TYPE)
408 .get(String.class);
409
410 JsonCodec<McastRoute> routeCodec = codecService.getCodec(McastRoute.class);
411 ObjectMapper mapper = new ObjectMapper();
412 List<McastRoute> mcastRoutes = Lists.newArrayList();
413 try {
414 ObjectNode node = (ObjectNode) mapper.readTree(response);
415 ArrayNode list = (ArrayNode) node.path("routes");
416
417 list.forEach(n -> mcastRoutes.add(
418 routeCodec.decode((ObjectNode) n, new AbstractWebResource())));
419 } catch (IOException e) {
420 log.warn("Error clearing remote routes", e);
421 }
422
423 mcastRoutes.forEach(this::removeRemoteRoute);
424 }
425
Jonathan Hart28271642016-02-10 16:13:54 -0800426 private Integer allocateId() {
427 return channels.getAndIncrement();
428 }
429
430 private WebResource.Builder getClientBuilder(String uri) {
431 Client client = Client.create();
432 client.addFilter(new HTTPBasicAuthFilter(user, password));
433 WebResource resource = client.resource(uri);
434 return resource.accept(JSON_UTF_8.toString())
435 .type(JSON_UTF_8.toString());
436 }
437
alshabib3b1eadc2016-02-01 17:57:00 -0800438}