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