PeerConnectivityManager.java
10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
* Copyright 2014 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.sdnip;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.onosproject.core.ApplicationId;
import org.onosproject.net.ConnectPoint;
import org.onosproject.net.flow.DefaultTrafficSelector;
import org.onosproject.net.flow.DefaultTrafficTreatment;
import org.onosproject.net.flow.TrafficSelector;
import org.onosproject.net.flow.TrafficTreatment;
import org.onosproject.net.intent.PointToPointIntent;
import org.onosproject.sdnip.bgp.BgpConstants;
import org.onosproject.sdnip.config.BgpPeer;
import org.onosproject.sdnip.config.BgpSpeaker;
import org.onosproject.sdnip.config.Interface;
import org.onosproject.sdnip.config.InterfaceAddress;
import org.onosproject.sdnip.config.SdnIpConfigurationService;
import org.onlab.packet.Ethernet;
import org.onlab.packet.IPv4;
import org.onlab.packet.IpAddress;
import org.onlab.packet.IpPrefix;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Manages the connectivity requirements between peers.
*/
public class PeerConnectivityManager {
private static final Logger log = LoggerFactory.getLogger(
PeerConnectivityManager.class);
private final IntentSynchronizer intentSynchronizer;
private final SdnIpConfigurationService configService;
private final InterfaceService interfaceService;
private final ApplicationId appId;
/**
* Creates a new PeerConnectivityManager.
*
* @param appId the application ID
* @param intentSynchronizer the intent synchronizer
* @param configService the SDN-IP config service
* @param interfaceService the interface service
*/
public PeerConnectivityManager(ApplicationId appId,
IntentSynchronizer intentSynchronizer,
SdnIpConfigurationService configService,
InterfaceService interfaceService) {
this.appId = appId;
this.intentSynchronizer = intentSynchronizer;
this.configService = configService;
this.interfaceService = interfaceService;
}
/**
* Starts the peer connectivity manager.
*/
public void start() {
// TODO are any of these errors?
if (interfaceService.getInterfaces().isEmpty()) {
log.warn("The interface in configuration file is empty. "
+ "Thus, the SDN-IP application can not be started.");
} else if (configService.getBgpPeers().isEmpty()) {
log.warn("The BGP peer in configuration file is empty."
+ "Thus, the SDN-IP application can not be started.");
} else if (configService.getBgpSpeakers() == null) {
log.error("The BGP speaker in configuration file is empty. "
+ "Thus, the SDN-IP application can not be started.");
return;
}
setUpConnectivity();
}
/**
* Stops the peer connectivity manager.
*/
public void stop() {
// TODO: Implement it
}
/**
* Sets up paths to establish connectivity between all internal
* {@link BgpSpeaker}s and all external {@link BgpPeer}s.
*/
private void setUpConnectivity() {
List<PointToPointIntent> intents = new ArrayList<>();
for (BgpSpeaker bgpSpeaker : configService.getBgpSpeakers()
.values()) {
log.debug("Start to set up BGP paths for BGP speaker: {}",
bgpSpeaker);
for (BgpPeer bgpPeer : configService.getBgpPeers().values()) {
log.debug("Start to set up BGP paths between BGP speaker: {} "
+ "to BGP peer: {}", bgpSpeaker, bgpPeer);
intents.addAll(buildPeerIntents(bgpSpeaker, bgpPeer));
}
}
// Submit all the intents.
intentSynchronizer.submitPeerIntents(intents);
}
/**
* Builds the required intents between a given internal BGP speaker and
* external BGP peer.
*
* @param bgpSpeaker the BGP speaker
* @param bgpPeer the BGP peer
* @return the intents to install
*/
private Collection<PointToPointIntent> buildPeerIntents(
BgpSpeaker bgpSpeaker,
BgpPeer bgpPeer) {
List<PointToPointIntent> intents = new ArrayList<>();
ConnectPoint bgpdConnectPoint = bgpSpeaker.connectPoint();
List<InterfaceAddress> interfaceAddresses =
bgpSpeaker.interfaceAddresses();
Interface peerInterface = interfaceService.getInterface(
bgpPeer.connectPoint());
if (peerInterface == null) {
log.error("No interface found for peer {}", bgpPeer.ipAddress());
return intents;
}
IpAddress bgpdAddress = null;
for (InterfaceAddress interfaceAddress : interfaceAddresses) {
if (interfaceAddress.connectPoint().equals(
peerInterface.connectPoint())) {
bgpdAddress = interfaceAddress.ipAddress();
break;
}
}
if (bgpdAddress == null) {
log.debug("No IP address found for peer {} on interface {}",
bgpPeer, bgpPeer.connectPoint());
return intents;
}
IpAddress bgpdPeerAddress = bgpPeer.ipAddress();
ConnectPoint bgpdPeerConnectPoint = peerInterface.connectPoint();
TrafficTreatment treatment = DefaultTrafficTreatment.builder()
.build();
TrafficSelector selector;
// install intent for BGP path from BGPd to BGP peer matching
// destination TCP port 179
selector = buildSelector(IPv4.PROTOCOL_TCP,
bgpdAddress,
bgpdPeerAddress,
null,
(short) BgpConstants.BGP_PORT);
intents.add(new PointToPointIntent(appId, selector, treatment,
bgpdConnectPoint, bgpdPeerConnectPoint));
// install intent for BGP path from BGPd to BGP peer matching
// source TCP port 179
selector = buildSelector(IPv4.PROTOCOL_TCP,
bgpdAddress,
bgpdPeerAddress,
(short) BgpConstants.BGP_PORT,
null);
intents.add(new PointToPointIntent(appId, selector, treatment,
bgpdConnectPoint, bgpdPeerConnectPoint));
// install intent for reversed BGP path from BGP peer to BGPd
// matching destination TCP port 179
selector = buildSelector(IPv4.PROTOCOL_TCP,
bgpdPeerAddress,
bgpdAddress,
null,
(short) BgpConstants.BGP_PORT);
intents.add(new PointToPointIntent(appId, selector, treatment,
bgpdPeerConnectPoint, bgpdConnectPoint));
// install intent for reversed BGP path from BGP peer to BGPd
// matching source TCP port 179
selector = buildSelector(IPv4.PROTOCOL_TCP,
bgpdPeerAddress,
bgpdAddress,
(short) BgpConstants.BGP_PORT,
null);
intents.add(new PointToPointIntent(appId, selector, treatment,
bgpdPeerConnectPoint, bgpdConnectPoint));
// install intent for ICMP path from BGPd to BGP peer
selector = buildSelector(IPv4.PROTOCOL_ICMP,
bgpdAddress,
bgpdPeerAddress,
null,
null);
intents.add(new PointToPointIntent(appId, selector, treatment,
bgpdConnectPoint, bgpdPeerConnectPoint));
// install intent for reversed ICMP path from BGP peer to BGPd
selector = buildSelector(IPv4.PROTOCOL_ICMP,
bgpdPeerAddress,
bgpdAddress,
null,
null);
intents.add(new PointToPointIntent(appId, selector, treatment,
bgpdPeerConnectPoint, bgpdConnectPoint));
return intents;
}
/**
* Builds a traffic selector based on the set of input parameters.
*
* @param ipProto IP protocol
* @param srcIp source IP address
* @param dstIp destination IP address
* @param srcTcpPort source TCP port, or null if shouldn't be set
* @param dstTcpPort destination TCP port, or null if shouldn't be set
* @return the new traffic selector
*/
private TrafficSelector buildSelector(byte ipProto, IpAddress srcIp,
IpAddress dstIp, Short srcTcpPort,
Short dstTcpPort) {
TrafficSelector.Builder builder = DefaultTrafficSelector.builder()
.matchEthType(Ethernet.TYPE_IPV4)
.matchIPProtocol(ipProto)
.matchIPSrc(IpPrefix.valueOf(srcIp,
IpPrefix.MAX_INET_MASK_LENGTH))
.matchIPDst(IpPrefix.valueOf(dstIp,
IpPrefix.MAX_INET_MASK_LENGTH));
if (srcTcpPort != null) {
builder.matchTcpSrc(srcTcpPort);
}
if (dstTcpPort != null) {
builder.matchTcpDst(dstTcpPort);
}
return builder.build();
}
}