diff options
Diffstat (limited to 'framework/src/onos/bgp')
57 files changed, 5955 insertions, 0 deletions
diff --git a/framework/src/onos/bgp/api/pom.xml b/framework/src/onos/bgp/api/pom.xml new file mode 100755 index 00000000..6fa1cc7b --- /dev/null +++ b/framework/src/onos/bgp/api/pom.xml @@ -0,0 +1,95 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + ~ 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. + --> +<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/maven-v4_0_0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.onosproject</groupId> + <artifactId>onos-bgp</artifactId> + <version>1.4.0-SNAPSHOT</version> + <relativePath>../pom.xml</relativePath> + </parent> + + <artifactId>onos-bgp-api</artifactId> + <packaging>bundle</packaging> + + <description>ONOS BGP controller subsystem API</description> + + <dependencies> + <dependency> + <groupId>org.onosproject</groupId> + <artifactId>onos-bgpio</artifactId> + </dependency> + <dependency> + <groupId>io.netty</groupId> + <artifactId>netty</artifactId> + </dependency> + <dependency> + <groupId>org.onosproject</groupId> + <artifactId>onos-api</artifactId> + </dependency> + <dependency> + <groupId>org.onosproject</groupId> + <artifactId>onlab-misc</artifactId> + </dependency> + + </dependencies> + + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-shade-plugin</artifactId> + <version>2.3</version> + <configuration> + <artifactSet> + <excludes> + <exclude>io.netty:netty</exclude> + <exclude>com.google.guava:guava</exclude> + <exclude>org.slf4j:slfj-api</exclude> + <exclude>ch.qos.logback:logback-core</exclude> + <exclude>ch.qos.logback:logback-classic</exclude> + <exclude>com.google.code.findbugs:annotations</exclude> + </excludes> + </artifactSet> + </configuration> + <executions> + <execution> + <phase>package</phase> + <goals> + <goal>shade</goal> + </goals> + </execution> + </executions> + </plugin> + <plugin> + <groupId>org.apache.felix</groupId> + <artifactId>maven-bundle-plugin</artifactId> + <configuration> + <instructions> + <Export-Package> + org.onosproject.bgp.*,org.onosproject.bgpio.*,org.onosproject.bgp.controller + </Export-Package> + </instructions> + </configuration> + </plugin> + </plugins> + </build> + +</project> diff --git a/framework/src/onos/bgp/api/src/main/java/org/onosproject/bgp/controller/BGPCfg.java b/framework/src/onos/bgp/api/src/main/java/org/onosproject/bgp/controller/BGPCfg.java new file mode 100755 index 00000000..46165d87 --- /dev/null +++ b/framework/src/onos/bgp/api/src/main/java/org/onosproject/bgp/controller/BGPCfg.java @@ -0,0 +1,297 @@ +/* + * 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.bgp.controller; + +import java.util.TreeMap; + +/** + * Abstraction of an BGP configuration. Manages the BGP configuration from CLI to the BGP controller. + */ +public interface BGPCfg { + + enum State { + /** + * Signifies that its just created. + */ + INIT, + + /** + * Signifies that only IP Address is configured. + */ + IP_CONFIGURED, + + /** + * Signifies that only Autonomous System is configured. + */ + AS_CONFIGURED, + + /** + * Signifies that both IP and Autonomous System is configured. + */ + IP_AS_CONFIGURED + } + + /** + * Returns the status of the configuration based on this state certain operations like connection is handled. + * + * @return State of the configuration + */ + State getState(); + + /** + * To set the current state of the configuration. + * + * @param state Configuration State enum + */ + void setState(State state); + + /** + * Get the status of the link state support for this BGP speaker. + * + * @return true if the link state is supported else false + */ + boolean getLsCapability(); + + /** + * Set the link state support to this BGP speaker. + * + * @param lscapability true value if link state is supported else false + */ + void setLsCapability(boolean lscapability); + + /** + * Get the status of the 32 bit AS support for this BGP speaker. + * + * @return true if the 32 bit AS number is supported else false + */ + boolean getLargeASCapability(); + + /** + * Set the 32 bit AS support capability to this BGP speaker. + * + * @param largeAs true value if the 32 bit AS is supported else false + */ + void setLargeASCapability(boolean largeAs); + + /** + * Set the AS number to which this BGP speaker belongs. + * + * @param localAs 16 or 32 bit AS number, length is dependent on the capability + */ + void setAsNumber(int localAs); + + /** + * Get the AS number to which this BGP speaker belongs. + * + * @return 16 or 32 bit AS number, length is dependent on the capability + */ + int getAsNumber(); + + /** + * Get the connection retry count number. + * + * @return connection retry count if there is a connection error + */ + int getMaxConnRetryCount(); + + /** + * Set the connection retry count. + * + * @param retryCount number of times to try to connect if there is any error + */ + void setMaxConnRetryCout(int retryCount); + + /** + * Get the connection retry time in seconds. + * + * @return connection retry time in seconds + */ + int getMaxConnRetryTime(); + + /** + * Set the connection retry time in seconds. + * + * @param retryTime connection retry times in seconds + */ + void setMaxConnRetryTime(int retryTime); + + /** + * Set the keep alive timer for the connection. + * + * @param holdTime connection hold timer in seconds + */ + void setHoldTime(short holdTime); + + /** + * Returns the connection hold timer in seconds. + * + * @return connection hold timer in seconds + */ + short getHoldTime(); + + /** + * Returns the maximum number of session supported. + * + * @return maximum number of session supported + */ + int getMaxSession(); + + /** + * Set the maximum number of sessions to support. + * + * @param maxsession maximum number of session + */ + void setMaxSession(int maxsession); + + /** + * Returns the Router ID of this BGP speaker. + * + * @return IP address in string format + */ + String getRouterId(); + + /** + * Set the Router ID of this BGP speaker. + * + * @param routerid IP address in string format + */ + void setRouterId(String routerid); + + /** + * Add the BGP peer IP address and the AS number to which it belongs. + * + * @param routerid IP address in string format + * @param remoteAs AS number to which it belongs + * + * @return true if added successfully else false + */ + boolean addPeer(String routerid, int remoteAs); + + /** + * Add the BGP peer IP address and the keep alive time. + * + * @param routerid IP address in string format + * @param holdTime keep alive time for the connection + * + * @return true if added successfully else false + */ + boolean addPeer(String routerid, short holdTime); + + /** + * Add the BGP peer IP address, the AS number to which it belongs and keep alive time. + * + * @param routerid IP address in string format + * @param remoteAs AS number to which it belongs + * @param holdTime keep alive time for the connection + * + * @return true if added successfully else false + */ + boolean addPeer(String routerid, int remoteAs, short holdTime); + + /** + * Remove the BGP peer with this IP address. + * + * @param routerid router IP address + * + * @return true if removed successfully else false + */ + boolean removePeer(String routerid); + + /** + * Connect to BGP peer with this IP address. + * + * @param routerid router IP address + * + * @return true of the configuration is found and able to connect else false + */ + boolean connectPeer(String routerid); + + /** + * Disconnect this BGP peer with this IP address. + * + * @param routerid router IP address in string format + * + * @return true if the configuration is found and able to disconnect else false + */ + boolean disconnectPeer(String routerid); + + /** + * Returns the peer tree information. + * + * @return return the tree map with IP as key and BGPPeerCfg as object + */ + TreeMap<String, BGPPeerCfg> displayPeers(); + + /** + * Return the BGP Peer information with this matching IP. + * + * @param routerid router IP address in string format + * + * @return BGPPeerCfg object + */ + BGPPeerCfg displayPeers(String routerid); + + /** + * Check if this BGP peer is configured. + * + * @param routerid router IP address in string format + * + * @return true if configured exists else false + */ + boolean isPeerConfigured(String routerid); + + /** + * Check if this BGP speaker is having connection with the peer. + * + * @param routerid router IP address in string format + * + * @return true if the connection exists else false + */ + boolean isPeerConnected(String routerid); + + /** + * Return the peer tree map. + * + * @return return the tree map with IP as key and BGPPeerCfg as object + */ + TreeMap<String, BGPPeerCfg> getPeerTree(); + + /** + * Set the current connection state information. + * + * @param routerid router IP address in string format + * @param state state information + */ + void setPeerConnState(String routerid, BGPPeerCfg.State state); + + /** + * Check if the peer can be connected or not. + * + * @param routerid router IP address in string format + * + * @return true if the peer can be connected else false + */ + boolean isPeerConnectable(String routerid); + + /** + * Get the current peer connection state information. + * + * @param routerid router IP address in string format + * + * @return state information + */ + BGPPeerCfg.State getPeerConnState(String routerid); +} diff --git a/framework/src/onos/bgp/api/src/main/java/org/onosproject/bgp/controller/BGPController.java b/framework/src/onos/bgp/api/src/main/java/org/onosproject/bgp/controller/BGPController.java new file mode 100755 index 00000000..6d758122 --- /dev/null +++ b/framework/src/onos/bgp/api/src/main/java/org/onosproject/bgp/controller/BGPController.java @@ -0,0 +1,49 @@ +/* + * 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.bgp.controller; + +import org.onosproject.bgpio.protocol.BGPMessage; + +/** + * Abstraction of an BGP controller. Serves as a one stop shop for obtaining BGP devices and (un)register listeners + * on bgp events + */ +public interface BGPController { + + /** + * Send a message to a particular bgp peer. + * + * @param bgpId the id of the peer to send message. + * @param msg the message to send + */ + void writeMsg(BGPId bgpId, BGPMessage msg); + + /** + * Process a message and notify the appropriate listeners. + * + * @param bgpId id of the peer the message arrived on + * @param msg the message to process. + */ + void processBGPPacket(BGPId bgpId, BGPMessage msg); + + /** + * Get the BGPConfig class to the caller. + * + * @return configuration object + */ + BGPCfg getConfig(); +}
\ No newline at end of file diff --git a/framework/src/onos/bgp/api/src/main/java/org/onosproject/bgp/controller/BGPId.java b/framework/src/onos/bgp/api/src/main/java/org/onosproject/bgp/controller/BGPId.java new file mode 100755 index 00000000..636e72f3 --- /dev/null +++ b/framework/src/onos/bgp/api/src/main/java/org/onosproject/bgp/controller/BGPId.java @@ -0,0 +1,121 @@ +/* + * 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.bgp.controller; + +import org.onlab.packet.IpAddress; +import java.net.URI; +import java.net.URISyntaxException; +import java.util.Objects; + +import static com.google.common.base.Preconditions.checkArgument; + +/** + * The class representing a network peer bgp ip. + * This class is immutable. + */ +public final class BGPId { + + private static final String SCHEME = "bgp"; + private static final long UNKNOWN = 0; + private final IpAddress ipAddress; + + /** + * Private constructor. + */ + private BGPId(IpAddress ipAddress) { + this.ipAddress = ipAddress; + } + + /** + * Create a BGPId from ip address. + * + * @param ipAddress IP address + * @return object of BGPId + */ + public static BGPId bgpId(IpAddress ipAddress) { + return new BGPId(ipAddress); + } + + /** + * Returns the ip address. + * + * @return ipAddress + */ + public IpAddress ipAddress() { + return ipAddress; + } + + /** + * Convert the BGPId value to a ':' separated hexadecimal string. + * + * @return the BGPId value as a ':' separated hexadecimal string. + */ + @Override + public String toString() { + return ipAddress.toString(); + } + + @Override + public boolean equals(Object other) { + if (!(other instanceof BGPId)) { + return false; + } + + BGPId otherBGPid = (BGPId) other; + return Objects.equals(ipAddress, otherBGPid.ipAddress); + } + + @Override + public int hashCode() { + return Objects.hash(ipAddress); + } + + /** + * Returns BGPId created from the given device URI. + * + * @param uri device URI + * @return object of BGPId + */ + public static BGPId bgpId(URI uri) { + checkArgument(uri.getScheme().equals(SCHEME), "Unsupported URI scheme"); + return new BGPId(IpAddress.valueOf(uri.getSchemeSpecificPart())); + } + + /** + * Produces device URI from the given DPID. + * + * @param bgpId device bgpId + * @return device URI + */ + public static URI uri(BGPId bgpId) { + return uri(bgpId.ipAddress()); + } + + /** + * Produces device URI from the given DPID long. + * + * @param ipAddress device ip address + * @return device URI + */ + public static URI uri(IpAddress ipAddress) { + try { + return new URI(SCHEME, ipAddress.toString(), null); + } catch (URISyntaxException e) { + return null; + } + } +}
\ No newline at end of file diff --git a/framework/src/onos/bgp/api/src/main/java/org/onosproject/bgp/controller/BGPPacketStats.java b/framework/src/onos/bgp/api/src/main/java/org/onosproject/bgp/controller/BGPPacketStats.java new file mode 100755 index 00000000..95f83a2d --- /dev/null +++ b/framework/src/onos/bgp/api/src/main/java/org/onosproject/bgp/controller/BGPPacketStats.java @@ -0,0 +1,52 @@ +/* + * 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.bgp.controller; + +/** + * A representation of a packet context which allows any provider to view a packet in event, but may block the response + * to the event if blocked has been called. This packet context can be used to react to the packet in event with a + * packet out. + */ +public interface BGPPacketStats { + /** + * Returns the count for no of packets sent out. + * + * @return int value of no of packets sent + */ + int outPacketCount(); + + /** + * Returns the count for no of packets received. + * + * @return int value of no of packets sent + */ + int inPacketCount(); + + /** + * Returns the count for no of wrong packets received. + * + * @return int value of no of wrong packets received + */ + int wrongPacketCount(); + + /** + * Returns the time. + * + * @return the time + */ + long getTime(); +}
\ No newline at end of file diff --git a/framework/src/onos/bgp/api/src/main/java/org/onosproject/bgp/controller/BGPPeerCfg.java b/framework/src/onos/bgp/api/src/main/java/org/onosproject/bgp/controller/BGPPeerCfg.java new file mode 100755 index 00000000..87ec031f --- /dev/null +++ b/framework/src/onos/bgp/api/src/main/java/org/onosproject/bgp/controller/BGPPeerCfg.java @@ -0,0 +1,166 @@ +/* + * 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.bgp.controller; + +/** + * BGP Peer configuration information. + */ +public interface BGPPeerCfg { + + enum State { + + /** + * Signifies that peer connection is idle. + */ + IDLE, + + /** + * Signifies that connection is initiated. + */ + CONNECT, + + /** + * Signifies that state is active and connection can be established. + */ + ACTIVE, + + /** + * Signifies that open is sent and anticipating reply. + */ + OPENSENT, + + /** + * Signifies that peer sent the open message as reply. + */ + OPENCONFIRM, + + /** + * Signifies that all the negotiation is successful and ready to exchange other messages. + */ + ESTABLISHED, + + /** + * Signifies that invalid state. + */ + INVALID + } + + /** + * Returns the connection State information of the peer. + * + * @return + * enum state is returned + */ + State getState(); + + /** + * Set the connection state information of the peer. + * + * @param state + * enum state + */ + void setState(State state); + + /** + * Returns the connection is initiated from us or not. + * + * @return + * true if the connection is initiated by this peer, false if it has been received. + */ + boolean getSelfInnitConnection(); + + /** + * Set the connection is initiated from us or not. + * + * @param selfInit + * true if the connection is initiated by this peer, false if it has been received. + */ + void setSelfInnitConnection(boolean selfInit); + + /** + * Returns the AS number to which this peer belongs. + * + * @return + * AS number + */ + int getAsNumber(); + + /** + * Set the AS number to which this peer belongs. + * + * @param asNumber + * AS number + */ + void setAsNumber(int asNumber); + + /** + * Get the keep alive timer value configured. + * + * @return + * keep alive timer value in seconds + */ + short getHoldtime(); + + /** + * Set the keep alive timer value. + * + * @param holdTime + * keep alive timer value in seconds + */ + void setHoldtime(short holdTime); + + /** + * Return the connection type eBGP or iBGP. + * + * @return + * true if iBGP, false if it is eBGP + */ + boolean getIsIBgp(); + + /** + * Set the connection type eBGP or iBGP. + * + * @param isIBgp + * true if iBGP, false if it is eBGP + */ + void setIsIBgp(boolean isIBgp); + + /** + * Return the peer router IP address. + * + * @return + * IP address in string format + */ + String getPeerRouterId(); + + /** + * Set the peer router IP address. + * + * @param peerId + * IP address in string format + */ + void setPeerRouterId(String peerId); + + /** + * Set the peer router IP address and AS number. + * + * @param peerId + * IP address in string format + * @param asNumber + * AS number + */ + void setPeerRouterId(String peerId, int asNumber); +} diff --git a/framework/src/onos/bgp/api/src/main/java/org/onosproject/bgp/controller/package-info.java b/framework/src/onos/bgp/api/src/main/java/org/onosproject/bgp/controller/package-info.java new file mode 100755 index 00000000..4dd775b8 --- /dev/null +++ b/framework/src/onos/bgp/api/src/main/java/org/onosproject/bgp/controller/package-info.java @@ -0,0 +1,20 @@ +/* + * 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 controller API. + */ +package org.onosproject.bgp.controller; diff --git a/framework/src/onos/bgp/bgpio/pom.xml b/framework/src/onos/bgp/bgpio/pom.xml new file mode 100755 index 00000000..5d67f18c --- /dev/null +++ b/framework/src/onos/bgp/bgpio/pom.xml @@ -0,0 +1,76 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + ~ 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. + --> +<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/maven-v4_0_0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.onosproject</groupId> + <artifactId>onos-bgp</artifactId> + <version>1.4.0-SNAPSHOT</version> + <relativePath>../pom.xml</relativePath> + </parent> + + <artifactId>onos-bgpio</artifactId> + <packaging>bundle</packaging> + + <description>ONOS BGPio Protocol subsystem</description> + + <dependencies> + <dependency> + <groupId>org.onosproject</groupId> + <artifactId>onos-api</artifactId> + </dependency> + <dependency> + <groupId>org.onosproject</groupId> + <artifactId>onlab-osgi</artifactId> + </dependency> + + <dependency> + <groupId>com.fasterxml.jackson.core</groupId> + <artifactId>jackson-databind</artifactId> + </dependency> + <dependency> + <groupId>com.fasterxml.jackson.core</groupId> + <artifactId>jackson-annotations</artifactId> + </dependency> + + <dependency> + <groupId>org.osgi</groupId> + <artifactId>org.osgi.core</artifactId> + </dependency> + <dependency> + <groupId>org.apache.karaf.shell</groupId> + <artifactId>org.apache.karaf.shell.console</artifactId> + </dependency> + <dependency> + <groupId>org.apache.felix</groupId> + <artifactId>org.apache.felix.scr.annotations</artifactId> + </dependency> + </dependencies> + + <build> + <plugins> + <plugin> + <groupId>org.apache.felix</groupId> + <artifactId>maven-bundle-plugin</artifactId> + </plugin> + </plugins> + </build> + +</project> diff --git a/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/exceptions/BGPParseException.java b/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/exceptions/BGPParseException.java new file mode 100755 index 00000000..62427a44 --- /dev/null +++ b/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/exceptions/BGPParseException.java @@ -0,0 +1,106 @@ +/* + * 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.bgpio.exceptions; + +import org.jboss.netty.buffer.ChannelBuffer; + +/** + * Custom Exception for BGP IO. + */ +public class BGPParseException extends Exception { + + private static final long serialVersionUID = 1L; + private byte errorCode; + private byte errorSubCode; + private ChannelBuffer data; + + /** + * Default constructor to create a new exception. + */ + public BGPParseException() { + super(); + } + + /** + * Constructor to create exception from message and cause. + * + * @param message the detail of exception in string + * @param cause underlying cause of the error + */ + public BGPParseException(final String message, final Throwable cause) { + super(message, cause); + } + + /** + * Constructor to create exception from message. + * + * @param message the detail of exception in string + */ + public BGPParseException(final String message) { + super(message); + } + + /** + * Constructor to create exception from cause. + * + * @param cause underlying cause of the error + */ + public BGPParseException(final Throwable cause) { + super(cause); + } + + /** + * Constructor to create exception from error code and error subcode. + * + * @param errorCode error code of BGP message + * @param errorSubCode error subcode of BGP message + * @param data error data of BGP message + */ + public BGPParseException(final byte errorCode, final byte errorSubCode, final ChannelBuffer data) { + super(); + this.errorCode = errorCode; + this.errorSubCode = errorSubCode; + this.data = data; + } + + /** + * Returns errorcode for this exception. + * + * @return errorcode for this exception + */ + public byte getErrorCode() { + return this.errorCode; + } + + /** + * Returns error Subcode for this exception. + * + * @return error Subcode for this exception + */ + public byte getErrorSubCode() { + return this.errorSubCode; + } + + /** + * Returns error data for this exception. + * + * @return error data for this exception + */ + public ChannelBuffer getData() { + return this.data; + } +}
\ No newline at end of file diff --git a/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/exceptions/package-info.java b/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/exceptions/package-info.java new file mode 100755 index 00000000..78b28072 --- /dev/null +++ b/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/exceptions/package-info.java @@ -0,0 +1,20 @@ +/* + * 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 custom exceptions. + */ +package org.onosproject.bgpio.exceptions; diff --git a/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BGPKeepaliveMsg.java b/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BGPKeepaliveMsg.java new file mode 100644 index 00000000..c8aef36e --- /dev/null +++ b/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BGPKeepaliveMsg.java @@ -0,0 +1,58 @@ +/* + * 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.bgpio.protocol; + +import org.jboss.netty.buffer.ChannelBuffer; +import org.onosproject.bgpio.types.BGPHeader; + +/** + * Abstraction of an entity providing BGP Keepalive Message. + */ +public interface BGPKeepaliveMsg extends BGPMessage { + + @Override + BGPVersion getVersion(); + + @Override + BGPType getType(); + + @Override + void writeTo(ChannelBuffer channelBuffer); + + @Override + BGPHeader getHeader(); + + /** + * Builder interface with get and set functions to build Keepalive message. + */ + interface Builder extends BGPMessage.Builder { + + @Override + BGPKeepaliveMsg build(); + + @Override + BGPVersion getVersion(); + + @Override + BGPType getType(); + + @Override + Builder setHeader(BGPHeader bgpMsgHeader); + + @Override + BGPHeader getHeader(); + } +} diff --git a/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BGPMessage.java b/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BGPMessage.java new file mode 100644 index 00000000..a5d8154f --- /dev/null +++ b/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BGPMessage.java @@ -0,0 +1,92 @@ +/* + * 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.bgpio.protocol; + +import org.jboss.netty.buffer.ChannelBuffer; +import org.onosproject.bgpio.exceptions.BGPParseException; +import org.onosproject.bgpio.types.BGPHeader; + +/** + * Abstraction of an entity providing BGP Messages. + */ +public interface BGPMessage extends Writeable { + /** + * Returns BGP Header of BGP Message. + * + * @return BGP Header of BGP Message + */ + BGPHeader getHeader(); + + /** + * Returns version of BGP Message. + * + * @return version of BGP Message + */ + BGPVersion getVersion(); + + /** + * Returns BGP Type of BGP Message. + * + * @return BGP Type of BGP Message + */ + BGPType getType(); + + @Override + void writeTo(ChannelBuffer cb) throws BGPParseException; + + /** + * Builder interface with get and set functions to build BGP Message. + */ + interface Builder { + /** + * Builds BGP Message. + * + * @return BGP Message + * @throws BGPParseException while building bgp message + */ + BGPMessage build() throws BGPParseException; + + /** + * Returns BGP Version of BGP Message. + * + * @return BGP Version of BGP Message + */ + BGPVersion getVersion(); + + /** + * Returns BGP Type of BGP Message. + * + * @return BGP Type of BGP Message + */ + BGPType getType(); + + /** + * Returns BGP Header of BGP Message. + * + * @return BGP Header of BGP Message + */ + BGPHeader getHeader(); + + /** + * Sets BgpHeader and return its builder. + * + * @param bgpMsgHeader BGP Message Header + * @return builder by setting BGP message header + */ + Builder setHeader(BGPHeader bgpMsgHeader); + } +}
\ No newline at end of file diff --git a/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BGPMessageReader.java b/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BGPMessageReader.java new file mode 100755 index 00000000..18b8f58d --- /dev/null +++ b/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BGPMessageReader.java @@ -0,0 +1,36 @@ +/* + * 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.bgpio.protocol; + +import org.jboss.netty.buffer.ChannelBuffer; +import org.onosproject.bgpio.exceptions.BGPParseException; +import org.onosproject.bgpio.types.BGPHeader; + +/** + * Abstraction of an entity providing BGP Message Reader. + */ +public interface BGPMessageReader<T> { + + /** + * Reads the Objects in the BGP Message and Returns BGP Message. + * + * @param cb Channel Buffer + * @param bgpHeader BGP message header + * @return BGP Message + * @throws BGPParseException while parsing BGP message. + */ + T readFrom(ChannelBuffer cb, BGPHeader bgpHeader) throws BGPParseException; +}
\ No newline at end of file diff --git a/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BGPMessageWriter.java b/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BGPMessageWriter.java new file mode 100644 index 00000000..11f161c4 --- /dev/null +++ b/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BGPMessageWriter.java @@ -0,0 +1,36 @@ +/* + * 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.bgpio.protocol; + +import org.jboss.netty.buffer.ChannelBuffer; +import org.onosproject.bgpio.exceptions.BGPParseException; + +/** + * Abstraction of an entity providing BGP Message Writer. + */ +public interface BGPMessageWriter<T> { + + /** + * Writes the Objects of the BGP Message into Channel Buffer. + * + * @param cb Channel Buffer + * @param message BGP Message + * @throws BGPParseException + * While writing message + */ + void write(ChannelBuffer cb, T message) throws BGPParseException; +}
\ No newline at end of file diff --git a/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BGPOpenMsg.java b/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BGPOpenMsg.java new file mode 100644 index 00000000..c41e5eb6 --- /dev/null +++ b/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BGPOpenMsg.java @@ -0,0 +1,151 @@ +/* + * 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.bgpio.protocol; + +import java.util.LinkedList; + +import org.onosproject.bgpio.exceptions.BGPParseException; +import org.onosproject.bgpio.types.BGPHeader; +import org.onosproject.bgpio.types.BGPValueType; + +/** + * Abstraction of an entity providing BGP Open Message. + */ +public interface BGPOpenMsg extends BGPMessage { + + @Override + BGPHeader getHeader(); + + @Override + BGPVersion getVersion(); + + @Override + BGPType getType(); + + /** + * Returns hold time of Open Message. + * + * @return hold time of Open Message + */ + short getHoldTime(); + + /** + * Returns AS Number of Open Message. + * + * @return AS Number of Open Message + */ + short getAsNumber(); + + /** + * Returns BGP Identifier of Open Message. + * + * @return BGP Identifier of Open Message + */ + int getBgpId(); + + /** + * Returns capabilities of Open Message. + * + * @return capabilities of Open Message + */ + LinkedList<BGPValueType> getCapabilityTlv(); + + /** + * Builder interface with get and set functions to build Open message. + */ + interface Builder extends BGPMessage.Builder { + + @Override + BGPOpenMsg build() throws BGPParseException; + + @Override + BGPHeader getHeader(); + + @Override + BGPVersion getVersion(); + + @Override + BGPType getType(); + + /** + * Returns hold time of Open Message. + * + * @return hold time of Open Message + */ + short getHoldTime(); + + /** + * Sets hold time in Open Message and return its builder. + * + * @param holdtime + * hold timer value in open message + * @return builder by setting hold time + */ + Builder setHoldTime(short holdtime); + + /** + * Returns as number of Open Message. + * + * @return as number of Open Message + */ + short getAsNumber(); + + /** + * Sets AS number in Open Message and return its builder. + * + * @param asNumber + * as number in open message + * @return builder by setting asNumber + */ + Builder setAsNumber(short asNumber); + + /** + * Returns BGP Identifier of Open Message. + * + * @return BGP Identifier of Open Message + */ + int getBgpId(); + + /** + * Sets BGP Identifier in Open Message and return its builder. + * + * @param bgpId + * BGP Identifier in open message + * @return builder by setting BGP Identifier + */ + Builder setBgpId(int bgpId); + + /** + * Returns capabilities of Open Message. + * + * @return capabilities of Open Message + */ + LinkedList<BGPValueType> getCapabilityTlv(); + + /** + * Sets capabilities in Open Message and return its builder. + * + * @param capabilityTlv + * capabilities in open message + * @return builder by setting capabilities + */ + Builder setCapabilityTlv(LinkedList<BGPValueType> capabilityTlv); + + @Override + Builder setHeader(BGPHeader bgpMsgHeader); + } +} diff --git a/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BGPType.java b/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BGPType.java new file mode 100755 index 00000000..d3349156 --- /dev/null +++ b/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BGPType.java @@ -0,0 +1,45 @@ +/* + * 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.bgpio.protocol; + +/** + * Enum to Provide the Different types of BGP messages. + */ +public enum BGPType { + + NONE(0), OPEN(1), UPDATE(2), NOTIFICATION(3), KEEP_ALIVE(4); + + int value; + + /** + * Assign value with the value val as the types of BGP message. + * + * @param val type of BGP message + */ + BGPType(int val) { + value = val; + } + + /** + * Returns value as type of BGP message. + * + * @return value type of BGP message + */ + public byte getType() { + return (byte) value; + } +}
\ No newline at end of file diff --git a/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BGPVersion.java b/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BGPVersion.java new file mode 100755 index 00000000..97bc7dce --- /dev/null +++ b/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BGPVersion.java @@ -0,0 +1,45 @@ +/* + * 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.bgpio.protocol; + +/** + * Enum to provide BGP Message Version. + */ +public enum BGPVersion { + + BGP_4(4); + + public final int packetVersion; + + /** + * Assign BGP PacketVersion with specified packetVersion. + * + * @param packetVersion version of BGP + */ + BGPVersion(final int packetVersion) { + this.packetVersion = packetVersion; + } + + /** + * Returns Packet version of BGP Message. + * + * @return packetVersion + */ + public int getPacketVersion() { + return packetVersion; + } +}
\ No newline at end of file diff --git a/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/IGPRouterID.java b/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/IGPRouterID.java new file mode 100644 index 00000000..377d12b7 --- /dev/null +++ b/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/IGPRouterID.java @@ -0,0 +1,23 @@ +/*
+ * 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.bgpio.protocol;
+
+/**
+ * Provides Abstraction of IGP RouterID TLV.
+ */
+public interface IGPRouterID {
+}
\ No newline at end of file diff --git a/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/Writeable.java b/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/Writeable.java new file mode 100755 index 00000000..72df7f32 --- /dev/null +++ b/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/Writeable.java @@ -0,0 +1,35 @@ +/* + * 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.bgpio.protocol; + +import org.jboss.netty.buffer.ChannelBuffer; +import org.onosproject.bgpio.exceptions.BGPParseException; + +/** + * Abstraction of an entity providing functionality to write byte streams of + * Messages to channel buffer. + */ +public interface Writeable { + + /** + * Writes byte streams of messages to channel buffer. + * + * @param cb channelBuffer + * @throws BGPParseException when error occurs while writing BGP message to channel buffer + */ + void writeTo(ChannelBuffer cb) throws BGPParseException; +}
\ No newline at end of file diff --git a/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/link_state/BGPPrefixLSIdentifier.java b/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/link_state/BGPPrefixLSIdentifier.java new file mode 100644 index 00000000..4fef47ff --- /dev/null +++ b/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/link_state/BGPPrefixLSIdentifier.java @@ -0,0 +1,228 @@ +/* + * 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.bgpio.protocol.link_state; + +import java.util.Iterator; +import java.util.LinkedList; +import java.util.Objects; + +import org.jboss.netty.buffer.ChannelBuffer; +import org.onosproject.bgpio.exceptions.BGPParseException; +import org.onosproject.bgpio.types.BGPErrorType; +import org.onosproject.bgpio.types.BGPValueType; +import org.onosproject.bgpio.types.IPReachabilityInformationTlv; +import org.onosproject.bgpio.types.OSPFRouteTypeTlv; +import org.onosproject.bgpio.types.attr.BgpAttrNodeMultiTopologyId; +import org.onosproject.bgpio.util.UnSupportedAttribute; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.base.MoreObjects; + +/** + * Provides Implementation of Local node descriptors and prefix descriptors. + */ +public class BGPPrefixLSIdentifier { + + protected static final Logger log = LoggerFactory.getLogger(BGPPrefixLSIdentifier.class); + public static final int TYPE_AND_LEN = 4; + private NodeDescriptors localNodeDescriptors; + private LinkedList<BGPValueType> prefixDescriptor; + + /** + * Resets parameters. + */ + public BGPPrefixLSIdentifier() { + this.localNodeDescriptors = null; + this.prefixDescriptor = null; + } + + /** + * Constructor to initialize parameters. + * + * @param localNodeDescriptors Local node descriptors + * @param prefixDescriptor Prefix Descriptors + */ + public BGPPrefixLSIdentifier(NodeDescriptors localNodeDescriptors, LinkedList<BGPValueType> prefixDescriptor) { + this.localNodeDescriptors = localNodeDescriptors; + this.prefixDescriptor = prefixDescriptor; + } + + /** + * Reads the channel buffer and parses Prefix Identifier. + * + * @param cb ChannelBuffer + * @param protocolId protocol ID + * @return object of this class + * @throws BGPParseException while parsing Prefix Identifier + */ + public static BGPPrefixLSIdentifier parsePrefixIdendifier(ChannelBuffer cb, byte protocolId) + throws BGPParseException { + //Parse Local Node descriptor + NodeDescriptors localNodeDescriptors = new NodeDescriptors(); + localNodeDescriptors = parseLocalNodeDescriptors(cb, protocolId); + + //Parse Prefix descriptor + LinkedList<BGPValueType> prefixDescriptor = new LinkedList<>(); + prefixDescriptor = parsePrefixDescriptors(cb); + return new BGPPrefixLSIdentifier(localNodeDescriptors, prefixDescriptor); + } + + /** + * Parse local node descriptors. + * + * @param cb ChannelBuffer + * @param protocolId protocol identifier + * @return LocalNodeDescriptors + * @throws BGPParseException while parsing local node descriptors + */ + public static NodeDescriptors parseLocalNodeDescriptors(ChannelBuffer cb, byte protocolId) + throws BGPParseException { + ChannelBuffer tempBuf = cb; + short type = cb.readShort(); + short length = cb.readShort(); + if (cb.readableBytes() < length) { + //length + 4 implies data contains type, length and value + throw new BGPParseException(BGPErrorType.UPDATE_MESSAGE_ERROR, BGPErrorType.OPTIONAL_ATTRIBUTE_ERROR, + tempBuf.readBytes(cb.readableBytes() + TYPE_AND_LEN)); + } + NodeDescriptors localNodeDescriptors = new NodeDescriptors(); + ChannelBuffer tempCb = cb.readBytes(length); + + if (type == NodeDescriptors.LOCAL_NODE_DES_TYPE) { + localNodeDescriptors = NodeDescriptors.read(tempCb, length, type, protocolId); + } else { + throw new BGPParseException(BGPErrorType.UPDATE_MESSAGE_ERROR, + BGPErrorType.MALFORMED_ATTRIBUTE_LIST, null); + } + return localNodeDescriptors; + } + + /** + * Parse list of prefix descriptors. + * + * @param cb ChannelBuffer + * @return list of prefix descriptors + * @throws BGPParseException while parsing list of prefix descriptors + */ + public static LinkedList<BGPValueType> parsePrefixDescriptors(ChannelBuffer cb) throws BGPParseException { + LinkedList<BGPValueType> prefixDescriptor = new LinkedList<>(); + BGPValueType tlv = null; + boolean isIpReachInfo = false; + ChannelBuffer tempCb; + int count = 0; + + while (cb.readableBytes() > 0) { + ChannelBuffer tempBuf = cb; + short type = cb.readShort(); + short length = cb.readShort(); + if (cb.readableBytes() < length) { + //length + 4 implies data contains type, length and value + throw new BGPParseException(BGPErrorType.UPDATE_MESSAGE_ERROR, BGPErrorType.OPTIONAL_ATTRIBUTE_ERROR, + tempBuf.readBytes(cb.readableBytes() + TYPE_AND_LEN)); + } + tempCb = cb.readBytes(length); + switch (type) { + case OSPFRouteTypeTlv.TYPE: + tlv = OSPFRouteTypeTlv.read(tempCb); + break; + case IPReachabilityInformationTlv.TYPE: + tlv = IPReachabilityInformationTlv.read(tempCb, length); + isIpReachInfo = true; + break; + case BgpAttrNodeMultiTopologyId.ATTRNODE_MULTITOPOLOGY: + tlv = BgpAttrNodeMultiTopologyId.read(tempCb); + count = count + 1; + if (count > 1) { + //length + 4 implies data contains type, length and value + throw new BGPParseException(BGPErrorType.UPDATE_MESSAGE_ERROR, + BGPErrorType.OPTIONAL_ATTRIBUTE_ERROR, tempBuf.readBytes(length + TYPE_AND_LEN)); + } + break; + default: + UnSupportedAttribute.skipBytes(tempCb, length); + } + prefixDescriptor.add(tlv); + } + + if (!isIpReachInfo) { + throw new BGPParseException(BGPErrorType.UPDATE_MESSAGE_ERROR, BGPErrorType.OPTIONAL_ATTRIBUTE_ERROR, + null); + } + return prefixDescriptor; + } + + /** + * Returns local node descriptors. + * + * @return local node descriptors + */ + public NodeDescriptors getLocalNodeDescriptors() { + return this.localNodeDescriptors; + } + + /** + * Returns Prefix descriptors. + * + * @return Prefix descriptors + */ + public LinkedList<BGPValueType> getPrefixdescriptor() { + return this.prefixDescriptor; + } + + @Override + public int hashCode() { + return Objects.hash(prefixDescriptor.hashCode(), localNodeDescriptors); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + + if (obj instanceof BGPPrefixLSIdentifier) { + int countObjSubTlv = 0; + int countOtherSubTlv = 0; + boolean isCommonSubTlv = true; + BGPPrefixLSIdentifier other = (BGPPrefixLSIdentifier) obj; + + Iterator<BGPValueType> objListIterator = other.prefixDescriptor.iterator(); + countOtherSubTlv = other.prefixDescriptor.size(); + countObjSubTlv = prefixDescriptor.size(); + if (countObjSubTlv != countOtherSubTlv) { + return false; + } else { + while (objListIterator.hasNext() && isCommonSubTlv) { + BGPValueType subTlv = objListIterator.next(); + isCommonSubTlv = Objects.equals(prefixDescriptor.contains(subTlv), + other.prefixDescriptor.contains(subTlv)); + } + return isCommonSubTlv && Objects.equals(this.localNodeDescriptors, other.localNodeDescriptors); + } + } + return false; + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(getClass()) + .add("localNodeDescriptors", localNodeDescriptors) + .add("prefixDescriptor", prefixDescriptor) + .toString(); + } +}
\ No newline at end of file diff --git a/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/link_state/NodeDescriptors.java b/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/link_state/NodeDescriptors.java new file mode 100644 index 00000000..a03b2bae --- /dev/null +++ b/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/link_state/NodeDescriptors.java @@ -0,0 +1,225 @@ +/* + * 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.bgpio.protocol.link_state; + +import java.util.Iterator; +import java.util.LinkedList; +import java.util.Objects; + +import org.jboss.netty.buffer.ChannelBuffer; +import org.onosproject.bgpio.exceptions.BGPParseException; +import org.onosproject.bgpio.types.AreaIDTlv; +import org.onosproject.bgpio.types.AutonomousSystemTlv; +import org.onosproject.bgpio.types.BGPErrorType; +import org.onosproject.bgpio.types.BGPLSIdentifierTlv; +import org.onosproject.bgpio.types.BGPValueType; +import org.onosproject.bgpio.types.IsIsNonPseudonode; +import org.onosproject.bgpio.types.IsIsPseudonode; +import org.onosproject.bgpio.types.OSPFNonPseudonode; +import org.onosproject.bgpio.types.OSPFPseudonode; +import org.onosproject.bgpio.util.UnSupportedAttribute; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.base.MoreObjects; + +/** + * Provides Local and Remote NodeDescriptors which contains Node Descriptor Sub-TLVs. + */ +public class NodeDescriptors { + + /* + *Reference :draft-ietf-idr-ls-distribution-11 + 0 1 2 3 + 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | Type | Length | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | | + // Node Descriptor Sub-TLVs (variable) // + | | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + + Figure : Local or Remote Node Descriptors TLV format + */ + + protected static final Logger log = LoggerFactory.getLogger(NodeDescriptors.class); + + public static final short LOCAL_NODE_DES_TYPE = 256; + public static final short REMOTE_NODE_DES_TYPE = 257; + public static final short IGP_ROUTERID_TYPE = 515; + public static final short IS_IS_LEVEL_1_PROTOCOL_ID = 1; + public static final short IS_IS_LEVEL_2_PROTOCOL_ID = 2; + public static final short OSPF_V2_PROTOCOL_ID = 3; + public static final short OSPF_V3_PROTOCOL_ID = 6; + public static final int TYPE_AND_LEN = 4; + public static final int ISISNONPSEUDONODE_LEN = 6; + public static final int ISISPSEUDONODE_LEN = 7; + public static final int OSPFNONPSEUDONODE_LEN = 4; + public static final int OSPFPSEUDONODE_LEN = 8; + private LinkedList<BGPValueType> subTlvs; + private short deslength; + private short desType; + + /** + * Resets parameters. + */ + public NodeDescriptors() { + this.subTlvs = null; + this.deslength = 0; + this.desType = 0; + } + + /** + * Constructor to initialize parameters. + * + * @param subTlvs list of subTlvs + * @param deslength Descriptors length + * @param desType local node descriptor or remote node descriptor type + */ + public NodeDescriptors(LinkedList<BGPValueType> subTlvs, short deslength, short desType) { + this.subTlvs = subTlvs; + this.deslength = deslength; + this.desType = desType; + } + + /** + * Returns list of subTlvs. + * + * @return subTlvs list of subTlvs + */ + public LinkedList<BGPValueType> getSubTlvs() { + return subTlvs; + } + + @Override + public int hashCode() { + return Objects.hash(subTlvs.hashCode()); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + + if (obj instanceof NodeDescriptors) { + int countObjSubTlv = 0; + int countOtherSubTlv = 0; + boolean isCommonSubTlv = true; + NodeDescriptors other = (NodeDescriptors) obj; + Iterator<BGPValueType> objListIterator = other.subTlvs.iterator(); + countOtherSubTlv = other.subTlvs.size(); + countObjSubTlv = subTlvs.size(); + if (countObjSubTlv != countOtherSubTlv) { + return false; + } else { + while (objListIterator.hasNext() && isCommonSubTlv) { + BGPValueType subTlv = objListIterator.next(); + isCommonSubTlv = Objects.equals(subTlvs.contains(subTlv), other.subTlvs.contains(subTlv)); + } + return isCommonSubTlv; + } + } + return false; + } + + /** + * Reads node descriptors Sub-TLVs. + * + * @param cb ChannelBuffer + * @param desLength node descriptor length + * @param desType local node descriptor or remote node descriptor type + * @param protocolId protocol ID + * @return object of NodeDescriptors + * @throws BGPParseException while parsing node descriptors + */ + public static NodeDescriptors read(ChannelBuffer cb, short desLength, short desType, byte protocolId) + throws BGPParseException { + LinkedList<BGPValueType> subTlvs; + subTlvs = new LinkedList<>(); + BGPValueType tlv = null; + + while (cb.readableBytes() > 0) { + ChannelBuffer tempBuf = cb; + short type = cb.readShort(); + short length = cb.readShort(); + if (cb.readableBytes() < length) { + throw new BGPParseException(BGPErrorType.UPDATE_MESSAGE_ERROR, BGPErrorType.OPTIONAL_ATTRIBUTE_ERROR, + tempBuf.readBytes(cb.readableBytes() + TYPE_AND_LEN)); + } + ChannelBuffer tempCb = cb.readBytes(length); + switch (type) { + case AutonomousSystemTlv.TYPE: + tlv = AutonomousSystemTlv.read(tempCb); + break; + case BGPLSIdentifierTlv.TYPE: + tlv = BGPLSIdentifierTlv.read(tempCb); + break; + case AreaIDTlv.TYPE: + tlv = AreaIDTlv.read(tempCb); + break; + case IGP_ROUTERID_TYPE: + if (protocolId == IS_IS_LEVEL_1_PROTOCOL_ID || protocolId == IS_IS_LEVEL_2_PROTOCOL_ID) { + if (length == ISISNONPSEUDONODE_LEN) { + tlv = IsIsNonPseudonode.read(tempCb); + } else if (length == ISISPSEUDONODE_LEN) { + tlv = IsIsPseudonode.read(tempCb); + } + } else if (protocolId == OSPF_V2_PROTOCOL_ID || protocolId == OSPF_V3_PROTOCOL_ID) { + if (length == OSPFNONPSEUDONODE_LEN) { + tlv = OSPFNonPseudonode.read(tempCb); + } else if (length == OSPFPSEUDONODE_LEN) { + tlv = OSPFPseudonode.read(tempCb); + } + } + break; + default: + UnSupportedAttribute.skipBytes(tempCb, length); + } + subTlvs.add(tlv); + } + return new NodeDescriptors(subTlvs, desLength, desType); + } + + /** + * Returns node descriptors length. + * + * @return node descriptors length + */ + public short getLength() { + return this.deslength; + } + + /** + * Returns node descriptors type. + * + * @return node descriptors type + */ + public short getType() { + return this.desType; + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(getClass()) + .add("desType", desType) + .add("deslength", deslength) + .add("subTlvs", subTlvs) + .toString(); + } +}
\ No newline at end of file diff --git a/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/link_state/package-info.java b/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/link_state/package-info.java new file mode 100755 index 00000000..d2a2ccf3 --- /dev/null +++ b/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/link_state/package-info.java @@ -0,0 +1,20 @@ +/* + * 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 Protocol specific link state details. + */ +package org.onosproject.bgpio.protocol.link_state;
\ No newline at end of file diff --git a/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/package-info.java b/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/package-info.java new file mode 100755 index 00000000..723b31b1 --- /dev/null +++ b/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/package-info.java @@ -0,0 +1,20 @@ +/* + * 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 Protocol specific components. + */ +package org.onosproject.bgpio.protocol; diff --git a/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/BGPKeepaliveMsgVer4.java b/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/BGPKeepaliveMsgVer4.java new file mode 100644 index 00000000..a6668b3a --- /dev/null +++ b/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/BGPKeepaliveMsgVer4.java @@ -0,0 +1,172 @@ +/* + * 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.bgpio.protocol.ver4; + +import org.jboss.netty.buffer.ChannelBuffer; +import org.onosproject.bgpio.exceptions.BGPParseException; +import org.onosproject.bgpio.protocol.BGPKeepaliveMsg; +import org.onosproject.bgpio.protocol.BGPMessageReader; +import org.onosproject.bgpio.protocol.BGPMessageWriter; +import org.onosproject.bgpio.types.BGPHeader; +import org.onosproject.bgpio.protocol.BGPType; +import org.onosproject.bgpio.protocol.BGPVersion; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.base.MoreObjects; + +/** + * Provides BGP keep alive message. + */ +class BGPKeepaliveMsgVer4 implements BGPKeepaliveMsg { + + /* + <Keepalive Message>::= <Common Header> + A KEEPALIVE message consists of only the message header and has a + length of 19 octets. + + 0 1 2 3 + 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | | + + + + | | + + + + | Marker | + + + + | | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | Length | Type | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + + REFERENCE : RFC 4271 + */ + + protected static final Logger log = LoggerFactory + .getLogger(BGPKeepaliveMsgVer4.class); + + private BGPHeader bgpMsgHeader; + public static final byte PACKET_VERSION = 4; + public static final int PACKET_MINIMUM_LENGTH = 19; + public static final int MARKER_LENGTH = 16; + public static final BGPType MSG_TYPE = BGPType.KEEP_ALIVE; + public static byte[] marker = new byte[] {(byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, + (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, + (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, + (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff}; + + public static final BGPKeepaliveMsgVer4.Reader READER = new Reader(); + + /** + * Reader class for reading BGP keepalive message from channel buffer. + */ + static class Reader implements BGPMessageReader<BGPKeepaliveMsg> { + + @Override + public BGPKeepaliveMsg readFrom(ChannelBuffer cb, BGPHeader bgpHeader) + throws BGPParseException { + + /* bgpHeader is not required in case of keepalive message and + Header is already read and no other fields except header in keepalive message.*/ + return new BGPKeepaliveMsgVer4(); + } + } + + /** + * Default constructor. + */ + BGPKeepaliveMsgVer4() { + } + + /** + * Builder class for BGP keepalive message. + */ + static class Builder implements BGPKeepaliveMsg.Builder { + BGPHeader bgpMsgHeader; + + @Override + public BGPVersion getVersion() { + return BGPVersion.BGP_4; + } + + @Override + public BGPType getType() { + return BGPType.KEEP_ALIVE; + } + + @Override + public BGPHeader getHeader() { + return this.bgpMsgHeader; + } + + @Override + public Builder setHeader(BGPHeader bgpMsgHeader) { + this.bgpMsgHeader = bgpMsgHeader; + return this; + } + + @Override + public BGPKeepaliveMsg build() { + return new BGPKeepaliveMsgVer4(); + } + } + + @Override + public void writeTo(ChannelBuffer cb) { + WRITER.write(cb, this); + } + + static final Writer WRITER = new Writer(); + + /** + * Writer class for writing the BGP keepalive message to channel buffer. + */ + static class Writer implements BGPMessageWriter<BGPKeepaliveMsgVer4> { + + @Override + public void write(ChannelBuffer cb, BGPKeepaliveMsgVer4 message) { + + // write marker + cb.writeBytes(marker, 0, MARKER_LENGTH); + + // write length of header + cb.writeShort(PACKET_MINIMUM_LENGTH); + + // write the type of message + cb.writeByte(MSG_TYPE.getType()); + } + } + + @Override + public BGPVersion getVersion() { + return BGPVersion.BGP_4; + } + + @Override + public BGPType getType() { + return MSG_TYPE; + } + + @Override + public BGPHeader getHeader() { + return this.bgpMsgHeader; + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(getClass()).toString(); + } +} diff --git a/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/BGPOpenMsgVer4.java b/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/BGPOpenMsgVer4.java new file mode 100644 index 00000000..1348ecfd --- /dev/null +++ b/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/BGPOpenMsgVer4.java @@ -0,0 +1,468 @@ +/* + * 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.bgpio.protocol.ver4; + +import java.util.LinkedList; +import java.util.ListIterator; + +import org.jboss.netty.buffer.ChannelBuffer; +import org.onosproject.bgpio.exceptions.BGPParseException; +import org.onosproject.bgpio.protocol.BGPMessageReader; +import org.onosproject.bgpio.protocol.BGPMessageWriter; +import org.onosproject.bgpio.protocol.BGPOpenMsg; +import org.onosproject.bgpio.protocol.BGPType; +import org.onosproject.bgpio.protocol.BGPVersion; +import org.onosproject.bgpio.types.BGPErrorType; +import org.onosproject.bgpio.types.BGPHeader; +import org.onosproject.bgpio.types.BGPValueType; +import org.onosproject.bgpio.util.Validation; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.base.MoreObjects; + +/** + * Provides BGP open message. + */ +public class BGPOpenMsgVer4 implements BGPOpenMsg { + + /* + 0 1 2 3 + 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 + +-+-+-+-+-+-+-+-+ + | Version | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | My Autonomous System | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | Hold Time | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | BGP Identifier | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | Opt Parm Len | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | Optional Parameters (variable) | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + OPEN Message Format + REFERENCE : RFC 4271 + */ + + protected static final Logger log = LoggerFactory.getLogger(BGPOpenMsgVer4.class); + + public static final byte PACKET_VERSION = 4; + public static final int OPEN_MSG_MINIMUM_LENGTH = 10; + public static final int MSG_HEADER_LENGTH = 19; + public static final int MARKER_LENGTH = 16; + public static final int DEFAULT_HOLD_TIME = 120; + public static final int OPT_PARA_TYPE_CAPABILITY = 2; + public static final BGPType MSG_TYPE = BGPType.OPEN; + public static final byte[] MARKER = new byte[]{(byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, + (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, + (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff}; + public static final BGPHeader DEFAULT_OPEN_HEADER = new BGPHeader(MARKER, + (short) OPEN_MSG_MINIMUM_LENGTH, (byte) 0X01); + private BGPHeader bgpMsgHeader; + private byte version; + private short asNumber; + private short holdTime; + private int bgpId; + private LinkedList<BGPValueType> capabilityTlv; + + public static final BGPOpenMsgVer4.Reader READER = new Reader(); + + /** + * reset variables. + */ + public BGPOpenMsgVer4() { + this.bgpMsgHeader = null; + this.version = 0; + this.holdTime = 0; + this.asNumber = 0; + this.bgpId = 0; + this.capabilityTlv = null; + } + + /** + * Constructor to initialize all variables of BGP Open message. + * + * @param bgpMsgHeader + * BGP Header in open message + * @param version + * BGP version in open message + * @param holdTime + * hold time in open message + * @param asNumber + * AS number in open message + * @param bgpId + * BGP identifier in open message + * @param capabilityTlv + * capabilities in open message + */ + public BGPOpenMsgVer4(BGPHeader bgpMsgHeader, byte version, short asNumber, short holdTime, + int bgpId, LinkedList<BGPValueType> capabilityTlv) { + this.bgpMsgHeader = bgpMsgHeader; + this.version = version; + this.asNumber = asNumber; + this.holdTime = holdTime; + this.bgpId = bgpId; + this.capabilityTlv = capabilityTlv; + } + + @Override + public BGPHeader getHeader() { + return this.bgpMsgHeader; + } + + @Override + public BGPVersion getVersion() { + return BGPVersion.BGP_4; + } + + @Override + public BGPType getType() { + return MSG_TYPE; + } + + @Override + public short getHoldTime() { + return this.holdTime; + } + + @Override + public short getAsNumber() { + return this.asNumber; + } + + @Override + public int getBgpId() { + return this.bgpId; + } + + @Override + public LinkedList<BGPValueType> getCapabilityTlv() { + return this.capabilityTlv; + } + + /** + * Reader class for reading BGP open message from channel buffer. + */ + public static class Reader implements BGPMessageReader<BGPOpenMsg> { + + @Override + public BGPOpenMsg readFrom(ChannelBuffer cb, BGPHeader bgpHeader) throws BGPParseException { + + byte version; + short holdTime; + short asNumber; + int bgpId; + byte optParaLen = 0; + byte optParaType; + byte capParaLen = 0; + LinkedList<BGPValueType> capabilityTlv = new LinkedList<>(); + + if (cb.readableBytes() < OPEN_MSG_MINIMUM_LENGTH) { + log.error("[readFrom] Invalid length: Packet size is less than the minimum length "); + Validation.validateLen(BGPErrorType.OPEN_MESSAGE_ERROR, BGPErrorType.BAD_MESSAGE_LENGTH, + cb.readableBytes()); + } + + // Read version + version = cb.readByte(); + if (version != PACKET_VERSION) { + log.error("[readFrom] Invalid version: " + version); + throw new BGPParseException(BGPErrorType.OPEN_MESSAGE_ERROR, + BGPErrorType.UNSUPPORTED_VERSION_NUMBER, null); + } + + // Read AS number + asNumber = cb.readShort(); + + // Read Hold timer + holdTime = cb.readShort(); + + // Read BGP Identifier + bgpId = cb.readInt(); + + // Read optional parameter length + optParaLen = cb.readByte(); + + // Read Capabilities if optional parameter length is greater than 0 + if (optParaLen != 0) { + // Read optional parameter type + optParaType = cb.readByte(); + + // Read optional parameter length + capParaLen = cb.readByte(); + + if (cb.readableBytes() < capParaLen) { + throw new BGPParseException(BGPErrorType.OPEN_MESSAGE_ERROR, (byte) 0, null); + } + + ChannelBuffer capaCb = cb.readBytes(capParaLen); + + // Parse capabilities only if optional parameter type is 2 + if ((optParaType == OPT_PARA_TYPE_CAPABILITY) && (capParaLen != 0)) { + capabilityTlv = parseCapabilityTlv(capaCb); + } else { + throw new BGPParseException(BGPErrorType.OPEN_MESSAGE_ERROR, + BGPErrorType.UNSUPPORTED_OPTIONAL_PARAMETER, null); + } + } + return new BGPOpenMsgVer4(bgpHeader, version, asNumber, holdTime, bgpId, capabilityTlv); + } + } + + /** + * Parsing capabilities. + * + * @param cb of type channel buffer + * @return capabilityTlv of open message + * @throws BGPParseException while parsing capabilities + */ + protected static LinkedList<BGPValueType> parseCapabilityTlv(ChannelBuffer cb) throws BGPParseException { + + LinkedList<BGPValueType> capabilityTlv = new LinkedList<>(); + + // TODO: Capability parsing + return capabilityTlv; + } + + /** + * Builder class for BGP open message. + */ + static class Builder implements BGPOpenMsg.Builder { + + private boolean isHeaderSet = false; + private BGPHeader bgpMsgHeader; + private boolean isHoldTimeSet = false; + private short holdTime; + private boolean isAsNumSet = false; + private short asNumber; + private boolean isBgpIdSet = false; + private int bgpId; + + LinkedList<BGPValueType> capabilityTlv = new LinkedList<>(); + + @Override + public BGPOpenMsg build() throws BGPParseException { + BGPHeader bgpMsgHeader = this.isHeaderSet ? this.bgpMsgHeader : DEFAULT_OPEN_HEADER; + short holdTime = this.isHoldTimeSet ? this.holdTime : DEFAULT_HOLD_TIME; + + if (!this.isAsNumSet) { + throw new BGPParseException("BGP AS number is not set (mandatory)"); + } + + if (!this.isBgpIdSet) { + throw new BGPParseException("BGPID is not set (mandatory)"); + } + + // TODO: capabilities build + + return new BGPOpenMsgVer4(bgpMsgHeader, PACKET_VERSION, this.asNumber, holdTime, this.bgpId, + this.capabilityTlv); + } + + @Override + public BGPHeader getHeader() { + return this.bgpMsgHeader; + } + + @Override + public Builder setHeader(BGPHeader bgpMsgHeader) { + this.bgpMsgHeader = bgpMsgHeader; + return this; + } + + @Override + public BGPVersion getVersion() { + return BGPVersion.BGP_4; + } + + @Override + public BGPType getType() { + return MSG_TYPE; + } + + @Override + public short getHoldTime() { + return this.holdTime; + } + + @Override + public short getAsNumber() { + return this.asNumber; + } + + @Override + public int getBgpId() { + return this.bgpId; + } + + @Override + public LinkedList<BGPValueType> getCapabilityTlv() { + return this.capabilityTlv; + } + + @Override + public Builder setHoldTime(short holdTime) { + this.holdTime = holdTime; + this.isHoldTimeSet = true; + return this; + } + + @Override + public Builder setAsNumber(short asNumber) { + this.asNumber = asNumber; + this.isAsNumSet = true; + return this; + } + + @Override + public Builder setBgpId(int bgpId) { + this.bgpId = bgpId; + this.isBgpIdSet = true; + return this; + } + + @Override + public Builder setCapabilityTlv(LinkedList<BGPValueType> capabilityTlv) { + this.capabilityTlv = capabilityTlv; + return this; + } + } + + @Override + public void writeTo(ChannelBuffer cb) { + try { + WRITER.write(cb, this); + } catch (BGPParseException e) { + log.debug("[writeTo] Error: " + e.toString()); + } + } + + public static final Writer WRITER = new Writer(); + + /** + * Writer class for writing BGP open message to channel buffer. + */ + public static class Writer implements BGPMessageWriter<BGPOpenMsgVer4> { + + @Override + public void write(ChannelBuffer cb, BGPOpenMsgVer4 message) throws BGPParseException { + + int optParaLen = 0; + + int startIndex = cb.writerIndex(); + + // write common header and get msg length index + int msgLenIndex = message.bgpMsgHeader.write(cb); + + if (msgLenIndex <= 0) { + throw new BGPParseException("Unable to write message header."); + } + + // write version in 1-octet + cb.writeByte(message.version); + + // TODO : Write AS number based on capabilities + cb.writeShort(message.asNumber); + + // write HoldTime in next 2-octet + cb.writeShort(message.holdTime); + + // write BGP Identifier in next 4-octet + cb.writeInt(message.bgpId); + + // store the index of Optional parameter length + int optParaLenIndex = cb.writerIndex(); + + // set optional parameter length as 0 + cb.writeByte(0); + + // Pack capability TLV + optParaLen = message.packCapabilityTlv(cb, message); + + if (optParaLen != 0) { + // Update optional parameter length + cb.setByte(optParaLenIndex, (byte) (optParaLen + 2)); //+2 for optional parameter type. + } + + // write OPEN Object Length + int length = cb.writerIndex() - startIndex; + cb.setShort(msgLenIndex, (short) length); + message.bgpMsgHeader.setLength((short) length); + } + } + + /** + * returns length of capability tlvs. + * + * @param cb of type channel buffer + * @param message of type BGPOpenMsgVer4 + * @return capParaLen of open message + */ + protected int packCapabilityTlv(ChannelBuffer cb, BGPOpenMsgVer4 message) { + int startIndex = cb.writerIndex(); + int capParaLen = 0; + int capParaLenIndex = 0; + + LinkedList<BGPValueType> capabilityTlv = message.capabilityTlv; + ListIterator<BGPValueType> listIterator = capabilityTlv.listIterator(); + + if (listIterator.hasNext()) { + // Set optional parameter type as 2 + cb.writeByte(OPT_PARA_TYPE_CAPABILITY); + + // Store the index of capability parameter length and update length at the end + capParaLenIndex = cb.writerIndex(); + + // Set capability parameter length as 0 + cb.writeByte(0); + + // Update the startIndex to know the length of capability tlv + startIndex = cb.writerIndex(); + } + + while (listIterator.hasNext()) { + BGPValueType tlv = listIterator.next(); + if (tlv == null) { + log.debug("Warning: tlv is null from CapabilityTlv list"); + continue; + } + tlv.write(cb); + } + + capParaLen = cb.writerIndex() - startIndex; + + if (capParaLen != 0) { + // Update capability parameter length + cb.setByte(capParaLenIndex, (byte) capParaLen); + } + return capParaLen; + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(getClass()) + .add("bgpMsgHeader", bgpMsgHeader) + .add("version", version) + .add("holdTime", holdTime) + .add("asNumber", asNumber) + .add("bgpId", bgpId) + .add("capabilityTlv", capabilityTlv) + .toString(); + } +} diff --git a/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/package-info.java b/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/package-info.java new file mode 100755 index 00000000..fb8c67c0 --- /dev/null +++ b/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/package-info.java @@ -0,0 +1,20 @@ +/* + * 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 Protocol specific details of version 4. + */ +package org.onosproject.bgpio.protocol.ver4;
\ No newline at end of file diff --git a/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/AreaIDTlv.java b/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/AreaIDTlv.java new file mode 100644 index 00000000..52bae466 --- /dev/null +++ b/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/AreaIDTlv.java @@ -0,0 +1,126 @@ +/* + * 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.bgpio.types; + +import java.util.Objects; + +import org.jboss.netty.buffer.ChannelBuffer; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.base.MoreObjects; + +/** + * Provides AreaID Tlv which contains opaque value (32 Bit Area-ID). + */ +public class AreaIDTlv implements BGPValueType { + + /* Reference :draft-ietf-idr-ls-distribution-11 + * 0 1 2 3 + 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | Type= 514 | Length=4 | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | opaque value (32 Bit Area-ID) | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + */ + + protected static final Logger log = LoggerFactory.getLogger(AreaIDTlv.class); + + public static final short TYPE = 514; + public static final short LENGTH = 4; + + private final int areaID; + + /** + * Constructor to initialize areaID. + * + * @param areaID of BGP AreaID Tlv + */ + public AreaIDTlv(int areaID) { + this.areaID = areaID; + } + + /** + * Returns object of this class with specified areaID. + * + * @param areaID opaque value of area id + * @return object of AreaIDTlv + */ + public static AreaIDTlv of(final int areaID) { + return new AreaIDTlv(areaID); + } + + /** + * Returns opaque value of area id. + * + * @return opaque value of area id + */ + public int getAreaID() { + return areaID; + } + + @Override + public int hashCode() { + return Objects.hash(areaID); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + + if (obj instanceof AreaIDTlv) { + AreaIDTlv other = (AreaIDTlv) obj; + return Objects.equals(areaID, other.areaID); + } + return false; + } + + @Override + public int write(ChannelBuffer c) { + int iLenStartIndex = c.writerIndex(); + c.writeShort(TYPE); + c.writeShort(LENGTH); + c.writeInt(areaID); + return c.writerIndex() - iLenStartIndex; + } + + /** + * Reads the channel buffer and returns object of AreaIDTlv. + * + * @param cb ChannelBuffer + * @return object of AreaIDTlv + */ + public static AreaIDTlv read(ChannelBuffer cb) { + return AreaIDTlv.of(cb.readInt()); + } + + @Override + public short getType() { + return TYPE; + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(getClass()) + .add("Type", TYPE) + .add("Length", LENGTH) + .add("Value", areaID) + .toString(); + } +}
\ No newline at end of file diff --git a/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/AutonomousSystemTlv.java b/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/AutonomousSystemTlv.java new file mode 100644 index 00000000..5d8a9193 --- /dev/null +++ b/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/AutonomousSystemTlv.java @@ -0,0 +1,126 @@ +/* + * 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.bgpio.types; + +import java.util.Objects; + +import org.jboss.netty.buffer.ChannelBuffer; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.base.MoreObjects; + +/** + * Provides Autonomous System Tlv which contains opaque value (32 Bit AS Number). + */ +public class AutonomousSystemTlv implements BGPValueType { + + /* Reference :draft-ietf-idr-ls-distribution-11 + * 0 1 2 3 + 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | Type= 512 | Length=4 | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | opaque value (32 Bit AS Number) | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + */ + + protected static final Logger log = LoggerFactory.getLogger(AutonomousSystemTlv.class); + + public static final short TYPE = 512; + public static final short LENGTH = 4; + + private final int asNum; + + /** + * Constructor to initialize asNum. + * + * @param asNum 32 Bit AS Number + */ + public AutonomousSystemTlv(int asNum) { + this.asNum = asNum; + } + + /** + * Returns object of this class with specified asNum. + * + * @param asNum 32 Bit AS Number + * @return object of AutonomousSystemTlv + */ + public static AutonomousSystemTlv of(final int asNum) { + return new AutonomousSystemTlv(asNum); + } + + /** + * Returns opaque value of AS Number. + * + * @return opaque value of AS Number + */ + public int getAsNum() { + return asNum; + } + + @Override + public int hashCode() { + return Objects.hash(asNum); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + + if (obj instanceof AutonomousSystemTlv) { + AutonomousSystemTlv other = (AutonomousSystemTlv) obj; + return Objects.equals(asNum, other.asNum); + } + return false; + } + + @Override + public int write(ChannelBuffer c) { + int iLenStartIndex = c.writerIndex(); + c.writeShort(TYPE); + c.writeShort(LENGTH); + c.writeInt(asNum); + return c.writerIndex() - iLenStartIndex; + } + + /** + * Reads the channel buffer and returns object of AutonomousSystemTlv. + * + * @param c ChannelBuffer + * @return object of AutonomousSystemTlv + */ + public static AutonomousSystemTlv read(ChannelBuffer c) { + return AutonomousSystemTlv.of(c.readInt()); + } + + @Override + public short getType() { + return TYPE; + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(getClass()) + .add("Type", TYPE) + .add("Length", LENGTH) + .add("asNum", asNum) + .toString(); + } +}
\ No newline at end of file diff --git a/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BGPErrorType.java b/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BGPErrorType.java new file mode 100644 index 00000000..f643ae00 --- /dev/null +++ b/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BGPErrorType.java @@ -0,0 +1,57 @@ +/* + * 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.bgpio.types; + +/** + * BgpErrorType class defines all errorCodes and error Subcodes required for Notification message. + */ +public final class BGPErrorType { + private BGPErrorType() { + } + + //Error Codes + public static final byte MESSAGE_HEADER_ERROR = 1; + public static final byte OPEN_MESSAGE_ERROR = 2; + public static final byte UPDATE_MESSAGE_ERROR = 3; + public static final byte HOLD_TIMER_EXPIRED = 4; + public static final byte FINITE_STATE_MACHINE_ERROR = 4; + public static final byte CEASE = 5; + + //Message Header Error subcodes + public static final byte CONNECTION_NOT_SYNCHRONIZED = 1; + public static final byte BAD_MESSAGE_LENGTH = 2; + public static final byte BAD_MESSAGE_TYPE = 3; + + //OPEN Message Error subcodes + public static final byte UNSUPPORTED_VERSION_NUMBER = 1; + public static final byte BAD_PEER_AS = 2; + public static final byte BAD_BGP_IDENTIFIER = 3; + public static final byte UNSUPPORTED_OPTIONAL_PARAMETER = 4; + public static final byte UNACCEPTABLE_HOLD_TIME = 5; + + //UPDATE Message Error subcodes + public static final byte MALFORMED_ATTRIBUTE_LIST = 1; + public static final byte UNRECOGNIZED_WELLKNOWN_ATTRIBUTE = 2; + public static final byte MISSING_WELLKNOWN_ATTRIBUTE = 3; + public static final byte ATTRIBUTE_FLAGS_ERROR = 4; + public static final byte ATTRIBUTE_LENGTH_ERROR = 5; + public static final byte INVALID_ORIGIN_ATTRIBUTE = 6; + public static final byte INVALID_NEXTHOP_ATTRIBUTE = 8; + public static final byte OPTIONAL_ATTRIBUTE_ERROR = 9; + public static final byte INVALID_NETWORK_FIELD = 10; + public static final byte MALFORMED_ASPATH = 11; +}
\ No newline at end of file diff --git a/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BGPHeader.java b/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BGPHeader.java new file mode 100755 index 00000000..6acda0d6 --- /dev/null +++ b/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BGPHeader.java @@ -0,0 +1,161 @@ +/* + * 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.bgpio.types; + +import org.jboss.netty.buffer.ChannelBuffer; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Provides BGP Message Header which is common for all the Messages. + */ + +public class BGPHeader { + + /* 0 1 2 3 + 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | | + + + + | | + + + + | Marker | + + + + | | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | Length | Type | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + */ + + protected static final Logger log = LoggerFactory.getLogger(BGPHeader.class); + + public static final int MARKER_LENGTH = 16; + public static final short DEFAULT_HEADER_LENGTH = 19; + + private byte[] marker; + private byte type; + private short length; + + /** + * Reset fields. + */ + public BGPHeader() { + this.marker = null; + this.length = 0; + this.type = 0; + } + + /** + * Constructors to initialize parameters. + * + * @param marker field in BGP header + * @param length message length + * @param type message type + */ + public BGPHeader(byte[] marker, short length, byte type) { + this.marker = marker; + this.length = length; + this.type = type; + } + + /** + * Sets marker field. + * + * @param value marker field + */ + public void setMarker(byte[] value) { + this.marker = value; + } + + /** + * Sets message type. + * + * @param value message type + */ + public void setType(byte value) { + this.type = value; + } + + /** + * Sets message length. + * + * @param value message length + */ + public void setLength(short value) { + this.length = value; + } + + /** + * Returns message length. + * + * @return message length + */ + public short getLength() { + return this.length; + } + + /** + * Returns message marker. + * + * @return message marker + */ + public byte[] getMarker() { + return this.marker; + } + + /** + * Returns message type. + * + * @return message type + */ + public byte getType() { + return this.type; + } + + /** + * Writes Byte stream of BGP header to channel buffer. + * + * @param cb ChannelBuffer + * @return length index of message header + */ + public int write(ChannelBuffer cb) { + + cb.writeBytes(getMarker(), 0, MARKER_LENGTH); + + int headerLenIndex = cb.writerIndex(); + cb.writeShort((short) 0); + cb.writeByte(type); + + return headerLenIndex; + } + + /** + * Read from channel buffer and Returns BGP header. + * + * @param cb ChannelBuffer + * @return object of BGPHeader + */ + public static BGPHeader read(ChannelBuffer cb) { + + byte[] marker = new byte[MARKER_LENGTH]; + byte type; + short length; + cb.readBytes(marker, 0, MARKER_LENGTH); + length = cb.readShort(); + type = cb.readByte(); + return new BGPHeader(marker, length, type); + } +}
\ No newline at end of file diff --git a/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BGPLSIdentifierTlv.java b/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BGPLSIdentifierTlv.java new file mode 100644 index 00000000..f723d2ca --- /dev/null +++ b/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BGPLSIdentifierTlv.java @@ -0,0 +1,127 @@ +/* + * 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.bgpio.types; + +import java.util.Objects; + +import org.jboss.netty.buffer.ChannelBuffer; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.base.MoreObjects; + +/** + * Provides BGPLSIdentifier Tlv which contains opaque value (32 Bit BGPLS-Identifier). + */ +public class BGPLSIdentifierTlv implements BGPValueType { + + /* Reference :draft-ietf-idr-ls-distribution-11 + * 0 1 2 3 + 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | Type= 513 | Length=4 | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | opaque value (32 Bit BGPLS-Identifier) | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + */ + + protected static final Logger log = LoggerFactory.getLogger(BGPLSIdentifierTlv.class); + + public static final short TYPE = 513; + public static final short LENGTH = 4; + + private final int bgpLSIdentifier; + + /** + * Constructor to initialize bgpLSIdentifier. + * + * @param bgpLSIdentifier BgpLS-Identifier + */ + public BGPLSIdentifierTlv(int bgpLSIdentifier) { + this.bgpLSIdentifier = bgpLSIdentifier; + } + + /** + * Returns object of this class with specified rbgpLSIdentifier. + * + * @param bgpLSIdentifier BgpLS-Identifier + * @return BgpLS-Identifier + */ + public static BGPLSIdentifierTlv of(final int bgpLSIdentifier) { + return new BGPLSIdentifierTlv(bgpLSIdentifier); + } + + /** + * Returns opaque value of BgpLS-Identifier. + * + * @return opaque value of BgpLS-Identifier + */ + public int getBgpLSIdentifier() { + return bgpLSIdentifier; + } + + @Override + public int hashCode() { + return Objects.hash(bgpLSIdentifier); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + + if (obj instanceof BGPLSIdentifierTlv) { + BGPLSIdentifierTlv other = (BGPLSIdentifierTlv) obj; + return Objects.equals(bgpLSIdentifier, other.bgpLSIdentifier); + } + return false; + } + + @Override + public int write(ChannelBuffer c) { + int iLenStartIndex = c.writerIndex(); + c.writeShort(TYPE); + c.writeShort(LENGTH); + c.writeInt(bgpLSIdentifier); + return c.writerIndex() - iLenStartIndex; + } + + /** + * Reads the channel buffer and parses BGPLS Identifier TLV. + * + * @param cb ChannelBuffer + * @return object of BGPLSIdentifierTlv + */ + public static BGPLSIdentifierTlv read(ChannelBuffer cb) { + return BGPLSIdentifierTlv.of(cb.readInt()); + } + + @Override + public short getType() { + return TYPE; + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(getClass()) + .add("Type", TYPE) + .add("Length", LENGTH) + .add("Value", bgpLSIdentifier) + .toString(); + } +}
\ No newline at end of file diff --git a/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BGPValueType.java b/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BGPValueType.java new file mode 100755 index 00000000..54a8b43c --- /dev/null +++ b/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BGPValueType.java @@ -0,0 +1,39 @@ +/* + * 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.bgpio.types; + +import org.jboss.netty.buffer.ChannelBuffer; + +/** + * Abstraction which Provides the BGP of TLV format. + */ +public interface BGPValueType { + /** + * Returns the Type of BGP Message. + * + * @return short value of type + */ + short getType(); + + /** + * Writes the byte Stream of BGP Message to channel buffer. + * + * @param cb channel buffer + * @return length written to channel buffer + */ + int write(ChannelBuffer cb); +}
\ No newline at end of file diff --git a/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/IPReachabilityInformationTlv.java b/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/IPReachabilityInformationTlv.java new file mode 100644 index 00000000..85fb748b --- /dev/null +++ b/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/IPReachabilityInformationTlv.java @@ -0,0 +1,156 @@ +/* + * 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.bgpio.types; + +import java.util.Objects; + +import org.jboss.netty.buffer.ChannelBuffer; +import org.onlab.packet.IpPrefix; +import org.onosproject.bgpio.util.Validation; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.base.MoreObjects; + +/** + * Provides IP Reachability InformationTlv Tlv which contains IP Prefix. + */ +public class IPReachabilityInformationTlv implements BGPValueType { + + /* + * Reference :draft-ietf-idr-ls-distribution-11 + + 0 1 2 3 + 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | Type | Length | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | Prefix Length | IP Prefix (variable) // + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + + Figure 14: IP Reachability Information TLV Format + */ + + protected static final Logger log = LoggerFactory.getLogger(IPReachabilityInformationTlv.class); + + public static final short TYPE = 265; + public static final int ONE_BYTE_LEN = 8; + private byte prefixLen; + private byte[] ipPrefix; + public short length; + + /** + * Constructor to initialize parameters. + * + * @param prefixLen length of IP Prefix + * @param ipPrefix IP Prefix + * @param length length of value field + */ + public IPReachabilityInformationTlv(byte prefixLen, byte[] ipPrefix, short length) { + this.ipPrefix = ipPrefix; + this.prefixLen = prefixLen; + this.length = length; + } + + /** + * Returns IP Prefix. + * + * @return IP Prefix + */ + public IpPrefix getPrefixValue() { + IpPrefix prefix = Validation.bytesToPrefix(ipPrefix, prefixLen); + return prefix; + } + + /** + * Returns IP Prefix length. + * + * @return IP Prefix length + */ + public byte getPrefixLen() { + return this.prefixLen; + } + + @Override + public int hashCode() { + return Objects.hash(ipPrefix, prefixLen); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + + if (obj instanceof IPReachabilityInformationTlv) { + IPReachabilityInformationTlv other = (IPReachabilityInformationTlv) obj; + return Objects.equals(prefixLen, other.prefixLen) && Objects.equals(ipPrefix, other.ipPrefix); + } + return false; + } + + @Override + public int write(ChannelBuffer cb) { + int iLenStartIndex = cb.writerIndex(); + cb.writeShort(TYPE); + cb.writeShort(length); + cb.writeByte(prefixLen); + cb.writeBytes(ipPrefix); + return cb.writerIndex() - iLenStartIndex; + } + + /** + * Reads the channel buffer and returns object of IPReachabilityInformationTlv. + * + * @param cb ChannelBuffer + * @param length of value field + * @return object of IPReachabilityInformationTlv + */ + public static IPReachabilityInformationTlv read(ChannelBuffer cb, short length) { + byte preficLen = cb.readByte(); + byte[] prefix; + if (preficLen == 0) { + prefix = new byte[] {0}; + } else { + int len = preficLen / ONE_BYTE_LEN; + int reminder = preficLen % ONE_BYTE_LEN; + if (reminder > 0) { + len = len + 1; + } + prefix = new byte[len]; + cb.readBytes(prefix, 0, len); + } + return IPReachabilityInformationTlv.of(preficLen, prefix, length); + } + + public static IPReachabilityInformationTlv of(final byte preficLen, final byte[] prefix, final short length) { + return new IPReachabilityInformationTlv(preficLen, prefix, length); + } + @Override + public short getType() { + return TYPE; + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(getClass()) + .add("Type", TYPE) + .add("Length", length) + .add("Prefixlength", getPrefixLen()) + .add("Prefixvalue", getPrefixValue()) + .toString(); + } +}
\ No newline at end of file diff --git a/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/IsIsNonPseudonode.java b/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/IsIsNonPseudonode.java new file mode 100644 index 00000000..d5f3e7f3 --- /dev/null +++ b/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/IsIsNonPseudonode.java @@ -0,0 +1,117 @@ +/* + * 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.bgpio.types; + +import java.util.Objects; + +import org.jboss.netty.buffer.ChannelBuffer; +import org.onosproject.bgpio.protocol.IGPRouterID; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.base.MoreObjects; + +/** + * Provides Implementation of IsIsNonPseudonode Tlv. + */ +public class IsIsNonPseudonode implements IGPRouterID, BGPValueType { + protected static final Logger log = LoggerFactory.getLogger(IsIsNonPseudonode.class); + + public static final short TYPE = 515; + public static final short LENGTH = 6; + + private final byte[] isoNodeID; + + /** + * Constructor to initialize isoNodeID. + * + * @param isoNodeID ISO system-ID + */ + public IsIsNonPseudonode(byte[] isoNodeID) { + this.isoNodeID = isoNodeID; + } + + /** + * Returns object of this class with specified isoNodeID. + * + * @param isoNodeID ISO system-ID + * @return object of IsIsNonPseudonode + */ + public static IsIsNonPseudonode of(final byte[] isoNodeID) { + return new IsIsNonPseudonode(isoNodeID); + } + + /** + * Returns ISO NodeID. + * + * @return ISO NodeID + */ + public byte[] getISONodeID() { + return isoNodeID; + } + + @Override + public int hashCode() { + return Objects.hash(isoNodeID); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj instanceof IsIsNonPseudonode) { + IsIsNonPseudonode other = (IsIsNonPseudonode) obj; + return Objects.equals(isoNodeID, other.isoNodeID); + } + return false; + } + + @Override + public int write(ChannelBuffer c) { + int iLenStartIndex = c.writerIndex(); + c.writeShort(TYPE); + c.writeShort(LENGTH); + c.writeBytes(isoNodeID); + return c.writerIndex() - iLenStartIndex; + } + + /** + * Reads the channel buffer and returns object of IsIsNonPseudonode. + * + * @param cb ChannelBuffer + * @return object of IsIsNonPseudonode + */ + public static IsIsNonPseudonode read(ChannelBuffer cb) { + byte[] isoNodeID = new byte[LENGTH]; + cb.readBytes(isoNodeID, 0, LENGTH); + return IsIsNonPseudonode.of(isoNodeID); + } + + @Override + public short getType() { + return TYPE; + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(getClass()) + .add("Type", TYPE) + .add("Length", LENGTH) + .add("ISONodeID", isoNodeID) + .toString(); + } +}
\ No newline at end of file diff --git a/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/IsIsPseudonode.java b/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/IsIsPseudonode.java new file mode 100644 index 00000000..5c742d0f --- /dev/null +++ b/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/IsIsPseudonode.java @@ -0,0 +1,133 @@ +/* + * 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.bgpio.types; + +import java.util.Objects; + +import org.jboss.netty.buffer.ChannelBuffer; +import org.onosproject.bgpio.protocol.IGPRouterID; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.base.MoreObjects; + +/** + * Provides implementation of IsIsPseudonode Tlv. + */ +public class IsIsPseudonode implements IGPRouterID, BGPValueType { + protected static final Logger log = LoggerFactory.getLogger(IsIsPseudonode.class); + + public static final short TYPE = 515; + public static final short LENGTH = 7; + + private final byte[] isoNodeID; + private byte psnIdentifier; + + /** + * Constructor to initialize isoNodeID. + * + * @param isoNodeID ISO system-ID + * @param psnIdentifier PSN identifier + */ + public IsIsPseudonode(byte[] isoNodeID, byte psnIdentifier) { + this.isoNodeID = isoNodeID; + this.psnIdentifier = psnIdentifier; + } + + /** + * Returns object of this class with specified values. + * + * @param isoNodeID ISO system-ID + * @param psnIdentifier PSN identifier + * @return object of IsIsPseudonode + */ + public static IsIsPseudonode of(final byte[] isoNodeID, final byte psnIdentifier) { + return new IsIsPseudonode(isoNodeID, psnIdentifier); + } + + /** + * Returns ISO NodeID. + * + * @return ISO NodeID + */ + public byte[] getISONodeID() { + return isoNodeID; + } + + /** + * Returns PSN Identifier. + * + * @return PSN Identifier + */ + public byte getPSNIdentifier() { + return this.psnIdentifier; + } + + @Override + public int hashCode() { + return Objects.hash(isoNodeID, psnIdentifier); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj instanceof IsIsPseudonode) { + IsIsPseudonode other = (IsIsPseudonode) obj; + return Objects.equals(isoNodeID, other.isoNodeID) && Objects.equals(psnIdentifier, other.psnIdentifier); + } + return false; + } + + @Override + public int write(ChannelBuffer c) { + int iLenStartIndex = c.writerIndex(); + c.writeShort(TYPE); + c.writeShort(LENGTH); + c.writeBytes(isoNodeID); + c.writeByte(psnIdentifier); + return c.writerIndex() - iLenStartIndex; + } + + /** + * Reads the channel buffer and returns object of IsIsPseudonode. + * + * @param cb ChannelBuffer + * @return object of IsIsPseudonode + */ + public static IsIsPseudonode read(ChannelBuffer cb) { + byte[] isoNodeID = new byte[LENGTH - 1]; + cb.readBytes(isoNodeID, 0, LENGTH - 1); + byte psnIdentifier = cb.readByte(); + return IsIsPseudonode.of(isoNodeID, psnIdentifier); + } + + @Override + public short getType() { + return TYPE; + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(getClass()) + .add("Type", TYPE) + .add("Length", LENGTH) + .add("isoNodeID", isoNodeID) + .add("psnIdentifier", psnIdentifier) + .toString(); + } +}
\ No newline at end of file diff --git a/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/OSPFNonPseudonode.java b/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/OSPFNonPseudonode.java new file mode 100644 index 00000000..6d8282b7 --- /dev/null +++ b/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/OSPFNonPseudonode.java @@ -0,0 +1,118 @@ +/* + * 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.bgpio.types; + +import java.util.Objects; + +import org.jboss.netty.buffer.ChannelBuffer; +import org.onosproject.bgpio.protocol.IGPRouterID; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.base.MoreObjects; + +/** + * Provides implementation of OSPFNonPseudonode Tlv. + */ +public class OSPFNonPseudonode implements IGPRouterID, BGPValueType { + + protected static final Logger log = LoggerFactory.getLogger(OSPFNonPseudonode.class); + + public static final short TYPE = 515; + public static final short LENGTH = 4; + + private final int routerID; + + /** + * Constructor to initialize routerID. + * + * @param routerID routerID + */ + public OSPFNonPseudonode(int routerID) { + this.routerID = routerID; + } + + /** + * Returns object of this class with specified routerID. + * + * @param routerID routerID + * @return object of OSPFNonPseudonode + */ + public static OSPFNonPseudonode of(final int routerID) { + return new OSPFNonPseudonode(routerID); + } + + /** + * Returns RouterID. + * + * @return RouterID + */ + public int getrouterID() { + return this.routerID; + } + + @Override + public int hashCode() { + return Objects.hash(routerID); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + + if (obj instanceof OSPFNonPseudonode) { + OSPFNonPseudonode other = (OSPFNonPseudonode) obj; + return Objects.equals(routerID, other.routerID); + } + return false; + } + + @Override + public int write(ChannelBuffer c) { + int iLenStartIndex = c.writerIndex(); + c.writeShort(TYPE); + c.writeShort(LENGTH); + c.writeInt(routerID); + return c.writerIndex() - iLenStartIndex; + } + + /** + * Reads the channel buffer and returns object of OSPFNonPseudonode. + * + * @param cb ChannelBuffer + * @return object of OSPFNonPseudonode + */ + public static OSPFNonPseudonode read(ChannelBuffer cb) { + return OSPFNonPseudonode.of(cb.readInt()); + } + + @Override + public short getType() { + return TYPE; + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(getClass()) + .add("Type", TYPE) + .add("Length", LENGTH) + .add("RouterID", routerID) + .toString(); + } +}
\ No newline at end of file diff --git a/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/OSPFPseudonode.java b/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/OSPFPseudonode.java new file mode 100644 index 00000000..82a39bd1 --- /dev/null +++ b/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/OSPFPseudonode.java @@ -0,0 +1,125 @@ +/* + * 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.bgpio.types; + +import java.util.Objects; + +import org.jboss.netty.buffer.ChannelBuffer; +import org.onlab.packet.Ip4Address; +import org.onosproject.bgpio.protocol.IGPRouterID; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.base.MoreObjects; + +/** + * Provides implementation of OSPFPseudonode Tlv. + */ +public class OSPFPseudonode implements IGPRouterID, BGPValueType { + + protected static final Logger log = LoggerFactory.getLogger(OSPFPseudonode.class); + + public static final short TYPE = 515; + public static final short LENGTH = 8; + + private final int routerID; + private final Ip4Address drInterface; + + /** + * Constructor to initialize parameters. + * + * @param routerID routerID + * @param drInterface IPv4 address of the DR's interface + */ + public OSPFPseudonode(int routerID, Ip4Address drInterface) { + this.routerID = routerID; + this.drInterface = drInterface; + } + + /** + * Returns object of this class with specified values. + * + * @param routerID routerID + * @param drInterface IPv4 address of the DR's interface + * @return object of OSPFPseudonode + */ + public static OSPFPseudonode of(final int routerID, final Ip4Address drInterface) { + return new OSPFPseudonode(routerID, drInterface); + } + + /** + * Returns RouterID. + * + * @return RouterID + */ + public int getrouterID() { + return this.routerID; + } + + @Override + public int hashCode() { + return Objects.hash(routerID, drInterface); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj instanceof OSPFPseudonode) { + OSPFPseudonode other = (OSPFPseudonode) obj; + return Objects.equals(routerID, other.routerID) && Objects.equals(drInterface, other.drInterface); + } + return false; + } + + @Override + public int write(ChannelBuffer c) { + int iLenStartIndex = c.writerIndex(); + c.writeShort(TYPE); + c.writeShort(LENGTH); + c.writeInt(routerID); + c.writeInt(drInterface.toInt()); + return c.writerIndex() - iLenStartIndex; + } + + /** + * Reads the channel buffer and returns object of OSPFPseudonode. + * + * @param cb ChannelBuffer + * @return object of OSPFPseudonode + */ + public static OSPFPseudonode read(ChannelBuffer cb) { + int routerID = cb.readInt(); + Ip4Address drInterface = Ip4Address.valueOf(cb.readInt()); + return OSPFPseudonode.of(routerID, drInterface); + } + + @Override + public short getType() { + return TYPE; + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(getClass()) + .add("Type", TYPE) + .add("Length", LENGTH) + .add("RouterID", routerID) + .add("DRInterface", drInterface) + .toString(); + } +}
\ No newline at end of file diff --git a/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/OSPFRouteTypeTlv.java b/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/OSPFRouteTypeTlv.java new file mode 100644 index 00000000..20fffbfa --- /dev/null +++ b/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/OSPFRouteTypeTlv.java @@ -0,0 +1,163 @@ +/* + * 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.bgpio.types; + +import java.util.Objects; + +import org.jboss.netty.buffer.ChannelBuffer; +import org.onosproject.bgpio.exceptions.BGPParseException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.base.MoreObjects; + +/** + * Provides OSPF Route Type Tlv which contains route type. + */ +public class OSPFRouteTypeTlv implements BGPValueType { + + /* Reference :draft-ietf-idr-ls-distribution-11 + 0 1 2 3 + 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | Type | Length | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | Route Type | + +-+-+-+-+-+-+-+-+ + + Figure : OSPF Route Type TLV Format + */ + + protected static final Logger log = LoggerFactory.getLogger(OSPFRouteTypeTlv.class); + + public static final short TYPE = 264; + public static final short LENGTH = 1; + public static final int INTRA_AREA_TYPE = 1; + public static final short INTER_AREA_TYPE = 2; + public static final short EXTERNAL_TYPE_1 = 3; + public static final short EXTERNAL_TYPE_2 = 4; + public static final short NSSA_TYPE_1 = 5; + public static final short NSSA_TYPE_2 = 6; + private final byte routeType; + + /** + * Enum for Route Type. + */ + public enum ROUTETYPE { + Intra_Area(1), Inter_Area(2), External_1(3), External_2(4), NSSA_1(5), NSSA_2(6); + int value; + ROUTETYPE(int val) { + value = val; + } + public byte getType() { + return (byte) value; + } + } + + /** + * Constructor to initialize routeType. + * + * @param routeType Route type + */ + public OSPFRouteTypeTlv(byte routeType) { + this.routeType = routeType; + } + + /** + * Returns object of this class with specified routeType. + * + * @param routeType Route type + * @return object of OSPFRouteTypeTlv + */ + public static OSPFRouteTypeTlv of(final byte routeType) { + return new OSPFRouteTypeTlv(routeType); + } + + /** + * Returns RouteType. + * + * @return RouteType + * @throws BGPParseException if routeType is not matched + */ + public ROUTETYPE getValue() throws BGPParseException { + switch (routeType) { + case INTRA_AREA_TYPE: + return ROUTETYPE.Intra_Area; + case INTER_AREA_TYPE: + return ROUTETYPE.Inter_Area; + case EXTERNAL_TYPE_1: + return ROUTETYPE.External_1; + case EXTERNAL_TYPE_2: + return ROUTETYPE.External_2; + case NSSA_TYPE_1: + return ROUTETYPE.NSSA_1; + case NSSA_TYPE_2: + return ROUTETYPE.NSSA_2; + default: + throw new BGPParseException(BGPErrorType.UPDATE_MESSAGE_ERROR, (byte) 0, null); + } + } + + @Override + public int hashCode() { + return Objects.hash(routeType); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj instanceof OSPFRouteTypeTlv) { + OSPFRouteTypeTlv other = (OSPFRouteTypeTlv) obj; + return Objects.equals(routeType, other.routeType); + } + return false; + } + + @Override + public int write(ChannelBuffer c) { + int iLenStartIndex = c.writerIndex(); + c.writeShort(TYPE); + c.writeShort(LENGTH); + c.writeByte(routeType); + return c.writerIndex() - iLenStartIndex; + } + + /** + * Reads from ChannelBuffer and parses OSPFRouteTypeTlv. + * + * @param cb channelBuffer + * @return object of OSPFRouteTypeTlv + */ + public static OSPFRouteTypeTlv read(ChannelBuffer cb) { + return OSPFRouteTypeTlv.of(cb.readByte()); + } + + @Override + public short getType() { + return TYPE; + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(getClass()) + .add("Type", TYPE) + .add("Length", LENGTH) + .add("Value", routeType) + .toString(); + } +}
\ No newline at end of file diff --git a/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/attr/BgpAttrNodeFlagBitTlv.java b/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/attr/BgpAttrNodeFlagBitTlv.java new file mode 100755 index 00000000..ba02f6d1 --- /dev/null +++ b/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/attr/BgpAttrNodeFlagBitTlv.java @@ -0,0 +1,177 @@ +/* + * 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.bgpio.types.attr; + +import java.util.Objects; + +import org.jboss.netty.buffer.ChannelBuffer; +import org.onosproject.bgpio.exceptions.BGPParseException; +import org.onosproject.bgpio.types.BGPErrorType; +import org.onosproject.bgpio.types.BGPValueType; +import org.onosproject.bgpio.util.Validation; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.base.MoreObjects; + +/** + * Implements BGP attribute node flag. + */ +public class BgpAttrNodeFlagBitTlv implements BGPValueType { + + protected static final Logger log = LoggerFactory + .getLogger(BgpAttrNodeFlagBitTlv.class); + + public static final int ATTRNODE_FLAGBIT = 1024; + + /* Node flag bit TLV */ + private boolean bOverloadBit; + private boolean bAttachedBit; + private boolean bExternalBit; + private boolean bABRBit; + + public static final int BIT_SET = 1; + public static final int FIRST_BIT = 0x80; + public static final int SECOND_BIT = 0x40; + public static final int THIRD_BIT = 0x20; + public static final int FOURTH_BIT = 0x01; + + /** + * Constructor to initialize parameters. + * + * @param bOverloadBit Overload bit + * @param bAttachedBit Attached bit + * @param bExternalBit External bit + * @param bABRBit ABR Bit + */ + BgpAttrNodeFlagBitTlv(boolean bOverloadBit, boolean bAttachedBit, + boolean bExternalBit, boolean bABRBit) { + this.bOverloadBit = bOverloadBit; + this.bAttachedBit = bAttachedBit; + this.bExternalBit = bExternalBit; + this.bABRBit = bABRBit; + } + + /** + * Reads the Node Flag Bits. + * + * @param cb ChannelBuffer + * @return attribute node flag bit tlv + * @throws BGPParseException while parsing BgpAttrNodeFlagBitTlv + */ + public static BgpAttrNodeFlagBitTlv read(ChannelBuffer cb) + throws BGPParseException { + boolean bOverloadBit = false; + boolean bAttachedBit = false; + boolean bExternalBit = false; + boolean bABRBit = false; + + short lsAttrLength = cb.readShort(); + + if (lsAttrLength != 1) { + Validation.validateLen(BGPErrorType.UPDATE_MESSAGE_ERROR, + BGPErrorType.ATTRIBUTE_LENGTH_ERROR, + lsAttrLength); + } + + byte nodeFlagBits = cb.readByte(); + + bOverloadBit = ((nodeFlagBits & (byte) FIRST_BIT) == FIRST_BIT); + bAttachedBit = ((nodeFlagBits & (byte) SECOND_BIT) == SECOND_BIT); + bExternalBit = ((nodeFlagBits & (byte) THIRD_BIT) == THIRD_BIT); + bABRBit = ((nodeFlagBits & (byte) FOURTH_BIT) == FOURTH_BIT); + + return new BgpAttrNodeFlagBitTlv(bOverloadBit, bAttachedBit, + bExternalBit, bABRBit); + } + + /** + * Returns Overload Bit. + * + * @return Overload Bit + */ + boolean getOverLoadBit() { + return bOverloadBit; + } + + /** + * Returns Attached Bit. + * + * @return Attached Bit + */ + boolean getAttachedBit() { + return bAttachedBit; + } + + /** + * Returns External Bit. + * + * @return External Bit + */ + boolean getExternalBit() { + return bExternalBit; + } + + /** + * Returns ABR Bit. + * + * @return ABR Bit + */ + boolean getABRBit() { + return bABRBit; + } + + @Override + public short getType() { + return ATTRNODE_FLAGBIT; + } + + @Override + public int write(ChannelBuffer cb) { + // TODO will be implementing it later + return 0; + } + + @Override + public int hashCode() { + return Objects.hash(bOverloadBit, bAttachedBit, bExternalBit, bABRBit); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + + if (obj instanceof BgpAttrNodeFlagBitTlv) { + BgpAttrNodeFlagBitTlv other = (BgpAttrNodeFlagBitTlv) obj; + return Objects.equals(bOverloadBit, other.bOverloadBit) + && Objects.equals(bAttachedBit, other.bAttachedBit) + && Objects.equals(bExternalBit, other.bExternalBit) + && Objects.equals(bABRBit, other.bABRBit); + } + return false; + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(getClass()) + .add("bOverloadBit", bOverloadBit) + .add("bAttachedBit", bAttachedBit) + .add("bExternalBit", bExternalBit).add("bABRBit", bABRBit) + .toString(); + } +} diff --git a/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/attr/BgpAttrNodeMultiTopologyId.java b/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/attr/BgpAttrNodeMultiTopologyId.java new file mode 100644 index 00000000..194d6dac --- /dev/null +++ b/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/attr/BgpAttrNodeMultiTopologyId.java @@ -0,0 +1,127 @@ +/* + * 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.bgpio.types.attr; + +import java.util.Objects; + +import org.jboss.netty.buffer.ChannelBuffer; +import org.onosproject.bgpio.exceptions.BGPParseException; +import org.onosproject.bgpio.types.BGPErrorType; +import org.onosproject.bgpio.types.BGPValueType; +import org.onosproject.bgpio.util.Validation; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.base.MoreObjects; + +/** + * BGP Multi-Topology ID of the LS attribute. + */ +public class BgpAttrNodeMultiTopologyId implements BGPValueType { + + private static final Logger log = LoggerFactory + .getLogger(BgpAttrNodeMultiTopologyId.class); + + public static final int ATTRNODE_MULTITOPOLOGY = 263; + + /* Opaque Node Attribute */ + private short[] multiTopologyId; + + /** + * Constructor to initialize the Node attribute multi-topology ID. + * + * @param multiTopologyId multi-topology ID + */ + BgpAttrNodeMultiTopologyId(short[] multiTopologyId) { + this.multiTopologyId = multiTopologyId; + } + + /** + * Reads the Multi-topology ID of Node attribute. + * + * @param cb ChannelBuffer + * @return Constructor of BgpAttrNodeMultiTopologyId + * @throws BGPParseException while parsing BgpAttrNodeMultiTopologyId + */ + public static BgpAttrNodeMultiTopologyId read(ChannelBuffer cb) + throws BGPParseException { + + log.debug("BgpAttrNodeMultiTopologyId"); + short lsAttrLength = cb.readShort(); + int len = lsAttrLength / 2; // Length is 2*n and n is the number of MT-IDs + + if (cb.readableBytes() < lsAttrLength) { + Validation.validateLen(BGPErrorType.UPDATE_MESSAGE_ERROR, + BGPErrorType.ATTRIBUTE_LENGTH_ERROR, + cb.readableBytes()); + } + + short[] multiTopologyId; + multiTopologyId = new short[len]; + for (int i = 0; i < len; i++) { + multiTopologyId[i] = cb.readShort(); + } + + return new BgpAttrNodeMultiTopologyId(multiTopologyId); + } + + /** + * to get the multi-topology ID. + * + * @return multitopology ID + */ + short[] getAttrMultiTopologyId() { + return multiTopologyId; + } + + @Override + public short getType() { + return ATTRNODE_MULTITOPOLOGY; + } + + @Override + public int hashCode() { + return Objects.hash(multiTopologyId); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + + if (obj instanceof BgpAttrNodeMultiTopologyId) { + BgpAttrNodeMultiTopologyId other = (BgpAttrNodeMultiTopologyId) obj; + return Objects.equals(multiTopologyId, other.multiTopologyId); + } + return false; + } + + @Override + public int write(ChannelBuffer cb) { + // TODO Auto-generated method stub + return 0; + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(getClass()) + .omitNullValues() + .add("multiTopologyId", multiTopologyId) + .toString(); + } +}
\ No newline at end of file diff --git a/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/attr/package-info.java b/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/attr/package-info.java new file mode 100755 index 00000000..e2a74dbc --- /dev/null +++ b/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/attr/package-info.java @@ -0,0 +1,20 @@ +/* + * 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. + */ + +/** + * Implementation of BGP Link state attribute Tlvs. + */ +package org.onosproject.bgpio.types.attr; diff --git a/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/package-info.java b/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/package-info.java new file mode 100755 index 00000000..1f2ed95e --- /dev/null +++ b/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/package-info.java @@ -0,0 +1,20 @@ +/* + * 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. + */ + +/** + * Implementation of Tlvs, Attributes and Descriptors. + */ +package org.onosproject.bgpio.types; diff --git a/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/util/UnSupportedAttribute.java b/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/util/UnSupportedAttribute.java new file mode 100644 index 00000000..663b1e9a --- /dev/null +++ b/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/util/UnSupportedAttribute.java @@ -0,0 +1,51 @@ +/* + * 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.bgpio.util; + +import org.jboss.netty.buffer.ChannelBuffer; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Provides methods to handle UnSupportedAttribute. + */ +public final class UnSupportedAttribute { + protected static final Logger log = LoggerFactory.getLogger(UnSupportedAttribute.class); + + private UnSupportedAttribute() { + } + + /** + * Reads channel buffer parses attribute header and skips specified length. + * + * @param cb channelBuffer + */ + public static void read(ChannelBuffer cb) { + Validation parseFlags = Validation.parseAttributeHeader(cb); + cb.skipBytes(parseFlags.getLength()); + } + + /** + * Skip specified bytes in channel buffer. + * + * @param cb channelBuffer + * @param length to be skipped + */ + public static void skipBytes(ChannelBuffer cb, short length) { + cb.skipBytes(length); + } +}
\ No newline at end of file diff --git a/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/util/Validation.java b/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/util/Validation.java new file mode 100644 index 00000000..915aa580 --- /dev/null +++ b/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/util/Validation.java @@ -0,0 +1,189 @@ +/* + * 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.bgpio.util; + +import java.util.Arrays; + +import org.jboss.netty.buffer.ChannelBuffer; +import org.jboss.netty.buffer.ChannelBuffers; +import org.onlab.packet.IpAddress; +import org.onlab.packet.IpPrefix; +import org.onosproject.bgpio.exceptions.BGPParseException; + +import com.google.common.primitives.Ints; + +/** + * Provides methods to parse attribute header, validate length and type. + */ +public class Validation { + public static final byte FIRST_BIT = (byte) 0x80; + public static final byte SECOND_BIT = 0x40; + public static final byte THIRD_BIT = 0x20; + public static final byte FOURTH_BIT = 0x01; + public static final byte IPV4_SIZE = 4; + private boolean firstBit; + private boolean secondBit; + private boolean thirdBit; + private boolean fourthBit; + private int len; + private boolean isShort; + + Validation(boolean firstBit, boolean secondBit, boolean thirdBit, boolean fourthBit, int len, boolean isShort) { + this.firstBit = firstBit; + this.secondBit = secondBit; + this.thirdBit = thirdBit; + this.fourthBit = fourthBit; + this.len = len; + this.isShort = isShort; + } + + /** + * Parses attribute Header. + * + * @param cb ChannelBuffer + * @return object of Validation + */ + public static Validation parseAttributeHeader(ChannelBuffer cb) { + + boolean firstBit; + boolean secondBit; + boolean thirdBit; + boolean fourthBit; + boolean isShort; + byte flags = cb.readByte(); + byte typeCode = cb.readByte(); + byte temp = flags; + //first Bit : Optional (1) or well-known (0) + firstBit = ((temp & FIRST_BIT) == FIRST_BIT); + //second Bit : Transitive (1) or non-Transitive (0) + secondBit = ((temp & SECOND_BIT) == SECOND_BIT); + //third Bit : partial (1) or complete (0) + thirdBit = ((temp & THIRD_BIT) == THIRD_BIT); + //forth Bit(Extended Length bit) : Attribute Length is 1 octects (0) or 2 octects (1) + fourthBit = ((temp & FOURTH_BIT) == FOURTH_BIT); + int len; + if (fourthBit) { + isShort = true; + short length = cb.readShort(); + len = length; + } else { + isShort = false; + byte length = cb.readByte(); + len = length; + } + return new Validation(firstBit, secondBit, thirdBit, fourthBit, len, isShort); + } + + /** + * Throws exception if length is not correct. + * + * @param errorCode Error code + * @param subErrCode Sub Error Code + * @param length erroneous length + * @throws BGPParseException for erroneous length + */ + public static void validateLen(byte errorCode, byte subErrCode, int length) throws BGPParseException { + byte[] errLen = Ints.toByteArray(length); + ChannelBuffer buffer = ChannelBuffers.dynamicBuffer(); + buffer.writeBytes(errLen); + throw new BGPParseException(errorCode, subErrCode, buffer); + } + + /** + * Throws exception if type is not correct. + * + * @param errorCode Error code + * @param subErrCode Sub Error Code + * @param type erroneous type + * @throws BGPParseException for erroneous type + */ + public static void validateType(byte errorCode, byte subErrCode, int type) throws BGPParseException { + byte[] errType = Ints.toByteArray(type); + ChannelBuffer buffer = ChannelBuffers.dynamicBuffer(); + buffer.writeBytes(errType); + throw new BGPParseException(errorCode, subErrCode, buffer); + } + + /** + * Returns first bit in type flags. + * + * @return first bit in type flags + */ + public boolean getFirstBit() { + return this.firstBit; + } + + /** + * Returns second bit in type flags. + * + * @return second bit in type flags + */ + public boolean getSecondBit() { + return this.secondBit; + } + + /** + * Returns third bit in type flags. + * + * @return third bit in type flags + */ + public boolean getThirdBit() { + return this.thirdBit; + } + + /** + * Returns fourth bit in type flags. + * + * @return fourth bit in type flags + */ + public boolean getFourthBit() { + return this.fourthBit; + } + + /** + * Returns attribute length. + * + * @return attribute length + */ + public int getLength() { + return this.len; + } + + /** + * Returns whether attribute length read in short or byte. + * + * @return whether attribute length read in short or byte + */ + public boolean isShort() { + return this.isShort; + } + + /** + * Converts byte array of prefix value to IpPrefix object. + * + * @param value byte array of prefix value + * @param length prefix length in bits + * @return object of IpPrefix + */ + public static IpPrefix bytesToPrefix(byte[] value, int length) { + if (value.length != IPV4_SIZE) { + value = Arrays.copyOf(value, IPV4_SIZE); + } + IpPrefix ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET, value, length); + return ipPrefix; + } +}
\ No newline at end of file diff --git a/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/util/package-info.java b/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/util/package-info.java new file mode 100755 index 00000000..3229d89a --- /dev/null +++ b/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/util/package-info.java @@ -0,0 +1,20 @@ +/* + * 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. + */ + +/** + * Implementation of BGP utility functions. + */ +package org.onosproject.bgpio.util; diff --git a/framework/src/onos/bgp/ctl/pom.xml b/framework/src/onos/bgp/ctl/pom.xml new file mode 100755 index 00000000..5cefd737 --- /dev/null +++ b/framework/src/onos/bgp/ctl/pom.xml @@ -0,0 +1,65 @@ +<!-- + ~ 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. + --> +<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/maven-v4_0_0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.onosproject</groupId> + <artifactId>onos-bgp</artifactId> + <version>1.4.0-SNAPSHOT</version> + <relativePath>../pom.xml</relativePath> + </parent> + + <artifactId>onos-bgp-ctl</artifactId> + <packaging>bundle</packaging> + + <description>ONOS BGP controller subsystem API</description> + + <dependencies> + <dependency> + <groupId>org.onosproject</groupId> + <artifactId>onos-bgp-api</artifactId> + </dependency> + <dependency> + <groupId>io.netty</groupId> + <artifactId>netty</artifactId> + </dependency> + <dependency> + <groupId>org.apache.felix</groupId> + <artifactId>org.apache.felix.scr.annotations</artifactId> + </dependency> + <dependency> + <groupId>org.osgi</groupId> + <artifactId>org.osgi.compendium</artifactId> + </dependency> + <dependency> + <groupId>org.onosproject</groupId> + <artifactId>onlab-misc</artifactId> + </dependency> + </dependencies> + + <build> + <plugins> + <plugin> + <groupId>org.apache.felix</groupId> + <artifactId>maven-scr-plugin</artifactId> + </plugin> + </plugins> + </build> + +</project> diff --git a/framework/src/onos/bgp/ctl/src/main/java/org/onosproject/bgp/controller/impl/BGPChannelHandler.java b/framework/src/onos/bgp/ctl/src/main/java/org/onosproject/bgp/controller/impl/BGPChannelHandler.java new file mode 100755 index 00000000..942d3658 --- /dev/null +++ b/framework/src/onos/bgp/ctl/src/main/java/org/onosproject/bgp/controller/impl/BGPChannelHandler.java @@ -0,0 +1,34 @@ +/* + * 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.bgp.controller.impl; + +import org.jboss.netty.handler.timeout.IdleStateAwareChannelHandler; + +/** + * Channel handler deals with the bgp peer connection and dispatches messages from peer to the appropriate locations. + */ +class BGPChannelHandler extends IdleStateAwareChannelHandler { + + // TODO: implement FSM and session handling mechanism + /** + * Create a new unconnected BGPChannelHandler. + * + * @param bgpCtrlImpl bgp controller implementation object + */ + BGPChannelHandler(BGPControllerImpl bgpCtrlImpl) { + } +}
\ No newline at end of file diff --git a/framework/src/onos/bgp/ctl/src/main/java/org/onosproject/bgp/controller/impl/BGPConfig.java b/framework/src/onos/bgp/ctl/src/main/java/org/onosproject/bgp/controller/impl/BGPConfig.java new file mode 100755 index 00000000..56877a16 --- /dev/null +++ b/framework/src/onos/bgp/ctl/src/main/java/org/onosproject/bgp/controller/impl/BGPConfig.java @@ -0,0 +1,344 @@ +/* + * 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.bgp.controller.impl; + +import java.util.Iterator; +import java.util.Map.Entry; +import java.util.Set; +import java.util.TreeMap; + +import org.onlab.packet.Ip4Address; +import org.onosproject.bgp.controller.BGPCfg; +import org.onosproject.bgp.controller.BGPPeerCfg; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Provides BGP configuration of this BGP speaker. + */ +public class BGPConfig implements BGPCfg { + + protected static final Logger log = LoggerFactory.getLogger(BGPConfig.class); + + private static final short DEFAULT_HOLD_TIMER = 120; + private static final short DEFAULT_CONN_RETRY_TIME = 120; + private static final short DEFAULT_CONN_RETRY_COUNT = 5; + + private State state = State.INIT; + private int localAs; + private int maxSession; + private boolean lsCapability; + private short holdTime; + private boolean largeAs = false; + private int maxConnRetryTime; + private int maxConnRetryCount; + + private Ip4Address routerId = null; + private TreeMap<String, BGPPeerCfg> bgpPeerTree = new TreeMap<>(); + + /** + * Constructor to initialize the values. + */ + public BGPConfig() { + + this.holdTime = DEFAULT_HOLD_TIMER; + this.maxConnRetryTime = DEFAULT_CONN_RETRY_TIME; + this.maxConnRetryCount = DEFAULT_CONN_RETRY_COUNT; + } + + @Override + public State getState() { + return state; + } + + @Override + public void setState(State state) { + this.state = state; + } + + @Override + public int getAsNumber() { + return this.localAs; + } + + @Override + public void setAsNumber(int localAs) { + + State localState = getState(); + this.localAs = localAs; + + /* Set configuration state */ + if (localState == State.IP_CONFIGURED) { + setState(State.IP_AS_CONFIGURED); + } else { + setState(State.AS_CONFIGURED); + } + } + + @Override + public int getMaxSession() { + return this.maxSession; + } + + @Override + public void setMaxSession(int maxSession) { + this.maxSession = maxSession; + } + + @Override + public boolean getLsCapability() { + return this.lsCapability; + } + + @Override + public void setLsCapability(boolean lsCapability) { + this.lsCapability = lsCapability; + } + + @Override + public String getRouterId() { + if (this.routerId != null) { + return this.routerId.toString(); + } else { + return null; + } + } + + @Override + public void setRouterId(String routerId) { + State localState = getState(); + this.routerId = Ip4Address.valueOf(routerId); + + /* Set configuration state */ + if (localState == State.AS_CONFIGURED) { + setState(State.IP_AS_CONFIGURED); + } else { + setState(State.IP_CONFIGURED); + } + } + + @Override + public boolean addPeer(String routerid, int remoteAs) { + return addPeer(routerid, remoteAs, DEFAULT_HOLD_TIMER); + } + + @Override + public boolean addPeer(String routerid, short holdTime) { + return addPeer(routerid, this.getAsNumber(), holdTime); + } + + @Override + public boolean addPeer(String routerid, int remoteAs, short holdTime) { + BGPPeerConfig lspeer = new BGPPeerConfig(); + if (this.bgpPeerTree.get(routerid) == null) { + + lspeer.setPeerRouterId(routerid); + lspeer.setAsNumber(remoteAs); + lspeer.setHoldtime(holdTime); + lspeer.setState(BGPPeerCfg.State.IDLE); + lspeer.setSelfInnitConnection(false); + + if (this.getAsNumber() == remoteAs) { + lspeer.setIsIBgp(true); + } else { + lspeer.setIsIBgp(false); + } + + this.bgpPeerTree.put(routerid, lspeer); + log.debug("added successfully"); + return true; + } else { + log.debug("already exists"); + return false; + } + } + + @Override + public boolean connectPeer(String routerid) { + BGPPeerCfg lspeer = this.bgpPeerTree.get(routerid); + + if (lspeer != null) { + lspeer.setSelfInnitConnection(true); + // TODO: initiate peer connection + return true; + } + + return false; + } + + @Override + public boolean removePeer(String routerid) { + BGPPeerCfg lspeer = this.bgpPeerTree.get(routerid); + + if (lspeer != null) { + + //TODO DISCONNECT PEER + disconnectPeer(routerid); + lspeer.setSelfInnitConnection(false); + lspeer = this.bgpPeerTree.remove(routerid); + log.debug("Deleted : " + routerid + " successfully"); + + return true; + } else { + log.debug("Did not find : " + routerid); + return false; + } + } + + @Override + public boolean disconnectPeer(String routerid) { + BGPPeerCfg lspeer = this.bgpPeerTree.get(routerid); + + if (lspeer != null) { + + //TODO DISCONNECT PEER + lspeer.setState(BGPPeerCfg.State.IDLE); + lspeer.setSelfInnitConnection(false); + log.debug("Disconnected : " + routerid + " successfully"); + + return true; + } else { + log.debug("Did not find : " + routerid); + return false; + } + } + + @Override + public void setPeerConnState(String routerid, BGPPeerCfg.State state) { + BGPPeerCfg lspeer = this.bgpPeerTree.get(routerid); + + if (lspeer != null) { + lspeer.setState(state); + log.debug("Peer : " + routerid + " is not available"); + + return; + } else { + log.debug("Did not find : " + routerid); + return; + } + } + + @Override + public BGPPeerCfg.State getPeerConnState(String routerid) { + BGPPeerCfg lspeer = this.bgpPeerTree.get(routerid); + + if (lspeer != null) { + return lspeer.getState(); + } else { + return BGPPeerCfg.State.INVALID; //No instance + } + } + + @Override + public boolean isPeerConnectable(String routerid) { + BGPPeerCfg lspeer = this.bgpPeerTree.get(routerid); + + if ((lspeer != null) && lspeer.getState().equals(BGPPeerCfg.State.IDLE)) { + return true; + } + + return false; + } + + @Override + public TreeMap<String, BGPPeerCfg> getPeerTree() { + return this.bgpPeerTree; + } + + @Override + public TreeMap<String, BGPPeerCfg> displayPeers() { + if (this.bgpPeerTree.isEmpty()) { + log.debug("There are no BGP peers"); + } else { + Set<Entry<String, BGPPeerCfg>> set = this.bgpPeerTree.entrySet(); + Iterator<Entry<String, BGPPeerCfg>> list = set.iterator(); + BGPPeerCfg lspeer; + + while (list.hasNext()) { + Entry<String, BGPPeerCfg> me = list.next(); + lspeer = me.getValue(); + log.debug("Peer neighbor IP :" + me.getKey()); + log.debug(", AS Number : " + lspeer.getAsNumber()); + log.debug(", Hold Timer : " + lspeer.getHoldtime()); + log.debug(", Is iBGP : " + lspeer.getIsIBgp()); + } + } + return null; + } + + @Override + public BGPPeerCfg displayPeers(String routerid) { + + if (this.bgpPeerTree.isEmpty()) { + log.debug("There are no BGP peers"); + } else { + return this.bgpPeerTree.get(routerid); + } + return null; + } + + @Override + public void setHoldTime(short holdTime) { + this.holdTime = holdTime; + } + + @Override + public short getHoldTime() { + return this.holdTime; + } + + @Override + public boolean getLargeASCapability() { + return this.largeAs; + } + + @Override + public void setLargeASCapability(boolean largeAs) { + this.largeAs = largeAs; + } + + @Override + public boolean isPeerConfigured(String routerid) { + BGPPeerCfg lspeer = this.bgpPeerTree.get(routerid); + return (lspeer != null) ? true : false; + } + + @Override + public boolean isPeerConnected(String routerid) { + // TODO: is peer connected + return true; + } + + @Override + public int getMaxConnRetryCount() { + return this.maxConnRetryCount; + } + + @Override + public void setMaxConnRetryCout(int retryCount) { + this.maxConnRetryCount = retryCount; + } + + @Override + public int getMaxConnRetryTime() { + return this.maxConnRetryTime; + } + + @Override + public void setMaxConnRetryTime(int retryTime) { + this.maxConnRetryTime = retryTime; + } +} diff --git a/framework/src/onos/bgp/ctl/src/main/java/org/onosproject/bgp/controller/impl/BGPControllerImpl.java b/framework/src/onos/bgp/ctl/src/main/java/org/onosproject/bgp/controller/impl/BGPControllerImpl.java new file mode 100755 index 00000000..95eafdbc --- /dev/null +++ b/framework/src/onos/bgp/ctl/src/main/java/org/onosproject/bgp/controller/impl/BGPControllerImpl.java @@ -0,0 +1,104 @@ +/* + * 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.bgp.controller.impl; + +import static org.onlab.util.Tools.groupedThreads; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +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.Service; +import org.onosproject.bgp.controller.BGPCfg; +import org.onosproject.bgp.controller.BGPController; +import org.onosproject.bgp.controller.BGPId; +import org.onosproject.bgpio.protocol.BGPMessage; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@Component(immediate = true) +@Service +public class BGPControllerImpl implements BGPController { + + private static final Logger log = LoggerFactory.getLogger(BGPControllerImpl.class); + + private final ExecutorService executorMsgs = Executors.newFixedThreadPool(32, + groupedThreads("onos/bgp", + "event-stats-%d")); + + private final ExecutorService executorBarrier = Executors.newFixedThreadPool(4, + groupedThreads("onos/bgp", + "event-barrier-%d")); + + final Controller ctrl = new Controller(this); + + private BGPConfig bgpconfig = new BGPConfig(); + + @Activate + public void activate() { + this.ctrl.start(); + log.info("Started"); + } + + @Deactivate + public void deactivate() { + // Close all connected peers + this.ctrl.stop(); + log.info("Stopped"); + } + + @Override + public void writeMsg(BGPId bgpId, BGPMessage msg) { + // TODO: Send message + } + + @Override + public void processBGPPacket(BGPId bgpId, BGPMessage msg) { + + switch (msg.getType()) { + case OPEN: + // TODO: Process Open message + break; + case KEEP_ALIVE: + // TODO: Process keepalive message + break; + case NOTIFICATION: + // TODO: Process notificatoin message + break; + case UPDATE: + // TODO: Process update message + break; + default: + // TODO: Process other message + break; + } + } + + /** + * Get controller instance. + * + * @return ctrl the controller. + */ + public Controller getController() { + return ctrl; + } + + @Override + public BGPCfg getConfig() { + return this.bgpconfig; + } +}
\ No newline at end of file diff --git a/framework/src/onos/bgp/ctl/src/main/java/org/onosproject/bgp/controller/impl/BGPMessageDecoder.java b/framework/src/onos/bgp/ctl/src/main/java/org/onosproject/bgp/controller/impl/BGPMessageDecoder.java new file mode 100755 index 00000000..39f2862d --- /dev/null +++ b/framework/src/onos/bgp/ctl/src/main/java/org/onosproject/bgp/controller/impl/BGPMessageDecoder.java @@ -0,0 +1,53 @@ +/* + * 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.bgp.controller.impl; + +import java.util.LinkedList; +import java.util.List; + +import org.jboss.netty.buffer.ChannelBuffer; +import org.jboss.netty.channel.Channel; +import org.jboss.netty.channel.ChannelHandlerContext; +import org.jboss.netty.handler.codec.frame.FrameDecoder; +import org.onosproject.bgpio.protocol.BGPMessage; +import org.onlab.util.HexDump; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Decode an bgp message from a Channel, for use in a netty pipeline. + */ +public class BGPMessageDecoder extends FrameDecoder { + + protected static final Logger log = LoggerFactory.getLogger(BGPMessageDecoder.class); + + @Override + protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer) throws Exception { + + List<BGPMessage> msgList = new LinkedList<BGPMessage>(); + + log.debug("MESSAGE IS RECEIVED."); + if (!channel.isConnected()) { + log.info("Channel is not connected."); + return null; + } + + HexDump.dump(buffer); + + // TODO: decode bgp messages + return msgList; + } +}
\ No newline at end of file diff --git a/framework/src/onos/bgp/ctl/src/main/java/org/onosproject/bgp/controller/impl/BGPMessageEncoder.java b/framework/src/onos/bgp/ctl/src/main/java/org/onosproject/bgp/controller/impl/BGPMessageEncoder.java new file mode 100755 index 00000000..f0d38c3d --- /dev/null +++ b/framework/src/onos/bgp/ctl/src/main/java/org/onosproject/bgp/controller/impl/BGPMessageEncoder.java @@ -0,0 +1,60 @@ +/* + * 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.bgp.controller.impl; + +import java.util.List; + +import org.jboss.netty.buffer.ChannelBuffer; +import org.jboss.netty.buffer.ChannelBuffers; +import org.jboss.netty.channel.Channel; +import org.jboss.netty.channel.ChannelHandlerContext; +import org.jboss.netty.handler.codec.oneone.OneToOneEncoder; +import org.onosproject.bgpio.protocol.BGPMessage; +import org.onlab.util.HexDump; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Encode an bgp message for output into a ChannelBuffer, for use in a + * netty pipeline. + */ +public class BGPMessageEncoder extends OneToOneEncoder { + protected static final Logger log = LoggerFactory.getLogger(BGPMessageEncoder.class); + + @Override + protected Object encode(ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception { + log.debug("BGPMessageEncoder::encode"); + if (!(msg instanceof List)) { + log.debug("Invalid msg."); + return msg; + } + + @SuppressWarnings("unchecked") + List<BGPMessage> msglist = (List<BGPMessage>) msg; + + ChannelBuffer buf = ChannelBuffers.dynamicBuffer(); + + log.debug("SENDING MESSAGE"); + for (BGPMessage pm : msglist) { + pm.writeTo(buf); + } + + HexDump.dump(buf); + + return buf; + } +}
\ No newline at end of file diff --git a/framework/src/onos/bgp/ctl/src/main/java/org/onosproject/bgp/controller/impl/BGPPacketStatsImpl.java b/framework/src/onos/bgp/ctl/src/main/java/org/onosproject/bgp/controller/impl/BGPPacketStatsImpl.java new file mode 100755 index 00000000..41407dc4 --- /dev/null +++ b/framework/src/onos/bgp/ctl/src/main/java/org/onosproject/bgp/controller/impl/BGPPacketStatsImpl.java @@ -0,0 +1,128 @@ +/* + * 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.bgp.controller.impl; + +import org.onosproject.bgp.controller.BGPPacketStats; + +/** + * A representation of a packet context which allows any provider + * to view a packet in event, but may block the response to the + * event if blocked has been called. This packet context can be used + * to react to the packet in event with a packet out. + */ +public class BGPPacketStatsImpl implements BGPPacketStats { + + private int inPacketCount; + private int outPacketCount; + private int wrongPacketCount; + private long time; + + /** + * Resets parameter. + */ + public BGPPacketStatsImpl() { + this.inPacketCount = 0; + this.outPacketCount = 0; + this.wrongPacketCount = 0; + this.time = 0; + } + + /** + * Get the outgoing packet count number. + * + * @return + * packet count + */ + public int outPacketCount() { + return outPacketCount; + } + + /** + * Get the incoming packet count number. + * + * @return + * packet count + */ + public int inPacketCount() { + return inPacketCount; + } + + /** + * Get the wrong packet count number. + * + * @return + * packet count + */ + public int wrongPacketCount() { + return wrongPacketCount; + } + + /** + * Increments the received packet counter. + */ + public void addInPacket() { + this.inPacketCount++; + } + + /** + * Increments the sent packet counter. + */ + public void addOutPacket() { + this.outPacketCount++; + } + + /** + * Increments the sent packet counter by specified value. + * + * @param value of no of packets sent + */ + public void addOutPacket(int value) { + this.outPacketCount = this.outPacketCount + value; + } + + /** + * Increments the wrong packet counter. + */ + public void addWrongPacket() { + this.wrongPacketCount++; + } + + /** + * Resets wrong packet count. + */ + public void resetWrongPacket() { + this.wrongPacketCount = 0; + } + + /** + * Get the time. + * + * @return + * time + */ + public long getTime() { + return this.time; + } + + /** + * Sets the time. + * + * @param time value to set + */ + public void setTime(long time) { + this.time = time; + } +}
\ No newline at end of file diff --git a/framework/src/onos/bgp/ctl/src/main/java/org/onosproject/bgp/controller/impl/BGPPeerConfig.java b/framework/src/onos/bgp/ctl/src/main/java/org/onosproject/bgp/controller/impl/BGPPeerConfig.java new file mode 100755 index 00000000..51b95a4b --- /dev/null +++ b/framework/src/onos/bgp/ctl/src/main/java/org/onosproject/bgp/controller/impl/BGPPeerConfig.java @@ -0,0 +1,109 @@ +/* + * 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.bgp.controller.impl; + +import org.onlab.packet.Ip4Address; +import org.onosproject.bgp.controller.BGPPeerCfg; + +/** + * BGP Peer configuration information. + */ +public class BGPPeerConfig implements BGPPeerCfg { + private int asNumber; + private short holdTime; + private boolean isIBgp; + private Ip4Address peerId = null; + private State state; + private boolean selfInitiated; + + /** + * Constructor to initialize the values. + */ + BGPPeerConfig() { + state = State.IDLE; + selfInitiated = false; + } + + @Override + public int getAsNumber() { + return this.asNumber; + } + + @Override + public void setAsNumber(int asNumber) { + this.asNumber = asNumber; + } + + @Override + public short getHoldtime() { + return this.holdTime; + } + + @Override + public void setHoldtime(short holdTime) { + this.holdTime = holdTime; + } + + @Override + public boolean getIsIBgp() { + return this.isIBgp; + } + + @Override + public void setIsIBgp(boolean isIBgp) { + this.isIBgp = isIBgp; + } + + @Override + public String getPeerRouterId() { + if (this.peerId != null) { + return this.peerId.toString(); + } else { + return null; + } + } + + @Override + public void setPeerRouterId(String peerId) { + this.peerId = Ip4Address.valueOf(peerId); + } + + @Override + public void setPeerRouterId(String peerId, int asNumber) { + this.peerId = Ip4Address.valueOf(peerId); + this.asNumber = asNumber; + } + + @Override + public State getState() { + return this.state; + } + + @Override + public void setState(State state) { + this.state = state; + } + + @Override + public boolean getSelfInnitConnection() { + return this.selfInitiated; + } + + @Override + public void setSelfInnitConnection(boolean selfInit) { + this.selfInitiated = selfInit; + } +} diff --git a/framework/src/onos/bgp/ctl/src/main/java/org/onosproject/bgp/controller/impl/BGPPipelineFactory.java b/framework/src/onos/bgp/ctl/src/main/java/org/onosproject/bgp/controller/impl/BGPPipelineFactory.java new file mode 100755 index 00000000..b2ca5077 --- /dev/null +++ b/framework/src/onos/bgp/ctl/src/main/java/org/onosproject/bgp/controller/impl/BGPPipelineFactory.java @@ -0,0 +1,67 @@ +/* + * 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.bgp.controller.impl; + +import org.jboss.netty.channel.ChannelPipeline; +import org.jboss.netty.channel.ChannelPipelineFactory; +import org.jboss.netty.channel.Channels; +import org.jboss.netty.handler.timeout.ReadTimeoutHandler; +import org.jboss.netty.util.ExternalResourceReleasable; +import org.jboss.netty.util.HashedWheelTimer; +import org.jboss.netty.util.Timer; + +/** + * Creates a ChannelPipeline for a server-side bgp channel. + */ +public class BGPPipelineFactory + implements ChannelPipelineFactory, ExternalResourceReleasable { + + static final Timer TIMER = new HashedWheelTimer(); + protected ReadTimeoutHandler readTimeoutHandler; + BGPControllerImpl bgpCtrlImpl; + + /** + * Constructor to initialize the values. + * + * @param ctrlImpl parent ctrlImpl + * @param isServBgp if it is a server or not + */ + public BGPPipelineFactory(BGPControllerImpl ctrlImpl, boolean isServBgp) { + super(); + bgpCtrlImpl = ctrlImpl; + /* hold time*/ + readTimeoutHandler = new ReadTimeoutHandler(TIMER, bgpCtrlImpl.getConfig().getHoldTime()); + } + + @Override + public ChannelPipeline getPipeline() throws Exception { + BGPChannelHandler handler = new BGPChannelHandler(bgpCtrlImpl); + + ChannelPipeline pipeline = Channels.pipeline(); + pipeline.addLast("bgpmessagedecoder", new BGPMessageDecoder()); + pipeline.addLast("bgpmessageencoder", new BGPMessageEncoder()); + pipeline.addLast("holdTime", readTimeoutHandler); + pipeline.addLast("PassiveHandler", handler); + + return pipeline; + } + + @Override + public void releaseExternalResources() { + TIMER.stop(); + } +}
\ No newline at end of file diff --git a/framework/src/onos/bgp/ctl/src/main/java/org/onosproject/bgp/controller/impl/Controller.java b/framework/src/onos/bgp/ctl/src/main/java/org/onosproject/bgp/controller/impl/Controller.java new file mode 100755 index 00000000..402e8c94 --- /dev/null +++ b/framework/src/onos/bgp/ctl/src/main/java/org/onosproject/bgp/controller/impl/Controller.java @@ -0,0 +1,183 @@ +/* + * 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.bgp.controller.impl; + +import static org.onlab.util.Tools.groupedThreads; + +import java.lang.management.ManagementFactory; +import java.lang.management.RuntimeMXBean; +import java.net.InetSocketAddress; +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.Executors; + +import org.jboss.netty.bootstrap.ServerBootstrap; +import org.jboss.netty.channel.ChannelPipelineFactory; +import org.jboss.netty.channel.group.ChannelGroup; +import org.jboss.netty.channel.group.DefaultChannelGroup; +import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * The main controller class. Handles all setup and network listeners - Distributed ownership control of bgp peer + * through IControllerRegistryService + */ +public class Controller { + + protected static final Logger log = LoggerFactory.getLogger(Controller.class); + + private ChannelGroup cg; + + // Configuration options + private static final short BGP_PORT_NUM = 179; + private int workerThreads = 16; + + // Start time of the controller + protected long systemStartTime; + + private NioServerSocketChannelFactory serverExecFactory; + + // Perf. related configuration + protected static final int SEND_BUFFER_SIZE = 4 * 1024 * 1024; + + BGPControllerImpl bgpCtrlImpl; + + /** + * Constructor to initialize parameter. + * + * @param bgpCtrlImpl BGP controller Impl instance + */ + public Controller(BGPControllerImpl bgpCtrlImpl) { + this.bgpCtrlImpl = bgpCtrlImpl; + } + + // *************** + // Getters/Setters + // *************** + + /** + * To get system start time. + * + * @return system start time in milliseconds + */ + public long getSystemStartTime() { + return (this.systemStartTime); + } + + // ************** + // Initialization + // ************** + + /** + * Tell controller that we're ready to accept bgp peer connections. + */ + public void run() { + + try { + final ServerBootstrap bootstrap = createServerBootStrap(); + + bootstrap.setOption("reuseAddr", true); + bootstrap.setOption("child.keepAlive", true); + bootstrap.setOption("child.tcpNoDelay", true); + bootstrap.setOption("child.sendBufferSize", Controller.SEND_BUFFER_SIZE); + + ChannelPipelineFactory pfact = new BGPPipelineFactory(bgpCtrlImpl, true); + + bootstrap.setPipelineFactory(pfact); + InetSocketAddress sa = new InetSocketAddress(getBgpPortNum()); + cg = new DefaultChannelGroup(); + cg.add(bootstrap.bind(sa)); + log.info("Listening for Peer connection on {}", sa); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + /** + * Creates server boot strap. + * + * @return ServerBootStrap + */ + private ServerBootstrap createServerBootStrap() { + + if (workerThreads == 0) { + serverExecFactory = new NioServerSocketChannelFactory( + Executors.newCachedThreadPool(groupedThreads("onos/bgp", "boss-%d")), + Executors.newCachedThreadPool(groupedThreads("onos/bgp", "worker-%d"))); + return new ServerBootstrap(serverExecFactory); + } else { + serverExecFactory = new NioServerSocketChannelFactory( + Executors.newCachedThreadPool(groupedThreads("onos/bgp", "boss-%d")), + Executors.newCachedThreadPool(groupedThreads("onos/bgp", "worker-%d")), + workerThreads); + return new ServerBootstrap(serverExecFactory); + } + } + + /** + * Initialize internal data structures. + */ + public void init() { + // These data structures are initialized here because other + // module's startUp() might be called before ours + this.systemStartTime = System.currentTimeMillis(); + } + + // ************** + // Utility methods + // ************** + + public Map<String, Long> getMemory() { + Map<String, Long> m = new HashMap<>(); + Runtime runtime = Runtime.getRuntime(); + m.put("total", runtime.totalMemory()); + m.put("free", runtime.freeMemory()); + return m; + } + + public Long getUptime() { + RuntimeMXBean rb = ManagementFactory.getRuntimeMXBean(); + return rb.getUptime(); + } + + /** + * Starts the BGP controller. + */ + public void start() { + log.info("Started"); + this.init(); + this.run(); + } + + /** + * Stops the BGP controller. + */ + public void stop() { + log.info("Stopped"); + serverExecFactory.shutdown(); + cg.close(); + } + + /** + * Returns port number. + * + * @return port number + */ + public static short getBgpPortNum() { + return BGP_PORT_NUM; + } +}
\ No newline at end of file diff --git a/framework/src/onos/bgp/ctl/src/main/java/org/onosproject/bgp/controller/impl/package-info.java b/framework/src/onos/bgp/ctl/src/main/java/org/onosproject/bgp/controller/impl/package-info.java new file mode 100755 index 00000000..fd4e9506 --- /dev/null +++ b/framework/src/onos/bgp/ctl/src/main/java/org/onosproject/bgp/controller/impl/package-info.java @@ -0,0 +1,20 @@ +/* + * 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. + */ + +/** + * Implementation of the BGP controller IO subsystem. + */ +package org.onosproject.bgp.controller.impl; diff --git a/framework/src/onos/bgp/pom.xml b/framework/src/onos/bgp/pom.xml new file mode 100755 index 00000000..941168ba --- /dev/null +++ b/framework/src/onos/bgp/pom.xml @@ -0,0 +1,60 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + ~ 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. + --> +<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/maven-v4_0_0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.onosproject</groupId> + <artifactId>onos</artifactId> + <version>1.4.0-SNAPSHOT</version> + <relativePath>../pom.xml</relativePath> + </parent> + + <artifactId>onos-bgp</artifactId> + <packaging>pom</packaging> + + <description>ONOS BGP Protocol subsystem</description> + + <modules> + <module>api</module> + <module>ctl</module> + <module>bgpio</module> + </modules> + + <dependencies> + <dependency> + <groupId>org.onosproject</groupId> + <artifactId>onlab-misc</artifactId> + </dependency> + <dependency> + <groupId>org.onosproject</groupId> + <artifactId>onlab-junit</artifactId> + </dependency> + </dependencies> + + <build> + <plugins> + <plugin> + <groupId>org.apache.felix</groupId> + <artifactId>maven-bundle-plugin</artifactId> + </plugin> + </plugins> + </build> + +</project> |