Jonathan Hart

Rafactored PeerConnectivityManager for code reuse between the two path types.

Change-Id: Ib05f676d071ef1c545e93244a9ed5ed89672113f
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
15 */ 15 */
16 package org.onlab.onos.sdnip; 16 package org.onlab.onos.sdnip;
17 17
18 +import java.util.ArrayList;
18 import java.util.List; 19 import java.util.List;
19 20
20 import org.onlab.onos.core.ApplicationId; 21 import org.onlab.onos.core.ApplicationId;
...@@ -23,6 +24,7 @@ import org.onlab.onos.net.flow.DefaultTrafficSelector; ...@@ -23,6 +24,7 @@ import org.onlab.onos.net.flow.DefaultTrafficSelector;
23 import org.onlab.onos.net.flow.DefaultTrafficTreatment; 24 import org.onlab.onos.net.flow.DefaultTrafficTreatment;
24 import org.onlab.onos.net.flow.TrafficSelector; 25 import org.onlab.onos.net.flow.TrafficSelector;
25 import org.onlab.onos.net.flow.TrafficTreatment; 26 import org.onlab.onos.net.flow.TrafficTreatment;
27 +import org.onlab.onos.net.intent.Intent;
26 import org.onlab.onos.net.intent.IntentService; 28 import org.onlab.onos.net.intent.IntentService;
27 import org.onlab.onos.net.intent.PointToPointIntent; 29 import org.onlab.onos.net.intent.PointToPointIntent;
28 import org.onlab.onos.sdnip.bgp.BgpConstants; 30 import org.onlab.onos.sdnip.bgp.BgpConstants;
...@@ -90,39 +92,50 @@ public class PeerConnectivityManager { ...@@ -90,39 +92,50 @@ public class PeerConnectivityManager {
90 return; 92 return;
91 } 93 }
92 94
93 - setupBgpPaths(); 95 + setUpConnectivity();
94 - setupIcmpPaths();
95 } 96 }
96 97
97 /** 98 /**
98 - * Sets up paths for all {@link BgpSpeaker}s and all external peers. 99 + * Sets up paths to establish connectivity between all internal
99 - * <p/> 100 + * {@link BgpSpeaker}s and all external {@link BgpPeer}s.
100 - * Run a loop for all BGP speakers and a loop for all BGP peers outside.
101 - * Push intents for paths from each BGP speaker to all peers. Push intents
102 - * for paths from all peers to each BGP speaker.
103 */ 101 */
104 - private void setupBgpPaths() { 102 + private void setUpConnectivity() {
105 for (BgpSpeaker bgpSpeaker : configService.getBgpSpeakers() 103 for (BgpSpeaker bgpSpeaker : configService.getBgpSpeakers()
106 .values()) { 104 .values()) {
107 log.debug("Start to set up BGP paths for BGP speaker: {}", 105 log.debug("Start to set up BGP paths for BGP speaker: {}",
108 bgpSpeaker); 106 bgpSpeaker);
109 - ConnectPoint bgpdConnectPoint = bgpSpeaker.connectPoint();
110 -
111 - List<InterfaceAddress> interfaceAddresses =
112 - bgpSpeaker.interfaceAddresses();
113 107
114 for (BgpPeer bgpPeer : configService.getBgpPeers().values()) { 108 for (BgpPeer bgpPeer : configService.getBgpPeers().values()) {
115 109
116 log.debug("Start to set up BGP paths between BGP speaker: {} " 110 log.debug("Start to set up BGP paths between BGP speaker: {} "
117 + "to BGP peer: {}", bgpSpeaker, bgpPeer); 111 + "to BGP peer: {}", bgpSpeaker, bgpPeer);
118 112
113 + buildPeerIntents(bgpSpeaker, bgpPeer);
114 + }
115 + }
116 + }
117 +
118 + /**
119 + * Builds the required intents between a given internal BGP speaker and
120 + * external BGP peer.
121 + *
122 + * @param bgpSpeaker the BGP speaker
123 + * @param bgpPeer the BGP peer
124 + */
125 + private void buildPeerIntents(BgpSpeaker bgpSpeaker, BgpPeer bgpPeer) {
126 + List<Intent> intents = new ArrayList<Intent>();
127 +
128 + ConnectPoint bgpdConnectPoint = bgpSpeaker.connectPoint();
129 +
130 + List<InterfaceAddress> interfaceAddresses =
131 + bgpSpeaker.interfaceAddresses();
132 +
119 Interface peerInterface = interfaceService.getInterface( 133 Interface peerInterface = interfaceService.getInterface(
120 bgpPeer.connectPoint()); 134 bgpPeer.connectPoint());
135 +
121 if (peerInterface == null) { 136 if (peerInterface == null) {
122 - log.error("Can not find the corresponding Interface from " 137 + log.error("No interface found for peer {}", bgpPeer.ipAddress());
123 - + "configuration for BGP peer {}", 138 + return;
124 - bgpPeer.ipAddress());
125 - continue;
126 } 139 }
127 140
128 IpAddress bgpdAddress = null; 141 IpAddress bgpdAddress = null;
...@@ -134,187 +147,119 @@ public class PeerConnectivityManager { ...@@ -134,187 +147,119 @@ public class PeerConnectivityManager {
134 } 147 }
135 } 148 }
136 if (bgpdAddress == null) { 149 if (bgpdAddress == null) {
137 - log.debug("There is no interface IP address for bgpPeer: {}" 150 + log.debug("No IP address found for peer {} on interface {}",
138 - + " on interface {}", bgpPeer, bgpPeer.connectPoint()); 151 + bgpPeer, bgpPeer.connectPoint());
139 - continue; 152 + return;
140 } 153 }
141 154
142 IpAddress bgpdPeerAddress = bgpPeer.ipAddress(); 155 IpAddress bgpdPeerAddress = bgpPeer.ipAddress();
143 ConnectPoint bgpdPeerConnectPoint = peerInterface.connectPoint(); 156 ConnectPoint bgpdPeerConnectPoint = peerInterface.connectPoint();
144 157
145 - // install intent for BGP path from BGPd to BGP peer matching
146 - // destination TCP port 179
147 - TrafficSelector selector = DefaultTrafficSelector.builder()
148 - .matchEthType(Ethernet.TYPE_IPV4)
149 - .matchIPProtocol(IPv4.PROTOCOL_TCP)
150 - .matchIPSrc(IpPrefix.valueOf(bgpdAddress,
151 - IpPrefix.MAX_INET_MASK_LENGTH))
152 - .matchIPDst(IpPrefix.valueOf(bgpdPeerAddress,
153 - IpPrefix.MAX_INET_MASK_LENGTH))
154 - .matchTcpDst((short) BgpConstants.BGP_PORT)
155 - .build();
156 -
157 TrafficTreatment treatment = DefaultTrafficTreatment.builder() 158 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
158 .build(); 159 .build();
159 160
160 - PointToPointIntent intentMatchDstTcpPort = 161 + TrafficSelector selector;
161 - new PointToPointIntent(appId, selector, treatment, 162 +
162 - bgpdConnectPoint, bgpdPeerConnectPoint); 163 + // install intent for BGP path from BGPd to BGP peer matching
163 - intentService.submit(intentMatchDstTcpPort); 164 + // destination TCP port 179
164 - log.debug("Submitted BGP path intent matching dst TCP port 179 " 165 + selector = buildSelector(IPv4.PROTOCOL_TCP,
165 - + "from BGPd {} to peer {}: {}", 166 + bgpdAddress,
166 - bgpdAddress, bgpdPeerAddress, intentMatchDstTcpPort); 167 + bgpdPeerAddress,
168 + null,
169 + (short) BgpConstants.BGP_PORT);
170 +
171 + intents.add(new PointToPointIntent(appId, selector, treatment,
172 + bgpdConnectPoint, bgpdPeerConnectPoint));
167 173
168 // install intent for BGP path from BGPd to BGP peer matching 174 // install intent for BGP path from BGPd to BGP peer matching
169 // source TCP port 179 175 // source TCP port 179
170 - selector = DefaultTrafficSelector.builder() 176 + selector = buildSelector(IPv4.PROTOCOL_TCP,
171 - .matchEthType(Ethernet.TYPE_IPV4) 177 + bgpdAddress,
172 - .matchIPProtocol(IPv4.PROTOCOL_TCP) 178 + bgpdPeerAddress,
173 - .matchIPSrc(IpPrefix.valueOf(bgpdAddress, 179 + (short) BgpConstants.BGP_PORT,
174 - IpPrefix.MAX_INET_MASK_LENGTH)) 180 + null);
175 - .matchIPDst(IpPrefix.valueOf(bgpdPeerAddress,
176 - IpPrefix.MAX_INET_MASK_LENGTH))
177 - .matchTcpSrc((short) BgpConstants.BGP_PORT)
178 - .build();
179 181
180 - PointToPointIntent intentMatchSrcTcpPort = 182 + intents.add(new PointToPointIntent(appId, selector, treatment,
181 - new PointToPointIntent(appId, selector, treatment, 183 + bgpdConnectPoint, bgpdPeerConnectPoint));
182 - bgpdConnectPoint, bgpdPeerConnectPoint);
183 - intentService.submit(intentMatchSrcTcpPort);
184 - log.debug("Submitted BGP path intent matching src TCP port 179"
185 - + "from BGPd {} to peer {}: {}",
186 - bgpdAddress, bgpdPeerAddress, intentMatchSrcTcpPort);
187 184
188 // install intent for reversed BGP path from BGP peer to BGPd 185 // install intent for reversed BGP path from BGP peer to BGPd
189 // matching destination TCP port 179 186 // matching destination TCP port 179
190 - selector = DefaultTrafficSelector.builder() 187 + selector = buildSelector(IPv4.PROTOCOL_TCP,
191 - .matchEthType(Ethernet.TYPE_IPV4) 188 + bgpdPeerAddress,
192 - .matchIPProtocol(IPv4.PROTOCOL_TCP) 189 + bgpdAddress,
193 - .matchIPSrc(IpPrefix.valueOf(bgpdPeerAddress, 190 + null,
194 - IpPrefix.MAX_INET_MASK_LENGTH)) 191 + (short) BgpConstants.BGP_PORT);
195 - .matchIPDst(IpPrefix.valueOf(bgpdAddress,
196 - IpPrefix.MAX_INET_MASK_LENGTH))
197 - .matchTcpDst((short) BgpConstants.BGP_PORT)
198 - .build();
199 192
200 - PointToPointIntent reversedIntentMatchDstTcpPort = 193 + intents.add(new PointToPointIntent(appId, selector, treatment,
201 - new PointToPointIntent(appId, selector, treatment, 194 + bgpdPeerConnectPoint, bgpdConnectPoint));
202 - bgpdPeerConnectPoint, bgpdConnectPoint);
203 - intentService.submit(reversedIntentMatchDstTcpPort);
204 - log.debug("Submitted BGP path intent matching dst TCP port 179"
205 - + "from BGP peer {} to BGPd {} : {}",
206 - bgpdPeerAddress, bgpdAddress, reversedIntentMatchDstTcpPort);
207 195
208 // install intent for reversed BGP path from BGP peer to BGPd 196 // install intent for reversed BGP path from BGP peer to BGPd
209 // matching source TCP port 179 197 // matching source TCP port 179
210 - selector = DefaultTrafficSelector.builder() 198 + selector = buildSelector(IPv4.PROTOCOL_TCP,
211 - .matchEthType(Ethernet.TYPE_IPV4) 199 + bgpdPeerAddress,
212 - .matchIPProtocol(IPv4.PROTOCOL_TCP) 200 + bgpdAddress,
213 - .matchIPSrc(IpPrefix.valueOf(bgpdPeerAddress, 201 + (short) BgpConstants.BGP_PORT,
214 - IpPrefix.MAX_INET_MASK_LENGTH)) 202 + null);
215 - .matchIPDst(IpPrefix.valueOf(bgpdAddress,
216 - IpPrefix.MAX_INET_MASK_LENGTH))
217 - .matchTcpSrc((short) BgpConstants.BGP_PORT)
218 - .build();
219 -
220 - PointToPointIntent reversedIntentMatchSrcTcpPort =
221 - new PointToPointIntent(appId, selector, treatment,
222 - bgpdPeerConnectPoint, bgpdConnectPoint);
223 - intentService.submit(reversedIntentMatchSrcTcpPort);
224 - log.debug("Submitted BGP path intent matching src TCP port 179"
225 - + "from BGP peer {} to BGPd {} : {}",
226 - bgpdPeerAddress, bgpdAddress, reversedIntentMatchSrcTcpPort);
227 -
228 - }
229 - }
230 - }
231 -
232 - /**
233 - * Sets up ICMP paths between each {@link BgpSpeaker} and all BGP peers
234 - * located in other external networks.
235 - * <p/>
236 - * Run a loop for all BGP speakers and a loop for all BGP Peers. Push
237 - * intents for paths from each BGP speaker to all peers. Push intents
238 - * for paths from all peers to each BGP speaker.
239 - */
240 - private void setupIcmpPaths() {
241 - for (BgpSpeaker bgpSpeaker : configService.getBgpSpeakers()
242 - .values()) {
243 - log.debug("Start to set up ICMP paths for BGP speaker: {}",
244 - bgpSpeaker);
245 - ConnectPoint bgpdConnectPoint = bgpSpeaker.connectPoint();
246 - List<InterfaceAddress> interfaceAddresses = bgpSpeaker
247 - .interfaceAddresses();
248 -
249 - for (BgpPeer bgpPeer : configService.getBgpPeers().values()) {
250 -
251 - Interface peerInterface = interfaceService.getInterface(
252 - bgpPeer.connectPoint());
253 203
254 - if (peerInterface == null) { 204 + intents.add(new PointToPointIntent(appId, selector, treatment,
255 - log.error("Can not find the corresponding Interface from " 205 + bgpdPeerConnectPoint, bgpdConnectPoint));
256 - + "configuration for BGP peer {}",
257 - bgpPeer.ipAddress());
258 - continue;
259 - }
260 - IpAddress bgpdAddress = null;
261 - for (InterfaceAddress interfaceAddress : interfaceAddresses) {
262 - if (interfaceAddress.connectPoint().equals(
263 - peerInterface.connectPoint())) {
264 - bgpdAddress = interfaceAddress.ipAddress();
265 - break;
266 - }
267 -
268 - }
269 - if (bgpdAddress == null) {
270 - log.debug("There is no IP address for bgpPeer: {} on "
271 - + "interface port: {}", bgpPeer,
272 - bgpPeer.connectPoint());
273 - continue;
274 - }
275 -
276 - IpAddress bgpdPeerAddress = bgpPeer.ipAddress();
277 - ConnectPoint bgpdPeerConnectPoint = peerInterface.connectPoint();
278 206
279 // install intent for ICMP path from BGPd to BGP peer 207 // install intent for ICMP path from BGPd to BGP peer
280 - TrafficSelector selector = DefaultTrafficSelector.builder() 208 + selector = buildSelector(IPv4.PROTOCOL_ICMP,
281 - .matchEthType(Ethernet.TYPE_IPV4) 209 + bgpdAddress,
282 - .matchIPProtocol(IPv4.PROTOCOL_ICMP) 210 + bgpdPeerAddress,
283 - .matchIPSrc(IpPrefix.valueOf(bgpdAddress, 211 + null,
284 - IpPrefix.MAX_INET_MASK_LENGTH)) 212 + null);
285 - .matchIPDst(IpPrefix.valueOf(bgpdPeerAddress,
286 - IpPrefix.MAX_INET_MASK_LENGTH))
287 - .build();
288 213
289 - TrafficTreatment treatment = DefaultTrafficTreatment.builder() 214 + intents.add(new PointToPointIntent(appId, selector, treatment,
290 - .build(); 215 + bgpdConnectPoint, bgpdPeerConnectPoint));
291 216
292 - PointToPointIntent intent = 217 + // install intent for reversed ICMP path from BGP peer to BGPd
293 - new PointToPointIntent(appId, selector, treatment, 218 + selector = buildSelector(IPv4.PROTOCOL_ICMP,
294 - bgpdConnectPoint, bgpdPeerConnectPoint); 219 + bgpdPeerAddress,
220 + bgpdAddress,
221 + null,
222 + null);
223 +
224 + intents.add(new PointToPointIntent(appId, selector, treatment,
225 + bgpdPeerConnectPoint, bgpdConnectPoint));
226 +
227 + // Submit all the intents.
228 + // TODO submit as a batch
229 + for (Intent intent : intents) {
295 intentService.submit(intent); 230 intentService.submit(intent);
296 - log.debug("Submitted ICMP path intent from BGPd {} to peer {} :" 231 + }
297 - + " {}", bgpdAddress, bgpdPeerAddress, intent); 232 + }
298 233
299 - // install intent for reversed ICMP path from BGP peer to BGPd 234 + /**
300 - selector = DefaultTrafficSelector.builder() 235 + * Builds a traffic selector based on the set of input parameters.
236 + *
237 + * @param ipProto IP protocol
238 + * @param srcIp source IP address
239 + * @param dstIp destination IP address
240 + * @param srcTcpPort source TCP port, or null if shouldn't be set
241 + * @param dstTcpPort destination TCP port, or null if shouldn't be set
242 + * @return the new traffic selector
243 + */
244 + private TrafficSelector buildSelector(byte ipProto, IpAddress srcIp,
245 + IpAddress dstIp, Short srcTcpPort, Short dstTcpPort) {
246 + TrafficSelector.Builder builder = DefaultTrafficSelector.builder()
301 .matchEthType(Ethernet.TYPE_IPV4) 247 .matchEthType(Ethernet.TYPE_IPV4)
302 - .matchIPProtocol(IPv4.PROTOCOL_ICMP) 248 + .matchIPProtocol(ipProto)
303 - .matchIPSrc(IpPrefix.valueOf(bgpdPeerAddress, 249 + .matchIPSrc(IpPrefix.valueOf(srcIp,
304 - IpPrefix.MAX_INET_MASK_LENGTH))
305 - .matchIPDst(IpPrefix.valueOf(bgpdAddress,
306 IpPrefix.MAX_INET_MASK_LENGTH)) 250 IpPrefix.MAX_INET_MASK_LENGTH))
307 - .build(); 251 + .matchIPDst(IpPrefix.valueOf(dstIp,
252 + IpPrefix.MAX_INET_MASK_LENGTH));
308 253
309 - PointToPointIntent reversedIntent = 254 + if (srcTcpPort != null) {
310 - new PointToPointIntent(appId, selector, treatment, 255 + builder.matchTcpSrc(srcTcpPort);
311 - bgpdPeerConnectPoint, bgpdConnectPoint);
312 - intentService.submit(reversedIntent);
313 - log.debug("Submitted ICMP path intent from BGP peer {} to BGPd"
314 - + " {} : {}",
315 - bgpdPeerAddress, bgpdAddress, reversedIntent);
316 } 256 }
257 +
258 + if (dstTcpPort != null) {
259 + builder.matchTcpDst(dstTcpPort);
317 } 260 }
261 +
262 + return builder.build();
318 } 263 }
319 264
320 } 265 }
......