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 2063 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.
*/
/**
* 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