alshabib | 7115cd2 | 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 | 9ad777f | 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 | fcdcb4f | 2016-02-10 16:13:54 -0800 | [diff] [blame] | 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Jonathan Hart | 9ad777f | 2016-02-19 12:44:36 -0800 | [diff] [blame] | 21 | import com.google.common.collect.Lists; |
alshabib | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 22 | import com.google.common.collect.Maps; |
Jonathan Hart | fcdcb4f | 2016-02-10 16:13:54 -0800 | [diff] [blame] | 23 | import com.sun.jersey.api.client.Client; |
Jonathan Hart | 7f4bc52 | 2016-02-20 11:32:43 -0800 | [diff] [blame^] | 24 | import com.sun.jersey.api.client.ClientHandlerException; |
Jonathan Hart | fcdcb4f | 2016-02-10 16:13:54 -0800 | [diff] [blame] | 25 | import com.sun.jersey.api.client.WebResource; |
| 26 | import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter; |
alshabib | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 27 | import org.apache.felix.scr.annotations.Activate; |
| 28 | import org.apache.felix.scr.annotations.Component; |
| 29 | import org.apache.felix.scr.annotations.Deactivate; |
Jonathan Hart | fcdcb4f | 2016-02-10 16:13:54 -0800 | [diff] [blame] | 30 | import org.apache.felix.scr.annotations.Modified; |
| 31 | import org.apache.felix.scr.annotations.Property; |
alshabib | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 32 | import org.apache.felix.scr.annotations.Reference; |
| 33 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 34 | import org.onlab.packet.Ethernet; |
alshabib | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 35 | import org.onlab.packet.IpAddress; |
| 36 | import org.onlab.packet.VlanId; |
Jonathan Hart | fcdcb4f | 2016-02-10 16:13:54 -0800 | [diff] [blame] | 37 | import org.onosproject.cfg.ComponentConfigService; |
| 38 | import org.onosproject.codec.CodecService; |
Jonathan Hart | 9ad777f | 2016-02-19 12:44:36 -0800 | [diff] [blame] | 39 | import org.onosproject.codec.JsonCodec; |
alshabib | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 40 | import org.onosproject.core.ApplicationId; |
| 41 | import org.onosproject.core.CoreService; |
| 42 | import org.onosproject.net.ConnectPoint; |
| 43 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 44 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 45 | import org.onosproject.net.flow.TrafficSelector; |
| 46 | import org.onosproject.net.flowobjective.DefaultForwardingObjective; |
| 47 | import org.onosproject.net.flowobjective.DefaultNextObjective; |
| 48 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
| 49 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 50 | import org.onosproject.net.flowobjective.NextObjective; |
| 51 | import org.onosproject.net.flowobjective.Objective; |
| 52 | import org.onosproject.net.flowobjective.ObjectiveContext; |
| 53 | import org.onosproject.net.flowobjective.ObjectiveError; |
alshabib | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 54 | import org.onosproject.net.mcast.McastEvent; |
| 55 | import org.onosproject.net.mcast.McastListener; |
Jonathan Hart | fcdcb4f | 2016-02-10 16:13:54 -0800 | [diff] [blame] | 56 | import org.onosproject.net.mcast.McastRoute; |
alshabib | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 57 | import org.onosproject.net.mcast.McastRouteInfo; |
| 58 | import org.onosproject.net.mcast.MulticastRouteService; |
Jonathan Hart | fcdcb4f | 2016-02-10 16:13:54 -0800 | [diff] [blame] | 59 | import org.onosproject.rest.AbstractWebResource; |
| 60 | import org.osgi.service.component.ComponentContext; |
alshabib | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 61 | import org.slf4j.Logger; |
| 62 | |
Jonathan Hart | 9ad777f | 2016-02-19 12:44:36 -0800 | [diff] [blame] | 63 | import javax.ws.rs.core.MediaType; |
| 64 | import java.io.IOException; |
Jonathan Hart | fcdcb4f | 2016-02-10 16:13:54 -0800 | [diff] [blame] | 65 | import java.util.Dictionary; |
Jonathan Hart | 9ad777f | 2016-02-19 12:44:36 -0800 | [diff] [blame] | 66 | import java.util.List; |
alshabib | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 67 | import java.util.Map; |
alshabib | 1aa5814 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 68 | import java.util.Properties; |
Jonathan Hart | 9ad777f | 2016-02-19 12:44:36 -0800 | [diff] [blame] | 69 | import java.util.Set; |
Jonathan Hart | fcdcb4f | 2016-02-10 16:13:54 -0800 | [diff] [blame] | 70 | import java.util.concurrent.atomic.AtomicBoolean; |
alshabib | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 71 | import java.util.concurrent.atomic.AtomicInteger; |
| 72 | |
Jonathan Hart | 9ad777f | 2016-02-19 12:44:36 -0800 | [diff] [blame] | 73 | import static com.google.common.base.Preconditions.checkNotNull; |
alshabib | 1aa5814 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 74 | import static com.google.common.base.Strings.isNullOrEmpty; |
Jonathan Hart | fcdcb4f | 2016-02-10 16:13:54 -0800 | [diff] [blame] | 75 | import static com.google.common.net.MediaType.JSON_UTF_8; |
alshabib | 1aa5814 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 76 | import static org.onlab.util.Tools.get; |
alshabib | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 77 | import static org.slf4j.LoggerFactory.getLogger; |
| 78 | |
| 79 | /** |
Jonathan Hart | 9ad777f | 2016-02-19 12:44:36 -0800 | [diff] [blame] | 80 | * CORD multicast provisioning application. Operates by listening to |
Jonathan Hart | fcdcb4f | 2016-02-10 16:13:54 -0800 | [diff] [blame] | 81 | * events on the multicast rib and provisioning groups to program multicast |
alshabib | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 82 | * flows on the dataplane. |
| 83 | */ |
| 84 | @Component(immediate = true) |
| 85 | public class CordMcast { |
| 86 | |
Jonathan Hart | 7f4bc52 | 2016-02-20 11:32:43 -0800 | [diff] [blame^] | 87 | private static final int DEFAULT_REST_TIMEOUT_MS = 2000; |
alshabib | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 88 | private static final int DEFAULT_PRIORITY = 1000; |
| 89 | private static final short DEFAULT_MCAST_VLAN = 4000; |
alshabib | 1aa5814 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 90 | private static final String DEFAULT_SYNC_HOST = "10.90.0.8:8181"; |
| 91 | private static final String DEFAULT_USER = "karaf"; |
| 92 | private static final String DEFAULT_PASSWORD = "karaf"; |
| 93 | |
alshabib | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 94 | private final Logger log = getLogger(getClass()); |
| 95 | |
| 96 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 97 | protected MulticastRouteService mcastService; |
| 98 | |
| 99 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
alshabib | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 100 | protected FlowObjectiveService flowObjectiveService; |
| 101 | |
| 102 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 103 | protected CoreService coreService; |
| 104 | |
Jonathan Hart | fcdcb4f | 2016-02-10 16:13:54 -0800 | [diff] [blame] | 105 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 106 | protected CodecService codecService; |
| 107 | |
| 108 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 109 | protected ComponentConfigService componentConfigService; |
| 110 | |
alshabib | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 111 | protected McastListener listener = new InternalMulticastListener(); |
| 112 | |
alshabib | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 113 | //TODO: move this to a ec map |
| 114 | private Map<IpAddress, Integer> groups = Maps.newConcurrentMap(); |
| 115 | |
| 116 | //TODO: move this to distributed atomic long |
| 117 | private AtomicInteger channels = new AtomicInteger(0); |
| 118 | |
| 119 | private ApplicationId appId; |
| 120 | |
alshabib | 1aa5814 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 121 | @Property(name = "mcastVlan", intValue = DEFAULT_MCAST_VLAN, |
| 122 | label = "VLAN for multicast traffic") |
| 123 | private int mcastVlan = DEFAULT_MCAST_VLAN; |
alshabib | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 124 | |
alshabib | 1aa5814 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 125 | @Property(name = "vlanEnabled", boolValue = false, |
| 126 | label = "Use vlan for multicast traffic") |
| 127 | private boolean vlanEnabled = false; |
| 128 | |
| 129 | @Property(name = "priority", intValue = DEFAULT_PRIORITY, |
| 130 | label = "Priority for multicast rules") |
alshabib | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 131 | private int priority = DEFAULT_PRIORITY; |
| 132 | |
alshabib | 1aa5814 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 133 | @Property(name = "syncHost", value = DEFAULT_SYNC_HOST, |
Jonathan Hart | fcdcb4f | 2016-02-10 16:13:54 -0800 | [diff] [blame] | 134 | label = "host:port to synchronize routes to") |
alshabib | 1aa5814 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 135 | private String syncHost = DEFAULT_SYNC_HOST; |
Jonathan Hart | fcdcb4f | 2016-02-10 16:13:54 -0800 | [diff] [blame] | 136 | |
| 137 | @Property(name = "username", value = DEFAULT_USER, |
| 138 | label = "Username for REST password authentication") |
| 139 | private String user = DEFAULT_USER; |
| 140 | |
| 141 | @Property(name = "password", value = DEFAULT_PASSWORD, |
| 142 | label = "Password for REST authentication") |
| 143 | private String password = DEFAULT_PASSWORD; |
| 144 | |
| 145 | private String fabricOnosUrl; |
| 146 | |
alshabib | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 147 | @Activate |
Jonathan Hart | 81d7310 | 2016-02-19 10:32:05 -0800 | [diff] [blame] | 148 | public void activate(ComponentContext context) { |
Jonathan Hart | 9ad777f | 2016-02-19 12:44:36 -0800 | [diff] [blame] | 149 | componentConfigService.registerProperties(getClass()); |
Jonathan Hart | 81d7310 | 2016-02-19 10:32:05 -0800 | [diff] [blame] | 150 | modified(context); |
| 151 | |
alshabib | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 152 | appId = coreService.registerApplication("org.onosproject.cordmcast"); |
Jonathan Hart | fcdcb4f | 2016-02-10 16:13:54 -0800 | [diff] [blame] | 153 | |
Jonathan Hart | 9ad777f | 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 | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 165 | log.info("Started"); |
| 166 | } |
| 167 | |
| 168 | @Deactivate |
| 169 | public void deactivate() { |
Jonathan Hart | 9ad777f | 2016-02-19 12:44:36 -0800 | [diff] [blame] | 170 | componentConfigService.unregisterProperties(getClass(), false); |
alshabib | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 171 | mcastService.removeListener(listener); |
| 172 | log.info("Stopped"); |
| 173 | } |
| 174 | |
Jonathan Hart | fcdcb4f | 2016-02-10 16:13:54 -0800 | [diff] [blame] | 175 | @Modified |
| 176 | public void modified(ComponentContext context) { |
alshabib | 1aa5814 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 177 | Dictionary<?, ?> properties = context != null ? context.getProperties() : new Properties(); |
| 178 | |
alshabib | 1aa5814 | 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 | |
Jonathan Hart | 7f4bc52 | 2016-02-20 11:32:43 -0800 | [diff] [blame^] | 195 | s = get(properties, "syncHost"); |
alshabib | 1aa5814 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 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 | 7f4bc52 | 2016-02-20 11:32:43 -0800 | [diff] [blame^] | 205 | fabricOnosUrl = createRemoteUrl(syncHost); |
| 206 | } |
| 207 | |
| 208 | private static String createRemoteUrl(String remoteHost) { |
| 209 | return "http://" + remoteHost + "/onos/v1/mcast"; |
Jonathan Hart | fcdcb4f | 2016-02-10 16:13:54 -0800 | [diff] [blame] | 210 | } |
| 211 | |
alshabib | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 212 | private class InternalMulticastListener implements McastListener { |
| 213 | @Override |
| 214 | public void event(McastEvent event) { |
Jonathan Hart | 9ad777f | 2016-02-19 12:44:36 -0800 | [diff] [blame] | 215 | McastRouteInfo info = event.subject(); |
alshabib | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 216 | switch (event.type()) { |
| 217 | case ROUTE_ADDED: |
| 218 | break; |
| 219 | case ROUTE_REMOVED: |
| 220 | break; |
| 221 | case SOURCE_ADDED: |
| 222 | break; |
| 223 | case SINK_ADDED: |
Jonathan Hart | 9ad777f | 2016-02-19 12:44:36 -0800 | [diff] [blame] | 224 | if (!info.sink().isPresent()) { |
| 225 | log.warn("No sink given after sink added event: {}", info); |
| 226 | return; |
| 227 | } |
| 228 | provisionGroup(info.route(), info.sink().get()); |
alshabib | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 229 | break; |
| 230 | case SINK_REMOVED: |
alshabib | 1aa5814 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 231 | unprovisionGroup(event.subject()); |
alshabib | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 232 | break; |
| 233 | default: |
| 234 | log.warn("Unknown mcast event {}", event.type()); |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | |
alshabib | 1aa5814 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 239 | private void unprovisionGroup(McastRouteInfo info) { |
Jonathan Hart | dd7e3d5 | 2016-02-18 15:56:22 -0800 | [diff] [blame] | 240 | if (info.sinks().isEmpty()) { |
Jonathan Hart | 9ad777f | 2016-02-19 12:44:36 -0800 | [diff] [blame] | 241 | removeRemoteRoute(info.route()); |
Jonathan Hart | dd7e3d5 | 2016-02-18 15:56:22 -0800 | [diff] [blame] | 242 | } |
| 243 | |
alshabib | 1aa5814 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 244 | if (!info.sink().isPresent()) { |
| 245 | log.warn("No sink given after sink removed event: {}", info); |
| 246 | return; |
| 247 | } |
| 248 | ConnectPoint loc = info.sink().get(); |
| 249 | |
| 250 | NextObjective next = DefaultNextObjective.builder() |
| 251 | .fromApp(appId) |
| 252 | .addTreatment(DefaultTrafficTreatment.builder().setOutput(loc.port()).build()) |
| 253 | .withType(NextObjective.Type.BROADCAST) |
| 254 | .withId(groups.get(info.route().group())) |
| 255 | .removeFromExisting(new ObjectiveContext() { |
| 256 | @Override |
| 257 | public void onSuccess(Objective objective) { |
| 258 | //TODO: change to debug |
| 259 | log.info("Next Objective {} installed", objective.id()); |
| 260 | } |
| 261 | |
| 262 | @Override |
| 263 | public void onError(Objective objective, ObjectiveError error) { |
| 264 | //TODO: change to debug |
| 265 | log.info("Next Objective {} failed, because {}", |
| 266 | objective.id(), |
| 267 | error); |
| 268 | } |
| 269 | }); |
| 270 | |
| 271 | flowObjectiveService.next(loc.deviceId(), next); |
alshabib | 1aa5814 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 272 | } |
| 273 | |
Jonathan Hart | 9ad777f | 2016-02-19 12:44:36 -0800 | [diff] [blame] | 274 | private void provisionGroup(McastRoute route, ConnectPoint sink) { |
| 275 | checkNotNull(route, "Route cannot be null"); |
| 276 | checkNotNull(sink, "Sink cannot be null"); |
alshabib | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 277 | |
Jonathan Hart | fcdcb4f | 2016-02-10 16:13:54 -0800 | [diff] [blame] | 278 | final AtomicBoolean sync = new AtomicBoolean(false); |
alshabib | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 279 | |
Jonathan Hart | 9ad777f | 2016-02-19 12:44:36 -0800 | [diff] [blame] | 280 | Integer nextId = groups.computeIfAbsent(route.group(), (g) -> { |
Jonathan Hart | fcdcb4f | 2016-02-10 16:13:54 -0800 | [diff] [blame] | 281 | Integer id = allocateId(); |
alshabib | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 282 | |
alshabib | 1aa5814 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 283 | NextObjective next = DefaultNextObjective.builder() |
| 284 | .fromApp(appId) |
Jonathan Hart | 9ad777f | 2016-02-19 12:44:36 -0800 | [diff] [blame] | 285 | .addTreatment(DefaultTrafficTreatment.builder().setOutput(sink.port()).build()) |
alshabib | 1aa5814 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 286 | .withType(NextObjective.Type.BROADCAST) |
| 287 | .withId(id) |
| 288 | .add(new ObjectiveContext() { |
| 289 | @Override |
| 290 | public void onSuccess(Objective objective) { |
| 291 | //TODO: change to debug |
| 292 | log.info("Next Objective {} installed", objective.id()); |
| 293 | } |
| 294 | |
| 295 | @Override |
| 296 | public void onError(Objective objective, ObjectiveError error) { |
| 297 | //TODO: change to debug |
| 298 | log.info("Next Objective {} failed, because {}", |
| 299 | objective.id(), |
| 300 | error); |
| 301 | } |
| 302 | }); |
| 303 | |
Jonathan Hart | 9ad777f | 2016-02-19 12:44:36 -0800 | [diff] [blame] | 304 | flowObjectiveService.next(sink.deviceId(), next); |
alshabib | 1aa5814 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 305 | |
| 306 | TrafficSelector.Builder mcast = DefaultTrafficSelector.builder() |
alshabib | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 307 | .matchEthType(Ethernet.TYPE_IPV4) |
alshabib | 1aa5814 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 308 | .matchIPDst(g.toIpPrefix()); |
| 309 | |
alshabib | 1aa5814 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 310 | if (vlanEnabled) { |
| 311 | mcast.matchVlanId(VlanId.vlanId((short) mcastVlan)); |
| 312 | } |
alshabib | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 313 | |
alshabib | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 314 | ForwardingObjective fwd = DefaultForwardingObjective.builder() |
| 315 | .fromApp(appId) |
| 316 | .nextStep(id) |
| 317 | .makePermanent() |
| 318 | .withFlag(ForwardingObjective.Flag.VERSATILE) |
| 319 | .withPriority(priority) |
alshabib | 1aa5814 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 320 | .withSelector(mcast.build()) |
alshabib | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 321 | .add(new ObjectiveContext() { |
| 322 | @Override |
| 323 | public void onSuccess(Objective objective) { |
| 324 | //TODO: change to debug |
| 325 | log.info("Forwarding objective installed {}", objective); |
| 326 | } |
| 327 | |
| 328 | @Override |
| 329 | public void onError(Objective objective, ObjectiveError error) { |
| 330 | //TODO: change to debug |
| 331 | log.info("Forwarding objective failed {}", objective); |
| 332 | } |
| 333 | }); |
| 334 | |
Jonathan Hart | 9ad777f | 2016-02-19 12:44:36 -0800 | [diff] [blame] | 335 | flowObjectiveService.forward(sink.deviceId(), fwd); |
alshabib | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 336 | |
Jonathan Hart | fcdcb4f | 2016-02-10 16:13:54 -0800 | [diff] [blame] | 337 | sync.set(true); |
| 338 | |
alshabib | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 339 | return id; |
| 340 | }); |
| 341 | |
alshabib | 1aa5814 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 342 | if (!sync.get()) { |
| 343 | NextObjective next = DefaultNextObjective.builder() |
| 344 | .fromApp(appId) |
Jonathan Hart | 9ad777f | 2016-02-19 12:44:36 -0800 | [diff] [blame] | 345 | .addTreatment(DefaultTrafficTreatment.builder().setOutput(sink.port()).build()) |
alshabib | 1aa5814 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 346 | .withType(NextObjective.Type.BROADCAST) |
| 347 | .withId(nextId) |
| 348 | .addToExisting(new ObjectiveContext() { |
| 349 | @Override |
| 350 | public void onSuccess(Objective objective) { |
| 351 | //TODO: change to debug |
| 352 | log.info("Next Objective {} installed", objective.id()); |
| 353 | } |
alshabib | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 354 | |
alshabib | 1aa5814 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 355 | @Override |
| 356 | public void onError(Objective objective, ObjectiveError error) { |
| 357 | //TODO: change to debug |
| 358 | log.info("Next Objective {} failed, because {}", |
| 359 | objective.id(), |
| 360 | error); |
| 361 | } |
| 362 | }); |
alshabib | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 363 | |
Jonathan Hart | 9ad777f | 2016-02-19 12:44:36 -0800 | [diff] [blame] | 364 | flowObjectiveService.next(sink.deviceId(), next); |
alshabib | 1aa5814 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 365 | } |
Jonathan Hart | fcdcb4f | 2016-02-10 16:13:54 -0800 | [diff] [blame] | 366 | |
Jonathan Hart | 9ad777f | 2016-02-19 12:44:36 -0800 | [diff] [blame] | 367 | addRemoteRoute(route); |
alshabib | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 368 | } |
| 369 | |
Jonathan Hart | 9ad777f | 2016-02-19 12:44:36 -0800 | [diff] [blame] | 370 | private void addRemoteRoute(McastRoute route) { |
| 371 | checkNotNull(route); |
Jonathan Hart | fcdcb4f | 2016-02-10 16:13:54 -0800 | [diff] [blame] | 372 | if (syncHost == null) { |
| 373 | log.warn("No host configured for synchronization; route will be dropped"); |
| 374 | return; |
| 375 | } |
| 376 | |
Jonathan Hart | 7f4bc52 | 2016-02-20 11:32:43 -0800 | [diff] [blame^] | 377 | log.debug("Sending route {} to other ONOS {}", route, fabricOnosUrl); |
Jonathan Hart | fcdcb4f | 2016-02-10 16:13:54 -0800 | [diff] [blame] | 378 | |
| 379 | WebResource.Builder builder = getClientBuilder(fabricOnosUrl); |
| 380 | |
| 381 | ObjectNode json = codecService.getCodec(McastRoute.class) |
Jonathan Hart | 9ad777f | 2016-02-19 12:44:36 -0800 | [diff] [blame] | 382 | .encode(route, new AbstractWebResource()); |
Jonathan Hart | 7f4bc52 | 2016-02-20 11:32:43 -0800 | [diff] [blame^] | 383 | |
| 384 | try { |
| 385 | builder.post(json.toString()); |
| 386 | } catch (ClientHandlerException e) { |
| 387 | log.warn("Unable to send route to remote controller: {}", e.getMessage()); |
| 388 | } |
alshabib | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 389 | } |
Jonathan Hart | fcdcb4f | 2016-02-10 16:13:54 -0800 | [diff] [blame] | 390 | |
Jonathan Hart | 9ad777f | 2016-02-19 12:44:36 -0800 | [diff] [blame] | 391 | private void removeRemoteRoute(McastRoute route) { |
Jonathan Hart | dd7e3d5 | 2016-02-18 15:56:22 -0800 | [diff] [blame] | 392 | if (syncHost == null) { |
| 393 | log.warn("No host configured for synchronization; route will be dropped"); |
| 394 | return; |
| 395 | } |
| 396 | |
Jonathan Hart | 7f4bc52 | 2016-02-20 11:32:43 -0800 | [diff] [blame^] | 397 | log.debug("Removing route {} from other ONOS {}", route, fabricOnosUrl); |
Jonathan Hart | dd7e3d5 | 2016-02-18 15:56:22 -0800 | [diff] [blame] | 398 | |
| 399 | WebResource.Builder builder = getClientBuilder(fabricOnosUrl); |
| 400 | |
| 401 | ObjectNode json = codecService.getCodec(McastRoute.class) |
Jonathan Hart | 9ad777f | 2016-02-19 12:44:36 -0800 | [diff] [blame] | 402 | .encode(route, new AbstractWebResource()); |
Jonathan Hart | 7f4bc52 | 2016-02-20 11:32:43 -0800 | [diff] [blame^] | 403 | try { |
| 404 | builder.delete(json.toString()); |
| 405 | } catch (ClientHandlerException e) { |
| 406 | log.warn("Unable to delete route from remote controller: {}", e.getMessage()); |
| 407 | } |
Jonathan Hart | dd7e3d5 | 2016-02-18 15:56:22 -0800 | [diff] [blame] | 408 | } |
| 409 | |
Jonathan Hart | 9ad777f | 2016-02-19 12:44:36 -0800 | [diff] [blame] | 410 | private void clearRemoteRoutes() { |
| 411 | if (syncHost == null) { |
| 412 | log.warn("No host configured for synchronization"); |
| 413 | return; |
| 414 | } |
| 415 | |
Jonathan Hart | 7f4bc52 | 2016-02-20 11:32:43 -0800 | [diff] [blame^] | 416 | log.debug("Clearing remote multicast routes from {}", fabricOnosUrl); |
Jonathan Hart | 9ad777f | 2016-02-19 12:44:36 -0800 | [diff] [blame] | 417 | |
| 418 | WebResource.Builder builder = getClientBuilder(fabricOnosUrl); |
Jonathan Hart | 9ad777f | 2016-02-19 12:44:36 -0800 | [diff] [blame] | 419 | List<McastRoute> mcastRoutes = Lists.newArrayList(); |
Jonathan Hart | 7f4bc52 | 2016-02-20 11:32:43 -0800 | [diff] [blame^] | 420 | |
Jonathan Hart | 9ad777f | 2016-02-19 12:44:36 -0800 | [diff] [blame] | 421 | try { |
Jonathan Hart | 7f4bc52 | 2016-02-20 11:32:43 -0800 | [diff] [blame^] | 422 | String response = builder |
| 423 | .accept(MediaType.APPLICATION_JSON_TYPE) |
| 424 | .get(String.class); |
| 425 | |
| 426 | JsonCodec<McastRoute> routeCodec = codecService.getCodec(McastRoute.class); |
| 427 | ObjectMapper mapper = new ObjectMapper(); |
| 428 | |
| 429 | |
Jonathan Hart | 9ad777f | 2016-02-19 12:44:36 -0800 | [diff] [blame] | 430 | ObjectNode node = (ObjectNode) mapper.readTree(response); |
| 431 | ArrayNode list = (ArrayNode) node.path("routes"); |
| 432 | |
| 433 | list.forEach(n -> mcastRoutes.add( |
| 434 | routeCodec.decode((ObjectNode) n, new AbstractWebResource()))); |
Jonathan Hart | 7f4bc52 | 2016-02-20 11:32:43 -0800 | [diff] [blame^] | 435 | |
| 436 | } catch (ClientHandlerException e) { |
| 437 | log.warn("Unable to clear routes from remote controller: {}", e.getMessage()); |
Jonathan Hart | 9ad777f | 2016-02-19 12:44:36 -0800 | [diff] [blame] | 438 | } catch (IOException e) { |
| 439 | log.warn("Error clearing remote routes", e); |
| 440 | } |
| 441 | |
| 442 | mcastRoutes.forEach(this::removeRemoteRoute); |
| 443 | } |
| 444 | |
Jonathan Hart | fcdcb4f | 2016-02-10 16:13:54 -0800 | [diff] [blame] | 445 | private Integer allocateId() { |
| 446 | return channels.getAndIncrement(); |
| 447 | } |
| 448 | |
| 449 | private WebResource.Builder getClientBuilder(String uri) { |
| 450 | Client client = Client.create(); |
Jonathan Hart | 7f4bc52 | 2016-02-20 11:32:43 -0800 | [diff] [blame^] | 451 | client.setConnectTimeout(DEFAULT_REST_TIMEOUT_MS); |
| 452 | client.setReadTimeout(DEFAULT_REST_TIMEOUT_MS); |
Jonathan Hart | fcdcb4f | 2016-02-10 16:13:54 -0800 | [diff] [blame] | 453 | client.addFilter(new HTTPBasicAuthFilter(user, password)); |
| 454 | WebResource resource = client.resource(uri); |
| 455 | return resource.accept(JSON_UTF_8.toString()) |
| 456 | .type(JSON_UTF_8.toString()); |
| 457 | } |
| 458 | |
alshabib | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 459 | } |