Chidambar babu
Committed by Thomas Vachuska

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

Change-Id: I4bf4b7eb26a0e2b5006b41b24d67c7f21450b11b
Showing 21 changed files with 2796 additions and 0 deletions
<!--
~ Copyright 2014 Open Networking Laboratory
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.onosproject</groupId>
<artifactId>onos-ospf</artifactId>
<version>1.4.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>onos-ospf-protocol</artifactId>
<packaging>bundle</packaging>
<description>ONOS Ospf controller protocol</description>
<dependencies>
<dependency>
<groupId>org.onosproject</groupId>
<artifactId>onos-ospf-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.onosproject</groupId>
<artifactId>onos-ospf-api</artifactId>
<version>1.4.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
/*
* 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
/*
* 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
/*
* 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
/*
* 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
/*
* 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
/*
* 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
/*
* 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
/*
* 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
/*
* 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
/*
* 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;
/*
* 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
/*
* 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
/*
* 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
/*
* 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
/*
* 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
/*
* 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
/*
* 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 org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.onlab.packet.Ip4Address;
import org.onosproject.ospf.controller.OspfLsaType;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
/**
* Unit test class for LsaHeader.
*/
public class LsaHeaderTest {
private LsaHeader lsaHeader;
private int result;
private Ip4Address result1;
private long result2;
private OspfLsaType ospflsaType;
private LsaHeader header;
private byte[] result3;
private LsaHeader lsaHeader1;
private String result4;
@Before
public void setUp() throws Exception {
lsaHeader = new LsaHeader();
}
@After
public void tearDown() throws Exception {
lsaHeader = null;
result1 = null;
ospflsaType = null;
header = null;
result3 = null;
lsaHeader1 = null;
}
/**
* Tests equals() method.
*/
@Test
public void testEquals() throws Exception {
assertThat(lsaHeader.equals(new LsaHeader()), is(true));
}
/**
* Tests hashCode() method.
*/
@Test
public void testHashCode() throws Exception {
result = lsaHeader.hashCode();
assertThat(result, is(notNullValue()));
}
/**
* Tests age() getter method.
*/
@Test
public void testGetAge() throws Exception {
lsaHeader.setAge(10);
result = lsaHeader.age();
assertThat(result, is(10));
}
/**
* Tests age() setter method.
*/
@Test
public void testSetAge() throws Exception {
lsaHeader.setAge(10);
result = lsaHeader.age();
assertThat(result, is(10));
}
/**
* Tests options() getter method.
*/
@Test
public void testGetOptions() throws Exception {
lsaHeader.setOptions(2);
result = lsaHeader.options();
assertThat(result, is(2));
}
/**
* Tests options() setter method.
*/
@Test
public void testSetOptions() throws Exception {
lsaHeader.setOptions(2);
result = lsaHeader.options();
assertThat(result, is(2));
}
/**
* Tests lsType() getter method.
*/
@Test
public void testGetLsType() throws Exception {
lsaHeader.setLsType(1);
result = lsaHeader.lsType();
assertThat(result, is(1));
}
/**
* Tests lsType() setter method.
*/
@Test
public void testSetLsType() throws Exception {
lsaHeader.setLsType(1);
result = lsaHeader.lsType();
assertThat(result, is(1));
}
/**
* Tests linkStateId() getter method.
*/
@Test
public void testGetLinkStateId() throws Exception {
lsaHeader.setLinkStateId("10.226.165.164");
result4 = lsaHeader.linkStateId();
assertThat(result4, is("10.226.165.164"));
}
/**
* Tests linkStateId() setter method.
*/
@Test
public void testSetLinkStateId() throws Exception {
lsaHeader.setLinkStateId("10.226.165.164");
result4 = lsaHeader.linkStateId();
assertThat(result4, is("10.226.165.164"));
}
/**
* Tests advertisingRouter() setter method.
*/
@Test
public void testGetAdvertisingRouter() throws Exception {
lsaHeader.setAdvertisingRouter(Ip4Address.valueOf("10.226.165.164"));
result1 = lsaHeader.advertisingRouter();
assertThat(result1, is(Ip4Address.valueOf("10.226.165.164")));
}
/**
* Tests advertisingRouter() setter method.
*/
@Test
public void testSetAdvertisingRouter() throws Exception {
lsaHeader.setAdvertisingRouter(Ip4Address.valueOf("10.226.165.164"));
result1 = lsaHeader.advertisingRouter();
assertThat(result1, is(Ip4Address.valueOf("10.226.165.164")));
}
/**
* Tests lsSequenceNo() getter method.
*/
@Test
public void testGetLsSequenceNo() throws Exception {
lsaHeader.setLsSequenceNo(222);
result2 = lsaHeader.lsSequenceNo();
assertThat(result2, is(222L));
}
/**
* Tests lsSequenceNo() setter method.
*/
@Test
public void testSetLsSequenceNo() throws Exception {
lsaHeader.setLsSequenceNo(222);
result2 = lsaHeader.lsSequenceNo();
assertThat(result2, is(222L));
}
/**
* Tests lsCheckSum() getter method.
*/
@Test
public void testGetLsChecksum() throws Exception {
lsaHeader.setLsCheckSum(2);
result = lsaHeader.lsCheckSum();
assertThat(result, is(2));
}
/**
* Tests lsCheckSum() setter method.
*/
@Test
public void testSetLsChecksum() throws Exception {
lsaHeader.setLsCheckSum(2);
result = lsaHeader.lsCheckSum();
assertThat(result, is(2));
}
/**
* Tests lsPacketLen() getter method.
*/
@Test
public void testGetLsPacketLen() throws Exception {
lsaHeader.setLsPacketLen(48);
result = lsaHeader.lsPacketLen();
assertThat(result, is(48));
}
/**
* Tests lsPacketLen() getter method.
*/
@Test
public void testSetLsPacketLen() throws Exception {
lsaHeader.setLsPacketLen(48);
result = lsaHeader.lsPacketLen();
assertThat(result, is(48));
}
/**
* Tests getOspfLsaType() getter method.
*/
@Test
public void testGetOspfLsaType() throws Exception {
lsaHeader.setLsType(1);
ospflsaType = lsaHeader.getOspfLsaType();
assertThat(ospflsaType, is(notNullValue()));
assertThat(ospflsaType, is(OspfLsaType.ROUTER));
lsaHeader.setLsType(2);
ospflsaType = lsaHeader.getOspfLsaType();
assertThat(ospflsaType, is(notNullValue()));
assertThat(ospflsaType, is(OspfLsaType.NETWORK));
lsaHeader.setLsType(3);
ospflsaType = lsaHeader.getOspfLsaType();
assertThat(ospflsaType, is(notNullValue()));
assertThat(ospflsaType, is(OspfLsaType.SUMMARY));
lsaHeader.setLsType(4);
ospflsaType = lsaHeader.getOspfLsaType();
assertThat(ospflsaType, is(notNullValue()));
assertThat(ospflsaType, is(OspfLsaType.ASBR_SUMMARY));
lsaHeader.setLsType(5);
ospflsaType = lsaHeader.getOspfLsaType();
assertThat(ospflsaType, is(notNullValue()));
assertThat(ospflsaType, is(OspfLsaType.EXTERNAL_LSA));
lsaHeader.setLsType(6);
ospflsaType = lsaHeader.getOspfLsaType();
assertThat(ospflsaType, is(notNullValue()));
assertThat(ospflsaType, is(OspfLsaType.UNDEFINED));
}
/**
* Tests lsaHeader() getter method.
*/
@Test
public void testGetLsaHeader() throws Exception {
header = (LsaHeader) lsaHeader.lsaHeader();
assertThat(header, instanceOf(LsaHeader.class));
}
/**
* Tests getLsaHeaderAsByteArray() method.
*/
@Test
public void testGetLsaHeaderAsByteArray() throws Exception {
result3 = lsaHeader.getLsaHeaderAsByteArray();
assertThat(result3, is(notNullValue()));
}
/**
* Tests to string method.
*/
@Test
public void testToString() throws Exception {
assertThat(lsaHeader.toString(), is(notNullValue()));
}
/**
* Tests populateHeader() method.
*/
@Test
public void testPopulateHeader() throws Exception {
lsaHeader1 = new LsaHeader();
lsaHeader1.setLsPacketLen(10);
lsaHeader1.setAdvertisingRouter(Ip4Address.valueOf("1.1.1.1"));
lsaHeader1.setOptions(2);
lsaHeader1.setAge(20);
lsaHeader1.setLsType(3);
lsaHeader1.setLinkStateId("2.2.2.2");
lsaHeader1.setLsCheckSum(1234);
lsaHeader1.setLsSequenceNo(456789);
lsaHeader.populateHeader(lsaHeader1);
assertThat(lsaHeader1, is(notNullValue()));
}
}
\ No newline at end of file
/*
* 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 org.hamcrest.Matchers;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.onlab.packet.Ip4Address;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
/**
* Unit test class for OpaqueLsaHeader.
*/
public class OpaqueLsaHeaderTest {
private OpaqueLsaHeader opaqueHeader;
private OpaqueLsaHeader opaqueLsaHeader1;
private int num;
private byte[] result;
private int result1;
@Before
public void setUp() throws Exception {
opaqueHeader = new OpaqueLsaHeader();
}
@After
public void tearDown() throws Exception {
opaqueHeader = null;
opaqueLsaHeader1 = null;
result = null;
}
/**
* Tests populateHeader() method.
*/
@Test
public void testPopulateHeader() throws Exception {
opaqueLsaHeader1 = new OpaqueLsaHeader();
opaqueLsaHeader1.setLsPacketLen(10);
opaqueLsaHeader1.setAdvertisingRouter(Ip4Address.valueOf("1.1.1.1"));
opaqueLsaHeader1.setOptions(2);
opaqueLsaHeader1.setAge(20);
opaqueLsaHeader1.setLsType(3);
opaqueLsaHeader1.setOpaqueId(1);
opaqueLsaHeader1.setOpaqueType(3);
opaqueLsaHeader1.setLsCheckSum(1234);
opaqueLsaHeader1.setLsSequenceNo(456789);
opaqueLsaHeader1.populateHeader(opaqueLsaHeader1);
assertThat(opaqueLsaHeader1, is(notNullValue()));
}
/**
* Tests opaqueId() getter method.
*/
@Test
public void testGetOpaqueId() throws Exception {
opaqueHeader.setOpaqueId(1);
num = opaqueHeader.opaqueId();
assertThat(num, is(1));
}
/**
* Tests opaqueId() setter method.
*/
@Test
public void testSetOpaqueId() throws Exception {
opaqueHeader.setOpaqueId(1);
num = opaqueHeader.opaqueId();
assertThat(num, is(1));
}
/**
* Tests opaqueType() getter method.
*/
@Test
public void testGetOpaqueType() throws Exception {
opaqueHeader.setOpaqueType(1);
num = opaqueHeader.opaqueType();
assertThat(num, is(1));
}
/**
* Tests opaqueType() setter method.
*/
@Test
public void testSetOpaqueType() throws Exception {
opaqueHeader.setOpaqueType(1);
num = opaqueHeader.opaqueType();
assertThat(num, is(1));
}
/**
* Tests getOpaqueLsaHeaderAsByteArray() method.
*/
@Test
public void testGetOpaqueLsaHeaderAsByteArray() throws Exception {
result = opaqueHeader.getOpaqueLsaHeaderAsByteArray();
assertThat(result, is(notNullValue()));
}
/**
* Tests to string method.
*/
@Test
public void testToString() throws Exception {
assertThat(opaqueHeader.toString(), is(notNullValue()));
}
/**
* Tests hashCode() method.
*/
@Test
public void testHashcode() throws Exception {
result1 = opaqueHeader.hashCode();
assertThat(result1, is(Matchers.notNullValue()));
}
}
\ No newline at end of file
/*
* 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.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.onlab.packet.Ip4Address;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.assertThat;
/**
* Unit test class for OspfPacketHeader.
*/
public class OspfPacketHeaderTest {
private final byte[] packet = {0, 0, 0, 0};
private OspfPacketHeader ospfPacketHeader;
private ChannelBuffer channelBuffer;
private byte[] result2;
private int result;
private Ip4Address result1;
@Before
public void setUp() throws Exception {
ospfPacketHeader = new OspfPacketHeader();
}
@After
public void tearDown() throws Exception {
ospfPacketHeader = null;
ospfPacketHeader = null;
channelBuffer = null;
result2 = null;
result1 = null;
}
/**
* Tests sourceIp() getter method.
*/
@Test
public void testGetSourceIP() throws Exception {
ospfPacketHeader.setSourceIp(Ip4Address.valueOf("1.1.1.1"));
assertThat(ospfPacketHeader.sourceIp(), is(Ip4Address.valueOf("1.1.1.1")));
}
/**
* Tests sourceIp() setter method.
*/
@Test
public void testSetSourceIP() throws Exception {
ospfPacketHeader.setSourceIp(Ip4Address.valueOf("1.1.1.1"));
assertThat(result, is(notNullValue()));
assertThat(ospfPacketHeader.sourceIp(), is(Ip4Address.valueOf("1.1.1.1")));
}
/**
* Tests ospfMessageType() getter method.
*/
@Test
public void testGetOspfMessageType() throws Exception {
assertThat(ospfPacketHeader.ospfMessageType(), nullValue());
}
/**
* Tests readFrom() method.
*/
@Test
public void testReadFrom() throws Exception {
channelBuffer = ChannelBuffers.copiedBuffer(packet);
ospfPacketHeader.readFrom(channelBuffer);
assertThat(ospfPacketHeader, is(notNullValue()));
}
/**
* Tests asBytes() method.
*/
@Test
public void testAsBytes() throws Exception {
result2 = ospfPacketHeader.asBytes();
assertThat(result2, is(notNullValue()));
}
/**
* Tests ospfVersion() getter method.
*/
@Test
public void testGetOspfVer() throws Exception {
ospfPacketHeader.setOspfVer(2);
result = ospfPacketHeader.ospfVersion();
assertThat(result, is(notNullValue()));
assertThat(result, is(2));
}
/**
* Tests ospfVersion() setter method.
*/
@Test
public void testSetOspfVer() throws Exception {
ospfPacketHeader.setOspfVer(2);
result = ospfPacketHeader.ospfVersion();
assertThat(result, is(notNullValue()));
assertThat(result, is(2));
}
/**
* Tests ospfType() getter method.
*/
@Test
public void testGetOspfType() throws Exception {
ospfPacketHeader.setOspftype(3);
result = ospfPacketHeader.ospfType();
assertThat(result, is(notNullValue()));
assertThat(result, is(3));
}
/**
* Tests ospfType() setter method.
*/
@Test
public void testSetOspfType() throws Exception {
ospfPacketHeader.setOspftype(3);
result = ospfPacketHeader.ospfType();
assertThat(result, is(notNullValue()));
assertThat(result, is(3));
}
/**
* Tests ospfPacLength() getter method.
*/
@Test
public void testGetOspfPacLength() throws Exception {
ospfPacketHeader.setOspfPacLength(3);
result = ospfPacketHeader.ospfPacLength();
assertThat(result, is(notNullValue()));
assertThat(result, is(3));
}
/**
* Tests ospfPacLength() setter method.
*/
@Test
public void testSetOspfPacLength() throws Exception {
ospfPacketHeader.setOspfPacLength(3);
int result = ospfPacketHeader.ospfPacLength();
assertThat(result, is(notNullValue()));
assertThat(result, is(3));
}
/**
* Tests routerId()getter method.
*/
@Test
public void testGetRouterId() throws Exception {
ospfPacketHeader.setRouterId(Ip4Address.valueOf("1.1.1.1"));
result1 = ospfPacketHeader.routerId();
assertThat(result1, is(notNullValue()));
assertThat(result1, is(Ip4Address.valueOf("1.1.1.1")));
}
/**
* Tests routerId() setter method.
*/
@Test
public void testSetRouterId() throws Exception {
ospfPacketHeader.setRouterId(Ip4Address.valueOf("1.1.1.1"));
result1 = ospfPacketHeader.routerId();
assertThat(result1, is(notNullValue()));
assertThat(result1, is(Ip4Address.valueOf("1.1.1.1")));
}
/**
* Tests areaId() getter method.
*/
@Test
public void testGetAreaId() throws Exception {
ospfPacketHeader.setAreaId(Ip4Address.valueOf("1.1.1.1"));
result1 = ospfPacketHeader.areaId();
assertThat(result1, is(notNullValue()));
assertThat(result1, is(Ip4Address.valueOf("1.1.1.1")));
}
/**
* Tests areaId() setter method.
*/
@Test
public void testSetAreaId() throws Exception {
ospfPacketHeader.setAreaId(Ip4Address.valueOf("1.1.1.1"));
result1 = ospfPacketHeader.areaId();
assertThat(result1, is(notNullValue()));
assertThat(result1, is(Ip4Address.valueOf("1.1.1.1")));
}
/**
* Tests checksum() getter method.
*/
@Test
public void testGetChecksum() throws Exception {
ospfPacketHeader.setChecksum(3);
result = ospfPacketHeader.checksum();
assertThat(result, is(notNullValue()));
assertThat(result, is(3));
}
/**
* Tests checksum() setter method.
*/
@Test
public void testSetChecksum() throws Exception {
ospfPacketHeader.setChecksum(3);
result = ospfPacketHeader.checksum();
assertThat(result, is(notNullValue()));
assertThat(result, is(3));
}
/**
* Tests authType() getter method.
*/
@Test
public void testGetAutype() throws Exception {
ospfPacketHeader.setAuthType(3);
result = ospfPacketHeader.authType();
Assert.assertNotNull(result);
Assert.assertEquals(3, result);
}
/**
* Tests authType() setter method.
*/
@Test
public void testSetAutype() throws Exception {
ospfPacketHeader.setAuthType(3);
result = ospfPacketHeader.authType();
assertThat(result, is(notNullValue()));
assertThat(result, is(3));
}
/**
* Tests authentication() getter method.
*/
@Test
public void testGetAuthentication() throws Exception {
ospfPacketHeader.setAuthentication(3);
result = ospfPacketHeader.authentication();
assertThat(result, is(notNullValue()));
assertThat(result, is(3));
}
/**
* Tests authentication() setter method.
*/
@Test
public void testSetAuthentication() throws Exception {
ospfPacketHeader.setAuthentication(3);
result = ospfPacketHeader.authentication();
assertThat(result, is(notNullValue()));
assertThat(result, is(3));
}
/**
* Tests destinationIp() getter method.
*/
@Test
public void testGetDestinationIP() throws Exception {
ospfPacketHeader.setDestinationIp(Ip4Address.valueOf("1.1.1.1"));
result1 = ospfPacketHeader.destinationIp();
assertThat(result1, is(notNullValue()));
assertThat(result1, is(Ip4Address.valueOf("1.1.1.1")));
}
/**
* Tests destinationIp() setter method.
*/
@Test
public void testSetDestinationIP() throws Exception {
ospfPacketHeader.setDestinationIp(Ip4Address.valueOf("1.1.1.1"));
result1 = ospfPacketHeader.destinationIp();
assertThat(result1, is(notNullValue()));
assertThat(result1, is(Ip4Address.valueOf("1.1.1.1")));
}
/**
* Tests to string method.
*/
@Test
public void testToString() throws Exception {
assertThat(ospfPacketHeader.toString(), is(notNullValue()));
}
/**
* Tests populateHeader() method.
*/
@Test
public void testPopulateHeader() throws Exception {
ospfPacketHeader.populateHeader(new OspfPacketHeader());
assertThat(ospfPacketHeader, is(notNullValue()));
}
}
\ No newline at end of file
/*
* 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.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.onlab.packet.Ip4Address;
import org.onosproject.ospf.protocol.lsa.LsaHeader;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat;
/**
* Unit test class for OspfUtil.
*/
public class OspfUtilTest {
private final int ospfChecksumPos1 = 12;
private final int ospfChecksumPos2 = 13;
private final int lsaChecksumPos1 = 16;
private final int lsaChecksumPos2 = 17;
private final int ospfLengthPos1 = 2;
private final int ospfLengthPos2 = 3;
private final int lsaLengthPos1 = 18;
private final int lsaLengthPos2 = 19;
private final byte[] input = {0, 2};
private final byte[] packet = {2, 1, 0, 52, -64, -88, 56, 1, -64, -88, 56, 1, 0, 100, 0, 100, 0, 0, 0, 0, 0, 0,
0, 0, -64, -88, 56, 1, 0, 10, 1, 1, 0, 0, 0, 40, -64, -88, 56, 1, -64, -88, 56, 1, -64, -88, 56, 1, -64,
-88, 56, 1};
private final byte[] rLsa = {14, 16, 2, 1, -64, -88, -86, 2, -64, -88, -86, 2, -128, 0, 0, 1, 74, -114, 0, 48, 2,
0, 0, 2, -64, -88, -86, 0, -1, -1, -1, 0, 3, 0, 0, 10, -64, -88, -86, 0, -1, -1, -1, 0, 3, 0, 0, 10};
private final byte[] opaqueheader = {14, 16, 2, 1, -64, -88, -86, 2, -64, -88, -86, 2, -128, 0, 0, 1, 74, -114,
0, 48};
private int num;
private int result;
private int input2;
private long input4;
private ChannelBuffer channelBuffer;
private byte[] result1;
private LsaHeader lsaHeader;
private boolean result2;
private long result3;
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
channelBuffer = null;
result1 = null;
lsaHeader = null;
}
/**
* Tests byteToInteger() method.
*/
@Test
public void testByteToInteger() throws Exception {
result = OspfUtil.byteToInteger(input);
assertThat(result, is(notNullValue()));
assertThat(result, is(2));
}
/**
* Tests byteToLong() method.
*/
@Test
public void testByteToLong() throws Exception {
result3 = OspfUtil.byteToLong(input);
assertThat(result3, is(notNullValue()));
assertThat(result3, is(2L));
}
/**
* Tests byteToInteger() method.
*/
@Test
public void testByteToLong1() throws Exception {
result3 = OspfUtil.byteToLong(input);
assertThat(result3, is(notNullValue()));
assertThat(result3, is(2L));
}
/**
* Tests byteToInteger() method.
*/
@Test
public void testByteToInteger1() throws Exception {
result = OspfUtil.byteToInteger(input);
assertThat(result, is(notNullValue()));
assertThat(result, is(2));
}
/**
* Tests to createRandomNumber() method.
*/
@Test
public void testCreateRandomNumber() throws Exception {
num = OspfUtil.createRandomNumber();
assertThat(num, is(notNullValue()));
}
/**
* Tests readLsaHeader() method.
*/
@Test
public void testReadLsaHeader2() throws Exception {
channelBuffer = ChannelBuffers.copiedBuffer(packet);
lsaHeader = OspfUtil.readLsaHeader(channelBuffer);
assertThat(lsaHeader, is(notNullValue()));
}
/**
* Tests to readLsaHeader method.
*/
@Test
public void testReadLsaHeader1() throws Exception {
channelBuffer = ChannelBuffers.copiedBuffer(opaqueheader);
lsaHeader = OspfUtil.readLsaHeader(channelBuffer);
assertThat(lsaHeader, is(notNullValue()));
}
/**
* Tests convertToTwoBytes() method.
*/
@Test
public void testConvertToTwoBytes() throws Exception {
input2 = 4;
result1 = OspfUtil.convertToTwoBytes(input2);
assertThat(result1.length, is(2));
input2 = 1000;
result1 = OspfUtil.convertToTwoBytes(input2);
assertThat(result1.length, is(2));
}
/**
* Tests convertToThreeBytes() method.
*/
@Test
public void testConvertToThreeBytes() throws Exception {
input2 = 1000000;
result1 = OspfUtil.convertToThreeBytes(input2);
assertThat(result1.length, is(3));
input2 = 1000;
result1 = OspfUtil.convertToThreeBytes(input2);
assertThat(result1.length, is(3));
input2 = 1;
result1 = OspfUtil.convertToThreeBytes(input2);
assertThat(result1.length, is(3));
}
/**
* Tests convertToFourBytes() method.
*/
@Test
public void testConvertToFourBytes() throws Exception {
input4 = 214748364110L;
result1 = OspfUtil.convertToFourBytes(input4);
assertThat(result1.length, is(4));
input4 = 1000000;
result1 = OspfUtil.convertToFourBytes(input4);
assertThat(result1.length, is(4));
input4 = 10000;
result1 = OspfUtil.convertToFourBytes(input4);
assertThat(result1.length, is(4));
input4 = 1;
result1 = OspfUtil.convertToFourBytes(input4);
assertThat(result1.length, is(4));
}
/**
* Tests convertToFourBytes() method.
*/
@Test
public void testConvertToFourBytes1() throws Exception {
input4 = 2147483635;
result1 = OspfUtil.convertToFourBytes(this.input4);
assertThat(result1.length, is(4));
this.input4 = 1000000;
result1 = OspfUtil.convertToFourBytes(this.input4);
assertThat(result1.length, is(4));
this.input4 = 10000;
result1 = OspfUtil.convertToFourBytes(this.input4);
assertThat(result1.length, is(4));
this.input4 = 1;
result1 = OspfUtil.convertToFourBytes(this.input4);
assertThat(result1.length, is(4));
}
/**
* Tests addLengthAndCheckSum() method.
*/
@Test
public void testAddLengthAndCheckSum() throws Exception {
result1 = OspfUtil.addLengthAndCheckSum(packet, ospfLengthPos1, ospfLengthPos2,
ospfChecksumPos1, ospfChecksumPos2);
assertThat(result1[ospfChecksumPos1], is(packet[ospfChecksumPos1]));
assertThat(result1[ospfChecksumPos2], is(packet[ospfChecksumPos2]));
assertThat(result1[ospfLengthPos1], is(packet[ospfLengthPos1]));
assertThat(result1[ospfLengthPos2], is(packet[ospfLengthPos2]));
}
/**
* Tests addMetadata() method.
*/
@Test
public void testAddMetadata() throws Exception {
result1 = OspfUtil.addMetadata(packet, 123, Ip4Address.valueOf("1.1.1.1"));
assertThat(result1, is(notNullValue()));
}
/**
* Tests addLengthAndCheckSum() method.
*/
@Test
public void testAddLsaLengthAndCheckSum() throws Exception {
result1 = OspfUtil.addLengthAndCheckSum(rLsa, lsaLengthPos1, lsaLengthPos2,
lsaChecksumPos1, lsaChecksumPos2);
assertThat(result1[lsaLengthPos1], is(rLsa[lsaLengthPos1]));
assertThat(result1[lsaLengthPos2], is(rLsa[lsaLengthPos2]));
assertThat(result1[lsaChecksumPos1], is(rLsa[lsaChecksumPos1]));
assertThat(result1[lsaChecksumPos2], is(rLsa[lsaChecksumPos2]));
}
/**
* Tests addMetadata() method.
*/
@Test
public void testAddMetaData() throws Exception {
result1 = OspfUtil.addMetadata(packet, 1, Ip4Address.valueOf("2.2.2.2"));
assertThat(result1, is(notNullValue()));
}
/**
* Tests sameNetwork() method.
*/
@Test
public void testSameNetwork() throws Exception {
result2 = OspfUtil.sameNetwork(Ip4Address.valueOf("10.10.10.10"), Ip4Address.valueOf("10.10.10.11"),
Ip4Address.valueOf("255.255.255.255"));
assertThat(result2, is(false));
result2 = OspfUtil.sameNetwork(Ip4Address.valueOf("10.10.10.10"), Ip4Address.valueOf("10.10.10.10"),
Ip4Address.valueOf("255.255.255.255"));
assertThat(result2, is(true));
}
/**
* Tests isOpaqueEnabled() method.
*/
@Test
public void testIsOpaqueEnabled() throws Exception {
result2 = OspfUtil.isOpaqueEnabled(2);
assertThat(result2, is(false));
}
/**
* Tests sameNetwork() method.
*/
@Test
public void testisIsOpaqueEnabled() throws Exception {
result2 = OspfUtil.isOpaqueEnabled(2);
assertThat(result2, is(false));
}
/**
* Tests readLsaHeader() method.
*/
@Test
public void testReadLsaHeader() throws Exception {
byte[] header = {0, 10, 2, 1, 7, 7, 7, 7, 7, 7, 7, 7, -128, 0, 0, 2, 46, -126, 0,
48, 0, 0, 0, 2, 1, 1, 1, 1, 10, 10, 10, 7, 1, 0, 0, 10, 10, 10, 10, 0, -1, -1, -1,
0, 3, 0, 0, 10, 0, 10, 66, 10, 1, 0, 0, 1, 7, 7, 7, 7, -128, 0, 0, 1, -64, 79, 0,
116, 0, 1, 0, 4, 0, 0, 0, 0, 0, 2, 0, 84, 0, 1, 0, 1, 1, 0, 0, 0, 0, 2, 0, 4, 10,
10, 10, 0, 0, 5, 0, 4, 0, 0, 0, 0, 0, 6, 0, 4, 73, -104, -106, -128, 0, 7, 0, 4, 73
, -104, -106, -128, 0, 8, 0, 32, 73, -104, -106, -128, 73, -104, -106, -128, 73, -104, -106,
-128, 73, -104, -106, -128, 73, -104, -106, -128, 73, -104, -106, -128, 73, -104, -106, -128,
73, -104, -106, -128, 0, 9, 0, 4, 0, 0, 0, 0};
channelBuffer = ChannelBuffers.copiedBuffer(header);
lsaHeader = OspfUtil.readLsaHeader(channelBuffer);
assertThat(lsaHeader, is(notNullValue()));
}
/**
* Tests readLsaHeader() method.
*/
@Test
public void testReadreadLsaHeader() throws Exception {
byte[] header = {0, 2, 2, 1, -64, -88, -86, 3, -64, -88, -86, 3, -128, 0, 0, 1, 58, -100, 0, 48};
channelBuffer = ChannelBuffers.copiedBuffer(header);
lsaHeader = OspfUtil.readLsaHeader(channelBuffer);
assertThat(lsaHeader, is(notNullValue()));
}
}
\ No newline at end of file