blob: ba205bc11e3af3d17b6b36d3736d876731816c6f [file] [log] [blame]
Thejaswi N K38879622015-12-08 22:14:47 +05301/*
2 * Copyright 2015 Open Networking Laboratory
3 *
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
7 *
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.
15 */
16package org.onosproject.provider.bgp.cfg.impl;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import org.apache.felix.scr.annotations.Reference;
20import org.apache.felix.scr.annotations.ReferenceCardinality;
21import org.onlab.osgi.DefaultServiceDirectory;
22import org.onlab.packet.IpAddress;
23import org.onosproject.bgp.controller.BgpCfg;
24import org.onosproject.bgp.controller.BgpController;
25import org.onosproject.core.ApplicationId;
26import org.onosproject.net.config.Config;
27
28import java.util.ArrayList;
29import java.util.List;
30
31import static org.onosproject.net.config.Config.FieldPresence.MANDATORY;
32import static org.onosproject.net.config.Config.FieldPresence.OPTIONAL;
33import static com.google.common.base.Preconditions.checkNotNull;
34
35/**
36 * Configuration object for BGP.
37 */
38public class BgpAppConfig extends Config<ApplicationId> {
39 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
40 BgpController bgpController;
41
42 BgpCfg bgpConfig = null;
43
44 public static final String ROUTER_ID = "routerId";
45 public static final String LOCAL_AS = "localAs";
46 public static final String MAX_SESSION = "maxSession";
47 public static final String LS_CAPABILITY = "lsCapability";
48 public static final String HOLD_TIME = "holdTime";
49 public static final String LARGE_AS_CAPABILITY = "largeAsCapability";
50
51 public static final String BGP_PEER = "bgpPeer";
52 public static final String PEER_IP = "peerIp";
53 public static final String REMOTE_AS = "remoteAs";
54 public static final String PEER_HOLD_TIME = "peerHoldTime";
Thejaswi N K0008f1d2015-12-11 12:47:41 +053055 public static final String PEER_CONNECT_MODE = "connectMode";
56 public static final String PEER_CONNECT_PASSIVE = "passive";
57 public static final String PEER_CONNECT_ACTIVE = "active";
Thejaswi N K38879622015-12-08 22:14:47 +053058
59 static final int MAX_SHORT_AS_NUMBER = 65535;
60 static final long MAX_LONG_AS_NUMBER = 4294967295L;
61
62 @Override
63 public boolean isValid() {
64 boolean fields = false;
65
66 this.bgpController = DefaultServiceDirectory.getService(BgpController.class);
67 bgpConfig = bgpController.getConfig();
68
69 fields = hasOnlyFields(ROUTER_ID, LOCAL_AS, MAX_SESSION, LS_CAPABILITY,
70 HOLD_TIME, LARGE_AS_CAPABILITY, BGP_PEER) &&
71 isIpAddress(ROUTER_ID, MANDATORY) && isNumber(LOCAL_AS, MANDATORY) &&
72 isNumber(MAX_SESSION, OPTIONAL, 20) && isNumber(HOLD_TIME, OPTIONAL, 180) &&
73 isBoolean(LS_CAPABILITY, OPTIONAL) && isBoolean(LARGE_AS_CAPABILITY, OPTIONAL);
74
75 if (!fields) {
76 return fields;
77 }
78
79 return validateBgpConfiguration();
80 }
81
82 /**
83 * Returns routerId from the configuration.
84 *
85 * @return routerId
86 */
87 public String routerId() {
88 return get(ROUTER_ID, null);
89 }
90
91 /**
92 * Returns localAs number from the configuration.
93 *
94 * @return local As number
95 */
96 public int localAs() {
97 return Integer.parseInt(get(LOCAL_AS, null));
98 }
99
100 /**
101 * Returns max session from the configuration.
102 *
103 * @return max session
104 */
105 public int maxSession() {
106 return Integer.parseInt(get(MAX_SESSION, null));
107 }
108
109 /**
110 * Returns BGP-LS capability support from the configuration.
111 *
112 * @return true if BGP-LS capability is set else false
113 */
114 public boolean lsCapability() {
115 return Boolean.parseBoolean(get(LS_CAPABILITY, null));
116 }
117
118 /**
119 * Returns largeAs capability support from the configuration.
120 *
121 * @return largeAs capability
122 */
123 public boolean largeAsCapability() {
124 return Boolean.parseBoolean(get(LARGE_AS_CAPABILITY, null));
125 }
126
127 /**
128 * Returns holdTime of the local node from the configuration.
129 *
130 * @return holdTime
131 */
132 public short holdTime() {
133 return Short.parseShort(get(HOLD_TIME, null));
134 }
135
136 /**
137 * Validates the Bgp local and peer configuration.
138 *
139 * @return true if valid else false
140 */
141 public boolean validateBgpConfiguration() {
142
143 if (!validateLocalAs()) {
144 return false;
145 }
146
147 if (!validateRouterId()) {
148 return false;
149 }
150
151 if (!validateBgpPeers()) {
152 return false;
153 }
154
155 return true;
156 }
157
158 /**
159 * Validates the Bgp As number.
160 *
161 * @return true if valid else false
162 */
163 public boolean validateLocalAs() {
164
165 long localAs = 0;
166 localAs = localAs();
167
168 if (bgpController.connectedPeerCount() != 0) {
169 return false;
170 }
171
172 if (largeAsCapability()) {
173
174 if (localAs == 0 || localAs >= MAX_LONG_AS_NUMBER) {
175 return false;
176 }
177 } else {
178 if (localAs == 0 || localAs >= MAX_SHORT_AS_NUMBER) {
179 return false;
180 }
181 }
182
183 return true;
184 }
185
186 /**
187 * Validates the Bgp peer As number.
188 *
Jian Lidfba7392016-01-22 16:46:58 -0800189 * @param remoteAs remote As number
Thejaswi N K38879622015-12-08 22:14:47 +0530190 * @return true if valid else false
191 */
192 public boolean validateRemoteAs(long remoteAs) {
193 if (largeAsCapability()) {
194
195 if (remoteAs == 0 || remoteAs >= MAX_LONG_AS_NUMBER) {
196 return false;
197 }
198 } else {
199 if (remoteAs == 0 || remoteAs >= MAX_SHORT_AS_NUMBER) {
200 return false;
201 }
202 }
203 return true;
204 }
205
206 /**
207 * Validates the Bgp Router ID configuration.
208 *
209 * @return true if valid else false
210 */
211 public boolean validateRouterId() {
212 String routerId = routerId();
213 if (bgpController.connectedPeerCount() != 0) {
214 return false;
215 }
216 return true;
217 }
218
219 /**
220 * Validates the Bgp peer holdTime.
221 *
Jian Lidfba7392016-01-22 16:46:58 -0800222 * @param remoteAs remote As number
Thejaswi N K38879622015-12-08 22:14:47 +0530223 * @return true if valid else false
224 */
225 public boolean validatePeerHoldTime(long remoteAs) {
226 //TODO:Validate it later..
227 return true;
228 }
229
230 /**
231 * Validates the Bgp peer configuration.
232 *
233 * @return true if valid else false
234 */
235 public boolean validateBgpPeers() {
236 List<BgpPeerConfig> nodes;
Thejaswi N K0008f1d2015-12-11 12:47:41 +0530237 String connectMode;
Thejaswi N K38879622015-12-08 22:14:47 +0530238
239 nodes = bgpPeer();
240 for (int i = 0; i < nodes.size(); i++) {
Thejaswi N K0008f1d2015-12-11 12:47:41 +0530241 connectMode = nodes.get(i).connectMode();
Thejaswi N K38879622015-12-08 22:14:47 +0530242 if ((IpAddress.valueOf(nodes.get(i).hostname()) == null) ||
243 !validateRemoteAs(nodes.get(i).asNumber()) ||
Thejaswi N K0008f1d2015-12-11 12:47:41 +0530244 !validatePeerHoldTime(nodes.get(i).holdTime()) ||
245 !(connectMode.equals(PEER_CONNECT_ACTIVE) || connectMode.equals(PEER_CONNECT_PASSIVE))) {
Thejaswi N K38879622015-12-08 22:14:47 +0530246 return false;
247 }
248 }
249
250 return true;
251 }
252
253 /**
254 * Returns the set of nodes read from network config.
255 *
256 * @return list of BgpPeerConfig or null
257 */
258 public List<BgpPeerConfig> bgpPeer() {
259 List<BgpPeerConfig> nodes = new ArrayList<BgpPeerConfig>();
260
261 JsonNode jsonNodes = object.get(BGP_PEER);
262 if (jsonNodes == null) {
263 return null;
264 }
265
266 jsonNodes.forEach(jsonNode -> nodes.add(new BgpPeerConfig(
267 jsonNode.path(PEER_IP).asText(),
268 jsonNode.path(REMOTE_AS).asInt(),
Thejaswi N K0008f1d2015-12-11 12:47:41 +0530269 jsonNode.path(PEER_HOLD_TIME).asInt(),
270 jsonNode.path(PEER_CONNECT_MODE).asText())));
Thejaswi N K38879622015-12-08 22:14:47 +0530271
272 return nodes;
273 }
274
275 /**
276 * Configuration for Bgp peer nodes.
277 */
278 public static class BgpPeerConfig {
279
280 private final String hostname;
281 private final int asNumber;
282 private final short holdTime;
Thejaswi N K0008f1d2015-12-11 12:47:41 +0530283 private final String connectMode;
Thejaswi N K38879622015-12-08 22:14:47 +0530284
Thejaswi N K0008f1d2015-12-11 12:47:41 +0530285 public BgpPeerConfig(String hostname, int asNumber, int holdTime, String connectMode) {
Thejaswi N K38879622015-12-08 22:14:47 +0530286 this.hostname = checkNotNull(hostname);
287 this.asNumber = asNumber;
288 this.holdTime = (short) holdTime;
Thejaswi N K0008f1d2015-12-11 12:47:41 +0530289 this.connectMode = connectMode;
Thejaswi N K38879622015-12-08 22:14:47 +0530290 }
291
292 /**
293 * Returns hostname of the peer node.
294 *
295 * @return hostname
296 */
297 public String hostname() {
298 return this.hostname;
299 }
300
301 /**
302 * Returns asNumber if peer.
303 *
304 * @return asNumber
305 */
306 public int asNumber() {
307 return this.asNumber;
308 }
309
310 /**
311 * Returns holdTime of the peer node.
312 *
313 * @return holdTime
314 */
315 public short holdTime() {
316 return this.holdTime;
317 }
Thejaswi N K0008f1d2015-12-11 12:47:41 +0530318
319 /**
320 * Returns connection mode for the peer node.
321 *
322 * @return active or passive connection
323 */
324 public String connectMode() {
325 return this.connectMode;
326 }
Thejaswi N K38879622015-12-08 22:14:47 +0530327 }
328}