Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 Open Networking Laboratory |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.sdnip; |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 17 | |
Jonathan Hart | 5137218 | 2014-12-03 21:32:34 -0800 | [diff] [blame] | 18 | import org.onlab.packet.Ethernet; |
| 19 | import org.onlab.packet.IPv4; |
Kunihiro Ishiguro | 6e2ee15 | 2015-01-20 16:57:54 -0800 | [diff] [blame] | 20 | import org.onlab.packet.IPv6; |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 21 | import org.onlab.packet.IpAddress; |
Jonathan Hart | 5137218 | 2014-12-03 21:32:34 -0800 | [diff] [blame] | 22 | import org.onlab.packet.IpPrefix; |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 23 | import org.onlab.packet.TpPort; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 24 | import org.onosproject.core.ApplicationId; |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 25 | import org.onosproject.incubator.net.intf.Interface; |
| 26 | import org.onosproject.incubator.net.intf.InterfaceService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 27 | import org.onosproject.net.ConnectPoint; |
Jonathan Hart | 08105be | 2015-10-10 18:31:06 -0700 | [diff] [blame] | 28 | import org.onosproject.net.config.NetworkConfigEvent; |
| 29 | import org.onosproject.net.config.NetworkConfigListener; |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 30 | import org.onosproject.net.config.NetworkConfigService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 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; |
Jonathan Hart | 2a9ea49 | 2015-07-30 15:53:04 -0700 | [diff] [blame] | 35 | import org.onosproject.net.host.InterfaceIpAddress; |
Luca Prete | 86ac7d1 | 2015-12-02 23:36:49 -0800 | [diff] [blame] | 36 | import org.onosproject.net.intent.IntentUtils; |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 37 | import org.onosproject.net.intent.Key; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 38 | import org.onosproject.net.intent.PointToPointIntent; |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 39 | import org.onosproject.routing.IntentSynchronizationService; |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 40 | import org.onosproject.routing.RoutingService; |
| 41 | import org.onosproject.routing.config.BgpConfig; |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 42 | import org.slf4j.Logger; |
| 43 | import org.slf4j.LoggerFactory; |
| 44 | |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 45 | import java.util.ArrayList; |
| 46 | import java.util.Collection; |
Jonathan Hart | 08105be | 2015-10-10 18:31:06 -0700 | [diff] [blame] | 47 | import java.util.HashMap; |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 48 | import java.util.List; |
Jonathan Hart | 08105be | 2015-10-10 18:31:06 -0700 | [diff] [blame] | 49 | import java.util.Map; |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 50 | |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 51 | import static com.google.common.base.Preconditions.checkNotNull; |
| 52 | |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 53 | /** |
| 54 | * Manages the connectivity requirements between peers. |
| 55 | */ |
Jonathan Hart | ce430a4 | 2014-10-16 20:44:29 -0700 | [diff] [blame] | 56 | public class PeerConnectivityManager { |
Pavlin Radoslavov | 2aa1f32 | 2015-03-11 17:59:44 -0700 | [diff] [blame] | 57 | private static final int PRIORITY_OFFSET = 1000; |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 58 | |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 59 | private static final String SUFFIX_DST = "dst"; |
| 60 | private static final String SUFFIX_SRC = "src"; |
| 61 | private static final String SUFFIX_ICMP = "icmp"; |
| 62 | |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 63 | private static final Logger log = LoggerFactory.getLogger( |
Jonathan Hart | ce430a4 | 2014-10-16 20:44:29 -0700 | [diff] [blame] | 64 | PeerConnectivityManager.class); |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 65 | |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 66 | private static final short BGP_PORT = 179; |
| 67 | |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 68 | private final IntentSynchronizationService intentSynchronizer; |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 69 | private final NetworkConfigService configService; |
| 70 | private final InterfaceService interfaceService; |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 71 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 72 | private final ApplicationId appId; |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 73 | private final ApplicationId routerAppId; |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 74 | |
Jonathan Hart | 08105be | 2015-10-10 18:31:06 -0700 | [diff] [blame] | 75 | private final Map<Key, PointToPointIntent> peerIntents; |
| 76 | |
| 77 | private final InternalNetworkConfigListener configListener |
| 78 | = new InternalNetworkConfigListener(); |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 79 | |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 80 | /** |
| 81 | * Creates a new PeerConnectivityManager. |
| 82 | * |
Jonathan Hart | 5137218 | 2014-12-03 21:32:34 -0800 | [diff] [blame] | 83 | * @param appId the application ID |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 84 | * @param intentSynchronizer the intent synchronizer |
Jonathan Hart | 5137218 | 2014-12-03 21:32:34 -0800 | [diff] [blame] | 85 | * @param configService the SDN-IP config service |
Ray Milkey | 9b36d81 | 2015-09-09 15:24:54 -0700 | [diff] [blame] | 86 | * @param interfaceService the interface service |
| 87 | * @param routerAppId application ID |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 88 | */ |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 89 | public PeerConnectivityManager(ApplicationId appId, |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 90 | IntentSynchronizationService intentSynchronizer, |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 91 | NetworkConfigService configService, |
| 92 | ApplicationId routerAppId, |
| 93 | InterfaceService interfaceService) { |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 94 | this.appId = appId; |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 95 | this.intentSynchronizer = intentSynchronizer; |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 96 | this.configService = configService; |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 97 | this.routerAppId = routerAppId; |
| 98 | this.interfaceService = interfaceService; |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 99 | |
Jonathan Hart | 08105be | 2015-10-10 18:31:06 -0700 | [diff] [blame] | 100 | peerIntents = new HashMap<>(); |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 103 | /** |
| 104 | * Starts the peer connectivity manager. |
| 105 | */ |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 106 | public void start() { |
Jonathan Hart | 08105be | 2015-10-10 18:31:06 -0700 | [diff] [blame] | 107 | configService.addListener(configListener); |
Jonathan Hart | 6ec6829 | 2014-11-14 15:09:30 -0800 | [diff] [blame] | 108 | setUpConnectivity(); |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | /** |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 112 | * Stops the peer connectivity manager. |
| 113 | */ |
| 114 | public void stop() { |
Jonathan Hart | 08105be | 2015-10-10 18:31:06 -0700 | [diff] [blame] | 115 | configService.removeListener(configListener); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | /** |
Jonathan Hart | 6ec6829 | 2014-11-14 15:09:30 -0800 | [diff] [blame] | 119 | * Sets up paths to establish connectivity between all internal |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 120 | * BGP speakers and external BGP peers. |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 121 | */ |
Jonathan Hart | 6ec6829 | 2014-11-14 15:09:30 -0800 | [diff] [blame] | 122 | private void setUpConnectivity() { |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 123 | BgpConfig config = configService.getConfig(routerAppId, RoutingService.CONFIG_CLASS); |
| 124 | |
| 125 | if (config == null) { |
| 126 | log.warn("No BgpConfig found"); |
| 127 | return; |
| 128 | } |
| 129 | |
Jonathan Hart | 08105be | 2015-10-10 18:31:06 -0700 | [diff] [blame] | 130 | Map<Key, PointToPointIntent> existingIntents = new HashMap<>(peerIntents); |
| 131 | |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 132 | for (BgpConfig.BgpSpeakerConfig bgpSpeaker : config.bgpSpeakers()) { |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 133 | log.debug("Start to set up BGP paths for BGP speaker: {}", |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 134 | bgpSpeaker); |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 135 | |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 136 | buildSpeakerIntents(bgpSpeaker).forEach(i -> { |
Jonathan Hart | 08105be | 2015-10-10 18:31:06 -0700 | [diff] [blame] | 137 | PointToPointIntent intent = existingIntents.remove(i.key()); |
Ray Milkey | 4fd3ceb | 2015-12-10 14:43:08 -0800 | [diff] [blame^] | 138 | if (intent == null || !IntentUtils.intentsAreEqual(i, intent)) { |
Jonathan Hart | 08105be | 2015-10-10 18:31:06 -0700 | [diff] [blame] | 139 | peerIntents.put(i.key(), i); |
| 140 | intentSynchronizer.submit(i); |
| 141 | } |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 142 | }); |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 143 | } |
Jonathan Hart | 08105be | 2015-10-10 18:31:06 -0700 | [diff] [blame] | 144 | |
| 145 | // Remove any remaining intents that we used to have that we don't need |
| 146 | // anymore |
| 147 | existingIntents.values().forEach(i -> { |
| 148 | peerIntents.remove(i.key()); |
| 149 | intentSynchronizer.withdraw(i); |
| 150 | }); |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 153 | private Collection<PointToPointIntent> buildSpeakerIntents(BgpConfig.BgpSpeakerConfig speaker) { |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 154 | List<PointToPointIntent> intents = new ArrayList<>(); |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 155 | |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 156 | for (IpAddress peerAddress : speaker.peers()) { |
| 157 | Interface peeringInterface = interfaceService.getMatchingInterface(peerAddress); |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 158 | |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 159 | if (peeringInterface == null) { |
| 160 | log.debug("No peering interface found for peer {} on speaker {}", |
| 161 | peerAddress, speaker); |
Jonathan Hart | e30fcda | 2015-08-06 16:22:34 -0700 | [diff] [blame] | 162 | continue; |
| 163 | } |
| 164 | |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 165 | IpAddress peeringAddress = null; |
| 166 | for (InterfaceIpAddress address : peeringInterface.ipAddresses()) { |
| 167 | if (address.subnetAddress().contains(peerAddress)) { |
| 168 | peeringAddress = address.ipAddress(); |
Jonathan Hart | e30fcda | 2015-08-06 16:22:34 -0700 | [diff] [blame] | 169 | break; |
Jonathan Hart | 2a9ea49 | 2015-07-30 15:53:04 -0700 | [diff] [blame] | 170 | } |
Jonathan Hart | e30fcda | 2015-08-06 16:22:34 -0700 | [diff] [blame] | 171 | } |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 172 | |
| 173 | checkNotNull(peeringAddress); |
| 174 | |
| 175 | intents.addAll(buildIntents(speaker.connectPoint(), peeringAddress, |
| 176 | peeringInterface.connectPoint(), peerAddress)); |
Jonathan Hart | 6ec6829 | 2014-11-14 15:09:30 -0800 | [diff] [blame] | 177 | } |
| 178 | |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 179 | return intents; |
| 180 | } |
Jonathan Hart | 6ec6829 | 2014-11-14 15:09:30 -0800 | [diff] [blame] | 181 | |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 182 | /** |
| 183 | * Builds the required intents between the two pairs of connect points and |
| 184 | * IP addresses. |
| 185 | * |
| 186 | * @param portOne the first connect point |
| 187 | * @param ipOne the first IP address |
| 188 | * @param portTwo the second connect point |
| 189 | * @param ipTwo the second IP address |
| 190 | * @return the intents to install |
| 191 | */ |
| 192 | private Collection<PointToPointIntent> buildIntents(ConnectPoint portOne, |
| 193 | IpAddress ipOne, |
| 194 | ConnectPoint portTwo, |
| 195 | IpAddress ipTwo) { |
| 196 | |
| 197 | List<PointToPointIntent> intents = new ArrayList<>(); |
Kunihiro Ishiguro | 6e2ee15 | 2015-01-20 16:57:54 -0800 | [diff] [blame] | 198 | |
Brian O'Connor | 6b52813 | 2015-03-10 16:39:52 -0700 | [diff] [blame] | 199 | TrafficTreatment treatment = DefaultTrafficTreatment.emptyTreatment(); |
Jonathan Hart | 6ec6829 | 2014-11-14 15:09:30 -0800 | [diff] [blame] | 200 | TrafficSelector selector; |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 201 | Key key; |
Jonathan Hart | 6ec6829 | 2014-11-14 15:09:30 -0800 | [diff] [blame] | 202 | |
Kunihiro Ishiguro | 6e2ee15 | 2015-01-20 16:57:54 -0800 | [diff] [blame] | 203 | byte tcpProtocol; |
| 204 | byte icmpProtocol; |
| 205 | |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 206 | if (ipOne.isIp4()) { |
Kunihiro Ishiguro | 6e2ee15 | 2015-01-20 16:57:54 -0800 | [diff] [blame] | 207 | tcpProtocol = IPv4.PROTOCOL_TCP; |
| 208 | icmpProtocol = IPv4.PROTOCOL_ICMP; |
| 209 | } else { |
| 210 | tcpProtocol = IPv6.PROTOCOL_TCP; |
| 211 | icmpProtocol = IPv6.PROTOCOL_ICMP6; |
| 212 | } |
| 213 | |
Jonathan Hart | 5137218 | 2014-12-03 21:32:34 -0800 | [diff] [blame] | 214 | // Path from BGP speaker to BGP peer matching destination TCP port 179 |
Kunihiro Ishiguro | 6e2ee15 | 2015-01-20 16:57:54 -0800 | [diff] [blame] | 215 | selector = buildSelector(tcpProtocol, |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 216 | ipOne, |
| 217 | ipTwo, |
Ray Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 218 | null, |
| 219 | BGP_PORT); |
Jonathan Hart | 6ec6829 | 2014-11-14 15:09:30 -0800 | [diff] [blame] | 220 | |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 221 | key = buildKey(ipOne, ipTwo, SUFFIX_DST); |
| 222 | |
Ray Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 223 | intents.add(PointToPointIntent.builder() |
| 224 | .appId(appId) |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 225 | .key(key) |
Ray Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 226 | .selector(selector) |
| 227 | .treatment(treatment) |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 228 | .ingressPoint(portOne) |
| 229 | .egressPoint(portTwo) |
| 230 | .priority(PRIORITY_OFFSET) |
Ray Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 231 | .build()); |
Jonathan Hart | 6ec6829 | 2014-11-14 15:09:30 -0800 | [diff] [blame] | 232 | |
Jonathan Hart | 5137218 | 2014-12-03 21:32:34 -0800 | [diff] [blame] | 233 | // Path from BGP speaker to BGP peer matching source TCP port 179 |
Kunihiro Ishiguro | 6e2ee15 | 2015-01-20 16:57:54 -0800 | [diff] [blame] | 234 | selector = buildSelector(tcpProtocol, |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 235 | ipOne, |
| 236 | ipTwo, |
Ray Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 237 | BGP_PORT, |
| 238 | null); |
Jonathan Hart | 6ec6829 | 2014-11-14 15:09:30 -0800 | [diff] [blame] | 239 | |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 240 | key = buildKey(ipOne, ipTwo, SUFFIX_SRC); |
| 241 | |
Ray Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 242 | intents.add(PointToPointIntent.builder() |
| 243 | .appId(appId) |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 244 | .key(key) |
Ray Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 245 | .selector(selector) |
| 246 | .treatment(treatment) |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 247 | .ingressPoint(portOne) |
| 248 | .egressPoint(portTwo) |
| 249 | .priority(PRIORITY_OFFSET) |
Ray Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 250 | .build()); |
Jonathan Hart | 6ec6829 | 2014-11-14 15:09:30 -0800 | [diff] [blame] | 251 | |
Jonathan Hart | 5137218 | 2014-12-03 21:32:34 -0800 | [diff] [blame] | 252 | // Path from BGP peer to BGP speaker matching destination TCP port 179 |
Kunihiro Ishiguro | 6e2ee15 | 2015-01-20 16:57:54 -0800 | [diff] [blame] | 253 | selector = buildSelector(tcpProtocol, |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 254 | ipTwo, |
| 255 | ipOne, |
Ray Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 256 | null, |
| 257 | BGP_PORT); |
Jonathan Hart | 6ec6829 | 2014-11-14 15:09:30 -0800 | [diff] [blame] | 258 | |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 259 | key = buildKey(ipTwo, ipOne, SUFFIX_DST); |
| 260 | |
Ray Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 261 | intents.add(PointToPointIntent.builder() |
| 262 | .appId(appId) |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 263 | .key(key) |
Ray Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 264 | .selector(selector) |
| 265 | .treatment(treatment) |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 266 | .ingressPoint(portTwo) |
| 267 | .egressPoint(portOne) |
| 268 | .priority(PRIORITY_OFFSET) |
Ray Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 269 | .build()); |
Jonathan Hart | 6ec6829 | 2014-11-14 15:09:30 -0800 | [diff] [blame] | 270 | |
Jonathan Hart | 5137218 | 2014-12-03 21:32:34 -0800 | [diff] [blame] | 271 | // Path from BGP peer to BGP speaker matching source TCP port 179 |
Kunihiro Ishiguro | 6e2ee15 | 2015-01-20 16:57:54 -0800 | [diff] [blame] | 272 | selector = buildSelector(tcpProtocol, |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 273 | ipTwo, |
| 274 | ipOne, |
Ray Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 275 | BGP_PORT, |
| 276 | null); |
Jonathan Hart | 6ec6829 | 2014-11-14 15:09:30 -0800 | [diff] [blame] | 277 | |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 278 | key = buildKey(ipTwo, ipOne, SUFFIX_SRC); |
| 279 | |
Ray Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 280 | intents.add(PointToPointIntent.builder() |
| 281 | .appId(appId) |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 282 | .key(key) |
Ray Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 283 | .selector(selector) |
| 284 | .treatment(treatment) |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 285 | .ingressPoint(portTwo) |
| 286 | .egressPoint(portOne) |
| 287 | .priority(PRIORITY_OFFSET) |
Ray Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 288 | .build()); |
Jonathan Hart | 6ec6829 | 2014-11-14 15:09:30 -0800 | [diff] [blame] | 289 | |
Jonathan Hart | 5137218 | 2014-12-03 21:32:34 -0800 | [diff] [blame] | 290 | // ICMP path from BGP speaker to BGP peer |
Kunihiro Ishiguro | 6e2ee15 | 2015-01-20 16:57:54 -0800 | [diff] [blame] | 291 | selector = buildSelector(icmpProtocol, |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 292 | ipOne, |
| 293 | ipTwo, |
Ray Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 294 | null, |
| 295 | null); |
Jonathan Hart | 6ec6829 | 2014-11-14 15:09:30 -0800 | [diff] [blame] | 296 | |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 297 | key = buildKey(ipOne, ipTwo, SUFFIX_ICMP); |
| 298 | |
Ray Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 299 | intents.add(PointToPointIntent.builder() |
| 300 | .appId(appId) |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 301 | .key(key) |
Ray Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 302 | .selector(selector) |
| 303 | .treatment(treatment) |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 304 | .ingressPoint(portOne) |
| 305 | .egressPoint(portTwo) |
| 306 | .priority(PRIORITY_OFFSET) |
Ray Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 307 | .build()); |
Jonathan Hart | 6ec6829 | 2014-11-14 15:09:30 -0800 | [diff] [blame] | 308 | |
Jonathan Hart | 5137218 | 2014-12-03 21:32:34 -0800 | [diff] [blame] | 309 | // ICMP path from BGP peer to BGP speaker |
Kunihiro Ishiguro | 6e2ee15 | 2015-01-20 16:57:54 -0800 | [diff] [blame] | 310 | selector = buildSelector(icmpProtocol, |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 311 | ipTwo, |
| 312 | ipOne, |
Ray Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 313 | null, |
| 314 | null); |
Jonathan Hart | 6ec6829 | 2014-11-14 15:09:30 -0800 | [diff] [blame] | 315 | |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 316 | key = buildKey(ipTwo, ipOne, SUFFIX_ICMP); |
| 317 | |
Ray Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 318 | intents.add(PointToPointIntent.builder() |
| 319 | .appId(appId) |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 320 | .key(key) |
Ray Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 321 | .selector(selector) |
| 322 | .treatment(treatment) |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 323 | .ingressPoint(portTwo) |
| 324 | .egressPoint(portOne) |
| 325 | .priority(PRIORITY_OFFSET) |
Ray Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 326 | .build()); |
Jonathan Hart | 6ec6829 | 2014-11-14 15:09:30 -0800 | [diff] [blame] | 327 | |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 328 | return intents; |
Jonathan Hart | 6ec6829 | 2014-11-14 15:09:30 -0800 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | /** |
| 332 | * Builds a traffic selector based on the set of input parameters. |
| 333 | * |
| 334 | * @param ipProto IP protocol |
| 335 | * @param srcIp source IP address |
| 336 | * @param dstIp destination IP address |
| 337 | * @param srcTcpPort source TCP port, or null if shouldn't be set |
| 338 | * @param dstTcpPort destination TCP port, or null if shouldn't be set |
| 339 | * @return the new traffic selector |
| 340 | */ |
| 341 | private TrafficSelector buildSelector(byte ipProto, IpAddress srcIp, |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 342 | IpAddress dstIp, Short srcTcpPort, |
| 343 | Short dstTcpPort) { |
Pier Luigi Ventre | b38243f | 2015-11-11 13:01:37 +0100 | [diff] [blame] | 344 | TrafficSelector.Builder builder = DefaultTrafficSelector.builder().matchIPProtocol(ipProto); |
Kunihiro Ishiguro | 6e2ee15 | 2015-01-20 16:57:54 -0800 | [diff] [blame] | 345 | |
Pavlin Radoslavov | 87dd930 | 2015-03-10 13:53:24 -0700 | [diff] [blame] | 346 | if (dstIp.isIp4()) { |
Pier Luigi Ventre | b38243f | 2015-11-11 13:01:37 +0100 | [diff] [blame] | 347 | builder.matchEthType(Ethernet.TYPE_IPV4) |
| 348 | .matchIPSrc(IpPrefix.valueOf(srcIp, IpPrefix.MAX_INET_MASK_LENGTH)) |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 349 | .matchIPDst(IpPrefix.valueOf(dstIp, IpPrefix.MAX_INET_MASK_LENGTH)); |
Kunihiro Ishiguro | 6e2ee15 | 2015-01-20 16:57:54 -0800 | [diff] [blame] | 350 | } else { |
Pier Luigi Ventre | b38243f | 2015-11-11 13:01:37 +0100 | [diff] [blame] | 351 | builder.matchEthType(Ethernet.TYPE_IPV6) |
| 352 | .matchIPv6Src(IpPrefix.valueOf(srcIp, IpPrefix.MAX_INET6_MASK_LENGTH)) |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 353 | .matchIPv6Dst(IpPrefix.valueOf(dstIp, IpPrefix.MAX_INET6_MASK_LENGTH)); |
Kunihiro Ishiguro | 6e2ee15 | 2015-01-20 16:57:54 -0800 | [diff] [blame] | 354 | } |
Jonathan Hart | 6ec6829 | 2014-11-14 15:09:30 -0800 | [diff] [blame] | 355 | |
| 356 | if (srcTcpPort != null) { |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 357 | builder.matchTcpSrc(TpPort.tpPort(srcTcpPort)); |
Jonathan Hart | 6ec6829 | 2014-11-14 15:09:30 -0800 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | if (dstTcpPort != null) { |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 361 | builder.matchTcpDst(TpPort.tpPort(dstTcpPort)); |
Jonathan Hart | 6ec6829 | 2014-11-14 15:09:30 -0800 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | return builder.build(); |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 365 | } |
| 366 | |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 367 | /** |
| 368 | * Builds an intent Key for a point-to-point intent based off the source |
| 369 | * and destination IP address, as well as a suffix String to distinguish |
| 370 | * between different types of intents between the same source and |
| 371 | * destination. |
| 372 | * |
| 373 | * @param srcIp source IP address |
| 374 | * @param dstIp destination IP address |
| 375 | * @param suffix suffix string |
Jonathan Hart | 08105be | 2015-10-10 18:31:06 -0700 | [diff] [blame] | 376 | * @return intent key |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 377 | */ |
| 378 | private Key buildKey(IpAddress srcIp, IpAddress dstIp, String suffix) { |
| 379 | String keyString = new StringBuilder() |
| 380 | .append(srcIp.toString()) |
| 381 | .append("-") |
| 382 | .append(dstIp.toString()) |
| 383 | .append("-") |
| 384 | .append(suffix) |
| 385 | .toString(); |
| 386 | |
| 387 | return Key.of(keyString, appId); |
| 388 | } |
| 389 | |
Jonathan Hart | 08105be | 2015-10-10 18:31:06 -0700 | [diff] [blame] | 390 | private class InternalNetworkConfigListener implements NetworkConfigListener { |
| 391 | |
| 392 | @Override |
| 393 | public void event(NetworkConfigEvent event) { |
| 394 | switch (event.type()) { |
| 395 | case CONFIG_REGISTERED: |
| 396 | break; |
| 397 | case CONFIG_UNREGISTERED: |
| 398 | break; |
| 399 | case CONFIG_ADDED: |
| 400 | case CONFIG_UPDATED: |
| 401 | case CONFIG_REMOVED: |
| 402 | if (event.configClass() == RoutingService.CONFIG_CLASS) { |
| 403 | setUpConnectivity(); |
| 404 | } |
| 405 | break; |
| 406 | default: |
| 407 | break; |
| 408 | } |
| 409 | } |
| 410 | } |
| 411 | |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 412 | } |