blob: acf261da58a665f6e785589eb8d9a262d0d9adbf [file] [log] [blame]
alshabib3b1eadc2016-02-01 17:57:00 -08001/*
Brian O'Connoraf764bf2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
alshabib3b1eadc2016-02-01 17:57:00 -08003 *
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 */
alshabib772e1582016-06-01 17:50:05 -070016package org.opencord.cordmcast;
alshabib3b1eadc2016-02-01 17:57:00 -080017
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;
alshabib09069c92016-02-21 14:49:51 -080023import org.apache.commons.lang3.tuple.ImmutablePair;
alshabib3b1eadc2016-02-01 17:57:00 -080024import org.apache.felix.scr.annotations.Activate;
25import org.apache.felix.scr.annotations.Component;
26import org.apache.felix.scr.annotations.Deactivate;
Jonathan Hart28271642016-02-10 16:13:54 -080027import org.apache.felix.scr.annotations.Modified;
28import org.apache.felix.scr.annotations.Property;
alshabib3b1eadc2016-02-01 17:57:00 -080029import org.apache.felix.scr.annotations.Reference;
30import org.apache.felix.scr.annotations.ReferenceCardinality;
Jian Li46472d72016-03-09 10:52:49 -080031import org.glassfish.jersey.client.ClientConfig;
32import org.glassfish.jersey.client.ClientProperties;
33import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
alshabib3b1eadc2016-02-01 17:57:00 -080034import org.onlab.packet.Ethernet;
alshabib3b1eadc2016-02-01 17:57:00 -080035import org.onlab.packet.IpAddress;
36import org.onlab.packet.VlanId;
Jonathan Hart28271642016-02-10 16:13:54 -080037import org.onosproject.cfg.ComponentConfigService;
38import org.onosproject.codec.CodecService;
Jonathan Hartc3f84eb2016-02-19 12:44:36 -080039import org.onosproject.codec.JsonCodec;
alshabib772e1582016-06-01 17:50:05 -070040
alshabib3b1eadc2016-02-01 17:57:00 -080041import org.onosproject.core.ApplicationId;
42import org.onosproject.core.CoreService;
43import org.onosproject.net.ConnectPoint;
ke hanf1709e82016-08-12 10:48:17 +080044import org.onosproject.net.config.ConfigFactory;
45import org.onosproject.net.config.NetworkConfigEvent;
46import org.onosproject.net.config.NetworkConfigListener;
47import org.onosproject.net.config.NetworkConfigRegistry;
48import org.onosproject.net.config.basics.SubjectFactories;
alshabib3b1eadc2016-02-01 17:57:00 -080049import org.onosproject.net.flow.DefaultTrafficSelector;
50import org.onosproject.net.flow.DefaultTrafficTreatment;
51import org.onosproject.net.flow.TrafficSelector;
52import org.onosproject.net.flowobjective.DefaultForwardingObjective;
53import org.onosproject.net.flowobjective.DefaultNextObjective;
54import org.onosproject.net.flowobjective.FlowObjectiveService;
55import org.onosproject.net.flowobjective.ForwardingObjective;
56import org.onosproject.net.flowobjective.NextObjective;
57import org.onosproject.net.flowobjective.Objective;
58import org.onosproject.net.flowobjective.ObjectiveContext;
59import org.onosproject.net.flowobjective.ObjectiveError;
alshabib3b1eadc2016-02-01 17:57:00 -080060import org.onosproject.net.mcast.McastEvent;
61import org.onosproject.net.mcast.McastListener;
Jonathan Hart28271642016-02-10 16:13:54 -080062import org.onosproject.net.mcast.McastRoute;
alshabib3b1eadc2016-02-01 17:57:00 -080063import org.onosproject.net.mcast.McastRouteInfo;
64import org.onosproject.net.mcast.MulticastRouteService;
Jonathan Hart28271642016-02-10 16:13:54 -080065import org.onosproject.rest.AbstractWebResource;
alshabib772e1582016-06-01 17:50:05 -070066import org.opencord.cordconfig.access.AccessAgentData;
67import org.opencord.cordconfig.access.AccessDeviceData;
Charles Chane8ed8ee2016-06-13 16:37:01 -070068import org.opencord.cordconfig.CordConfigService;
Jonathan Hart28271642016-02-10 16:13:54 -080069import org.osgi.service.component.ComponentContext;
alshabib3b1eadc2016-02-01 17:57:00 -080070import org.slf4j.Logger;
ke hanf1709e82016-08-12 10:48:17 +080071import org.onosproject.incubator.net.config.basics.McastConfig;
alshabib3b1eadc2016-02-01 17:57:00 -080072
Jian Li46472d72016-03-09 10:52:49 -080073import javax.ws.rs.ProcessingException;
74import javax.ws.rs.client.Client;
75import javax.ws.rs.client.ClientBuilder;
76import javax.ws.rs.client.Entity;
77import javax.ws.rs.client.Invocation;
78import javax.ws.rs.client.WebTarget;
Jonathan Hartc3f84eb2016-02-19 12:44:36 -080079import javax.ws.rs.core.MediaType;
80import java.io.IOException;
Jonathan Hart28271642016-02-10 16:13:54 -080081import java.util.Dictionary;
Jonathan Hartc3f84eb2016-02-19 12:44:36 -080082import java.util.List;
alshabib3b1eadc2016-02-01 17:57:00 -080083import java.util.Map;
Jonathan Hart0c194962016-05-23 17:08:15 -070084import java.util.Optional;
alshabibfc1cb032016-02-17 15:37:56 -080085import java.util.Properties;
Jonathan Hart28271642016-02-10 16:13:54 -080086import java.util.concurrent.atomic.AtomicBoolean;
alshabib3b1eadc2016-02-01 17:57:00 -080087
Jonathan Hartc3f84eb2016-02-19 12:44:36 -080088import static com.google.common.base.Preconditions.checkNotNull;
alshabibfc1cb032016-02-17 15:37:56 -080089import static com.google.common.base.Strings.isNullOrEmpty;
Jonathan Hart28271642016-02-10 16:13:54 -080090import static com.google.common.net.MediaType.JSON_UTF_8;
alshabibfc1cb032016-02-17 15:37:56 -080091import static org.onlab.util.Tools.get;
alshabib3b1eadc2016-02-01 17:57:00 -080092import static org.slf4j.LoggerFactory.getLogger;
93
94/**
Jonathan Hartc3f84eb2016-02-19 12:44:36 -080095 * CORD multicast provisioning application. Operates by listening to
Jonathan Hart28271642016-02-10 16:13:54 -080096 * events on the multicast rib and provisioning groups to program multicast
alshabib3b1eadc2016-02-01 17:57:00 -080097 * flows on the dataplane.
98 */
99@Component(immediate = true)
100public class CordMcast {
101
Jonathan Hart0c194962016-05-23 17:08:15 -0700102 private final Logger log = getLogger(getClass());
alshabib09069c92016-02-21 14:49:51 -0800103
Jonathan Hart0c194962016-05-23 17:08:15 -0700104 private static final int DEFAULT_REST_TIMEOUT_MS = 1000;
alshabib09069c92016-02-21 14:49:51 -0800105 private static final int DEFAULT_PRIORITY = 500;
alshabib3b1eadc2016-02-01 17:57:00 -0800106 private static final short DEFAULT_MCAST_VLAN = 4000;
alshabibfc1cb032016-02-17 15:37:56 -0800107 private static final String DEFAULT_SYNC_HOST = "10.90.0.8:8181";
108 private static final String DEFAULT_USER = "karaf";
109 private static final String DEFAULT_PASSWORD = "karaf";
alshabib09069c92016-02-21 14:49:51 -0800110 private static final boolean DEFAULT_VLAN_ENABLED = true;
alshabibfc1cb032016-02-17 15:37:56 -0800111
alshabib3b1eadc2016-02-01 17:57:00 -0800112 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
113 protected MulticastRouteService mcastService;
114
115 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
alshabib3b1eadc2016-02-01 17:57:00 -0800116 protected FlowObjectiveService flowObjectiveService;
117
118 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
119 protected CoreService coreService;
120
Jonathan Hart28271642016-02-10 16:13:54 -0800121 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
122 protected CodecService codecService;
123
124 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
125 protected ComponentConfigService componentConfigService;
126
alshabib09069c92016-02-21 14:49:51 -0800127 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Jonathan Hart0c194962016-05-23 17:08:15 -0700128 protected CordConfigService cordConfigService;
alshabib09069c92016-02-21 14:49:51 -0800129
ke hanf1709e82016-08-12 10:48:17 +0800130 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
131 protected NetworkConfigRegistry networkConfig;
132
alshabib3b1eadc2016-02-01 17:57:00 -0800133 protected McastListener listener = new InternalMulticastListener();
ke hanf1709e82016-08-12 10:48:17 +0800134 private InternalNetworkConfigListener configListener =
135 new InternalNetworkConfigListener();
alshabib3b1eadc2016-02-01 17:57:00 -0800136
alshabib3b1eadc2016-02-01 17:57:00 -0800137 //TODO: move this to a ec map
138 private Map<IpAddress, Integer> groups = Maps.newConcurrentMap();
139
alshabib3b1eadc2016-02-01 17:57:00 -0800140 private ApplicationId appId;
ke hanf1709e82016-08-12 10:48:17 +0800141 private ApplicationId coreAppId;
alshabibfc1cb032016-02-17 15:37:56 -0800142 private int mcastVlan = DEFAULT_MCAST_VLAN;
alshabib3b1eadc2016-02-01 17:57:00 -0800143
alshabib09069c92016-02-21 14:49:51 -0800144 @Property(name = "vlanEnabled", boolValue = DEFAULT_VLAN_ENABLED,
145 label = "Use vlan for multicast traffic?")
146 private boolean vlanEnabled = DEFAULT_VLAN_ENABLED;
alshabibfc1cb032016-02-17 15:37:56 -0800147
148 @Property(name = "priority", intValue = DEFAULT_PRIORITY,
149 label = "Priority for multicast rules")
alshabib3b1eadc2016-02-01 17:57:00 -0800150 private int priority = DEFAULT_PRIORITY;
151
alshabibfc1cb032016-02-17 15:37:56 -0800152 @Property(name = "syncHost", value = DEFAULT_SYNC_HOST,
Jonathan Hart28271642016-02-10 16:13:54 -0800153 label = "host:port to synchronize routes to")
alshabibfc1cb032016-02-17 15:37:56 -0800154 private String syncHost = DEFAULT_SYNC_HOST;
Jonathan Hart28271642016-02-10 16:13:54 -0800155
156 @Property(name = "username", value = DEFAULT_USER,
157 label = "Username for REST password authentication")
158 private String user = DEFAULT_USER;
159
160 @Property(name = "password", value = DEFAULT_PASSWORD,
161 label = "Password for REST authentication")
162 private String password = DEFAULT_PASSWORD;
163
164 private String fabricOnosUrl;
ke hanf1709e82016-08-12 10:48:17 +0800165 private static final Class<McastConfig> CORD_MCAST_CONFIG_CLASS =
166 McastConfig.class;
167
168 private ConfigFactory<ApplicationId, McastConfig> cordMcastConfigFactory =
169 new ConfigFactory<ApplicationId, McastConfig>(
170 SubjectFactories.APP_SUBJECT_FACTORY, CORD_MCAST_CONFIG_CLASS, "multicast") {
171 @Override
172 public McastConfig createConfig() {
173 return new McastConfig();
174 }
175 };
Jonathan Hart28271642016-02-10 16:13:54 -0800176
alshabib3b1eadc2016-02-01 17:57:00 -0800177 @Activate
Jonathan Hart435ffc42016-02-19 10:32:05 -0800178 public void activate(ComponentContext context) {
Jonathan Hartc3f84eb2016-02-19 12:44:36 -0800179 componentConfigService.registerProperties(getClass());
Jonathan Hart435ffc42016-02-19 10:32:05 -0800180 modified(context);
181
alshabib3b1eadc2016-02-01 17:57:00 -0800182 appId = coreService.registerApplication("org.onosproject.cordmcast");
ke hanf1709e82016-08-12 10:48:17 +0800183 coreAppId = coreService.registerApplication(CoreService.CORE_APP_NAME);
Jonathan Hart28271642016-02-10 16:13:54 -0800184
Jonathan Hartc3f84eb2016-02-19 12:44:36 -0800185 clearRemoteRoutes();
ke hanf1709e82016-08-12 10:48:17 +0800186 networkConfig.registerConfigFactory(cordMcastConfigFactory);
187 networkConfig.addListener(configListener);
Jonathan Hartc3f84eb2016-02-19 12:44:36 -0800188 mcastService.addListener(listener);
189
alshabib09069c92016-02-21 14:49:51 -0800190 mcastService.getRoutes().stream()
191 .map(r -> new ImmutablePair<>(r, mcastService.fetchSinks(r)))
192 .filter(pair -> pair.getRight() != null && !pair.getRight().isEmpty())
193 .forEach(pair -> pair.getRight().forEach(sink -> provisionGroup(pair.getLeft(),
Jian Li46472d72016-03-09 10:52:49 -0800194 sink)));
Jonathan Hartc3f84eb2016-02-19 12:44:36 -0800195
ke hanf1709e82016-08-12 10:48:17 +0800196 McastConfig config = networkConfig.getConfig(coreAppId, CORD_MCAST_CONFIG_CLASS);
197 if (config != null) {
198 mcastVlan = config.egressVlan().toShort();
199 }
200
alshabib3b1eadc2016-02-01 17:57:00 -0800201 log.info("Started");
202 }
203
204 @Deactivate
205 public void deactivate() {
Jonathan Hartc3f84eb2016-02-19 12:44:36 -0800206 componentConfigService.unregisterProperties(getClass(), false);
alshabib3b1eadc2016-02-01 17:57:00 -0800207 mcastService.removeListener(listener);
ke hanf1709e82016-08-12 10:48:17 +0800208 networkConfig.removeListener(configListener);
alshabib3b1eadc2016-02-01 17:57:00 -0800209 log.info("Stopped");
210 }
211
Jonathan Hart28271642016-02-10 16:13:54 -0800212 @Modified
213 public void modified(ComponentContext context) {
alshabibfc1cb032016-02-17 15:37:56 -0800214 Dictionary<?, ?> properties = context != null ? context.getProperties() : new Properties();
215
alshabibfc1cb032016-02-17 15:37:56 -0800216 try {
217 String s = get(properties, "username");
218 user = isNullOrEmpty(s) ? DEFAULT_USER : s.trim();
219
220 s = get(properties, "password");
221 password = isNullOrEmpty(s) ? DEFAULT_PASSWORD : s.trim();
222
alshabibfc1cb032016-02-17 15:37:56 -0800223 s = get(properties, "vlanEnabled");
alshabib09069c92016-02-21 14:49:51 -0800224 vlanEnabled = isNullOrEmpty(s) ? DEFAULT_VLAN_ENABLED : Boolean.parseBoolean(s.trim());
alshabibfc1cb032016-02-17 15:37:56 -0800225
226 s = get(properties, "priority");
227 priority = isNullOrEmpty(s) ? DEFAULT_PRIORITY : Integer.parseInt(s.trim());
228
Jonathan Hart0212f642016-02-20 11:32:43 -0800229 s = get(properties, "syncHost");
alshabibfc1cb032016-02-17 15:37:56 -0800230 syncHost = isNullOrEmpty(s) ? DEFAULT_SYNC_HOST : s.trim();
231 } catch (Exception e) {
232 user = DEFAULT_USER;
233 password = DEFAULT_PASSWORD;
234 syncHost = DEFAULT_SYNC_HOST;
235 mcastVlan = DEFAULT_MCAST_VLAN;
236 vlanEnabled = false;
237 priority = DEFAULT_PRIORITY;
238 }
Jonathan Hart0212f642016-02-20 11:32:43 -0800239 fabricOnosUrl = createRemoteUrl(syncHost);
240 }
241
242 private static String createRemoteUrl(String remoteHost) {
243 return "http://" + remoteHost + "/onos/v1/mcast";
Jonathan Hart28271642016-02-10 16:13:54 -0800244 }
245
alshabib3b1eadc2016-02-01 17:57:00 -0800246 private class InternalMulticastListener implements McastListener {
247 @Override
248 public void event(McastEvent event) {
Jonathan Hartc3f84eb2016-02-19 12:44:36 -0800249 McastRouteInfo info = event.subject();
alshabib3b1eadc2016-02-01 17:57:00 -0800250 switch (event.type()) {
251 case ROUTE_ADDED:
252 break;
253 case ROUTE_REMOVED:
254 break;
255 case SOURCE_ADDED:
256 break;
257 case SINK_ADDED:
Jonathan Hartc3f84eb2016-02-19 12:44:36 -0800258 if (!info.sink().isPresent()) {
259 log.warn("No sink given after sink added event: {}", info);
260 return;
261 }
262 provisionGroup(info.route(), info.sink().get());
alshabib3b1eadc2016-02-01 17:57:00 -0800263 break;
264 case SINK_REMOVED:
alshabibfc1cb032016-02-17 15:37:56 -0800265 unprovisionGroup(event.subject());
alshabib3b1eadc2016-02-01 17:57:00 -0800266 break;
267 default:
268 log.warn("Unknown mcast event {}", event.type());
269 }
270 }
271 }
272
alshabibfc1cb032016-02-17 15:37:56 -0800273 private void unprovisionGroup(McastRouteInfo info) {
Jonathan Hart718c0452016-02-18 15:56:22 -0800274 if (info.sinks().isEmpty()) {
Jonathan Hartc3f84eb2016-02-19 12:44:36 -0800275 removeRemoteRoute(info.route());
Jonathan Hart718c0452016-02-18 15:56:22 -0800276 }
277
alshabibfc1cb032016-02-17 15:37:56 -0800278 if (!info.sink().isPresent()) {
279 log.warn("No sink given after sink removed event: {}", info);
280 return;
281 }
282 ConnectPoint loc = info.sink().get();
283
284 NextObjective next = DefaultNextObjective.builder()
285 .fromApp(appId)
286 .addTreatment(DefaultTrafficTreatment.builder().setOutput(loc.port()).build())
287 .withType(NextObjective.Type.BROADCAST)
288 .withId(groups.get(info.route().group()))
289 .removeFromExisting(new ObjectiveContext() {
290 @Override
291 public void onSuccess(Objective objective) {
292 //TODO: change to debug
293 log.info("Next Objective {} installed", objective.id());
294 }
295
296 @Override
297 public void onError(Objective objective, ObjectiveError error) {
298 //TODO: change to debug
299 log.info("Next Objective {} failed, because {}",
Jian Li46472d72016-03-09 10:52:49 -0800300 objective.id(),
301 error);
alshabibfc1cb032016-02-17 15:37:56 -0800302 }
303 });
304
305 flowObjectiveService.next(loc.deviceId(), next);
alshabibfc1cb032016-02-17 15:37:56 -0800306 }
307
Jonathan Hartc3f84eb2016-02-19 12:44:36 -0800308 private void provisionGroup(McastRoute route, ConnectPoint sink) {
309 checkNotNull(route, "Route cannot be null");
310 checkNotNull(sink, "Sink cannot be null");
alshabib3b1eadc2016-02-01 17:57:00 -0800311
Jonathan Hart0c194962016-05-23 17:08:15 -0700312 Optional<AccessDeviceData> oltInfo = cordConfigService.getAccessDevice(sink.deviceId());
alshabib09069c92016-02-21 14:49:51 -0800313
Jonathan Hart0c194962016-05-23 17:08:15 -0700314 if (!oltInfo.isPresent()) {
alshabib09069c92016-02-21 14:49:51 -0800315 log.warn("Unknown OLT device : {}", sink.deviceId());
316 return;
317 }
318
Jonathan Hart28271642016-02-10 16:13:54 -0800319 final AtomicBoolean sync = new AtomicBoolean(false);
alshabib3b1eadc2016-02-01 17:57:00 -0800320
Jonathan Hartc3f84eb2016-02-19 12:44:36 -0800321 Integer nextId = groups.computeIfAbsent(route.group(), (g) -> {
Zsolt Harasztiab436262016-02-25 09:39:10 -0800322 Integer id = flowObjectiveService.allocateNextId();
alshabib3b1eadc2016-02-01 17:57:00 -0800323
alshabibfc1cb032016-02-17 15:37:56 -0800324 NextObjective next = DefaultNextObjective.builder()
325 .fromApp(appId)
Jonathan Hartc3f84eb2016-02-19 12:44:36 -0800326 .addTreatment(DefaultTrafficTreatment.builder().setOutput(sink.port()).build())
alshabibfc1cb032016-02-17 15:37:56 -0800327 .withType(NextObjective.Type.BROADCAST)
328 .withId(id)
329 .add(new ObjectiveContext() {
330 @Override
331 public void onSuccess(Objective objective) {
332 //TODO: change to debug
333 log.info("Next Objective {} installed", objective.id());
334 }
335
336 @Override
337 public void onError(Objective objective, ObjectiveError error) {
338 //TODO: change to debug
339 log.info("Next Objective {} failed, because {}",
Jian Li46472d72016-03-09 10:52:49 -0800340 objective.id(),
341 error);
alshabibfc1cb032016-02-17 15:37:56 -0800342 }
343 });
344
Jonathan Hartc3f84eb2016-02-19 12:44:36 -0800345 flowObjectiveService.next(sink.deviceId(), next);
alshabibfc1cb032016-02-17 15:37:56 -0800346
347 TrafficSelector.Builder mcast = DefaultTrafficSelector.builder()
Jonathan Hart0c194962016-05-23 17:08:15 -0700348 .matchInPort(oltInfo.get().uplink())
alshabib3b1eadc2016-02-01 17:57:00 -0800349 .matchEthType(Ethernet.TYPE_IPV4)
alshabibfc1cb032016-02-17 15:37:56 -0800350 .matchIPDst(g.toIpPrefix());
351
alshabibfc1cb032016-02-17 15:37:56 -0800352 if (vlanEnabled) {
353 mcast.matchVlanId(VlanId.vlanId((short) mcastVlan));
354 }
alshabib3b1eadc2016-02-01 17:57:00 -0800355
alshabib3b1eadc2016-02-01 17:57:00 -0800356 ForwardingObjective fwd = DefaultForwardingObjective.builder()
357 .fromApp(appId)
358 .nextStep(id)
359 .makePermanent()
360 .withFlag(ForwardingObjective.Flag.VERSATILE)
361 .withPriority(priority)
alshabibfc1cb032016-02-17 15:37:56 -0800362 .withSelector(mcast.build())
alshabib3b1eadc2016-02-01 17:57:00 -0800363 .add(new ObjectiveContext() {
364 @Override
365 public void onSuccess(Objective objective) {
366 //TODO: change to debug
367 log.info("Forwarding objective installed {}", objective);
368 }
369
370 @Override
371 public void onError(Objective objective, ObjectiveError error) {
372 //TODO: change to debug
373 log.info("Forwarding objective failed {}", objective);
374 }
375 });
376
Jonathan Hartc3f84eb2016-02-19 12:44:36 -0800377 flowObjectiveService.forward(sink.deviceId(), fwd);
alshabib3b1eadc2016-02-01 17:57:00 -0800378
Jonathan Hart28271642016-02-10 16:13:54 -0800379 sync.set(true);
380
alshabib09069c92016-02-21 14:49:51 -0800381 return id;
alshabib3b1eadc2016-02-01 17:57:00 -0800382 });
383
alshabibfc1cb032016-02-17 15:37:56 -0800384 if (!sync.get()) {
385 NextObjective next = DefaultNextObjective.builder()
386 .fromApp(appId)
Jonathan Hartc3f84eb2016-02-19 12:44:36 -0800387 .addTreatment(DefaultTrafficTreatment.builder().setOutput(sink.port()).build())
alshabibfc1cb032016-02-17 15:37:56 -0800388 .withType(NextObjective.Type.BROADCAST)
389 .withId(nextId)
390 .addToExisting(new ObjectiveContext() {
391 @Override
392 public void onSuccess(Objective objective) {
393 //TODO: change to debug
394 log.info("Next Objective {} installed", objective.id());
395 }
alshabib3b1eadc2016-02-01 17:57:00 -0800396
alshabibfc1cb032016-02-17 15:37:56 -0800397 @Override
398 public void onError(Objective objective, ObjectiveError error) {
399 //TODO: change to debug
400 log.info("Next Objective {} failed, because {}",
Jian Li46472d72016-03-09 10:52:49 -0800401 objective.id(),
402 error);
alshabibfc1cb032016-02-17 15:37:56 -0800403 }
404 });
alshabib3b1eadc2016-02-01 17:57:00 -0800405
Jonathan Hartc3f84eb2016-02-19 12:44:36 -0800406 flowObjectiveService.next(sink.deviceId(), next);
alshabibfc1cb032016-02-17 15:37:56 -0800407 }
Jonathan Hart28271642016-02-10 16:13:54 -0800408
Jonathan Hart0c194962016-05-23 17:08:15 -0700409 addRemoteRoute(route, sink);
alshabib3b1eadc2016-02-01 17:57:00 -0800410 }
411
Jonathan Hart0c194962016-05-23 17:08:15 -0700412 private void addRemoteRoute(McastRoute route, ConnectPoint inPort) {
Jonathan Hartc3f84eb2016-02-19 12:44:36 -0800413 checkNotNull(route);
Jonathan Hart28271642016-02-10 16:13:54 -0800414 if (syncHost == null) {
415 log.warn("No host configured for synchronization; route will be dropped");
416 return;
417 }
418
Jonathan Hart0c194962016-05-23 17:08:15 -0700419 Optional<AccessAgentData> accessAgent = cordConfigService.getAccessAgent(inPort.deviceId());
420 if (!accessAgent.isPresent()) {
421 log.warn("No accessAgent config found for in port {}", inPort);
422 return;
423 }
424
425 if (!accessAgent.get().getOltConnectPoint(inPort).isPresent()) {
426 log.warn("No OLT configured for in port {}", inPort);
427 return;
428 }
429
430 ConnectPoint oltConnectPoint = accessAgent.get().getOltConnectPoint(inPort).get();
431
Jonathan Hart0212f642016-02-20 11:32:43 -0800432 log.debug("Sending route {} to other ONOS {}", route, fabricOnosUrl);
Jonathan Hart28271642016-02-10 16:13:54 -0800433
Jian Li46472d72016-03-09 10:52:49 -0800434 Invocation.Builder builder = getClientBuilder(fabricOnosUrl);
Jonathan Hart28271642016-02-10 16:13:54 -0800435
436 ObjectNode json = codecService.getCodec(McastRoute.class)
Jonathan Hartc3f84eb2016-02-19 12:44:36 -0800437 .encode(route, new AbstractWebResource());
Jonathan Hart0212f642016-02-20 11:32:43 -0800438
439 try {
Jian Li46472d72016-03-09 10:52:49 -0800440 builder.post(Entity.json(json.toString()));
Jonathan Hart0c194962016-05-23 17:08:15 -0700441
442 builder = getClientBuilder(fabricOnosUrl + "/sinks/" + route.group() + "/" + route.source());
443 ObjectMapper mapper = new ObjectMapper();
444 ObjectNode obj = mapper.createObjectNode();
445 obj.putArray("sinks").add(oltConnectPoint.deviceId() + "/" + oltConnectPoint.port());
446
447 builder.post(Entity.json(obj.toString()));
Jian Li46472d72016-03-09 10:52:49 -0800448 } catch (ProcessingException e) {
Jonathan Hart0212f642016-02-20 11:32:43 -0800449 log.warn("Unable to send route to remote controller: {}", e.getMessage());
450 }
alshabib3b1eadc2016-02-01 17:57:00 -0800451 }
Jonathan Hart28271642016-02-10 16:13:54 -0800452
Jonathan Hartc3f84eb2016-02-19 12:44:36 -0800453 private void removeRemoteRoute(McastRoute route) {
Jonathan Hart718c0452016-02-18 15:56:22 -0800454 if (syncHost == null) {
455 log.warn("No host configured for synchronization; route will be dropped");
456 return;
457 }
458
Jonathan Hart0212f642016-02-20 11:32:43 -0800459 log.debug("Removing route {} from other ONOS {}", route, fabricOnosUrl);
Jonathan Hart718c0452016-02-18 15:56:22 -0800460
Jian Li46472d72016-03-09 10:52:49 -0800461 Invocation.Builder builder = getClientBuilder(fabricOnosUrl)
462 .property(ClientProperties.SUPPRESS_HTTP_COMPLIANCE_VALIDATION, true);
Jonathan Hart718c0452016-02-18 15:56:22 -0800463
464 ObjectNode json = codecService.getCodec(McastRoute.class)
Jonathan Hartc3f84eb2016-02-19 12:44:36 -0800465 .encode(route, new AbstractWebResource());
Jian Li46472d72016-03-09 10:52:49 -0800466
467 builder.method("DELETE", Entity.entity(json.asText(),
468 MediaType.APPLICATION_OCTET_STREAM));
Jonathan Hart718c0452016-02-18 15:56:22 -0800469 }
470
Jonathan Hartc3f84eb2016-02-19 12:44:36 -0800471 private void clearRemoteRoutes() {
472 if (syncHost == null) {
473 log.warn("No host configured for synchronization");
474 return;
475 }
476
Jonathan Hart0212f642016-02-20 11:32:43 -0800477 log.debug("Clearing remote multicast routes from {}", fabricOnosUrl);
Jonathan Hartc3f84eb2016-02-19 12:44:36 -0800478
Jian Li46472d72016-03-09 10:52:49 -0800479 Invocation.Builder builder = getClientBuilder(fabricOnosUrl);
Jonathan Hartc3f84eb2016-02-19 12:44:36 -0800480 List<McastRoute> mcastRoutes = Lists.newArrayList();
Jonathan Hart0212f642016-02-20 11:32:43 -0800481
Jonathan Hartc3f84eb2016-02-19 12:44:36 -0800482 try {
Jonathan Hart0212f642016-02-20 11:32:43 -0800483 String response = builder
484 .accept(MediaType.APPLICATION_JSON_TYPE)
485 .get(String.class);
486
487 JsonCodec<McastRoute> routeCodec = codecService.getCodec(McastRoute.class);
488 ObjectMapper mapper = new ObjectMapper();
489
490
Jonathan Hartc3f84eb2016-02-19 12:44:36 -0800491 ObjectNode node = (ObjectNode) mapper.readTree(response);
492 ArrayNode list = (ArrayNode) node.path("routes");
493
494 list.forEach(n -> mcastRoutes.add(
495 routeCodec.decode((ObjectNode) n, new AbstractWebResource())));
Jonathan Hart0212f642016-02-20 11:32:43 -0800496
Jonathan Hart0c194962016-05-23 17:08:15 -0700497 } catch (IOException | ProcessingException e) {
Jonathan Hartc3f84eb2016-02-19 12:44:36 -0800498 log.warn("Error clearing remote routes", e);
499 }
500
501 mcastRoutes.forEach(this::removeRemoteRoute);
502 }
503
Jian Li46472d72016-03-09 10:52:49 -0800504 private Invocation.Builder getClientBuilder(String uri) {
505 ClientConfig config = new ClientConfig();
506 Client client = ClientBuilder.newClient(config);
507
508 client.property(ClientProperties.CONNECT_TIMEOUT, DEFAULT_REST_TIMEOUT_MS);
509 client.property(ClientProperties.READ_TIMEOUT, DEFAULT_REST_TIMEOUT_MS);
510 client.register(HttpAuthenticationFeature.basic(user, password));
511
512 WebTarget wt = client.target(uri);
513 return wt.request(JSON_UTF_8.toString());
Jonathan Hart28271642016-02-10 16:13:54 -0800514 }
515
ke hanf1709e82016-08-12 10:48:17 +0800516 private class InternalNetworkConfigListener implements NetworkConfigListener {
517 @Override
518 public void event(NetworkConfigEvent event) {
519 switch (event.type()) {
520
521 case CONFIG_ADDED:
522 case CONFIG_UPDATED:
523 if (event.configClass().equals(CORD_MCAST_CONFIG_CLASS)) {
524 McastConfig config = networkConfig.getConfig(coreAppId, CORD_MCAST_CONFIG_CLASS);
525 if (config != null) {
526 mcastVlan = config.egressVlan().toShort();
527 }
528 }
529 break;
530 case CONFIG_REGISTERED:
531 case CONFIG_UNREGISTERED:
532 case CONFIG_REMOVED:
533 break;
534 default:
535 break;
536 }
537 }
538 }
alshabib3b1eadc2016-02-01 17:57:00 -0800539}
ke hanf1709e82016-08-12 10:48:17 +0800540