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 | f544c01 | 2016-02-21 14:49:51 -0800 | [diff] [blame] | 27 | import org.apache.commons.lang3.tuple.ImmutablePair; |
alshabib | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 28 | import org.apache.felix.scr.annotations.Activate; |
| 29 | import org.apache.felix.scr.annotations.Component; |
| 30 | import org.apache.felix.scr.annotations.Deactivate; |
Jonathan Hart | fcdcb4f | 2016-02-10 16:13:54 -0800 | [diff] [blame] | 31 | import org.apache.felix.scr.annotations.Modified; |
| 32 | import org.apache.felix.scr.annotations.Property; |
alshabib | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 33 | import org.apache.felix.scr.annotations.Reference; |
| 34 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 35 | import org.onlab.packet.Ethernet; |
alshabib | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 36 | import org.onlab.packet.IpAddress; |
| 37 | import org.onlab.packet.VlanId; |
Jonathan Hart | fcdcb4f | 2016-02-10 16:13:54 -0800 | [diff] [blame] | 38 | import org.onosproject.cfg.ComponentConfigService; |
| 39 | import org.onosproject.codec.CodecService; |
Jonathan Hart | 9ad777f | 2016-02-19 12:44:36 -0800 | [diff] [blame] | 40 | import org.onosproject.codec.JsonCodec; |
alshabib | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 41 | import org.onosproject.core.ApplicationId; |
| 42 | import org.onosproject.core.CoreService; |
| 43 | import org.onosproject.net.ConnectPoint; |
alshabib | f544c01 | 2016-02-21 14:49:51 -0800 | [diff] [blame] | 44 | import org.onosproject.net.DeviceId; |
| 45 | import org.onosproject.net.config.ConfigFactory; |
| 46 | import org.onosproject.net.config.NetworkConfigEvent; |
| 47 | import org.onosproject.net.config.NetworkConfigListener; |
| 48 | import org.onosproject.net.config.NetworkConfigRegistry; |
| 49 | import org.onosproject.net.config.basics.SubjectFactories; |
alshabib | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 50 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 51 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 52 | import org.onosproject.net.flow.TrafficSelector; |
| 53 | import org.onosproject.net.flowobjective.DefaultForwardingObjective; |
| 54 | import org.onosproject.net.flowobjective.DefaultNextObjective; |
| 55 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
| 56 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 57 | import org.onosproject.net.flowobjective.NextObjective; |
| 58 | import org.onosproject.net.flowobjective.Objective; |
| 59 | import org.onosproject.net.flowobjective.ObjectiveContext; |
| 60 | import org.onosproject.net.flowobjective.ObjectiveError; |
alshabib | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 61 | import org.onosproject.net.mcast.McastEvent; |
| 62 | import org.onosproject.net.mcast.McastListener; |
Jonathan Hart | fcdcb4f | 2016-02-10 16:13:54 -0800 | [diff] [blame] | 63 | import org.onosproject.net.mcast.McastRoute; |
alshabib | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 64 | import org.onosproject.net.mcast.McastRouteInfo; |
| 65 | import org.onosproject.net.mcast.MulticastRouteService; |
alshabib | f544c01 | 2016-02-21 14:49:51 -0800 | [diff] [blame] | 66 | import org.onosproject.olt.AccessDeviceConfig; |
| 67 | import org.onosproject.olt.AccessDeviceData; |
Jonathan Hart | fcdcb4f | 2016-02-10 16:13:54 -0800 | [diff] [blame] | 68 | import org.onosproject.rest.AbstractWebResource; |
| 69 | import org.osgi.service.component.ComponentContext; |
alshabib | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 70 | import org.slf4j.Logger; |
| 71 | |
Jonathan Hart | 9ad777f | 2016-02-19 12:44:36 -0800 | [diff] [blame] | 72 | import javax.ws.rs.core.MediaType; |
| 73 | import java.io.IOException; |
Jonathan Hart | fcdcb4f | 2016-02-10 16:13:54 -0800 | [diff] [blame] | 74 | import java.util.Dictionary; |
Jonathan Hart | 9ad777f | 2016-02-19 12:44:36 -0800 | [diff] [blame] | 75 | import java.util.List; |
alshabib | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 76 | import java.util.Map; |
alshabib | 1aa5814 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 77 | import java.util.Properties; |
alshabib | f544c01 | 2016-02-21 14:49:51 -0800 | [diff] [blame] | 78 | import java.util.concurrent.ConcurrentHashMap; |
Jonathan Hart | fcdcb4f | 2016-02-10 16:13:54 -0800 | [diff] [blame] | 79 | import java.util.concurrent.atomic.AtomicBoolean; |
alshabib | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 80 | |
Jonathan Hart | 9ad777f | 2016-02-19 12:44:36 -0800 | [diff] [blame] | 81 | import static com.google.common.base.Preconditions.checkNotNull; |
alshabib | 1aa5814 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 82 | import static com.google.common.base.Strings.isNullOrEmpty; |
Jonathan Hart | fcdcb4f | 2016-02-10 16:13:54 -0800 | [diff] [blame] | 83 | import static com.google.common.net.MediaType.JSON_UTF_8; |
alshabib | 1aa5814 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 84 | import static org.onlab.util.Tools.get; |
alshabib | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 85 | import static org.slf4j.LoggerFactory.getLogger; |
| 86 | |
| 87 | /** |
Jonathan Hart | 9ad777f | 2016-02-19 12:44:36 -0800 | [diff] [blame] | 88 | * CORD multicast provisioning application. Operates by listening to |
Jonathan Hart | fcdcb4f | 2016-02-10 16:13:54 -0800 | [diff] [blame] | 89 | * events on the multicast rib and provisioning groups to program multicast |
alshabib | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 90 | * flows on the dataplane. |
| 91 | */ |
| 92 | @Component(immediate = true) |
| 93 | public class CordMcast { |
| 94 | |
alshabib | f544c01 | 2016-02-21 14:49:51 -0800 | [diff] [blame] | 95 | |
Jonathan Hart | 7f4bc52 | 2016-02-20 11:32:43 -0800 | [diff] [blame] | 96 | private static final int DEFAULT_REST_TIMEOUT_MS = 2000; |
alshabib | f544c01 | 2016-02-21 14:49:51 -0800 | [diff] [blame] | 97 | private static final int DEFAULT_PRIORITY = 500; |
alshabib | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 98 | private static final short DEFAULT_MCAST_VLAN = 4000; |
alshabib | 1aa5814 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 99 | private static final String DEFAULT_SYNC_HOST = "10.90.0.8:8181"; |
| 100 | private static final String DEFAULT_USER = "karaf"; |
| 101 | private static final String DEFAULT_PASSWORD = "karaf"; |
alshabib | f544c01 | 2016-02-21 14:49:51 -0800 | [diff] [blame] | 102 | private static final boolean DEFAULT_VLAN_ENABLED = true; |
alshabib | 1aa5814 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 103 | |
alshabib | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 104 | private final Logger log = getLogger(getClass()); |
| 105 | |
| 106 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 107 | protected MulticastRouteService mcastService; |
| 108 | |
| 109 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
alshabib | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 110 | protected FlowObjectiveService flowObjectiveService; |
| 111 | |
| 112 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 113 | protected CoreService coreService; |
| 114 | |
Jonathan Hart | fcdcb4f | 2016-02-10 16:13:54 -0800 | [diff] [blame] | 115 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 116 | protected CodecService codecService; |
| 117 | |
| 118 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 119 | protected ComponentConfigService componentConfigService; |
| 120 | |
alshabib | f544c01 | 2016-02-21 14:49:51 -0800 | [diff] [blame] | 121 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 122 | protected NetworkConfigRegistry networkConfig; |
| 123 | |
alshabib | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 124 | protected McastListener listener = new InternalMulticastListener(); |
alshabib | f544c01 | 2016-02-21 14:49:51 -0800 | [diff] [blame] | 125 | private InternalNetworkConfigListener configListener = |
| 126 | new InternalNetworkConfigListener(); |
alshabib | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 127 | |
alshabib | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 128 | //TODO: move this to a ec map |
| 129 | private Map<IpAddress, Integer> groups = Maps.newConcurrentMap(); |
| 130 | |
alshabib | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 131 | private ApplicationId appId; |
| 132 | |
alshabib | 1aa5814 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 133 | @Property(name = "mcastVlan", intValue = DEFAULT_MCAST_VLAN, |
| 134 | label = "VLAN for multicast traffic") |
| 135 | private int mcastVlan = DEFAULT_MCAST_VLAN; |
alshabib | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 136 | |
alshabib | f544c01 | 2016-02-21 14:49:51 -0800 | [diff] [blame] | 137 | @Property(name = "vlanEnabled", boolValue = DEFAULT_VLAN_ENABLED, |
| 138 | label = "Use vlan for multicast traffic?") |
| 139 | private boolean vlanEnabled = DEFAULT_VLAN_ENABLED; |
alshabib | 1aa5814 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 140 | |
| 141 | @Property(name = "priority", intValue = DEFAULT_PRIORITY, |
| 142 | label = "Priority for multicast rules") |
alshabib | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 143 | private int priority = DEFAULT_PRIORITY; |
| 144 | |
alshabib | 1aa5814 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 145 | @Property(name = "syncHost", value = DEFAULT_SYNC_HOST, |
Jonathan Hart | fcdcb4f | 2016-02-10 16:13:54 -0800 | [diff] [blame] | 146 | label = "host:port to synchronize routes to") |
alshabib | 1aa5814 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 147 | private String syncHost = DEFAULT_SYNC_HOST; |
Jonathan Hart | fcdcb4f | 2016-02-10 16:13:54 -0800 | [diff] [blame] | 148 | |
| 149 | @Property(name = "username", value = DEFAULT_USER, |
| 150 | label = "Username for REST password authentication") |
| 151 | private String user = DEFAULT_USER; |
| 152 | |
| 153 | @Property(name = "password", value = DEFAULT_PASSWORD, |
| 154 | label = "Password for REST authentication") |
| 155 | private String password = DEFAULT_PASSWORD; |
| 156 | |
| 157 | private String fabricOnosUrl; |
| 158 | |
alshabib | f544c01 | 2016-02-21 14:49:51 -0800 | [diff] [blame] | 159 | private Map<DeviceId, AccessDeviceData> oltData = new ConcurrentHashMap<>(); |
| 160 | |
| 161 | private static final Class<AccessDeviceConfig> CONFIG_CLASS = |
| 162 | AccessDeviceConfig.class; |
| 163 | |
| 164 | private ConfigFactory<DeviceId, AccessDeviceConfig> configFactory = |
| 165 | new ConfigFactory<DeviceId, AccessDeviceConfig>( |
| 166 | SubjectFactories.DEVICE_SUBJECT_FACTORY, CONFIG_CLASS, "accessDevice") { |
| 167 | @Override |
| 168 | public AccessDeviceConfig createConfig() { |
| 169 | return new AccessDeviceConfig(); |
| 170 | } |
| 171 | }; |
| 172 | |
alshabib | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 173 | @Activate |
Jonathan Hart | 81d7310 | 2016-02-19 10:32:05 -0800 | [diff] [blame] | 174 | public void activate(ComponentContext context) { |
Jonathan Hart | 9ad777f | 2016-02-19 12:44:36 -0800 | [diff] [blame] | 175 | componentConfigService.registerProperties(getClass()); |
Jonathan Hart | 81d7310 | 2016-02-19 10:32:05 -0800 | [diff] [blame] | 176 | modified(context); |
| 177 | |
alshabib | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 178 | appId = coreService.registerApplication("org.onosproject.cordmcast"); |
Jonathan Hart | fcdcb4f | 2016-02-10 16:13:54 -0800 | [diff] [blame] | 179 | |
alshabib | f544c01 | 2016-02-21 14:49:51 -0800 | [diff] [blame] | 180 | |
Jonathan Hart | 9ad777f | 2016-02-19 12:44:36 -0800 | [diff] [blame] | 181 | clearRemoteRoutes(); |
| 182 | |
alshabib | f544c01 | 2016-02-21 14:49:51 -0800 | [diff] [blame] | 183 | networkConfig.registerConfigFactory(configFactory); |
| 184 | networkConfig.addListener(configListener); |
| 185 | |
| 186 | networkConfig.getSubjects(DeviceId.class, AccessDeviceConfig.class).forEach( |
| 187 | subject -> { |
| 188 | AccessDeviceConfig config = networkConfig.getConfig(subject, AccessDeviceConfig.class); |
| 189 | if (config != null) { |
| 190 | AccessDeviceData data = config.getOlt(); |
| 191 | oltData.put(data.deviceId(), data); |
| 192 | } |
| 193 | } |
| 194 | ); |
| 195 | |
| 196 | |
Jonathan Hart | 9ad777f | 2016-02-19 12:44:36 -0800 | [diff] [blame] | 197 | mcastService.addListener(listener); |
| 198 | |
alshabib | f544c01 | 2016-02-21 14:49:51 -0800 | [diff] [blame] | 199 | mcastService.getRoutes().stream() |
| 200 | .map(r -> new ImmutablePair<>(r, mcastService.fetchSinks(r))) |
| 201 | .filter(pair -> pair.getRight() != null && !pair.getRight().isEmpty()) |
| 202 | .forEach(pair -> pair.getRight().forEach(sink -> provisionGroup(pair.getLeft(), |
| 203 | sink))); |
Jonathan Hart | 9ad777f | 2016-02-19 12:44:36 -0800 | [diff] [blame] | 204 | |
alshabib | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 205 | log.info("Started"); |
| 206 | } |
| 207 | |
| 208 | @Deactivate |
| 209 | public void deactivate() { |
Jonathan Hart | 9ad777f | 2016-02-19 12:44:36 -0800 | [diff] [blame] | 210 | componentConfigService.unregisterProperties(getClass(), false); |
alshabib | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 211 | mcastService.removeListener(listener); |
alshabib | f544c01 | 2016-02-21 14:49:51 -0800 | [diff] [blame] | 212 | networkConfig.unregisterConfigFactory(configFactory); |
| 213 | networkConfig.removeListener(configListener); |
alshabib | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 214 | log.info("Stopped"); |
| 215 | } |
| 216 | |
Jonathan Hart | fcdcb4f | 2016-02-10 16:13:54 -0800 | [diff] [blame] | 217 | @Modified |
| 218 | public void modified(ComponentContext context) { |
alshabib | 1aa5814 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 219 | Dictionary<?, ?> properties = context != null ? context.getProperties() : new Properties(); |
| 220 | |
alshabib | 1aa5814 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 221 | try { |
| 222 | String s = get(properties, "username"); |
| 223 | user = isNullOrEmpty(s) ? DEFAULT_USER : s.trim(); |
| 224 | |
| 225 | s = get(properties, "password"); |
| 226 | password = isNullOrEmpty(s) ? DEFAULT_PASSWORD : s.trim(); |
| 227 | |
| 228 | s = get(properties, "mcastVlan"); |
| 229 | mcastVlan = isNullOrEmpty(s) ? DEFAULT_MCAST_VLAN : Short.parseShort(s.trim()); |
| 230 | |
| 231 | s = get(properties, "vlanEnabled"); |
alshabib | f544c01 | 2016-02-21 14:49:51 -0800 | [diff] [blame] | 232 | vlanEnabled = isNullOrEmpty(s) ? DEFAULT_VLAN_ENABLED : Boolean.parseBoolean(s.trim()); |
alshabib | 1aa5814 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 233 | |
| 234 | s = get(properties, "priority"); |
| 235 | priority = isNullOrEmpty(s) ? DEFAULT_PRIORITY : Integer.parseInt(s.trim()); |
| 236 | |
Jonathan Hart | 7f4bc52 | 2016-02-20 11:32:43 -0800 | [diff] [blame] | 237 | s = get(properties, "syncHost"); |
alshabib | 1aa5814 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 238 | syncHost = isNullOrEmpty(s) ? DEFAULT_SYNC_HOST : s.trim(); |
| 239 | } catch (Exception e) { |
| 240 | user = DEFAULT_USER; |
| 241 | password = DEFAULT_PASSWORD; |
| 242 | syncHost = DEFAULT_SYNC_HOST; |
| 243 | mcastVlan = DEFAULT_MCAST_VLAN; |
| 244 | vlanEnabled = false; |
| 245 | priority = DEFAULT_PRIORITY; |
| 246 | } |
Jonathan Hart | 7f4bc52 | 2016-02-20 11:32:43 -0800 | [diff] [blame] | 247 | fabricOnosUrl = createRemoteUrl(syncHost); |
| 248 | } |
| 249 | |
| 250 | private static String createRemoteUrl(String remoteHost) { |
| 251 | return "http://" + remoteHost + "/onos/v1/mcast"; |
Jonathan Hart | fcdcb4f | 2016-02-10 16:13:54 -0800 | [diff] [blame] | 252 | } |
| 253 | |
alshabib | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 254 | private class InternalMulticastListener implements McastListener { |
| 255 | @Override |
| 256 | public void event(McastEvent event) { |
Jonathan Hart | 9ad777f | 2016-02-19 12:44:36 -0800 | [diff] [blame] | 257 | McastRouteInfo info = event.subject(); |
alshabib | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 258 | switch (event.type()) { |
| 259 | case ROUTE_ADDED: |
| 260 | break; |
| 261 | case ROUTE_REMOVED: |
| 262 | break; |
| 263 | case SOURCE_ADDED: |
| 264 | break; |
| 265 | case SINK_ADDED: |
Jonathan Hart | 9ad777f | 2016-02-19 12:44:36 -0800 | [diff] [blame] | 266 | if (!info.sink().isPresent()) { |
| 267 | log.warn("No sink given after sink added event: {}", info); |
| 268 | return; |
| 269 | } |
| 270 | provisionGroup(info.route(), info.sink().get()); |
alshabib | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 271 | break; |
| 272 | case SINK_REMOVED: |
alshabib | 1aa5814 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 273 | unprovisionGroup(event.subject()); |
alshabib | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 274 | break; |
| 275 | default: |
| 276 | log.warn("Unknown mcast event {}", event.type()); |
| 277 | } |
| 278 | } |
| 279 | } |
| 280 | |
alshabib | 1aa5814 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 281 | private void unprovisionGroup(McastRouteInfo info) { |
Jonathan Hart | dd7e3d5 | 2016-02-18 15:56:22 -0800 | [diff] [blame] | 282 | if (info.sinks().isEmpty()) { |
Jonathan Hart | 9ad777f | 2016-02-19 12:44:36 -0800 | [diff] [blame] | 283 | removeRemoteRoute(info.route()); |
Jonathan Hart | dd7e3d5 | 2016-02-18 15:56:22 -0800 | [diff] [blame] | 284 | } |
| 285 | |
alshabib | 1aa5814 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 286 | if (!info.sink().isPresent()) { |
| 287 | log.warn("No sink given after sink removed event: {}", info); |
| 288 | return; |
| 289 | } |
| 290 | ConnectPoint loc = info.sink().get(); |
| 291 | |
| 292 | NextObjective next = DefaultNextObjective.builder() |
| 293 | .fromApp(appId) |
| 294 | .addTreatment(DefaultTrafficTreatment.builder().setOutput(loc.port()).build()) |
| 295 | .withType(NextObjective.Type.BROADCAST) |
| 296 | .withId(groups.get(info.route().group())) |
| 297 | .removeFromExisting(new ObjectiveContext() { |
| 298 | @Override |
| 299 | public void onSuccess(Objective objective) { |
| 300 | //TODO: change to debug |
| 301 | log.info("Next Objective {} installed", objective.id()); |
| 302 | } |
| 303 | |
| 304 | @Override |
| 305 | public void onError(Objective objective, ObjectiveError error) { |
| 306 | //TODO: change to debug |
| 307 | log.info("Next Objective {} failed, because {}", |
| 308 | objective.id(), |
| 309 | error); |
| 310 | } |
| 311 | }); |
| 312 | |
| 313 | flowObjectiveService.next(loc.deviceId(), next); |
alshabib | 1aa5814 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 314 | } |
| 315 | |
Jonathan Hart | 9ad777f | 2016-02-19 12:44:36 -0800 | [diff] [blame] | 316 | private void provisionGroup(McastRoute route, ConnectPoint sink) { |
| 317 | checkNotNull(route, "Route cannot be null"); |
| 318 | checkNotNull(sink, "Sink cannot be null"); |
alshabib | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 319 | |
alshabib | f544c01 | 2016-02-21 14:49:51 -0800 | [diff] [blame] | 320 | AccessDeviceData oltInfo = oltData.get(sink.deviceId()); |
| 321 | |
| 322 | if (oltInfo == null) { |
| 323 | log.warn("Unknown OLT device : {}", sink.deviceId()); |
| 324 | return; |
| 325 | } |
| 326 | |
Jonathan Hart | fcdcb4f | 2016-02-10 16:13:54 -0800 | [diff] [blame] | 327 | final AtomicBoolean sync = new AtomicBoolean(false); |
alshabib | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 328 | |
Jonathan Hart | 9ad777f | 2016-02-19 12:44:36 -0800 | [diff] [blame] | 329 | Integer nextId = groups.computeIfAbsent(route.group(), (g) -> { |
Zsolt Haraszti | 05a1ba1 | 2016-02-25 09:39:10 -0800 | [diff] [blame] | 330 | Integer id = flowObjectiveService.allocateNextId(); |
alshabib | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 331 | |
alshabib | 1aa5814 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 332 | NextObjective next = DefaultNextObjective.builder() |
| 333 | .fromApp(appId) |
Jonathan Hart | 9ad777f | 2016-02-19 12:44:36 -0800 | [diff] [blame] | 334 | .addTreatment(DefaultTrafficTreatment.builder().setOutput(sink.port()).build()) |
alshabib | 1aa5814 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 335 | .withType(NextObjective.Type.BROADCAST) |
| 336 | .withId(id) |
| 337 | .add(new ObjectiveContext() { |
| 338 | @Override |
| 339 | public void onSuccess(Objective objective) { |
| 340 | //TODO: change to debug |
| 341 | log.info("Next Objective {} installed", objective.id()); |
| 342 | } |
| 343 | |
| 344 | @Override |
| 345 | public void onError(Objective objective, ObjectiveError error) { |
| 346 | //TODO: change to debug |
| 347 | log.info("Next Objective {} failed, because {}", |
| 348 | objective.id(), |
| 349 | error); |
| 350 | } |
| 351 | }); |
| 352 | |
Jonathan Hart | 9ad777f | 2016-02-19 12:44:36 -0800 | [diff] [blame] | 353 | flowObjectiveService.next(sink.deviceId(), next); |
alshabib | 1aa5814 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 354 | |
| 355 | TrafficSelector.Builder mcast = DefaultTrafficSelector.builder() |
alshabib | f544c01 | 2016-02-21 14:49:51 -0800 | [diff] [blame] | 356 | .matchInPort(oltInfo.uplink()) |
alshabib | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 357 | .matchEthType(Ethernet.TYPE_IPV4) |
alshabib | 1aa5814 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 358 | .matchIPDst(g.toIpPrefix()); |
| 359 | |
alshabib | 1aa5814 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 360 | if (vlanEnabled) { |
| 361 | mcast.matchVlanId(VlanId.vlanId((short) mcastVlan)); |
| 362 | } |
alshabib | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 363 | |
alshabib | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 364 | ForwardingObjective fwd = DefaultForwardingObjective.builder() |
| 365 | .fromApp(appId) |
| 366 | .nextStep(id) |
| 367 | .makePermanent() |
| 368 | .withFlag(ForwardingObjective.Flag.VERSATILE) |
| 369 | .withPriority(priority) |
alshabib | 1aa5814 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 370 | .withSelector(mcast.build()) |
alshabib | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 371 | .add(new ObjectiveContext() { |
| 372 | @Override |
| 373 | public void onSuccess(Objective objective) { |
| 374 | //TODO: change to debug |
| 375 | log.info("Forwarding objective installed {}", objective); |
| 376 | } |
| 377 | |
| 378 | @Override |
| 379 | public void onError(Objective objective, ObjectiveError error) { |
| 380 | //TODO: change to debug |
| 381 | log.info("Forwarding objective failed {}", objective); |
| 382 | } |
| 383 | }); |
| 384 | |
Jonathan Hart | 9ad777f | 2016-02-19 12:44:36 -0800 | [diff] [blame] | 385 | flowObjectiveService.forward(sink.deviceId(), fwd); |
alshabib | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 386 | |
Jonathan Hart | fcdcb4f | 2016-02-10 16:13:54 -0800 | [diff] [blame] | 387 | sync.set(true); |
| 388 | |
alshabib | f544c01 | 2016-02-21 14:49:51 -0800 | [diff] [blame] | 389 | return id; |
alshabib | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 390 | }); |
| 391 | |
alshabib | 1aa5814 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 392 | if (!sync.get()) { |
| 393 | NextObjective next = DefaultNextObjective.builder() |
| 394 | .fromApp(appId) |
Jonathan Hart | 9ad777f | 2016-02-19 12:44:36 -0800 | [diff] [blame] | 395 | .addTreatment(DefaultTrafficTreatment.builder().setOutput(sink.port()).build()) |
alshabib | 1aa5814 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 396 | .withType(NextObjective.Type.BROADCAST) |
| 397 | .withId(nextId) |
| 398 | .addToExisting(new ObjectiveContext() { |
| 399 | @Override |
| 400 | public void onSuccess(Objective objective) { |
| 401 | //TODO: change to debug |
| 402 | log.info("Next Objective {} installed", objective.id()); |
| 403 | } |
alshabib | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 404 | |
alshabib | 1aa5814 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 405 | @Override |
| 406 | public void onError(Objective objective, ObjectiveError error) { |
| 407 | //TODO: change to debug |
| 408 | log.info("Next Objective {} failed, because {}", |
| 409 | objective.id(), |
| 410 | error); |
| 411 | } |
| 412 | }); |
alshabib | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 413 | |
Jonathan Hart | 9ad777f | 2016-02-19 12:44:36 -0800 | [diff] [blame] | 414 | flowObjectiveService.next(sink.deviceId(), next); |
alshabib | 1aa5814 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 415 | } |
Jonathan Hart | fcdcb4f | 2016-02-10 16:13:54 -0800 | [diff] [blame] | 416 | |
alshabib | f544c01 | 2016-02-21 14:49:51 -0800 | [diff] [blame] | 417 | |
Jonathan Hart | 9ad777f | 2016-02-19 12:44:36 -0800 | [diff] [blame] | 418 | addRemoteRoute(route); |
alshabib | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 419 | } |
| 420 | |
Jonathan Hart | 9ad777f | 2016-02-19 12:44:36 -0800 | [diff] [blame] | 421 | private void addRemoteRoute(McastRoute route) { |
| 422 | checkNotNull(route); |
Jonathan Hart | fcdcb4f | 2016-02-10 16:13:54 -0800 | [diff] [blame] | 423 | if (syncHost == null) { |
| 424 | log.warn("No host configured for synchronization; route will be dropped"); |
| 425 | return; |
| 426 | } |
| 427 | |
Jonathan Hart | 7f4bc52 | 2016-02-20 11:32:43 -0800 | [diff] [blame] | 428 | log.debug("Sending route {} to other ONOS {}", route, fabricOnosUrl); |
Jonathan Hart | fcdcb4f | 2016-02-10 16:13:54 -0800 | [diff] [blame] | 429 | |
| 430 | WebResource.Builder builder = getClientBuilder(fabricOnosUrl); |
| 431 | |
| 432 | ObjectNode json = codecService.getCodec(McastRoute.class) |
Jonathan Hart | 9ad777f | 2016-02-19 12:44:36 -0800 | [diff] [blame] | 433 | .encode(route, new AbstractWebResource()); |
Jonathan Hart | 7f4bc52 | 2016-02-20 11:32:43 -0800 | [diff] [blame] | 434 | |
| 435 | try { |
| 436 | builder.post(json.toString()); |
| 437 | } catch (ClientHandlerException e) { |
| 438 | log.warn("Unable to send route to remote controller: {}", e.getMessage()); |
| 439 | } |
alshabib | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 440 | } |
Jonathan Hart | fcdcb4f | 2016-02-10 16:13:54 -0800 | [diff] [blame] | 441 | |
Jonathan Hart | 9ad777f | 2016-02-19 12:44:36 -0800 | [diff] [blame] | 442 | private void removeRemoteRoute(McastRoute route) { |
Jonathan Hart | dd7e3d5 | 2016-02-18 15:56:22 -0800 | [diff] [blame] | 443 | if (syncHost == null) { |
| 444 | log.warn("No host configured for synchronization; route will be dropped"); |
| 445 | return; |
| 446 | } |
| 447 | |
Jonathan Hart | 7f4bc52 | 2016-02-20 11:32:43 -0800 | [diff] [blame] | 448 | log.debug("Removing route {} from other ONOS {}", route, fabricOnosUrl); |
Jonathan Hart | dd7e3d5 | 2016-02-18 15:56:22 -0800 | [diff] [blame] | 449 | |
| 450 | WebResource.Builder builder = getClientBuilder(fabricOnosUrl); |
| 451 | |
| 452 | ObjectNode json = codecService.getCodec(McastRoute.class) |
Jonathan Hart | 9ad777f | 2016-02-19 12:44:36 -0800 | [diff] [blame] | 453 | .encode(route, new AbstractWebResource()); |
Jonathan Hart | 7f4bc52 | 2016-02-20 11:32:43 -0800 | [diff] [blame] | 454 | try { |
| 455 | builder.delete(json.toString()); |
| 456 | } catch (ClientHandlerException e) { |
| 457 | log.warn("Unable to delete route from remote controller: {}", e.getMessage()); |
| 458 | } |
Jonathan Hart | dd7e3d5 | 2016-02-18 15:56:22 -0800 | [diff] [blame] | 459 | } |
| 460 | |
Jonathan Hart | 9ad777f | 2016-02-19 12:44:36 -0800 | [diff] [blame] | 461 | private void clearRemoteRoutes() { |
| 462 | if (syncHost == null) { |
| 463 | log.warn("No host configured for synchronization"); |
| 464 | return; |
| 465 | } |
| 466 | |
Jonathan Hart | 7f4bc52 | 2016-02-20 11:32:43 -0800 | [diff] [blame] | 467 | log.debug("Clearing remote multicast routes from {}", fabricOnosUrl); |
Jonathan Hart | 9ad777f | 2016-02-19 12:44:36 -0800 | [diff] [blame] | 468 | |
| 469 | WebResource.Builder builder = getClientBuilder(fabricOnosUrl); |
Jonathan Hart | 9ad777f | 2016-02-19 12:44:36 -0800 | [diff] [blame] | 470 | List<McastRoute> mcastRoutes = Lists.newArrayList(); |
Jonathan Hart | 7f4bc52 | 2016-02-20 11:32:43 -0800 | [diff] [blame] | 471 | |
Jonathan Hart | 9ad777f | 2016-02-19 12:44:36 -0800 | [diff] [blame] | 472 | try { |
Jonathan Hart | 7f4bc52 | 2016-02-20 11:32:43 -0800 | [diff] [blame] | 473 | String response = builder |
| 474 | .accept(MediaType.APPLICATION_JSON_TYPE) |
| 475 | .get(String.class); |
| 476 | |
| 477 | JsonCodec<McastRoute> routeCodec = codecService.getCodec(McastRoute.class); |
| 478 | ObjectMapper mapper = new ObjectMapper(); |
| 479 | |
| 480 | |
Jonathan Hart | 9ad777f | 2016-02-19 12:44:36 -0800 | [diff] [blame] | 481 | ObjectNode node = (ObjectNode) mapper.readTree(response); |
| 482 | ArrayNode list = (ArrayNode) node.path("routes"); |
| 483 | |
| 484 | list.forEach(n -> mcastRoutes.add( |
| 485 | routeCodec.decode((ObjectNode) n, new AbstractWebResource()))); |
Jonathan Hart | 7f4bc52 | 2016-02-20 11:32:43 -0800 | [diff] [blame] | 486 | |
| 487 | } catch (ClientHandlerException e) { |
| 488 | log.warn("Unable to clear routes from remote controller: {}", e.getMessage()); |
Jonathan Hart | 9ad777f | 2016-02-19 12:44:36 -0800 | [diff] [blame] | 489 | } catch (IOException e) { |
| 490 | log.warn("Error clearing remote routes", e); |
| 491 | } |
| 492 | |
| 493 | mcastRoutes.forEach(this::removeRemoteRoute); |
| 494 | } |
| 495 | |
Jonathan Hart | fcdcb4f | 2016-02-10 16:13:54 -0800 | [diff] [blame] | 496 | private WebResource.Builder getClientBuilder(String uri) { |
| 497 | Client client = Client.create(); |
Jonathan Hart | 7f4bc52 | 2016-02-20 11:32:43 -0800 | [diff] [blame] | 498 | client.setConnectTimeout(DEFAULT_REST_TIMEOUT_MS); |
| 499 | client.setReadTimeout(DEFAULT_REST_TIMEOUT_MS); |
Jonathan Hart | fcdcb4f | 2016-02-10 16:13:54 -0800 | [diff] [blame] | 500 | client.addFilter(new HTTPBasicAuthFilter(user, password)); |
| 501 | WebResource resource = client.resource(uri); |
| 502 | return resource.accept(JSON_UTF_8.toString()) |
| 503 | .type(JSON_UTF_8.toString()); |
| 504 | } |
| 505 | |
alshabib | f544c01 | 2016-02-21 14:49:51 -0800 | [diff] [blame] | 506 | private class InternalNetworkConfigListener implements NetworkConfigListener { |
| 507 | @Override |
| 508 | public void event(NetworkConfigEvent event) { |
| 509 | switch (event.type()) { |
| 510 | |
| 511 | case CONFIG_ADDED: |
| 512 | case CONFIG_UPDATED: |
| 513 | AccessDeviceConfig config = |
| 514 | networkConfig.getConfig((DeviceId) event.subject(), CONFIG_CLASS); |
| 515 | if (config != null) { |
| 516 | oltData.put(config.getOlt().deviceId(), config.getOlt()); |
| 517 | } |
| 518 | |
| 519 | break; |
| 520 | case CONFIG_REGISTERED: |
| 521 | case CONFIG_UNREGISTERED: |
| 522 | break; |
| 523 | case CONFIG_REMOVED: |
| 524 | oltData.remove(event.subject()); |
| 525 | break; |
| 526 | default: |
| 527 | break; |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | @Override |
| 532 | public boolean isRelevant(NetworkConfigEvent event) { |
| 533 | return event.configClass().equals(CONFIG_CLASS); |
| 534 | } |
| 535 | |
| 536 | |
| 537 | } |
| 538 | |
alshabib | 7115cd2 | 2016-02-01 17:57:00 -0800 | [diff] [blame] | 539 | } |