blob: a27baaf9ff4ca12269ac39aeb318b3d35f91c1c7 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 Open Networking Laboratory
Thomas Vachuska781d18b2014-10-27 10:31:25 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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 Vachuska781d18b2014-10-27 10:31:25 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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 Vachuska781d18b2014-10-27 10:31:25 -070015 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.sdnip;
Jonathan Hartdc711bd2014-10-15 11:24:23 -070017
Jonathan Hart51372182014-12-03 21:32:34 -080018import org.onlab.packet.Ethernet;
19import org.onlab.packet.IPv4;
Kunihiro Ishiguro6e2ee152015-01-20 16:57:54 -080020import org.onlab.packet.IPv6;
Jonathan Hart41349e92015-02-09 14:14:02 -080021import org.onlab.packet.IpAddress;
Jonathan Hart51372182014-12-03 21:32:34 -080022import org.onlab.packet.IpPrefix;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -070023import org.onlab.packet.TpPort;
Brian O'Connorabafb502014-12-02 22:26:20 -080024import org.onosproject.core.ApplicationId;
Jonathan Hart4cb39882015-08-12 23:50:55 -040025import org.onosproject.incubator.net.intf.Interface;
26import org.onosproject.incubator.net.intf.InterfaceService;
Brian O'Connorabafb502014-12-02 22:26:20 -080027import org.onosproject.net.ConnectPoint;
Jonathan Hart08105be2015-10-10 18:31:06 -070028import org.onosproject.net.config.NetworkConfigEvent;
29import org.onosproject.net.config.NetworkConfigListener;
Jonathan Hart9a426f82015-09-03 15:43:13 +020030import org.onosproject.net.config.NetworkConfigService;
Brian O'Connorabafb502014-12-02 22:26:20 -080031import org.onosproject.net.flow.DefaultTrafficSelector;
32import org.onosproject.net.flow.DefaultTrafficTreatment;
33import org.onosproject.net.flow.TrafficSelector;
34import org.onosproject.net.flow.TrafficTreatment;
Jonathan Hart2a9ea492015-07-30 15:53:04 -070035import org.onosproject.net.host.InterfaceIpAddress;
Jonathan Hart9a426f82015-09-03 15:43:13 +020036import org.onosproject.net.intent.Key;
Brian O'Connorabafb502014-12-02 22:26:20 -080037import org.onosproject.net.intent.PointToPointIntent;
Jonathan Hart9a426f82015-09-03 15:43:13 +020038import org.onosproject.routing.IntentSynchronizationService;
Jonathan Hart4cb39882015-08-12 23:50:55 -040039import org.onosproject.routing.RoutingService;
40import org.onosproject.routing.config.BgpConfig;
Jonathan Hartdc711bd2014-10-15 11:24:23 -070041import org.slf4j.Logger;
42import org.slf4j.LoggerFactory;
43
Jonathan Hart41349e92015-02-09 14:14:02 -080044import java.util.ArrayList;
45import java.util.Collection;
Jonathan Hart08105be2015-10-10 18:31:06 -070046import java.util.HashMap;
Jonathan Hart41349e92015-02-09 14:14:02 -080047import java.util.List;
Jonathan Hart08105be2015-10-10 18:31:06 -070048import java.util.Map;
Jonathan Hart41349e92015-02-09 14:14:02 -080049
Jonathan Hart4cb39882015-08-12 23:50:55 -040050import static com.google.common.base.Preconditions.checkNotNull;
51
Jonathan Hartdc711bd2014-10-15 11:24:23 -070052/**
53 * Manages the connectivity requirements between peers.
54 */
Jonathan Hartce430a42014-10-16 20:44:29 -070055public class PeerConnectivityManager {
Pavlin Radoslavov2aa1f322015-03-11 17:59:44 -070056 private static final int PRIORITY_OFFSET = 1000;
Jonathan Hartdc711bd2014-10-15 11:24:23 -070057
Jonathan Hart9a426f82015-09-03 15:43:13 +020058 private static final String SUFFIX_DST = "dst";
59 private static final String SUFFIX_SRC = "src";
60 private static final String SUFFIX_ICMP = "icmp";
61
Jonathan Hartdc711bd2014-10-15 11:24:23 -070062 private static final Logger log = LoggerFactory.getLogger(
Jonathan Hartce430a42014-10-16 20:44:29 -070063 PeerConnectivityManager.class);
Jonathan Hartdc711bd2014-10-15 11:24:23 -070064
Jonathan Hart41349e92015-02-09 14:14:02 -080065 private static final short BGP_PORT = 179;
66
Jonathan Hart9a426f82015-09-03 15:43:13 +020067 private final IntentSynchronizationService intentSynchronizer;
Jonathan Hart4cb39882015-08-12 23:50:55 -040068 private final NetworkConfigService configService;
69 private final InterfaceService interfaceService;
Jonathan Hartdc711bd2014-10-15 11:24:23 -070070
Thomas Vachuskab97cf282014-10-20 23:31:12 -070071 private final ApplicationId appId;
Jonathan Hart4cb39882015-08-12 23:50:55 -040072 private final ApplicationId routerAppId;
Jonathan Hartdc711bd2014-10-15 11:24:23 -070073
Jonathan Hart08105be2015-10-10 18:31:06 -070074 private final Map<Key, PointToPointIntent> peerIntents;
75
76 private final InternalNetworkConfigListener configListener
77 = new InternalNetworkConfigListener();
Jonathan Hart9a426f82015-09-03 15:43:13 +020078
Jonathan Hart31582d12014-10-22 13:52:41 -070079 /**
80 * Creates a new PeerConnectivityManager.
81 *
Jonathan Hart51372182014-12-03 21:32:34 -080082 * @param appId the application ID
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -080083 * @param intentSynchronizer the intent synchronizer
Jonathan Hart51372182014-12-03 21:32:34 -080084 * @param configService the SDN-IP config service
Ray Milkey9b36d812015-09-09 15:24:54 -070085 * @param interfaceService the interface service
86 * @param routerAppId application ID
Jonathan Hart31582d12014-10-22 13:52:41 -070087 */
Thomas Vachuskab97cf282014-10-20 23:31:12 -070088 public PeerConnectivityManager(ApplicationId appId,
Jonathan Hart9a426f82015-09-03 15:43:13 +020089 IntentSynchronizationService intentSynchronizer,
Jonathan Hart4cb39882015-08-12 23:50:55 -040090 NetworkConfigService configService,
91 ApplicationId routerAppId,
92 InterfaceService interfaceService) {
Thomas Vachuskab97cf282014-10-20 23:31:12 -070093 this.appId = appId;
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -080094 this.intentSynchronizer = intentSynchronizer;
Jonathan Hart31582d12014-10-22 13:52:41 -070095 this.configService = configService;
Jonathan Hart4cb39882015-08-12 23:50:55 -040096 this.routerAppId = routerAppId;
97 this.interfaceService = interfaceService;
Jonathan Hart9a426f82015-09-03 15:43:13 +020098
Jonathan Hart08105be2015-10-10 18:31:06 -070099 peerIntents = new HashMap<>();
Jonathan Hartdc711bd2014-10-15 11:24:23 -0700100 }
101
Jonathan Hart31582d12014-10-22 13:52:41 -0700102 /**
103 * Starts the peer connectivity manager.
104 */
Jonathan Hartdc711bd2014-10-15 11:24:23 -0700105 public void start() {
Jonathan Hart08105be2015-10-10 18:31:06 -0700106 configService.addListener(configListener);
Jonathan Hart6ec68292014-11-14 15:09:30 -0800107 setUpConnectivity();
Jonathan Hartdc711bd2014-10-15 11:24:23 -0700108 }
109
110 /**
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800111 * Stops the peer connectivity manager.
112 */
113 public void stop() {
Jonathan Hart08105be2015-10-10 18:31:06 -0700114 configService.removeListener(configListener);
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800115 }
116
117 /**
Jonathan Hart6ec68292014-11-14 15:09:30 -0800118 * Sets up paths to establish connectivity between all internal
Jonathan Hart4cb39882015-08-12 23:50:55 -0400119 * BGP speakers and external BGP peers.
Jonathan Hartdc711bd2014-10-15 11:24:23 -0700120 */
Jonathan Hart6ec68292014-11-14 15:09:30 -0800121 private void setUpConnectivity() {
Jonathan Hart4cb39882015-08-12 23:50:55 -0400122 BgpConfig config = configService.getConfig(routerAppId, RoutingService.CONFIG_CLASS);
123
124 if (config == null) {
125 log.warn("No BgpConfig found");
126 return;
127 }
128
Jonathan Hart08105be2015-10-10 18:31:06 -0700129 Map<Key, PointToPointIntent> existingIntents = new HashMap<>(peerIntents);
130
Jonathan Hart4cb39882015-08-12 23:50:55 -0400131 for (BgpConfig.BgpSpeakerConfig bgpSpeaker : config.bgpSpeakers()) {
Jonathan Hartdc711bd2014-10-15 11:24:23 -0700132 log.debug("Start to set up BGP paths for BGP speaker: {}",
Jonathan Hart4cb39882015-08-12 23:50:55 -0400133 bgpSpeaker);
Jonathan Hartdc711bd2014-10-15 11:24:23 -0700134
Jonathan Hart9a426f82015-09-03 15:43:13 +0200135 buildSpeakerIntents(bgpSpeaker).forEach(i -> {
Jonathan Hart08105be2015-10-10 18:31:06 -0700136 PointToPointIntent intent = existingIntents.remove(i.key());
137 if (intent == null || !IntentUtils.equals(i, intent)) {
138 peerIntents.put(i.key(), i);
139 intentSynchronizer.submit(i);
140 }
Jonathan Hart9a426f82015-09-03 15:43:13 +0200141 });
Jonathan Hart9a426f82015-09-03 15:43:13 +0200142 }
Jonathan Hart08105be2015-10-10 18:31:06 -0700143
144 // Remove any remaining intents that we used to have that we don't need
145 // anymore
146 existingIntents.values().forEach(i -> {
147 peerIntents.remove(i.key());
148 intentSynchronizer.withdraw(i);
149 });
Jonathan Hartdc711bd2014-10-15 11:24:23 -0700150 }
151
Jonathan Hart4cb39882015-08-12 23:50:55 -0400152 private Collection<PointToPointIntent> buildSpeakerIntents(BgpConfig.BgpSpeakerConfig speaker) {
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800153 List<PointToPointIntent> intents = new ArrayList<>();
Jonathan Hartdc711bd2014-10-15 11:24:23 -0700154
Jonathan Hart4cb39882015-08-12 23:50:55 -0400155 for (IpAddress peerAddress : speaker.peers()) {
156 Interface peeringInterface = interfaceService.getMatchingInterface(peerAddress);
Jonathan Hartdc711bd2014-10-15 11:24:23 -0700157
Jonathan Hart4cb39882015-08-12 23:50:55 -0400158 if (peeringInterface == null) {
159 log.debug("No peering interface found for peer {} on speaker {}",
160 peerAddress, speaker);
Jonathan Harte30fcda2015-08-06 16:22:34 -0700161 continue;
162 }
163
Jonathan Hart4cb39882015-08-12 23:50:55 -0400164 IpAddress peeringAddress = null;
165 for (InterfaceIpAddress address : peeringInterface.ipAddresses()) {
166 if (address.subnetAddress().contains(peerAddress)) {
167 peeringAddress = address.ipAddress();
Jonathan Harte30fcda2015-08-06 16:22:34 -0700168 break;
Jonathan Hart2a9ea492015-07-30 15:53:04 -0700169 }
Jonathan Harte30fcda2015-08-06 16:22:34 -0700170 }
Jonathan Hart4cb39882015-08-12 23:50:55 -0400171
172 checkNotNull(peeringAddress);
173
174 intents.addAll(buildIntents(speaker.connectPoint(), peeringAddress,
175 peeringInterface.connectPoint(), peerAddress));
Jonathan Hart6ec68292014-11-14 15:09:30 -0800176 }
177
Jonathan Hart4cb39882015-08-12 23:50:55 -0400178 return intents;
179 }
Jonathan Hart6ec68292014-11-14 15:09:30 -0800180
Jonathan Hart4cb39882015-08-12 23:50:55 -0400181 /**
182 * Builds the required intents between the two pairs of connect points and
183 * IP addresses.
184 *
185 * @param portOne the first connect point
186 * @param ipOne the first IP address
187 * @param portTwo the second connect point
188 * @param ipTwo the second IP address
189 * @return the intents to install
190 */
191 private Collection<PointToPointIntent> buildIntents(ConnectPoint portOne,
192 IpAddress ipOne,
193 ConnectPoint portTwo,
194 IpAddress ipTwo) {
195
196 List<PointToPointIntent> intents = new ArrayList<>();
Kunihiro Ishiguro6e2ee152015-01-20 16:57:54 -0800197
Brian O'Connor6b528132015-03-10 16:39:52 -0700198 TrafficTreatment treatment = DefaultTrafficTreatment.emptyTreatment();
Jonathan Hart6ec68292014-11-14 15:09:30 -0800199 TrafficSelector selector;
Jonathan Hart9a426f82015-09-03 15:43:13 +0200200 Key key;
Jonathan Hart6ec68292014-11-14 15:09:30 -0800201
Kunihiro Ishiguro6e2ee152015-01-20 16:57:54 -0800202 byte tcpProtocol;
203 byte icmpProtocol;
204
Jonathan Hart4cb39882015-08-12 23:50:55 -0400205 if (ipOne.isIp4()) {
Kunihiro Ishiguro6e2ee152015-01-20 16:57:54 -0800206 tcpProtocol = IPv4.PROTOCOL_TCP;
207 icmpProtocol = IPv4.PROTOCOL_ICMP;
208 } else {
209 tcpProtocol = IPv6.PROTOCOL_TCP;
210 icmpProtocol = IPv6.PROTOCOL_ICMP6;
211 }
212
Jonathan Hart51372182014-12-03 21:32:34 -0800213 // Path from BGP speaker to BGP peer matching destination TCP port 179
Kunihiro Ishiguro6e2ee152015-01-20 16:57:54 -0800214 selector = buildSelector(tcpProtocol,
Jonathan Hart4cb39882015-08-12 23:50:55 -0400215 ipOne,
216 ipTwo,
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700217 null,
218 BGP_PORT);
Jonathan Hart6ec68292014-11-14 15:09:30 -0800219
Jonathan Hart9a426f82015-09-03 15:43:13 +0200220 key = buildKey(ipOne, ipTwo, SUFFIX_DST);
221
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700222 intents.add(PointToPointIntent.builder()
223 .appId(appId)
Jonathan Hart9a426f82015-09-03 15:43:13 +0200224 .key(key)
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700225 .selector(selector)
226 .treatment(treatment)
Jonathan Hart4cb39882015-08-12 23:50:55 -0400227 .ingressPoint(portOne)
228 .egressPoint(portTwo)
229 .priority(PRIORITY_OFFSET)
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700230 .build());
Jonathan Hart6ec68292014-11-14 15:09:30 -0800231
Jonathan Hart51372182014-12-03 21:32:34 -0800232 // Path from BGP speaker to BGP peer matching source TCP port 179
Kunihiro Ishiguro6e2ee152015-01-20 16:57:54 -0800233 selector = buildSelector(tcpProtocol,
Jonathan Hart4cb39882015-08-12 23:50:55 -0400234 ipOne,
235 ipTwo,
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700236 BGP_PORT,
237 null);
Jonathan Hart6ec68292014-11-14 15:09:30 -0800238
Jonathan Hart9a426f82015-09-03 15:43:13 +0200239 key = buildKey(ipOne, ipTwo, SUFFIX_SRC);
240
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700241 intents.add(PointToPointIntent.builder()
242 .appId(appId)
Jonathan Hart9a426f82015-09-03 15:43:13 +0200243 .key(key)
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700244 .selector(selector)
245 .treatment(treatment)
Jonathan Hart4cb39882015-08-12 23:50:55 -0400246 .ingressPoint(portOne)
247 .egressPoint(portTwo)
248 .priority(PRIORITY_OFFSET)
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700249 .build());
Jonathan Hart6ec68292014-11-14 15:09:30 -0800250
Jonathan Hart51372182014-12-03 21:32:34 -0800251 // Path from BGP peer to BGP speaker matching destination TCP port 179
Kunihiro Ishiguro6e2ee152015-01-20 16:57:54 -0800252 selector = buildSelector(tcpProtocol,
Jonathan Hart4cb39882015-08-12 23:50:55 -0400253 ipTwo,
254 ipOne,
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700255 null,
256 BGP_PORT);
Jonathan Hart6ec68292014-11-14 15:09:30 -0800257
Jonathan Hart9a426f82015-09-03 15:43:13 +0200258 key = buildKey(ipTwo, ipOne, SUFFIX_DST);
259
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700260 intents.add(PointToPointIntent.builder()
261 .appId(appId)
Jonathan Hart9a426f82015-09-03 15:43:13 +0200262 .key(key)
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700263 .selector(selector)
264 .treatment(treatment)
Jonathan Hart4cb39882015-08-12 23:50:55 -0400265 .ingressPoint(portTwo)
266 .egressPoint(portOne)
267 .priority(PRIORITY_OFFSET)
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700268 .build());
Jonathan Hart6ec68292014-11-14 15:09:30 -0800269
Jonathan Hart51372182014-12-03 21:32:34 -0800270 // Path from BGP peer to BGP speaker matching source TCP port 179
Kunihiro Ishiguro6e2ee152015-01-20 16:57:54 -0800271 selector = buildSelector(tcpProtocol,
Jonathan Hart4cb39882015-08-12 23:50:55 -0400272 ipTwo,
273 ipOne,
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700274 BGP_PORT,
275 null);
Jonathan Hart6ec68292014-11-14 15:09:30 -0800276
Jonathan Hart9a426f82015-09-03 15:43:13 +0200277 key = buildKey(ipTwo, ipOne, SUFFIX_SRC);
278
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700279 intents.add(PointToPointIntent.builder()
280 .appId(appId)
Jonathan Hart9a426f82015-09-03 15:43:13 +0200281 .key(key)
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700282 .selector(selector)
283 .treatment(treatment)
Jonathan Hart4cb39882015-08-12 23:50:55 -0400284 .ingressPoint(portTwo)
285 .egressPoint(portOne)
286 .priority(PRIORITY_OFFSET)
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700287 .build());
Jonathan Hart6ec68292014-11-14 15:09:30 -0800288
Jonathan Hart51372182014-12-03 21:32:34 -0800289 // ICMP path from BGP speaker to BGP peer
Kunihiro Ishiguro6e2ee152015-01-20 16:57:54 -0800290 selector = buildSelector(icmpProtocol,
Jonathan Hart4cb39882015-08-12 23:50:55 -0400291 ipOne,
292 ipTwo,
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700293 null,
294 null);
Jonathan Hart6ec68292014-11-14 15:09:30 -0800295
Jonathan Hart9a426f82015-09-03 15:43:13 +0200296 key = buildKey(ipOne, ipTwo, SUFFIX_ICMP);
297
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700298 intents.add(PointToPointIntent.builder()
299 .appId(appId)
Jonathan Hart9a426f82015-09-03 15:43:13 +0200300 .key(key)
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700301 .selector(selector)
302 .treatment(treatment)
Jonathan Hart4cb39882015-08-12 23:50:55 -0400303 .ingressPoint(portOne)
304 .egressPoint(portTwo)
305 .priority(PRIORITY_OFFSET)
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700306 .build());
Jonathan Hart6ec68292014-11-14 15:09:30 -0800307
Jonathan Hart51372182014-12-03 21:32:34 -0800308 // ICMP path from BGP peer to BGP speaker
Kunihiro Ishiguro6e2ee152015-01-20 16:57:54 -0800309 selector = buildSelector(icmpProtocol,
Jonathan Hart4cb39882015-08-12 23:50:55 -0400310 ipTwo,
311 ipOne,
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700312 null,
313 null);
Jonathan Hart6ec68292014-11-14 15:09:30 -0800314
Jonathan Hart9a426f82015-09-03 15:43:13 +0200315 key = buildKey(ipTwo, ipOne, SUFFIX_ICMP);
316
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700317 intents.add(PointToPointIntent.builder()
318 .appId(appId)
Jonathan Hart9a426f82015-09-03 15:43:13 +0200319 .key(key)
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700320 .selector(selector)
321 .treatment(treatment)
Jonathan Hart4cb39882015-08-12 23:50:55 -0400322 .ingressPoint(portTwo)
323 .egressPoint(portOne)
324 .priority(PRIORITY_OFFSET)
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700325 .build());
Jonathan Hart6ec68292014-11-14 15:09:30 -0800326
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800327 return intents;
Jonathan Hart6ec68292014-11-14 15:09:30 -0800328 }
329
330 /**
331 * Builds a traffic selector based on the set of input parameters.
332 *
333 * @param ipProto IP protocol
334 * @param srcIp source IP address
335 * @param dstIp destination IP address
336 * @param srcTcpPort source TCP port, or null if shouldn't be set
337 * @param dstTcpPort destination TCP port, or null if shouldn't be set
338 * @return the new traffic selector
339 */
340 private TrafficSelector buildSelector(byte ipProto, IpAddress srcIp,
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800341 IpAddress dstIp, Short srcTcpPort,
342 Short dstTcpPort) {
Jonathan Hart4cb39882015-08-12 23:50:55 -0400343 TrafficSelector.Builder builder = DefaultTrafficSelector.builder()
344 .matchEthType(Ethernet.TYPE_IPV4)
345 .matchIPProtocol(ipProto);
Kunihiro Ishiguro6e2ee152015-01-20 16:57:54 -0800346
Pavlin Radoslavov87dd9302015-03-10 13:53:24 -0700347 if (dstIp.isIp4()) {
Jonathan Hart4cb39882015-08-12 23:50:55 -0400348 builder.matchIPSrc(IpPrefix.valueOf(srcIp, IpPrefix.MAX_INET_MASK_LENGTH))
349 .matchIPDst(IpPrefix.valueOf(dstIp, IpPrefix.MAX_INET_MASK_LENGTH));
Kunihiro Ishiguro6e2ee152015-01-20 16:57:54 -0800350 } else {
Jonathan Hart4cb39882015-08-12 23:50:55 -0400351 builder.matchIPv6Src(IpPrefix.valueOf(srcIp, IpPrefix.MAX_INET6_MASK_LENGTH))
352 .matchIPv6Dst(IpPrefix.valueOf(dstIp, IpPrefix.MAX_INET6_MASK_LENGTH));
Kunihiro Ishiguro6e2ee152015-01-20 16:57:54 -0800353 }
Jonathan Hart6ec68292014-11-14 15:09:30 -0800354
355 if (srcTcpPort != null) {
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700356 builder.matchTcpSrc(TpPort.tpPort(srcTcpPort));
Jonathan Hart6ec68292014-11-14 15:09:30 -0800357 }
358
359 if (dstTcpPort != null) {
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700360 builder.matchTcpDst(TpPort.tpPort(dstTcpPort));
Jonathan Hart6ec68292014-11-14 15:09:30 -0800361 }
362
363 return builder.build();
Jonathan Hartdc711bd2014-10-15 11:24:23 -0700364 }
365
Jonathan Hart9a426f82015-09-03 15:43:13 +0200366 /**
367 * Builds an intent Key for a point-to-point intent based off the source
368 * and destination IP address, as well as a suffix String to distinguish
369 * between different types of intents between the same source and
370 * destination.
371 *
372 * @param srcIp source IP address
373 * @param dstIp destination IP address
374 * @param suffix suffix string
Jonathan Hart08105be2015-10-10 18:31:06 -0700375 * @return intent key
Jonathan Hart9a426f82015-09-03 15:43:13 +0200376 */
377 private Key buildKey(IpAddress srcIp, IpAddress dstIp, String suffix) {
378 String keyString = new StringBuilder()
379 .append(srcIp.toString())
380 .append("-")
381 .append(dstIp.toString())
382 .append("-")
383 .append(suffix)
384 .toString();
385
386 return Key.of(keyString, appId);
387 }
388
Jonathan Hart08105be2015-10-10 18:31:06 -0700389 private class InternalNetworkConfigListener implements NetworkConfigListener {
390
391 @Override
392 public void event(NetworkConfigEvent event) {
393 switch (event.type()) {
394 case CONFIG_REGISTERED:
395 break;
396 case CONFIG_UNREGISTERED:
397 break;
398 case CONFIG_ADDED:
399 case CONFIG_UPDATED:
400 case CONFIG_REMOVED:
401 if (event.configClass() == RoutingService.CONFIG_CLASS) {
402 setUpConnectivity();
403 }
404 break;
405 default:
406 break;
407 }
408 }
409 }
410
Jonathan Hartdc711bd2014-10-15 11:24:23 -0700411}