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