ONOS-2739 - OSPF Basic Packet Structures , which includes encoding and decoding

Change-Id: Ifaaa4d3640c6a4fec4931db54a1f03100cc9d9e6
diff --git a/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/lsa/subtypes/package-info.java b/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/lsa/subtypes/package-info.java
new file mode 100644
index 0000000..404e681
--- /dev/null
+++ b/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/lsa/subtypes/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 Sub Types.
+ */
+package org.onosproject.ospf.protocol.lsa.subtypes;
\ No newline at end of file
diff --git a/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/lsa/types/OpaqueLsa10.java b/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/lsa/types/OpaqueLsa10.java
new file mode 100644
index 0000000..e9faf3e
--- /dev/null
+++ b/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/lsa/types/OpaqueLsa10.java
@@ -0,0 +1,202 @@
+/*
+ * 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.types;
+
+import com.google.common.base.MoreObjects;
+import com.google.common.base.Objects;
+import com.google.common.primitives.Bytes;
+import org.jboss.netty.buffer.ChannelBuffer;
+import org.onosproject.ospf.controller.OspfLsaType;
+import org.onosproject.ospf.exceptions.OspfErrorType;
+import org.onosproject.ospf.exceptions.OspfParseException;
+import org.onosproject.ospf.protocol.lsa.OpaqueLsaHeader;
+import org.onosproject.ospf.protocol.lsa.TlvHeader;
+import org.onosproject.ospf.protocol.lsa.tlvtypes.LinkTlv;
+import org.onosproject.ospf.protocol.lsa.tlvtypes.OpaqueTopLevelTlvTypes;
+import org.onosproject.ospf.protocol.lsa.tlvtypes.RouterTlv;
+import org.onosproject.ospf.protocol.util.OspfParameters;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Representation of an Opaque LSA of type area local (10).
+ */
+public class OpaqueLsa10 extends OpaqueLsaHeader {
+    /*
+      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 Information                       |
+     +                                                               +
+     |                              ...                              |
+
+        Opaque LSA format
+        REFERENCE : RFC 5250
+    */
+    private List<TopLevelTlv> topLevelValues = new ArrayList<>();
+    private byte[] opaqueInfo = null;
+
+    /**
+     * Creates an instance of Opaque type 10 LSA.
+     *
+     * @param lsaHeader LSA header instance
+     */
+    public OpaqueLsa10(OpaqueLsaHeader lsaHeader) {
+        populateHeader(lsaHeader);
+    }
+
+    /**
+     * Returns the list of top level TLVs.
+     *
+     * @return list of top level TLVs
+     */
+    public List<TopLevelTlv> topLevelValues() {
+        return topLevelValues;
+    }
+
+    /**
+     * Adds TLV value.
+     *
+     * @param value TLV value
+     */
+    public void addValue(TopLevelTlv value) {
+        topLevelValues.add(value);
+    }
+
+    /**
+     * Reads from channel buffer and populate instance.
+     *
+     * @param channelBuffer channelBuffer instance
+     * @throws OspfParseException might throws exception while parsing buffer
+     */
+    public void readFrom(ChannelBuffer channelBuffer) throws OspfParseException {
+
+        try {
+            if (this.opaqueId() == OspfParameters.TRAFFIC_ENGINEERING) {
+                while (channelBuffer.readableBytes() > 0) {
+                    TlvHeader tlvHeader = new TlvHeader();
+                    tlvHeader.setTlvType(channelBuffer.readUnsignedShort());
+                    tlvHeader.setTlvLength(channelBuffer.readUnsignedShort());
+                    if (tlvHeader.tlvType() == OpaqueTopLevelTlvTypes.ROUTER.value()) {
+                        RouterTlv routerTlv = new RouterTlv(tlvHeader);
+                        routerTlv.readFrom(channelBuffer.readBytes(tlvHeader.tlvLength()));
+                        this.addValue(routerTlv);
+                    } else if (tlvHeader.tlvType() == OpaqueTopLevelTlvTypes.LINK.value()) {
+                        LinkTlv linkTlv = new LinkTlv(tlvHeader);
+                        linkTlv.readFrom(channelBuffer.readBytes(tlvHeader.tlvLength()));
+                        this.addValue(linkTlv);
+                    }
+                }
+            } else {
+                int length = channelBuffer.readableBytes();
+                opaqueInfo = new byte[length];
+                channelBuffer.readBytes(opaqueInfo, 0, length);
+            }
+
+        } catch (Exception e) {
+            log.debug("Error::OpaqueLsa10:: {}", e.getMessage());
+            throw new OspfParseException(OspfErrorType.OSPF_MESSAGE_ERROR,
+                                         OspfErrorType.BAD_MESSAGE);
+        }
+    }
+
+    /**
+     * Returns instance as bytes.
+     *
+     * @return instance as bytes
+     * @throws Exception might throws exception while parsing packet
+     */
+    public byte[] asBytes() throws Exception {
+
+        byte[] lsaMessage = null;
+        byte[] lsaHeader = getOpaqueLsaHeaderAsByteArray();
+        byte[] lsaBody = getLsaBodyAsByteArray();
+        lsaMessage = Bytes.concat(lsaHeader, lsaBody);
+        return lsaMessage;
+
+    }
+
+    /**
+     * Gets the LSA body as byte array.
+     *
+     * @return the lsa body as byte array
+     * @throws Exception might throws exception while parsing packet
+     */
+    public byte[] getLsaBodyAsByteArray() throws Exception {
+        List<Byte> bodyLst = new ArrayList<>();
+        if (this.opaqueId() == 1) {
+            for (TopLevelTlv tlv : this.topLevelValues) {
+                //Check the sub type of lsa and build bytes accordingly
+                if (tlv instanceof RouterTlv) {
+                    RouterTlv routerTlv = (RouterTlv) tlv;
+                    bodyLst.addAll(Bytes.asList(routerTlv.asBytes()));
+                } else if (tlv instanceof LinkTlv) {
+                    LinkTlv linkTlv = (LinkTlv) tlv;
+                    bodyLst.addAll(Bytes.asList(linkTlv.asBytes()));
+                }
+            }
+        } else {
+            return opaqueInfo;
+        }
+
+        return Bytes.toArray(bodyLst);
+    }
+
+    @Override
+    public OspfLsaType getOspfLsaType() {
+        return OspfLsaType.AREA_LOCAL_OPAQUE_LSA;
+    }
+
+    @Override
+    public String toString() {
+        return MoreObjects.toStringHelper(getClass())
+                .omitNullValues()
+                .add("topLevelValues", topLevelValues)
+                .add("opaqueInfo", opaqueInfo)
+                .toString();
+    }
+
+    @Override
+    public boolean equals(Object o) {
+        if (this == o) {
+            return true;
+        }
+        if (o == null || getClass() != o.getClass()) {
+            return false;
+        }
+        OpaqueLsa10 that = (OpaqueLsa10) o;
+        return Objects.equal(topLevelValues, that.topLevelValues) &&
+                Objects.equal(opaqueInfo, that.opaqueInfo);
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hashCode(opaqueInfo, topLevelValues);
+    }
+}
\ No newline at end of file
diff --git a/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/lsa/types/RouterLsa.java b/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/lsa/types/RouterLsa.java
new file mode 100644
index 0000000..9ddef5a
--- /dev/null
+++ b/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/lsa/types/RouterLsa.java
@@ -0,0 +1,304 @@
+/*
+ * 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.types;
+
+import com.google.common.base.MoreObjects;
+import com.google.common.base.Objects;
+import com.google.common.primitives.Bytes;
+import org.jboss.netty.buffer.ChannelBuffer;
+import org.onosproject.ospf.exceptions.OspfErrorType;
+import org.onosproject.ospf.exceptions.OspfParseException;
+import org.onosproject.ospf.protocol.lsa.LsaHeader;
+import org.onosproject.ospf.protocol.lsa.subtypes.OspfLsaLink;
+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;
+
+/**
+ * Representation of a Router LSA, and the fields and methods to access them.
+ */
+public class RouterLsa 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   |       1       |
+       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+       |                        Link State ID                          |
+       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+       |                     Advertising Router                        |
+       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+       |                     LS sequence number                        |
+       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+       |         LS checksum           |             length            |
+       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+       |    0    |V|E|B|        0      |            # links            |
+       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+       |                          Link ID                              |
+       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+       |                         Link Data                             |
+       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+       |     Type      |     # TOS     |            metric             |
+       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+       |                              ...                              |
+       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+       |      TOS      |        0      |          TOS  metric          |
+       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+       |                          Link ID                              |
+       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+       |                         Link Data                             |
+       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+       |                              ...                              |
+     */
+    private static final Logger log =
+            LoggerFactory.getLogger(RouterLsa.class);
+    private boolean isVirtualEndPoint;
+    private boolean isAsBoundaryRouter;
+    private boolean isAreaBorderRouter;
+    private int noLink;
+    private List<OspfLsaLink> routerLinks = new ArrayList<>();
+
+    /**
+     * Creates an instance of Router LSA.
+     */
+    public RouterLsa() {
+    }
+
+    /**
+     * Creates an instance of Router LSA.
+     *
+     * @param lsaHeader lsa header instance
+     */
+    public RouterLsa(LsaHeader lsaHeader) {
+        populateHeader(lsaHeader);
+    }
+
+    /**
+     * Sets virtual endpoint or not.
+     *
+     * @param isVirtualEndPoint true or false
+     */
+    public void setVirtualEndPoint(boolean isVirtualEndPoint) {
+        this.isVirtualEndPoint = isVirtualEndPoint;
+    }
+
+    /**
+     * Sets if it is an AS boundary router or not.
+     *
+     * @param isAsBoundaryRouter true if AS boundary router else false
+     */
+    public void setAsBoundaryRouter(boolean isAsBoundaryRouter) {
+        this.isAsBoundaryRouter = isAsBoundaryRouter;
+    }
+
+    /**
+     * Sets whether it is an ABR or not.
+     *
+     * @param isAreaBorderRouter true if ABR else false
+     */
+    public void setAreaBorderRouter(boolean isAreaBorderRouter) {
+        this.isAreaBorderRouter = isAreaBorderRouter;
+    }
+
+    /**
+     * Gets number of links.
+     *
+     * @return number of links
+     */
+    public int noLink() {
+        return noLink;
+    }
+
+    /**
+     * Sets number of links.
+     *
+     * @param noLink number of links
+     */
+    public void setNoLink(int noLink) {
+        this.noLink = noLink;
+    }
+
+
+    /**
+     * Adds router link.
+     *
+     * @param lsaLink LSA link
+     */
+    public void addRouterLink(OspfLsaLink lsaLink) {
+        if (!this.routerLinks.contains(lsaLink)) {
+            this.routerLinks.add(lsaLink);
+        }
+    }
+
+    /**
+     * Gets router link.
+     *
+     * @return routerLinks LSA link list
+     */
+    public List<OspfLsaLink> routerLink() {
+        return this.routerLinks;
+    }
+
+    /**
+     * Reads from channel buffer and populate this.
+     *
+     * @param channelBuffer channelBuffer instance.
+     * @throws OspfParseException might throws exception while parsing buffer
+     */
+    public void readFrom(ChannelBuffer channelBuffer) throws OspfParseException {
+
+        try {
+            int veb = channelBuffer.readByte();
+            int unUsed = channelBuffer.readByte();
+            //Convert the byte to veb bits
+            String strVeb = Integer.toBinaryString(veb);
+            if (strVeb.length() == 3) {
+                this.setVirtualEndPoint((Integer.parseInt(Character.toString(strVeb.charAt(0))) == 1) ? true : false);
+                this.setAsBoundaryRouter((Integer.parseInt(Character.toString(strVeb.charAt(1))) == 1) ? true : false);
+                this.setAreaBorderRouter((Integer.parseInt(Character.toString(strVeb.charAt(2))) == 1) ? true : false);
+            } else if (strVeb.length() == 2) {
+                this.setVirtualEndPoint(false);
+                this.setAsBoundaryRouter((Integer.parseInt(Character.toString(strVeb.charAt(0))) == 1) ? true : false);
+                this.setAreaBorderRouter((Integer.parseInt(Character.toString(strVeb.charAt(1))) == 1) ? true : false);
+            } else if (strVeb.length() == 1) {
+                this.setVirtualEndPoint(false);
+                this.setAsBoundaryRouter(false);
+                this.setAreaBorderRouter((Integer.parseInt(Character.toString(strVeb.charAt(0))) == 1) ? true : false);
+            }
+            this.setNoLink(channelBuffer.readUnsignedShort());
+            while (channelBuffer.readableBytes() >= OspfUtil.TWELVE_BYTES) {
+                OspfLsaLink ospfLsaLink = new OspfLsaLink();
+
+                byte[] tempByteArray = new byte[OspfUtil.FOUR_BYTES];
+                channelBuffer.readBytes(tempByteArray, 0, OspfUtil.FOUR_BYTES);
+                ospfLsaLink.setLinkId(InetAddress.getByAddress(tempByteArray).getHostName());
+                tempByteArray = new byte[OspfUtil.FOUR_BYTES];
+                channelBuffer.readBytes(tempByteArray, 0, OspfUtil.FOUR_BYTES);
+                ospfLsaLink.setLinkData(InetAddress.getByAddress(tempByteArray).getHostName());
+                ospfLsaLink.setLinkType(channelBuffer.readByte());
+                ospfLsaLink.setTos(channelBuffer.readByte());
+                ospfLsaLink.setMetric(channelBuffer.readUnsignedShort());
+                //add the link
+                this.addRouterLink(ospfLsaLink);
+            }
+        } catch (Exception e) {
+            log.debug("Error::RouterLsa:: {}", e.getMessage());
+            throw new OspfParseException(OspfErrorType.OSPF_MESSAGE_ERROR, OspfErrorType.BAD_MESSAGE);
+        }
+    }
+
+    /**
+     * Returns instance as bytes.
+     *
+     * @return instance as bytes
+     * @throws OspfParseException might throws exception while parsing packet
+     */
+    public byte[] asBytes() throws OspfParseException {
+        byte[] lsaMessage = null;
+
+        byte[] lsaHeader = getLsaHeaderAsByteArray();
+        byte[] lsaBody = getLsaBodyAsByteArray();
+        lsaMessage = Bytes.concat(lsaHeader, lsaBody);
+
+        return lsaMessage;
+    }
+
+    /**
+     * Gets the LSA body as bytes.
+     *
+     * @return LSA body as bytes
+     */
+    public byte[] getLsaBodyAsByteArray() {
+        List<Byte> bodyLst = new ArrayList<>();
+
+        try {
+            int isVirtualEndPointVal = this.isVirtualEndPoint ? 1 : 0;
+            int isASBoundaryRouterVal = this.isAsBoundaryRouter ? 1 : 0;
+            int isAreaBorderRouterVal = this.isAreaBorderRouter ? 1 : 0;
+
+            StringBuilder sb = new StringBuilder();
+            sb.append(Integer.toBinaryString(isVirtualEndPointVal));
+            sb.append(Integer.toBinaryString(isASBoundaryRouterVal));
+            sb.append(Integer.toBinaryString(isAreaBorderRouterVal));
+
+            //added VEB
+            bodyLst.add((byte) Integer.parseInt(sb.toString(), 2));
+            //second byte is 0.
+            bodyLst.add((byte) 0);
+            //Number of links
+            bodyLst.addAll(Bytes.asList(OspfUtil.convertToTwoBytes(this.noLink())));
+
+            //add each link details
+            for (OspfLsaLink lsaLink : routerLinks) {
+                bodyLst.addAll(Bytes.asList(InetAddress.getByName(lsaLink.linkId()).getAddress()));
+                bodyLst.addAll(Bytes.asList(InetAddress.getByName(lsaLink.linkData()).getAddress()));
+                bodyLst.add((byte) lsaLink.linkType());
+                bodyLst.add((byte) lsaLink.tos());
+                bodyLst.addAll(Bytes.asList(OspfUtil.convertToTwoBytes(lsaLink.metric())));
+            }
+        } catch (Exception e) {
+            log.debug("Error::getLsrBodyAsByteArray {}", e.getMessage());
+            return Bytes.toArray(bodyLst);
+        }
+
+        return Bytes.toArray(bodyLst);
+    }
+
+    /**
+     * Increment the link by 1.
+     */
+    public void incrementLinkNo() {
+        this.noLink++;
+    }
+
+    @Override
+    public String toString() {
+        return MoreObjects.toStringHelper(getClass())
+                .omitNullValues()
+                .add("isVirtualEndPoint", isVirtualEndPoint)
+                .add("isAsBoundaryRouter", isAsBoundaryRouter)
+                .add("isAreaBorderRouter", isAreaBorderRouter)
+                .add("noLink", noLink)
+                .add("routerLinks", routerLinks)
+                .toString();
+    }
+
+    @Override
+    public boolean equals(Object o) {
+        if (this == o) {
+            return true;
+        }
+        if (o == null || getClass() != o.getClass()) {
+            return false;
+        }
+        RouterLsa that = (RouterLsa) o;
+        return Objects.equal(isVirtualEndPoint, that.isVirtualEndPoint) &&
+                Objects.equal(isAsBoundaryRouter, that.isAsBoundaryRouter) &&
+                Objects.equal(isAreaBorderRouter, that.isAreaBorderRouter) &&
+                Objects.equal(noLink, that.noLink) &&
+                Objects.equal(routerLinks, that.routerLinks);
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hashCode(isVirtualEndPoint, isAsBoundaryRouter, isAreaBorderRouter,
+                                noLink, routerLinks);
+    }
+}
\ No newline at end of file
diff --git a/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/lsa/types/package-info.java b/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/lsa/types/package-info.java
new file mode 100644
index 0000000..00412ac
--- /dev/null
+++ b/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/lsa/types/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 types.
+ */
+package org.onosproject.ospf.protocol.lsa.types;
\ No newline at end of file
diff --git a/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/ospfpacket/OspfMessageReader.java b/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/ospfpacket/OspfMessageReader.java
new file mode 100644
index 0000000..4f26617
--- /dev/null
+++ b/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/ospfpacket/OspfMessageReader.java
@@ -0,0 +1,148 @@
+/*
+ * 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.OspfErrorType;
+import org.onosproject.ospf.exceptions.OspfParseException;
+import org.onosproject.ospf.protocol.ospfpacket.types.DdPacket;
+import org.onosproject.ospf.protocol.ospfpacket.types.HelloPacket;
+import org.onosproject.ospf.protocol.ospfpacket.types.LsAcknowledge;
+import org.onosproject.ospf.protocol.ospfpacket.types.LsRequest;
+import org.onosproject.ospf.protocol.ospfpacket.types.LsUpdate;
+import org.onosproject.ospf.protocol.util.OspfParameters;
+import org.onosproject.ospf.protocol.util.OspfUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * A message reader which reads OSPF messages from ChannelBuffer and converts to OspfMessage instances.
+ */
+public class OspfMessageReader {
+    private static final Logger log = LoggerFactory.getLogger(OspfMessageReader.class);
+
+    /**
+     * Reads and Converts the channel buffer to OspfMessage instance.
+     *
+     * @param channelBuffer channel buffer instance.
+     * @return OSPF message instance.
+     * @throws Exception might throws exception while parsing buffer
+     */
+    public OspfMessage readFromBuffer(ChannelBuffer channelBuffer)
+            throws Exception {
+
+        if (channelBuffer.readableBytes() < OspfUtil.PACKET_MINIMUM_LENGTH) {
+            log.error("Packet should have minimum length...");
+            throw new OspfParseException(OspfErrorType.MESSAGE_HEADER_ERROR, OspfErrorType.BAD_MESSAGE_LENGTH);
+        }
+
+        try {
+            OspfPacketHeader ospfHeader = getOspfHeader(channelBuffer);
+            int len = ospfHeader.ospfPacLength() - OspfUtil.OSPF_HEADER_LENGTH;
+
+            OspfMessage ospfMessage = null;
+            switch (ospfHeader.ospfType()) {
+                case OspfParameters.HELLO:
+                    ospfMessage = new HelloPacket(ospfHeader);
+                    break;
+                case OspfParameters.DD:
+                    ospfMessage = new DdPacket(ospfHeader);
+                    break;
+                case OspfParameters.LSREQUEST:
+                    ospfMessage = new LsRequest(ospfHeader);
+                    break;
+                case OspfParameters.LSUPDATE:
+                    ospfMessage = new LsUpdate(ospfHeader);
+                    break;
+                case OspfParameters.LSACK:
+                    ospfMessage = new LsAcknowledge(ospfHeader);
+                    break;
+                default:
+                    log.debug("Message Reader[Decoder] - Unknown LSA type..!!!");
+                    break;
+            }
+
+            if (ospfMessage != null) {
+                try {
+                    log.debug("{} Received::Message Length :: {} ", ospfMessage.ospfMessageType(),
+                              ospfHeader.ospfPacLength());
+                    ospfMessage.readFrom(channelBuffer.readBytes(len));
+                } catch (Exception e) {
+                    throw new OspfParseException(OspfErrorType.OSPF_MESSAGE_ERROR,
+                                                 OspfErrorType.BAD_MESSAGE);
+                }
+
+            }
+
+            return ospfMessage;
+        } catch (Exception e) {
+            throw new OspfParseException(OspfErrorType.OSPF_MESSAGE_ERROR,
+                                         OspfErrorType.BAD_MESSAGE);
+        }
+    }
+
+    /**
+     * Gets the OSPF packet Header.
+     *
+     * @param channelBuffer channel buffer instance.
+     * @return Ospf Header instance.
+     */
+    private OspfPacketHeader getOspfHeader(ChannelBuffer channelBuffer) throws Exception {
+        OspfPacketHeader ospfPacketHeader = new OspfPacketHeader();
+
+        byte[] sourceIpBytes = new byte[OspfUtil.FOUR_BYTES];
+        channelBuffer.readBytes(sourceIpBytes, 0, OspfUtil.FOUR_BYTES);
+        Ip4Address sourceIP = Ip4Address.valueOf(sourceIpBytes);
+
+        // Determine ospf version & Packet Type
+        int version = channelBuffer.readByte(); //byte 1 is ospf version
+        int packetType = channelBuffer.readByte(); //byte 2 is ospf packet type
+
+        // byte 3 & 4 combine is packet length.
+        int packetLength = channelBuffer.readShort();
+
+        if (packetLength > channelBuffer.readableBytes() + OspfUtil.FOUR_BYTES) {
+            log.error("Packet should have minimum length...");
+            throw new OspfParseException(OspfErrorType.MESSAGE_HEADER_ERROR, OspfErrorType.BAD_MESSAGE_LENGTH);
+        }
+
+        byte[] tempByteArray = new byte[OspfUtil.FOUR_BYTES];
+        channelBuffer.readBytes(tempByteArray, 0, OspfUtil.FOUR_BYTES);
+        Ip4Address routerId = Ip4Address.valueOf(tempByteArray);
+
+        tempByteArray = new byte[OspfUtil.FOUR_BYTES];
+        channelBuffer.readBytes(tempByteArray, 0, OspfUtil.FOUR_BYTES);
+        Ip4Address areaId = Ip4Address.valueOf(tempByteArray);
+
+        int checkSum = channelBuffer.readUnsignedShort();
+        int auType = channelBuffer.readUnsignedShort();
+        int authentication = (int) channelBuffer.readLong();
+
+        ospfPacketHeader.setSourceIp(sourceIP);
+        ospfPacketHeader.setOspfVer(version);
+        ospfPacketHeader.setOspftype(packetType);
+        ospfPacketHeader.setOspfPacLength(packetLength);
+        ospfPacketHeader.setRouterId(routerId);
+        ospfPacketHeader.setAreaId(areaId);
+        ospfPacketHeader.setChecksum(checkSum);
+        ospfPacketHeader.setAuthType(auType);
+        ospfPacketHeader.setAuthentication(authentication);
+
+        return ospfPacketHeader;
+    }
+}
\ No newline at end of file
diff --git a/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/ospfpacket/OspfMessageWriter.java b/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/ospfpacket/OspfMessageWriter.java
new file mode 100644
index 0000000..9782194
--- /dev/null
+++ b/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/ospfpacket/OspfMessageWriter.java
@@ -0,0 +1,91 @@
+/*
+* 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.jboss.netty.buffer.ChannelBuffers;
+import org.onosproject.ospf.protocol.util.OspfParameters;
+import org.onosproject.ospf.protocol.util.OspfUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * A message writer which writes an OspfMessage to ChannelBuffer.
+ */
+public class OspfMessageWriter {
+    private static final Logger log = LoggerFactory.getLogger(OspfMessageWriter.class);
+
+    /**
+     * Writes OSPF message to ChannelBuffer.
+     *
+     * @param ospfMessage    OSPF message
+     * @param interfaceState interface state
+     * @param interfaceType  interface type
+     * @return channelBuffer channel buffer instance
+     * @throws Exception might throws exception while parsing message
+     */
+    public ChannelBuffer writeToBuffer(OspfMessage ospfMessage, int interfaceState,
+                                       int interfaceType) throws Exception {
+
+        ChannelBuffer buf = null;
+        switch (ospfMessage.ospfMessageType().value()) {
+            case OspfParameters.HELLO:
+            case OspfParameters.LSACK:
+                buf = writeMessageToBuffer(ospfMessage, interfaceState);
+                break;
+            case OspfParameters.DD:
+            case OspfParameters.LSREQUEST:
+            case OspfParameters.LSUPDATE:
+                buf = writeMessageToBuffer(ospfMessage, interfaceState);
+                break;
+            default:
+                log.debug("Message Writer[Encoder] - Unknown Message to encode..!!!");
+                break;
+        }
+
+        return buf;
+    }
+
+    /**
+     * Writes an OSPF Message to channel buffer.
+     *
+     * @param ospfMessage    OSPF Message instance
+     * @param interfaceState interface state
+     * @return channelBuffer instance
+     */
+    private ChannelBuffer writeMessageToBuffer(OspfMessage ospfMessage, int interfaceState) throws Exception {
+        ChannelBuffer channelBuffer = null;
+        byte[] ospfMessageAsByte = ospfMessage.asBytes();
+        //Add the length and checksum in byte array at length position 2 & 3 and Checksum position
+        ospfMessageAsByte = OspfUtil.addLengthAndCheckSum(ospfMessageAsByte, OspfUtil.OSPFPACKET_LENGTH_POS1,
+                                                          OspfUtil.OSPFPACKET_LENGTH_POS2,
+                                                          OspfUtil.OSPFPACKET_CHECKSUM_POS1,
+                                                          OspfUtil.OSPFPACKET_CHECKSUM_POS2);
+        //Add Interface State Info and destination IP as metadata
+        if (interfaceState == OspfParameters.DR || interfaceState == OspfParameters.BDR) {
+            ospfMessageAsByte = OspfUtil.addMetadata(ospfMessageAsByte, OspfUtil.JOIN_ALL_DROUTERS,
+                                                     ospfMessage.destinationIp());
+        } else {
+            ospfMessageAsByte = OspfUtil.addMetadata(ospfMessageAsByte, OspfUtil.ONLY_ALL_SPF_ROUTERS,
+                                                     ospfMessage.destinationIp());
+        }
+
+        channelBuffer = ChannelBuffers.buffer(ospfMessageAsByte.length);
+        channelBuffer.writeBytes(ospfMessageAsByte);
+
+        return channelBuffer;
+    }
+}
\ No newline at end of file
diff --git a/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/ospfpacket/types/LsUpdate.java b/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/ospfpacket/types/LsUpdate.java
new file mode 100644
index 0000000..e9c80e2
--- /dev/null
+++ b/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/ospfpacket/types/LsUpdate.java
@@ -0,0 +1,310 @@
+/*
+ * 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.types;
+
+import com.google.common.base.MoreObjects;
+import com.google.common.primitives.Bytes;
+import org.jboss.netty.buffer.ChannelBuffer;
+import org.onosproject.ospf.controller.OspfLsa;
+import org.onosproject.ospf.exceptions.OspfErrorType;
+import org.onosproject.ospf.exceptions.OspfParseException;
+import org.onosproject.ospf.protocol.lsa.LsaHeader;
+import org.onosproject.ospf.protocol.lsa.OpaqueLsaHeader;
+import org.onosproject.ospf.protocol.lsa.types.AsbrSummaryLsa;
+import org.onosproject.ospf.protocol.lsa.types.ExternalLsa;
+import org.onosproject.ospf.protocol.lsa.types.NetworkLsa;
+import org.onosproject.ospf.protocol.lsa.types.OpaqueLsa10;
+import org.onosproject.ospf.protocol.lsa.types.OpaqueLsa11;
+import org.onosproject.ospf.protocol.lsa.types.OpaqueLsa9;
+import org.onosproject.ospf.protocol.lsa.types.RouterLsa;
+import org.onosproject.ospf.protocol.lsa.types.SummaryLsa;
+import org.onosproject.ospf.protocol.ospfpacket.OspfPacketHeader;
+import org.onosproject.ospf.protocol.util.OspfPacketType;
+import org.onosproject.ospf.protocol.util.OspfParameters;
+import org.onosproject.ospf.protocol.util.OspfUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.ArrayList;
+import java.util.LinkedList;
+import java.util.List;
+
+/**
+ * Representation of an OSPF Link State Update packet.
+ * Link State Update packets are OSPF packet type 4.  These packets
+ * implement the flooding of LSAs.  Each Link State Update packet
+ * carries a collection of LSAs one hop further from their origin.
+ * Several LSAs may be included in a single packet.
+ */
+public class LsUpdate extends OspfPacketHeader {
+    /*
+        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 #   |       4       |         Packet length         |
+       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+       |                          Router ID                            |
+       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+       |                           Area ID                             |
+       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+       |           Checksum            |             AuType            |
+       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+       |                       Authentication                          |
+       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+       |                       Authentication                          |
+       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+       |                            # LSAs                             |
+       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+       |                                                               |
+       +-                                                            +-+
+       |                             LSAs                              |
+       +-                                                            +-+
+       |                              ...                              |
+     */
+    private static final Logger log = LoggerFactory.getLogger(LsUpdate.class);
+    private int numberOfLsa;
+    private List<OspfLsa> lsaList = new LinkedList<>();
+
+    /**
+     * Creates an instance of Link State Update packet.
+     */
+    public LsUpdate() {
+    }
+
+    /**
+     * Creates an instance of Link State Update packet.
+     *
+     * @param ospfHeader ospf header instance.
+     */
+    public LsUpdate(OspfPacketHeader ospfHeader) {
+        populateHeader(ospfHeader);
+    }
+
+    /**
+     * Gets the LSA list.
+     *
+     * @return list of LSA
+     */
+    public List getLsaList() {
+        return lsaList;
+    }
+
+    /**
+     * Adds the LSA to list.
+     *
+     * @param lsa LSA
+     */
+    public void addLsa(OspfLsa lsa) {
+        if (!lsaList.contains(lsa)) {
+            lsaList.add(lsa);
+        }
+    }
+
+    /**
+     * Gets the number of LSA.
+     *
+     * @return number of LSA
+     */
+    public int noLsa() {
+        return numberOfLsa;
+    }
+
+    /**
+     * Sets number of LSA.
+     *
+     * @param numberOfLsa number of LSA
+     */
+    public void setNumberOfLsa(int numberOfLsa) {
+        this.numberOfLsa = numberOfLsa;
+    }
+
+
+    @Override
+    public OspfPacketType ospfMessageType() {
+        return OspfPacketType.LSUPDATE;
+    }
+
+    @Override
+    public void readFrom(ChannelBuffer channelBuffer) throws OspfParseException {
+        try {
+            //From header 4 bytes is number of lsa's
+            this.setNumberOfLsa(channelBuffer.readInt());
+            //get the remaining bytes represents Number of LSA's present. Add all the LSA's
+            while (channelBuffer.readableBytes() > OspfUtil.LSA_HEADER_LENGTH) {
+
+                LsaHeader header = OspfUtil.readLsaHeader(channelBuffer.readBytes(OspfUtil.LSA_HEADER_LENGTH));
+                int lsaLength = header.lsPacketLen();
+                int lsType = header.lsType();
+
+                switch (lsType) {
+                    case OspfParameters.LINK_LOCAL_OPAQUE_LSA:
+                        OpaqueLsa9 opaqueLsa9 = new OpaqueLsa9((OpaqueLsaHeader) header);
+                        opaqueLsa9.readFrom(channelBuffer.readBytes(lsaLength - OspfUtil.LSA_HEADER_LENGTH));
+                        addLsa(opaqueLsa9);
+                        break;
+                    case OspfParameters.AREA_LOCAL_OPAQUE_LSA:
+                        OpaqueLsa10 opaqueLsa10 = new OpaqueLsa10((OpaqueLsaHeader) header);
+                        opaqueLsa10.readFrom(channelBuffer.readBytes(lsaLength - OspfUtil.LSA_HEADER_LENGTH));
+                        addLsa(opaqueLsa10);
+                        break;
+                    case OspfParameters.AS_OPAQUE_LSA:
+                        OpaqueLsa11 opaqueLsa11 = new OpaqueLsa11((OpaqueLsaHeader) header);
+                        opaqueLsa11.readFrom(channelBuffer.readBytes(lsaLength - OspfUtil.LSA_HEADER_LENGTH));
+                        addLsa(opaqueLsa11);
+                        break;
+                    case OspfParameters.ROUTER:
+                        RouterLsa routerLsa = new RouterLsa(header);
+                        routerLsa.readFrom(channelBuffer.readBytes(lsaLength - OspfUtil.LSA_HEADER_LENGTH));
+                        addLsa(routerLsa);
+                        break;
+                    case OspfParameters.NETWORK:
+                        NetworkLsa networkLsa = new NetworkLsa(header);
+                        networkLsa.readFrom(channelBuffer.readBytes(lsaLength - OspfUtil.LSA_HEADER_LENGTH));
+                        addLsa(networkLsa);
+                        break;
+                    case OspfParameters.ASBR_SUMMARY:
+                        AsbrSummaryLsa asbrSummaryLsa = new AsbrSummaryLsa(header);
+                        asbrSummaryLsa.readFrom(channelBuffer.readBytes(lsaLength - OspfUtil.LSA_HEADER_LENGTH));
+                        addLsa(asbrSummaryLsa);
+                        break;
+                    case OspfParameters.SUMMARY:
+                        SummaryLsa summaryLSA = new SummaryLsa(header);
+                        summaryLSA.readFrom(channelBuffer.readBytes(lsaLength - OspfUtil.LSA_HEADER_LENGTH));
+                        addLsa(summaryLSA);
+                        break;
+                    case OspfParameters.EXTERNAL_LSA:
+                        ExternalLsa externalLsa = new ExternalLsa(header);
+                        externalLsa.readFrom(channelBuffer.readBytes(lsaLength - OspfUtil.LSA_HEADER_LENGTH));
+                        addLsa(externalLsa);
+                        break;
+                    default:
+                        log.debug("LSUpdate::readLsUpdateBody::UnKnown LS Type: {}", lsType);
+                        break;
+                }
+            }
+        } catch (Exception e) {
+            log.debug("Error::LsUpdate:: {}", e.getMessage());
+            throw new OspfParseException(OspfErrorType.MESSAGE_HEADER_ERROR, OspfErrorType.BAD_MESSAGE_LENGTH);
+        }
+    }
+
+    @Override
+    public byte[] asBytes() {
+        byte[] lsuMessage = null;
+
+        byte[] ospfHeader = getLsuHeaderAsByteArray();
+        byte[] lsuBody = getLsuBodyAsByteArray();
+        lsuMessage = Bytes.concat(ospfHeader, lsuBody);
+
+        return lsuMessage;
+    }
+
+    /**
+     * Gets lsu header.
+     *
+     * @return lsu header as byte array
+     */
+    public byte[] getLsuHeaderAsByteArray() {
+        List<Byte> headerLst = new ArrayList<>();
+        try {
+            headerLst.add((byte) this.ospfVersion());
+            headerLst.add((byte) this.ospfType());
+            headerLst.addAll(Bytes.asList(OspfUtil.convertToTwoBytes(this.ospfPacLength())));
+            headerLst.addAll(Bytes.asList(this.routerId().toOctets()));
+            headerLst.addAll(Bytes.asList(this.areaId().toOctets()));
+            headerLst.addAll(Bytes.asList(OspfUtil.convertToTwoBytes(this.checksum())));
+            headerLst.addAll(Bytes.asList(OspfUtil.convertToTwoBytes(this.authType())));
+            //Authentication is 0 always. Total 8 bytes consist of zero
+            byte[] auth = new byte[OspfUtil.EIGHT_BYTES];
+            headerLst.addAll(Bytes.asList(auth));
+        } catch (Exception e) {
+            log.debug("Error::LSUpdate::getLsuHeaderAsByteArray:: {}", e.getMessage());
+            return Bytes.toArray(headerLst);
+        }
+
+        return Bytes.toArray(headerLst);
+    }
+
+    /**
+     * Get lsu body as byte array.
+     *
+     * @return lsu body as byte array
+     */
+    public byte[] getLsuBodyAsByteArray() {
+        List<Byte> bodyLst = new ArrayList<>();
+
+        try {
+            //add number of LSA's
+            bodyLst.addAll(Bytes.asList(OspfUtil.convertToFourBytes(this.noLsa())));
+            //for each type of LSA's from the list get lsa bytes
+            for (OspfLsa ospfLsa : lsaList) {
+                //Check the type of lsa and build bytes accordingly
+                switch (ospfLsa.getOspfLsaType().value()) {
+                    case OspfParameters.LINK_LOCAL_OPAQUE_LSA:
+                        OpaqueLsa9 opaqueLsa9 = (OpaqueLsa9) ospfLsa;
+                        bodyLst.addAll(Bytes.asList(opaqueLsa9.asBytes()));
+                        break;
+                    case OspfParameters.AREA_LOCAL_OPAQUE_LSA:
+                        OpaqueLsa10 opaqueLsa10 = (OpaqueLsa10) ospfLsa;
+                        bodyLst.addAll(Bytes.asList(opaqueLsa10.asBytes()));
+                        break;
+                    case OspfParameters.AS_OPAQUE_LSA:
+                        OpaqueLsa11 opaqueLsa11 = (OpaqueLsa11) ospfLsa;
+                        bodyLst.addAll(Bytes.asList(opaqueLsa11.asBytes()));
+                        break;
+                    case OspfParameters.ROUTER:
+                        RouterLsa routerLsa = (RouterLsa) ospfLsa;
+                        bodyLst.addAll(Bytes.asList(routerLsa.asBytes()));
+                        break;
+                    case OspfParameters.NETWORK:
+                        NetworkLsa networkLsa = (NetworkLsa) ospfLsa;
+                        bodyLst.addAll(Bytes.asList(networkLsa.asBytes()));
+                        break;
+                    case OspfParameters.ASBR_SUMMARY:
+                        AsbrSummaryLsa asbrSummaryLsa = (AsbrSummaryLsa) ospfLsa;
+                        bodyLst.addAll(Bytes.asList(asbrSummaryLsa.asBytes()));
+                        break;
+                    case OspfParameters.SUMMARY:
+                        SummaryLsa summaryLsa = (SummaryLsa) ospfLsa;
+                        bodyLst.addAll(Bytes.asList(summaryLsa.asBytes()));
+                        break;
+                    case OspfParameters.EXTERNAL_LSA:
+                        ExternalLsa externalLsa = (ExternalLsa) ospfLsa;
+                        bodyLst.addAll(Bytes.asList(externalLsa.asBytes()));
+                        break;
+                    default:
+                        log.debug("LSUpdate::getLsuBodyAsByteArray::UnKnown ospfLsa: {}", ospfLsa);
+                        break;
+                }
+            }
+
+        } catch (Exception e) {
+            log.debug("Error::getLsuBodyAsByteArray {}", e.getMessage());
+            return Bytes.toArray(bodyLst);
+        }
+
+        return Bytes.toArray(bodyLst);
+    }
+
+    @Override
+    public String toString() {
+        return MoreObjects.toStringHelper(getClass())
+                .omitNullValues()
+                .add("noLsa", numberOfLsa)
+                .add("lsaList", lsaList)
+                .toString();
+    }
+}
\ No newline at end of file
diff --git a/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/util/ChecksumCalculator.java b/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/util/ChecksumCalculator.java
new file mode 100644
index 0000000..306ad1b
--- /dev/null
+++ b/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/util/ChecksumCalculator.java
@@ -0,0 +1,309 @@
+/*
+ * 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 org.onosproject.ospf.controller.OspfLsa;
+import org.onosproject.ospf.controller.OspfLsaType;
+import org.onosproject.ospf.protocol.lsa.types.AsbrSummaryLsa;
+import org.onosproject.ospf.protocol.lsa.types.ExternalLsa;
+import org.onosproject.ospf.protocol.lsa.types.NetworkLsa;
+import org.onosproject.ospf.protocol.lsa.types.OpaqueLsa10;
+import org.onosproject.ospf.protocol.lsa.types.OpaqueLsa11;
+import org.onosproject.ospf.protocol.lsa.types.OpaqueLsa9;
+import org.onosproject.ospf.protocol.lsa.types.RouterLsa;
+import org.onosproject.ospf.protocol.lsa.types.SummaryLsa;
+import org.onosproject.ospf.protocol.ospfpacket.OspfMessage;
+import org.onosproject.ospf.protocol.ospfpacket.types.DdPacket;
+import org.onosproject.ospf.protocol.ospfpacket.types.HelloPacket;
+import org.onosproject.ospf.protocol.ospfpacket.types.LsAcknowledge;
+import org.onosproject.ospf.protocol.ospfpacket.types.LsRequest;
+import org.onosproject.ospf.protocol.ospfpacket.types.LsUpdate;
+
+import java.util.Arrays;
+
+/**
+ * Calculates checksum for different types of OSPF packets.
+ */
+public class ChecksumCalculator {
+
+    /**
+     * Converts given string to sixteen bits integer.
+     * If hexasum is more than 16 bit value, needs to be reduced to 16 bit value.
+     *
+     * @param strToConvert hexasum value to convert
+     * @return 16 bit integer value
+     */
+    public static int convertToSixteenBits(String strToConvert) {
+        StringBuilder sb = new StringBuilder(strToConvert);
+        sb = sb.reverse();
+        StringBuilder s1 = new StringBuilder(sb.substring(0, 4));
+        s1 = s1.reverse();
+        StringBuilder s2 = new StringBuilder(sb.substring(4, sb.length()));
+        s2 = s2.reverse();
+        int num = Integer.parseInt(s1.toString(), 16) + Integer.parseInt(s2.toString(), 16);
+        return num;
+    }
+
+    /**
+     * Checks whether checksum is valid or not in the given OSPF message.
+     *
+     * @param ospfMessage  ospf message instance
+     * @param checksumPos1 position of checksum bit in packet
+     * @param checksumPos2 position of checksum bit in packet
+     * @return true if valid else false
+     */
+    public boolean isValidOspfCheckSum(OspfMessage ospfMessage, int checksumPos1, int checksumPos2) {
+
+        switch (ospfMessage.ospfMessageType().value()) {
+            case OspfParameters.HELLO:
+                ospfMessage = (HelloPacket) ospfMessage;
+                break;
+            case OspfParameters.DD:
+                ospfMessage = (DdPacket) ospfMessage;
+                break;
+            case OspfParameters.LSREQUEST:
+                ospfMessage = (LsRequest) ospfMessage;
+                break;
+            case OspfParameters.LSUPDATE:
+                ospfMessage = (LsUpdate) ospfMessage;
+                break;
+            case OspfParameters.LSACK:
+                ospfMessage = (LsAcknowledge) ospfMessage;
+                break;
+            default:
+                break;
+        }
+
+        byte[] messageAsBytes = ospfMessage.asBytes();
+        return validateOspfCheckSum(messageAsBytes, checksumPos1, checksumPos2);
+    }
+
+    /**
+     * Checks whether checksum is valid or not in the given OSPF LSA.
+     *
+     * @param ospfLsa         lsa instance
+     * @param lsType          lsa type
+     * @param lsaChecksumPos1 lsa checksum position in packet
+     * @param lsaChecksumPos2 lsa checksum position in packet
+     * @return true if valid else false
+     * @throws Exception might throw exception while processing
+     */
+    public boolean isValidLsaCheckSum(OspfLsa ospfLsa, int lsType, int lsaChecksumPos1,
+                                      int lsaChecksumPos2) throws Exception {
+        if (lsType == OspfLsaType.ROUTER.value()) {
+            RouterLsa lsa = (RouterLsa) ospfLsa;
+            return validateLsaCheckSum(lsa.asBytes(), lsaChecksumPos1, lsaChecksumPos2);
+        } else if (lsType == OspfLsaType.NETWORK.value()) {
+            NetworkLsa lsa = (NetworkLsa) ospfLsa;
+            return validateLsaCheckSum(lsa.asBytes(), lsaChecksumPos1, lsaChecksumPos2);
+        } else if (lsType == OspfLsaType.SUMMARY.value()) {
+            SummaryLsa lsa = (SummaryLsa) ospfLsa;
+            return validateLsaCheckSum(lsa.asBytes(), lsaChecksumPos1, lsaChecksumPos2);
+        } else if (lsType == OspfLsaType.ASBR_SUMMARY.value()) {
+            AsbrSummaryLsa lsa = (AsbrSummaryLsa) ospfLsa;
+            return validateLsaCheckSum(lsa.asBytes(), lsaChecksumPos1, lsaChecksumPos2);
+        } else if (lsType == OspfLsaType.EXTERNAL_LSA.value()) {
+            ExternalLsa lsa = (ExternalLsa) ospfLsa;
+            return validateLsaCheckSum(lsa.asBytes(), lsaChecksumPos1, lsaChecksumPos2);
+        } else if (lsType == OspfLsaType.LINK_LOCAL_OPAQUE_LSA.value()) {
+            OpaqueLsa9 lsa = (OpaqueLsa9) ospfLsa;
+            return validateLsaCheckSum(lsa.asBytes(), lsaChecksumPos1, lsaChecksumPos2);
+        } else if (lsType == OspfLsaType.AREA_LOCAL_OPAQUE_LSA.value()) {
+            OpaqueLsa10 lsa = (OpaqueLsa10) ospfLsa;
+            return validateLsaCheckSum(lsa.asBytes(), lsaChecksumPos1, lsaChecksumPos2);
+        } else if (lsType == OspfLsaType.AS_OPAQUE_LSA.value()) {
+            OpaqueLsa11 lsa = (OpaqueLsa11) ospfLsa;
+            return validateLsaCheckSum(lsa.asBytes(), lsaChecksumPos1, lsaChecksumPos2);
+        }
+
+        return false;
+    }
+
+    /**
+     * Verifies the checksum is valid in given LSA packet bytes.
+     *
+     * @param lsaPacket       lsa as byte array
+     * @param lsaChecksumPos1 position of checksum bit in packet
+     * @param lsaChecksumPos2 position of checksum bit in packet
+     * @return true if valid else false
+     */
+    public boolean validateLsaCheckSum(byte[] lsaPacket, int lsaChecksumPos1, int lsaChecksumPos2) {
+
+        byte[] checksum = calculateLsaChecksum(lsaPacket, lsaChecksumPos1, lsaChecksumPos2);
+
+        if (lsaPacket[lsaChecksumPos1] == checksum[0] && lsaPacket[lsaChecksumPos2] == checksum[1]) {
+            return true;
+        }
+
+        return false;
+    }
+
+    /**
+     * Verifies the checksum is valid in given OSPF packet bytes.
+     *
+     * @param ospfPacket   as byte array
+     * @param checksumPos1 position of checksum bit in packet
+     * @param checksumPos2 position of checksum bit in packet
+     * @return true if valid else false
+     */
+    public boolean validateOspfCheckSum(byte[] ospfPacket, int checksumPos1, int checksumPos2) {
+
+        byte[] checkSum = calculateOspfCheckSum(ospfPacket, checksumPos1, checksumPos2);
+
+        if (ospfPacket[checksumPos1] == checkSum[0] && ospfPacket[checksumPos2] == checkSum[1]) {
+            return true;
+        }
+
+        return false;
+    }
+
+    /**
+     * Calculates the LSA checksum.
+     *
+     * @param lsaBytes        as byte array
+     * @param lsaChecksumPos1 position of checksum bit in packet
+     * @param lsaChecksumPos2 position of checksum bit in packet
+     * @return checksum bytes
+     */
+    public byte[] calculateLsaChecksum(byte[] lsaBytes, int lsaChecksumPos1, int lsaChecksumPos2) {
+
+        byte[] tempLsaByte = Arrays.copyOf(lsaBytes, lsaBytes.length);
+
+        int[] checksumOut = {0, 0};
+        tempLsaByte[lsaChecksumPos1] = 0;
+        tempLsaByte[lsaChecksumPos2] = 0;
+        byte[] byteCheckSum = {0, 0};
+        if (lsaBytes != null) {
+            for (int i = 2; i < tempLsaByte.length; i++) {
+                checksumOut[0] = checksumOut[0] + ((int) tempLsaByte[i] & 0xFF);
+                checksumOut[1] = checksumOut[1] + checksumOut[0];
+            }
+            checksumOut[0] = checksumOut[0] % 255;
+            checksumOut[1] = checksumOut[1] % 255;
+        }
+        int byte1 = (int) ((tempLsaByte.length - lsaChecksumPos1 - 1) * checksumOut[0] - checksumOut[1]) % 255;
+        if (byte1 <= 0) {
+            byte1 += 255;
+        }
+        int byte2 = 510 - checksumOut[0] - byte1;
+        if (byte2 > 255) {
+            byte2 -= 255;
+        }
+
+        byteCheckSum[0] = (byte) byte1;
+        byteCheckSum[1] = (byte) byte2;
+
+        return byteCheckSum;
+    }
+
+    /**
+     * Calculate checksum from hexasum.
+     *
+     * @param hexasum total of 16 bits hexadecimal values
+     * @return checksum value
+     */
+    private int calculateChecksum(int hexasum) {
+
+        char[] tempZeros = {'0', '0', '0', '0'};
+        StringBuffer hexaAsBinaryStr = new StringBuffer(Integer.toBinaryString(hexasum));
+        int length = hexaAsBinaryStr.length();
+        while (length > 16) {
+            if (hexaAsBinaryStr.length() % 4 != 0) {
+                int offset = hexaAsBinaryStr.length() % 4;
+                hexaAsBinaryStr.insert(0, tempZeros, 0, 4 - offset);
+            }
+            StringBuffer hexaStr1 = new StringBuffer(hexaAsBinaryStr.reverse().substring(0, 16));
+            String revHexaStr1 = hexaStr1.reverse().toString();
+            StringBuffer hexaStr2 = new StringBuffer(hexaAsBinaryStr.reverse());
+            StringBuffer hexaStr3 = new StringBuffer(hexaStr2.reverse().substring(16, hexaStr2.length()));
+            String revHexaStr3 = hexaStr3.reverse().toString();
+            int lastSixteenHexaBits = Integer.parseInt(revHexaStr1, 2);
+            int remainingHexaBits = Integer.parseInt(revHexaStr3, 2);
+            int totalCheckSum = lastSixteenHexaBits + remainingHexaBits;
+            hexaAsBinaryStr = new StringBuffer(Integer.toBinaryString(totalCheckSum));
+            length = hexaAsBinaryStr.length();
+        }
+        if (hexaAsBinaryStr.length() < 16) {
+            int count = 16 - hexaAsBinaryStr.length();
+            String s = hexaAsBinaryStr.toString();
+            for (int i = 0; i < count; i++) {
+                s = "0" + s;
+            }
+
+            hexaAsBinaryStr = new StringBuffer(s);
+
+        }
+        StringBuffer checksum = negate(hexaAsBinaryStr);
+        return Integer.parseInt(checksum.toString(), 2);
+    }
+
+    /**
+     * Negates given hexasum.
+     *
+     * @param binaryString binary form of hexasum
+     * @return binary from of calculateChecksum
+     */
+    private StringBuffer negate(StringBuffer binaryString) {
+        for (int i = 0; i < binaryString.length(); i++) {
+            if (binaryString.charAt(i) == '1') {
+                binaryString.replace(i, i + 1, "0");
+            } else {
+                binaryString.replace(i, i + 1, "1");
+            }
+        }
+
+        return binaryString;
+    }
+
+    /**
+     * Calculates the OSPF checksum for the given packet.
+     *
+     * @param packet       as byte array
+     * @param checksumPos1 position of checksum bit in packet
+     * @param checksumPos2 position of checksum bit in packet
+     * @return checksum bytes
+     */
+    public byte[] calculateOspfCheckSum(byte[] packet, int checksumPos1, int checksumPos2) {
+
+        int hexasum = 0;
+        for (int i = 0; i < packet.length; i = i + 2) {
+            if (i != 12) {
+                byte b1 = packet[i];
+                String s1 = String.format("%8s", Integer.toBinaryString(b1 & 0xFF)).replace(' ', '0');
+                b1 = packet[i + 1];
+                String s2 = String.format("%8s", Integer.toBinaryString(b1 & 0xFF)).replace(' ', '0');
+                String hexa = s1 + s2;
+                int num1 = Integer.parseInt(hexa, 2);
+                hexasum = hexasum + num1;
+                String convertTo16 = Integer.toHexString(hexasum);
+                if (convertTo16.length() > 4) {
+                    hexasum = convertToSixteenBits(convertTo16);
+                }
+            }
+        }
+        StringBuilder sb = new StringBuilder(Integer.toHexString(hexasum));
+        if (sb.length() > 4) {
+            sb = sb.reverse();
+            StringBuilder s1 = new StringBuilder(sb.substring(0, 4));
+            s1 = s1.reverse();
+            StringBuilder s2 = new StringBuilder(sb.substring(4, sb.length()));
+            s2 = s2.reverse();
+            hexasum = Integer.parseInt(s1.toString(), 16) + Integer.parseInt(s2.toString(), 16);
+        }
+        int finalChecksum = calculateChecksum(hexasum);
+        return OspfUtil.convertToTwoBytes(finalChecksum);
+    }
+}
\ No newline at end of file