sunish vk
Committed by Gerrit Code Review

ONOS-4665 OSPF refactoring to align with ISIS design

Change-Id: If4a15d5a1c4ae3a5b2f877fa688ce07972749205
Showing 104 changed files with 588 additions and 763 deletions
/*
* Copyright 2016-present Open Networking Laboratory
* 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.
......
/*
* Copyright 2016-present Open Networking Laboratory
* 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.
......
......@@ -98,4 +98,11 @@ public interface LsdbAge {
* @param lsaWrapper wrapper instance
*/
public void removeLsaFromBin(LsaWrapper lsaWrapper);
/**
* Gets the age counter roll over.
*
* @return the age counter roll over
*/
public int getAgeCounterRollOver();
}
\ No newline at end of file
......
......@@ -48,7 +48,8 @@ public interface OspfAgent {
/**
* Notifies that got a packet of link from network and need do processing.
*
* @param ospfRouter router instance
* @param ospfRouter router instance
* @param ospfLinkTed link ted instance
*/
void deleteLink(OspfRouter ospfRouter);
void deleteLink(OspfRouter ospfRouter, OspfLinkTed ospfLinkTed);
}
\ No newline at end of file
......
......@@ -59,7 +59,7 @@ public interface OspfArea {
*
* @return LSDB instance for this area
*/
public OspfLsdb database();
OspfLsdb database();
/**
* Checks whether an instance of the given LSA exists in the database.
......@@ -67,19 +67,12 @@ public interface OspfArea {
* @param lookupLsa LSA instance to lookup
* @return LSA wrapper instance which contains the LSA
*/
public LsaWrapper lsaLookup(OspfLsa lookupLsa);
LsaWrapper lsaLookup(OspfLsa lookupLsa);
/**
* Initializes link state database, this acts as a place holder for storing the received LSA.
*/
public void initializeDb();
/**
* Sets the stub cost.
*
* @param stubCost stub cost
*/
void setStubCost(int stubCost);
void initializeDb();
/**
* Sets the options value.
......@@ -89,38 +82,6 @@ public interface OspfArea {
void setOptions(int options);
/**
* Gets area address ranges to which this area belongs to.
*
* @return list of area address ranges
*/
List<OspfAreaAddressRange> addressRanges();
/**
* Sets the area address ranges to which this area belongs to.
*
* @param addrRangeList list of area address ranges
*/
void setAddressRanges(List<OspfAreaAddressRange> addrRangeList);
/**
* Gets whether the area is transit capable or not.
* This indicates whether the area can carry data traffic that neither originates
* nor terminates in the area itself.
*
* @return true if transit capable, else false
*/
boolean isTransitCapability();
/**
* Sets whether the area is transit capable or not.
* This indicates whether the area can carry data traffic that neither originates
* nor terminates in the area itself.
*
* @param transitCapability true if transit capable, else false
*/
void setTransitCapability(boolean transitCapability);
/**
* Gets external routing capability.
* This indicates Whether AS-external-LSAs will be flooded into/throughout the area.
*
......@@ -137,13 +98,6 @@ public interface OspfArea {
void setExternalRoutingCapability(boolean externalRoutingCapability);
/**
* Gets the stub cost, which indicates if the area has been configured as a stub area.
*
* @return stub cost
*/
int stubCost();
/**
* Gets if the router is opaque enabled or not.
* This indicates whether the router accepts opaque LSA.
*
......@@ -156,14 +110,14 @@ public interface OspfArea {
*
* @return list of interfaces
*/
List<OspfInterface> getInterfacesLst();
List<OspfInterface> ospfInterfaceList();
/**
* Sets the list of interfaces attached to this area.
*
* @param interfacesLst list of interface instances
*/
void setInterfacesLst(List<OspfInterface> interfacesLst);
void setOspfInterfaceList(List<OspfInterface> interfacesLst);
/**
* Gets the options value, which indicates the supported optional capabilities.
......
......@@ -15,6 +15,8 @@
*/
package org.onosproject.ospf.controller;
import com.fasterxml.jackson.databind.JsonNode;
import java.util.List;
import java.util.Set;
......@@ -55,9 +57,9 @@ public interface OspfController {
/**
* Updates configuration of processes.
*
* @param processes process info to update
* @param processesNode process info to update
*/
public void updateConfig(List<OspfProcess> processes);
void updateConfig(JsonNode processesNode);
/**
* Deletes configuration parameters.
......@@ -65,7 +67,7 @@ public interface OspfController {
* @param processes list of process instance
* @param attribute attribute to delete
*/
public void deleteConfig(List<OspfProcess> processes, String attribute);
void deleteConfig(List<OspfProcess> processes, String attribute);
/**
* Gets the list of listeners registered for router events.
......
......@@ -15,9 +15,10 @@
*/
package org.onosproject.ospf.controller;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.onlab.packet.Ip4Address;
import java.util.HashMap;
import java.util.Map;
/**
* Represents an OSPF Interface.
......@@ -25,34 +26,39 @@ import java.util.HashMap;
public interface OspfInterface {
/**
* Gets network mask of the interface.
* Returns interface index.
*
* @return network mask
* @return interface index
*/
Ip4Address ipNetworkMask();
public int interfaceIndex();
/**
* Sets area id, to which the interface belongs.
* Set interface index.
*
* @param areaId area identifier
* @param interfaceIndex interface index
*/
void setAreaId(int areaId);
public void setInterfaceIndex(int interfaceIndex);
/**
* Sets the authentication key.
* Interface uses this to authenticate while establishing communication with other routers.
* Returns OSPF area instance.
*
* @param authKey represents authentication key
* @return OSPF area instance
*/
void setAuthKey(String authKey);
public OspfArea ospfArea();
/**
* Sets the authentication type,
* Interface uses this to authenticate while establishing communication with other routers.
* Sets OSPF area instance.
*
* @param authType authType represents authentication type
* @param ospfArea OSPF area instance
*/
public void setOspfArea(OspfArea ospfArea);
/**
* Gets network mask of the interface.
*
* @return network mask
*/
void setAuthType(String authType);
Ip4Address ipNetworkMask();
/**
* Sets the value of BDR.
......@@ -87,13 +93,6 @@ public interface OspfInterface {
void setRouterDeadIntervalTime(int routerDeadIntervalTime);
/**
* Sets the interface cost which is the cost of sending a data packet onto the network.
*
* @param interfaceCost an integer represents interface cost
*/
void setInterfaceCost(int interfaceCost);
/**
* Sets interface type.
* This indicates whether the interface is on point to point mode or broadcast mode.
*
......@@ -116,22 +115,6 @@ public interface OspfInterface {
void setIpNetworkMask(Ip4Address ipNetworkMask);
/**
* Sets the polling interval.
* Polling interval indicates the interval until when the Hello Packets are
* sent to a dead neighbor.
*
* @param pollInterval an integer represents poll interval
*/
void setPollInterval(int pollInterval);
/**
* Sets transmission delay.
*
* @param transmitDelay an integer represents delay
*/
void setTransmitDelay(int transmitDelay);
/**
* Sets retransmit interval which indicates the number of seconds between LSA retransmissions.
*
* @param reTransmitInterval an integer represents interval
......@@ -153,13 +136,6 @@ public interface OspfInterface {
void setRouterPriority(int routerPriority);
/**
* Gets the area id to which router belongs.
*
* @return areaId an integer value
*/
int areaId();
/**
* Gets the IP address.
*
* @return an string represents IP address
......@@ -181,32 +157,11 @@ public interface OspfInterface {
int mtu();
/**
* Gets interface cost.
*
* @return an integer representing interface cost
*/
int interfaceCost();
/**
* Gets the list of neighbors associated with the interface.
*
* @return listOfNeighbors as key value pair
*/
HashMap<String, OspfNbr> listOfNeighbors();
/**
* Gets poll interval.
*
* @return pollInterval an integer representing poll interval
*/
int pollInterval();
/**
* Gets transmission delay.
*
* @return transmitDelay an integer representing delay
*/
int transmitDelay();
Map<String, OspfNbr> listOfNeighbors();
/**
* Gets the IP address of the BDR.
......@@ -223,20 +178,6 @@ public interface OspfInterface {
Ip4Address dr();
/**
* Gets authentication key.
*
* @return authKey represents authentication key
*/
String authKey();
/**
* Gets authentication type.
*
* @return authType represents authentication type
*/
String authType();
/**
* Gets hello interval time in seconds, this defines how often we send the hello packet.
*
* @return hello interval time in seconds
......@@ -294,4 +235,54 @@ public interface OspfInterface {
* @param lsaKey key used to store lsa in map
*/
void removeLsaFromNeighborMap(String lsaKey);
/**
* When an OSPF message received it is handed over to this method.
* Based on the type of the OSPF message received it will be handed over
* to corresponding message handler methods.
*
* @param ospfMessage received OSPF message
* @param ctx channel handler context instance.
* @throws Exception might throws exception
*/
void processOspfMessage(OspfMessage ospfMessage, ChannelHandlerContext ctx) throws Exception;
/**
* Represents an interface is up and connected.
*
* @throws Exception might throws exception
*/
void interfaceUp() throws Exception;
/**
* Starts the timer which waits for configured seconds and sends Delayed Ack Packet.
*/
void startDelayedAckTimer();
/**
* Stops the delayed acknowledge timer.
*/
void stopDelayedAckTimer();
/**
* Starts the hello timer which sends hello packet every configured seconds.
*/
void startHelloTimer();
/**
* Stops the hello timer.
*/
void stopHelloTimer();
/**
* Gets called when an interface is down.
* All interface variables are reset, and interface timers disabled.
* Also all neighbor connections associated with the interface are destroyed.
*/
void interfaceDown();
/**
* Removes all the neighbors.
*/
void removeNeighbors();
}
\ No newline at end of file
......
......@@ -31,7 +31,8 @@ public interface OspfLinkListener {
/**
* Notifies that a link got removed from network.
*
* @param ospfRouter router instance
* @param ospfRouter router instance
* @param ospfLinkTed link TED information of router
*/
void deleteLink(OspfRouter ospfRouter);
void deleteLink(OspfRouter ospfRouter, OspfLinkTed ospfLinkTed);
}
\ No newline at end of file
......
......@@ -13,12 +13,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.ospf.protocol.ospfpacket;
package org.onosproject.ospf.controller;
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.
......@@ -26,6 +24,20 @@ import org.onosproject.ospf.protocol.util.OspfPacketType;
public interface OspfMessage {
/**
* Returns the interface index on which the message received.
*
* @return interface index on which the message received
*/
int interfaceIndex();
/**
* Sets the interface index on which the message received.
*
* @param interfaceIndex interface index on which the message received
*/
void setInterfaceIndex(int interfaceIndex);
/**
* Returns the type of OSPF message.
*
* @return OSPF message type
......@@ -36,9 +48,9 @@ public interface OspfMessage {
* Reads from ChannelBuffer and initializes the type of LSA.
*
* @param channelBuffer channel buffer instance
* @throws OspfParseException might throws exception while parsing buffer
* @throws Exception might throws exception while parsing buffer
*/
void readFrom(ChannelBuffer channelBuffer) throws OspfParseException;
void readFrom(ChannelBuffer channelBuffer) throws Exception;
/**
* Returns OSPFMessage as byte array.
......
......@@ -189,4 +189,36 @@ public interface OspfNbr {
* @return neighbor's state
*/
OspfNeighborState getState();
/**
* Starts the inactivity timer.
*/
void startInactivityTimeCheck();
/**
* Stops the inactivity timer.
*/
void stopInactivityTimeCheck();
/**
* Sets router dead interval.
*
* @param routerDeadInterval router dead interval
*/
void setRouterDeadInterval(int routerDeadInterval);
/**
* Stops the flooding timer.
*/
void stopFloodingTimer();
/**
* Stops the Dd Retransmission executor task.
*/
void stopRxMtDdTimer();
/**
* Stops Ls request retransmission executor task.
*/
void stopRxMtLsrTimer();
}
\ No newline at end of file
......
......@@ -13,17 +13,32 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.ospf.protocol.util;
package org.onosproject.ospf.controller;
/**
* Representation of different OSPF packet types.
*/
public enum OspfPacketType {
/**
* OSPF hello packet.
*/
HELLO(1),
/**
* OSPF device description packet.
*/
DD(2),
/**
* OSPF link state request packet.
*/
LSREQUEST(3),
/**
* OSPF link state update packet.
*/
LSUPDATE(4),
/**
* OSPF link state acknowledge packet.
*/
LSAACK(5);
private int value;
......
......@@ -50,7 +50,7 @@ public interface TopologyForDeviceAndLink {
/**
* Sets link information.
*
* @param key key used to store in map
* @param key key used to store in map
* @param linkInformationMap link information instance
*/
void setLinkInformationMap(String key, LinkInformation linkInformationMap);
......@@ -109,4 +109,56 @@ public interface TopologyForDeviceAndLink {
* @param ospfArea area instance
*/
void updateLinkInformation(OspfLsa ospfLsa, OspfArea ospfArea);
/**
* Gets deviceInformation as map.
*
* @return deviceInformationMap to delete from core
*/
Map<String, DeviceInformation> deviceInformationMapToDelete();
/**
* Sets deviceInformation as map.
*
* @param key key to store in device info map
* @param deviceInformationMapToDelete device information instance
*/
void setDeviceInformationMapToDelete(String key, DeviceInformation deviceInformationMapToDelete);
/**
* Removes Device Information from deviceInformationMapToDelete.
*
* @param key key to remove device information
*/
void removeDeviceInformationMapFromDeleteMap(String key);
/**
* Gets deviceInformation as map for Point-To-Point.
*
* @return deviceInformationMap
*/
Map<String, DeviceInformation> deviceInformationMapForPointToPoint();
/**
* Sets deviceInformation as map for Point-To-Point..
*
* @param key key to store in device info
* @param deviceInformationMap device information instance
*/
void setDeviceInformationMapForPointToPoint(String key, DeviceInformation deviceInformationMap);
/**
* Gets linkInformation as map for PointToPoint.
*
* @return linkInformationMap
*/
Map<String, LinkInformation> linkInformationMapForPointToPoint();
/**
* Sets linkInformation as map for PointToPoint.
*
* @param key key to store link info
* @param linkInformationMap link information instance
*/
void setLinkInformationMapForPointToPoint(String key, LinkInformation linkInformationMap);
}
\ No newline at end of file
......
......@@ -21,7 +21,6 @@ import com.google.common.base.Objects;
import org.onlab.packet.Ip4Address;
import org.onosproject.ospf.controller.LsaWrapper;
import org.onosproject.ospf.controller.OspfArea;
import org.onosproject.ospf.controller.OspfAreaAddressRange;
import org.onosproject.ospf.controller.OspfInterface;
import org.onosproject.ospf.controller.OspfLsa;
import org.onosproject.ospf.controller.OspfLsaType;
......@@ -54,28 +53,14 @@ import java.util.Map;
public class OspfAreaImpl implements OspfArea {
private static final Logger log = LoggerFactory.getLogger(OspfAreaImpl.class);
/**
* Address ranges in order to aggregate routing information at area.
* boundaries. Each address range is specified by an [address,mask] pair and
* a status indication of either Advertise or DoNotAdvertise
*/
private List<OspfAreaAddressRange> addressRanges;
/**
* This parameter indicates whether the area can carry data traffic that.
* neither originates nor terminates in the area itself.
*/
private boolean transitCapability;
/**
* Whether AS-external-LSAs will be flooded into/throughout the area.
*/
private boolean externalRoutingCapability;
/**
* Indicates the cost of the default summary-LSA.
*/
private int stubCost;
/**
* Represents a list of all router's interfaces associated with this area.
*/
private List<OspfInterface> interfacesLst;
private List<OspfInterface> ospfInterfaceList;
/**
* The LS Database for this area. It includes router-LSAs, network-LSAs and.
* summary-LSAs. AS-external-LSAs are hold in the OSPF class itself.
......@@ -116,18 +101,15 @@ public class OspfAreaImpl implements OspfArea {
OspfAreaImpl that = (OspfAreaImpl) o;
return Objects.equal(areaId, that.areaId) &&
Objects.equal(routerId, that.routerId) &&
Objects.equal(addressRanges.size(), that.addressRanges.size()) &&
Objects.equal(transitCapability, that.transitCapability) &&
Objects.equal(externalRoutingCapability, that.externalRoutingCapability) &&
Objects.equal(stubCost, that.stubCost) &&
Objects.equal(interfacesLst.size(), that.interfacesLst.size()) &&
Objects.equal(ospfInterfaceList.size(), that.ospfInterfaceList.size()) &&
Objects.equal(database, that.database);
}
@Override
public int hashCode() {
return Objects.hashCode(areaId, routerId, addressRanges, transitCapability, externalRoutingCapability,
stubCost, interfacesLst, database);
return Objects.hashCode(areaId, routerId, externalRoutingCapability,
ospfInterfaceList, database);
}
/**
......@@ -234,7 +216,7 @@ public class OspfAreaImpl implements OspfArea {
networkLsa.setNetworkMask(mask);
//Adding our own router.
networkLsa.addAttachedRouter(routerId());
Iterator iter = interfacesLst.iterator();
Iterator iter = ospfInterfaceList.iterator();
OspfInterfaceImpl ospfInterface = null;
while (iter.hasNext()) {
ospfInterface = (OspfInterfaceImpl) iter.next();
......@@ -310,7 +292,7 @@ public class OspfAreaImpl implements OspfArea {
*/
private void buildLinkForRouterLsa(RouterLsa routerLsa, OspfInterface ospfInterface) {
OspfInterfaceImpl nextInterface;
Iterator interfaces = interfacesLst.iterator();
Iterator interfaces = ospfInterfaceList.iterator();
while (interfaces.hasNext()) {
nextInterface = (OspfInterfaceImpl) interfaces.next();
if (nextInterface.state() == OspfInterfaceState.DOWN) {
......@@ -418,44 +400,6 @@ public class OspfAreaImpl implements OspfArea {
}
/**
* Gets address range.
*
* @return list of area address ranges
*/
public List<OspfAreaAddressRange> addressRanges() {
return addressRanges;
}
/**
* Sets the area address ranges.
*
* @param addressRanges list of area address range
*/
@JsonProperty("addressRange")
public void setAddressRanges(List<OspfAreaAddressRange> addressRanges) {
this.addressRanges = addressRanges;
}
/**
* Gets is transit capable or not.
*
* @return true if transit capable, else false
*/
public boolean isTransitCapability() {
return transitCapability;
}
/**
* Sets transit capability.
*
* @param transitCapability true if transit capable, else false
*/
@JsonProperty("transitCapability")
public void setTransitCapability(boolean transitCapability) {
this.transitCapability = transitCapability;
}
/**
* Gets external routing capability.
*
* @return true if external routing capable, else false
......@@ -475,41 +419,22 @@ public class OspfAreaImpl implements OspfArea {
}
/**
* Gets the stub cost.
*
* @return stub cost
*/
public int stubCost() {
return stubCost;
}
/**
* Sets the stub cost.
*
* @param stubCost stub cost
*/
@JsonProperty("stubCost")
public void setStubCost(int stubCost) {
this.stubCost = stubCost;
}
/**
* Gets the list of interfaces in this area.
*
* @return list of interfaces
*/
public List<OspfInterface> getInterfacesLst() {
return interfacesLst;
public List<OspfInterface> ospfInterfaceList() {
return ospfInterfaceList;
}
/**
* Sets the list of interfaces attached to the area.
*
* @param interfacesLst list of OspfInterface instances
* @param ospfInterfaceList list of OspfInterface instances
*/
@JsonProperty("interface")
public void setInterfacesLst(List<OspfInterface> interfacesLst) {
this.interfacesLst = interfacesLst;
public void setOspfInterfaceList(List<OspfInterface> ospfInterfaceList) {
this.ospfInterfaceList = ospfInterfaceList;
}
/**
......@@ -522,7 +447,7 @@ public class OspfAreaImpl implements OspfArea {
public boolean noNeighborInLsaExchangeProcess() {
OspfInterfaceImpl nextInterface;
OspfNeighborState nextNeighborState;
Iterator interfaces = interfacesLst.iterator();
Iterator interfaces = ospfInterfaceList.iterator();
while (interfaces.hasNext()) {
nextInterface = (OspfInterfaceImpl) interfaces.next();
Iterator neighbors = nextInterface.listOfNeighbors().values().iterator();
......@@ -659,19 +584,14 @@ public class OspfAreaImpl implements OspfArea {
return MoreObjects.toStringHelper(getClass())
.omitNullValues()
.add("areaID", areaId)
.add("stubCost", stubCost)
.add("addressRanges", addressRanges)
.add("interfacesLst", interfacesLst)
.add("transitCapability", transitCapability)
.add("ospfInterfaceList", ospfInterfaceList)
.add("externalRoutingCapability", externalRoutingCapability)
.toString();
}
/**
* Checks all Neighbors belonging to this Area whether they are in state lesser than the EXCHANGE.
* <p>
* Creates list of such neighbors
* <p>
* Returns list of neighbors who satisfy the conditions
*
* @param ospfInterface OSPF interface instance
......@@ -716,7 +636,7 @@ public class OspfAreaImpl implements OspfArea {
public void addToOtherNeighborLsaTxList(LsaHeader recLsa) {
//Add the received LSA in other neighbors retransmission list.
log.debug("OspfAreaImpl: addToOtherNeighborLsaTxList");
List<OspfInterface> ospfInterfaces = getInterfacesLst();
List<OspfInterface> ospfInterfaces = ospfInterfaceList();
for (OspfInterface ospfInterfaceFromArea : ospfInterfaces) {
Map neighbors = ospfInterfaceFromArea.listOfNeighbors();
for (Object neighborIP : neighbors.keySet()) {
......
......@@ -16,6 +16,7 @@
package org.onosproject.ospf.controller.impl;
import com.fasterxml.jackson.databind.JsonNode;
import com.google.common.collect.Sets;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
......@@ -34,7 +35,6 @@ import org.onosproject.ospf.controller.OspfRouterListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
......@@ -111,27 +111,23 @@ public class OspfControllerImpl implements OspfController {
}
@Override
public void updateConfig(List processes) {
List<OspfProcess> ospfProcesses = new ArrayList<>();
if (processes != null) {
for (Object process : processes) {
ospfProcesses.add((OspfProcess) process);
public void updateConfig(JsonNode processesNode) {
try {
List<OspfProcess> ospfProcesses = OspfConfigUtil.processes(processesNode);
//if there is interface details then update configuration
if (ospfProcesses.size() > 0 &&
ospfProcesses.get(0).areas() != null && ospfProcesses.get(0).areas().size() > 0 &&
ospfProcesses.get(0).areas().get(0) != null &&
ospfProcesses.get(0).areas().get(0).ospfInterfaceList().size() > 0) {
ctrl.updateConfig(ospfProcesses);
}
} catch (Exception e) {
log.debug("Error::updateConfig::{}", e.getMessage());
}
log.debug("updateConfig::OspfList::processes::{}", ospfProcesses);
ctrl.updateConfig(ospfProcesses);
}
@Override
public void deleteConfig(List<OspfProcess> processes, String attribute) {
List<OspfProcess> ospfProcesses = new ArrayList<>();
if (processes != null) {
for (Object process : processes) {
ospfProcesses.add((OspfProcess) process);
}
}
log.debug("deleteConfig::OspfList::processes::{}", ospfProcesses);
ctrl.deleteConfig(ospfProcesses, attribute);
}
/**
......@@ -163,9 +159,9 @@ public class OspfControllerImpl implements OspfController {
}
@Override
public void deleteLink(OspfRouter ospfRouter) {
public void deleteLink(OspfRouter ospfRouter, OspfLinkTed ospfLinkTed) {
for (OspfLinkListener l : linkListener()) {
l.deleteLink(ospfRouter);
l.deleteLink(ospfRouter, ospfLinkTed);
}
}
}
......
......@@ -19,8 +19,10 @@ import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.handler.codec.frame.FrameDecoder;
import org.onosproject.ospf.protocol.ospfpacket.OspfMessage;
import org.onlab.packet.Ip4Address;
import org.onosproject.ospf.controller.OspfMessage;
import org.onosproject.ospf.protocol.ospfpacket.OspfMessageReader;
import org.onosproject.ospf.protocol.util.OspfUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -35,24 +37,35 @@ public class OspfMessageDecoder extends FrameDecoder {
private static final Logger log = LoggerFactory.getLogger(OspfMessageDecoder.class);
@Override
protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer channelBuffer) throws Exception {
log.debug("OspfMessageDecoder::Message received <:> length {}", channelBuffer.readableBytes());
log.debug("channelBuffer.readableBytes - decode {}", channelBuffer.readableBytes());
protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer) throws Exception {
log.debug("OspfMessageDecoder::Message received <:> length {}", buffer.readableBytes());
if (!channel.isConnected()) {
log.info("Channel is not connected.");
return null;
}
OspfMessageReader messageReader = new OspfMessageReader();
List<OspfMessage> ospfMessageList = new LinkedList<>();
while (channelBuffer.readableBytes() > 0) {
OspfMessage message = messageReader.readFromBuffer(channelBuffer);
while (buffer.readableBytes() >= OspfUtil.MINIMUM_FRAME_LEN) {
ChannelBuffer ospfDataBuffer = buffer.readBytes(OspfUtil.MINIMUM_FRAME_LEN);
int readableBytes = ospfDataBuffer.readableBytes();
OspfMessage message = messageReader.readFromBuffer(ospfDataBuffer);
if (message != null) {
if (ospfDataBuffer.readableBytes() >= OspfUtil.METADATA_LEN) {
ospfDataBuffer.readerIndex(readableBytes - OspfUtil.METADATA_LEN);
log.debug("IsisMessageDecoder::Reading metadata <:> length {}", ospfDataBuffer.readableBytes());
int interfaceIndex = ospfDataBuffer.readByte();
byte[] sourceIpBytes = new byte[OspfUtil.FOUR_BYTES];
ospfDataBuffer.readBytes(sourceIpBytes, 0, OspfUtil.FOUR_BYTES);
Ip4Address sourceIP = Ip4Address.valueOf(sourceIpBytes);
message.setSourceIp(sourceIP);
message.setInterfaceIndex(interfaceIndex);
}
ospfMessageList.add(message);
}
}
return ospfMessageList;
return (ospfMessageList.size() > 0) ? ospfMessageList : null;
}
}
\ No newline at end of file
......
......@@ -17,15 +17,10 @@
package org.onosproject.ospf.controller.impl;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.handler.codec.oneone.OneToOneEncoder;
import org.onosproject.ospf.controller.OspfInterface;
import org.onosproject.ospf.controller.area.OspfInterfaceImpl;
import org.onosproject.ospf.protocol.ospfpacket.OspfMessage;
import org.onosproject.ospf.protocol.ospfpacket.OspfMessageWriter;
import org.onosproject.ospf.protocol.util.OspfInterfaceState;
import org.onosproject.ospf.protocol.util.OspfUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -35,46 +30,15 @@ import org.slf4j.LoggerFactory;
public class OspfMessageEncoder extends OneToOneEncoder {
private static final Logger log = LoggerFactory.getLogger(OspfMessageEncoder.class);
private OspfInterface ospfInterface;
/**
* Creates an instance of OSPF message encoder.
*/
OspfMessageEncoder() {
}
/**
* Creates an instance of OSPF message encoder.
*
* @param ospfInterface OSPF interface instance
*/
OspfMessageEncoder(OspfInterface ospfInterface) {
this.ospfInterface = ospfInterface;
}
@Override
protected Object encode(ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception {
log.debug("Encoding ospfMessage...!!!");
if (!(msg instanceof OspfMessage)) {
log.debug("Invalid msg.");
return msg;
}
OspfMessage ospfMessage = (OspfMessage) msg;
OspfMessageWriter messageWriter = new OspfMessageWriter();
if (((OspfInterfaceImpl) ospfInterface).state().equals(OspfInterfaceState.POINT2POINT)) {
ospfMessage.setDestinationIp(OspfUtil.ALL_SPF_ROUTERS);
}
ChannelBuffer buf = messageWriter.writeToBuffer(ospfMessage,
((OspfInterfaceImpl) ospfInterface).state().value(),
ospfInterface.interfaceType());
log.info("OspfMessageEncoder sending packet of lenght {}", buf.readableBytes());
log.debug("OspfMessageEncoder sending packet of lenght {}", buf.readableBytes());
log.debug("Sending {} Message to {}, Length :: {}, <=> {}", ospfMessage.ospfMessageType(),
ospfMessage.destinationIp(), buf.readableBytes(), buf.array());
byte[] byteMsg = (byte[]) msg;
log.debug("Encoding ospfMessage of length {}", byteMsg.length);
ChannelBuffer channelBuffer = ChannelBuffers.buffer(byteMsg.length);
channelBuffer.writeBytes(byteMsg);
return buf;
return channelBuffer;
}
}
......
......@@ -18,57 +18,29 @@ package org.onosproject.ospf.controller.impl;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.handler.timeout.ReadTimeoutHandler;
import org.jboss.netty.util.ExternalResourceReleasable;
import org.jboss.netty.util.HashedWheelTimer;
import org.jboss.netty.util.Timer;
import org.onosproject.ospf.controller.OspfArea;
import org.onosproject.ospf.controller.OspfInterface;
/**
* Creates a ChannelPipeline for a server-side OSPF channel.
* Creates a ChannelPipeline for a client-side OSPF channel.
*/
public class OspfPipelineFactory implements ChannelPipelineFactory, ExternalResourceReleasable {
private static final Timer TIMER = new HashedWheelTimer();
private Controller controller;
private ReadTimeoutHandler readTimeoutHandler;
private OspfArea ospfArea;
private OspfInterface ospfInterface;
private int holdTime = 120 * 1000;
public class OspfPipelineFactory implements ChannelPipelineFactory {
private OspfInterfaceChannelHandler ospfChannelHandler;
/**
* Creates an instance of OSPF pipeline factory.
* Creates an instance of OSPF channel pipeline factory.
*
* @param controller controller instance.
* @param ospfArea OSPF area instance.
* @param ospfInterface OSPF interface instance.
* @param ospfChannelHandler OSPF channel handler instance
*/
public OspfPipelineFactory(Controller controller, OspfArea ospfArea, OspfInterface ospfInterface) {
super();
this.controller = controller;
this.ospfArea = ospfArea;
this.ospfInterface = ospfInterface;
readTimeoutHandler = new ReadTimeoutHandler(TIMER, holdTime);
public OspfPipelineFactory(OspfInterfaceChannelHandler ospfChannelHandler) {
this.ospfChannelHandler = ospfChannelHandler;
}
@Override
public ChannelPipeline getPipeline() throws Exception {
OspfInterfaceChannelHandler interfaceHandler = new OspfInterfaceChannelHandler(
controller, ospfArea, ospfInterface);
ChannelPipeline pipeline = Channels.pipeline();
pipeline.addLast("encoder", new OspfMessageEncoder(ospfInterface));
pipeline.addLast("decoder", new OspfMessageDecoder());
pipeline.addLast("holdTime", readTimeoutHandler);
pipeline.addLast("interfacehandler", interfaceHandler);
pipeline.addLast("encoder", new OspfMessageDecoder());
pipeline.addLast("decoder", new OspfMessageEncoder());
pipeline.addLast("handler", ospfChannelHandler);
return pipeline;
}
@Override
public void releaseExternalResources() {
TIMER.stop();
}
}
\ No newline at end of file
......
......@@ -49,6 +49,15 @@ public class LsaWrapperImpl implements LsaWrapper {
private int binNumber = -1;
private OspfInterface ospfInterface;
private LsdbAge lsdbAge;
private int ageCounterRollOverWhenAdded;
public int getAgeCounterRollOverWhenAdded() {
return ageCounterRollOverWhenAdded;
}
public void setAgeCounterRollOverWhenAdded(int ageCounterRollOverWhenAdded) {
this.ageCounterRollOverWhenAdded = ageCounterRollOverWhenAdded;
}
/**
* Gets the LSA type.
......@@ -318,6 +327,8 @@ public class LsaWrapperImpl implements LsaWrapper {
if (currentAge >= OspfParameters.MAXAGE) {
return OspfParameters.MAXAGE;
} else if ((currentAge == lsaAgeReceived) && ageCounterRollOverWhenAdded != lsdbAge.getAgeCounterRollOver()) {
return OspfParameters.MAXAGE;
}
return currentAge;
......
......@@ -41,7 +41,7 @@ public class LsdbAgeImpl implements LsdbAge {
private static final Logger log =
LoggerFactory.getLogger(LsdbAgeImpl.class);
protected int ageCounter = 0;
protected static int ageCounter = 0;
private InternalAgeTimer dbAgeTimer;
private ScheduledExecutorService exServiceage;
// creating age bins of MAXAGE
......
......@@ -267,6 +267,7 @@ public class OspfLsdbImpl implements OspfLsdb {
lsaWrapper.setLsaHeader(newLsa);
lsaWrapper.setLsaAgeReceived(newLsa.age());
lsaWrapper.setAgeCounterWhenReceived(lsdbAge.getAgeCounter());
lsaWrapper.setAgeCounterRollOverWhenAdded(lsdbAge.getAgeCounterRollOver());
lsaWrapper.setIsSelfOriginated(isSelfOriginated);
lsaWrapper.setIsSelfOriginated(isSelfOriginated);
lsaWrapper.setOspfInterface(ospfInterface);
......
......@@ -71,7 +71,7 @@ public class ControllerTest {
ospfInterface.setIpAddress(Ip4Address.valueOf("1.1.1.1"));
ospfInterfaces.add(ospfInterface);
ospfArea.setAreaId(Ip4Address.valueOf("2.2.2.2"));
ospfArea.setInterfacesLst(ospfInterfaces);
ospfArea.setOspfInterfaceList(ospfInterfaces);
ospfProcess.setProcessId("10.10.10.10");
ospfAreas = new ArrayList();
ospfAreas.add(ospfArea);
......@@ -82,7 +82,7 @@ public class ControllerTest {
ospfProcess1.setProcessId("11.11.11.11");
ospfArea1 = new OspfAreaImpl();
ospfArea1.setAreaId(Ip4Address.valueOf("2.2.2.2"));
ospfArea1.setInterfacesLst(ospfInterfaces);
ospfArea1.setOspfInterfaceList(ospfInterfaces);
ospfAreas.add(ospfArea1);
ospfProcess1.setAreas(ospfAreas);
ospfProcesses.add(ospfProcess1);
......@@ -167,83 +167,6 @@ public class ControllerTest {
assertThat(controller, is(notNullValue()));
}
/**
* Tests deleteInterfaceFromArea() method.
*/
@Test
public void testDeleteInterfaceFromArea() throws Exception {
controller.updateConfig(ospfProcesses);
assertThat(controller.deleteInterfaceFromArea("10.10.10.10", "2.2.2.2", "1.1.1.1"), is(true));
assertThat(controller.deleteInterfaceFromArea("10.10.10.10", "2.2.2.2", "5.5.5.5"), is(false));
}
/**
* Tests checkArea() method.
*/
@Test
public void testCheckArea() throws Exception {
controller.updateConfig(ospfProcesses);
assertThat(controller.checkArea("10.10.10.10", "2.2.2.2"), is(true));
}
/**
* Tests checkArea() method.
*/
@Test
public void testCheckArea1() throws Exception {
controller.updateConfig(ospfProcesses);
assertThat(controller.checkArea("10.10.10.10", "111.111.111.111"), is(false));
}
/**
* Tests checkProcess() method.
*/
@Test
public void testCheckProcess() throws Exception {
controller.updateConfig(ospfProcesses);
assertThat(controller.checkProcess("3.3.3.3"), is(false));
assertThat(controller.checkProcess("1.1.1.1"), is(false));
}
/**
* Tests checkInterface() method.
*/
@Test
public void testCheckInterface() throws Exception {
controller.updateConfig(ospfProcesses);
assertThat(controller.checkInterface("10.10.10.10", "2.2.2.2", "1.1.1.1"), is(true));
}
/**
* Tests updateAreaInProcess() method.
*/
@Test
public void testUpdateAreaInProcess() throws Exception {
controller.updateConfig(ospfProcesses);
controller.updateAreaInProcess("10.10.10.10", "2.2.2.2", ospfArea);
assertThat(controller, is(notNullValue()));
}
/**
* Tests updateConfig() method.
*/
@Test
public void testUpdateConfig() throws Exception {
controller.updateConfig(ospfProcesses);
controller.updateConfig(ospfProcesses);
controller.updateConfig(ospfProcesses);
assertThat(controller, is(notNullValue()));
}
/**
* Tests updateConfig() method.
*/
@Test
public void testUpdateConfig2() throws Exception {
controller.updateConfig(ospfProcesses);
controller.updateConfig(ospfProcesses);
assertThat(controller, is(notNullValue()));
}
/**
* Tests updateConfig() method.
......@@ -257,7 +180,7 @@ public class ControllerTest {
ospfInterface.setIpAddress(Ip4Address.valueOf("10.10.10.5"));
ospfInterfaces.add(ospfInterface);
ospfArea.setAreaId(Ip4Address.valueOf("2.2.2.2"));
ospfArea.setInterfacesLst(ospfInterfaces);
ospfArea.setOspfInterfaceList(ospfInterfaces);
ospfProcess.setProcessId("10.10.10.10");
ospfAreas = new ArrayList();
ospfAreas.add(ospfArea);
......@@ -269,67 +192,6 @@ public class ControllerTest {
}
/**
* Tests deleteConfig() method.
*/
@Test(expected = Exception.class)
public void testDeleteConfig() throws Exception {
controller.updateConfig(ospfProcesses);
controller.deleteConfig(ospfProcesses, "INTERFACE");
assertThat(controller, is(notNullValue()));
}
/**
* Tests deleteConfig() method.
*/
@Test(expected = Exception.class)
public void testDeleteConfig1() throws Exception {
controller.updateConfig(ospfProcesses);
controller.deleteConfig(ospfProcesses, "AREA");
assertThat(controller, is(notNullValue()));
}
/**
* Tests deleteConfig() method.
*/
@Test
public void testDeleteConfig2() throws Exception {
controller.updateConfig(ospfProcesses);
controller.deleteConfig(ospfProcesses, "PROCESS");
assertThat(controller, is(notNullValue()));
}
/**
* Tests deleteConfig() method.
*/
@Test
public void testDeleteConfig3() throws Exception {
ospfProcesses = new ArrayList();
controller.deleteConfig(ospfProcesses, "PROCESS");
assertThat(controller, is(notNullValue()));
}
/**
* Tests deleteConfig() method.
*/
@Test
public void testDeleteConfig4() throws Exception {
controller.updateConfig(ospfProcesses);
controller.deleteConfig(ospfProcesses, "PROCESS");
controller.updateConfig(ospfProcesses);
assertThat(controller, is(notNullValue()));
}
/**
* Tests deleteProcessWhenExists() method.
*/
@Test
public void testDeleteProcessWhenExists() throws Exception {
controller.updateConfig(ospfProcesses);
controller.deleteProcessWhenExists(ospfProcesses, "PROCESS");
}
/**
* Tests addLinkDetails() method.
*/
@Test
......@@ -350,7 +212,7 @@ public class ControllerTest {
controller.start(ospfAgent, driverService);
ospfRouter = new OspfRouterImpl();
controller.addLinkDetails(ospfRouter, new OspfLinkTedImpl());
controller.removeLinkDetails(ospfRouter);
controller.removeLinkDetails(ospfRouter, new OspfLinkTedImpl());
assertThat(controller, is(notNullValue()));
}
}
\ No newline at end of file
......
/*
* Copyright 2016-present 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.controller.impl;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.onosproject.ospf.controller.OspfProcess;
import java.util.ArrayList;
import java.util.List;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;
/**
* Unit test class for OspfJsonParsingUtilTest.
*/
public class OspfConfigUtilTest {
private ObjectMapper mapper;
private JsonNode jsonNode;
private List<OspfProcess> ospfProcessList = new ArrayList<>();
private String jsonString = "{\n" +
"\t\"processes\": {\n" +
"\t\t\"areas\": [{\n" +
"\t\t\t\"interface\": [{\n" +
"\t\t\t\t\"interfaceIndex\": \"2\",\n" +
"\n" +
"\t\t\t\t\"helloIntervalTime\": \"10\",\n" +
"\n" +
"\t\t\t\t\"routerDeadIntervalTime\": \"40\",\n" +
"\n" +
"\t\t\t\t\"interfaceType\": \"2\",\n" +
"\n" +
"\t\t\t\t\"reTransmitInterval\": \"5\"\n" +
"\t\t\t}],\n" +
"\t\t\t\"areaId\": \"5.5.5.5\",\n" +
"\n" +
"\t\t\t\"routerId\": \"7.7.7.7\",\n" +
"\n" +
"\t\t\t\"isOpaqueEnable\": \"false\",\n" +
"\n" +
"\t\t\t\"externalRoutingCapability\": \"true\"\n" +
"\t\t}]\n" +
"\t}\n" +
"}";
@Before
public void setUp() throws Exception {
mapper = new ObjectMapper();
jsonNode = mapper.readTree(jsonString);
mapper = new ObjectMapper();
}
@After
public void tearDown() throws Exception {
}
@Test
public void testProcesses() throws Exception {
jsonNode.path("areas");
ospfProcessList = OspfConfigUtil.processes(jsonNode);
assertThat(ospfProcessList, is(notNullValue()));
}
}
\ No newline at end of file
......@@ -46,10 +46,8 @@ public class OspfControllerImplTest {
private OspfControllerImpl ospfController;
private OspfRouterListener ospfRouterListener;
private OspfLinkListener ospfLinkListener;
private Controller controller;
private List<OspfProcess> ospfProcesses;
private OspfProcess process1;
private OspfProcess process2;
private List<OspfArea> areas;
private OspfAreaImpl ospfArea;
private List<OspfInterface> ospfInterfaces;
......@@ -61,7 +59,6 @@ public class OspfControllerImplTest {
@Before
public void setUp() throws Exception {
ospfController = new OspfControllerImpl();
controller = new Controller();
}
@After
......@@ -69,7 +66,6 @@ public class OspfControllerImplTest {
ospfController = null;
ospfRouterListener = null;
ospfLinkListener = null;
controller = null;
ospfProcesses = null;
areas = null;
ospfArea = null;
......@@ -140,38 +136,6 @@ public class OspfControllerImplTest {
}
/**
* Tests updateConfig() method.
*/
@Test
public void testUpdateConfig() throws Exception {
ospfProcess = new OspfProcessImpl();
ospfArea = new OspfAreaImpl();
ospfInterface = new OspfInterfaceImpl();
ospfInterfaces = new ArrayList();
ospfInterface.setIpAddress(Ip4Address.valueOf("11.11.11.11"));
ospfInterfaces.add(ospfInterface);
ospfArea.setAreaId(Ip4Address.valueOf("2.2.2.2"));
ospfArea.setInterfacesLst(ospfInterfaces);
ospfProcess.setProcessId("10.10.10.10");
areas = new ArrayList();
areas.add(ospfArea);
ospfProcess.setAreas(areas);
ospfProcesses = new ArrayList();
ospfProcesses.add(ospfProcess);
process1 = new OspfProcessImpl();
process1.setProcessId("11.11.11.11");
ospfArea1 = new OspfAreaImpl();
ospfArea1.setAreaId(Ip4Address.valueOf("2.2.2.2"));
ospfArea1.setInterfacesLst(ospfInterfaces);
areas.add(ospfArea1);
process1.setAreas(areas);
ospfProcesses.add(process1);
ospfController.updateConfig(ospfProcesses);
assertThat(ospfController, is(notNullValue()));
}
/**
* Tests deleteConfig() method.
*/
@Test
......@@ -183,7 +147,7 @@ public class OspfControllerImplTest {
ospfInterface.setIpAddress(Ip4Address.valueOf("10.10.10.5"));
ospfInterfaces.add(ospfInterface);
ospfArea.setAreaId(Ip4Address.valueOf("2.2.2.2"));
ospfArea.setInterfacesLst(ospfInterfaces);
ospfArea.setOspfInterfaceList(ospfInterfaces);
ospfProcess.setProcessId("10.10.10.10");
areas = new ArrayList();
areas.add(ospfArea);
......@@ -194,7 +158,7 @@ public class OspfControllerImplTest {
process1.setProcessId("11.11.11.11");
ospfArea1 = new OspfAreaImpl();
ospfArea1.setAreaId(Ip4Address.valueOf("2.2.2.2"));
ospfArea1.setInterfacesLst(ospfInterfaces);
ospfArea1.setOspfInterfaceList(ospfInterfaces);
areas.add(ospfArea1);
process1.setAreas(areas);
ospfProcesses.add(process1);
......@@ -221,7 +185,7 @@ public class OspfControllerImplTest {
ospfRouter = new OspfRouterImpl();
ospfController.agent.addLink(ospfRouter, new OspfLinkTedImpl());
ospfController.agent.deleteLink(ospfRouter);
ospfController.agent.deleteLink(ospfRouter, new OspfLinkTedImpl());
assertThat(ospfController, is(notNullValue()));
}
......
......@@ -21,6 +21,7 @@ import org.junit.Test;
import org.onlab.packet.Ip4Address;
import org.onlab.packet.Ip6Address;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.List;
......@@ -31,8 +32,6 @@ import static org.junit.Assert.assertThat;
* Unit test class for OspfDeviceTedImpl.
*/
public class OspfDeviceTedImplTest {
private static final Ip6Address LOCAL_ADDRESS = Ip6Address.valueOf("::1");
private OspfDeviceTedImpl ospfDeviceTed;
@Before
......@@ -128,10 +127,10 @@ public class OspfDeviceTedImplTest {
/**
* Tests ipv6RouterIds() getter method.
*/
@Test
@Test(expected = Exception.class)
public void testIpv6RouterIds() throws Exception {
List list = new ArrayList();
list.add(LOCAL_ADDRESS);
list.add(Ip6Address.valueOf(InetAddress.getLocalHost()));
ospfDeviceTed.setIpv6RouterIds(list);
assertThat(ospfDeviceTed.ipv6RouterIds().size(), is(1));
}
......@@ -139,11 +138,11 @@ public class OspfDeviceTedImplTest {
/**
* Tests ipv6RouterIds() setter method.
*/
@Test
@Test(expected = Exception.class)
public void testSetIpv6RouterIds() throws Exception {
List list = new ArrayList();
list.add(LOCAL_ADDRESS);
list.add(Ip6Address.valueOf(InetAddress.getLocalHost()));
ospfDeviceTed.setIpv6RouterIds(list);
assertThat(ospfDeviceTed.ipv6RouterIds().size(), is(1));
}
}
}
\ No newline at end of file
......
......@@ -21,6 +21,7 @@ import org.junit.Test;
import org.onlab.packet.Ip4Address;
import org.onlab.util.Bandwidth;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.List;
......@@ -32,8 +33,6 @@ import static org.junit.Assert.assertThat;
* Unit test class for OspfDeviceTedImpl.
*/
public class OspfLinkTedImplTest {
private static final Ip4Address LOCAL_ADDRESS = Ip4Address.valueOf("127.0.0.1");
private OspfLinkTedImpl ospfLinkTed;
@Before
......@@ -91,7 +90,7 @@ public class OspfLinkTedImplTest {
@Test
public void testIpv4RemRouterId() throws Exception {
List list = new ArrayList();
list.add(LOCAL_ADDRESS);
list.add(Ip4Address.valueOf(InetAddress.getLocalHost()));
ospfLinkTed.setIpv4RemRouterId(list);
assertThat(ospfLinkTed.ipv4RemRouterId().size(), is(1));
}
......@@ -102,7 +101,7 @@ public class OspfLinkTedImplTest {
@Test
public void testSetIpv4RemRouterId() throws Exception {
List list = new ArrayList();
list.add(LOCAL_ADDRESS);
list.add(Ip4Address.valueOf(InetAddress.getLocalHost()));
ospfLinkTed.setIpv4RemRouterId(list);
assertThat(ospfLinkTed.ipv4RemRouterId().size(), is(1));
}
......@@ -133,7 +132,7 @@ public class OspfLinkTedImplTest {
@Test
public void testIpv4LocRouterId() throws Exception {
List list = new ArrayList();
list.add(LOCAL_ADDRESS);
list.add(Ip4Address.valueOf(InetAddress.getLocalHost()));
ospfLinkTed.setIpv4LocRouterId(list);
assertThat(ospfLinkTed.ipv4LocRouterId().size(), is(1));
}
......@@ -144,7 +143,7 @@ public class OspfLinkTedImplTest {
@Test
public void testSetIpv4LocRouterId() throws Exception {
List list = new ArrayList();
list.add(LOCAL_ADDRESS);
list.add(Ip4Address.valueOf(InetAddress.getLocalHost()));
ospfLinkTed.setIpv4LocRouterId(list);
assertThat(ospfLinkTed.ipv4LocRouterId().size(), is(1));
}
......@@ -202,4 +201,4 @@ public class OspfLinkTedImplTest {
ospfLinkTed.setMaxUnResBandwidth(Bandwidth.bps(1234.0));
assertThat(ospfLinkTed.maxUnResBandwidth(), is(notNullValue()));
}
}
}
\ No newline at end of file
......
......@@ -28,7 +28,7 @@ import java.net.InetSocketAddress;
import java.net.SocketAddress;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;
/**
......@@ -36,7 +36,7 @@ import static org.hamcrest.MatcherAssert.assertThat;
*/
public class OspfMessageDecoderTest {
private final byte[] hellopacket = {0, 0, 0, 0, 2, 1, 0, 44, -64, -88, -86, 8, 0, 0, 0, 1, 39, 59,
private final byte[] hellopacket = {0, 0, 0, 0, 0, 2, 1, 0, 44, -64, -88, -86, 8, 0, 0, 0, 1, 39, 59,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, 0, 0, 10, 2, 1, 0, 0, 0,
40, -64, -88, -86, 8, 0, 0, 0, 0};
private final byte[] ddpacket = {0, 0, 0, 0, 2, 2, 0, 32, -64, -88, -86, 8, 0, 0, 0, 1, -96, 82,
......@@ -75,10 +75,10 @@ public class OspfMessageDecoderTest {
*/
@Test
public void testDecode() throws Exception {
channel = EasyMock.createMock(Channel.class);
socketAddress = InetSocketAddress.createUnresolved("127.0.0.1", 7000);
channelBuffer = ChannelBuffers.copiedBuffer(hellopacket);
assertThat(ospfMessageDecoder.decode(ctx, channel, channelBuffer), is(nullValue()));
ospfMessageDecoder.decode(ctx, channel, channelBuffer);
assertThat(ospfMessageDecoder, is(notNullValue()));
}
}
\ No newline at end of file
......
......@@ -17,24 +17,11 @@ package org.onosproject.ospf.controller.impl;
import org.easymock.EasyMock;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.onlab.packet.Ip4Address;
import org.onosproject.ospf.controller.area.OspfInterfaceImpl;
import org.onosproject.ospf.protocol.ospfpacket.types.DdPacket;
import org.onosproject.ospf.protocol.ospfpacket.types.HelloPacket;
import org.onosproject.ospf.protocol.ospfpacket.types.LsAcknowledge;
import org.onosproject.ospf.protocol.ospfpacket.types.LsRequest;
import org.onosproject.ospf.protocol.ospfpacket.types.LsUpdate;
import org.onosproject.ospf.protocol.util.OspfInterfaceState;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
......@@ -44,55 +31,24 @@ import static org.hamcrest.MatcherAssert.assertThat;
* Created by sdn on 13/1/16.
*/
public class OspfMessageEncoderTest {
private final byte[] hpacket = {2, 1, 0, 44, -64, -88, -86, 8, 0, 0, 0, 1, 39, 59,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, 0, 0, 10, 2, 1, 0, 0, 0,
40, -64, -88, -86, 8, 0, 0, 0, 0};
private final byte[] dpacket = {2, 2, 0, 32, -64, -88, -86, 8, 0, 0, 0, 1, -96, 82,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, -36, 2, 7, 65, 119, -87, 126};
private final byte[] lrpacket = {2, 3, 0, 36, -64, -88, -86, 3, 0, 0, 0, 1, -67, -57,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, -64, -88, -86, 8, -64, -88, -86, 8};
private byte[] lAckpacket = {2, 5, 0, 44, -64, -88, -86, 8, 0, 0, 0, 1, -30, -12,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 16, 2, 1, -64, -88, -86, 2, -64,
-88, -86, 2, -128, 0, 0, 1, 74, -114, 0, 48};
private HelloPacket helloPacket;
private DdPacket ddPacket;
private LsAcknowledge lsAcknowledge;
private LsRequest lsRequest;
private LsUpdate lsUpdate;
private ChannelHandlerContext ctx;
private final byte[] object = {2, 1, 0, 44, -64, -88, -86, 8, 0, 0, 0, 1, 39,
59, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, 0, 0, 10, 2, 1, 0, 0,
0, 40, -64, -88, -86, 8, 0, 0, 0, 0};
private OspfMessageEncoder ospfMessageEncoder;
private ChannelBuffer buf;
private SocketAddress socketAddress;
private ChannelHandlerContext channelHandlerContext;
private Channel channel;
@Before
public void setUp() throws Exception {
ospfMessageEncoder = new OspfMessageEncoder();
helloPacket = new HelloPacket();
ddPacket = new DdPacket();
lsAcknowledge = new LsAcknowledge();
lsRequest = new LsRequest();
lsUpdate = new LsUpdate();
helloPacket.setOspftype(1);
ddPacket.setOspftype(2);
lsAcknowledge.setOspftype(5);
lsRequest.setOspftype(3);
lsUpdate.setOspftype(4);
OspfInterfaceImpl ospfInterface = new OspfInterfaceImpl();
ospfInterface.setState(OspfInterfaceState.DROTHER);
ospfMessageEncoder = new OspfMessageEncoder(ospfInterface);
channelHandlerContext = EasyMock.createMock(ChannelHandlerContext.class);
channel = EasyMock.createMock(Channel.class);
}
@After
public void tearDown() throws Exception {
helloPacket = null;
ddPacket = null;
lsAcknowledge = null;
lsRequest = null;
lsUpdate = null;
ospfMessageEncoder = null;
buf = null;
}
/**
......@@ -100,33 +56,6 @@ public class OspfMessageEncoderTest {
*/
@Test
public void testEncode() throws Exception {
socketAddress = InetSocketAddress.createUnresolved("127.0.0.1", 8600);
channel = EasyMock.createMock(Channel.class);
helloPacket = new HelloPacket();
helloPacket.setDestinationIp(Ip4Address.valueOf("15.15.15.15"));
buf = ChannelBuffers.buffer(hpacket.length);
buf.writeBytes(hpacket);
helloPacket.readFrom(buf);
ospfMessageEncoder.encode(ctx, channel, helloPacket);
ddPacket = new DdPacket();
ddPacket.setDestinationIp(Ip4Address.valueOf("15.15.15.15"));
buf = ChannelBuffers.buffer(dpacket.length);
buf.writeBytes(dpacket);
ddPacket.readFrom(buf);
ospfMessageEncoder.encode(ctx, channel, ddPacket);
lsRequest = new LsRequest();
lsRequest.setDestinationIp(Ip4Address.valueOf("15.15.15.15"));
buf = ChannelBuffers.buffer(lrpacket.length);
buf.writeBytes(lrpacket);
lsRequest.readFrom(buf);
ospfMessageEncoder.encode(ctx, channel, lsRequest);
lsAcknowledge = new LsAcknowledge();
lsAcknowledge.setDestinationIp(Ip4Address.valueOf("15.15.15.15"));
buf = ChannelBuffers.buffer(lAckpacket.length);
buf.writeBytes(lAckpacket);
lsAcknowledge.readFrom(buf);
ospfMessageEncoder.encode(ctx, channel, lsAcknowledge);
assertThat(ospfMessageEncoder, is(notNullValue()));
assertThat(ospfMessageEncoder.encode(channelHandlerContext, channel, object), is(notNullValue()));
}
}
\ No newline at end of file
......
......@@ -18,12 +18,6 @@ package org.onosproject.ospf.controller.impl;
import org.jboss.netty.channel.ChannelPipeline;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.onosproject.ospf.controller.area.OspfAreaImpl;
import org.onosproject.ospf.controller.area.OspfInterfaceImpl;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat;
/**
* Unit test class for OspfPipelineFactory.
......@@ -35,8 +29,6 @@ public class OspfPipelineFactoryTest {
@Before
public void setUp() throws Exception {
ospfPipelineFactory = new OspfPipelineFactory(new Controller(), new OspfAreaImpl(), new OspfInterfaceImpl());
}
@After
......@@ -44,22 +36,4 @@ public class OspfPipelineFactoryTest {
ospfPipelineFactory = null;
channelPipeline = null;
}
/**
* Tests getPipeline() method.
*/
@Test
public void testGetPipeline() throws Exception {
channelPipeline = ospfPipelineFactory.getPipeline();
assertThat(channelPipeline, is(notNullValue()));
}
/**
* Tests releaseExternalResources() method.
*/
@Test
public void testReleaseExternalResources() throws Exception {
ospfPipelineFactory.releaseExternalResources();
assertThat(channelPipeline, is(nullValue()));
}
}
\ No newline at end of file
......
......@@ -74,8 +74,9 @@ public class LsaQueueConsumerTest {
@Test
public void testRun() throws Exception {
blockingQueue = new ArrayBlockingQueue(5);
channel = EasyMock.createMock(Channel.class);
ospfArea = new OspfAreaImpl();
lsdbAge = new LsdbAgeImpl(ospfArea);
channel = EasyMock.createMock(Channel.class);
lsaWrapper = new LsaWrapperImpl();
lsaWrapper.setLsaProcessing("verifyChecksum");
blockingQueue.add(lsaWrapper);
......@@ -104,7 +105,7 @@ public class LsaQueueConsumerTest {
lsaHeader.setLsType(1);
lsaWrapper.setLsaHeader(lsaHeader);
lsaWrapper.setLsaProcessing("refreshLsa");
lsaWrapper.setLsdbAge(new LsdbAgeImpl(new OspfAreaImpl()));
lsaWrapper.setLsdbAge(new LsdbAgeImpl(ospfArea));
blockingQueue.add(lsaWrapper);
lsaQueueConsumer = new LsaQueueConsumer(blockingQueue, channel, ospfArea);
lsaQueueConsumer.run();
......@@ -128,7 +129,7 @@ public class LsaQueueConsumerTest {
lsaHeader.setLsType(2);
lsaWrapper.setLsaHeader(lsaHeader);
lsaWrapper.setLsaProcessing("refreshLsa");
lsaWrapper.setLsdbAge(new LsdbAgeImpl(new OspfAreaImpl()));
lsaWrapper.setLsdbAge(new LsdbAgeImpl(ospfArea));
blockingQueue.add(lsaWrapper);
lsaQueueConsumer = new LsaQueueConsumer(blockingQueue, channel, ospfArea);
lsaQueueConsumer.run();
......@@ -155,7 +156,7 @@ public class LsaQueueConsumerTest {
lsaHeader.setLsType(2);
lsaWrapper.setLsaHeader(lsaHeader);
lsaWrapper.setLsaProcessing("maxAgeLsa");
lsaWrapper.setLsdbAge(new LsdbAgeImpl(new OspfAreaImpl()));
lsaWrapper.setLsdbAge(new LsdbAgeImpl(ospfArea));
blockingQueue.add(lsaWrapper);
lsaQueueConsumer = new LsaQueueConsumer(blockingQueue, channel, ospfArea);
lsaQueueConsumer.run();
......@@ -168,7 +169,7 @@ public class LsaQueueConsumerTest {
@Test
public void testSetChannel() throws Exception {
channel = EasyMock.createMock(Channel.class);
lsdbAge = new LsdbAgeImpl(new OspfAreaImpl());
lsdbAge = new LsdbAgeImpl(ospfArea);
lsdbAge.startDbAging();
lsdbAge.setChannel(channel);
assertThat(lsaQueueConsumer, is(notNullValue()));
......
......@@ -23,6 +23,7 @@ import org.junit.Before;
import org.junit.Test;
import org.onosproject.ospf.controller.LsaBin;
import org.onosproject.ospf.controller.OspfArea;
import org.onosproject.ospf.controller.OspfLsaType;
import org.onosproject.ospf.controller.area.OspfAreaImpl;
import static org.hamcrest.CoreMatchers.*;
......@@ -181,14 +182,10 @@ public class LsdbAgeImplTest {
public void testRefereshLsa() throws Exception {
lsaWrapper = EasyMock.createMock(LsaWrapperImpl.class);
lsaWrapper.setBinNumber(0);
lsaWrapper.setLsaType(OspfLsaType.NETWORK);
lsdbAge.addLsaToMaxAgeBin("lsa1", lsaWrapper);
lsdbAge.ageLsaAndFlood();
lsaWrapper.setBinNumber(0);
lsaWrapper = EasyMock.createMock(LsaWrapperImpl.class);
lsdbAge.addLsaToMaxAgeBin("lsa2", lsaWrapper);
lsdbAge.ageLsaAndFlood();
lsdbAge.startDbAging();
lsaBin = new LsaBinImpl(1809);
lsdbAge.refreshLsa();
assertThat(lsdbAge, is(notNullValue()));
}
......
/*
* Copyright 2016-present Open Networking Laboratory
* 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.
......
......@@ -103,6 +103,16 @@ public class NetworkLsa extends LsaHeader {
}
/**
* Gets the list of attached routers.
*
* @return list of attached routers
*/
public List<Ip4Address> attachedRouters() {
return attachedRouters;
}
/**
* Reads from channel buffer and populate instance.
*
* @param channelBuffer channel buffer instance
......
......@@ -29,7 +29,6 @@ import org.onosproject.ospf.protocol.lsa.tlvtypes.OpaqueTopLevelTlvTypes;
import org.onosproject.ospf.protocol.lsa.tlvtypes.RouterTlv;
import org.onosproject.ospf.protocol.util.OspfParameters;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
......@@ -193,11 +192,11 @@ public class OpaqueLsa10 extends OpaqueLsaHeader {
}
OpaqueLsa10 that = (OpaqueLsa10) o;
return Objects.equal(topLevelValues, that.topLevelValues) &&
Arrays.equals(opaqueInfo, that.opaqueInfo);
Objects.equal(opaqueInfo, that.opaqueInfo);
}
@Override
public int hashCode() {
return Objects.hashCode(Arrays.hashCode(opaqueInfo), topLevelValues);
return Objects.hashCode(opaqueInfo, topLevelValues);
}
}
\ No newline at end of file
......
......@@ -16,13 +16,12 @@
package org.onosproject.ospf.protocol.lsa.types;
import com.google.common.base.MoreObjects;
import com.google.common.base.Objects;
import com.google.common.primitives.Bytes;
import org.jboss.netty.buffer.ChannelBuffer;
import org.onosproject.ospf.controller.OspfLsaType;
import org.onosproject.ospf.protocol.lsa.OpaqueLsaHeader;
import java.util.Arrays;
/**
* Representation of an Opaque LSA of type AS (11).
*/
......@@ -107,12 +106,12 @@ public class OpaqueLsa11 extends OpaqueLsaHeader {
return false;
}
OpaqueLsa11 that = (OpaqueLsa11) o;
return Arrays.equals(opaqueInfo, that.opaqueInfo);
return Objects.equal(opaqueInfo, that.opaqueInfo);
}
@Override
public int hashCode() {
return Arrays.hashCode(opaqueInfo);
return Objects.hashCode(opaqueInfo);
}
@Override
......
......@@ -16,13 +16,12 @@
package org.onosproject.ospf.protocol.lsa.types;
import com.google.common.base.MoreObjects;
import com.google.common.base.Objects;
import com.google.common.primitives.Bytes;
import org.jboss.netty.buffer.ChannelBuffer;
import org.onosproject.ospf.controller.OspfLsaType;
import org.onosproject.ospf.protocol.lsa.OpaqueLsaHeader;
import java.util.Arrays;
/**
* Representation of an Opaque LSA of type link local (9).
*/
......@@ -109,12 +108,12 @@ public class OpaqueLsa9 extends OpaqueLsaHeader {
return false;
}
OpaqueLsa9 that = (OpaqueLsa9) o;
return Arrays.equals(opaqueInfo, that.opaqueInfo);
return Objects.equal(opaqueInfo, that.opaqueInfo);
}
@Override
public int hashCode() {
return Arrays.hashCode(opaqueInfo);
return Objects.hashCode(opaqueInfo);
}
@Override
......
......@@ -18,6 +18,7 @@ package org.onosproject.ospf.protocol.ospfpacket;
import org.jboss.netty.buffer.ChannelBuffer;
import org.onlab.packet.Ip4Address;
import org.onosproject.ospf.controller.OspfMessage;
import org.onosproject.ospf.exceptions.OspfErrorType;
import org.onosproject.ospf.exceptions.OspfParseException;
import org.onosproject.ospf.protocol.ospfpacket.types.DdPacket;
......@@ -46,15 +47,8 @@ public class OspfMessageReader {
public OspfMessage readFromBuffer(ChannelBuffer channelBuffer)
throws Exception {
if (channelBuffer.readableBytes() < OspfUtil.PACKET_MINIMUM_LENGTH) {
log.error("Packet should have minimum length...");
throw new OspfParseException(OspfErrorType.MESSAGE_HEADER_ERROR, OspfErrorType.BAD_MESSAGE_LENGTH);
}
try {
OspfPacketHeader ospfHeader = getOspfHeader(channelBuffer);
int len = ospfHeader.ospfPacLength() - OspfUtil.OSPF_HEADER_LENGTH;
OspfMessage ospfMessage = null;
switch (ospfHeader.ospfType()) {
case OspfParameters.HELLO:
......@@ -81,7 +75,8 @@ public class OspfMessageReader {
try {
log.debug("{} Received::Message Length :: {} ", ospfMessage.ospfMessageType(),
ospfHeader.ospfPacLength());
ospfMessage.readFrom(channelBuffer.readBytes(len));
ospfMessage.readFrom(channelBuffer.readBytes(ospfHeader.ospfPacLength() -
OspfUtil.OSPF_HEADER_LENGTH));
} catch (Exception e) {
throw new OspfParseException(OspfErrorType.OSPF_MESSAGE_ERROR,
OspfErrorType.BAD_MESSAGE);
......@@ -105,22 +100,13 @@ public class OspfMessageReader {
private OspfPacketHeader getOspfHeader(ChannelBuffer channelBuffer) throws Exception {
OspfPacketHeader ospfPacketHeader = new OspfPacketHeader();
byte[] sourceIpBytes = new byte[OspfUtil.FOUR_BYTES];
channelBuffer.readBytes(sourceIpBytes, 0, OspfUtil.FOUR_BYTES);
Ip4Address sourceIP = Ip4Address.valueOf(sourceIpBytes);
// Determine ospf version & Packet Type
// Determine OSPF version & Packet Type
int version = channelBuffer.readByte(); //byte 1 is ospf version
int packetType = channelBuffer.readByte(); //byte 2 is ospf packet type
// byte 3 & 4 combine is packet length.
int packetLength = channelBuffer.readShort();
if (packetLength > channelBuffer.readableBytes() + OspfUtil.FOUR_BYTES) {
log.error("Packet should have minimum length...");
throw new OspfParseException(OspfErrorType.MESSAGE_HEADER_ERROR, OspfErrorType.BAD_MESSAGE_LENGTH);
}
byte[] tempByteArray = new byte[OspfUtil.FOUR_BYTES];
channelBuffer.readBytes(tempByteArray, 0, OspfUtil.FOUR_BYTES);
Ip4Address routerId = Ip4Address.valueOf(tempByteArray);
......@@ -133,7 +119,6 @@ public class OspfMessageReader {
int auType = channelBuffer.readUnsignedShort();
int authentication = (int) channelBuffer.readLong();
ospfPacketHeader.setSourceIp(sourceIP);
ospfPacketHeader.setOspfVer(version);
ospfPacketHeader.setOspftype(packetType);
ospfPacketHeader.setOspfPacLength(packetLength);
......
/*
* Copyright 2016-present Open Networking Laboratory
* 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.
......@@ -15,41 +15,36 @@
*/
package org.onosproject.ospf.protocol.ospfpacket;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
import org.onosproject.ospf.controller.OspfMessage;
import org.onosproject.ospf.protocol.util.OspfParameters;
import org.onosproject.ospf.protocol.util.OspfUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* A message writer which writes an OspfMessage to ChannelBuffer.
* A message writer which writes an OSPF message to byte array.
*/
public class OspfMessageWriter {
private static final Logger log = LoggerFactory.getLogger(OspfMessageWriter.class);
/**
* Writes OSPF message to ChannelBuffer.
* Writes OSPF message to byte array.
*
* @param ospfMessage OSPF message
* @param interfaceIndex interface index
* @param interfaceState interface state
* @param interfaceType interface type
* @return channelBuffer channel buffer instance
* @throws Exception might throws exception while parsing message
* @return message as byte array
*/
public ChannelBuffer writeToBuffer(OspfMessage ospfMessage, int interfaceState,
int interfaceType) throws Exception {
public byte[] getMessage(OspfMessage ospfMessage, int interfaceIndex, int interfaceState) {
ChannelBuffer buf = null;
byte[] buf = null;
switch (ospfMessage.ospfMessageType().value()) {
case OspfParameters.HELLO:
case OspfParameters.LSACK:
buf = writeMessageToBuffer(ospfMessage, interfaceState);
break;
case OspfParameters.DD:
case OspfParameters.LSREQUEST:
case OspfParameters.LSUPDATE:
buf = writeMessageToBuffer(ospfMessage, interfaceState);
buf = writeMessageToBytes(ospfMessage, interfaceIndex, interfaceState);
break;
default:
log.debug("Message Writer[Encoder] - Unknown Message to encode..!!!");
......@@ -60,14 +55,13 @@ public class OspfMessageWriter {
}
/**
* Writes an OSPF Message to channel buffer.
* Writes an OSPF Message to byte array.
*
* @param ospfMessage OSPF Message instance
* @param interfaceState interface state
* @return channelBuffer instance
* @return message as byte array
*/
private ChannelBuffer writeMessageToBuffer(OspfMessage ospfMessage, int interfaceState) throws Exception {
ChannelBuffer channelBuffer = null;
private byte[] writeMessageToBytes(OspfMessage ospfMessage, int interfaceIndex, int interfaceState) {
byte[] ospfMessageAsByte = ospfMessage.asBytes();
//Add the length and checksum in byte array at length position 2 & 3 and Checksum position
ospfMessageAsByte = OspfUtil.addLengthAndCheckSum(ospfMessageAsByte, OspfUtil.OSPFPACKET_LENGTH_POS1,
......@@ -76,16 +70,13 @@ public class OspfMessageWriter {
OspfUtil.OSPFPACKET_CHECKSUM_POS2);
//Add Interface State Info and destination IP as metadata
if (interfaceState == OspfParameters.DR || interfaceState == OspfParameters.BDR) {
ospfMessageAsByte = OspfUtil.addMetadata(ospfMessageAsByte, OspfUtil.JOIN_ALL_DROUTERS,
ospfMessageAsByte = OspfUtil.addMetadata(interfaceIndex, ospfMessageAsByte, OspfUtil.JOIN_ALL_DROUTERS,
ospfMessage.destinationIp());
} else {
ospfMessageAsByte = OspfUtil.addMetadata(ospfMessageAsByte, OspfUtil.ONLY_ALL_SPF_ROUTERS,
ospfMessageAsByte = OspfUtil.addMetadata(interfaceIndex, ospfMessageAsByte, OspfUtil.ONLY_ALL_SPF_ROUTERS,
ospfMessage.destinationIp());
}
channelBuffer = ChannelBuffers.buffer(ospfMessageAsByte.length);
channelBuffer.writeBytes(ospfMessageAsByte);
return channelBuffer;
return ospfMessageAsByte;
}
}
\ No newline at end of file
......
......@@ -18,8 +18,9 @@ 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.controller.OspfMessage;
import org.onosproject.ospf.controller.OspfPacketType;
import org.onosproject.ospf.exceptions.OspfParseException;
import org.onosproject.ospf.protocol.util.OspfPacketType;
/**
* Defines the OSPF Packet Header, fields and access methods.
......@@ -57,6 +58,7 @@ public class OspfPacketHeader implements OspfMessage {
private int authentication;
private Ip4Address destinationIp;
private Ip4Address sourceIp;
private int interfaceIndex;
/**
* Gets the source IP.
......@@ -256,11 +258,30 @@ public class OspfPacketHeader implements OspfMessage {
}
/**
* Returns the interface index on which the message received.
*
* @return interface index on which the message received
*/
public int interfaceIndex() {
return interfaceIndex;
}
/**
* Sets the interface index on which the message received.
*
* @param interfaceIndex interface index on which the message received
*/
public void setInterfaceIndex(int interfaceIndex) {
this.interfaceIndex = interfaceIndex;
}
/**
* Populates the header from the packetHeader instance.
*
* @param ospfPacketHeader packet header instance.
*/
public void populateHeader(OspfPacketHeader ospfPacketHeader) {
this.setInterfaceIndex(ospfPacketHeader.interfaceIndex());
this.setSourceIp(ospfPacketHeader.sourceIp());
this.setOspfVer(ospfPacketHeader.ospfVersion());
this.setOspftype(ospfPacketHeader.ospfType());
......
......@@ -23,7 +23,7 @@ import org.onosproject.ospf.exceptions.OspfParseException;
import org.onosproject.ospf.protocol.lsa.LsaHeader;
import org.onosproject.ospf.protocol.lsa.OpaqueLsaHeader;
import org.onosproject.ospf.protocol.ospfpacket.OspfPacketHeader;
import org.onosproject.ospf.protocol.util.OspfPacketType;
import org.onosproject.ospf.controller.OspfPacketType;
import org.onosproject.ospf.protocol.util.OspfParameters;
import org.onosproject.ospf.protocol.util.OspfUtil;
import org.slf4j.Logger;
......
......@@ -22,7 +22,7 @@ import org.onlab.packet.Ip4Address;
import org.onosproject.ospf.exceptions.OspfErrorType;
import org.onosproject.ospf.exceptions.OspfParseException;
import org.onosproject.ospf.protocol.ospfpacket.OspfPacketHeader;
import org.onosproject.ospf.protocol.util.OspfPacketType;
import org.onosproject.ospf.controller.OspfPacketType;
import org.onosproject.ospf.protocol.util.OspfUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......
......@@ -23,7 +23,7 @@ import org.onosproject.ospf.exceptions.OspfParseException;
import org.onosproject.ospf.protocol.lsa.LsaHeader;
import org.onosproject.ospf.protocol.lsa.OpaqueLsaHeader;
import org.onosproject.ospf.protocol.ospfpacket.OspfPacketHeader;
import org.onosproject.ospf.protocol.util.OspfPacketType;
import org.onosproject.ospf.controller.OspfPacketType;
import org.onosproject.ospf.protocol.util.OspfParameters;
import org.onosproject.ospf.protocol.util.OspfUtil;
import org.slf4j.Logger;
......
......@@ -22,7 +22,7 @@ import org.onlab.packet.Ip4Address;
import org.onosproject.ospf.exceptions.OspfParseException;
import org.onosproject.ospf.protocol.ospfpacket.OspfPacketHeader;
import org.onosproject.ospf.protocol.ospfpacket.subtype.LsRequestPacket;
import org.onosproject.ospf.protocol.util.OspfPacketType;
import org.onosproject.ospf.controller.OspfPacketType;
import org.onosproject.ospf.protocol.util.OspfUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......
......@@ -32,7 +32,7 @@ import org.onosproject.ospf.protocol.lsa.types.OpaqueLsa9;
import org.onosproject.ospf.protocol.lsa.types.RouterLsa;
import org.onosproject.ospf.protocol.lsa.types.SummaryLsa;
import org.onosproject.ospf.protocol.ospfpacket.OspfPacketHeader;
import org.onosproject.ospf.protocol.util.OspfPacketType;
import org.onosproject.ospf.controller.OspfPacketType;
import org.onosproject.ospf.protocol.util.OspfParameters;
import org.onosproject.ospf.protocol.util.OspfUtil;
import org.slf4j.Logger;
......
......@@ -25,7 +25,7 @@ import org.onosproject.ospf.protocol.lsa.types.OpaqueLsa11;
import org.onosproject.ospf.protocol.lsa.types.OpaqueLsa9;
import org.onosproject.ospf.protocol.lsa.types.RouterLsa;
import org.onosproject.ospf.protocol.lsa.types.SummaryLsa;
import org.onosproject.ospf.protocol.ospfpacket.OspfMessage;
import org.onosproject.ospf.controller.OspfMessage;
import org.onosproject.ospf.protocol.ospfpacket.types.DdPacket;
import org.onosproject.ospf.protocol.ospfpacket.types.HelloPacket;
import org.onosproject.ospf.protocol.ospfpacket.types.LsAcknowledge;
......
......@@ -31,20 +31,55 @@ public enum OspfInterfaceState {
private int value;
/**
* Creates an instance of Interface State.
* Creates an instance of interface state.
*
* @param value Interface State value
* @param value Interface state value
*/
OspfInterfaceState(int value) {
this.value = value;
}
/**
* Gets value for Interface State.
* Gets value for Interface state.
*
* @return value Interface State
* @return value Interface state
*/
public int value() {
return value;
}
/**
* Gets interface state.
*
* @return interface state
*/
public String interfaceState() {
String state = null;
switch (value) {
case 1:
state = "DOWN";
break;
case 2:
state = "LOOPBACK";
break;
case 3:
state = "WAITING";
break;
case 4:
state = "POINT2POINT";
break;
case 5:
state = "DROTHER";
break;
case 6:
state = "BDR";
break;
case 7:
state = "DR";
break;
default:
break;
}
return state;
}
}
\ No newline at end of file
......
......@@ -36,10 +36,11 @@ 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 METADATA_LEN = 5;
public static final int MINIMUM_FRAME_LEN = 1487;
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;
......@@ -52,6 +53,8 @@ public final class OspfUtil {
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 Ip4Address DEFAULTIP = Ip4Address.valueOf("0.0.0.0");
public static final int RETRANSMITINTERVAL = 5;
public static final int ONLY_ALL_SPF_ROUTERS = 1;
public static final int JOIN_ALL_DROUTERS = 2;
public static final int INITIALIZE_SET = 1;
......@@ -62,9 +65,16 @@ public final class OspfUtil {
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 FIVE_BYTES = 5;
public static final int EIGHT_BYTES = 8;
public static final int TWELVE_BYTES = 12;
public static final int EXTERNAL_DESTINATION_LENGTH = 12;
public static final String SHOST = "127.0.0.1";
public static final int SPORT = 7000;
public static final int MTU = 1500;
public static final char CONFIG_LENGTH = 1498;
public static final char ROUTER_PRIORITY = 0;
public static final int HELLO_PACKET_OPTIONS = 2;
private static final Logger log =
LoggerFactory.getLogger(OspfUtil.class);
......@@ -398,18 +408,21 @@ public final class OspfUtil {
/**
* Adds metadata to ospf packet like whether to join multi cast group and destination IP.
*
* @param interfaceIndex interface index
* @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) {
public static byte[] addMetadata(int interfaceIndex, byte[] ospfPacket, int allDroutersValue,
Ip4Address destinationIp) {
byte[] packet;
byte[] interfaceIndexByteVal = {(byte) interfaceIndex};
byte[] allDroutersByteVal = {(byte) allDroutersValue};
byte[] destIpAsBytes = destinationIp.toOctets();
byte[] metadata = Bytes.concat(allDroutersByteVal, destIpAsBytes);
packet = Bytes.concat(metadata, ospfPacket);
byte[] metadata = Bytes.concat(interfaceIndexByteVal, allDroutersByteVal);
metadata = Bytes.concat(metadata, destIpAsBytes);
packet = Bytes.concat(ospfPacket, metadata);
return packet;
}
......
......@@ -20,6 +20,7 @@ import org.jboss.netty.buffer.ChannelBuffers;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.onosproject.ospf.protocol.util.OspfUtil;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
......@@ -31,17 +32,17 @@ import static org.hamcrest.MatcherAssert.assertThat;
public class OspfMessageReaderTest {
private final byte[] packet1 = {1, 1, 1, 1, 2, 1, 0, 44, -64, -88, -86, 8,
private final byte[] packet1 = {2, 1, 0, 44, -64, -88, -86, 8,
0, 0, 0, 1, 39, 59, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, 0, 0,
10, 2, 1, 0, 0, 0, 40, -64, -88, -86, 8, 0, 0, 0, 0};
private final byte[] packet2 = {1, 1, 1, 1, 2, 2, 0, 52, -64, -88, -86, 8, 0,
private final byte[] packet2 = {2, 2, 0, 52, -64, -88, -86, 8, 0,
0, 0, 1, -96, 82, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, -36, 2, 7, 65, 119,
-87, 126, 0, 23, 2, 1, 10, 10, 10, 10, 10, 10, 10, 10, -128, 0, 0, 6,
-69, 26, 0, 36};
private final byte[] packet3 = {1, 1, 1, 1, 2, 3, 0, 36, -64, -88, -86, 3, 0,
private final byte[] packet3 = {2, 3, 0, 36, -64, -88, -86, 3, 0,
0, 0, 1, -67, -57, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, -64, -88,
-86, 8, -64, -88, -86, 8};
private final byte[] packet4 = {1, 1, 1, 1, 2, 4, 1, 36, -64, -88, -86, 3, 0,
private final byte[] packet4 = {2, 4, 1, 36, -64, -88, -86, 3, 0,
0, 0, 1, 54, 107, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 0,
2, 2, 1, -64, -88, -86, 3, -64, -88, -86, 3, -128, 0,
0, 1, 58, -100, 0, 48, 2, 0, 0, 2, -64, -88, -86,
......@@ -62,7 +63,7 @@ public class OspfMessageReaderTest {
0, 0, 0, 0, 0, 0, 0, 3, 2, 5, -64, -88, -84, 0, -64,
-88, -86, 2, -128, 0, 0, 1, 51, 65, 0, 36, -1, -1, -1, 0,
-128, 0, 0, 20, -64, -88, -86, 10, 0, 0, 0, 0};
private final byte[] packet5 = {1, 1, 1, 1, 2, 5, 0, 44, -64, -88, -86, 8, 0, 0,
private final byte[] packet5 = {2, 5, 0, 44, -64, -88, -86, 8, 0, 0,
0, 1, -30, -12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 16, 2, 1, -64, -88, -86,
2, -64, -88, -86, 2, -128, 0, 0, 1, 74, -114, 0, 48};
private OspfMessageReader ospfMessageReader;
......@@ -84,21 +85,39 @@ public class OspfMessageReaderTest {
*/
@Test
public void testReadFromBuffer() throws Exception {
channelBuffer = ChannelBuffers.copiedBuffer(packet1);
channelBuffer = ChannelBuffers.copiedBuffer(framePacket(packet1));
ospfMessageReader.readFromBuffer(channelBuffer);
channelBuffer = ChannelBuffers.copiedBuffer(packet2);
channelBuffer = ChannelBuffers.copiedBuffer(framePacket(packet2));
ospfMessageReader.readFromBuffer(channelBuffer);
channelBuffer = ChannelBuffers.copiedBuffer(packet3);
channelBuffer = ChannelBuffers.copiedBuffer(framePacket(packet3));
ospfMessageReader.readFromBuffer(channelBuffer);
channelBuffer = ChannelBuffers.copiedBuffer(packet4);
channelBuffer = ChannelBuffers.copiedBuffer(framePacket(packet4));
ospfMessageReader.readFromBuffer(channelBuffer);
channelBuffer = ChannelBuffers.copiedBuffer(packet5);
channelBuffer = ChannelBuffers.copiedBuffer(framePacket(packet5));
ospfMessageReader.readFromBuffer(channelBuffer);
assertThat(ospfMessageReader, is(notNullValue()));
}
/**
* Frames the packet to min frame length.
*
* @param ospfPacket OSPF packet
* @return OSPF packet as byte array
*/
private byte[] framePacket(byte[] ospfPacket) {
//Set the length of the packet
//Get the total length of the packet
int length = ospfPacket.length;
//PDU_LENGTH + 1 byte for interface index
if (length < OspfUtil.MINIMUM_FRAME_LEN) {
byte[] bytes = new byte[OspfUtil.MINIMUM_FRAME_LEN + 5];
System.arraycopy(ospfPacket, 0, bytes, 0, length);
return bytes;
}
return ospfPacket;
}
}
\ No newline at end of file
......
......@@ -57,10 +57,10 @@ public class OspfMessageWriterTest {
}
/**
* Tests writeToBuffer() method.
* Tests getMessage() method.
*/
@Test
public void testWriteToBuffer() throws Exception {
public void testGetMessage() throws Exception {
helloPacket = new HelloPacket();
helloPacket.setAuthType(1);
helloPacket.setOspftype(1);
......@@ -79,12 +79,12 @@ public class OspfMessageWriterTest {
helloPacket.setBdr(Ip4Address.valueOf("2.2.2.2"));
helloPacket.addNeighbor(Ip4Address.valueOf("8.8.8.8"));
helloPacket.setDestinationIp(Ip4Address.valueOf("5.5.5.5"));
ospfMessageWriter.writeToBuffer(helloPacket, 7, 1);
ospfMessageWriter.getMessage(helloPacket, 7, 1);
assertThat(ospfMessageWriter, is(notNullValue()));
}
@Test(expected = Exception.class)
public void testWriteToBuffer1() throws Exception {
public void testGetMessage1() throws Exception {
ddPacket = new DdPacket();
ddPacket.setAuthType(1);
......@@ -95,12 +95,12 @@ public class OspfMessageWriterTest {
ddPacket.setAuthentication(2);
ddPacket.setOspfPacLength(48);
ddPacket.setOspfVer(2);
ospfMessageWriter.writeToBuffer(ddPacket, 1, 1);
ospfMessageWriter.getMessage(ddPacket, 1, 1);
assertThat(ospfMessageWriter, is(notNullValue()));
}
@Test(expected = Exception.class)
public void testWriteToBuffer2() throws Exception {
public void testGetMessage2() throws Exception {
lsAck = new LsAcknowledge();
lsAck.setAuthType(1);
......@@ -111,12 +111,12 @@ public class OspfMessageWriterTest {
lsAck.setAuthentication(2);
lsAck.setOspfPacLength(48);
lsAck.setOspfVer(2);
ospfMessageWriter.writeToBuffer(lsAck, 1, 1);
ospfMessageWriter.getMessage(lsAck, 1, 1);
assertThat(ospfMessageWriter, is(notNullValue()));
}
@Test(expected = Exception.class)
public void testWriteToBuffer3() throws Exception {
public void testGetMessage3() throws Exception {
lsReq = new LsRequest();
lsReq.setAuthType(1);
lsReq.setOspftype(3);
......@@ -126,12 +126,15 @@ public class OspfMessageWriterTest {
lsReq.setAuthentication(2);
lsReq.setOspfPacLength(48);
lsReq.setOspfVer(2);
ospfMessageWriter.writeToBuffer(lsReq, 1, 1);
ospfMessageWriter.getMessage(lsReq, 1, 1);
assertThat(ospfMessageWriter, is(notNullValue()));
}
/**
* Tests getMessage() method.
*/
@Test(expected = Exception.class)
public void testWriteToBuffer4() throws Exception {
public void testGetMessage4() throws Exception {
lsUpdate = new LsUpdate();
lsUpdate.setAuthType(1);
lsUpdate.setOspftype(3);
......@@ -141,15 +144,15 @@ public class OspfMessageWriterTest {
lsUpdate.setAuthentication(2);
lsUpdate.setOspfPacLength(48);
lsUpdate.setOspfVer(2);
ospfMessageWriter.writeToBuffer(lsUpdate, 1, 1);
ospfMessageWriter.getMessage(lsUpdate, 1, 1);
assertThat(ospfMessageWriter, is(notNullValue()));
}
/**
* Tests writeToBuffer() method.
* Tests getMessage() method.
*/
@Test(expected = Exception.class)
public void testWriteToBuffer5() throws Exception {
public void testGetMessage5() throws Exception {
lsAck = new LsAcknowledge();
lsAck.setAuthType(1);
lsAck.setOspftype(5);
......@@ -159,6 +162,6 @@ public class OspfMessageWriterTest {
lsAck.setAuthentication(2);
lsAck.setOspfPacLength(48);
lsAck.setOspfVer(2);
ospfMessageWriter.writeToBuffer(lsAck, 1, 1);
ospfMessageWriter.getMessage(lsAck, 1, 1);
}
}
\ No newline at end of file
......
......@@ -22,10 +22,10 @@ import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.onlab.packet.Ip4Address;
import org.onosproject.ospf.controller.OspfPacketType;
import org.onosproject.ospf.protocol.lsa.LsaHeader;
import org.onosproject.ospf.protocol.lsa.OpaqueLsaHeader;
import org.onosproject.ospf.protocol.ospfpacket.OspfPacketHeader;
import org.onosproject.ospf.protocol.util.OspfPacketType;
import java.util.List;
import java.util.Vector;
......
......@@ -21,8 +21,8 @@ import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.onlab.packet.Ip4Address;
import org.onosproject.ospf.controller.OspfPacketType;
import org.onosproject.ospf.protocol.ospfpacket.OspfPacketHeader;
import org.onosproject.ospf.protocol.util.OspfPacketType;
import java.util.Vector;
......