Thejaswi N K
Committed by Gerrit Code Review

Implementing Bgp provider and REST for configuration

Change-Id: I741497d7d724bd5dc6b7bbe4a2579f11a9e80e0f
...@@ -22,4 +22,5 @@ ...@@ -22,4 +22,5 @@
22 <artifact>mvn:${project.groupId}/onos-bgp-api/${project.version}</artifact> 22 <artifact>mvn:${project.groupId}/onos-bgp-api/${project.version}</artifact>
23 <artifact>mvn:${project.groupId}/onos-bgp-ctl/${project.version}</artifact> 23 <artifact>mvn:${project.groupId}/onos-bgp-ctl/${project.version}</artifact>
24 <artifact>mvn:${project.groupId}/onos-bgp-provider-topology/${project.version}</artifact> 24 <artifact>mvn:${project.groupId}/onos-bgp-provider-topology/${project.version}</artifact>
25 + <artifact>mvn:${project.groupId}/onos-bgp-provider-cfg/${project.version}</artifact>
25 </app> 26 </app>
......
...@@ -22,5 +22,6 @@ ...@@ -22,5 +22,6 @@
22 <bundle>mvn:${project.groupId}/onos-bgp-api/${project.version}</bundle> 22 <bundle>mvn:${project.groupId}/onos-bgp-api/${project.version}</bundle>
23 <bundle>mvn:${project.groupId}/onos-bgp-ctl/${project.version}</bundle> 23 <bundle>mvn:${project.groupId}/onos-bgp-ctl/${project.version}</bundle>
24 <bundle>mvn:${project.groupId}/onos-bgp-provider-topology/${project.version}</bundle> 24 <bundle>mvn:${project.groupId}/onos-bgp-provider-topology/${project.version}</bundle>
25 + <bundle>mvn:${project.groupId}/onos-bgp-provider-cfg/${project.version}</bundle>
25 </feature> 26 </feature>
26 </features> 27 </features>
......
1 +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2 + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3 + <modelVersion>4.0.0</modelVersion>
4 + <parent>
5 + <groupId>org.onosproject</groupId>
6 + <artifactId>onos-bgp-providers</artifactId>
7 + <version>1.4.0-SNAPSHOT</version>
8 + <relativePath>../pom.xml</relativePath>
9 + </parent>
10 + <artifactId>onos-bgp-provider-cfg</artifactId>
11 + <packaging>bundle</packaging>
12 +
13 + <dependencies>
14 + <dependency>
15 + <groupId>org.osgi</groupId>
16 + <artifactId>org.osgi.compendium</artifactId>
17 + </dependency>
18 + <dependency>
19 + <groupId>org.onosproject</groupId>
20 + <artifactId>onos-bgp-api</artifactId>
21 + </dependency>
22 + </dependencies>
23 +</project>
1 +/*
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 + */
16 +package org.onosproject.provider.bgp.cfg.impl;
17 +
18 +import com.fasterxml.jackson.databind.JsonNode;
19 +import org.apache.felix.scr.annotations.Reference;
20 +import org.apache.felix.scr.annotations.ReferenceCardinality;
21 +import org.onlab.osgi.DefaultServiceDirectory;
22 +import org.onlab.packet.IpAddress;
23 +import org.onosproject.bgp.controller.BgpCfg;
24 +import org.onosproject.bgp.controller.BgpController;
25 +import org.onosproject.core.ApplicationId;
26 +import org.onosproject.net.config.Config;
27 +
28 +import java.util.ArrayList;
29 +import java.util.List;
30 +
31 +import static org.onosproject.net.config.Config.FieldPresence.MANDATORY;
32 +import static org.onosproject.net.config.Config.FieldPresence.OPTIONAL;
33 +import static com.google.common.base.Preconditions.checkNotNull;
34 +
35 +/**
36 + * Configuration object for BGP.
37 + */
38 +public 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";
55 +
56 + static final int MAX_SHORT_AS_NUMBER = 65535;
57 + static final long MAX_LONG_AS_NUMBER = 4294967295L;
58 +
59 + @Override
60 + public boolean isValid() {
61 + boolean fields = false;
62 +
63 + this.bgpController = DefaultServiceDirectory.getService(BgpController.class);
64 + bgpConfig = bgpController.getConfig();
65 +
66 + fields = hasOnlyFields(ROUTER_ID, LOCAL_AS, MAX_SESSION, LS_CAPABILITY,
67 + HOLD_TIME, LARGE_AS_CAPABILITY, BGP_PEER) &&
68 + isIpAddress(ROUTER_ID, MANDATORY) && isNumber(LOCAL_AS, MANDATORY) &&
69 + isNumber(MAX_SESSION, OPTIONAL, 20) && isNumber(HOLD_TIME, OPTIONAL, 180) &&
70 + isBoolean(LS_CAPABILITY, OPTIONAL) && isBoolean(LARGE_AS_CAPABILITY, OPTIONAL);
71 +
72 + if (!fields) {
73 + return fields;
74 + }
75 +
76 + return validateBgpConfiguration();
77 + }
78 +
79 + /**
80 + * Returns routerId from the configuration.
81 + *
82 + * @return routerId
83 + */
84 + public String routerId() {
85 + return get(ROUTER_ID, null);
86 + }
87 +
88 + /**
89 + * Returns localAs number from the configuration.
90 + *
91 + * @return local As number
92 + */
93 + public int localAs() {
94 + return Integer.parseInt(get(LOCAL_AS, null));
95 + }
96 +
97 + /**
98 + * Returns max session from the configuration.
99 + *
100 + * @return max session
101 + */
102 + public int maxSession() {
103 + return Integer.parseInt(get(MAX_SESSION, null));
104 + }
105 +
106 + /**
107 + * Returns BGP-LS capability support from the configuration.
108 + *
109 + * @return true if BGP-LS capability is set else false
110 + */
111 + public boolean lsCapability() {
112 + return Boolean.parseBoolean(get(LS_CAPABILITY, null));
113 + }
114 +
115 + /**
116 + * Returns largeAs capability support from the configuration.
117 + *
118 + * @return largeAs capability
119 + */
120 + public boolean largeAsCapability() {
121 + return Boolean.parseBoolean(get(LARGE_AS_CAPABILITY, null));
122 + }
123 +
124 + /**
125 + * Returns holdTime of the local node from the configuration.
126 + *
127 + * @return holdTime
128 + */
129 + public short holdTime() {
130 + return Short.parseShort(get(HOLD_TIME, null));
131 + }
132 +
133 + /**
134 + * Validates the Bgp local and peer configuration.
135 + *
136 + * @return true if valid else false
137 + */
138 + public boolean validateBgpConfiguration() {
139 +
140 + if (!validateLocalAs()) {
141 + return false;
142 + }
143 +
144 + if (!validateRouterId()) {
145 + return false;
146 + }
147 +
148 + if (!validateBgpPeers()) {
149 + return false;
150 + }
151 +
152 + return true;
153 + }
154 +
155 + /**
156 + * Validates the Bgp As number.
157 + *
158 + * @return true if valid else false
159 + */
160 + public boolean validateLocalAs() {
161 +
162 + long localAs = 0;
163 + localAs = localAs();
164 +
165 + if (bgpController.connectedPeerCount() != 0) {
166 + return false;
167 + }
168 +
169 + if (largeAsCapability()) {
170 +
171 + if (localAs == 0 || localAs >= MAX_LONG_AS_NUMBER) {
172 + return false;
173 + }
174 + } else {
175 + if (localAs == 0 || localAs >= MAX_SHORT_AS_NUMBER) {
176 + return false;
177 + }
178 + }
179 +
180 + return true;
181 + }
182 +
183 + /**
184 + * Validates the Bgp peer As number.
185 + *
186 + * @return true if valid else false
187 + */
188 + public boolean validateRemoteAs(long remoteAs) {
189 + if (largeAsCapability()) {
190 +
191 + if (remoteAs == 0 || remoteAs >= MAX_LONG_AS_NUMBER) {
192 + return false;
193 + }
194 + } else {
195 + if (remoteAs == 0 || remoteAs >= MAX_SHORT_AS_NUMBER) {
196 + return false;
197 + }
198 + }
199 + return true;
200 + }
201 +
202 + /**
203 + * Validates the Bgp Router ID configuration.
204 + *
205 + * @return true if valid else false
206 + */
207 + public boolean validateRouterId() {
208 + String routerId = routerId();
209 + if (bgpController.connectedPeerCount() != 0) {
210 + return false;
211 + }
212 + return true;
213 + }
214 +
215 + /**
216 + * Validates the Bgp peer holdTime.
217 + *
218 + * @return true if valid else false
219 + */
220 + public boolean validatePeerHoldTime(long remoteAs) {
221 + //TODO:Validate it later..
222 + return true;
223 + }
224 +
225 + /**
226 + * Validates the Bgp peer configuration.
227 + *
228 + * @return true if valid else false
229 + */
230 + public boolean validateBgpPeers() {
231 + List<BgpPeerConfig> nodes;
232 +
233 + nodes = bgpPeer();
234 + for (int i = 0; i < nodes.size(); i++) {
235 + if ((IpAddress.valueOf(nodes.get(i).hostname()) == null) ||
236 + !validateRemoteAs(nodes.get(i).asNumber()) ||
237 + !validatePeerHoldTime(nodes.get(i).holdTime())) {
238 + return false;
239 + }
240 + }
241 +
242 + return true;
243 + }
244 +
245 + /**
246 + * Returns the set of nodes read from network config.
247 + *
248 + * @return list of BgpPeerConfig or null
249 + */
250 + public List<BgpPeerConfig> bgpPeer() {
251 + List<BgpPeerConfig> nodes = new ArrayList<BgpPeerConfig>();
252 +
253 + JsonNode jsonNodes = object.get(BGP_PEER);
254 + if (jsonNodes == null) {
255 + return null;
256 + }
257 +
258 + jsonNodes.forEach(jsonNode -> nodes.add(new BgpPeerConfig(
259 + jsonNode.path(PEER_IP).asText(),
260 + jsonNode.path(REMOTE_AS).asInt(),
261 + jsonNode.path(PEER_HOLD_TIME).asInt())));
262 +
263 + return nodes;
264 + }
265 +
266 + /**
267 + * Configuration for Bgp peer nodes.
268 + */
269 + public static class BgpPeerConfig {
270 +
271 + private final String hostname;
272 + private final int asNumber;
273 + private final short holdTime;
274 +
275 + public BgpPeerConfig(String hostname, int asNumber, int holdTime) {
276 + this.hostname = checkNotNull(hostname);
277 + this.asNumber = asNumber;
278 + this.holdTime = (short) holdTime;
279 + }
280 +
281 + /**
282 + * Returns hostname of the peer node.
283 + *
284 + * @return hostname
285 + */
286 + public String hostname() {
287 + return this.hostname;
288 + }
289 +
290 + /**
291 + * Returns asNumber if peer.
292 + *
293 + * @return asNumber
294 + */
295 + public int asNumber() {
296 + return this.asNumber;
297 + }
298 +
299 + /**
300 + * Returns holdTime of the peer node.
301 + *
302 + * @return holdTime
303 + */
304 + public short holdTime() {
305 + return this.holdTime;
306 + }
307 + }
308 +}
1 +/*
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 + */
16 +package org.onosproject.provider.bgp.cfg.impl;
17 +
18 +import org.apache.felix.scr.annotations.Activate;
19 +import org.apache.felix.scr.annotations.Component;
20 +import org.apache.felix.scr.annotations.Deactivate;
21 +import org.apache.felix.scr.annotations.Reference;
22 +import org.apache.felix.scr.annotations.ReferenceCardinality;
23 +import org.apache.felix.scr.annotations.Service;
24 +
25 +import org.onosproject.bgp.controller.BgpCfg;
26 +import org.onosproject.core.ApplicationId;
27 +import org.onosproject.core.CoreService;
28 +import org.onosproject.net.config.ConfigFactory;
29 +import org.onosproject.net.config.NetworkConfigEvent;
30 +import org.onosproject.net.config.NetworkConfigListener;
31 +import org.onosproject.net.config.NetworkConfigRegistry;
32 +import org.onosproject.net.config.NetworkConfigService;
33 +import org.onosproject.net.config.basics.SubjectFactories;
34 +import org.onosproject.net.provider.AbstractProvider;
35 +import org.onosproject.net.provider.ProviderId;
36 +import org.onosproject.bgp.controller.BgpController;
37 +import org.slf4j.Logger;
38 +import org.osgi.service.component.ComponentContext;
39 +
40 +import java.util.List;
41 +
42 +import static org.slf4j.LoggerFactory.getLogger;
43 +
44 +/**
45 + * BGP config provider to validate and populate the configuration.
46 + */
47 +@Component(immediate = true)
48 +@Service
49 +public class BgpCfgProvider extends AbstractProvider {
50 +
51 + private static final Logger log = getLogger(BgpCfgProvider.class);
52 +
53 + static final String PROVIDER_ID = "org.onosproject.provider.bgp.cfg";
54 +
55 + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
56 + protected BgpController bgpController;
57 +
58 + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
59 + protected CoreService coreService;
60 +
61 + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
62 + protected NetworkConfigRegistry configRegistry;
63 +
64 + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
65 + protected NetworkConfigService configService;
66 +
67 + private final ConfigFactory configFactory =
68 + new ConfigFactory(SubjectFactories.APP_SUBJECT_FACTORY, BgpAppConfig.class, "bgpapp") {
69 + @Override
70 + public BgpAppConfig createConfig() {
71 + return new BgpAppConfig();
72 + }
73 + };
74 +
75 + private final NetworkConfigListener configListener = new InternalConfigListener();
76 +
77 + private ApplicationId appId;
78 +
79 + /**
80 + * Creates a Bgp config provider.
81 + */
82 + public BgpCfgProvider() {
83 + super(new ProviderId("bgp", PROVIDER_ID));
84 + }
85 +
86 + @Activate
87 + public void activate(ComponentContext context) {
88 + appId = coreService.registerApplication(PROVIDER_ID);
89 + configService.addListener(configListener);
90 + configRegistry.registerConfigFactory(configFactory);
91 + log.info("BGP cfg provider started");
92 + }
93 +
94 + @Deactivate
95 + public void deactivate(ComponentContext context) {
96 + configRegistry.unregisterConfigFactory(configFactory);
97 + configService.removeListener(configListener);
98 + }
99 +
100 + void setBgpController(BgpController bgpController) {
101 + this.bgpController = bgpController;
102 + }
103 +
104 + /**
105 + * Reads the configuration and set it to the BGP-LS south bound protocol.
106 + *
107 + * @return void
108 + */
109 + private void readConfiguration() {
110 + BgpCfg bgpConfig = null;
111 + List<BgpAppConfig.BgpPeerConfig> nodes;
112 + bgpConfig = bgpController.getConfig();
113 + BgpAppConfig config = configRegistry.getConfig(appId, BgpAppConfig.class);
114 +
115 + if (config == null) {
116 + log.warn("No configuration found");
117 + return;
118 + }
119 +
120 + /*Set the configuration */
121 + bgpConfig.setRouterId(config.routerId());
122 + bgpConfig.setAsNumber(config.localAs());
123 + bgpConfig.setLsCapability(config.lsCapability());
124 + bgpConfig.setHoldTime(config.holdTime());
125 + bgpConfig.setMaxSession(config.maxSession());
126 + bgpConfig.setLargeASCapability(config.largeAsCapability());
127 +
128 + nodes = config.bgpPeer();
129 + for (int i = 0; i < nodes.size(); i++) {
130 + bgpConfig.addPeer(nodes.get(i).hostname(), nodes.get(i).asNumber(), nodes.get(i).holdTime());
131 + }
132 + }
133 +
134 + /**
135 + * BGP config listener to populate the configuration.
136 + */
137 + private class InternalConfigListener implements NetworkConfigListener {
138 +
139 + @Override
140 + public void event(NetworkConfigEvent event) {
141 + if (!event.configClass().equals(BgpAppConfig.class)) {
142 + return;
143 + }
144 +
145 + switch (event.type()) {
146 + case CONFIG_ADDED:
147 + readConfiguration();
148 + break;
149 + case CONFIG_UPDATED:
150 + readConfiguration();
151 + break;
152 + case CONFIG_REMOVED:
153 + default:
154 + break;
155 + }
156 + }
157 + }
158 +}
1 +/*
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 + */
16 +
17 +/**
18 + *Bgp configuration provider.
19 + */
20 +package org.onosproject.provider.bgp.cfg.impl;
...@@ -26,6 +26,7 @@ ...@@ -26,6 +26,7 @@
26 <description>BGP-LS protocol providers root</description> 26 <description>BGP-LS protocol providers root</description>
27 <modules> 27 <modules>
28 <module>topology</module> 28 <module>topology</module>
29 + <module>cfg</module>
29 <module>app</module> 30 <module>app</module>
30 </modules> 31 </modules>
31 <dependencies> 32 <dependencies>
......