Thejaswi N K
Committed by Gerrit Code Review

Implementing Bgp provider and REST for configuration

Change-Id: I741497d7d724bd5dc6b7bbe4a2579f11a9e80e0f
......@@ -22,4 +22,5 @@
<artifact>mvn:${project.groupId}/onos-bgp-api/${project.version}</artifact>
<artifact>mvn:${project.groupId}/onos-bgp-ctl/${project.version}</artifact>
<artifact>mvn:${project.groupId}/onos-bgp-provider-topology/${project.version}</artifact>
<artifact>mvn:${project.groupId}/onos-bgp-provider-cfg/${project.version}</artifact>
</app>
......
......@@ -22,5 +22,6 @@
<bundle>mvn:${project.groupId}/onos-bgp-api/${project.version}</bundle>
<bundle>mvn:${project.groupId}/onos-bgp-ctl/${project.version}</bundle>
<bundle>mvn:${project.groupId}/onos-bgp-provider-topology/${project.version}</bundle>
<bundle>mvn:${project.groupId}/onos-bgp-provider-cfg/${project.version}</bundle>
</feature>
</features>
......
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.onosproject</groupId>
<artifactId>onos-bgp-providers</artifactId>
<version>1.4.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>onos-bgp-provider-cfg</artifactId>
<packaging>bundle</packaging>
<dependencies>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.compendium</artifactId>
</dependency>
<dependency>
<groupId>org.onosproject</groupId>
<artifactId>onos-bgp-api</artifactId>
</dependency>
</dependencies>
</project>
/*
* Copyright 2015 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.provider.bgp.cfg.impl;
import com.fasterxml.jackson.databind.JsonNode;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.onlab.osgi.DefaultServiceDirectory;
import org.onlab.packet.IpAddress;
import org.onosproject.bgp.controller.BgpCfg;
import org.onosproject.bgp.controller.BgpController;
import org.onosproject.core.ApplicationId;
import org.onosproject.net.config.Config;
import java.util.ArrayList;
import java.util.List;
import static org.onosproject.net.config.Config.FieldPresence.MANDATORY;
import static org.onosproject.net.config.Config.FieldPresence.OPTIONAL;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* Configuration object for BGP.
*/
public class BgpAppConfig extends Config<ApplicationId> {
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
BgpController bgpController;
BgpCfg bgpConfig = null;
public static final String ROUTER_ID = "routerId";
public static final String LOCAL_AS = "localAs";
public static final String MAX_SESSION = "maxSession";
public static final String LS_CAPABILITY = "lsCapability";
public static final String HOLD_TIME = "holdTime";
public static final String LARGE_AS_CAPABILITY = "largeAsCapability";
public static final String BGP_PEER = "bgpPeer";
public static final String PEER_IP = "peerIp";
public static final String REMOTE_AS = "remoteAs";
public static final String PEER_HOLD_TIME = "peerHoldTime";
static final int MAX_SHORT_AS_NUMBER = 65535;
static final long MAX_LONG_AS_NUMBER = 4294967295L;
@Override
public boolean isValid() {
boolean fields = false;
this.bgpController = DefaultServiceDirectory.getService(BgpController.class);
bgpConfig = bgpController.getConfig();
fields = hasOnlyFields(ROUTER_ID, LOCAL_AS, MAX_SESSION, LS_CAPABILITY,
HOLD_TIME, LARGE_AS_CAPABILITY, BGP_PEER) &&
isIpAddress(ROUTER_ID, MANDATORY) && isNumber(LOCAL_AS, MANDATORY) &&
isNumber(MAX_SESSION, OPTIONAL, 20) && isNumber(HOLD_TIME, OPTIONAL, 180) &&
isBoolean(LS_CAPABILITY, OPTIONAL) && isBoolean(LARGE_AS_CAPABILITY, OPTIONAL);
if (!fields) {
return fields;
}
return validateBgpConfiguration();
}
/**
* Returns routerId from the configuration.
*
* @return routerId
*/
public String routerId() {
return get(ROUTER_ID, null);
}
/**
* Returns localAs number from the configuration.
*
* @return local As number
*/
public int localAs() {
return Integer.parseInt(get(LOCAL_AS, null));
}
/**
* Returns max session from the configuration.
*
* @return max session
*/
public int maxSession() {
return Integer.parseInt(get(MAX_SESSION, null));
}
/**
* Returns BGP-LS capability support from the configuration.
*
* @return true if BGP-LS capability is set else false
*/
public boolean lsCapability() {
return Boolean.parseBoolean(get(LS_CAPABILITY, null));
}
/**
* Returns largeAs capability support from the configuration.
*
* @return largeAs capability
*/
public boolean largeAsCapability() {
return Boolean.parseBoolean(get(LARGE_AS_CAPABILITY, null));
}
/**
* Returns holdTime of the local node from the configuration.
*
* @return holdTime
*/
public short holdTime() {
return Short.parseShort(get(HOLD_TIME, null));
}
/**
* Validates the Bgp local and peer configuration.
*
* @return true if valid else false
*/
public boolean validateBgpConfiguration() {
if (!validateLocalAs()) {
return false;
}
if (!validateRouterId()) {
return false;
}
if (!validateBgpPeers()) {
return false;
}
return true;
}
/**
* Validates the Bgp As number.
*
* @return true if valid else false
*/
public boolean validateLocalAs() {
long localAs = 0;
localAs = localAs();
if (bgpController.connectedPeerCount() != 0) {
return false;
}
if (largeAsCapability()) {
if (localAs == 0 || localAs >= MAX_LONG_AS_NUMBER) {
return false;
}
} else {
if (localAs == 0 || localAs >= MAX_SHORT_AS_NUMBER) {
return false;
}
}
return true;
}
/**
* Validates the Bgp peer As number.
*
* @return true if valid else false
*/
public boolean validateRemoteAs(long remoteAs) {
if (largeAsCapability()) {
if (remoteAs == 0 || remoteAs >= MAX_LONG_AS_NUMBER) {
return false;
}
} else {
if (remoteAs == 0 || remoteAs >= MAX_SHORT_AS_NUMBER) {
return false;
}
}
return true;
}
/**
* Validates the Bgp Router ID configuration.
*
* @return true if valid else false
*/
public boolean validateRouterId() {
String routerId = routerId();
if (bgpController.connectedPeerCount() != 0) {
return false;
}
return true;
}
/**
* Validates the Bgp peer holdTime.
*
* @return true if valid else false
*/
public boolean validatePeerHoldTime(long remoteAs) {
//TODO:Validate it later..
return true;
}
/**
* Validates the Bgp peer configuration.
*
* @return true if valid else false
*/
public boolean validateBgpPeers() {
List<BgpPeerConfig> nodes;
nodes = bgpPeer();
for (int i = 0; i < nodes.size(); i++) {
if ((IpAddress.valueOf(nodes.get(i).hostname()) == null) ||
!validateRemoteAs(nodes.get(i).asNumber()) ||
!validatePeerHoldTime(nodes.get(i).holdTime())) {
return false;
}
}
return true;
}
/**
* Returns the set of nodes read from network config.
*
* @return list of BgpPeerConfig or null
*/
public List<BgpPeerConfig> bgpPeer() {
List<BgpPeerConfig> nodes = new ArrayList<BgpPeerConfig>();
JsonNode jsonNodes = object.get(BGP_PEER);
if (jsonNodes == null) {
return null;
}
jsonNodes.forEach(jsonNode -> nodes.add(new BgpPeerConfig(
jsonNode.path(PEER_IP).asText(),
jsonNode.path(REMOTE_AS).asInt(),
jsonNode.path(PEER_HOLD_TIME).asInt())));
return nodes;
}
/**
* Configuration for Bgp peer nodes.
*/
public static class BgpPeerConfig {
private final String hostname;
private final int asNumber;
private final short holdTime;
public BgpPeerConfig(String hostname, int asNumber, int holdTime) {
this.hostname = checkNotNull(hostname);
this.asNumber = asNumber;
this.holdTime = (short) holdTime;
}
/**
* Returns hostname of the peer node.
*
* @return hostname
*/
public String hostname() {
return this.hostname;
}
/**
* Returns asNumber if peer.
*
* @return asNumber
*/
public int asNumber() {
return this.asNumber;
}
/**
* Returns holdTime of the peer node.
*
* @return holdTime
*/
public short holdTime() {
return this.holdTime;
}
}
}
/*
* Copyright 2015 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.provider.bgp.cfg.impl;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.apache.felix.scr.annotations.Service;
import org.onosproject.bgp.controller.BgpCfg;
import org.onosproject.core.ApplicationId;
import org.onosproject.core.CoreService;
import org.onosproject.net.config.ConfigFactory;
import org.onosproject.net.config.NetworkConfigEvent;
import org.onosproject.net.config.NetworkConfigListener;
import org.onosproject.net.config.NetworkConfigRegistry;
import org.onosproject.net.config.NetworkConfigService;
import org.onosproject.net.config.basics.SubjectFactories;
import org.onosproject.net.provider.AbstractProvider;
import org.onosproject.net.provider.ProviderId;
import org.onosproject.bgp.controller.BgpController;
import org.slf4j.Logger;
import org.osgi.service.component.ComponentContext;
import java.util.List;
import static org.slf4j.LoggerFactory.getLogger;
/**
* BGP config provider to validate and populate the configuration.
*/
@Component(immediate = true)
@Service
public class BgpCfgProvider extends AbstractProvider {
private static final Logger log = getLogger(BgpCfgProvider.class);
static final String PROVIDER_ID = "org.onosproject.provider.bgp.cfg";
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected BgpController bgpController;
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected CoreService coreService;
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected NetworkConfigRegistry configRegistry;
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected NetworkConfigService configService;
private final ConfigFactory configFactory =
new ConfigFactory(SubjectFactories.APP_SUBJECT_FACTORY, BgpAppConfig.class, "bgpapp") {
@Override
public BgpAppConfig createConfig() {
return new BgpAppConfig();
}
};
private final NetworkConfigListener configListener = new InternalConfigListener();
private ApplicationId appId;
/**
* Creates a Bgp config provider.
*/
public BgpCfgProvider() {
super(new ProviderId("bgp", PROVIDER_ID));
}
@Activate
public void activate(ComponentContext context) {
appId = coreService.registerApplication(PROVIDER_ID);
configService.addListener(configListener);
configRegistry.registerConfigFactory(configFactory);
log.info("BGP cfg provider started");
}
@Deactivate
public void deactivate(ComponentContext context) {
configRegistry.unregisterConfigFactory(configFactory);
configService.removeListener(configListener);
}
void setBgpController(BgpController bgpController) {
this.bgpController = bgpController;
}
/**
* Reads the configuration and set it to the BGP-LS south bound protocol.
*
* @return void
*/
private void readConfiguration() {
BgpCfg bgpConfig = null;
List<BgpAppConfig.BgpPeerConfig> nodes;
bgpConfig = bgpController.getConfig();
BgpAppConfig config = configRegistry.getConfig(appId, BgpAppConfig.class);
if (config == null) {
log.warn("No configuration found");
return;
}
/*Set the configuration */
bgpConfig.setRouterId(config.routerId());
bgpConfig.setAsNumber(config.localAs());
bgpConfig.setLsCapability(config.lsCapability());
bgpConfig.setHoldTime(config.holdTime());
bgpConfig.setMaxSession(config.maxSession());
bgpConfig.setLargeASCapability(config.largeAsCapability());
nodes = config.bgpPeer();
for (int i = 0; i < nodes.size(); i++) {
bgpConfig.addPeer(nodes.get(i).hostname(), nodes.get(i).asNumber(), nodes.get(i).holdTime());
}
}
/**
* BGP config listener to populate the configuration.
*/
private class InternalConfigListener implements NetworkConfigListener {
@Override
public void event(NetworkConfigEvent event) {
if (!event.configClass().equals(BgpAppConfig.class)) {
return;
}
switch (event.type()) {
case CONFIG_ADDED:
readConfiguration();
break;
case CONFIG_UPDATED:
readConfiguration();
break;
case CONFIG_REMOVED:
default:
break;
}
}
}
}
/*
* Copyright 2015 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.
*/
/**
*Bgp configuration provider.
*/
package org.onosproject.provider.bgp.cfg.impl;
......@@ -26,6 +26,7 @@
<description>BGP-LS protocol providers root</description>
<modules>
<module>topology</module>
<module>cfg</module>
<module>app</module>
</modules>
<dependencies>
......