From 6a07d2d622eaa06953f3353e39c080984076e8de Mon Sep 17 00:00:00 2001 From: Ashlee Young Date: Fri, 9 Oct 2015 18:32:44 -0700 Subject: Updated master to commit id 6ee8aa3e67ce89908a8c93aa9445c6f71a18f986 Change-Id: I94b055ee2f298daf71e2ec794fd0f2495bd8081f --- framework/src/onos/bgp/bgpio/pom.xml | 76 ++++ .../bgpio/exceptions/BGPParseException.java | 106 +++++ .../onosproject/bgpio/exceptions/package-info.java | 20 + .../bgpio/protocol/BGPKeepaliveMsg.java | 58 +++ .../org/onosproject/bgpio/protocol/BGPMessage.java | 92 ++++ .../bgpio/protocol/BGPMessageReader.java | 36 ++ .../bgpio/protocol/BGPMessageWriter.java | 36 ++ .../org/onosproject/bgpio/protocol/BGPOpenMsg.java | 151 +++++++ .../org/onosproject/bgpio/protocol/BGPType.java | 45 ++ .../org/onosproject/bgpio/protocol/BGPVersion.java | 45 ++ .../onosproject/bgpio/protocol/IGPRouterID.java | 23 + .../org/onosproject/bgpio/protocol/Writeable.java | 35 ++ .../protocol/link_state/BGPPrefixLSIdentifier.java | 228 ++++++++++ .../bgpio/protocol/link_state/NodeDescriptors.java | 225 ++++++++++ .../bgpio/protocol/link_state/package-info.java | 20 + .../onosproject/bgpio/protocol/package-info.java | 20 + .../bgpio/protocol/ver4/BGPKeepaliveMsgVer4.java | 172 ++++++++ .../bgpio/protocol/ver4/BGPOpenMsgVer4.java | 468 +++++++++++++++++++++ .../bgpio/protocol/ver4/package-info.java | 20 + .../org/onosproject/bgpio/types/AreaIDTlv.java | 126 ++++++ .../bgpio/types/AutonomousSystemTlv.java | 126 ++++++ .../org/onosproject/bgpio/types/BGPErrorType.java | 57 +++ .../org/onosproject/bgpio/types/BGPHeader.java | 161 +++++++ .../bgpio/types/BGPLSIdentifierTlv.java | 127 ++++++ .../org/onosproject/bgpio/types/BGPValueType.java | 39 ++ .../bgpio/types/IPReachabilityInformationTlv.java | 156 +++++++ .../onosproject/bgpio/types/IsIsNonPseudonode.java | 117 ++++++ .../onosproject/bgpio/types/IsIsPseudonode.java | 133 ++++++ .../onosproject/bgpio/types/OSPFNonPseudonode.java | 118 ++++++ .../onosproject/bgpio/types/OSPFPseudonode.java | 125 ++++++ .../onosproject/bgpio/types/OSPFRouteTypeTlv.java | 163 +++++++ .../bgpio/types/attr/BgpAttrNodeFlagBitTlv.java | 177 ++++++++ .../types/attr/BgpAttrNodeMultiTopologyId.java | 127 ++++++ .../onosproject/bgpio/types/attr/package-info.java | 20 + .../org/onosproject/bgpio/types/package-info.java | 20 + .../bgpio/util/UnSupportedAttribute.java | 51 +++ .../org/onosproject/bgpio/util/Validation.java | 189 +++++++++ .../org/onosproject/bgpio/util/package-info.java | 20 + 38 files changed, 3928 insertions(+) create mode 100755 framework/src/onos/bgp/bgpio/pom.xml create mode 100755 framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/exceptions/BGPParseException.java create mode 100755 framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/exceptions/package-info.java create mode 100644 framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BGPKeepaliveMsg.java create mode 100644 framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BGPMessage.java create mode 100755 framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BGPMessageReader.java create mode 100644 framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BGPMessageWriter.java create mode 100644 framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BGPOpenMsg.java create mode 100755 framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BGPType.java create mode 100755 framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BGPVersion.java create mode 100644 framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/IGPRouterID.java create mode 100755 framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/Writeable.java create mode 100644 framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/link_state/BGPPrefixLSIdentifier.java create mode 100644 framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/link_state/NodeDescriptors.java create mode 100755 framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/link_state/package-info.java create mode 100755 framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/package-info.java create mode 100644 framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/BGPKeepaliveMsgVer4.java create mode 100644 framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/BGPOpenMsgVer4.java create mode 100755 framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/package-info.java create mode 100644 framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/AreaIDTlv.java create mode 100644 framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/AutonomousSystemTlv.java create mode 100644 framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BGPErrorType.java create mode 100755 framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BGPHeader.java create mode 100644 framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BGPLSIdentifierTlv.java create mode 100755 framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BGPValueType.java create mode 100644 framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/IPReachabilityInformationTlv.java create mode 100644 framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/IsIsNonPseudonode.java create mode 100644 framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/IsIsPseudonode.java create mode 100644 framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/OSPFNonPseudonode.java create mode 100644 framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/OSPFPseudonode.java create mode 100644 framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/OSPFRouteTypeTlv.java create mode 100755 framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/attr/BgpAttrNodeFlagBitTlv.java create mode 100644 framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/attr/BgpAttrNodeMultiTopologyId.java create mode 100755 framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/attr/package-info.java create mode 100755 framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/package-info.java create mode 100644 framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/util/UnSupportedAttribute.java create mode 100644 framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/util/Validation.java create mode 100755 framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/util/package-info.java (limited to 'framework/src/onos/bgp/bgpio') 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 @@ + + + + 4.0.0 + + + org.onosproject + onos-bgp + 1.4.0-SNAPSHOT + ../pom.xml + + + onos-bgpio + bundle + + ONOS BGPio Protocol subsystem + + + + org.onosproject + onos-api + + + org.onosproject + onlab-osgi + + + + com.fasterxml.jackson.core + jackson-databind + + + com.fasterxml.jackson.core + jackson-annotations + + + + org.osgi + org.osgi.core + + + org.apache.karaf.shell + org.apache.karaf.shell.console + + + org.apache.felix + org.apache.felix.scr.annotations + + + + + + + org.apache.felix + maven-bundle-plugin + + + + + 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 { + + /** + * 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 { + + /** + * 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 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 getCapabilityTlv(); + + /** + * Sets capabilities in Open Message and return its builder. + * + * @param capabilityTlv + * capabilities in open message + * @return builder by setting capabilities + */ + Builder setCapabilityTlv(LinkedList 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 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 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 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 parsePrefixDescriptors(ChannelBuffer cb) throws BGPParseException { + LinkedList 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 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 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 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 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 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 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 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 { + + /* + ::= + 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 { + + @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 { + + @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 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 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 getCapabilityTlv() { + return this.capabilityTlv; + } + + /** + * Reader class for reading BGP open message from channel buffer. + */ + public static class Reader implements BGPMessageReader { + + @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 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 parseCapabilityTlv(ChannelBuffer cb) throws BGPParseException { + + LinkedList 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 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 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 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 { + + @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 capabilityTlv = message.capabilityTlv; + ListIterator 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; -- cgit 1.2.3-korg