diff options
Diffstat (limited to 'framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol')
37 files changed, 4442 insertions, 0 deletions
diff --git a/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BgpFactories.java b/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BgpFactories.java new file mode 100755 index 00000000..eb18c7d1 --- /dev/null +++ b/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BgpFactories.java @@ -0,0 +1,82 @@ +/*
+ * 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.protocol.ver4.BgpFactoryVer4;
+import org.onosproject.bgpio.types.BgpHeader;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Abstraction to provide the version for BGP.
+ */
+public final class BgpFactories {
+
+ protected static final Logger log = LoggerFactory.getLogger(BgpFactories.class);
+
+ private static final GenericReader GENERIC_READER = new GenericReader();
+
+ private BgpFactories() {
+ }
+
+ /**
+ * Returns the instance of BGP Version.
+ *
+ * @param version BGP version
+ * @return BGP version
+ */
+ public static BgpFactory getFactory(BgpVersion version) {
+ switch (version) {
+ case BGP_4:
+ return BgpFactoryVer4.INSTANCE;
+ default:
+ throw new IllegalArgumentException("[BgpFactory:]Unknown version: " + version);
+ }
+ }
+
+ /**
+ * Reader class for reading BGP messages from channel buffer.
+ *
+ */
+ private static class GenericReader implements BgpMessageReader<BgpMessage> {
+
+ @Override
+ public BgpMessage readFrom(ChannelBuffer bb, BgpHeader bgpHeader)
+ throws BgpParseException {
+ BgpFactory factory;
+
+ if (!bb.readable()) {
+ log.error("Empty message received");
+ throw new BgpParseException("Empty message received");
+ }
+ // TODO: Currently only BGP version 4 is supported
+ factory = org.onosproject.bgpio.protocol.ver4.BgpFactoryVer4.INSTANCE;
+ return factory.getReader().readFrom(bb, bgpHeader);
+ }
+ }
+
+ /**
+ * Returns BGP messsage generic reader.
+ *
+ * @return bgp message generic reader
+ */
+ public static BgpMessageReader<BgpMessage> getGenericReader() {
+ return GENERIC_READER;
+ }
+}
diff --git a/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BgpFactory.java b/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BgpFactory.java new file mode 100755 index 00000000..11a85ff8 --- /dev/null +++ b/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BgpFactory.java @@ -0,0 +1,60 @@ +/* + * Copyright 2015 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.onosproject.bgpio.protocol; + +/** + * Abstraction of an message factory providing builder functions to BGP messages + * and objects. + * + */ +public interface BgpFactory { + + /** + * Gets the builder object for a open message. + * + * @return builder object for open message + */ + BgpOpenMsg.Builder openMessageBuilder(); + + /** + * Gets the builder object for a keepalive message. + * + * @return builder object for keepalive message + */ + BgpKeepaliveMsg.Builder keepaliveMessageBuilder(); + + /** + * Gets the builder object for a notification message. + * + * @return builder object for notification message. + */ + BgpNotificationMsg.Builder notificationMessageBuilder(); + + /** + * Gets the BGP message reader. + * + * @return BGP message reader + */ + BgpMessageReader<BgpMessage> getReader(); + + /** + * Returns BGP version. + * + * @return BGP version + */ + BgpVersion getVersion(); +} diff --git a/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BgpKeepaliveMsg.java b/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BgpKeepaliveMsg.java new file mode 100644 index 00000000..0cb776a0 --- /dev/null +++ b/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BgpKeepaliveMsg.java @@ -0,0 +1,48 @@ +/* + * 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 + Builder setHeader(BgpHeader bgpMsgHeader); + } +} diff --git a/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BgpLSNlri.java b/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BgpLSNlri.java new file mode 100644 index 00000000..dd8857a7 --- /dev/null +++ b/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BgpLSNlri.java @@ -0,0 +1,54 @@ +/* + * 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.onosproject.bgpio.exceptions.BgpParseException; +import org.onosproject.bgpio.protocol.linkstate.BgpNodeLSNlriVer4.ProtocolType; +import org.onosproject.bgpio.types.RouteDistinguisher; + +/** + * Abstraction of an entity providing BGP-LS NLRI. + */ +public interface BgpLSNlri { + /** + * Returns NlriType of BGP-LS NLRI. + * + * @return NlriType of BGP-LS NLRI + */ + NlriType getNlriType(); + + /** + * Returns Identifier in Nlri. + * + * @return Identifier in Nlri + */ + long getIdentifier(); + + /** + * Returns Protocol Id in Nlri. + * + * @return Protocol Id in Nlri + * @throws BgpParseException while getting protocol ID + */ + ProtocolType getProtocolId() throws BgpParseException; + + /** + * Returns Route distinguisher in Nlri. + * + * @return Route distinguisher in Nlri + */ + RouteDistinguisher getRouteDistinguisher(); +} diff --git a/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BgpLinkLsNlri.java b/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BgpLinkLsNlri.java new file mode 100644 index 00000000..3924a13b --- /dev/null +++ b/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BgpLinkLsNlri.java @@ -0,0 +1,47 @@ +/* + * 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.List; + +import org.onosproject.bgpio.types.BgpValueType; +import org.onosproject.bgpio.protocol.linkstate.NodeDescriptors; + +/** + * Abstraction of an entity providing BGP-LS Link NLRI. + */ +public interface BgpLinkLsNlri extends BgpLSNlri { + /** + * Returns local node descriptors. + * + * @return local node descriptors + */ + NodeDescriptors localNodeDescriptors(); + + /** + * Returns remote node descriptors. + * + * @return remote node descriptors + */ + NodeDescriptors remoteNodeDescriptors(); + + /** + * Returns link descriptors. + * + * @return link descriptors + */ + List<BgpValueType> linkDescriptors(); +}
\ No newline at end of file diff --git a/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BgpMessage.java b/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BgpMessage.java new file mode 100644 index 00000000..f1e1b96f --- /dev/null +++ b/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BgpMessage.java @@ -0,0 +1,71 @@ +/* + * 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; + + /** + * 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/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BgpMessageReader.java b/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BgpMessageReader.java new file mode 100755 index 00000000..b8318b29 --- /dev/null +++ b/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BgpMessageReader.java @@ -0,0 +1,36 @@ +/* + * Copyright 2015 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onosproject.bgpio.protocol; + +import org.jboss.netty.buffer.ChannelBuffer; +import org.onosproject.bgpio.exceptions.BgpParseException; +import org.onosproject.bgpio.types.BgpHeader; + +/** + * Abstraction of an entity providing BGP Message Reader. + */ +public interface BgpMessageReader<T> { + + /** + * Reads the Objects in the BGP Message and Returns BGP Message. + * + * @param cb Channel Buffer + * @param bgpHeader BGP message header + * @return BGP Message + * @throws BgpParseException while parsing BGP message. + */ + T readFrom(ChannelBuffer cb, BgpHeader bgpHeader) throws BgpParseException; +}
\ No newline at end of file diff --git a/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BgpMessageWriter.java b/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BgpMessageWriter.java new file mode 100644 index 00000000..bb75b5ea --- /dev/null +++ b/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BgpMessageWriter.java @@ -0,0 +1,36 @@ +/* + * Copyright 2015 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.onosproject.bgpio.protocol; + +import org.jboss.netty.buffer.ChannelBuffer; +import org.onosproject.bgpio.exceptions.BgpParseException; + +/** + * Abstraction of an entity providing BGP Message Writer. + */ +public interface BgpMessageWriter<T> { + + /** + * Writes the Objects of the BGP Message into Channel Buffer. + * + * @param cb Channel Buffer + * @param message BGP Message + * @throws BgpParseException + * While writing message + */ + void write(ChannelBuffer cb, T message) throws BgpParseException; +}
\ No newline at end of file diff --git a/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BgpNodeLSNlri.java b/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BgpNodeLSNlri.java new file mode 100644 index 00000000..13ef5cc5 --- /dev/null +++ b/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BgpNodeLSNlri.java @@ -0,0 +1,30 @@ +/* + * 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.onosproject.bgpio.protocol.linkstate.BgpNodeLSIdentifier; + +/** + * Abstraction of an entity providing BGP-LS Node NLRI. + */ +public interface BgpNodeLSNlri extends BgpLSNlri { + /** + * Returns local node descriptors. + * + * @return local node descriptors + */ + BgpNodeLSIdentifier getLocalNodeDescriptors(); +} diff --git a/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BgpNotificationMsg.java b/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BgpNotificationMsg.java new file mode 100644 index 00000000..e3361aa7 --- /dev/null +++ b/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BgpNotificationMsg.java @@ -0,0 +1,77 @@ +/* + * 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.onosproject.bgpio.exceptions.BgpParseException; + +/** + * Abstraction of an entity providing BGP notification message. + */ +public interface BgpNotificationMsg extends BgpMessage { + /** + * Returns errorCode in notification message. + * + * @return errorCode in notification message + */ + byte getErrorCode(); + + /** + * Returns error subCode in notification message. + * + * @return error subCode in notification message + */ + byte getErrorSubCode(); + + /** + * Returns error data in notification message. + * + * @return error data in notification message + */ + byte[] getData(); + + /** + * Builder interface with get and set functions to build notification message. + */ + public interface Builder extends BgpMessage.Builder { + + @Override + BgpNotificationMsg build() throws BgpParseException; + + /** + * Sets errorCode in notification message and return its builder. + * + * @param errorCode in notification message + * @return builder by setting errorCode in notification message + */ + Builder setErrorCode(byte errorCode); + + /** + * Sets error subCode in notification message and return its builder. + * + * @param errorSubCode in notification message + * @return builder by setting error subCode in notification message + */ + Builder setErrorSubCode(byte errorSubCode); + + /** + * Sets error data in notification message and return its builder. + * + * @param data in notification message + * @return builder by setting Data in notification message + */ + Builder setData(byte[] data); + } +}
\ No newline at end of file diff --git a/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BgpOpenMsg.java b/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BgpOpenMsg.java new file mode 100644 index 00000000..94ec2235 --- /dev/null +++ b/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BgpOpenMsg.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.protocol; + +import java.util.LinkedList; + +import org.onosproject.bgpio.exceptions.BgpParseException; +import org.onosproject.bgpio.types.BgpHeader; +import org.onosproject.bgpio.types.BgpValueType; + +/** + * Abstraction of an entity providing BGP Open Message. + */ +public interface BgpOpenMsg extends BgpMessage { + + @Override + BgpHeader getHeader(); + + @Override + BgpVersion getVersion(); + + @Override + BgpType getType(); + + /** + * Returns hold time of Open Message. + * + * @return hold time of Open Message + */ + short getHoldTime(); + + /** + * Returns AS Number of Open Message. + * + * @return AS Number of Open Message + */ + short getAsNumber(); + + /** + * Returns BGP Identifier of Open Message. + * + * @return BGP Identifier of Open Message + */ + int getBgpId(); + + /** + * Returns capabilities of Open Message. + * + * @return capabilities of Open Message + */ + LinkedList<BgpValueType> getCapabilityTlv(); + + /** + * Builder interface with get and set functions to build Open message. + */ + interface Builder extends BgpMessage.Builder { + + @Override + BgpOpenMsg build() throws BgpParseException; + + /** + * 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); + + /** + * 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); + + /** + * 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); + + /** + * Sets capabilities in Open Message and return its builder. + * + * @param capabilityTlv capabilities in open message + * @return builder by setting capabilities + */ + Builder setCapabilityTlv(LinkedList<BgpValueType> capabilityTlv); + + /** + * Sets isLargeAsCapabilityTlvSet and return its builder. + * + * @param isLargeAsCapabilitySet + * boolean value to know whether large AS capability is set or not + * @return builder by setting capabilities + */ + Builder setLargeAsCapabilityTlv(boolean isLargeAsCapabilitySet); + + /** + * Sets isLsCapabilityTlvSet and return its builder. + * + * @param isLsCapabilitySet + * boolean value to know whether LS capability is set or not + * @return builder by setting capabilities + */ + Builder setLsCapabilityTlv(boolean isLsCapabilitySet); + + @Override + Builder setHeader(BgpHeader bgpMsgHeader); + } +} diff --git a/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BgpPrefixLSNlri.java b/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BgpPrefixLSNlri.java new file mode 100644 index 00000000..54bcdba3 --- /dev/null +++ b/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BgpPrefixLSNlri.java @@ -0,0 +1,40 @@ +/* + * 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.List; + +import org.onosproject.bgpio.types.BgpValueType; +import org.onosproject.bgpio.protocol.linkstate.NodeDescriptors; + +/** + * Abstraction of an entity providing BGP-LS Prefix NLRI. + */ +public interface BgpPrefixLSNlri extends BgpLSNlri { + /** + * Returns local node descriptors. + * + * @return local node descriptors + */ + NodeDescriptors getLocalNodeDescriptors(); + + /** + * Returns list of Prefix descriptor. + * + * @return list of Prefix descriptor + */ + List<BgpValueType> getPrefixdescriptor(); +} diff --git a/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BgpType.java b/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BgpType.java new file mode 100755 index 00000000..b90721d5 --- /dev/null +++ b/framework/src/onos/protocols/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/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BgpUpdateMsg.java b/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BgpUpdateMsg.java new file mode 100644 index 00000000..d79a4236 --- /dev/null +++ b/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BgpUpdateMsg.java @@ -0,0 +1,48 @@ +/* + * 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.List; + +import org.onlab.packet.IpPrefix; +import org.onosproject.bgpio.protocol.ver4.BgpPathAttributes; + +/** + * Abstraction of an entity providing BGP Update Message. + */ +public interface BgpUpdateMsg extends BgpMessage { + /** + * Returns path attributes in BGP Update Message. + * + * @return path attributes in BGP Update Message + */ + BgpPathAttributes bgpPathAttributes(); + + /** + * Returns withdrawn Routes. + * + * @return withdrawn Routes + */ + List<IpPrefix> withdrawnRoutes(); + + /** + * Returns NLRI list of prefix. + * + * @return NLRI list of prefix + */ + List<IpPrefix> nlri(); +} diff --git a/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BgpVersion.java b/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BgpVersion.java new file mode 100755 index 00000000..b1f037f4 --- /dev/null +++ b/framework/src/onos/protocols/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/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/IGPRouterID.java b/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/IGPRouterID.java new file mode 100644 index 00000000..0e531d64 --- /dev/null +++ b/framework/src/onos/protocols/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/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/NlriType.java b/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/NlriType.java new file mode 100644 index 00000000..535ba71d --- /dev/null +++ b/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/NlriType.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 BGP-LS NLRI types. + */ +public enum NlriType { + + NODE(1), LINK(2), PREFIX_IPV4(3), PREFIX_IPV6(4); + + int value; + + /** + * Assign value with the value as the LINK-STATE NLRI type. + * + * @param value LINK-STATE NLRI type + */ + NlriType(int value) { + this.value = value; + } + + /** + * Returns value as LINK-STATE NLRI type. + * + * @return value LINK-STATE NLRI type + */ + public byte getType() { + return (byte) value; + } +}
\ No newline at end of file diff --git a/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/Writeable.java b/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/Writeable.java new file mode 100755 index 00000000..1c6f7fc2 --- /dev/null +++ b/framework/src/onos/protocols/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/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/linkstate/BgpLinkLSIdentifier.java b/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/linkstate/BgpLinkLSIdentifier.java new file mode 100644 index 00000000..a4360fdf --- /dev/null +++ b/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/linkstate/BgpLinkLSIdentifier.java @@ -0,0 +1,297 @@ +/* + * Copyright 2015 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onosproject.bgpio.protocol.linkstate; + +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.ListIterator; +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.IPv4AddressTlv; +import org.onosproject.bgpio.types.IPv6AddressTlv; +import org.onosproject.bgpio.types.LinkLocalRemoteIdentifiersTlv; +import org.onosproject.bgpio.types.attr.BgpAttrNodeMultiTopologyId; +import org.onosproject.bgpio.util.Constants; +import org.onosproject.bgpio.util.UnSupportedAttribute; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.base.MoreObjects; +import com.google.common.base.Preconditions; + +/** + * Implementation of local node descriptors, remote node descriptors and link descriptors. + */ +public class BgpLinkLSIdentifier implements Comparable<Object> { + private static final Logger log = LoggerFactory.getLogger(BgpLinkLSIdentifier.class); + public static final short IPV4_INTERFACE_ADDRESS_TYPE = 259; + public static final short IPV4_NEIGHBOR_ADDRESS_TYPE = 260; + public static final short IPV6_INTERFACE_ADDRESS_TYPE = 261; + public static final short IPV6_NEIGHBOR_ADDRESS_TYPE = 262; + + private NodeDescriptors localNodeDescriptors; + private NodeDescriptors remoteNodeDescriptors; + private List<BgpValueType> linkDescriptor; + + /** + * Initialize fields. + */ + public BgpLinkLSIdentifier() { + this.localNodeDescriptors = null; + this.remoteNodeDescriptors = null; + this.linkDescriptor = null; + } + + /** + * Constructors to initialize parameters. + * + * @param localNodeDescriptors local node descriptors + * @param remoteNodeDescriptors remote node descriptors + * @param linkDescriptor link descriptors + */ + public BgpLinkLSIdentifier(NodeDescriptors localNodeDescriptors, NodeDescriptors remoteNodeDescriptors, + LinkedList<BgpValueType> linkDescriptor) { + this.localNodeDescriptors = Preconditions.checkNotNull(localNodeDescriptors); + this.remoteNodeDescriptors = Preconditions.checkNotNull(remoteNodeDescriptors); + this.linkDescriptor = Preconditions.checkNotNull(linkDescriptor); + } + + /** + * Reads channel buffer and parses link identifier. + * + * @param cb ChannelBuffer + * @param protocolId in linkstate nlri + * @return object of BGPLinkLSIdentifier + * @throws BgpParseException while parsing link identifier + */ + public static BgpLinkLSIdentifier parseLinkIdendifier(ChannelBuffer cb, byte protocolId) throws BgpParseException { + //Parse local node descriptor + NodeDescriptors localNodeDescriptors = new NodeDescriptors(); + localNodeDescriptors = parseNodeDescriptors(cb, NodeDescriptors.LOCAL_NODE_DES_TYPE, protocolId); + + //Parse remote node descriptor + NodeDescriptors remoteNodeDescriptors = new NodeDescriptors(); + remoteNodeDescriptors = parseNodeDescriptors(cb, NodeDescriptors.REMOTE_NODE_DES_TYPE, protocolId); + + //Parse link descriptor + LinkedList<BgpValueType> linkDescriptor = new LinkedList<>(); + linkDescriptor = parseLinkDescriptors(cb); + return new BgpLinkLSIdentifier(localNodeDescriptors, remoteNodeDescriptors, linkDescriptor); + } + + /** + * Parses Local/Remote node descriptors. + * + * @param cb ChannelBuffer + * @param desType descriptor type + * @param protocolId protocol identifier + * @return object of NodeDescriptors + * @throws BgpParseException while parsing Local/Remote node descriptors + */ + public static NodeDescriptors parseNodeDescriptors(ChannelBuffer cb, short desType, byte protocolId) + throws BgpParseException { + log.debug("parse Node descriptors"); + ChannelBuffer tempBuf = cb.copy(); + 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() + Constants.TYPE_AND_LEN_AS_SHORT)); + } + NodeDescriptors nodeIdentifier = new NodeDescriptors(); + ChannelBuffer tempCb = cb.readBytes(length); + + if (type == desType) { + nodeIdentifier = NodeDescriptors.read(tempCb, length, desType, protocolId); + } else { + throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.MALFORMED_ATTRIBUTE_LIST, null); + } + return nodeIdentifier; + } + + /** + * Parses link descriptors. + * + * @param cb ChannelBuffer + * @return list of link descriptors + * @throws BgpParseException while parsing link descriptors + */ + public static LinkedList<BgpValueType> parseLinkDescriptors(ChannelBuffer cb) throws BgpParseException { + LinkedList<BgpValueType> linkDescriptor = new LinkedList<>(); + BgpValueType tlv = null; + int count = 0; + + while (cb.readableBytes() > 0) { + ChannelBuffer tempBuf = cb.copy(); + 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() + Constants.TYPE_AND_LEN_AS_SHORT)); + } + ChannelBuffer tempCb = cb.readBytes(length); + switch (type) { + case LinkLocalRemoteIdentifiersTlv.TYPE: + tlv = LinkLocalRemoteIdentifiersTlv.read(tempCb); + break; + case IPV4_INTERFACE_ADDRESS_TYPE: + tlv = IPv4AddressTlv.read(tempCb, IPV4_INTERFACE_ADDRESS_TYPE); + break; + case IPV4_NEIGHBOR_ADDRESS_TYPE: + tlv = IPv4AddressTlv.read(tempCb, IPV4_NEIGHBOR_ADDRESS_TYPE); + break; + case IPV6_INTERFACE_ADDRESS_TYPE: + tlv = IPv6AddressTlv.read(tempCb, IPV6_INTERFACE_ADDRESS_TYPE); + break; + case IPV6_NEIGHBOR_ADDRESS_TYPE: + tlv = IPv6AddressTlv.read(tempCb, IPV6_NEIGHBOR_ADDRESS_TYPE); + break; + case BgpAttrNodeMultiTopologyId.ATTRNODE_MULTITOPOLOGY: + tlv = BgpAttrNodeMultiTopologyId.read(tempCb); + count = count++; + //MultiTopologyId TLV cannot repeat more than once + 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 + + Constants.TYPE_AND_LEN_AS_SHORT)); + } + break; + default: + UnSupportedAttribute.skipBytes(tempCb, length); + } + linkDescriptor.add(tlv); + } + return linkDescriptor; + } + + /** + * Returns local node descriptors. + * + * @return local node descriptors + */ + public NodeDescriptors localNodeDescriptors() { + return this.localNodeDescriptors; + } + + /** + * Returns remote node descriptors. + * + * @return remote node descriptors + */ + public NodeDescriptors remoteNodeDescriptors() { + return this.remoteNodeDescriptors; + } + + /** + * Returns link descriptors. + * + * @return link descriptors + */ + public List<BgpValueType> linkDescriptors() { + return this.linkDescriptor; + } + + @Override + public int hashCode() { + return Objects.hash(linkDescriptor, localNodeDescriptors, remoteNodeDescriptors); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj instanceof BgpLinkLSIdentifier) { + int countObjSubTlv = 0; + int countOtherSubTlv = 0; + boolean isCommonSubTlv = true; + BgpLinkLSIdentifier other = (BgpLinkLSIdentifier) obj; + Iterator<BgpValueType> objListIterator = other.linkDescriptor.iterator(); + countOtherSubTlv = other.linkDescriptor.size(); + countObjSubTlv = linkDescriptor.size(); + if (countObjSubTlv != countOtherSubTlv) { + return false; + } else { + while (objListIterator.hasNext() && isCommonSubTlv) { + BgpValueType subTlv = objListIterator.next(); + if (linkDescriptor.contains(subTlv) && other.linkDescriptor.contains(subTlv)) { + isCommonSubTlv = Objects.equals(linkDescriptor.get(linkDescriptor.indexOf(subTlv)), + other.linkDescriptor.get(other.linkDescriptor.indexOf(subTlv))); + } else { + isCommonSubTlv = false; + } + } + return isCommonSubTlv && Objects.equals(this.localNodeDescriptors, other.localNodeDescriptors) + && Objects.equals(this.remoteNodeDescriptors, other.remoteNodeDescriptors); + } + } + return false; + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(getClass()) + .add("localNodeDescriptors", localNodeDescriptors) + .add("remoteNodeDescriptors", remoteNodeDescriptors) + .add("linkDescriptor", linkDescriptor) + .toString(); + } + + @Override + public int compareTo(Object o) { + if (this.equals(o)) { + return 0; + } + int result = this.localNodeDescriptors.compareTo(((BgpLinkLSIdentifier) o).localNodeDescriptors); + if (result != 0) { + return result; + } else if (this.remoteNodeDescriptors.compareTo(((BgpLinkLSIdentifier) o).remoteNodeDescriptors) != 0) { + return this.remoteNodeDescriptors.compareTo(((BgpLinkLSIdentifier) o).remoteNodeDescriptors); + } else { + int countOtherSubTlv = ((BgpLinkLSIdentifier) o).linkDescriptor.size(); + int countObjSubTlv = linkDescriptor.size(); + if (countOtherSubTlv != countObjSubTlv) { + if (countOtherSubTlv > countObjSubTlv) { + return 1; + } else { + return -1; + } + } + ListIterator<BgpValueType> listIterator = linkDescriptor.listIterator(); + ListIterator<BgpValueType> listIteratorOther = ((BgpLinkLSIdentifier) o).linkDescriptor.listIterator(); + while (listIterator.hasNext()) { + BgpValueType tlv = listIterator.next(); + if (linkDescriptor.contains(tlv) && ((BgpLinkLSIdentifier) o).linkDescriptor.contains(tlv)) { + int res = linkDescriptor.get(linkDescriptor.indexOf(tlv)).compareTo( + ((BgpLinkLSIdentifier) o).linkDescriptor.get(((BgpLinkLSIdentifier) o).linkDescriptor + .indexOf(tlv))); + if (res != 0) { + return res; + } + } else { + return 1; + } + } + } + return 0; + } +} diff --git a/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/linkstate/BgpLinkLsNlriVer4.java b/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/linkstate/BgpLinkLsNlriVer4.java new file mode 100755 index 00000000..01d369e4 --- /dev/null +++ b/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/linkstate/BgpLinkLsNlriVer4.java @@ -0,0 +1,210 @@ +/*
+ * 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.linkstate;
+
+import java.util.List;
+
+import org.jboss.netty.buffer.ChannelBuffer;
+import org.onosproject.bgpio.exceptions.BgpParseException;
+import org.onosproject.bgpio.protocol.BgpLinkLsNlri;
+import org.onosproject.bgpio.protocol.NlriType;
+import org.onosproject.bgpio.protocol.linkstate.BgpNodeLSNlriVer4.ProtocolType;
+import org.onosproject.bgpio.types.BgpErrorType;
+import org.onosproject.bgpio.types.BgpValueType;
+import org.onosproject.bgpio.types.RouteDistinguisher;
+import org.onosproject.bgpio.util.Constants;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.base.MoreObjects;
+
+/**
+ * Implementation of Link LS NLRI.
+ */
+public class BgpLinkLsNlriVer4 implements BgpLinkLsNlri {
+
+ /*
+ * 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
+ +-+-+-+-+-+-+-+-+
+ | Protocol-ID |
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ | Identifier |
+ | (64 bits) |
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ // Local Node Descriptors (variable) //
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ // Remote Node Descriptors (variable) //
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ // Link Descriptors (variable) //
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+
+ Figure : The Link NLRI format
+ */
+ private static final Logger log = LoggerFactory.getLogger(BgpLinkLsNlriVer4.class);
+ public static final int LINK_NLRITYPE = 2;
+
+ private BgpLinkLSIdentifier linkLSIdentifier;
+ private byte protocolId;
+ private long identifier;
+ private RouteDistinguisher routeDistinguisher;
+ private boolean isVpn;
+
+ /**
+ * Initialize fields.
+ */
+ public BgpLinkLsNlriVer4() {
+ this.protocolId = 0;
+ this.identifier = 0;
+ this.linkLSIdentifier = null;
+ this.routeDistinguisher = null;
+ this.isVpn = false;
+ }
+
+ /**
+ * Constructor to initialize parameters for BGP LinkLSNlri.
+ *
+ * @param protocolId protocol Id
+ * @param identifier field in BGP LinkLSNlri
+ * @param linkLSIdentifier link LS identifier
+ * @param routeDistinguisher route distinguisher from message
+ * @param isVpn vpn info availability in message
+ */
+ public BgpLinkLsNlriVer4(byte protocolId, long identifier, BgpLinkLSIdentifier linkLSIdentifier,
+ RouteDistinguisher routeDistinguisher, boolean isVpn) {
+ this.protocolId = protocolId;
+ this.identifier = identifier;
+ this.linkLSIdentifier = linkLSIdentifier;
+ this.routeDistinguisher = routeDistinguisher;
+ this.isVpn = isVpn;
+ }
+
+ /**
+ * Reads from channelBuffer and parses Link LS Nlri.
+ *
+ * @param cb ChannelBuffer
+ * @param afi Address Family Identifier
+ * @param safi Subsequent Address Family Identifier
+ * @return object of this class
+ * @throws BgpParseException while parsing Link LS NLRI
+ */
+ public static BgpLinkLsNlriVer4 read(ChannelBuffer cb, short afi, byte safi) throws BgpParseException {
+ boolean isVpn = false;
+ RouteDistinguisher routeDistinguisher = null;
+ if ((afi == Constants.AFI_VALUE) && (safi == Constants.VPN_SAFI_VALUE)) {
+ routeDistinguisher = new RouteDistinguisher();
+ routeDistinguisher = RouteDistinguisher.read(cb);
+ isVpn = true;
+ } else {
+ isVpn = false;
+ }
+ byte protocolId = cb.readByte();
+ long identifier = cb.readLong();
+
+ BgpLinkLSIdentifier linkLSIdentifier = new BgpLinkLSIdentifier();
+ linkLSIdentifier = BgpLinkLSIdentifier.parseLinkIdendifier(cb, protocolId);
+ return new BgpLinkLsNlriVer4(protocolId, identifier, linkLSIdentifier, routeDistinguisher, isVpn);
+ }
+
+ @Override
+ public NlriType getNlriType() {
+ return NlriType.LINK;
+ }
+
+ @Override
+ public long getIdentifier() {
+ return this.identifier;
+ }
+
+ /**
+ * Set the link LS identifier.
+ *
+ * @param linkLSIdentifier link LS identifier to set
+ */
+ public void setLinkLSIdentifier(BgpLinkLSIdentifier linkLSIdentifier) {
+ this.linkLSIdentifier = linkLSIdentifier;
+ }
+
+ @Override
+ public ProtocolType getProtocolId() throws BgpParseException {
+ switch (protocolId) {
+ case Constants.ISIS_LEVELONE:
+ return ProtocolType.ISIS_LEVEL_ONE;
+ case Constants.ISIS_LEVELTWO:
+ return ProtocolType.ISIS_LEVEL_TWO;
+ case Constants.OSPFV2:
+ return ProtocolType.OSPF_V2;
+ case Constants.DIRECT:
+ return ProtocolType.DIRECT;
+ case Constants.STATIC_CONFIGURATION:
+ return ProtocolType.STATIC_CONFIGURATION;
+ case Constants.OSPFV3:
+ return ProtocolType.OSPF_V3;
+ default:
+ throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR, (byte) 0, null);
+ }
+ }
+
+ @Override
+ public NodeDescriptors localNodeDescriptors() {
+ return this.linkLSIdentifier.localNodeDescriptors();
+ }
+
+ @Override
+ public NodeDescriptors remoteNodeDescriptors() {
+ return this.linkLSIdentifier.remoteNodeDescriptors();
+ }
+
+ /**
+ * Returns whether VPN is present or not.
+ *
+ * @return whether VPN is present or not
+ */
+ public boolean isVpnPresent() {
+ return this.isVpn;
+ }
+
+ @Override
+ public RouteDistinguisher getRouteDistinguisher() {
+ return this.routeDistinguisher;
+ }
+
+ /**
+ * Returns link identifier.
+ *
+ * @return link identifier
+ */
+ public BgpLinkLSIdentifier getLinkIdentifier() {
+ return this.linkLSIdentifier;
+ }
+
+ @Override
+ public List<BgpValueType> linkDescriptors() {
+ return this.linkLSIdentifier.linkDescriptors();
+ }
+
+ @Override
+ public String toString() {
+ return MoreObjects.toStringHelper(getClass())
+ .omitNullValues()
+ .add("protocolId", protocolId)
+ .add("identifier", identifier)
+ .add("RouteDistinguisher ", routeDistinguisher)
+ .add("linkLSIdentifier", linkLSIdentifier)
+ .toString();
+ }
+}
diff --git a/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/linkstate/BgpNodeLSIdentifier.java b/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/linkstate/BgpNodeLSIdentifier.java new file mode 100644 index 00000000..6c2c96d9 --- /dev/null +++ b/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/linkstate/BgpNodeLSIdentifier.java @@ -0,0 +1,122 @@ +/* + * 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.linkstate; + +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.util.Constants; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.base.MoreObjects; + +/** + * Implementation of Node Identifier which includes local node descriptor/remote node descriptors. + */ +public class BgpNodeLSIdentifier implements Comparable<Object> { + + private static final Logger log = LoggerFactory.getLogger(BgpNodeLSIdentifier.class); + private NodeDescriptors nodeDescriptors; + + /** + * Resets fields. + */ + public BgpNodeLSIdentifier() { + this.nodeDescriptors = null; + } + + /** + * Constructor to initialize fields. + * + * @param nodeDescriptors local/remote node descriptor + */ + public BgpNodeLSIdentifier(NodeDescriptors nodeDescriptors) { + this.nodeDescriptors = nodeDescriptors; + } + + /** + * Parse local node descriptors. + * + * @param cb ChannelBuffer + * @param protocolId protocol identifier + * @return object of this BGPNodeLSIdentifier + * @throws BgpParseException while parsing local node descriptors + */ + public static BgpNodeLSIdentifier parseLocalNodeDescriptors(ChannelBuffer cb, byte protocolId) + throws BgpParseException { + log.debug("parse Local node descriptor"); + ChannelBuffer tempBuf = cb.copy(); + 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() + Constants.TYPE_AND_LEN)); + } + NodeDescriptors nodeDescriptors = new NodeDescriptors(); + ChannelBuffer tempCb = cb.readBytes(length); + + if (type == NodeDescriptors.LOCAL_NODE_DES_TYPE) { + nodeDescriptors = NodeDescriptors.read(tempCb, length, type, protocolId); + } else { + throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.MALFORMED_ATTRIBUTE_LIST, null); + } + return new BgpNodeLSIdentifier(nodeDescriptors); + } + + /** + * Returns node descriptors. + * + * @return node descriptors + */ + public NodeDescriptors getNodedescriptors() { + return this.nodeDescriptors; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj instanceof BgpNodeLSIdentifier) { + BgpNodeLSIdentifier other = (BgpNodeLSIdentifier) obj; + return Objects.equals(nodeDescriptors, other.nodeDescriptors); + } + return false; + } + + @Override + public int hashCode() { + return Objects.hash(nodeDescriptors); + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(getClass()) + .add("NodeDescriptors", nodeDescriptors) + .toString(); + } + + @Override + public int compareTo(Object o) { + if (this.equals(o)) { + return 0; + } + return this.nodeDescriptors.compareTo(((BgpNodeLSIdentifier) o).nodeDescriptors); + } +} diff --git a/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/linkstate/BgpNodeLSNlriVer4.java b/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/linkstate/BgpNodeLSNlriVer4.java new file mode 100644 index 00000000..b27096cf --- /dev/null +++ b/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/linkstate/BgpNodeLSNlriVer4.java @@ -0,0 +1,212 @@ +/* + * 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.linkstate; + +import org.jboss.netty.buffer.ChannelBuffer; +import org.onosproject.bgpio.exceptions.BgpParseException; +import org.onosproject.bgpio.protocol.BgpNodeLSNlri; +import org.onosproject.bgpio.protocol.NlriType; +import org.onosproject.bgpio.types.BgpErrorType; +import org.onosproject.bgpio.types.RouteDistinguisher; +import org.onosproject.bgpio.util.Constants; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.base.MoreObjects; + +/** + * Implementation of Node LS NLRI. + */ +public class BgpNodeLSNlriVer4 implements BgpNodeLSNlri { + + /* + *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 + +-+-+-+-+-+-+-+-+ + | Protocol-ID | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | Identifier | + | (64 bits) | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + // Local Node Descriptors (variable) // + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + + Figure : The Node NLRI format + */ + + protected static final Logger log = LoggerFactory.getLogger(BgpNodeLSNlriVer4.class); + + public static final int NODE_NLRITYPE = 1; + public static final int IDENTIFIER_LENGTH = 16; + private long identifier; + private byte protocolId; + private BgpNodeLSIdentifier localNodeDescriptors; + private RouteDistinguisher routeDistinguisher; + private boolean isVpn; + + /** + * Enum to provide PROTOCOLTYPE. + */ + public enum ProtocolType { + ISIS_LEVEL_ONE(1), ISIS_LEVEL_TWO(2), OSPF_V2(3), DIRECT(4), STATIC_CONFIGURATION(5), OSPF_V3(6); + int value; + + /** + * Assign val with the value as the protocol type. + * + * @param val protocol type + */ + ProtocolType(int val) { + value = val; + } + + /** + * Returns value of protocol type. + * + * @return protocol type + */ + public byte getType() { + return (byte) value; + } + } + + /** + * Reset fields. + */ + public BgpNodeLSNlriVer4() { + this.identifier = 0; + this.protocolId = 0; + this.localNodeDescriptors = null; + this.routeDistinguisher = null; + this.isVpn = false; + } + + /** + * Constructors to initialize its parameters. + * + * @param identifier of LinkState Nlri + * @param protocolId of LinkState Nlri + * @param localNodeDescriptors local node descriptors + * @param isVpn true if VPN info is present + * @param routeDistinguisher unique for each VPN + */ + public BgpNodeLSNlriVer4(long identifier, byte protocolId, BgpNodeLSIdentifier localNodeDescriptors, boolean isVpn, + RouteDistinguisher routeDistinguisher) { + this.identifier = identifier; + this.protocolId = protocolId; + this.localNodeDescriptors = localNodeDescriptors; + this.routeDistinguisher = routeDistinguisher; + this.isVpn = isVpn; + } + + /** + * Reads from channelBuffer and parses Node LS Nlri. + * + * @param cb ChannelBuffer + * @param afi Address Family Identifier + * @param safi Subsequent Address Family Identifier + * @return object of this class + * @throws BgpParseException while parsing node descriptors + */ + public static BgpNodeLSNlriVer4 read(ChannelBuffer cb, short afi, byte safi) throws BgpParseException { + boolean isVpn = false; + RouteDistinguisher routeDistinguisher = null; + if ((afi == Constants.AFI_VALUE) && (safi == Constants.VPN_SAFI_VALUE)) { + routeDistinguisher = new RouteDistinguisher(); + routeDistinguisher = RouteDistinguisher.read(cb); + isVpn = true; + } else { + isVpn = false; + } + byte protocolId = cb.readByte(); + long identifier = cb.readLong(); + + // Parse Local Node Descriptors + BgpNodeLSIdentifier localNodeDescriptors = new BgpNodeLSIdentifier(); + localNodeDescriptors = BgpNodeLSIdentifier.parseLocalNodeDescriptors(cb, protocolId); + return new BgpNodeLSNlriVer4(identifier, protocolId, localNodeDescriptors, isVpn, routeDistinguisher); + } + + @Override + public NlriType getNlriType() { + return NlriType.NODE; + } + + @Override + public BgpNodeLSIdentifier getLocalNodeDescriptors() { + return this.localNodeDescriptors; + } + + /** + * Returns whether VPN is present or not. + * + * @return whether VPN is present or not + */ + public boolean isVpnPresent() { + return this.isVpn; + } + + @Override + public RouteDistinguisher getRouteDistinguisher() { + return this.routeDistinguisher; + } + + @Override + public long getIdentifier() { + return this.identifier; + } + + /** + * Set the node LS identifier. + * + * @param localNodeDescriptors node LS identifier to set + */ + public void setNodeLSIdentifier(BgpNodeLSIdentifier localNodeDescriptors) { + this.localNodeDescriptors = localNodeDescriptors; + } + + @Override + public ProtocolType getProtocolId() throws BgpParseException { + switch (protocolId) { + case Constants.ISIS_LEVELONE: + return ProtocolType.ISIS_LEVEL_ONE; + case Constants.ISIS_LEVELTWO: + return ProtocolType.ISIS_LEVEL_TWO; + case Constants.OSPFV2: + return ProtocolType.OSPF_V2; + case Constants.DIRECT: + return ProtocolType.DIRECT; + case Constants.STATIC_CONFIGURATION: + return ProtocolType.STATIC_CONFIGURATION; + case Constants.OSPFV3: + return ProtocolType.OSPF_V3; + default: + throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR, (byte) 0, null); + } + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(getClass()) + .omitNullValues() + .add("protocolId", protocolId) + .add("identifier", identifier) + .add("RouteDistinguisher ", routeDistinguisher) + .add("localNodeDescriptors", localNodeDescriptors) + .toString(); + } +} diff --git a/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/linkstate/BgpPrefixIPv4LSNlriVer4.java b/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/linkstate/BgpPrefixIPv4LSNlriVer4.java new file mode 100644 index 00000000..49cb74bd --- /dev/null +++ b/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/linkstate/BgpPrefixIPv4LSNlriVer4.java @@ -0,0 +1,205 @@ +/* + * 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.linkstate; + +import java.util.List; + +import org.jboss.netty.buffer.ChannelBuffer; +import org.onosproject.bgpio.exceptions.BgpParseException; +import org.onosproject.bgpio.protocol.BgpPrefixLSNlri; +import org.onosproject.bgpio.protocol.NlriType; +import org.onosproject.bgpio.protocol.linkstate.BgpNodeLSNlriVer4.ProtocolType; +import org.onosproject.bgpio.types.BgpValueType; +import org.onosproject.bgpio.types.RouteDistinguisher; +import org.onosproject.bgpio.util.Constants; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.base.MoreObjects; + +/** + * Implementation of Prefix IPV4 LS NLRI. + */ +public class BgpPrefixIPv4LSNlriVer4 implements BgpPrefixLSNlri { + + /* + * 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 + +-+-+-+-+-+-+-+-+ + | Protocol-ID | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | Identifier | + | (64 bits) | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + // Local Node Descriptor (variable) // + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + // Prefix Descriptors (variable) // + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + + Figure : The IPv4/IPv6 Topology Prefix NLRI format + */ + + protected static final Logger log = LoggerFactory.getLogger(BgpPrefixIPv4LSNlriVer4.class); + + public static final int PREFIX_IPV4_NLRITYPE = 3; + public static final int IDENTIFIER_LENGTH = 16; + private long identifier; + private byte protocolId; + private RouteDistinguisher routeDistinguisher; + private boolean isVpn; + private BgpPrefixLSIdentifier bgpPrefixLSIdentifier; + + /** + * Resets parameters. + */ + public BgpPrefixIPv4LSNlriVer4() { + this.identifier = 0; + this.protocolId = 0; + this.bgpPrefixLSIdentifier = null; + this.routeDistinguisher = null; + this.isVpn = false; + } + + /** + * Constructor to initialize parameters for BGP PrefixLSNlri. + * + * @param identifier field in BGP PrefixLSNlri + * @param protocolId protocol Id + * @param bgpPrefixLSIdentifier prefix LS Identifier + * @param routeDistinguisher RouteDistinguisher + * @param isVpn vpn availability in message + */ + public BgpPrefixIPv4LSNlriVer4(long identifier, byte protocolId, BgpPrefixLSIdentifier bgpPrefixLSIdentifier, + RouteDistinguisher routeDistinguisher, boolean isVpn) { + this.identifier = identifier; + this.protocolId = protocolId; + this.bgpPrefixLSIdentifier = bgpPrefixLSIdentifier; + this.routeDistinguisher = routeDistinguisher; + this.isVpn = isVpn; + } + + /** + * Reads from channelBuffer and parses Prefix LS Nlri. + * + * @param cb ChannelBuffer + * @param afi Address family identifier + * @param safi Subsequent address family identifier + * @return object of BGPPrefixIPv4LSNlriVer4 + * @throws BgpParseException while parsing Prefix LS Nlri + */ + public static BgpPrefixIPv4LSNlriVer4 read(ChannelBuffer cb, short afi, byte safi) throws BgpParseException { + + boolean isVpn = false; + RouteDistinguisher routeDistinguisher = null; + if ((afi == Constants.AFI_VALUE) && (safi == Constants.VPN_SAFI_VALUE)) { + routeDistinguisher = new RouteDistinguisher(); + routeDistinguisher = RouteDistinguisher.read(cb); + isVpn = true; + } else { + isVpn = false; + } + byte protocolId = cb.readByte(); + long identifier = cb.readLong(); + + BgpPrefixLSIdentifier bgpPrefixLSIdentifier = new BgpPrefixLSIdentifier(); + bgpPrefixLSIdentifier = BgpPrefixLSIdentifier.parsePrefixIdendifier(cb, protocolId); + return new BgpPrefixIPv4LSNlriVer4(identifier, protocolId, bgpPrefixLSIdentifier, routeDistinguisher, isVpn); + } + + @Override + public NlriType getNlriType() { + return NlriType.PREFIX_IPV4; + } + + @Override + public NodeDescriptors getLocalNodeDescriptors() { + return this.bgpPrefixLSIdentifier.getLocalNodeDescriptors(); + } + + @Override + public long getIdentifier() { + return this.identifier; + } + + /** + * Set the prefix LS identifier. + * + * @param bgpPrefixLSIdentifier prefix identifier to set + */ + public void setPrefixLSIdentifier(BgpPrefixLSIdentifier bgpPrefixLSIdentifier) { + this.bgpPrefixLSIdentifier = bgpPrefixLSIdentifier; + } + + @Override + public ProtocolType getProtocolId() throws BgpParseException { + switch (protocolId) { + case Constants.ISIS_LEVELONE: + return ProtocolType.ISIS_LEVEL_ONE; + case Constants.ISIS_LEVELTWO: + return ProtocolType.ISIS_LEVEL_TWO; + case Constants.OSPFV2: + return ProtocolType.OSPF_V2; + case Constants.DIRECT: + return ProtocolType.DIRECT; + case Constants.STATIC_CONFIGURATION: + return ProtocolType.STATIC_CONFIGURATION; + case Constants.OSPFV3: + return ProtocolType.OSPF_V3; + default: + throw new BgpParseException("protocol id not valid"); + } + } + + /** + * Returns whether VPN is present or not. + * + * @return whether VPN is present or not + */ + public boolean isVpnPresent() { + return this.isVpn; + } + + /** + * Returns Prefix Identifier. + * + * @return Prefix Identifier + */ + public BgpPrefixLSIdentifier getPrefixIdentifier() { + return this.bgpPrefixLSIdentifier; + } + + @Override + public RouteDistinguisher getRouteDistinguisher() { + return this.routeDistinguisher; + } + + @Override + public List<BgpValueType> getPrefixdescriptor() { + return this.bgpPrefixLSIdentifier.getPrefixdescriptor(); + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(getClass()) + .omitNullValues() + .add("protocolId", protocolId) + .add("identifier", identifier) + .add("RouteDistinguisher ", routeDistinguisher) + .add("bgpPrefixLSIdentifier", bgpPrefixLSIdentifier) + .toString(); + } +} diff --git a/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/linkstate/BgpPrefixLSIdentifier.java b/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/linkstate/BgpPrefixLSIdentifier.java new file mode 100644 index 00000000..22e68917 --- /dev/null +++ b/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/linkstate/BgpPrefixLSIdentifier.java @@ -0,0 +1,272 @@ +/* + * 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.linkstate; + +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.ListIterator; +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 implements Comparable<Object> { + + protected static final Logger log = LoggerFactory.getLogger(BgpPrefixLSIdentifier.class); + public static final int TYPE_AND_LEN = 4; + private NodeDescriptors localNodeDescriptors; + private List<BgpValueType> prefixDescriptor; + + /** + * Resets parameters. + */ + public BgpPrefixLSIdentifier() { + this.localNodeDescriptors = null; + this.prefixDescriptor = null; + } + + /** + * Constructor to initialize parameters. + * + * @param localNodeDescriptors Local node descriptors + * @param prefixDescriptor Prefix Descriptors + */ + public BgpPrefixLSIdentifier(NodeDescriptors localNodeDescriptors, List<BgpValueType> prefixDescriptor) { + this.localNodeDescriptors = localNodeDescriptors; + this.prefixDescriptor = prefixDescriptor; + } + + /** + * Reads the channel buffer and parses Prefix Identifier. + * + * @param cb ChannelBuffer + * @param protocolId protocol ID + * @return object of this class + * @throws BgpParseException while parsing Prefix Identifier + */ + public static BgpPrefixLSIdentifier parsePrefixIdendifier(ChannelBuffer cb, byte protocolId) + throws BgpParseException { + //Parse Local Node descriptor + NodeDescriptors localNodeDescriptors = new NodeDescriptors(); + localNodeDescriptors = parseLocalNodeDescriptors(cb, protocolId); + + //Parse Prefix descriptor + List<BgpValueType> prefixDescriptor = new LinkedList<>(); + prefixDescriptor = parsePrefixDescriptors(cb); + return new BgpPrefixLSIdentifier(localNodeDescriptors, prefixDescriptor); + } + + /** + * Parse local node descriptors. + * + * @param cb ChannelBuffer + * @param protocolId protocol identifier + * @return LocalNodeDescriptors + * @throws BgpParseException while parsing local node descriptors + */ + public static NodeDescriptors parseLocalNodeDescriptors(ChannelBuffer cb, byte protocolId) + throws BgpParseException { + ChannelBuffer tempBuf = cb.copy(); + 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 List<BgpValueType> parsePrefixDescriptors(ChannelBuffer cb) throws BgpParseException { + LinkedList<BgpValueType> prefixDescriptor = new LinkedList<>(); + BgpValueType tlv = null; + boolean isIpReachInfo = false; + ChannelBuffer tempCb; + int count = 0; + + while (cb.readableBytes() > 0) { + ChannelBuffer tempBuf = cb.copy(); + 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 List<BgpValueType> getPrefixdescriptor() { + return this.prefixDescriptor; + } + + @Override + public int hashCode() { + return Objects.hash(prefixDescriptor.hashCode(), localNodeDescriptors); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + + if (obj instanceof BgpPrefixLSIdentifier) { + int countObjSubTlv = 0; + int countOtherSubTlv = 0; + boolean isCommonSubTlv = true; + BgpPrefixLSIdentifier other = (BgpPrefixLSIdentifier) obj; + + Iterator<BgpValueType> objListIterator = other.prefixDescriptor.iterator(); + countOtherSubTlv = other.prefixDescriptor.size(); + countObjSubTlv = prefixDescriptor.size(); + if (countObjSubTlv != countOtherSubTlv) { + return false; + } else { + while (objListIterator.hasNext() && isCommonSubTlv) { + BgpValueType subTlv = objListIterator.next(); + if (prefixDescriptor.contains(subTlv) && other.prefixDescriptor.contains(subTlv)) { + isCommonSubTlv = Objects.equals(prefixDescriptor.get(prefixDescriptor.indexOf(subTlv)), + other.prefixDescriptor.get(other.prefixDescriptor.indexOf(subTlv))); + } else { + isCommonSubTlv = false; + } + } + 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(); + } + + @Override + public int compareTo(Object o) { + if (this.equals(o)) { + return 0; + } + int result = this.localNodeDescriptors.compareTo(((BgpPrefixLSIdentifier) o).localNodeDescriptors); + if (result != 0) { + return result; + } else { + int countOtherSubTlv = ((BgpPrefixLSIdentifier) o).prefixDescriptor.size(); + int countObjSubTlv = prefixDescriptor.size(); + if (countOtherSubTlv != countObjSubTlv) { + if (countOtherSubTlv > countObjSubTlv) { + return 1; + } else { + return -1; + } + } + + ListIterator<BgpValueType> listIterator = prefixDescriptor.listIterator(); + ListIterator<BgpValueType> listIteratorOther = ((BgpPrefixLSIdentifier) o).prefixDescriptor.listIterator(); + while (listIterator.hasNext()) { + BgpValueType tlv = listIterator.next(); + if (prefixDescriptor.contains(tlv) && ((BgpPrefixLSIdentifier) o).prefixDescriptor.contains(tlv)) { + int res = prefixDescriptor.get(prefixDescriptor.indexOf(tlv)).compareTo( + ((BgpPrefixLSIdentifier) o).prefixDescriptor + .get(((BgpPrefixLSIdentifier) o).prefixDescriptor.indexOf(tlv))); + if (res != 0) { + return res; + } + } else { + return 1; + } + } + } + return 0; + } +} diff --git a/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/linkstate/NodeDescriptors.java b/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/linkstate/NodeDescriptors.java new file mode 100644 index 00000000..f91ac260 --- /dev/null +++ b/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/linkstate/NodeDescriptors.java @@ -0,0 +1,264 @@ +/* + * 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.linkstate; + +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.ListIterator; +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 + */ + + private 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 List<BgpValueType> subTlvs; + private short deslength; + private short desType; + + /** + * Resets parameters. + */ + public NodeDescriptors() { + this.subTlvs = null; + this.deslength = 0; + this.desType = 0; + } + + /** + * Constructor to initialize parameters. + * + * @param subTlvs list of subTlvs + * @param deslength Descriptors length + * @param desType local node descriptor or remote node descriptor type + */ + public NodeDescriptors(List<BgpValueType> subTlvs, short deslength, short desType) { + this.subTlvs = subTlvs; + this.deslength = deslength; + this.desType = desType; + } + + /** + * Returns list of subTlvs. + * + * @return subTlvs list of subTlvs + */ + public List<BgpValueType> getSubTlvs() { + return subTlvs; + } + + @Override + public int hashCode() { + return Objects.hash(subTlvs.hashCode()); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + + if (obj instanceof NodeDescriptors) { + int countObjSubTlv = 0; + int countOtherSubTlv = 0; + boolean isCommonSubTlv = true; + NodeDescriptors other = (NodeDescriptors) obj; + Iterator<BgpValueType> objListIterator = other.subTlvs.iterator(); + countOtherSubTlv = other.subTlvs.size(); + countObjSubTlv = subTlvs.size(); + if (countObjSubTlv != countOtherSubTlv) { + return false; + } else { + while (objListIterator.hasNext() && isCommonSubTlv) { + BgpValueType subTlv = objListIterator.next(); + if (subTlvs.contains(subTlv) && other.subTlvs.contains(subTlv)) { + isCommonSubTlv = Objects.equals(subTlvs.get(subTlvs.indexOf(subTlv)), + other.subTlvs.get(other.subTlvs.indexOf(subTlv))); + } else { + isCommonSubTlv = false; + } + } + 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 { + log.debug("Read NodeDescriptor"); + List<BgpValueType> subTlvs = new LinkedList<>(); + BgpValueType tlv = null; + + while (cb.readableBytes() > 0) { + ChannelBuffer tempBuf = cb.copy(); + 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(); + } + + public int compareTo(Object o) { + if (this.equals(o)) { + return 0; + } + ListIterator<BgpValueType> listIterator = subTlvs.listIterator(); + ListIterator<BgpValueType> listIteratorOther = ((NodeDescriptors) o).subTlvs.listIterator(); + int countOtherSubTlv = ((NodeDescriptors) o).subTlvs.size(); + int countObjSubTlv = subTlvs.size(); + if (countOtherSubTlv != countObjSubTlv) { + if (countOtherSubTlv > countObjSubTlv) { + return 1; + } else { + return -1; + } + } else { + while (listIterator.hasNext()) { + BgpValueType tlv = listIterator.next(); + log.debug("NodeDescriptor compare subtlv's"); + if (subTlvs.contains(tlv) && ((NodeDescriptors) o).subTlvs.contains(tlv)) { + int result = subTlvs.get(subTlvs.indexOf(tlv)).compareTo( + ((NodeDescriptors) o).subTlvs.get(((NodeDescriptors) o).subTlvs.indexOf(tlv))); + if (result != 0) { + return result; + } + } else { + return 1; + } + } + } + return 0; + } +} diff --git a/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/linkstate/PathAttrNlriDetails.java b/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/linkstate/PathAttrNlriDetails.java new file mode 100755 index 00000000..9578ccfe --- /dev/null +++ b/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/linkstate/PathAttrNlriDetails.java @@ -0,0 +1,135 @@ +/* + * 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.linkstate; + +import java.util.Iterator; +import java.util.List; +import java.util.Objects; + +import org.onosproject.bgpio.protocol.linkstate.BgpNodeLSNlriVer4.ProtocolType; +import org.onosproject.bgpio.types.BgpValueType; + +import com.google.common.base.MoreObjects; + +/** + * This Class stores path Attributes, protocol ID and Identifier of LinkState NLRI. + */ +public class PathAttrNlriDetails { + private List<BgpValueType> pathAttributes; + private ProtocolType protocolID; + private long identifier; + + /** + * Sets path attribute with specified path attribute. + * + * @param pathAttributes in update message + */ + public void setPathAttribute(List<BgpValueType> pathAttributes) { + this.pathAttributes = pathAttributes; + } + + /** + * Returns path attributes. + * + * @return path attributes + */ + public List<BgpValueType> pathAttributes() { + return this.pathAttributes; + } + + /** + * Sets protocolID with specified protocolID. + * + * @param protocolID in linkstate nlri + */ + public void setProtocolID(ProtocolType protocolID) { + this.protocolID = protocolID; + } + + /** + * Returns protocolID. + * + * @return protocolID + */ + public ProtocolType protocolID() { + return this.protocolID; + } + + /** + * Sets identifier with specified identifier. + * + * @param identifier in linkstate nlri + */ + public void setIdentifier(long identifier) { + this.identifier = identifier; + } + + /** + * Returns Identifier. + * + * @return Identifier + */ + public long identifier() { + return this.identifier; + } + + @Override + public int hashCode() { + return Objects.hash(pathAttributes, protocolID, identifier); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + + if (obj instanceof PathAttrNlriDetails) { + int countObjSubTlv = 0; + int countOtherSubTlv = 0; + boolean isCommonSubTlv = true; + PathAttrNlriDetails other = (PathAttrNlriDetails) obj; + Iterator<BgpValueType> objListIterator = other.pathAttributes.iterator(); + countOtherSubTlv = other.pathAttributes.size(); + countObjSubTlv = pathAttributes.size(); + if (countObjSubTlv != countOtherSubTlv) { + return false; + } else { + while (objListIterator.hasNext() && isCommonSubTlv) { + BgpValueType subTlv = objListIterator.next(); + if (pathAttributes.contains(subTlv) && other.pathAttributes.contains(subTlv)) { + isCommonSubTlv = Objects.equals(pathAttributes.get(pathAttributes.indexOf(subTlv)), + other.pathAttributes.get(other.pathAttributes.indexOf(subTlv))); + } else { + isCommonSubTlv = false; + } + } + return isCommonSubTlv && Objects.equals(identifier, other.identifier) + && Objects.equals(protocolID, other.protocolID); + } + } + return false; + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(getClass()) + .add("identifier", identifier) + .add("protocolID", protocolID) + .add("pathAttributes", pathAttributes) + .toString(); + } +} diff --git a/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/linkstate/PathAttrNlriDetailsLocalRib.java b/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/linkstate/PathAttrNlriDetailsLocalRib.java new file mode 100755 index 00000000..4172ae46 --- /dev/null +++ b/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/linkstate/PathAttrNlriDetailsLocalRib.java @@ -0,0 +1,122 @@ +/* + * 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.linkstate; + +import java.util.Objects; + +import org.onlab.packet.IpAddress; +import com.google.common.base.MoreObjects; + +/** + * This Class stores path Attributes, protocol ID and Identifier of LinkState nlri. + */ +public class PathAttrNlriDetailsLocalRib { + + private IpAddress localRibIpAddress; + private long localRibAsNum; + private int localRibIdentifier; + private boolean isLocalRibIbgpSession; + private PathAttrNlriDetails localRibNlridetails; + + /** + * Constructor to initialize parameter. + * + * @param localRibIpAddress peer ip address + * @param localRibIdentifier peer identifier + * @param localRibAsNum peer As number + * @param isLocalRibIbgpSession flag to indicate is Ibgp session + * @param localRibNlridetails Nlri details + * + */ + public PathAttrNlriDetailsLocalRib(IpAddress localRibIpAddress, int localRibIdentifier, long localRibAsNum, + boolean isLocalRibIbgpSession, PathAttrNlriDetails localRibNlridetails) { + this.localRibIpAddress = localRibIpAddress; + this.localRibAsNum = localRibAsNum; + this.localRibIdentifier = localRibIdentifier; + this.isLocalRibIbgpSession = isLocalRibIbgpSession; + this.localRibNlridetails = localRibNlridetails; + } + + /** + * Gets the Ipaddress updated in local rib. + * + * @return localRibIpAddress ip address + */ + public IpAddress localRibIpAddress() { + return localRibIpAddress; + } + + /** + * Gets the autonomous system number updated in local rib. + * + * @return localRibAsNum autonomous system number + */ + public long localRibAsNum() { + return localRibAsNum; + } + + /** + * Gets the indetifier updated in local rib. + * + * @return localRibIdentifier identifier + */ + public int localRibIdentifier() { + return localRibIdentifier; + } + + /** + * Gets the bgp session type updated in local rib. + * + * @return isLocalRibIbgpSession session type + */ + public boolean isLocalRibIbgpSession() { + return isLocalRibIbgpSession; + } + + /** + * Returns local RIB Nlri details. + * + * @return localRibNlridetails Nlri details in local rib + */ + public PathAttrNlriDetails localRibNlridetails() { + return this.localRibNlridetails; + } + + @Override + public int hashCode() { + return Objects.hash(localRibIpAddress, localRibIdentifier, localRibAsNum, isLocalRibIbgpSession, + localRibNlridetails.hashCode()); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj instanceof PathAttrNlriDetailsLocalRib) { + PathAttrNlriDetailsLocalRib other = (PathAttrNlriDetailsLocalRib) obj; + return Objects.equals(localRibIpAddress, other.localRibIpAddress) + && Objects.equals(localRibIdentifier, other.localRibIdentifier) + && Objects.equals(localRibAsNum, other.localRibAsNum) + && Objects.equals(isLocalRibIbgpSession, other.isLocalRibIbgpSession) + && Objects.equals(localRibNlridetails, other.localRibNlridetails); + } + return false; + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(getClass()).add("peerIdentifier", localRibIdentifier) + .add("localRibpathAttributes", localRibNlridetails.pathAttributes()).toString(); + } +} diff --git a/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/linkstate/package-info.java b/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/linkstate/package-info.java new file mode 100755 index 00000000..87ba60f0 --- /dev/null +++ b/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/linkstate/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.linkstate; diff --git a/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/package-info.java b/framework/src/onos/protocols/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/protocols/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/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/BgpFactoryVer4.java b/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/BgpFactoryVer4.java new file mode 100755 index 00000000..c57832b6 --- /dev/null +++ b/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/BgpFactoryVer4.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.ver4; + +import org.onosproject.bgpio.protocol.BgpFactory; +import org.onosproject.bgpio.protocol.BgpKeepaliveMsg; +import org.onosproject.bgpio.protocol.BgpMessage; +import org.onosproject.bgpio.protocol.BgpMessageReader; +import org.onosproject.bgpio.protocol.BgpNotificationMsg; +import org.onosproject.bgpio.protocol.BgpOpenMsg; +import org.onosproject.bgpio.protocol.BgpVersion; + +/** + * Provides BGP Factory and returns builder classes for all objects and messages. + */ +public class BgpFactoryVer4 implements BgpFactory { + + public static final BgpFactoryVer4 INSTANCE = new BgpFactoryVer4(); + + @Override + public BgpOpenMsg.Builder openMessageBuilder() { + return new BgpOpenMsgVer4.Builder(); + } + + @Override + public BgpKeepaliveMsg.Builder keepaliveMessageBuilder() { + return new BgpKeepaliveMsgVer4.Builder(); + } + + @Override + public BgpNotificationMsg.Builder notificationMessageBuilder() { + return new BgpNotificationMsgVer4.Builder(); + } + + @Override + public BgpMessageReader<BgpMessage> getReader() { + return BgpMessageVer4.READER; + } + + @Override + public BgpVersion getVersion() { + return BgpVersion.BGP_4; + } +}
\ No newline at end of file diff --git a/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/BgpKeepaliveMsgVer4.java b/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/BgpKeepaliveMsgVer4.java new file mode 100644 index 00000000..2c141586 --- /dev/null +++ b/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/BgpKeepaliveMsgVer4.java @@ -0,0 +1,157 @@ +/* + * 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. + */ +public class BgpKeepaliveMsgVer4 implements BgpKeepaliveMsg { + + /* + <Keepalive Message>::= <Common Header> + A KEEPALIVE message consists of only the message header and has a + length of 19 octets. + + 0 1 2 3 + 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | | + + + + | | + + + + | Marker | + + + + | | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | Length | Type | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + + REFERENCE : RFC 4271 + */ + + protected static final Logger log = LoggerFactory + .getLogger(BgpKeepaliveMsgVer4.class); + + private BgpHeader bgpMsgHeader; + public static final byte PACKET_VERSION = 4; + public static final int PACKET_MINIMUM_LENGTH = 19; + public static final int MARKER_LENGTH = 16; + public static final BgpType MSG_TYPE = BgpType.KEEP_ALIVE; + public static byte[] marker = new byte[] {(byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, + (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, + (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, + (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff}; + + public static final BgpKeepaliveMsgVer4.Reader READER = new Reader(); + + /** + * Reader class for reading BGP keepalive message from channel buffer. + */ + static class Reader implements BgpMessageReader<BgpKeepaliveMsg> { + + @Override + public BgpKeepaliveMsg readFrom(ChannelBuffer cb, BgpHeader bgpHeader) + throws BgpParseException { + + /* bgpHeader is not required in case of keepalive message and + Header is already read and no other fields except header in keepalive message.*/ + return new BgpKeepaliveMsgVer4(); + } + } + + /** + * Default constructor. + */ + public BgpKeepaliveMsgVer4() { + } + + /** + * Builder class for BGP keepalive message. + */ + static class Builder implements BgpKeepaliveMsg.Builder { + BgpHeader bgpMsgHeader; + + @Override + public Builder setHeader(BgpHeader bgpMsgHeader) { + this.bgpMsgHeader = bgpMsgHeader; + return this; + } + + @Override + public BgpKeepaliveMsg build() { + return new BgpKeepaliveMsgVer4(); + } + } + + @Override + public void writeTo(ChannelBuffer cb) { + WRITER.write(cb, this); + } + + static final Writer WRITER = new Writer(); + + /** + * Writer class for writing the BGP keepalive message to channel buffer. + */ + static class Writer implements BgpMessageWriter<BgpKeepaliveMsgVer4> { + + @Override + public void write(ChannelBuffer cb, BgpKeepaliveMsgVer4 message) { + + // write marker + cb.writeBytes(marker, 0, MARKER_LENGTH); + + // write length of header + cb.writeShort(PACKET_MINIMUM_LENGTH); + + // write the type of message + cb.writeByte(MSG_TYPE.getType()); + } + } + + @Override + public BgpVersion getVersion() { + return BgpVersion.BGP_4; + } + + @Override + public BgpType getType() { + return MSG_TYPE; + } + + @Override + public BgpHeader getHeader() { + return this.bgpMsgHeader; + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(getClass()).toString(); + } +} diff --git a/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/BgpMessageVer4.java b/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/BgpMessageVer4.java new file mode 100755 index 00000000..1c05dae4 --- /dev/null +++ b/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/BgpMessageVer4.java @@ -0,0 +1,111 @@ +/* + * 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.BgpFactories; +import org.onosproject.bgpio.protocol.BgpMessage; +import org.onosproject.bgpio.protocol.BgpMessageReader; +import org.onosproject.bgpio.types.BgpErrorType; +import org.onosproject.bgpio.types.BgpHeader; +import org.onosproject.bgpio.util.Validation; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Provides BGP messages. + */ +public abstract class BgpMessageVer4 { + + protected static final Logger log = LoggerFactory.getLogger(BgpFactories.class); + + static final byte OPEN_MSG_TYPE = 0x1; + static final byte KEEPALIVE_MSG_TYPE = 0x4; + static final byte UPDATE_MSG_TYPE = 0x2; + static final byte NOTIFICATION_MSG_TYPE = 0x3; + static final int MINIMUM_COMMON_HEADER_LENGTH = 19; + static final int HEADER_AND_MSG_LEN = 18; + static final int MAXIMUM_PACKET_LENGTH = 4096; + + public static final BgpMessageVer4.Reader READER = new Reader(); + + /** + * Reader class for reading BGP messages from channel buffer. + * + */ + static class Reader implements BgpMessageReader<BgpMessage> { + @Override + public BgpMessage readFrom(ChannelBuffer cb, BgpHeader bgpHeader) + throws BgpParseException { + + if (cb.readableBytes() < MINIMUM_COMMON_HEADER_LENGTH) { + log.error("Packet should have minimum length."); + Validation.validateLen(BgpErrorType.MESSAGE_HEADER_ERROR, BgpErrorType.BAD_MESSAGE_LENGTH, + cb.readableBytes()); + } + if (cb.readableBytes() > MAXIMUM_PACKET_LENGTH) { + log.error("Packet length should not exceed {}.", MAXIMUM_PACKET_LENGTH); + Validation.validateLen(BgpErrorType.MESSAGE_HEADER_ERROR, BgpErrorType.BAD_MESSAGE_LENGTH, + cb.readableBytes()); + } + try { + // fixed value property version == 4 + byte[] marker = new byte[BgpHeader.MARKER_LENGTH]; + cb.readBytes(marker, 0, BgpHeader.MARKER_LENGTH); + bgpHeader.setMarker(marker); + for (int i = 0; i < BgpHeader.MARKER_LENGTH; i++) { + if (marker[i] != (byte) 0xff) { + throw new BgpParseException(BgpErrorType.MESSAGE_HEADER_ERROR, + BgpErrorType.CONNECTION_NOT_SYNCHRONIZED, null); + } + } + short length = cb.readShort(); + if (length > cb.readableBytes() + HEADER_AND_MSG_LEN) { + Validation.validateLen(BgpErrorType.MESSAGE_HEADER_ERROR, + BgpErrorType.BAD_MESSAGE_LENGTH, length); + } + bgpHeader.setLength(length); + byte type = cb.readByte(); + bgpHeader.setType(type); + log.debug("Reading update message of type " + type); + + int len = length - MINIMUM_COMMON_HEADER_LENGTH; + switch (type) { + case OPEN_MSG_TYPE: + log.debug("OPEN MESSAGE is received"); + return BgpOpenMsgVer4.READER.readFrom(cb.readBytes(len), bgpHeader); + case KEEPALIVE_MSG_TYPE: + log.debug("KEEPALIVE MESSAGE is received"); + return BgpKeepaliveMsgVer4.READER.readFrom(cb.readBytes(len), bgpHeader); + case UPDATE_MSG_TYPE: + log.debug("UPDATE MESSAGE is received"); + return BgpUpdateMsgVer4.READER.readFrom(cb.readBytes(len), bgpHeader); + case NOTIFICATION_MSG_TYPE: + log.debug("NOTIFICATION MESSAGE is received"); + return BgpNotificationMsgVer4.READER.readFrom(cb.readBytes(len), bgpHeader); + default: + Validation.validateType(BgpErrorType.MESSAGE_HEADER_ERROR, BgpErrorType.BAD_MESSAGE_TYPE, type); + return null; + } + } catch (IndexOutOfBoundsException e) { + throw new BgpParseException(BgpErrorType.MESSAGE_HEADER_ERROR, + BgpErrorType.BAD_MESSAGE_LENGTH, null); + } + } + } +}
\ No newline at end of file diff --git a/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/BgpNotificationMsgVer4.java b/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/BgpNotificationMsgVer4.java new file mode 100644 index 00000000..7243e21a --- /dev/null +++ b/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/BgpNotificationMsgVer4.java @@ -0,0 +1,265 @@ +/* + * 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.BgpMessageReader; +import org.onosproject.bgpio.protocol.BgpMessageWriter; +import org.onosproject.bgpio.protocol.BgpNotificationMsg; +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.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.base.MoreObjects; + +/** + * A NOTIFICATION message is sent when an error condition is detected. The BGP connection is closed immediately after it + * is sent. + */ +class BgpNotificationMsgVer4 implements BgpNotificationMsg { + + /* + 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 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | Error code | Error subcode | Data (variable) | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + REFERENCE : RFC 4271 + */ + + private static final Logger log = LoggerFactory.getLogger(BgpNotificationMsgVer4.class); + + static final byte PACKET_VERSION = 4; + //BGPHeader(19) + Error code(1) + Error subcode(1) + static final int TOTAL_MESSAGE_MIN_LENGTH = 21; + static final int PACKET_MINIMUM_LENGTH = 2; + static final BgpType MSG_TYPE = BgpType.NOTIFICATION; + static final byte DEFAULT_ERRORSUBCODE = 0; + static final byte[] MARKER = {(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 }; + static final byte MESSAGE_TYPE = 3; + static final BgpHeader DEFAULT_MESSAGE_HEADER = new BgpHeader(MARKER, BgpHeader.DEFAULT_HEADER_LENGTH, + MESSAGE_TYPE); + + private byte errorCode; + private byte errorSubCode; + private byte[] data; + private BgpHeader bgpHeader; + public static final BgpNotificationMsgVer4.Reader READER = new Reader(); + + /** + * Initialize fields. + */ + public BgpNotificationMsgVer4() { + this.bgpHeader = null; + this.data = null; + this.errorCode = 0; + this.errorSubCode = 0; + } + + /** + * Constructor to initialize parameters. + * + * @param bgpHeader BGP Header in notification message + * @param errorCode error code + * @param errorSubCode error subcode + * @param data field + */ + public BgpNotificationMsgVer4(BgpHeader bgpHeader, byte errorCode, byte errorSubCode, byte[] data) { + this.bgpHeader = bgpHeader; + this.data = data; + this.errorCode = errorCode; + this.errorSubCode = errorSubCode; + } + + /** + * Reader reads BGP Notification Message from the channel buffer. + */ + static class Reader implements BgpMessageReader<BgpNotificationMsg> { + @Override + public BgpNotificationMsg readFrom(ChannelBuffer cb, BgpHeader bgpHeader) throws BgpParseException { + byte errorCode; + byte errorSubCode; + if (cb.readableBytes() < PACKET_MINIMUM_LENGTH) { + throw new BgpParseException("Not enough readable bytes"); + } + errorCode = cb.readByte(); + errorSubCode = cb.readByte(); + //Message Length = 21 + Data Length + int dataLength = bgpHeader.getLength() - TOTAL_MESSAGE_MIN_LENGTH; + byte[] data = new byte[dataLength]; + cb.readBytes(data, 0, dataLength); + return new BgpNotificationMsgVer4(bgpHeader, errorCode, errorSubCode, data); + } + } + + /** + * Builder class for BGP notification message. + */ + static class Builder implements BgpNotificationMsg.Builder { + private byte errorCode; + private byte errorSubCode; + private byte[] data; + private BgpHeader bgpHeader; + private boolean isErrorCodeSet = false; + private boolean isErrorSubCodeSet = false; + private boolean isBGPHeaderSet = false; + + @Override + public BgpNotificationMsg build() throws BgpParseException { + BgpHeader bgpHeader = this.isBGPHeaderSet ? this.bgpHeader : DEFAULT_MESSAGE_HEADER; + if (!this.isErrorCodeSet) { + throw new BgpParseException("Error code must be present"); + } + + byte errorSubCode = this.isErrorSubCodeSet ? this.errorSubCode : DEFAULT_ERRORSUBCODE; + return new BgpNotificationMsgVer4(bgpHeader, this.errorCode, errorSubCode, this.data); + } + + @Override + public Builder setErrorCode(byte errorCode) { + this.errorCode = errorCode; + this.isErrorCodeSet = true; + return this; + } + + @Override + public Builder setErrorSubCode(byte errorSubCode) { + this.errorSubCode = errorSubCode; + this.isErrorSubCodeSet = true; + return this; + } + + @Override + public Builder setData(byte[] data) { + if (data != null) { + this.data = data; + } + return this; + } + + @Override + public Builder setHeader(BgpHeader bgpMsgHeader) { + this.bgpHeader = bgpMsgHeader; + return this; + } + } + + @Override + public BgpVersion getVersion() { + return BgpVersion.BGP_4; + } + + @Override + public BgpType getType() { + return BgpType.NOTIFICATION; + } + + @Override + public void writeTo(ChannelBuffer cb) throws BgpParseException { + WRITER.write(cb, this); + } + + static final Writer WRITER = new Writer(); + + /** + * Writer writes BGP notification message to channel buffer. + */ + static class Writer implements BgpMessageWriter<BgpNotificationMsgVer4> { + @Override + public void write(ChannelBuffer cb, BgpNotificationMsgVer4 message) throws BgpParseException { + int msgStartIndex = cb.writerIndex(); + int headerLenIndex = message.bgpHeader.write(cb); + if (headerLenIndex <= 0) { + throw new BgpParseException(BgpErrorType.MESSAGE_HEADER_ERROR, (byte) 0, null); + } + cb.writeByte(message.errorCode); + cb.writeByte(message.errorSubCode); + if (message.data != null) { + cb.writeBytes(message.data); + } + + //Update message length field in notification message + int length = cb.writerIndex() - msgStartIndex; + cb.setShort(headerLenIndex, (short) length); + message.bgpHeader.setLength((short) length); + } + } + + @Override + public byte getErrorCode() { + return this.errorCode; + } + + /** + * Sets errorcode with specified errorcode. + * + * @param errorCode field + */ + public void setErrorCode(byte errorCode) { + this.errorCode = errorCode; + } + + @Override + public byte getErrorSubCode() { + return this.errorSubCode; + } + + /** + * Sets error subcode with specified errorSubCode. + * + * @param errorSubCode field + */ + public void setErrorSubCode(byte errorSubCode) { + this.errorSubCode = errorSubCode; + } + + @Override + public byte[] getData() { + return this.data; + } + + /** + * Sets error data with specified data. + * + * @param data field + */ + public void setData(byte[] data) { + this.data = data; + } + + @Override + public BgpHeader getHeader() { + return this.bgpHeader; + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(getClass()) + .omitNullValues() + .add("bgpHeader", bgpHeader) + .add("data", data) + .add("errorCode", errorCode) + .add("errorSubCode", errorSubCode) + .toString(); + } +}
\ No newline at end of file diff --git a/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/BgpOpenMsgVer4.java b/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/BgpOpenMsgVer4.java new file mode 100644 index 00000000..359eec25 --- /dev/null +++ b/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/BgpOpenMsgVer4.java @@ -0,0 +1,518 @@ +/* + * 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.types.FourOctetAsNumCapabilityTlv; +import org.onosproject.bgpio.types.MultiProtocolExtnCapabilityTlv; +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 short AS_TRANS = 23456; + public static final int OPT_PARA_TYPE_CAPABILITY = 2; + public static final BgpType MSG_TYPE = BgpType.OPEN; + public static final short AFI = 16388; + public static final byte SAFI = 71; + public static final byte RES = 0; + public static final int FOUR_OCTET_AS_NUM_CAPA_TYPE = 65; + 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 boolean isLargeAsCapabilitySet; + private LinkedList<BgpValueType> capabilityTlv; + + public static final BgpOpenMsgVer4.Reader READER = new Reader(); + + /** + * reset variables. + */ + public BgpOpenMsgVer4() { + this.bgpMsgHeader = null; + this.version = 0; + this.holdTime = 0; + this.asNumber = 0; + this.bgpId = 0; + this.capabilityTlv = null; + } + + /** + * Constructor to initialize all variables of BGP Open message. + * + * @param bgpMsgHeader BGP Header in open message + * @param version BGP version in open message + * @param holdTime hold time in open message + * @param asNumber AS number in open message + * @param bgpId BGP identifier in open message + * @param capabilityTlv capabilities in open message + */ + public BgpOpenMsgVer4(BgpHeader bgpMsgHeader, byte version, short asNumber, short holdTime, + int bgpId, LinkedList<BgpValueType> capabilityTlv) { + this.bgpMsgHeader = bgpMsgHeader; + this.version = version; + this.asNumber = asNumber; + this.holdTime = holdTime; + this.bgpId = bgpId; + this.capabilityTlv = capabilityTlv; + } + + @Override + public BgpHeader getHeader() { + return this.bgpMsgHeader; + } + + @Override + public BgpVersion getVersion() { + return BgpVersion.BGP_4; + } + + @Override + public BgpType getType() { + return MSG_TYPE; + } + + @Override + public short getHoldTime() { + return this.holdTime; + } + + @Override + public short getAsNumber() { + return this.asNumber; + } + + @Override + public int getBgpId() { + return this.bgpId; + } + + @Override + public LinkedList<BgpValueType> getCapabilityTlv() { + return this.capabilityTlv; + } + + /** + * Reader class for reading BGP open message from channel buffer. + */ + public static class Reader implements BgpMessageReader<BgpOpenMsg> { + + @Override + public BgpOpenMsg readFrom(ChannelBuffer cb, BgpHeader bgpHeader) throws BgpParseException { + + byte version; + short holdTime; + short asNumber; + int bgpId; + byte optParaLen = 0; + byte optParaType; + byte capParaLen = 0; + LinkedList<BgpValueType> capabilityTlv = new LinkedList<>(); + + if (cb.readableBytes() < OPEN_MSG_MINIMUM_LENGTH) { + log.error("[readFrom] Invalid length: Packet size is less than the minimum length "); + Validation.validateLen(BgpErrorType.OPEN_MESSAGE_ERROR, BgpErrorType.BAD_MESSAGE_LENGTH, + cb.readableBytes()); + } + + // Read version + version = cb.readByte(); + if (version != PACKET_VERSION) { + log.error("[readFrom] Invalid version: " + version); + throw new BgpParseException(BgpErrorType.OPEN_MESSAGE_ERROR, + BgpErrorType.UNSUPPORTED_VERSION_NUMBER, null); + } + + // Read AS number + asNumber = cb.readShort(); + + // Read Hold timer + holdTime = cb.readShort(); + + // Read BGP Identifier + bgpId = cb.readInt(); + + // Read optional parameter length + optParaLen = cb.readByte(); + + // Read Capabilities if optional parameter length is greater than 0 + if (optParaLen != 0) { + // Read optional parameter type + optParaType = cb.readByte(); + + // Read optional parameter length + capParaLen = cb.readByte(); + + if (cb.readableBytes() < capParaLen) { + throw new BgpParseException(BgpErrorType.OPEN_MESSAGE_ERROR, (byte) 0, null); + } + + ChannelBuffer capaCb = cb.readBytes(capParaLen); + + // Parse capabilities only if optional parameter type is 2 + if ((optParaType == OPT_PARA_TYPE_CAPABILITY) && (capParaLen != 0)) { + capabilityTlv = parseCapabilityTlv(capaCb); + } else { + throw new BgpParseException(BgpErrorType.OPEN_MESSAGE_ERROR, + BgpErrorType.UNSUPPORTED_OPTIONAL_PARAMETER, null); + } + } + return new BgpOpenMsgVer4(bgpHeader, version, asNumber, holdTime, bgpId, capabilityTlv); + } + } + + /** + * Parsing capabilities. + * + * @param cb of type channel buffer + * @return capabilityTlv of open message + * @throws BgpParseException while parsing capabilities + */ + protected static LinkedList<BgpValueType> parseCapabilityTlv(ChannelBuffer cb) throws BgpParseException { + + LinkedList<BgpValueType> capabilityTlv = new LinkedList<>(); + + while (cb.readableBytes() > 0) { + BgpValueType tlv; + short type = cb.readByte(); + short length = cb.readByte(); + + switch (type) { + case FourOctetAsNumCapabilityTlv.TYPE: + log.debug("FourOctetAsNumCapabilityTlv"); + if (FourOctetAsNumCapabilityTlv.LENGTH != length) { + throw new BgpParseException("Invalid length received for FourOctetAsNumCapabilityTlv."); + } + if (length > cb.readableBytes()) { + throw new BgpParseException("Four octet as num tlv length" + + " is more than readableBytes."); + } + int as4Num = cb.readInt(); + tlv = new FourOctetAsNumCapabilityTlv(as4Num); + break; + case MultiProtocolExtnCapabilityTlv.TYPE: + log.debug("MultiProtocolExtnCapabilityTlv"); + if (MultiProtocolExtnCapabilityTlv.LENGTH != length) { + throw new BgpParseException("Invalid length received for MultiProtocolExtnCapabilityTlv."); + } + if (length > cb.readableBytes()) { + throw new BgpParseException("BGP LS tlv length is more than readableBytes."); + } + short afi = cb.readShort(); + byte res = cb.readByte(); + byte safi = cb.readByte(); + tlv = new MultiProtocolExtnCapabilityTlv(afi, res, safi); + break; + default: + log.debug("Warning: Unsupported TLV: " + type); + cb.skipBytes(length); + continue; + } + capabilityTlv.add(tlv); + } + 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; + private boolean isLargeAsCapabilityTlvSet = false; + private boolean isLsCapabilityTlvSet = false; + + LinkedList<BgpValueType> capabilityTlv = new LinkedList<>(); + + @Override + public BgpOpenMsg build() throws BgpParseException { + BgpHeader bgpMsgHeader = this.isHeaderSet ? this.bgpMsgHeader : DEFAULT_OPEN_HEADER; + short holdTime = this.isHoldTimeSet ? this.holdTime : DEFAULT_HOLD_TIME; + + if (!this.isAsNumSet) { + throw new BgpParseException("BGP AS number is not set (mandatory)"); + } + + if (!this.isBgpIdSet) { + throw new BgpParseException("BGPID is not set (mandatory)"); + } + + if (this.isLargeAsCapabilityTlvSet) { + BgpValueType tlv; + int value = this.asNumber; + tlv = new FourOctetAsNumCapabilityTlv(value); + this.capabilityTlv.add(tlv); + } + + if (this.isLsCapabilityTlvSet) { + BgpValueType tlv; + tlv = new MultiProtocolExtnCapabilityTlv(AFI, RES, SAFI); + this.capabilityTlv.add(tlv); + } + + return new BgpOpenMsgVer4(bgpMsgHeader, PACKET_VERSION, this.asNumber, holdTime, this.bgpId, + this.capabilityTlv); + } + + @Override + public Builder setHeader(BgpHeader bgpMsgHeader) { + this.bgpMsgHeader = bgpMsgHeader; + return this; + } + + @Override + public Builder setHoldTime(short holdTime) { + this.holdTime = holdTime; + this.isHoldTimeSet = true; + return this; + } + + @Override + public Builder setAsNumber(short asNumber) { + this.asNumber = asNumber; + this.isAsNumSet = true; + return this; + } + + @Override + public Builder setBgpId(int bgpId) { + this.bgpId = bgpId; + this.isBgpIdSet = true; + return this; + } + + @Override + public Builder setCapabilityTlv(LinkedList<BgpValueType> capabilityTlv) { + this.capabilityTlv = capabilityTlv; + return this; + } + + @Override + public Builder setLargeAsCapabilityTlv(boolean isLargeAsCapabilitySet) { + this.isLargeAsCapabilityTlvSet = isLargeAsCapabilitySet; + return this; + } + + @Override + public Builder setLsCapabilityTlv(boolean isLsCapabilitySet) { + this.isLsCapabilityTlvSet = isLsCapabilitySet; + return this; + } + } + + @Override + public void writeTo(ChannelBuffer cb) { + try { + WRITER.write(cb, this); + } catch (BgpParseException e) { + log.debug("[writeTo] Error: " + e.toString()); + } + } + + public static final Writer WRITER = new Writer(); + + /** + * Writer class for writing BGP open message to channel buffer. + */ + public static class Writer implements BgpMessageWriter<BgpOpenMsgVer4> { + + @Override + public void write(ChannelBuffer cb, BgpOpenMsgVer4 message) throws BgpParseException { + + int optParaLen = 0; + int as4num = 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); + + // get as4num if LS Capability is set + if (message.isLargeAsCapabilitySet) { + LinkedList<BgpValueType> capabilityTlv = message + .getCapabilityTlv(); + ListIterator<BgpValueType> listIterator = capabilityTlv + .listIterator(); + + while (listIterator.hasNext()) { + BgpValueType tlv = listIterator.next(); + if (tlv.getType() == FOUR_OCTET_AS_NUM_CAPA_TYPE) { + as4num = ((FourOctetAsNumCapabilityTlv) tlv).getInt(); + break; + } + } + } + + if ((message.isLargeAsCapabilitySet) && (as4num > 65535)) { + // write As number as AS_TRANS + cb.writeShort(AS_TRANS); + } else { + // write AS number in next 2-octet + cb.writeShort(message.asNumber); + } + + // write HoldTime in next 2-octet + cb.writeShort(message.holdTime); + + // write BGP Identifier in next 4-octet + cb.writeInt(message.bgpId); + + // store the index of Optional parameter length + int optParaLenIndex = cb.writerIndex(); + + // set optional parameter length as 0 + cb.writeByte(0); + + // Pack capability TLV + optParaLen = message.packCapabilityTlv(cb, message); + + if (optParaLen != 0) { + // Update optional parameter length + cb.setByte(optParaLenIndex, (byte) (optParaLen + 2)); //+2 for optional parameter type. + } + + // write OPEN Object Length + int length = cb.writerIndex() - startIndex; + cb.setShort(msgLenIndex, (short) length); + message.bgpMsgHeader.setLength((short) length); + } + } + + /** + * returns length of capability tlvs. + * + * @param cb of type channel buffer + * @param message of type BGPOpenMsgVer4 + * @return capParaLen of open message + */ + protected int packCapabilityTlv(ChannelBuffer cb, BgpOpenMsgVer4 message) { + int startIndex = cb.writerIndex(); + int capParaLen = 0; + int capParaLenIndex = 0; + + LinkedList<BgpValueType> capabilityTlv = message.capabilityTlv; + ListIterator<BgpValueType> listIterator = capabilityTlv.listIterator(); + + if (listIterator.hasNext()) { + // Set optional parameter type as 2 + cb.writeByte(OPT_PARA_TYPE_CAPABILITY); + + // Store the index of capability parameter length and update length at the end + capParaLenIndex = cb.writerIndex(); + + // Set capability parameter length as 0 + cb.writeByte(0); + + // Update the startIndex to know the length of capability tlv + startIndex = cb.writerIndex(); + } + + while (listIterator.hasNext()) { + BgpValueType tlv = listIterator.next(); + if (tlv == null) { + log.debug("Warning: tlv is null from CapabilityTlv list"); + continue; + } + tlv.write(cb); + } + + capParaLen = cb.writerIndex() - startIndex; + + if (capParaLen != 0) { + // Update capability parameter length + cb.setByte(capParaLenIndex, (byte) capParaLen); + } + return capParaLen; + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(getClass()) + .add("bgpMsgHeader", bgpMsgHeader) + .add("version", version) + .add("holdTime", holdTime) + .add("asNumber", asNumber) + .add("bgpId", bgpId) + .add("capabilityTlv", capabilityTlv) + .toString(); + } +} diff --git a/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/BgpPathAttributes.java b/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/BgpPathAttributes.java new file mode 100644 index 00000000..13e52ca2 --- /dev/null +++ b/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/BgpPathAttributes.java @@ -0,0 +1,200 @@ +/* + * 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.List; + +import org.jboss.netty.buffer.ChannelBuffer; +import org.onosproject.bgpio.exceptions.BgpParseException; +import org.onosproject.bgpio.types.As4Path; +import org.onosproject.bgpio.types.AsPath; +import org.onosproject.bgpio.types.BgpErrorType; +import org.onosproject.bgpio.types.BgpValueType; +import org.onosproject.bgpio.types.LocalPref; +import org.onosproject.bgpio.types.Med; +import org.onosproject.bgpio.types.NextHop; +import org.onosproject.bgpio.types.Origin; +import org.onosproject.bgpio.types.MpReachNlri; +import org.onosproject.bgpio.types.MpUnReachNlri; +import org.onosproject.bgpio.util.UnSupportedAttribute; +import org.onosproject.bgpio.util.Validation; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.base.MoreObjects; + +/** + * Provides Implementation of BGP Path Attribute. + */ +public class BgpPathAttributes { + + /* Path attribute: + <attribute type, attribute length, attribute value> + + 0 1 + 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | Attr. Flags |Attr. Type Code| + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + REFERENCE : RFC 4271 + */ + protected static final Logger log = LoggerFactory.getLogger(BgpPathAttributes.class); + + public static final int LINK_STATE_ATTRIBUTE_TYPE = 50; + public static final int MPREACHNLRI_TYPE = 14; + public static final int MPUNREACHNLRI_TYPE = 15; + + private final List<BgpValueType> pathAttribute; + + /** + * Initialize parameter. + */ + public BgpPathAttributes() { + this.pathAttribute = null; + } + + /** + * Constructor to initialize parameters for BGP path attributes. + * + * @param pathAttribute list of path attributes + */ + public BgpPathAttributes(List<BgpValueType> pathAttribute) { + this.pathAttribute = pathAttribute; + } + + /** + * Returns list of path attributes. + * + * @return list of path attributes + */ + public List<BgpValueType> pathAttributes() { + return this.pathAttribute; + } + + /** + * Reads from channelBuffer and parses BGP path attributes. + * + * @param cb channelBuffer + * @return object of BgpPathAttributes + * @throws BgpParseException while parsing BGP path attributes + */ + public static BgpPathAttributes read(ChannelBuffer cb) + throws BgpParseException { + + BgpValueType pathAttribute = null; + List<BgpValueType> pathAttributeList = new LinkedList<>(); + boolean isOrigin = false; + boolean isAsPath = false; + boolean isNextHop = false; + boolean isMpReach = false; + boolean isMpUnReach = false; + while (cb.readableBytes() > 0) { + cb.markReaderIndex(); + byte flags = cb.readByte(); + byte typeCode = cb.readByte(); + cb.resetReaderIndex(); + switch (typeCode) { + case Origin.ORIGIN_TYPE: + pathAttribute = Origin.read(cb); + isOrigin = ((Origin) pathAttribute).isOriginSet(); + break; + case AsPath.ASPATH_TYPE: + pathAttribute = AsPath.read(cb); + isAsPath = ((AsPath) pathAttribute).isaspathSet(); + break; + case As4Path.AS4PATH_TYPE: + pathAttribute = As4Path.read(cb); + break; + case NextHop.NEXTHOP_TYPE: + pathAttribute = NextHop.read(cb); + isNextHop = ((NextHop) pathAttribute).isNextHopSet(); + break; + case Med.MED_TYPE: + pathAttribute = Med.read(cb); + break; + case LocalPref.LOCAL_PREF_TYPE: + pathAttribute = LocalPref.read(cb); + break; + case MpReachNlri.MPREACHNLRI_TYPE: + pathAttribute = MpReachNlri.read(cb); + isMpReach = ((MpReachNlri) pathAttribute).isMpReachNlriSet(); + break; + case MpUnReachNlri.MPUNREACHNLRI_TYPE: + pathAttribute = MpUnReachNlri.read(cb); + isMpUnReach = ((MpUnReachNlri) pathAttribute) + .isMpUnReachNlriSet(); + break; + case LINK_STATE_ATTRIBUTE_TYPE: + //TODO: To be merged later + break; + default: + //skip bytes for unsupported attribute types + UnSupportedAttribute.read(cb); + } + pathAttributeList.add(pathAttribute); + } + + checkMandatoryAttr(isOrigin, isAsPath, isNextHop, isMpReach, isMpUnReach); + //TODO:if mp_reach or mp_unreach not present ignore the packet + return new BgpPathAttributes(pathAttributeList); + } + + /** + * Checks mandatory attributes are presents, if not present throws exception. + * + * @param isOrigin say whether origin attribute is present + * @param isAsPath say whether aspath attribute is present + * @param isNextHop say whether nexthop attribute is present + * @param isMpReach say whether mpreach attribute is present + * @param isMpUnReach say whether mpunreach attribute is present + * @throws BgpParseException if mandatory path attribute is not present + */ + public static void checkMandatoryAttr(boolean isOrigin, boolean isAsPath, + boolean isNextHop, boolean isMpReach, boolean isMpUnReach) + throws BgpParseException { + // Mandatory attributes validation not required for MP_UNREACH + if (isMpUnReach) { + return; + } + + if (!isOrigin) { + log.debug("Mandatory Attributes not Present"); + Validation.validateType(BgpErrorType.UPDATE_MESSAGE_ERROR, + BgpErrorType.MISSING_WELLKNOWN_ATTRIBUTE, + Origin.ORIGIN_TYPE); + } + if (!isAsPath) { + log.debug("Mandatory Attributes not Present"); + Validation.validateType(BgpErrorType.UPDATE_MESSAGE_ERROR, + BgpErrorType.MISSING_WELLKNOWN_ATTRIBUTE, + AsPath.ASPATH_TYPE); + } + if (!isMpUnReach && !isMpReach && !isNextHop) { + log.debug("Mandatory Attributes not Present"); + Validation.validateType(BgpErrorType.UPDATE_MESSAGE_ERROR, + BgpErrorType.MISSING_WELLKNOWN_ATTRIBUTE, + NextHop.NEXTHOP_TYPE); + } + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(getClass()) + .add("pathAttribute", pathAttribute) + .toString(); + } +} diff --git a/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/BgpUpdateMsgVer4.java b/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/BgpUpdateMsgVer4.java new file mode 100644 index 00000000..4d6af594 --- /dev/null +++ b/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/BgpUpdateMsgVer4.java @@ -0,0 +1,285 @@ +/* + * 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.List; + +import org.jboss.netty.buffer.ChannelBuffer; +import org.onlab.packet.IpPrefix; +import org.onosproject.bgpio.exceptions.BgpParseException; +import org.onosproject.bgpio.protocol.BgpMessageReader; +import org.onosproject.bgpio.protocol.BgpType; +import org.onosproject.bgpio.protocol.BgpUpdateMsg; +import org.onosproject.bgpio.types.BgpErrorType; +import org.onosproject.bgpio.types.BgpHeader; +import org.onosproject.bgpio.util.Validation; +import org.onosproject.bgpio.protocol.BgpVersion; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.base.MoreObjects; + +/** + * BGP Update Message: UPDATE messages are used to transfer routing information + * between BGP peers. The information in the UPDATE message is used by core to + * construct a graph + */ +public class BgpUpdateMsgVer4 implements BgpUpdateMsg { + + /* 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 | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | Withdrawn Routes Length (2 octets) | + +-----------------------------------------------------+ + | Withdrawn Routes (variable) | + +-----------------------------------------------------+ + | Total Path Attribute Length (2 octets) | + +-----------------------------------------------------+ + | Path Attributes (variable) | + +-----------------------------------------------------+ + | Network Layer Reachability Information (variable) | + +-----------------------------------------------------+ + REFERENCE : RFC 4271 + */ + + protected static final Logger log = LoggerFactory + .getLogger(BgpUpdateMsgVer4.class); + + public static final byte PACKET_VERSION = 4; + //Withdrawn Routes Length(2) + Total Path Attribute Length(2) + public static final int PACKET_MINIMUM_LENGTH = 4; + public static final int BYTE_IN_BITS = 8; + public static final int MIN_LEN_AFTER_WITHDRW_ROUTES = 2; + public static final int MINIMUM_COMMON_HEADER_LENGTH = 19; + public static final BgpType MSG_TYPE = BgpType.UPDATE; + public static final BgpUpdateMsgVer4.Reader READER = new Reader(); + + private List<IpPrefix> withdrawnRoutes; + private BgpPathAttributes bgpPathAttributes; + private BgpHeader bgpHeader; + private List<IpPrefix> nlri; + + /** + * Constructor to initialize parameters for BGP Update message. + * + * @param bgpHeader in Update message + * @param withdrawnRoutes withdrawn routes + * @param bgpPathAttributes BGP Path attributes + * @param nlri Network Layer Reachability Information + */ + public BgpUpdateMsgVer4(BgpHeader bgpHeader, List<IpPrefix> withdrawnRoutes, + BgpPathAttributes bgpPathAttributes, List<IpPrefix> nlri) { + this.bgpHeader = bgpHeader; + this.withdrawnRoutes = withdrawnRoutes; + this.bgpPathAttributes = bgpPathAttributes; + this.nlri = nlri; + } + + /** + * Reader reads BGP Update Message from the channel buffer. + */ + static class Reader implements BgpMessageReader<BgpUpdateMsg> { + + @Override + public BgpUpdateMsg readFrom(ChannelBuffer cb, BgpHeader bgpHeader) + throws BgpParseException { + + if (cb.readableBytes() != (bgpHeader.getLength() - MINIMUM_COMMON_HEADER_LENGTH)) { + Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, + BgpErrorType.BAD_MESSAGE_LENGTH, bgpHeader.getLength()); + } + + LinkedList<IpPrefix> withDrwRoutes = new LinkedList<>(); + LinkedList<IpPrefix> nlri = new LinkedList<>(); + BgpPathAttributes bgpPathAttributes = new BgpPathAttributes(); + // Reading Withdrawn Routes Length + Short withDrwLen = cb.readShort(); + + if (cb.readableBytes() < withDrwLen) { + Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, + BgpErrorType.MALFORMED_ATTRIBUTE_LIST, + cb.readableBytes()); + } + ChannelBuffer tempCb = cb.readBytes(withDrwLen); + if (withDrwLen != 0) { + // Parsing WithdrawnRoutes + withDrwRoutes = parseWithdrawnRoutes(tempCb); + } + if (cb.readableBytes() < MIN_LEN_AFTER_WITHDRW_ROUTES) { + log.debug("Bgp Path Attribute len field not present"); + throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR, + BgpErrorType.MALFORMED_ATTRIBUTE_LIST, null); + } + + // Reading Total Path Attribute Length + short totPathAttrLen = cb.readShort(); + int len = withDrwLen + totPathAttrLen + PACKET_MINIMUM_LENGTH; + if (len > bgpHeader.getLength()) { + throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR, + BgpErrorType.MALFORMED_ATTRIBUTE_LIST, null); + } + if (totPathAttrLen != 0) { + // Parsing BGPPathAttributes + if (cb.readableBytes() < totPathAttrLen) { + Validation + .validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, + BgpErrorType.MALFORMED_ATTRIBUTE_LIST, + cb.readableBytes()); + } + tempCb = cb.readBytes(totPathAttrLen); + bgpPathAttributes = BgpPathAttributes.read(tempCb); + } + if (cb.readableBytes() > 0) { + // Parsing NLRI + nlri = parseNlri(cb); + } + return new BgpUpdateMsgVer4(bgpHeader, withDrwRoutes, + bgpPathAttributes, nlri); + } + } + + /** + * Parses NLRI from channel buffer. + * + * @param cb channelBuffer + * @return list of IP Prefix + * @throws BgpParseException while parsing NLRI + */ + public static LinkedList<IpPrefix> parseNlri(ChannelBuffer cb) + throws BgpParseException { + LinkedList<IpPrefix> nlri = new LinkedList<>(); + while (cb.readableBytes() > 0) { + int length = cb.readByte(); + IpPrefix ipPrefix; + if (length == 0) { + byte[] prefix = new byte[] {0}; + ipPrefix = Validation.bytesToPrefix(prefix, length); + nlri.add(ipPrefix); + } else { + int len = length / BYTE_IN_BITS; + int reminder = length % BYTE_IN_BITS; + if (reminder > 0) { + len = len + 1; + } + if (cb.readableBytes() < len) { + Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, + BgpErrorType.MALFORMED_ATTRIBUTE_LIST, + cb.readableBytes()); + } + byte[] prefix = new byte[len]; + cb.readBytes(prefix, 0, len); + ipPrefix = Validation.bytesToPrefix(prefix, length); + nlri.add(ipPrefix); + } + } + return nlri; + } + + /** + * Parsing withdrawn routes from channel buffer. + * + * @param cb channelBuffer + * @return list of IP prefix + * @throws BgpParseException while parsing withdrawn routes + */ + public static LinkedList<IpPrefix> parseWithdrawnRoutes(ChannelBuffer cb) + throws BgpParseException { + LinkedList<IpPrefix> withDrwRoutes = new LinkedList<>(); + while (cb.readableBytes() > 0) { + int length = cb.readByte(); + IpPrefix ipPrefix; + if (length == 0) { + byte[] prefix = new byte[] {0}; + ipPrefix = Validation.bytesToPrefix(prefix, length); + withDrwRoutes.add(ipPrefix); + } else { + int len = length / BYTE_IN_BITS; + int reminder = length % BYTE_IN_BITS; + if (reminder > 0) { + len = len + 1; + } + if (cb.readableBytes() < len) { + Validation + .validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, + BgpErrorType.MALFORMED_ATTRIBUTE_LIST, + cb.readableBytes()); + } + byte[] prefix = new byte[len]; + cb.readBytes(prefix, 0, len); + ipPrefix = Validation.bytesToPrefix(prefix, length); + withDrwRoutes.add(ipPrefix); + } + } + return withDrwRoutes; + } + + @Override + public BgpVersion getVersion() { + return BgpVersion.BGP_4; + } + + @Override + public BgpType getType() { + return BgpType.UPDATE; + } + + @Override + public void writeTo(ChannelBuffer channelBuffer) throws BgpParseException { + //Not to be implemented as of now + } + + @Override + public BgpPathAttributes bgpPathAttributes() { + return this.bgpPathAttributes; + } + + @Override + public List<IpPrefix> withdrawnRoutes() { + return withdrawnRoutes; + } + + @Override + public List<IpPrefix> nlri() { + return nlri; + } + + @Override + public BgpHeader getHeader() { + return this.bgpHeader; + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(getClass()) + .omitNullValues() + .add("bgpHeader", bgpHeader) + .add("withDrawnRoutes", withdrawnRoutes) + .add("nlri", nlri) + .add("bgpPathAttributes", bgpPathAttributes) + .toString(); + } +} diff --git a/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/package-info.java b/framework/src/onos/protocols/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/protocols/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 |