Pingping Lin | ffa27d3 | 2015-04-30 14:41:03 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Pingping Lin | ffa27d3 | 2015-04-30 14:41:03 -0700 | [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.virtualbng; |
| 17 | |
Pingping Lin | 53ae34f | 2015-06-09 10:07:08 -0700 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Pingping Lin | ffa27d3 | 2015-04-30 14:41:03 -0700 | [diff] [blame] | 21 | import org.onlab.packet.Ethernet; |
| 22 | import org.onlab.packet.IpAddress; |
| 23 | import org.onlab.packet.IpPrefix; |
| 24 | import org.onlab.packet.MacAddress; |
| 25 | import org.onosproject.core.ApplicationId; |
| 26 | import org.onosproject.core.CoreService; |
| 27 | import org.onosproject.net.ConnectPoint; |
Pingping Lin | dead205 | 2015-06-08 16:07:23 -0700 | [diff] [blame] | 28 | import org.onosproject.net.DeviceId; |
Ray Milkey | a2cf3a1 | 2018-02-15 16:13:56 -0800 | [diff] [blame] | 29 | import org.onosproject.net.FilteredConnectPoint; |
Pingping Lin | ffa27d3 | 2015-04-30 14:41:03 -0700 | [diff] [blame] | 30 | import org.onosproject.net.Host; |
| 31 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 32 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 33 | import org.onosproject.net.flow.TrafficSelector; |
| 34 | import org.onosproject.net.flow.TrafficTreatment; |
Pingping Lin | 4e0c73d | 2015-05-06 15:41:10 -0700 | [diff] [blame] | 35 | import org.onosproject.net.host.HostEvent; |
| 36 | import org.onosproject.net.host.HostListener; |
Pingping Lin | ffa27d3 | 2015-04-30 14:41:03 -0700 | [diff] [blame] | 37 | import org.onosproject.net.host.HostService; |
| 38 | import org.onosproject.net.intent.IntentService; |
| 39 | import org.onosproject.net.intent.Key; |
| 40 | import org.onosproject.net.intent.PointToPointIntent; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 41 | import org.osgi.service.component.annotations.Activate; |
| 42 | import org.osgi.service.component.annotations.Component; |
| 43 | import org.osgi.service.component.annotations.Deactivate; |
| 44 | import org.osgi.service.component.annotations.Reference; |
| 45 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Pingping Lin | ffa27d3 | 2015-04-30 14:41:03 -0700 | [diff] [blame] | 46 | import org.slf4j.Logger; |
| 47 | import org.slf4j.LoggerFactory; |
| 48 | |
Jonathan Hart | bbc352f | 2015-10-27 19:24:36 -0700 | [diff] [blame] | 49 | import java.util.Iterator; |
| 50 | import java.util.Map; |
| 51 | import java.util.Map.Entry; |
| 52 | import java.util.concurrent.ConcurrentHashMap; |
| 53 | |
| 54 | import static com.google.common.base.Preconditions.checkNotNull; |
| 55 | |
Pingping Lin | ffa27d3 | 2015-04-30 14:41:03 -0700 | [diff] [blame] | 56 | /** |
| 57 | * This is a virtual Broadband Network Gateway (BNG) application. It mainly |
| 58 | * has 3 functions: |
| 59 | * (1) assigns and replies a public IP address to a REST request with a private |
| 60 | * IP address |
| 61 | * (2) maintains the mapping from the private IP address to the public IP address |
| 62 | * (3) installs point to point intents for the host configured with private IP |
| 63 | * address to access Internet |
| 64 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 65 | @Component(immediate = true, service = VbngService.class) |
Pingping Lin | ffa27d3 | 2015-04-30 14:41:03 -0700 | [diff] [blame] | 66 | public class VbngManager implements VbngService { |
| 67 | |
| 68 | private static final String APP_NAME = "org.onosproject.virtualbng"; |
Pingping Lin | 53ae34f | 2015-06-09 10:07:08 -0700 | [diff] [blame] | 69 | private static final String VBNG_MAP_NAME = "vbng_mapping"; |
Pingping Lin | ffa27d3 | 2015-04-30 14:41:03 -0700 | [diff] [blame] | 70 | |
| 71 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 72 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 73 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Pingping Lin | ffa27d3 | 2015-04-30 14:41:03 -0700 | [diff] [blame] | 74 | protected CoreService coreService; |
| 75 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 76 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Pingping Lin | ffa27d3 | 2015-04-30 14:41:03 -0700 | [diff] [blame] | 77 | protected HostService hostService; |
| 78 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 79 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Pingping Lin | ffa27d3 | 2015-04-30 14:41:03 -0700 | [diff] [blame] | 80 | protected IntentService intentService; |
| 81 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 82 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Pingping Lin | ffa27d3 | 2015-04-30 14:41:03 -0700 | [diff] [blame] | 83 | protected VbngConfigurationService vbngConfigurationService; |
| 84 | |
| 85 | private ApplicationId appId; |
| 86 | private Map<IpAddress, PointToPointIntent> p2pIntentsFromHost; |
| 87 | private Map<IpAddress, PointToPointIntent> p2pIntentsToHost; |
| 88 | |
Pingping Lin | dead205 | 2015-06-08 16:07:23 -0700 | [diff] [blame] | 89 | // This map stores the mapping from the private IP addresses to VcpeHost. |
| 90 | // The IP addresses in this map are all the private IP addresses we failed |
| 91 | // to create vBNGs due to the next hop host was not in ONOS. |
| 92 | private Map<IpAddress, VcpeHost> privateIpAddressMap; |
| 93 | |
| 94 | // Store the mapping from hostname to connect point |
| 95 | private Map<String, ConnectPoint> nodeToPort; |
Pingping Lin | 4e0c73d | 2015-05-06 15:41:10 -0700 | [diff] [blame] | 96 | |
| 97 | private HostListener hostListener; |
| 98 | private IpAddress nextHopIpAddress; |
| 99 | |
Pingping Lin | dead205 | 2015-06-08 16:07:23 -0700 | [diff] [blame] | 100 | private static final DeviceId FABRIC_DEVICE_ID = |
| 101 | DeviceId.deviceId("of:8f0e486e73000187"); |
| 102 | |
Pingping Lin | ffa27d3 | 2015-04-30 14:41:03 -0700 | [diff] [blame] | 103 | @Activate |
| 104 | public void activate() { |
| 105 | appId = coreService.registerApplication(APP_NAME); |
| 106 | p2pIntentsFromHost = new ConcurrentHashMap<>(); |
| 107 | p2pIntentsToHost = new ConcurrentHashMap<>(); |
Pingping Lin | dead205 | 2015-06-08 16:07:23 -0700 | [diff] [blame] | 108 | privateIpAddressMap = new ConcurrentHashMap<>(); |
| 109 | |
Pingping Lin | 4e0c73d | 2015-05-06 15:41:10 -0700 | [diff] [blame] | 110 | nextHopIpAddress = vbngConfigurationService.getNextHopIpAddress(); |
Jonathan Hart | bbc352f | 2015-10-27 19:24:36 -0700 | [diff] [blame] | 111 | nodeToPort = vbngConfigurationService.getNodeToPort(); |
Pingping Lin | 4e0c73d | 2015-05-06 15:41:10 -0700 | [diff] [blame] | 112 | hostListener = new InternalHostListener(); |
| 113 | hostService.addListener(hostListener); |
| 114 | |
Pingping Lin | ffa27d3 | 2015-04-30 14:41:03 -0700 | [diff] [blame] | 115 | log.info("vBNG Started"); |
Pingping Lin | 53ae34f | 2015-06-09 10:07:08 -0700 | [diff] [blame] | 116 | |
| 117 | // Recover the status before vBNG restarts |
| 118 | statusRecovery(); |
Pingping Lin | ffa27d3 | 2015-04-30 14:41:03 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | @Deactivate |
| 122 | public void deactivate() { |
Pingping Lin | 4e0c73d | 2015-05-06 15:41:10 -0700 | [diff] [blame] | 123 | hostService.removeListener(hostListener); |
Pingping Lin | ffa27d3 | 2015-04-30 14:41:03 -0700 | [diff] [blame] | 124 | log.info("vBNG Stopped"); |
| 125 | } |
| 126 | |
Pingping Lin | dead205 | 2015-06-08 16:07:23 -0700 | [diff] [blame] | 127 | /** |
Pingping Lin | 53ae34f | 2015-06-09 10:07:08 -0700 | [diff] [blame] | 128 | * Recovers from XOS record. Re-sets up the mapping between private IP |
| 129 | * address and public IP address, re-calculates intents and re-installs |
| 130 | * those intents. |
| 131 | */ |
| 132 | private void statusRecovery() { |
| 133 | log.info("vBNG starts to recover from XOS record......"); |
Jonathan Hart | bbc352f | 2015-10-27 19:24:36 -0700 | [diff] [blame] | 134 | ObjectNode map; |
| 135 | try { |
| 136 | RestClient restClient = |
| 137 | new RestClient(vbngConfigurationService.getXosIpAddress(), |
| 138 | vbngConfigurationService.getXosRestPort()); |
| 139 | map = restClient.getRest(); |
| 140 | } catch (Exception e) { |
Ray Milkey | 6ee38b8 | 2019-02-07 08:08:26 -0800 | [diff] [blame] | 141 | log.warn("Could not contact XOS {}", e.getMessage()); |
Jonathan Hart | bbc352f | 2015-10-27 19:24:36 -0700 | [diff] [blame] | 142 | return; |
| 143 | } |
Pingping Lin | 53ae34f | 2015-06-09 10:07:08 -0700 | [diff] [blame] | 144 | if (map == null) { |
| 145 | log.info("Stop to recover vBNG status due to the vBNG map " |
| 146 | + "is null!"); |
| 147 | return; |
| 148 | } |
| 149 | |
| 150 | log.info("Get record from XOS: {}", map); |
| 151 | |
| 152 | ArrayNode array = (ArrayNode) map.get(VBNG_MAP_NAME); |
| 153 | Iterator<JsonNode> entries = array.elements(); |
| 154 | while (entries.hasNext()) { |
| 155 | ObjectNode entry = (ObjectNode) entries.next(); |
| 156 | |
| 157 | IpAddress hostIpAdddress = |
| 158 | IpAddress.valueOf(entry.get("private_ip").asText()); |
| 159 | IpAddress publicIpAddress = |
| 160 | IpAddress.valueOf(entry.get("routeable_subnet").asText()); |
| 161 | MacAddress macAddress = |
| 162 | MacAddress.valueOf(entry.get("mac").asText()); |
| 163 | String hostName = entry.get("hostname").asText(); |
| 164 | |
| 165 | // Create vBNG |
| 166 | createVbng(hostIpAdddress, publicIpAddress, macAddress, hostName); |
| 167 | |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | /** |
Pingping Lin | 53ae34f | 2015-06-09 10:07:08 -0700 | [diff] [blame] | 172 | * Creates a new vBNG. |
| 173 | * |
| 174 | * @param privateIpAddress a private IP address |
| 175 | * @param publicIpAddress the public IP address for the private IP address |
| 176 | * @param hostMacAddress the MAC address for the private IP address |
| 177 | * @param hostName the host name for the private IP address |
| 178 | */ |
| 179 | private void createVbng(IpAddress privateIpAddress, |
| 180 | IpAddress publicIpAddress, |
| 181 | MacAddress hostMacAddress, |
| 182 | String hostName) { |
| 183 | boolean result = vbngConfigurationService |
| 184 | .assignSpecifiedPublicIp(publicIpAddress, privateIpAddress); |
| 185 | if (!result) { |
| 186 | log.info("Assign public IP address {} for private IP address {} " |
| 187 | + "failed!", publicIpAddress, privateIpAddress); |
| 188 | log.info("Failed to create vBNG for private IP address {}", |
| 189 | privateIpAddress); |
| 190 | return; |
| 191 | } |
| 192 | log.info("[ADD] Private IP to Public IP mapping: {} --> {}", |
| 193 | privateIpAddress, publicIpAddress); |
| 194 | |
| 195 | // Setup paths between the host configured with private IP and |
| 196 | // next hop |
| 197 | if (!setupForwardingPaths(privateIpAddress, publicIpAddress, |
| 198 | hostMacAddress, hostName)) { |
| 199 | privateIpAddressMap.put(privateIpAddress, |
| 200 | new VcpeHost(hostMacAddress, hostName)); |
| 201 | } |
| 202 | } |
| 203 | |
Pingping Lin | ffa27d3 | 2015-04-30 14:41:03 -0700 | [diff] [blame] | 204 | @Override |
Pingping Lin | dead205 | 2015-06-08 16:07:23 -0700 | [diff] [blame] | 205 | public IpAddress createVbng(IpAddress privateIpAddress, |
| 206 | MacAddress hostMacAddress, |
| 207 | String hostName) { |
Pingping Lin | ffa27d3 | 2015-04-30 14:41:03 -0700 | [diff] [blame] | 208 | |
Pingping Lin | ffa27d3 | 2015-04-30 14:41:03 -0700 | [diff] [blame] | 209 | IpAddress publicIpAddress = |
| 210 | vbngConfigurationService.getAvailablePublicIpAddress( |
| 211 | privateIpAddress); |
| 212 | if (publicIpAddress == null) { |
| 213 | log.info("Did not find an available public IP address to use."); |
| 214 | return null; |
| 215 | } |
Pingping Lin | de77ee5 | 2015-06-03 17:16:07 -0700 | [diff] [blame] | 216 | log.info("[ADD] Private IP to Public IP mapping: {} --> {}", |
Pingping Lin | ffa27d3 | 2015-04-30 14:41:03 -0700 | [diff] [blame] | 217 | privateIpAddress, publicIpAddress); |
| 218 | |
| 219 | // Setup paths between the host configured with private IP and |
| 220 | // next hop |
Pingping Lin | dead205 | 2015-06-08 16:07:23 -0700 | [diff] [blame] | 221 | if (!setupForwardingPaths(privateIpAddress, publicIpAddress, |
| 222 | hostMacAddress, hostName)) { |
| 223 | privateIpAddressMap.put(privateIpAddress, |
| 224 | new VcpeHost(hostMacAddress, hostName)); |
Pingping Lin | 4e0c73d | 2015-05-06 15:41:10 -0700 | [diff] [blame] | 225 | } |
Pingping Lin | ffa27d3 | 2015-04-30 14:41:03 -0700 | [diff] [blame] | 226 | return publicIpAddress; |
| 227 | } |
| 228 | |
Pingping Lin | d2afaf2 | 2015-06-02 10:46:29 -0700 | [diff] [blame] | 229 | @Override |
| 230 | public IpAddress deleteVbng(IpAddress privateIpAddress) { |
| 231 | // Recycle the public IP address assigned to this private IP address. |
| 232 | // Recycling will also delete the mapping entry from the private IP |
| 233 | // address to public IP address. |
| 234 | IpAddress assignedPublicIpAddress = vbngConfigurationService |
| 235 | .recycleAssignedPublicIpAddress(privateIpAddress); |
| 236 | if (assignedPublicIpAddress == null) { |
| 237 | return null; |
| 238 | } |
| 239 | |
Pingping Lin | dead205 | 2015-06-08 16:07:23 -0700 | [diff] [blame] | 240 | // Remove the private IP address from privateIpAddressMap |
| 241 | privateIpAddressMap.remove(privateIpAddress); |
Pingping Lin | d2afaf2 | 2015-06-02 10:46:29 -0700 | [diff] [blame] | 242 | |
| 243 | // Remove intents |
| 244 | removeForwardingPaths(privateIpAddress); |
| 245 | |
| 246 | return assignedPublicIpAddress; |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Removes the forwarding paths in both two directions between host |
| 251 | * configured with private IP and next hop. |
| 252 | * |
| 253 | * @param privateIp the private IP address of a local host |
| 254 | */ |
| 255 | private void removeForwardingPaths(IpAddress privateIp) { |
| 256 | PointToPointIntent toNextHopIntent = |
| 257 | p2pIntentsFromHost.remove(privateIp); |
| 258 | if (toNextHopIntent != null) { |
| 259 | intentService.withdraw(toNextHopIntent); |
| 260 | //intentService.purge(toNextHopIntent); |
| 261 | } |
| 262 | PointToPointIntent toLocalHostIntent = |
| 263 | p2pIntentsToHost.remove(privateIp); |
| 264 | if (toLocalHostIntent != null) { |
| 265 | intentService.withdraw(toLocalHostIntent); |
| 266 | //intentService.purge(toLocalHostIntent); |
| 267 | } |
| 268 | } |
| 269 | |
Pingping Lin | ffa27d3 | 2015-04-30 14:41:03 -0700 | [diff] [blame] | 270 | /** |
| 271 | * Sets up forwarding paths in both two directions between host configured |
| 272 | * with private IP and next hop. |
| 273 | * |
| 274 | * @param privateIp the private IP address of a local host |
| 275 | * @param publicIp the public IP address assigned for the private IP address |
Pingping Lin | dead205 | 2015-06-08 16:07:23 -0700 | [diff] [blame] | 276 | * @param hostMacAddress the MAC address for the IP address |
| 277 | * @param hostName the host name for the IP address |
Pingping Lin | ffa27d3 | 2015-04-30 14:41:03 -0700 | [diff] [blame] | 278 | */ |
Pingping Lin | dead205 | 2015-06-08 16:07:23 -0700 | [diff] [blame] | 279 | private boolean setupForwardingPaths(IpAddress privateIp, |
| 280 | IpAddress publicIp, |
| 281 | MacAddress hostMacAddress, |
| 282 | String hostName) { |
Pingping Lin | ffa27d3 | 2015-04-30 14:41:03 -0700 | [diff] [blame] | 283 | checkNotNull(privateIp); |
| 284 | checkNotNull(publicIp); |
Pingping Lin | dead205 | 2015-06-08 16:07:23 -0700 | [diff] [blame] | 285 | checkNotNull(hostMacAddress); |
| 286 | checkNotNull(hostName); |
Pingping Lin | 4e0c73d | 2015-05-06 15:41:10 -0700 | [diff] [blame] | 287 | |
| 288 | if (nextHopIpAddress == null) { |
| 289 | log.warn("Did not find next hop IP address"); |
| 290 | return false; |
| 291 | } |
Pingping Lin | ffa27d3 | 2015-04-30 14:41:03 -0700 | [diff] [blame] | 292 | |
| 293 | // If there are already intents for private IP address in the system, |
| 294 | // we will do nothing and directly return. |
| 295 | if (p2pIntentsFromHost.containsKey(privateIp) |
| 296 | && p2pIntentsToHost.containsKey(privateIp)) { |
Pingping Lin | 4e0c73d | 2015-05-06 15:41:10 -0700 | [diff] [blame] | 297 | return true; |
Pingping Lin | ffa27d3 | 2015-04-30 14:41:03 -0700 | [diff] [blame] | 298 | } |
| 299 | |
Pingping Lin | ffa27d3 | 2015-04-30 14:41:03 -0700 | [diff] [blame] | 300 | Host nextHopHost = null; |
| 301 | if (!hostService.getHostsByIp(nextHopIpAddress).isEmpty()) { |
| 302 | nextHopHost = hostService.getHostsByIp(nextHopIpAddress) |
| 303 | .iterator().next(); |
| 304 | } else { |
Pingping Lin | ffa27d3 | 2015-04-30 14:41:03 -0700 | [diff] [blame] | 305 | hostService.startMonitoringIp(nextHopIpAddress); |
| 306 | if (hostService.getHostsByIp(privateIp).isEmpty()) { |
| 307 | hostService.startMonitoringIp(privateIp); |
| 308 | } |
Pingping Lin | 4e0c73d | 2015-05-06 15:41:10 -0700 | [diff] [blame] | 309 | return false; |
Pingping Lin | ffa27d3 | 2015-04-30 14:41:03 -0700 | [diff] [blame] | 310 | } |
| 311 | |
Pingping Lin | ffa27d3 | 2015-04-30 14:41:03 -0700 | [diff] [blame] | 312 | ConnectPoint nextHopConnectPoint = |
| 313 | new ConnectPoint(nextHopHost.location().elementId(), |
| 314 | nextHopHost.location().port()); |
Pingping Lin | dead205 | 2015-06-08 16:07:23 -0700 | [diff] [blame] | 315 | ConnectPoint localHostConnectPoint = nodeToPort.get(hostName); |
Pingping Lin | ffa27d3 | 2015-04-30 14:41:03 -0700 | [diff] [blame] | 316 | |
| 317 | // Generate and install intent for traffic from host configured with |
| 318 | // private IP |
| 319 | if (!p2pIntentsFromHost.containsKey(privateIp)) { |
| 320 | PointToPointIntent toNextHopIntent |
| 321 | = srcMatchIntentGenerator(privateIp, |
| 322 | publicIp, |
| 323 | nextHopHost.mac(), |
| 324 | nextHopConnectPoint, |
| 325 | localHostConnectPoint |
| 326 | ); |
| 327 | p2pIntentsFromHost.put(privateIp, toNextHopIntent); |
| 328 | intentService.submit(toNextHopIntent); |
| 329 | } |
| 330 | |
| 331 | // Generate and install intent for traffic to host configured with |
| 332 | // private IP |
| 333 | if (!p2pIntentsToHost.containsKey(privateIp)) { |
| 334 | PointToPointIntent toLocalHostIntent |
| 335 | = dstMatchIntentGenerator(publicIp, |
| 336 | privateIp, |
Pingping Lin | dead205 | 2015-06-08 16:07:23 -0700 | [diff] [blame] | 337 | hostMacAddress, |
Pingping Lin | ffa27d3 | 2015-04-30 14:41:03 -0700 | [diff] [blame] | 338 | localHostConnectPoint, |
| 339 | nextHopConnectPoint); |
Pingping Lin | d2afaf2 | 2015-06-02 10:46:29 -0700 | [diff] [blame] | 340 | p2pIntentsToHost.put(privateIp, toLocalHostIntent); |
Pingping Lin | ffa27d3 | 2015-04-30 14:41:03 -0700 | [diff] [blame] | 341 | intentService.submit(toLocalHostIntent); |
| 342 | } |
| 343 | |
Pingping Lin | 4e0c73d | 2015-05-06 15:41:10 -0700 | [diff] [blame] | 344 | return true; |
| 345 | } |
| 346 | |
| 347 | /** |
| 348 | * Listener for host events. |
| 349 | */ |
| 350 | private class InternalHostListener implements HostListener { |
| 351 | @Override |
| 352 | public void event(HostEvent event) { |
| 353 | log.debug("Received HostEvent {}", event); |
| 354 | |
| 355 | Host host = event.subject(); |
| 356 | if (event.type() != HostEvent.Type.HOST_ADDED) { |
| 357 | return; |
| 358 | } |
| 359 | |
| 360 | for (IpAddress ipAddress: host.ipAddresses()) { |
Pingping Lin | dead205 | 2015-06-08 16:07:23 -0700 | [diff] [blame] | 361 | // The POST method from XOS gives us MAC and host name, so we |
| 362 | // do not need to do anything after receive a vCPE host event |
| 363 | // for now. |
| 364 | /*if (privateIpAddressSet.contains(ipAddress)) { |
Pingping Lin | 4e0c73d | 2015-05-06 15:41:10 -0700 | [diff] [blame] | 365 | createVbngAgain(ipAddress); |
Pingping Lin | dead205 | 2015-06-08 16:07:23 -0700 | [diff] [blame] | 366 | }*/ |
Pingping Lin | 4e0c73d | 2015-05-06 15:41:10 -0700 | [diff] [blame] | 367 | |
| 368 | if (nextHopIpAddress != null && |
| 369 | ipAddress.equals(nextHopIpAddress)) { |
Pingping Lin | dead205 | 2015-06-08 16:07:23 -0700 | [diff] [blame] | 370 | |
| 371 | for (Entry<IpAddress, VcpeHost> entry: |
| 372 | privateIpAddressMap.entrySet()) { |
| 373 | createVbngAgain(entry.getKey()); |
Pingping Lin | 4e0c73d | 2015-05-06 15:41:10 -0700 | [diff] [blame] | 374 | } |
Pingping Lin | dead205 | 2015-06-08 16:07:23 -0700 | [diff] [blame] | 375 | |
Pingping Lin | 4e0c73d | 2015-05-06 15:41:10 -0700 | [diff] [blame] | 376 | } |
| 377 | } |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | /** |
| 382 | * Tries to create vBNG again after receiving a host event if the IP |
Pingping Lin | dead205 | 2015-06-08 16:07:23 -0700 | [diff] [blame] | 383 | * address of the host is the next hop IP address. |
Pingping Lin | 4e0c73d | 2015-05-06 15:41:10 -0700 | [diff] [blame] | 384 | * |
| 385 | * @param privateIpAddress the private IP address |
| 386 | */ |
| 387 | private void createVbngAgain(IpAddress privateIpAddress) { |
| 388 | IpAddress publicIpAddress = vbngConfigurationService |
| 389 | .getAssignedPublicIpAddress(privateIpAddress); |
| 390 | if (publicIpAddress == null) { |
| 391 | // We only need to handle the private IP addresses for which we |
| 392 | // already returned the REST replies with assigned public IP |
| 393 | // addresses. If a private IP addresses does not have an assigned |
| 394 | // public IP address, we should not get it an available public IP |
| 395 | // address here, and we should delete it in the unhandled private |
Pingping Lin | dead205 | 2015-06-08 16:07:23 -0700 | [diff] [blame] | 396 | // IP address map. |
| 397 | privateIpAddressMap.remove(privateIpAddress); |
Pingping Lin | 4e0c73d | 2015-05-06 15:41:10 -0700 | [diff] [blame] | 398 | return; |
| 399 | } |
Pingping Lin | dead205 | 2015-06-08 16:07:23 -0700 | [diff] [blame] | 400 | VcpeHost vcpeHost = privateIpAddressMap.get(privateIpAddress); |
| 401 | if (setupForwardingPaths(privateIpAddress, publicIpAddress, |
| 402 | vcpeHost.macAddress, vcpeHost.hostName)) { |
| 403 | privateIpAddressMap.remove(privateIpAddress); |
Pingping Lin | 4e0c73d | 2015-05-06 15:41:10 -0700 | [diff] [blame] | 404 | } |
Pingping Lin | ffa27d3 | 2015-04-30 14:41:03 -0700 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | /** |
| 408 | * PointToPointIntent Generator. |
| 409 | * <p> |
| 410 | * The intent will match the source IP address in packet, rewrite the |
| 411 | * source IP address, and rewrite the destination MAC address. |
| 412 | * </p> |
| 413 | * |
| 414 | * @param srcIpAddress the source IP address in packet to match |
| 415 | * @param newSrcIpAddress the new source IP address to set |
| 416 | * @param dstMacAddress the destination MAC address to set |
| 417 | * @param dstConnectPoint the egress point |
| 418 | * @param srcConnectPoint the ingress point |
| 419 | * @return a PointToPointIntent |
| 420 | */ |
| 421 | private PointToPointIntent srcMatchIntentGenerator( |
| 422 | IpAddress srcIpAddress, |
| 423 | IpAddress newSrcIpAddress, |
| 424 | MacAddress dstMacAddress, |
| 425 | ConnectPoint dstConnectPoint, |
| 426 | ConnectPoint srcConnectPoint) { |
| 427 | checkNotNull(srcIpAddress); |
| 428 | checkNotNull(newSrcIpAddress); |
| 429 | checkNotNull(dstMacAddress); |
| 430 | checkNotNull(dstConnectPoint); |
| 431 | checkNotNull(srcConnectPoint); |
| 432 | |
| 433 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 434 | selector.matchEthType(Ethernet.TYPE_IPV4); |
| 435 | selector.matchIPSrc(IpPrefix.valueOf(srcIpAddress, |
| 436 | IpPrefix.MAX_INET_MASK_LENGTH)); |
| 437 | |
| 438 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); |
| 439 | treatment.setEthDst(dstMacAddress); |
| 440 | treatment.setIpSrc(newSrcIpAddress); |
| 441 | |
| 442 | Key key = Key.of(srcIpAddress.toString() + "MatchSrc", appId); |
| 443 | PointToPointIntent intent = PointToPointIntent.builder() |
| 444 | .appId(appId) |
| 445 | .key(key) |
| 446 | .selector(selector.build()) |
| 447 | .treatment(treatment.build()) |
Ray Milkey | a2cf3a1 | 2018-02-15 16:13:56 -0800 | [diff] [blame] | 448 | .filteredEgressPoint(new FilteredConnectPoint(dstConnectPoint)) |
| 449 | .filteredIngressPoint(new FilteredConnectPoint(srcConnectPoint)) |
Pingping Lin | ffa27d3 | 2015-04-30 14:41:03 -0700 | [diff] [blame] | 450 | .build(); |
| 451 | |
| 452 | log.info("Generated a PointToPointIntent for traffic from local host " |
| 453 | + ": {}", intent); |
| 454 | return intent; |
| 455 | } |
| 456 | |
| 457 | /** |
| 458 | * PointToPointIntent Generator. |
| 459 | * <p> |
| 460 | * The intent will match the destination IP address in packet, rewrite the |
| 461 | * destination IP address, and rewrite the destination MAC address. |
| 462 | * </p> |
| 463 | * |
| 464 | * @param dstIpAddress the destination IP address in packet to match |
| 465 | * @param newDstIpAddress the new destination IP address to set |
| 466 | * @param dstMacAddress the destination MAC address to set |
| 467 | * @param dstConnectPoint the egress point |
| 468 | * @param srcConnectPoint the ingress point |
| 469 | * @return a PointToPointIntent |
| 470 | */ |
| 471 | private PointToPointIntent dstMatchIntentGenerator( |
| 472 | IpAddress dstIpAddress, |
| 473 | IpAddress newDstIpAddress, |
| 474 | MacAddress dstMacAddress, |
| 475 | ConnectPoint dstConnectPoint, |
| 476 | ConnectPoint srcConnectPoint) { |
| 477 | checkNotNull(dstIpAddress); |
| 478 | checkNotNull(newDstIpAddress); |
| 479 | checkNotNull(dstMacAddress); |
| 480 | checkNotNull(dstConnectPoint); |
| 481 | checkNotNull(srcConnectPoint); |
| 482 | |
| 483 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 484 | selector.matchEthType(Ethernet.TYPE_IPV4); |
| 485 | selector.matchIPDst(IpPrefix.valueOf(dstIpAddress, |
| 486 | IpPrefix.MAX_INET_MASK_LENGTH)); |
| 487 | |
| 488 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); |
| 489 | treatment.setEthDst(dstMacAddress); |
| 490 | treatment.setIpDst(newDstIpAddress); |
| 491 | |
| 492 | Key key = Key.of(newDstIpAddress.toString() + "MatchDst", appId); |
| 493 | PointToPointIntent intent = PointToPointIntent.builder() |
| 494 | .appId(appId) |
| 495 | .key(key) |
| 496 | .selector(selector.build()) |
| 497 | .treatment(treatment.build()) |
Ray Milkey | a2cf3a1 | 2018-02-15 16:13:56 -0800 | [diff] [blame] | 498 | .filteredEgressPoint(new FilteredConnectPoint(dstConnectPoint)) |
| 499 | .filteredIngressPoint(new FilteredConnectPoint(srcConnectPoint)) |
Pingping Lin | ffa27d3 | 2015-04-30 14:41:03 -0700 | [diff] [blame] | 500 | .build(); |
| 501 | log.info("Generated a PointToPointIntent for traffic to local host " |
| 502 | + ": {}", intent); |
| 503 | |
| 504 | return intent; |
| 505 | } |
Pingping Lin | dead205 | 2015-06-08 16:07:23 -0700 | [diff] [blame] | 506 | |
| 507 | /** |
| 508 | * Constructor to store the a vCPE host info. |
| 509 | */ |
| 510 | private class VcpeHost { |
| 511 | MacAddress macAddress; |
| 512 | String hostName; |
| 513 | public VcpeHost(MacAddress macAddress, String hostName) { |
| 514 | this.macAddress = macAddress; |
| 515 | this.hostName = hostName; |
| 516 | } |
| 517 | } |
Pingping Lin | ffa27d3 | 2015-04-30 14:41:03 -0700 | [diff] [blame] | 518 | } |