ONOS-2739 - OSPF Basic Packet Structures , which includes encoding and decoding
Change-Id: I4bf4b7eb26a0e2b5006b41b24d67c7f21450b11b
diff --git a/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/exceptions/OspfErrorType.java b/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/exceptions/OspfErrorType.java
new file mode 100644
index 0000000..1aacb07
--- /dev/null
+++ b/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/exceptions/OspfErrorType.java
@@ -0,0 +1,38 @@
+/*
+ * Copyright 2016 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.ospf.exceptions;
+
+/**
+ * Defines all error codes and error sub codes.
+ */
+public final class OspfErrorType {
+
+ //Represents an invalid OSPF message header
+ public static final byte MESSAGE_HEADER_ERROR = 1;
+ //Represents an invalid OSPF message body
+ public static final byte OSPF_MESSAGE_ERROR = 2;
+ //Message Header error sub codes
+ //Represents an invalid OSPF message length
+ public static final byte BAD_MESSAGE_LENGTH = 2;
+ //Represents an invalid OSPF message
+ public static final byte BAD_MESSAGE = 4;
+
+ /**
+ * Creates an instance of OSPF error type.
+ */
+ private OspfErrorType() {
+ }
+}
\ No newline at end of file
diff --git a/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/exceptions/OspfParseException.java b/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/exceptions/OspfParseException.java
new file mode 100644
index 0000000..24e8703
--- /dev/null
+++ b/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/exceptions/OspfParseException.java
@@ -0,0 +1,102 @@
+/*
+ * 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.ospf.exceptions;
+
+import com.google.common.base.MoreObjects;
+
+/**
+ * Representation of a custom exception for OSPF.
+ */
+public class OspfParseException extends Exception {
+
+ private static final long serialVersionUID = 1L;
+ private byte errorCode;
+ private byte errorSubCode;
+
+ /**
+ * Creates a new OSPF exception.
+ */
+ public OspfParseException() {
+ super();
+ }
+
+ /**
+ * Creates a new OSPF exception based on the given arguments.
+ *
+ * @param message the detail of exception in string
+ * @param cause underlying cause of the error
+ */
+ public OspfParseException(final String message, final Throwable cause) {
+ super(message, cause);
+ }
+
+ /**
+ * Creates a new OSPF exception for the given message.
+ *
+ * @param message the detail of exception in string
+ */
+ public OspfParseException(final String message) {
+ super(message);
+ }
+
+ /**
+ * Creates a new OSPF exception from throwable instance.
+ *
+ * @param cause underlying cause of the error
+ */
+ public OspfParseException(final Throwable cause) {
+ super(cause);
+ }
+
+ /**
+ * Creates a new OSPF exception from error code and error sub code.
+ *
+ * @param errorCode error code of OSPF message
+ * @param errorSubCode error sub code of OSPF message
+ */
+ public OspfParseException(final byte errorCode, final byte errorSubCode) {
+ super();
+ this.errorCode = errorCode;
+ this.errorSubCode = errorSubCode;
+ }
+
+ /**
+ * Returns error code for this exception.
+ *
+ * @return error code for this exception
+ */
+ public byte errorCode() {
+ return this.errorCode;
+ }
+
+ /**
+ * Returns error sub code for this exception.
+ *
+ * @return error sub code for this exception
+ */
+ public byte errorSubCode() {
+ return this.errorSubCode;
+ }
+
+ @Override
+ public String toString() {
+ return MoreObjects.toStringHelper(getClass())
+ .omitNullValues()
+ .add("errorCode", errorCode)
+ .add("errorSubCode", errorSubCode)
+ .toString();
+ }
+}
\ No newline at end of file
diff --git a/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/exceptions/package-info.java b/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/exceptions/package-info.java
new file mode 100644
index 0000000..d74c2be
--- /dev/null
+++ b/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/exceptions/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * Implementation of the OSPF exception types.
+ */
+package org.onosproject.ospf.exceptions;
\ No newline at end of file
diff --git a/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/package-info.java b/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/package-info.java
new file mode 100644
index 0000000..f41cd9b
--- /dev/null
+++ b/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * Implementation of the ospf protocol.
+ */
+package org.onosproject.ospf;
\ No newline at end of file
diff --git a/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/lsa/LsaHeader.java b/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/lsa/LsaHeader.java
new file mode 100644
index 0000000..94faad4
--- /dev/null
+++ b/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/lsa/LsaHeader.java
@@ -0,0 +1,316 @@
+/*
+ * Copyright 2016 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.ospf.protocol.lsa;
+
+import com.google.common.base.MoreObjects;
+import com.google.common.base.Objects;
+import com.google.common.primitives.Bytes;
+import org.onlab.packet.Ip4Address;
+import org.onosproject.ospf.controller.OspfLsa;
+import org.onosproject.ospf.controller.OspfLsaType;
+import org.onosproject.ospf.protocol.util.OspfUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.net.InetAddress;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Defines the LSA header, fields and the methods to access them.
+ */
+public class LsaHeader implements OspfLsa {
+
+ /*
+ 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
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ | LS age | Options | LS type |
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ | Link State ID |
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ | Advertising Router |
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ | LS sequence number |
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ | LS checksum | length |
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ LSA header format
+ REFERENCE : RFC 2328
+ */
+ protected static final Logger log = LoggerFactory.getLogger(LsaHeader.class);
+ private int age;
+ private int options;
+ private int lsType;
+ private long lsSequenceNo;
+ private int lsCheckSum;
+ private int lsPacketLen;
+ private String linkStateId;
+ private Ip4Address advertisingRouter;
+
+ /**
+ * Gets LSA age.
+ *
+ * @return LSA age
+ */
+ public int age() {
+ return age;
+ }
+
+ /**
+ * Sets LSA age.
+ *
+ * @param age LSA age
+ */
+ public void setAge(int age) {
+ this.age = age;
+ }
+
+ /**
+ * Gets options value.
+ *
+ * @return options header value
+ */
+ public int options() {
+ return options;
+ }
+
+ /**
+ * Sets options header value.
+ *
+ * @param options header value
+ */
+ public void setOptions(int options) {
+ this.options = options;
+ }
+
+ /**
+ * Gets LSA type.
+ *
+ * @return LSA type
+ */
+ public int lsType() {
+ return lsType;
+ }
+
+ /**
+ * Sets LSA type.
+ *
+ * @param lsType LSA type
+ */
+ public void setLsType(int lsType) {
+ this.lsType = lsType;
+ }
+
+ /**
+ * Gets link state id.
+ *
+ * @return linkStateId link state id
+ */
+ public String linkStateId() {
+ return linkStateId;
+ }
+
+ /**
+ * Sets link state id.
+ *
+ * @param linkStateId link state id
+ */
+ public void setLinkStateId(String linkStateId) {
+ this.linkStateId = linkStateId;
+ }
+
+ /**
+ * Gets advertising router IP.
+ *
+ * @return advertising router
+ */
+ public Ip4Address advertisingRouter() {
+ return advertisingRouter;
+ }
+
+ /**
+ * Sets advertising router.
+ *
+ * @param advertisingRouter advertising router
+ */
+ public void setAdvertisingRouter(Ip4Address advertisingRouter) {
+ this.advertisingRouter = advertisingRouter;
+ }
+
+ /**
+ * Gets LSA sequence number.
+ *
+ * @return LSA sequence number
+ */
+ public long lsSequenceNo() {
+ return lsSequenceNo;
+ }
+
+ /**
+ * Sets LSA sequence number.
+ *
+ * @param lsSequenceNo LSA sequence number
+ */
+ public void setLsSequenceNo(long lsSequenceNo) {
+ this.lsSequenceNo = lsSequenceNo;
+ }
+
+ /**
+ * Gets LSA check sum.
+ *
+ * @return lsCheckSum LSA checksum
+ */
+ public int lsCheckSum() {
+ return lsCheckSum;
+ }
+
+ /**
+ * Sets LSA checksum.
+ *
+ * @param lsCheckSum LSA checksum
+ */
+ public void setLsCheckSum(int lsCheckSum) {
+ this.lsCheckSum = lsCheckSum;
+ }
+
+ /**
+ * Gets lsa packet length.
+ *
+ * @return lsPacketLen LSA packet length
+ */
+ public int lsPacketLen() {
+ return lsPacketLen;
+ }
+
+ /**
+ * Sets LSA packet length.
+ *
+ * @param lsPacketLen LSA packet length
+ */
+ public void setLsPacketLen(int lsPacketLen) {
+ this.lsPacketLen = lsPacketLen;
+ }
+
+ @Override
+ public OspfLsaType getOspfLsaType() {
+ if (lsType == OspfLsaType.ROUTER.value()) {
+ return OspfLsaType.ROUTER;
+ } else if (lsType == OspfLsaType.NETWORK.value()) {
+ return OspfLsaType.NETWORK;
+ } else if (lsType == OspfLsaType.SUMMARY.value()) {
+ return OspfLsaType.SUMMARY;
+ } else if (lsType == OspfLsaType.ASBR_SUMMARY.value()) {
+ return OspfLsaType.ASBR_SUMMARY;
+ } else if (lsType == OspfLsaType.EXTERNAL_LSA.value()) {
+ return OspfLsaType.EXTERNAL_LSA;
+ } else if (lsType == OspfLsaType.LINK_LOCAL_OPAQUE_LSA.value()) {
+ return OspfLsaType.LINK_LOCAL_OPAQUE_LSA;
+ } else if (lsType == OspfLsaType.AREA_LOCAL_OPAQUE_LSA.value()) {
+ return OspfLsaType.AREA_LOCAL_OPAQUE_LSA;
+ } else if (lsType == OspfLsaType.AS_OPAQUE_LSA.value()) {
+ return OspfLsaType.AS_OPAQUE_LSA;
+ }
+
+ return OspfLsaType.UNDEFINED;
+ }
+
+ @Override
+ public OspfLsa lsaHeader() {
+ return this;
+ }
+
+ /**
+ * Gets the LSA header as bytes.
+ *
+ * @return LSA header as bytes
+ */
+ public byte[] getLsaHeaderAsByteArray() {
+ List<Byte> headerLst = new ArrayList<>();
+ try {
+ headerLst.addAll(Bytes.asList(OspfUtil.convertToTwoBytes(this.age())));
+ headerLst.add((byte) this.options());
+ headerLst.add((byte) this.lsType());
+ headerLst.addAll(Bytes.asList(InetAddress.getByName(this.linkStateId()).getAddress()));
+ headerLst.addAll(Bytes.asList(this.advertisingRouter().toOctets()));
+ headerLst.addAll(Bytes.asList(OspfUtil.convertToFourBytes(this.lsSequenceNo())));
+ headerLst.addAll(Bytes.asList(OspfUtil.convertToTwoBytes(this.lsCheckSum())));
+ headerLst.addAll(Bytes.asList(OspfUtil.convertToTwoBytes(this.lsPacketLen())));
+ } catch (Exception e) {
+ log.debug("Error::getLsaHeaderAsByteArray {}", e.getMessage());
+ return Bytes.toArray(headerLst);
+ }
+ return Bytes.toArray(headerLst);
+ }
+
+ /**
+ * Populates the header from the LSA header instance.
+ *
+ * @param lsaHeader LSA header instance
+ */
+ public void populateHeader(LsaHeader lsaHeader) {
+ //assign all the header values
+ this.setAge(lsaHeader.age());
+ this.setOptions(lsaHeader.options());
+ this.setLsType(lsaHeader.lsType());
+ this.setLinkStateId(lsaHeader.linkStateId());
+ this.setAdvertisingRouter(lsaHeader.advertisingRouter());
+ this.setLsSequenceNo(lsaHeader.lsSequenceNo());
+ this.setLsCheckSum(lsaHeader.lsCheckSum());
+ this.setLsPacketLen(lsaHeader.lsPacketLen());
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) {
+ return true;
+ }
+ if (o == null || getClass() != o.getClass()) {
+ return false;
+ }
+ LsaHeader that = (LsaHeader) o;
+ return Objects.equal(age, that.age) &&
+ Objects.equal(options, that.options) &&
+ Objects.equal(lsType, that.lsType) &&
+ Objects.equal(lsSequenceNo, that.lsSequenceNo) &&
+ Objects.equal(lsCheckSum, that.lsCheckSum) &&
+ Objects.equal(lsPacketLen, that.lsPacketLen) &&
+ Objects.equal(linkStateId, that.linkStateId) &&
+ Objects.equal(advertisingRouter, that.advertisingRouter);
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hashCode(age, options, lsType, lsSequenceNo, lsCheckSum,
+ lsPacketLen, linkStateId, advertisingRouter);
+ }
+
+ @Override
+ public String toString() {
+ return MoreObjects.toStringHelper(getClass())
+ .omitNullValues()
+ .add("age", age)
+ .add("options", options)
+ .add("lsType", lsType)
+ .add("lsSequenceNo", lsSequenceNo)
+ .add("lsCheckSum", lsCheckSum)
+ .add("lsPacketLen", lsPacketLen)
+ .add("linkStateId;", linkStateId)
+ .add("advertisingRouter", advertisingRouter)
+ .toString();
+ }
+}
\ No newline at end of file
diff --git a/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/lsa/OpaqueLsaHeader.java b/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/lsa/OpaqueLsaHeader.java
new file mode 100644
index 0000000..ac27445
--- /dev/null
+++ b/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/lsa/OpaqueLsaHeader.java
@@ -0,0 +1,163 @@
+/*
+ * Copyright 2016 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.ospf.protocol.lsa;
+
+import com.google.common.base.MoreObjects;
+import com.google.common.base.Objects;
+import com.google.common.primitives.Bytes;
+import org.onosproject.ospf.protocol.util.OspfUtil;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Defines the Opaque LSA header, fields and the methods to access them.
+ */
+public class OpaqueLsaHeader extends LsaHeader {
+ /*
+ 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
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ | LS age | Options | 9, 10, or 11 |
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ | Opaque Type | Opaque ID |
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ | Advertising Router |
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ | LS Sequence Number |
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ | LS checksum | Length |
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ Opaque LSA header format
+ REFERENCE : RFC 5250
+ */
+ private int opaqueId;
+ private int opaqueType;
+
+ /**
+ * Populates the header from the lsaHeader instance.
+ *
+ * @param lsaHeader lsa header instance.
+ */
+ public void populateHeader(OpaqueLsaHeader lsaHeader) {
+ //assign all the header values
+ this.setAge(lsaHeader.age());
+ this.setOptions(lsaHeader.options());
+ this.setLsType(lsaHeader.lsType());
+ this.setLinkStateId(lsaHeader.linkStateId());
+ this.setAdvertisingRouter(lsaHeader.advertisingRouter());
+ this.setLsSequenceNo(lsaHeader.lsSequenceNo());
+ this.setLsCheckSum(lsaHeader.lsCheckSum());
+ this.setLsPacketLen(lsaHeader.lsPacketLen());
+ this.setOpaqueId(lsaHeader.opaqueId());
+ this.setOpaqueType(lsaHeader.opaqueType());
+ }
+
+ /**
+ * Gets the opaque id.
+ *
+ * @return opaque id
+ */
+ public int opaqueId() {
+ return opaqueId;
+ }
+
+ /**
+ * Sets the opaque id.
+ *
+ * @param opaqueId opaque id
+ */
+ public void setOpaqueId(int opaqueId) {
+ this.opaqueId = opaqueId;
+ }
+
+ /**
+ * Gets opaque type.
+ *
+ * @return opaque type
+ */
+ public int opaqueType() {
+ return opaqueType;
+ }
+
+ /**
+ * Sets opaque type.
+ *
+ * @param opaqueType opaque type
+ */
+ public void setOpaqueType(int opaqueType) {
+ this.opaqueType = opaqueType;
+ }
+
+ /**
+ * Gets header as byte array.
+ *
+ * @return header as byte array
+ */
+ public byte[] getOpaqueLsaHeaderAsByteArray() {
+ List<Byte> headerLst = new ArrayList<>();
+ try {
+ headerLst.addAll(Bytes.asList(OspfUtil.convertToTwoBytes(this.age())));
+ headerLst.add((byte) this.options());
+ headerLst.add((byte) this.lsType());
+ headerLst.add((byte) this.opaqueType());
+ headerLst.addAll(Bytes.asList(OspfUtil.convertToThreeBytes(this.opaqueId())));
+ headerLst.addAll(Bytes.asList(this.advertisingRouter().toOctets()));
+ headerLst.addAll(Bytes.asList(OspfUtil.convertToFourBytes(this.lsSequenceNo())));
+ headerLst.addAll(Bytes.asList(OspfUtil.convertToTwoBytes(this.lsCheckSum())));
+ headerLst.addAll(Bytes.asList(OspfUtil.convertToTwoBytes(this.lsPacketLen())));
+ } catch (Exception e) {
+ log.debug("Error::getLsaHeaderAsByteArray {}", e.getMessage());
+ return Bytes.toArray(headerLst);
+ }
+ return Bytes.toArray(headerLst);
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) {
+ return true;
+ }
+ if (o == null || getClass() != o.getClass()) {
+ return false;
+ }
+ OpaqueLsaHeader that = (OpaqueLsaHeader) o;
+ return Objects.equal(opaqueId, that.opaqueId) &&
+ Objects.equal(opaqueType, that.opaqueType) &&
+ Objects.equal(age(), that.age()) &&
+ Objects.equal(options(), that.options()) &&
+ Objects.equal(lsType(), that.lsType()) &&
+ Objects.equal(lsSequenceNo(), that.lsSequenceNo()) &&
+ Objects.equal(lsCheckSum(), that.lsCheckSum()) &&
+ Objects.equal(lsPacketLen(), that.lsPacketLen()) &&
+ Objects.equal(linkStateId(), that.linkStateId()) &&
+ Objects.equal(advertisingRouter(), that.advertisingRouter());
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hashCode(opaqueId, opaqueType);
+ }
+
+ @Override
+ public String toString() {
+ return MoreObjects.toStringHelper(getClass())
+ .omitNullValues()
+ .add("opaqueId", this.opaqueId)
+ .add("opaqueType", opaqueType)
+ .toString();
+ }
+}
\ No newline at end of file
diff --git a/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/lsa/package-info.java b/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/lsa/package-info.java
new file mode 100644
index 0000000..9e1ce7a
--- /dev/null
+++ b/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/lsa/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * Implementation of the OSPF LSA.
+ */
+package org.onosproject.ospf.protocol.lsa;
\ No newline at end of file
diff --git a/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/ospfpacket/OspfMessage.java b/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/ospfpacket/OspfMessage.java
new file mode 100644
index 0000000..e4d4448
--- /dev/null
+++ b/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/ospfpacket/OspfMessage.java
@@ -0,0 +1,70 @@
+/*
+ * Copyright 2016 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.ospf.protocol.ospfpacket;
+
+import org.jboss.netty.buffer.ChannelBuffer;
+import org.onlab.packet.Ip4Address;
+import org.onosproject.ospf.exceptions.OspfParseException;
+import org.onosproject.ospf.protocol.util.OspfPacketType;
+
+/**
+ * Representation of an OSPF message.
+ */
+public interface OspfMessage {
+
+ /**
+ * Returns the type of OSPF message.
+ *
+ * @return OSPF message type
+ */
+ public OspfPacketType ospfMessageType();
+
+ /**
+ * Reads from ChannelBuffer and initializes the type of LSA.
+ *
+ * @param channelBuffer channel buffer instance
+ * @throws OspfParseException might throws exception while parsing buffer
+ */
+ void readFrom(ChannelBuffer channelBuffer) throws OspfParseException;
+
+ /**
+ * Returns OSPFMessage as byte array.
+ *
+ * @return OSPF message as bytes
+ */
+ byte[] asBytes();
+
+ /**
+ * Sets the source IP address.
+ *
+ * @param sourceIp IP address
+ */
+ public void setSourceIp(Ip4Address sourceIp);
+
+ /**
+ * Gets the destination IP address.
+ *
+ * @return destination IP address
+ */
+ public Ip4Address destinationIp();
+
+ /**
+ * Sets destination IP.
+ *
+ * @param destinationIp destination IP address
+ */
+ public void setDestinationIp(Ip4Address destinationIp);
+}
\ No newline at end of file
diff --git a/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/ospfpacket/OspfPacketHeader.java b/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/ospfpacket/OspfPacketHeader.java
new file mode 100644
index 0000000..b9d52a9
--- /dev/null
+++ b/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/ospfpacket/OspfPacketHeader.java
@@ -0,0 +1,290 @@
+/*
+ * Copyright 2016 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.ospf.protocol.ospfpacket;
+
+import com.google.common.base.MoreObjects;
+import org.jboss.netty.buffer.ChannelBuffer;
+import org.onlab.packet.Ip4Address;
+import org.onosproject.ospf.exceptions.OspfParseException;
+import org.onosproject.ospf.protocol.util.OspfPacketType;
+
+/**
+ * Defines the OSPF Packet Header, fields and access methods.
+ * Every OSPF packet starts with a standard 24 byte header.
+ * This header contains all the information necessary to determine whether
+ * the packet should be accepted for further processing
+ */
+public class OspfPacketHeader implements OspfMessage {
+
+ /*
+ 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 # | Type | Packet length |
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ | Router ID |
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ | Area ID |
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ | Checksum | AuType |
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ | Authentication |
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ | Authentication |
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ */
+
+ private int ospfVer;
+ private int ospfType;
+ private int ospfPackLength;
+ private Ip4Address routerId;
+ private Ip4Address areaId;
+ private int checkSum;
+ private int auType;
+ private int authentication;
+ private Ip4Address destinationIp;
+ private Ip4Address sourceIp;
+
+ /**
+ * Gets the source IP.
+ *
+ * @return source IP address
+ */
+ public Ip4Address sourceIp() {
+ return sourceIp;
+ }
+
+ /**
+ * Sets the source IP address.
+ *
+ * @param sourceIp source IP address
+ */
+ public void setSourceIp(Ip4Address sourceIp) {
+ this.sourceIp = sourceIp;
+ }
+
+ @Override
+ public OspfPacketType ospfMessageType() {
+ //default impl
+ return null;
+ }
+
+ @Override
+ public void readFrom(ChannelBuffer channelBuffer) throws OspfParseException {
+ //default impl
+ }
+
+ @Override
+ public byte[] asBytes() {
+ //default impl
+ return new byte[0];
+ }
+
+ /**
+ * Gets OSPF version.
+ *
+ * @return OSPF version
+ */
+ public int ospfVersion() {
+ return ospfVer;
+ }
+
+ /**
+ * Sets OSPF version.
+ *
+ * @param ospfVer OSPF version
+ */
+ public void setOspfVer(int ospfVer) {
+ this.ospfVer = ospfVer;
+ }
+
+ /**
+ * Gets OSPF packet type.
+ *
+ * @return OSPF packet type
+ */
+ public int ospfType() {
+ return ospfType;
+ }
+
+ /**
+ * Sets OSPF packet type.
+ *
+ * @param ospfType packet type
+ */
+ public void setOspftype(int ospfType) {
+ this.ospfType = ospfType;
+ }
+
+ /**
+ * Gets ospf packet length.
+ *
+ * @return OSPF packet length
+ */
+ public int ospfPacLength() {
+ return ospfPackLength;
+ }
+
+ /**
+ * Sets OSPF packet length.
+ *
+ * @param ospfPacLength packet length
+ */
+ public void setOspfPacLength(int ospfPacLength) {
+ this.ospfPackLength = ospfPacLength;
+ }
+
+ /**
+ * Gets router id.
+ *
+ * @return routerId
+ */
+ public Ip4Address routerId() {
+ return routerId;
+ }
+
+ /**
+ * Sets router id.
+ *
+ * @param routerId router id
+ */
+ public void setRouterId(Ip4Address routerId) {
+ this.routerId = routerId;
+ }
+
+ /**
+ * Gets area id.
+ *
+ * @return areaId area id
+ */
+ public Ip4Address areaId() {
+ return areaId;
+ }
+
+ /**
+ * Sets area id.
+ *
+ * @param areaId area id
+ */
+ public void setAreaId(Ip4Address areaId) {
+ this.areaId = areaId;
+ }
+
+ /**
+ * Gets checksum value.
+ *
+ * @return checkSum check sum value
+ */
+ public int checksum() {
+ return checkSum;
+ }
+
+ /**
+ * Sets checksum.
+ *
+ * @param checkSum check sum value
+ */
+ public void setChecksum(int checkSum) {
+ this.checkSum = checkSum;
+ }
+
+ /**
+ * Gets auth type.
+ *
+ * @return authType authentication type
+ */
+ public int authType() {
+ return auType;
+ }
+
+ /**
+ * Sets auth Type.
+ *
+ * @param auType authentication type
+ */
+ public void setAuthType(int auType) {
+ this.auType = auType;
+ }
+
+ /**
+ * Gets authentication.
+ *
+ * @return authentication
+ */
+ public int authentication() {
+ return authentication;
+ }
+
+ /**
+ * Sets authentication.
+ *
+ * @param authentication authentication
+ */
+ public void setAuthentication(int authentication) {
+ this.authentication = authentication;
+ }
+
+ /**
+ * Gets destination IP.
+ *
+ * @return destination IP
+ */
+ public Ip4Address destinationIp() {
+ return destinationIp;
+ }
+
+ /**
+ * Sets destination IP.
+ *
+ * @param destinationIp destination IP
+ */
+ public void setDestinationIp(Ip4Address destinationIp) {
+ this.destinationIp = destinationIp;
+ }
+
+ /**
+ * Populates the header from the packetHeader instance.
+ *
+ * @param ospfPacketHeader packet header instance.
+ */
+ public void populateHeader(OspfPacketHeader ospfPacketHeader) {
+ this.setSourceIp(ospfPacketHeader.sourceIp());
+ this.setOspfVer(ospfPacketHeader.ospfVersion());
+ this.setOspftype(ospfPacketHeader.ospfType());
+ this.setOspfPacLength(ospfPacketHeader.ospfPacLength());
+ this.setRouterId(ospfPacketHeader.routerId());
+ this.setAreaId(ospfPacketHeader.areaId());
+ this.setChecksum(ospfPacketHeader.checksum());
+ this.setAuthType(ospfPacketHeader.authType());
+ this.setAuthentication(ospfPacketHeader.authentication());
+ }
+
+ @Override
+ public String toString() {
+ return MoreObjects.toStringHelper(getClass())
+ .omitNullValues()
+ .add("ospfVersion", ospfVer)
+ .add("ospfType", ospfType)
+ .add("ospfPackLength", ospfPackLength)
+ .add("routerId", routerId)
+ .add("areaId", areaId)
+ .add("checkSum", checkSum)
+ .add("auType", auType)
+ .add("authentication", authentication)
+ .add("destinationIP", destinationIp)
+ .toString();
+ }
+}
\ No newline at end of file
diff --git a/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/ospfpacket/package-info.java b/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/ospfpacket/package-info.java
new file mode 100644
index 0000000..592020d
--- /dev/null
+++ b/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/ospfpacket/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * Implementation of the different types of OSPF Packets.
+ */
+package org.onosproject.ospf.protocol.ospfpacket;
diff --git a/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/package-info.java b/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/package-info.java
new file mode 100644
index 0000000..56164ed
--- /dev/null
+++ b/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * Implementation of the OSPF protocol..
+ */
+package org.onosproject.ospf.protocol;
\ No newline at end of file
diff --git a/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/util/OspfInterfaceState.java b/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/util/OspfInterfaceState.java
new file mode 100644
index 0000000..5b55113
--- /dev/null
+++ b/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/util/OspfInterfaceState.java
@@ -0,0 +1,50 @@
+/*
+ * Copyright 2016 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.ospf.protocol.util;
+
+/**
+ * Representation of an OSPF Interface states.
+ */
+public enum OspfInterfaceState {
+
+ DOWN(1),
+ LOOPBACK(2),
+ WAITING(3),
+ POINT2POINT(4),
+ DROTHER(5),
+ BDR(6),
+ DR(7);
+
+ private int value;
+
+ /**
+ * Creates an instance of Interface State.
+ *
+ * @param value Interface State value
+ */
+ OspfInterfaceState(int value) {
+ this.value = value;
+ }
+
+ /**
+ * Gets value for Interface State.
+ *
+ * @return value Interface State
+ */
+ public int value() {
+ return value;
+ }
+}
\ No newline at end of file
diff --git a/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/util/OspfPacketType.java b/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/util/OspfPacketType.java
new file mode 100644
index 0000000..636d1b6
--- /dev/null
+++ b/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/util/OspfPacketType.java
@@ -0,0 +1,48 @@
+/*
+ * Copyright 2016 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.ospf.protocol.util;
+
+/**
+ * Representation of different OSPF packet types.
+ */
+public enum OspfPacketType {
+
+ HELLO(1),
+ DD(2),
+ LSREQUEST(3),
+ LSUPDATE(4),
+ LSAACK(5);
+
+ private int value;
+
+ /**
+ * Creates instance of OSPF packet types.
+ *
+ * @param value
+ */
+ OspfPacketType(int value) {
+ this.value = value;
+ }
+
+ /**
+ * Gets the value.
+ *
+ * @return value
+ */
+ public int value() {
+ return value;
+ }
+}
\ No newline at end of file
diff --git a/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/util/OspfParameters.java b/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/util/OspfParameters.java
new file mode 100644
index 0000000..357a529
--- /dev/null
+++ b/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/util/OspfParameters.java
@@ -0,0 +1,58 @@
+/*
+ * Copyright 2016 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.ospf.protocol.util;
+
+/**
+ * Representation of an OSPF configuration parameters and constants.
+ */
+public final class OspfParameters {
+
+ public static final int LSREFRESHTIME = 1800; //max time between updates;
+ public static final int MINLSINTERVAL = 5; // set to 5 second
+ public static final int MINLSARRIVAL = 1; // set to 1 second
+ public static final int MAXAGE = 3600; // set to 1 hour in seconds
+ public static final int CHECKAGE = 300; // set to 5 mins
+ public static final int MAXAGEDIFF = 900; // set to 15 mins
+ public static final long MAXSEQUENCENUMBER = 2147483647;
+ public static final long STARTLSSEQUENCENUM = -2147483647;
+ public static final int AGECOUNTER = 1;
+ public static final String VERIFYCHECKSUM = "verifyChecksum";
+ public static final String REFRESHLSA = "refreshLsa";
+ public static final String MAXAGELSA = "maxAgeLsa";
+ public static final int START_NOW = 0;
+ public static final int TRAFFIC_ENGINEERING = 1;
+ public static final int INITIAL_BANDWIDTH = 12500000;
+ public static final int ROUTER = 1;
+ public static final int NETWORK = 2;
+ public static final int SUMMARY = 3;
+ public static final int ASBR_SUMMARY = 4;
+ public static final int EXTERNAL_LSA = 5;
+ public static final int LINK_LOCAL_OPAQUE_LSA = 9;
+ public static final int AREA_LOCAL_OPAQUE_LSA = 10;
+ public static final int AS_OPAQUE_LSA = 11;
+ public static final int HELLO = 1;
+ public static final int DD = 2;
+ public static final int LSREQUEST = 3;
+ public static final int LSUPDATE = 4;
+ public static final int LSACK = 5;
+ public static final int INFTRA_NS_DELAY = 1;
+ public static final int BDR = 6;
+ public static final int DR = 7;
+ public static final String OPAQUE_ENABLED_OPTION_VALUE = "01000010";
+
+ private OspfParameters() {
+ }
+}
\ No newline at end of file
diff --git a/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/util/OspfUtil.java b/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/util/OspfUtil.java
new file mode 100644
index 0000000..0185542
--- /dev/null
+++ b/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/util/OspfUtil.java
@@ -0,0 +1,416 @@
+/*
+ * Copyright 2016 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.ospf.protocol.util;
+
+import com.google.common.primitives.Bytes;
+import org.jboss.netty.buffer.ChannelBuffer;
+import org.jboss.netty.buffer.ChannelBuffers;
+import org.onlab.packet.Ip4Address;
+import org.onosproject.ospf.protocol.lsa.LsaHeader;
+import org.onosproject.ospf.protocol.lsa.OpaqueLsaHeader;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.xml.bind.DatatypeConverter;
+import java.net.InetAddress;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.Random;
+import java.util.StringTokenizer;
+
+/**
+ * Representation of an OSPF constants and utility methods.
+ */
+public final class OspfUtil {
+
+ public static final int OSPF_VERSION_2 = 2;
+ public static final int OSPF_VERSION = OSPF_VERSION_2;
+ public static final int PACKET_MINIMUM_LENGTH = 24;
+ public static final int OSPF_HEADER_LENGTH = 24;
+ public static final int LSA_HEADER_LENGTH = 20;
+ public static final int DD_HEADER_LENGTH = OSPF_HEADER_LENGTH + 8;
+ public static final int LSREQUEST_LENGTH = 12;
+ public static final int OSPFPACKET_LENGTH_POS1 = 2;
+ public static final int OSPFPACKET_LENGTH_POS2 = 3;
+ public static final int OSPFPACKET_CHECKSUM_POS1 = 12;
+ public static final int OSPFPACKET_CHECKSUM_POS2 = 13;
+ public static final int LSAPACKET_CHECKSUM_POS1 = 16;
+ public static final int LSAPACKET_CHECKSUM_POS2 = 17;
+ public static final Ip4Address ALL_SPF_ROUTERS = Ip4Address.valueOf("224.0.0.5");
+ public static final Ip4Address ALL_DROUTERS = Ip4Address.valueOf("224.0.0.6");
+ public static final int ONLY_ALL_SPF_ROUTERS = 1;
+ public static final int JOIN_ALL_DROUTERS = 2;
+ public static final int INITIALIZE_SET = 1;
+ public static final int INITIALIZE_NOTSET = 0;
+ public static final int MORE_SET = 1;
+ public static final int MORE_NOTSET = 0;
+ public static final int IS_MASTER = 1;
+ public static final int NOT_MASTER = 0;
+ public static final int NOT_ASSIGNED = 0;
+ public static final int FOUR_BYTES = 4;
+ public static final int EIGHT_BYTES = 8;
+ public static final int TWELVE_BYTES = 12;
+ public static final int EXTERNAL_DESTINATION_LENGTH = 12;
+ private static final Logger log =
+ LoggerFactory.getLogger(OspfUtil.class);
+
+ /**
+ * Creates an instance.
+ */
+ private OspfUtil() {
+
+ }
+
+ /**
+ * Checks given IPs are in same network or not.
+ *
+ * @param ip1 IP address
+ * @param ip2 IP address
+ * @param mask network mask
+ * @return true if both are in same network else false
+ * @throws Exception might throws exception while parsing ip address
+ */
+ public static boolean sameNetwork(Ip4Address ip1, Ip4Address ip2, Ip4Address mask)
+ throws Exception {
+
+ byte[] a1 = ip1.toOctets();
+ byte[] a2 = ip2.toOctets();
+ byte[] m = mask.toOctets();
+
+ for (int i = 0; i < a1.length; i++) {
+ if ((a1[i] & m[i]) != (a2[i] & m[i])) {
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+ /**
+ * Converts IP address to long.
+ *
+ * @param ipAddress IP address
+ * @return long value represents IP address
+ */
+ public static long ipAddressToLong(String ipAddress) {
+ StringTokenizer st = new StringTokenizer(ipAddress, ".");
+ long ipAsNumber = Long.parseLong(st.nextToken()) * (long) Math.pow(256, 3);
+ ipAsNumber += Long.parseLong(st.nextToken()) * (long) Math.pow(256, 2);
+ ipAsNumber += Long.parseLong(st.nextToken()) * 256;
+ ipAsNumber += +Long.parseLong(st.nextToken());
+
+ return ipAsNumber;
+ }
+
+ /**
+ * Checks option field to see whether opaque enabled or not.
+ * 2nd Bit in options field of DdPacket represents Opaque.
+ * 7th bit is external capability.
+ * This method checks Opaque bit is set in the options or not.
+ *
+ * @param options options value
+ * @return true if opaque enabled else false.
+ */
+ public static boolean isOpaqueEnabled(int options) {
+ Boolean[] bits = new Boolean[8];
+ for (int i = 7; i >= 0; i--) {
+ bits[i] = (options & (1 << i)) != 0;
+ }
+
+ List<Boolean> list = Arrays.asList(bits);
+ Collections.reverse(list);
+
+ //2nd bit is Opaque.
+ return list.get(1);
+ }
+
+ /**
+ * Converts a byte to integer variable.
+ *
+ * @param bytesToConvert bytes to convert
+ * @return integer representation of bytes
+ */
+ public static int byteToInteger(byte[] bytesToConvert) {
+ final StringBuilder builder = new StringBuilder();
+ for (byte eachByte : bytesToConvert) {
+ builder.append(String.format("%02x", eachByte));
+ }
+ int num = Integer.parseInt(builder.toString(), 16);
+ return num;
+ }
+
+ /**
+ * Converts a byte to long variable.
+ *
+ * @param bytesToConvert bytes to convert
+ * @return long representation of bytes
+ */
+ public static long byteToLong(byte[] bytesToConvert) {
+ final StringBuilder builder = new StringBuilder();
+ for (byte eachByte : bytesToConvert) {
+ builder.append(String.format("%02x", eachByte));
+ }
+ long num = Long.parseLong(builder.toString(), 16);
+ return num;
+ }
+
+ /**
+ * Creates a random number.
+ *
+ * @return random number
+ */
+ public static int createRandomNumber() {
+ Random rnd = new Random();
+ int randomNumber = 10000000 + rnd.nextInt(90000000);
+ return randomNumber;
+ }
+
+ /**
+ * Reads the LSA header from channel buffer.
+ *
+ * @param channelBuffer channel buffer instance
+ * @return LSA header instance.
+ * @throws Exception might throws exception while parsing buffer
+ */
+ public static LsaHeader readLsaHeader(ChannelBuffer channelBuffer) throws Exception {
+ //add all the LSA Headers - one header is of 20 bytes
+ LsaHeader lsaHeader = null;
+ if (channelBuffer.readableBytes() >= OspfUtil.LSA_HEADER_LENGTH) {
+ byte[] byteArray = new byte[OspfUtil.FOUR_BYTES];
+ channelBuffer.readBytes(byteArray, 0, OspfUtil.FOUR_BYTES);
+ ChannelBuffer tempBuffer = ChannelBuffers.copiedBuffer(byteArray);
+ int lsType = byteArray[3];
+ if (lsType == OspfParameters.AREA_LOCAL_OPAQUE_LSA || lsType == OspfParameters.LINK_LOCAL_OPAQUE_LSA
+ || lsType == OspfParameters.AS_OPAQUE_LSA) {
+ OpaqueLsaHeader header = new OpaqueLsaHeader();
+ header.setAge(tempBuffer.readShort());
+ header.setOptions(tempBuffer.readByte());
+ header.setLsType(tempBuffer.readByte());
+ header.setOpaqueType(channelBuffer.readByte());
+ header.setOpaqueId(channelBuffer.readUnsignedMedium());
+ byte[] tempByteArray = new byte[OspfUtil.FOUR_BYTES];
+ channelBuffer.readBytes(tempByteArray, 0, OspfUtil.FOUR_BYTES);
+ header.setAdvertisingRouter(Ip4Address.valueOf(tempByteArray));
+ header.setLsSequenceNo(channelBuffer.readInt());
+ header.setLsCheckSum(channelBuffer.readUnsignedShort());
+ header.setLsPacketLen(channelBuffer.readShort());
+ byte[] opaqueIdBytes = OspfUtil.convertToTwoBytes(header.opaqueId());
+ header.setLinkStateId(header.opaqueType() + "." + "0" + "." +
+ opaqueIdBytes[0] + "." + opaqueIdBytes[1]);
+ lsaHeader = header;
+ } else {
+ LsaHeader header = new LsaHeader();
+ header.setAge(tempBuffer.readShort());
+ header.setOptions(tempBuffer.readByte());
+ header.setLsType(tempBuffer.readByte());
+ byte[] tempByteArray = new byte[OspfUtil.FOUR_BYTES];
+ channelBuffer.readBytes(tempByteArray, 0, OspfUtil.FOUR_BYTES);
+ header.setLinkStateId(InetAddress.getByAddress(tempByteArray).getHostName());
+ tempByteArray = new byte[OspfUtil.FOUR_BYTES];
+ channelBuffer.readBytes(tempByteArray, 0, OspfUtil.FOUR_BYTES);
+ header.setAdvertisingRouter(Ip4Address.valueOf(tempByteArray));
+ header.setLsSequenceNo(channelBuffer.readInt());
+ header.setLsCheckSum(channelBuffer.readUnsignedShort());
+ header.setLsPacketLen(channelBuffer.readShort());
+ lsaHeader = header;
+ }
+ }
+ return lsaHeader;
+ }
+
+
+ /**
+ * Converts an integer to two bytes.
+ *
+ * @param numberToConvert number to convert
+ * @return given number as bytes
+ */
+ public static byte[] convertToTwoBytes(int numberToConvert) {
+
+ byte[] numInBytes = new byte[2];
+ String s1 = Integer.toHexString(numberToConvert);
+ if (s1.length() % 2 != 0) {
+ s1 = "0" + s1;
+ }
+ byte[] hexas = DatatypeConverter.parseHexBinary(s1);
+ if (hexas.length == 1) {
+ numInBytes[0] = 0;
+ numInBytes[1] = hexas[0];
+ } else {
+ numInBytes[0] = hexas[0];
+ numInBytes[1] = hexas[1];
+ }
+ return numInBytes;
+ }
+
+ /**
+ * Converts a number to three bytes.
+ *
+ * @param numberToConvert number to convert
+ * @return given number as bytes
+ */
+ public static byte[] convertToThreeBytes(int numberToConvert) {
+ byte[] numInBytes = new byte[3];
+ String s1 = Integer.toHexString(numberToConvert);
+ if (s1.length() % 2 != 0) {
+ s1 = "0" + s1;
+ }
+ byte[] hexas = DatatypeConverter.parseHexBinary(s1);
+ if (hexas.length == 1) {
+ numInBytes[0] = 0;
+ numInBytes[1] = 0;
+ numInBytes[2] = hexas[0];
+ } else if (hexas.length == 2) {
+ numInBytes[0] = 0;
+ numInBytes[1] = hexas[0];
+ numInBytes[2] = hexas[1];
+ } else {
+ numInBytes[0] = hexas[0];
+ numInBytes[1] = hexas[1];
+ numInBytes[2] = hexas[2];
+ }
+ return numInBytes;
+ }
+
+ /**
+ * Converts a number to four bytes.
+ *
+ * @param numberToConvert number to convert
+ * @return given number as bytes
+ */
+ public static byte[] convertToFourBytes(int numberToConvert) {
+
+ byte[] numInBytes = new byte[4];
+ String s1 = Integer.toHexString(numberToConvert);
+ if (s1.length() % 2 != 0) {
+ s1 = "0" + s1;
+ }
+ byte[] hexas = DatatypeConverter.parseHexBinary(s1);
+ if (hexas.length == 1) {
+ numInBytes[0] = 0;
+ numInBytes[1] = 0;
+ numInBytes[2] = 0;
+ numInBytes[3] = hexas[0];
+ } else if (hexas.length == 2) {
+ numInBytes[0] = 0;
+ numInBytes[1] = 0;
+ numInBytes[2] = hexas[0];
+ numInBytes[3] = hexas[1];
+ } else if (hexas.length == 3) {
+ numInBytes[0] = 0;
+ numInBytes[1] = hexas[0];
+ numInBytes[2] = hexas[1];
+ numInBytes[3] = hexas[2];
+ } else {
+ numInBytes[0] = hexas[0];
+ numInBytes[1] = hexas[1];
+ numInBytes[2] = hexas[2];
+ numInBytes[3] = hexas[3];
+ }
+ return numInBytes;
+ }
+
+ /**
+ * Converts a number to four bytes.
+ *
+ * @param numberToConvert number to convert
+ * @return given number as bytes
+ */
+ public static byte[] convertToFourBytes(long numberToConvert) {
+
+ byte[] numInBytes = new byte[4];
+ String s1 = Long.toHexString(numberToConvert);
+ if (s1.length() % 2 != 0) {
+ s1 = "0" + s1;
+ }
+ if (s1.length() == 16) {
+ s1 = s1.substring(8, s1.length());
+ }
+ byte[] hexas = DatatypeConverter.parseHexBinary(s1);
+ if (hexas.length == 1) {
+ numInBytes[0] = 0;
+ numInBytes[1] = 0;
+ numInBytes[2] = 0;
+ numInBytes[3] = hexas[0];
+ } else if (hexas.length == 2) {
+ numInBytes[0] = 0;
+ numInBytes[1] = 0;
+ numInBytes[2] = hexas[0];
+ numInBytes[3] = hexas[1];
+ } else if (hexas.length == 3) {
+ numInBytes[0] = 0;
+ numInBytes[1] = hexas[0];
+ numInBytes[2] = hexas[1];
+ numInBytes[3] = hexas[2];
+ } else {
+ numInBytes[0] = hexas[0];
+ numInBytes[1] = hexas[1];
+ numInBytes[2] = hexas[2];
+ numInBytes[3] = hexas[3];
+ }
+ return numInBytes;
+ }
+
+ /**
+ * Adds the checksum and length in packet.
+ *
+ * @param ospfPacket ospf packet
+ * @param lengthBytePos1 length byte position
+ * @param lengthBytePos2 length byte position
+ * @param checksumBytePos1 checksum byte position
+ * @param checksumBytePos2 checksum byte position
+ * @return byte array with checksum and length
+ */
+ public static byte[] addLengthAndCheckSum(byte[] ospfPacket, int lengthBytePos1, int lengthBytePos2,
+ int checksumBytePos1, int checksumBytePos2) {
+ //Set the length of the packet
+ //Get the total length of the packet
+ int length = ospfPacket.length;
+ //Convert the lenth to two bytes as the length field is 2 bytes
+ byte[] lenthInTwoBytes = OspfUtil.convertToTwoBytes(length);
+ //ospf header 3rd and 4th position represents length
+ ospfPacket[lengthBytePos1] = lenthInTwoBytes[0]; //assign 1st byte in lengthBytePos1
+ ospfPacket[lengthBytePos2] = lenthInTwoBytes[1]; //assign 2st byte in lengthBytePos2
+
+ //Get the checksum as two bytes.
+ byte[] checkSumInTwoBytes = new ChecksumCalculator().calculateOspfCheckSum(ospfPacket,
+ checksumBytePos1, checksumBytePos2);
+ ospfPacket[checksumBytePos1] = checkSumInTwoBytes[0]; //assign 1st byte in checksumBytePos1
+ ospfPacket[checksumBytePos2] = checkSumInTwoBytes[1]; //assign 2st byte in checksumBytePos2
+
+ return ospfPacket;
+ }
+
+ /**
+ * Adds metadata to ospf packet like whether to join multi cast group and destination IP.
+ *
+ * @param ospfPacket OSPF packet
+ * @param allDroutersValue whether to join multi cast or not
+ * @param destinationIp destination ip address
+ * @return byte array
+ */
+ public static byte[] addMetadata(byte[] ospfPacket, int allDroutersValue, Ip4Address destinationIp) {
+ byte[] packet;
+ byte[] allDroutersByteVal = {(byte) allDroutersValue};
+ byte[] destIpAsBytes = destinationIp.toOctets();
+ byte[] metadata = Bytes.concat(allDroutersByteVal, destIpAsBytes);
+
+ packet = Bytes.concat(metadata, ospfPacket);
+
+ return packet;
+ }
+}
\ No newline at end of file
diff --git a/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/util/package-info.java b/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/util/package-info.java
new file mode 100644
index 0000000..c3ea5cd
--- /dev/null
+++ b/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/util/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * Implementation of the ospf protocol utilities.
+ */
+package org.onosproject.ospf.protocol.util;
\ No newline at end of file