blob: 4bd6e701760a0b943cafbbf3d3cc9495e959a5a9 [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 *
189 * @return true if valid else false
190 */
191 public boolean validateRemoteAs(long remoteAs) {
192 if (largeAsCapability()) {
193
194 if (remoteAs == 0 || remoteAs >= MAX_LONG_AS_NUMBER) {
195 return false;
196 }
197 } else {
198 if (remoteAs == 0 || remoteAs >= MAX_SHORT_AS_NUMBER) {
199 return false;
200 }
201 }
202 return true;
203 }
204
205 /**
206 * Validates the Bgp Router ID configuration.
207 *
208 * @return true if valid else false
209 */
210 public boolean validateRouterId() {
211 String routerId = routerId();
212 if (bgpController.connectedPeerCount() != 0) {
213 return false;
214 }
215 return true;
216 }
217
218 /**
219 * Validates the Bgp peer holdTime.
220 *
221 * @return true if valid else false
222 */
223 public boolean validatePeerHoldTime(long remoteAs) {
224 //TODO:Validate it later..
225 return true;
226 }
227
228 /**
229 * Validates the Bgp peer configuration.
230 *
231 * @return true if valid else false
232 */
233 public boolean validateBgpPeers() {
234 List<BgpPeerConfig> nodes;
Thejaswi N K0008f1d2015-12-11 12:47:41 +0530235 String connectMode;
Thejaswi N K38879622015-12-08 22:14:47 +0530236
237 nodes = bgpPeer();
238 for (int i = 0; i < nodes.size(); i++) {
Thejaswi N K0008f1d2015-12-11 12:47:41 +0530239 connectMode = nodes.get(i).connectMode();
Thejaswi N K38879622015-12-08 22:14:47 +0530240 if ((IpAddress.valueOf(nodes.get(i).hostname()) == null) ||
241 !validateRemoteAs(nodes.get(i).asNumber()) ||
Thejaswi N K0008f1d2015-12-11 12:47:41 +0530242 !validatePeerHoldTime(nodes.get(i).holdTime()) ||
243 !(connectMode.equals(PEER_CONNECT_ACTIVE) || connectMode.equals(PEER_CONNECT_PASSIVE))) {
Thejaswi N K38879622015-12-08 22:14:47 +0530244 return false;
245 }
246 }
247
248 return true;
249 }
250
251 /**
252 * Returns the set of nodes read from network config.
253 *
254 * @return list of BgpPeerConfig or null
255 */
256 public List<BgpPeerConfig> bgpPeer() {
257 List<BgpPeerConfig> nodes = new ArrayList<BgpPeerConfig>();
258
259 JsonNode jsonNodes = object.get(BGP_PEER);
260 if (jsonNodes == null) {
261 return null;
262 }
263
264 jsonNodes.forEach(jsonNode -> nodes.add(new BgpPeerConfig(
265 jsonNode.path(PEER_IP).asText(),
266 jsonNode.path(REMOTE_AS).asInt(),
Thejaswi N K0008f1d2015-12-11 12:47:41 +0530267 jsonNode.path(PEER_HOLD_TIME).asInt(),
268 jsonNode.path(PEER_CONNECT_MODE).asText())));
Thejaswi N K38879622015-12-08 22:14:47 +0530269
270 return nodes;
271 }
272
273 /**
274 * Configuration for Bgp peer nodes.
275 */
276 public static class BgpPeerConfig {
277
278 private final String hostname;
279 private final int asNumber;
280 private final short holdTime;
Thejaswi N K0008f1d2015-12-11 12:47:41 +0530281 private final String connectMode;
Thejaswi N K38879622015-12-08 22:14:47 +0530282
Thejaswi N K0008f1d2015-12-11 12:47:41 +0530283 public BgpPeerConfig(String hostname, int asNumber, int holdTime, String connectMode) {
Thejaswi N K38879622015-12-08 22:14:47 +0530284 this.hostname = checkNotNull(hostname);
285 this.asNumber = asNumber;
286 this.holdTime = (short) holdTime;
Thejaswi N K0008f1d2015-12-11 12:47:41 +0530287 this.connectMode = connectMode;
Thejaswi N K38879622015-12-08 22:14:47 +0530288 }
289
290 /**
291 * Returns hostname of the peer node.
292 *
293 * @return hostname
294 */
295 public String hostname() {
296 return this.hostname;
297 }
298
299 /**
300 * Returns asNumber if peer.
301 *
302 * @return asNumber
303 */
304 public int asNumber() {
305 return this.asNumber;
306 }
307
308 /**
309 * Returns holdTime of the peer node.
310 *
311 * @return holdTime
312 */
313 public short holdTime() {
314 return this.holdTime;
315 }
Thejaswi N K0008f1d2015-12-11 12:47:41 +0530316
317 /**
318 * Returns connection mode for the peer node.
319 *
320 * @return active or passive connection
321 */
322 public String connectMode() {
323 return this.connectMode;
324 }
Thejaswi N K38879622015-12-08 22:14:47 +0530325 }
326}