blob: b558c793b5c24ca00455cdc42651191b3344b7b7 [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;
Luca Prete86ac7d12015-12-02 23:36:49 -080036import org.onosproject.net.intent.IntentUtils;
Jonathan Hart9a426f82015-09-03 15:43:13 +020037import org.onosproject.net.intent.Key;
Brian O'Connorabafb502014-12-02 22:26:20 -080038import org.onosproject.net.intent.PointToPointIntent;
Jonathan Hart9a426f82015-09-03 15:43:13 +020039import org.onosproject.routing.IntentSynchronizationService;
Jonathan Hart4cb39882015-08-12 23:50:55 -040040import org.onosproject.routing.RoutingService;
41import org.onosproject.routing.config.BgpConfig;
Jonathan Hartdc711bd2014-10-15 11:24:23 -070042import org.slf4j.Logger;
43import org.slf4j.LoggerFactory;
44
Jonathan Hart41349e92015-02-09 14:14:02 -080045import java.util.ArrayList;
46import java.util.Collection;
Jonathan Hart08105be2015-10-10 18:31:06 -070047import java.util.HashMap;
Jonathan Hart41349e92015-02-09 14:14:02 -080048import java.util.List;
Jonathan Hart08105be2015-10-10 18:31:06 -070049import java.util.Map;
Jonathan Hart41349e92015-02-09 14:14:02 -080050
Jonathan Hart4cb39882015-08-12 23:50:55 -040051import static com.google.common.base.Preconditions.checkNotNull;
52
Jonathan Hartdc711bd2014-10-15 11:24:23 -070053/**
54 * Manages the connectivity requirements between peers.
55 */
Jonathan Hartce430a42014-10-16 20:44:29 -070056public class PeerConnectivityManager {
Pavlin Radoslavov2aa1f322015-03-11 17:59:44 -070057 private static final int PRIORITY_OFFSET = 1000;
Jonathan Hartdc711bd2014-10-15 11:24:23 -070058
Jonathan Hart9a426f82015-09-03 15:43:13 +020059 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 Hartdc711bd2014-10-15 11:24:23 -070063 private static final Logger log = LoggerFactory.getLogger(
Jonathan Hartce430a42014-10-16 20:44:29 -070064 PeerConnectivityManager.class);
Jonathan Hartdc711bd2014-10-15 11:24:23 -070065
Jonathan Hart41349e92015-02-09 14:14:02 -080066 private static final short BGP_PORT = 179;
67
Jonathan Hart9a426f82015-09-03 15:43:13 +020068 private final IntentSynchronizationService intentSynchronizer;
Jonathan Hart4cb39882015-08-12 23:50:55 -040069 private final NetworkConfigService configService;
70 private final InterfaceService interfaceService;
Jonathan Hartdc711bd2014-10-15 11:24:23 -070071
Thomas Vachuskab97cf282014-10-20 23:31:12 -070072 private final ApplicationId appId;
Jonathan Hart4cb39882015-08-12 23:50:55 -040073 private final ApplicationId routerAppId;
Jonathan Hartdc711bd2014-10-15 11:24:23 -070074
Jonathan Hart08105be2015-10-10 18:31:06 -070075 private final Map<Key, PointToPointIntent> peerIntents;
76
77 private final InternalNetworkConfigListener configListener
78 = new InternalNetworkConfigListener();
Jonathan Hart9a426f82015-09-03 15:43:13 +020079
Jonathan Hart31582d12014-10-22 13:52:41 -070080 /**
81 * Creates a new PeerConnectivityManager.
82 *
Jonathan Hart51372182014-12-03 21:32:34 -080083 * @param appId the application ID
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -080084 * @param intentSynchronizer the intent synchronizer
Jonathan Hart51372182014-12-03 21:32:34 -080085 * @param configService the SDN-IP config service
Ray Milkey9b36d812015-09-09 15:24:54 -070086 * @param interfaceService the interface service
87 * @param routerAppId application ID
Jonathan Hart31582d12014-10-22 13:52:41 -070088 */
Thomas Vachuskab97cf282014-10-20 23:31:12 -070089 public PeerConnectivityManager(ApplicationId appId,
Jonathan Hart9a426f82015-09-03 15:43:13 +020090 IntentSynchronizationService intentSynchronizer,
Jonathan Hart4cb39882015-08-12 23:50:55 -040091 NetworkConfigService configService,
92 ApplicationId routerAppId,
93 InterfaceService interfaceService) {
Thomas Vachuskab97cf282014-10-20 23:31:12 -070094 this.appId = appId;
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -080095 this.intentSynchronizer = intentSynchronizer;
Jonathan Hart31582d12014-10-22 13:52:41 -070096 this.configService = configService;
Jonathan Hart4cb39882015-08-12 23:50:55 -040097 this.routerAppId = routerAppId;
98 this.interfaceService = interfaceService;
Jonathan Hart9a426f82015-09-03 15:43:13 +020099
Jonathan Hart08105be2015-10-10 18:31:06 -0700100 peerIntents = new HashMap<>();
Jonathan Hartdc711bd2014-10-15 11:24:23 -0700101 }
102
Jonathan Hart31582d12014-10-22 13:52:41 -0700103 /**
104 * Starts the peer connectivity manager.
105 */
Jonathan Hartdc711bd2014-10-15 11:24:23 -0700106 public void start() {
Jonathan Hart08105be2015-10-10 18:31:06 -0700107 configService.addListener(configListener);
Jonathan Hart6ec68292014-11-14 15:09:30 -0800108 setUpConnectivity();
Jonathan Hartdc711bd2014-10-15 11:24:23 -0700109 }
110
111 /**
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800112 * Stops the peer connectivity manager.
113 */
114 public void stop() {
Jonathan Hart08105be2015-10-10 18:31:06 -0700115 configService.removeListener(configListener);
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800116 }
117
118 /**
Jonathan Hart6ec68292014-11-14 15:09:30 -0800119 * Sets up paths to establish connectivity between all internal
Jonathan Hart4cb39882015-08-12 23:50:55 -0400120 * BGP speakers and external BGP peers.
Jonathan Hartdc711bd2014-10-15 11:24:23 -0700121 */
Jonathan Hart6ec68292014-11-14 15:09:30 -0800122 private void setUpConnectivity() {
Jonathan Hart4cb39882015-08-12 23:50:55 -0400123 BgpConfig config = configService.getConfig(routerAppId, RoutingService.CONFIG_CLASS);
124
125 if (config == null) {
126 log.warn("No BgpConfig found");
127 return;
128 }
129
Jonathan Hart08105be2015-10-10 18:31:06 -0700130 Map<Key, PointToPointIntent> existingIntents = new HashMap<>(peerIntents);
131
Jonathan Hart4cb39882015-08-12 23:50:55 -0400132 for (BgpConfig.BgpSpeakerConfig bgpSpeaker : config.bgpSpeakers()) {
Jonathan Hartdc711bd2014-10-15 11:24:23 -0700133 log.debug("Start to set up BGP paths for BGP speaker: {}",
Jonathan Hart4cb39882015-08-12 23:50:55 -0400134 bgpSpeaker);
Jonathan Hartdc711bd2014-10-15 11:24:23 -0700135
Jonathan Hart9a426f82015-09-03 15:43:13 +0200136 buildSpeakerIntents(bgpSpeaker).forEach(i -> {
Jonathan Hart08105be2015-10-10 18:31:06 -0700137 PointToPointIntent intent = existingIntents.remove(i.key());
Ray Milkey4fd3ceb2015-12-10 14:43:08 -0800138 if (intent == null || !IntentUtils.intentsAreEqual(i, intent)) {
Jonathan Hart08105be2015-10-10 18:31:06 -0700139 peerIntents.put(i.key(), i);
140 intentSynchronizer.submit(i);
141 }
Jonathan Hart9a426f82015-09-03 15:43:13 +0200142 });
Jonathan Hart9a426f82015-09-03 15:43:13 +0200143 }
Jonathan Hart08105be2015-10-10 18:31:06 -0700144
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 Hartdc711bd2014-10-15 11:24:23 -0700151 }
152
Jonathan Hart4cb39882015-08-12 23:50:55 -0400153 private Collection<PointToPointIntent> buildSpeakerIntents(BgpConfig.BgpSpeakerConfig speaker) {
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800154 List<PointToPointIntent> intents = new ArrayList<>();
Jonathan Hartdc711bd2014-10-15 11:24:23 -0700155
Jonathan Hart4cb39882015-08-12 23:50:55 -0400156 for (IpAddress peerAddress : speaker.peers()) {
157 Interface peeringInterface = interfaceService.getMatchingInterface(peerAddress);
Jonathan Hartdc711bd2014-10-15 11:24:23 -0700158
Jonathan Hart4cb39882015-08-12 23:50:55 -0400159 if (peeringInterface == null) {
160 log.debug("No peering interface found for peer {} on speaker {}",
161 peerAddress, speaker);
Jonathan Harte30fcda2015-08-06 16:22:34 -0700162 continue;
163 }
164
Jonathan Hart4cb39882015-08-12 23:50:55 -0400165 IpAddress peeringAddress = null;
166 for (InterfaceIpAddress address : peeringInterface.ipAddresses()) {
167 if (address.subnetAddress().contains(peerAddress)) {
168 peeringAddress = address.ipAddress();
Jonathan Harte30fcda2015-08-06 16:22:34 -0700169 break;
Jonathan Hart2a9ea492015-07-30 15:53:04 -0700170 }
Jonathan Harte30fcda2015-08-06 16:22:34 -0700171 }
Jonathan Hart4cb39882015-08-12 23:50:55 -0400172
173 checkNotNull(peeringAddress);
174
175 intents.addAll(buildIntents(speaker.connectPoint(), peeringAddress,
176 peeringInterface.connectPoint(), peerAddress));
Jonathan Hart6ec68292014-11-14 15:09:30 -0800177 }
178
Jonathan Hart4cb39882015-08-12 23:50:55 -0400179 return intents;
180 }
Jonathan Hart6ec68292014-11-14 15:09:30 -0800181
Jonathan Hart4cb39882015-08-12 23:50:55 -0400182 /**
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 Ishiguro6e2ee152015-01-20 16:57:54 -0800198
Brian O'Connor6b528132015-03-10 16:39:52 -0700199 TrafficTreatment treatment = DefaultTrafficTreatment.emptyTreatment();
Jonathan Hart6ec68292014-11-14 15:09:30 -0800200 TrafficSelector selector;
Jonathan Hart9a426f82015-09-03 15:43:13 +0200201 Key key;
Jonathan Hart6ec68292014-11-14 15:09:30 -0800202
Kunihiro Ishiguro6e2ee152015-01-20 16:57:54 -0800203 byte tcpProtocol;
204 byte icmpProtocol;
205
Jonathan Hart4cb39882015-08-12 23:50:55 -0400206 if (ipOne.isIp4()) {
Kunihiro Ishiguro6e2ee152015-01-20 16:57:54 -0800207 tcpProtocol = IPv4.PROTOCOL_TCP;
208 icmpProtocol = IPv4.PROTOCOL_ICMP;
209 } else {
210 tcpProtocol = IPv6.PROTOCOL_TCP;
211 icmpProtocol = IPv6.PROTOCOL_ICMP6;
212 }
213
Jonathan Hart51372182014-12-03 21:32:34 -0800214 // Path from BGP speaker to BGP peer matching destination TCP port 179
Kunihiro Ishiguro6e2ee152015-01-20 16:57:54 -0800215 selector = buildSelector(tcpProtocol,
Jonathan Hart4cb39882015-08-12 23:50:55 -0400216 ipOne,
217 ipTwo,
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700218 null,
219 BGP_PORT);
Jonathan Hart6ec68292014-11-14 15:09:30 -0800220
Jonathan Hart9a426f82015-09-03 15:43:13 +0200221 key = buildKey(ipOne, ipTwo, SUFFIX_DST);
222
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700223 intents.add(PointToPointIntent.builder()
224 .appId(appId)
Jonathan Hart9a426f82015-09-03 15:43:13 +0200225 .key(key)
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700226 .selector(selector)
227 .treatment(treatment)
Jonathan Hart4cb39882015-08-12 23:50:55 -0400228 .ingressPoint(portOne)
229 .egressPoint(portTwo)
230 .priority(PRIORITY_OFFSET)
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700231 .build());
Jonathan Hart6ec68292014-11-14 15:09:30 -0800232
Jonathan Hart51372182014-12-03 21:32:34 -0800233 // Path from BGP speaker to BGP peer matching source TCP port 179
Kunihiro Ishiguro6e2ee152015-01-20 16:57:54 -0800234 selector = buildSelector(tcpProtocol,
Jonathan Hart4cb39882015-08-12 23:50:55 -0400235 ipOne,
236 ipTwo,
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700237 BGP_PORT,
238 null);
Jonathan Hart6ec68292014-11-14 15:09:30 -0800239
Jonathan Hart9a426f82015-09-03 15:43:13 +0200240 key = buildKey(ipOne, ipTwo, SUFFIX_SRC);
241
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700242 intents.add(PointToPointIntent.builder()
243 .appId(appId)
Jonathan Hart9a426f82015-09-03 15:43:13 +0200244 .key(key)
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700245 .selector(selector)
246 .treatment(treatment)
Jonathan Hart4cb39882015-08-12 23:50:55 -0400247 .ingressPoint(portOne)
248 .egressPoint(portTwo)
249 .priority(PRIORITY_OFFSET)
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700250 .build());
Jonathan Hart6ec68292014-11-14 15:09:30 -0800251
Jonathan Hart51372182014-12-03 21:32:34 -0800252 // Path from BGP peer to BGP speaker matching destination TCP port 179
Kunihiro Ishiguro6e2ee152015-01-20 16:57:54 -0800253 selector = buildSelector(tcpProtocol,
Jonathan Hart4cb39882015-08-12 23:50:55 -0400254 ipTwo,
255 ipOne,
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700256 null,
257 BGP_PORT);
Jonathan Hart6ec68292014-11-14 15:09:30 -0800258
Jonathan Hart9a426f82015-09-03 15:43:13 +0200259 key = buildKey(ipTwo, ipOne, SUFFIX_DST);
260
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700261 intents.add(PointToPointIntent.builder()
262 .appId(appId)
Jonathan Hart9a426f82015-09-03 15:43:13 +0200263 .key(key)
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700264 .selector(selector)
265 .treatment(treatment)
Jonathan Hart4cb39882015-08-12 23:50:55 -0400266 .ingressPoint(portTwo)
267 .egressPoint(portOne)
268 .priority(PRIORITY_OFFSET)
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700269 .build());
Jonathan Hart6ec68292014-11-14 15:09:30 -0800270
Jonathan Hart51372182014-12-03 21:32:34 -0800271 // Path from BGP peer to BGP speaker matching source TCP port 179
Kunihiro Ishiguro6e2ee152015-01-20 16:57:54 -0800272 selector = buildSelector(tcpProtocol,
Jonathan Hart4cb39882015-08-12 23:50:55 -0400273 ipTwo,
274 ipOne,
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700275 BGP_PORT,
276 null);
Jonathan Hart6ec68292014-11-14 15:09:30 -0800277
Jonathan Hart9a426f82015-09-03 15:43:13 +0200278 key = buildKey(ipTwo, ipOne, SUFFIX_SRC);
279
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700280 intents.add(PointToPointIntent.builder()
281 .appId(appId)
Jonathan Hart9a426f82015-09-03 15:43:13 +0200282 .key(key)
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700283 .selector(selector)
284 .treatment(treatment)
Jonathan Hart4cb39882015-08-12 23:50:55 -0400285 .ingressPoint(portTwo)
286 .egressPoint(portOne)
287 .priority(PRIORITY_OFFSET)
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700288 .build());
Jonathan Hart6ec68292014-11-14 15:09:30 -0800289
Jonathan Hart51372182014-12-03 21:32:34 -0800290 // ICMP path from BGP speaker to BGP peer
Kunihiro Ishiguro6e2ee152015-01-20 16:57:54 -0800291 selector = buildSelector(icmpProtocol,
Jonathan Hart4cb39882015-08-12 23:50:55 -0400292 ipOne,
293 ipTwo,
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700294 null,
295 null);
Jonathan Hart6ec68292014-11-14 15:09:30 -0800296
Jonathan Hart9a426f82015-09-03 15:43:13 +0200297 key = buildKey(ipOne, ipTwo, SUFFIX_ICMP);
298
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700299 intents.add(PointToPointIntent.builder()
300 .appId(appId)
Jonathan Hart9a426f82015-09-03 15:43:13 +0200301 .key(key)
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700302 .selector(selector)
303 .treatment(treatment)
Jonathan Hart4cb39882015-08-12 23:50:55 -0400304 .ingressPoint(portOne)
305 .egressPoint(portTwo)
306 .priority(PRIORITY_OFFSET)
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700307 .build());
Jonathan Hart6ec68292014-11-14 15:09:30 -0800308
Jonathan Hart51372182014-12-03 21:32:34 -0800309 // ICMP path from BGP peer to BGP speaker
Kunihiro Ishiguro6e2ee152015-01-20 16:57:54 -0800310 selector = buildSelector(icmpProtocol,
Jonathan Hart4cb39882015-08-12 23:50:55 -0400311 ipTwo,
312 ipOne,
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700313 null,
314 null);
Jonathan Hart6ec68292014-11-14 15:09:30 -0800315
Jonathan Hart9a426f82015-09-03 15:43:13 +0200316 key = buildKey(ipTwo, ipOne, SUFFIX_ICMP);
317
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700318 intents.add(PointToPointIntent.builder()
319 .appId(appId)
Jonathan Hart9a426f82015-09-03 15:43:13 +0200320 .key(key)
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700321 .selector(selector)
322 .treatment(treatment)
Jonathan Hart4cb39882015-08-12 23:50:55 -0400323 .ingressPoint(portTwo)
324 .egressPoint(portOne)
325 .priority(PRIORITY_OFFSET)
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700326 .build());
Jonathan Hart6ec68292014-11-14 15:09:30 -0800327
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800328 return intents;
Jonathan Hart6ec68292014-11-14 15:09:30 -0800329 }
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 Radoslavova7243cc2014-11-22 21:38:02 -0800342 IpAddress dstIp, Short srcTcpPort,
343 Short dstTcpPort) {
Pier Luigi Ventreb38243f2015-11-11 13:01:37 +0100344 TrafficSelector.Builder builder = DefaultTrafficSelector.builder().matchIPProtocol(ipProto);
Kunihiro Ishiguro6e2ee152015-01-20 16:57:54 -0800345
Pavlin Radoslavov87dd9302015-03-10 13:53:24 -0700346 if (dstIp.isIp4()) {
Pier Luigi Ventreb38243f2015-11-11 13:01:37 +0100347 builder.matchEthType(Ethernet.TYPE_IPV4)
348 .matchIPSrc(IpPrefix.valueOf(srcIp, IpPrefix.MAX_INET_MASK_LENGTH))
Jonathan Hart4cb39882015-08-12 23:50:55 -0400349 .matchIPDst(IpPrefix.valueOf(dstIp, IpPrefix.MAX_INET_MASK_LENGTH));
Kunihiro Ishiguro6e2ee152015-01-20 16:57:54 -0800350 } else {
Pier Luigi Ventreb38243f2015-11-11 13:01:37 +0100351 builder.matchEthType(Ethernet.TYPE_IPV6)
352 .matchIPv6Src(IpPrefix.valueOf(srcIp, IpPrefix.MAX_INET6_MASK_LENGTH))
Jonathan Hart4cb39882015-08-12 23:50:55 -0400353 .matchIPv6Dst(IpPrefix.valueOf(dstIp, IpPrefix.MAX_INET6_MASK_LENGTH));
Kunihiro Ishiguro6e2ee152015-01-20 16:57:54 -0800354 }
Jonathan Hart6ec68292014-11-14 15:09:30 -0800355
356 if (srcTcpPort != null) {
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700357 builder.matchTcpSrc(TpPort.tpPort(srcTcpPort));
Jonathan Hart6ec68292014-11-14 15:09:30 -0800358 }
359
360 if (dstTcpPort != null) {
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700361 builder.matchTcpDst(TpPort.tpPort(dstTcpPort));
Jonathan Hart6ec68292014-11-14 15:09:30 -0800362 }
363
364 return builder.build();
Jonathan Hartdc711bd2014-10-15 11:24:23 -0700365 }
366
Jonathan Hart9a426f82015-09-03 15:43:13 +0200367 /**
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 Hart08105be2015-10-10 18:31:06 -0700376 * @return intent key
Jonathan Hart9a426f82015-09-03 15:43:13 +0200377 */
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 Hart08105be2015-10-10 18:31:06 -0700390 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 Hartdc711bd2014-10-15 11:24:23 -0700412}