alshabib | 3b1eadc | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.cordmcast; |
| 17 | |
Jonathan Hart | c3f84eb | 2016-02-19 12:44:36 -0800 | [diff] [blame^] | 18 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 19 | import com.fasterxml.jackson.databind.node.ArrayNode; |
Jonathan Hart | 2827164 | 2016-02-10 16:13:54 -0800 | [diff] [blame] | 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Jonathan Hart | c3f84eb | 2016-02-19 12:44:36 -0800 | [diff] [blame^] | 21 | import com.google.common.collect.Lists; |
alshabib | 3b1eadc | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 22 | import com.google.common.collect.Maps; |
Jonathan Hart | 2827164 | 2016-02-10 16:13:54 -0800 | [diff] [blame] | 23 | import com.sun.jersey.api.client.Client; |
| 24 | import com.sun.jersey.api.client.WebResource; |
| 25 | import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter; |
alshabib | 3b1eadc | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 26 | import org.apache.felix.scr.annotations.Activate; |
| 27 | import org.apache.felix.scr.annotations.Component; |
| 28 | import org.apache.felix.scr.annotations.Deactivate; |
Jonathan Hart | 2827164 | 2016-02-10 16:13:54 -0800 | [diff] [blame] | 29 | import org.apache.felix.scr.annotations.Modified; |
| 30 | import org.apache.felix.scr.annotations.Property; |
alshabib | 3b1eadc | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 31 | import org.apache.felix.scr.annotations.Reference; |
| 32 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 33 | import org.onlab.packet.Ethernet; |
alshabib | 3b1eadc | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 34 | import org.onlab.packet.IpAddress; |
| 35 | import org.onlab.packet.VlanId; |
Jonathan Hart | 2827164 | 2016-02-10 16:13:54 -0800 | [diff] [blame] | 36 | import org.onosproject.cfg.ComponentConfigService; |
| 37 | import org.onosproject.codec.CodecService; |
Jonathan Hart | c3f84eb | 2016-02-19 12:44:36 -0800 | [diff] [blame^] | 38 | import org.onosproject.codec.JsonCodec; |
alshabib | 3b1eadc | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 39 | import org.onosproject.core.ApplicationId; |
| 40 | import org.onosproject.core.CoreService; |
| 41 | import org.onosproject.net.ConnectPoint; |
| 42 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 43 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 44 | import org.onosproject.net.flow.TrafficSelector; |
| 45 | import org.onosproject.net.flowobjective.DefaultForwardingObjective; |
| 46 | import org.onosproject.net.flowobjective.DefaultNextObjective; |
| 47 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
| 48 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 49 | import org.onosproject.net.flowobjective.NextObjective; |
| 50 | import org.onosproject.net.flowobjective.Objective; |
| 51 | import org.onosproject.net.flowobjective.ObjectiveContext; |
| 52 | import org.onosproject.net.flowobjective.ObjectiveError; |
alshabib | 3b1eadc | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 53 | import org.onosproject.net.mcast.McastEvent; |
| 54 | import org.onosproject.net.mcast.McastListener; |
Jonathan Hart | 2827164 | 2016-02-10 16:13:54 -0800 | [diff] [blame] | 55 | import org.onosproject.net.mcast.McastRoute; |
alshabib | 3b1eadc | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 56 | import org.onosproject.net.mcast.McastRouteInfo; |
| 57 | import org.onosproject.net.mcast.MulticastRouteService; |
Jonathan Hart | 2827164 | 2016-02-10 16:13:54 -0800 | [diff] [blame] | 58 | import org.onosproject.rest.AbstractWebResource; |
| 59 | import org.osgi.service.component.ComponentContext; |
alshabib | 3b1eadc | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 60 | import org.slf4j.Logger; |
| 61 | |
Jonathan Hart | c3f84eb | 2016-02-19 12:44:36 -0800 | [diff] [blame^] | 62 | import javax.ws.rs.core.MediaType; |
| 63 | import java.io.IOException; |
Jonathan Hart | 2827164 | 2016-02-10 16:13:54 -0800 | [diff] [blame] | 64 | import java.util.Dictionary; |
Jonathan Hart | c3f84eb | 2016-02-19 12:44:36 -0800 | [diff] [blame^] | 65 | import java.util.List; |
alshabib | 3b1eadc | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 66 | import java.util.Map; |
alshabib | fc1cb03 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 67 | import java.util.Properties; |
Jonathan Hart | c3f84eb | 2016-02-19 12:44:36 -0800 | [diff] [blame^] | 68 | import java.util.Set; |
Jonathan Hart | 2827164 | 2016-02-10 16:13:54 -0800 | [diff] [blame] | 69 | import java.util.concurrent.atomic.AtomicBoolean; |
alshabib | 3b1eadc | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 70 | import java.util.concurrent.atomic.AtomicInteger; |
| 71 | |
Jonathan Hart | c3f84eb | 2016-02-19 12:44:36 -0800 | [diff] [blame^] | 72 | import static com.google.common.base.Preconditions.checkNotNull; |
alshabib | fc1cb03 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 73 | import static com.google.common.base.Strings.isNullOrEmpty; |
Jonathan Hart | 2827164 | 2016-02-10 16:13:54 -0800 | [diff] [blame] | 74 | import static com.google.common.net.MediaType.JSON_UTF_8; |
alshabib | fc1cb03 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 75 | import static org.onlab.util.Tools.get; |
alshabib | 3b1eadc | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 76 | import static org.slf4j.LoggerFactory.getLogger; |
| 77 | |
| 78 | /** |
Jonathan Hart | c3f84eb | 2016-02-19 12:44:36 -0800 | [diff] [blame^] | 79 | * CORD multicast provisioning application. Operates by listening to |
Jonathan Hart | 2827164 | 2016-02-10 16:13:54 -0800 | [diff] [blame] | 80 | * events on the multicast rib and provisioning groups to program multicast |
alshabib | 3b1eadc | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 81 | * flows on the dataplane. |
| 82 | */ |
| 83 | @Component(immediate = true) |
| 84 | public class CordMcast { |
| 85 | |
| 86 | private static final int DEFAULT_PRIORITY = 1000; |
| 87 | private static final short DEFAULT_MCAST_VLAN = 4000; |
alshabib | fc1cb03 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 88 | 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 | |
alshabib | 3b1eadc | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 92 | private final Logger log = getLogger(getClass()); |
| 93 | |
| 94 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 95 | protected MulticastRouteService mcastService; |
| 96 | |
| 97 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
alshabib | 3b1eadc | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 98 | protected FlowObjectiveService flowObjectiveService; |
| 99 | |
| 100 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 101 | protected CoreService coreService; |
| 102 | |
Jonathan Hart | 2827164 | 2016-02-10 16:13:54 -0800 | [diff] [blame] | 103 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 104 | protected CodecService codecService; |
| 105 | |
| 106 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 107 | protected ComponentConfigService componentConfigService; |
| 108 | |
alshabib | 3b1eadc | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 109 | protected McastListener listener = new InternalMulticastListener(); |
| 110 | |
alshabib | 3b1eadc | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 111 | //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 | |
alshabib | fc1cb03 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 119 | @Property(name = "mcastVlan", intValue = DEFAULT_MCAST_VLAN, |
| 120 | label = "VLAN for multicast traffic") |
| 121 | private int mcastVlan = DEFAULT_MCAST_VLAN; |
alshabib | 3b1eadc | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 122 | |
alshabib | fc1cb03 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 123 | @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") |
alshabib | 3b1eadc | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 129 | private int priority = DEFAULT_PRIORITY; |
| 130 | |
alshabib | fc1cb03 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 131 | @Property(name = "syncHost", value = DEFAULT_SYNC_HOST, |
Jonathan Hart | 2827164 | 2016-02-10 16:13:54 -0800 | [diff] [blame] | 132 | label = "host:port to synchronize routes to") |
alshabib | fc1cb03 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 133 | private String syncHost = DEFAULT_SYNC_HOST; |
Jonathan Hart | 2827164 | 2016-02-10 16:13:54 -0800 | [diff] [blame] | 134 | |
| 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 | |
alshabib | 3b1eadc | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 145 | @Activate |
Jonathan Hart | 435ffc4 | 2016-02-19 10:32:05 -0800 | [diff] [blame] | 146 | public void activate(ComponentContext context) { |
Jonathan Hart | c3f84eb | 2016-02-19 12:44:36 -0800 | [diff] [blame^] | 147 | componentConfigService.registerProperties(getClass()); |
Jonathan Hart | 435ffc4 | 2016-02-19 10:32:05 -0800 | [diff] [blame] | 148 | modified(context); |
| 149 | |
alshabib | 3b1eadc | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 150 | appId = coreService.registerApplication("org.onosproject.cordmcast"); |
Jonathan Hart | 2827164 | 2016-02-10 16:13:54 -0800 | [diff] [blame] | 151 | |
| 152 | fabricOnosUrl = "http://" + syncHost + "/onos/v1/mcast"; |
| 153 | |
Jonathan Hart | c3f84eb | 2016-02-19 12:44:36 -0800 | [diff] [blame^] | 154 | 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 | |
alshabib | 3b1eadc | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 165 | log.info("Started"); |
| 166 | } |
| 167 | |
| 168 | @Deactivate |
| 169 | public void deactivate() { |
Jonathan Hart | c3f84eb | 2016-02-19 12:44:36 -0800 | [diff] [blame^] | 170 | componentConfigService.unregisterProperties(getClass(), false); |
alshabib | 3b1eadc | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 171 | mcastService.removeListener(listener); |
| 172 | log.info("Stopped"); |
| 173 | } |
| 174 | |
Jonathan Hart | 2827164 | 2016-02-10 16:13:54 -0800 | [diff] [blame] | 175 | @Modified |
| 176 | public void modified(ComponentContext context) { |
alshabib | fc1cb03 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 177 | Dictionary<?, ?> properties = context != null ? context.getProperties() : new Properties(); |
| 178 | |
alshabib | fc1cb03 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 179 | 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 Hart | 2827164 | 2016-02-10 16:13:54 -0800 | [diff] [blame] | 205 | } |
| 206 | |
alshabib | 3b1eadc | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 207 | private class InternalMulticastListener implements McastListener { |
| 208 | @Override |
| 209 | public void event(McastEvent event) { |
Jonathan Hart | c3f84eb | 2016-02-19 12:44:36 -0800 | [diff] [blame^] | 210 | McastRouteInfo info = event.subject(); |
alshabib | 3b1eadc | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 211 | 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 Hart | c3f84eb | 2016-02-19 12:44:36 -0800 | [diff] [blame^] | 219 | 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()); |
alshabib | 3b1eadc | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 224 | break; |
| 225 | case SINK_REMOVED: |
alshabib | fc1cb03 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 226 | unprovisionGroup(event.subject()); |
alshabib | 3b1eadc | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 227 | break; |
| 228 | default: |
| 229 | log.warn("Unknown mcast event {}", event.type()); |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | |
alshabib | fc1cb03 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 234 | private void unprovisionGroup(McastRouteInfo info) { |
Jonathan Hart | 718c045 | 2016-02-18 15:56:22 -0800 | [diff] [blame] | 235 | if (info.sinks().isEmpty()) { |
Jonathan Hart | c3f84eb | 2016-02-19 12:44:36 -0800 | [diff] [blame^] | 236 | removeRemoteRoute(info.route()); |
Jonathan Hart | 718c045 | 2016-02-18 15:56:22 -0800 | [diff] [blame] | 237 | } |
| 238 | |
alshabib | fc1cb03 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 239 | 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); |
alshabib | fc1cb03 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 267 | } |
| 268 | |
Jonathan Hart | c3f84eb | 2016-02-19 12:44:36 -0800 | [diff] [blame^] | 269 | private void provisionGroup(McastRoute route, ConnectPoint sink) { |
| 270 | checkNotNull(route, "Route cannot be null"); |
| 271 | checkNotNull(sink, "Sink cannot be null"); |
alshabib | 3b1eadc | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 272 | |
Jonathan Hart | 2827164 | 2016-02-10 16:13:54 -0800 | [diff] [blame] | 273 | final AtomicBoolean sync = new AtomicBoolean(false); |
alshabib | 3b1eadc | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 274 | |
Jonathan Hart | c3f84eb | 2016-02-19 12:44:36 -0800 | [diff] [blame^] | 275 | Integer nextId = groups.computeIfAbsent(route.group(), (g) -> { |
Jonathan Hart | 2827164 | 2016-02-10 16:13:54 -0800 | [diff] [blame] | 276 | Integer id = allocateId(); |
alshabib | 3b1eadc | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 277 | |
alshabib | fc1cb03 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 278 | NextObjective next = DefaultNextObjective.builder() |
| 279 | .fromApp(appId) |
Jonathan Hart | c3f84eb | 2016-02-19 12:44:36 -0800 | [diff] [blame^] | 280 | .addTreatment(DefaultTrafficTreatment.builder().setOutput(sink.port()).build()) |
alshabib | fc1cb03 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 281 | .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 Hart | c3f84eb | 2016-02-19 12:44:36 -0800 | [diff] [blame^] | 299 | flowObjectiveService.next(sink.deviceId(), next); |
alshabib | fc1cb03 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 300 | |
| 301 | TrafficSelector.Builder mcast = DefaultTrafficSelector.builder() |
alshabib | 3b1eadc | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 302 | .matchEthType(Ethernet.TYPE_IPV4) |
alshabib | fc1cb03 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 303 | .matchIPDst(g.toIpPrefix()); |
| 304 | |
alshabib | fc1cb03 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 305 | if (vlanEnabled) { |
| 306 | mcast.matchVlanId(VlanId.vlanId((short) mcastVlan)); |
| 307 | } |
alshabib | 3b1eadc | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 308 | |
alshabib | 3b1eadc | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 309 | ForwardingObjective fwd = DefaultForwardingObjective.builder() |
| 310 | .fromApp(appId) |
| 311 | .nextStep(id) |
| 312 | .makePermanent() |
| 313 | .withFlag(ForwardingObjective.Flag.VERSATILE) |
| 314 | .withPriority(priority) |
alshabib | fc1cb03 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 315 | .withSelector(mcast.build()) |
alshabib | 3b1eadc | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 316 | .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 Hart | c3f84eb | 2016-02-19 12:44:36 -0800 | [diff] [blame^] | 330 | flowObjectiveService.forward(sink.deviceId(), fwd); |
alshabib | 3b1eadc | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 331 | |
Jonathan Hart | 2827164 | 2016-02-10 16:13:54 -0800 | [diff] [blame] | 332 | sync.set(true); |
| 333 | |
alshabib | 3b1eadc | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 334 | return id; |
| 335 | }); |
| 336 | |
alshabib | fc1cb03 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 337 | if (!sync.get()) { |
| 338 | NextObjective next = DefaultNextObjective.builder() |
| 339 | .fromApp(appId) |
Jonathan Hart | c3f84eb | 2016-02-19 12:44:36 -0800 | [diff] [blame^] | 340 | .addTreatment(DefaultTrafficTreatment.builder().setOutput(sink.port()).build()) |
alshabib | fc1cb03 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 341 | .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 | } |
alshabib | 3b1eadc | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 349 | |
alshabib | fc1cb03 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 350 | @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 | }); |
alshabib | 3b1eadc | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 358 | |
Jonathan Hart | c3f84eb | 2016-02-19 12:44:36 -0800 | [diff] [blame^] | 359 | flowObjectiveService.next(sink.deviceId(), next); |
alshabib | fc1cb03 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 360 | } |
Jonathan Hart | 2827164 | 2016-02-10 16:13:54 -0800 | [diff] [blame] | 361 | |
Jonathan Hart | c3f84eb | 2016-02-19 12:44:36 -0800 | [diff] [blame^] | 362 | addRemoteRoute(route); |
alshabib | 3b1eadc | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 363 | } |
| 364 | |
Jonathan Hart | c3f84eb | 2016-02-19 12:44:36 -0800 | [diff] [blame^] | 365 | private void addRemoteRoute(McastRoute route) { |
| 366 | checkNotNull(route); |
Jonathan Hart | 2827164 | 2016-02-10 16:13:54 -0800 | [diff] [blame] | 367 | if (syncHost == null) { |
| 368 | log.warn("No host configured for synchronization; route will be dropped"); |
| 369 | return; |
| 370 | } |
| 371 | |
Jonathan Hart | c3f84eb | 2016-02-19 12:44:36 -0800 | [diff] [blame^] | 372 | log.debug("Sending route to other ONOS: {}", route); |
Jonathan Hart | 2827164 | 2016-02-10 16:13:54 -0800 | [diff] [blame] | 373 | |
| 374 | WebResource.Builder builder = getClientBuilder(fabricOnosUrl); |
| 375 | |
| 376 | ObjectNode json = codecService.getCodec(McastRoute.class) |
Jonathan Hart | c3f84eb | 2016-02-19 12:44:36 -0800 | [diff] [blame^] | 377 | .encode(route, new AbstractWebResource()); |
Jonathan Hart | 2827164 | 2016-02-10 16:13:54 -0800 | [diff] [blame] | 378 | builder.post(json.toString()); |
alshabib | 3b1eadc | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 379 | } |
Jonathan Hart | 2827164 | 2016-02-10 16:13:54 -0800 | [diff] [blame] | 380 | |
Jonathan Hart | c3f84eb | 2016-02-19 12:44:36 -0800 | [diff] [blame^] | 381 | private void removeRemoteRoute(McastRoute route) { |
Jonathan Hart | 718c045 | 2016-02-18 15:56:22 -0800 | [diff] [blame] | 382 | if (syncHost == null) { |
| 383 | log.warn("No host configured for synchronization; route will be dropped"); |
| 384 | return; |
| 385 | } |
| 386 | |
Jonathan Hart | c3f84eb | 2016-02-19 12:44:36 -0800 | [diff] [blame^] | 387 | log.debug("Removing route from other ONOS: {}", route); |
Jonathan Hart | 718c045 | 2016-02-18 15:56:22 -0800 | [diff] [blame] | 388 | |
| 389 | WebResource.Builder builder = getClientBuilder(fabricOnosUrl); |
| 390 | |
| 391 | ObjectNode json = codecService.getCodec(McastRoute.class) |
Jonathan Hart | c3f84eb | 2016-02-19 12:44:36 -0800 | [diff] [blame^] | 392 | .encode(route, new AbstractWebResource()); |
Jonathan Hart | 718c045 | 2016-02-18 15:56:22 -0800 | [diff] [blame] | 393 | builder.delete(json.toString()); |
| 394 | } |
| 395 | |
Jonathan Hart | c3f84eb | 2016-02-19 12:44:36 -0800 | [diff] [blame^] | 396 | 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 Hart | 2827164 | 2016-02-10 16:13:54 -0800 | [diff] [blame] | 426 | 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 | |
alshabib | 3b1eadc | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 438 | } |