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