@Test(expected = Exception.class) public void testProcessLSRequestMessage() throws Exception { ospfArea.setRouterId(Ip4Address.valueOf("11.11.11.11")); ospfArea.setAreaId(Ip4Address.valueOf("12.12.12.12")); ospfInterface.setOspfArea(ospfArea); ospfInterface.setIpAddress(Ip4Address.valueOf("11.11.11.11")); ospfInterface.setInterfaceType(2); ospfInterface.setIpNetworkMask(Ip4Address.valueOf("255.255.255.255")); ospfInterface.setHelloIntervalTime(10); ospfInterface.setRouterDeadIntervalTime(10); channelHandlerContext = EasyMock.createMock(ChannelHandlerContext.class); OspfMessage message; lsRequest = new LsRequest(); lsRequest.setSourceIp(Ip4Address.valueOf("1.1.1.1")); lsRequest.setOspfVer(2); lsRequest.setAreaId(Ip4Address.valueOf("12.12.12.12")); lsRequest.setRouterId(Ip4Address.valueOf("10.226.165.100")); List<LsRequestPacket> lsRequests = new ArrayList(); LsRequestPacket lsRequestPacket = new LsRequestPacket(); lsRequestPacket.setLsType(OspfLsaType.AREA_LOCAL_OPAQUE_LSA.value()); lsRequestPacket.setLinkStateId("2.2.2.2"); lsRequestPacket.setOwnRouterId("10.226.165.100"); lsRequests.add(lsRequestPacket); lsRequests.add(lsRequestPacket); lsRequest.addLinkStateRequests(lsRequestPacket); message = lsRequest; ospfNbrHashMap = new HashMap(); ospfNbr.setState(OspfNeighborState.EXCHANGE); ospfNbr = new OspfNbrImpl(ospfArea, createOspfInterface(), Ip4Address.valueOf("10.10.10.10"), Ip4Address.valueOf("10.226.165.100"), 2, topologyForDeviceAndLink); ospfNbr.setLastDdPacket(createDdPacket()); ospfNbr.setNeighborId(Ip4Address.valueOf("10.226.165.100")); ospfNbr.setState(OspfNeighborState.FULL); ospfNbr.setRouterPriority(0); ospfNbr.setNeighborDr(Ip4Address.valueOf("13.13.13.13")); ospfNbr.setDdSeqNum(123); ospfInterface.addNeighbouringRouter(ospfNbr); OpaqueLsaHeader lsaHeader = new OpaqueLsaHeader(); lsaHeader.setLsType(OspfLsaType.AREA_LOCAL_OPAQUE_LSA.value()); lsaHeader.setLinkStateId("2.2.2.2"); lsaHeader.setAdvertisingRouter(Ip4Address.valueOf("10.226.165.100")); OpaqueLsa10 opaqueLsa10 = new OpaqueLsa10(lsaHeader); ospfArea.addLsa(opaqueLsa10, false, ospfInterface); ospfInterface.setOspfArea(ospfArea); ospfInterface.processLsRequestMessage(message, channelHandlerContext); assertThat(ospfInterface, is(notNullValue())); }
void processLsRequestMessage(OspfMessage ospfMessage, ChannelHandlerContext ctx) throws Exception { log.debug("OspfChannelHandler::processLsRequestMessage...!!!"); Channel channel = ctx.getChannel(); LsRequest lsrPacket = (LsRequest) ospfMessage; OspfNbr nbr = neighbouringRouter(lsrPacket.routerId().toString()); if (nbr.getState() == OspfNeighborState.EXCHANGE || nbr.getState() == OspfNeighborState.LOADING || nbr.getState() == OspfNeighborState.FULL) { LsRequest reqMsg = (LsRequest) ospfMessage; if (reqMsg.getLinkStateRequests().isEmpty()) { log.debug("Received Link State Request Vector is Empty "); return; } else { ListIterator<LsRequestPacket> listItr = reqMsg.getLinkStateRequests().listIterator(); while (listItr.hasNext()) { LsUpdate lsupdate = new LsUpdate(); lsupdate.setOspfVer(OspfUtil.OSPF_VERSION); lsupdate.setOspftype(OspfPacketType.LSUPDATE.value()); lsupdate.setRouterId(ospfArea.routerId()); lsupdate.setAreaId(ospfArea.areaId()); lsupdate.setAuthType(OspfUtil.NOT_ASSIGNED); lsupdate.setAuthentication(OspfUtil.NOT_ASSIGNED); lsupdate.setOspfPacLength(OspfUtil.NOT_ASSIGNED); lsupdate.setChecksum(OspfUtil.NOT_ASSIGNED); int currentLength = OspfUtil.OSPF_HEADER_LENGTH + OspfUtil.FOUR_BYTES; int maxSize = mtu() - OspfUtil.LSA_HEADER_LENGTH; int noLsa = 0; while (listItr.hasNext()) { LsRequestPacket lsRequest = (LsRequestPacket) listItr.next(); LsaWrapper wrapper = ospfArea.getLsa(lsRequest.lsType(), lsRequest.linkStateId(), lsRequest.ownRouterId()); OspfLsa ospflsa = wrapper.ospfLsa(); if ((currentLength + ((LsaWrapperImpl) wrapper).lsaHeader().lsPacketLen()) >= maxSize) { listItr.previous(); break; } if (ospflsa != null) { lsupdate.addLsa(ospflsa); noLsa++; currentLength = currentLength + ((LsaWrapperImpl) wrapper).lsaHeader().lsPacketLen(); } else { nbr.badLSReq(channel); } } lsupdate.setNumberOfLsa(noLsa); if (state() == OspfInterfaceState.DR || state() == OspfInterfaceState.BDR || state() == OspfInterfaceState.POINT2POINT) { lsupdate.setDestinationIp(OspfUtil.ALL_SPF_ROUTERS); } else if (state() == OspfInterfaceState.DROTHER) { lsupdate.setDestinationIp(OspfUtil.ALL_DROUTERS); } byte[] messageToWrite = getMessage(lsupdate); ctx.getChannel().write(messageToWrite); } } } }
OspfInterfaceImpl implements OspfInterface { void processLsRequestMessage(OspfMessage ospfMessage, ChannelHandlerContext ctx) throws Exception { log.debug("OspfChannelHandler::processLsRequestMessage...!!!"); Channel channel = ctx.getChannel(); LsRequest lsrPacket = (LsRequest) ospfMessage; OspfNbr nbr = neighbouringRouter(lsrPacket.routerId().toString()); if (nbr.getState() == OspfNeighborState.EXCHANGE || nbr.getState() == OspfNeighborState.LOADING || nbr.getState() == OspfNeighborState.FULL) { LsRequest reqMsg = (LsRequest) ospfMessage; if (reqMsg.getLinkStateRequests().isEmpty()) { log.debug("Received Link State Request Vector is Empty "); return; } else { ListIterator<LsRequestPacket> listItr = reqMsg.getLinkStateRequests().listIterator(); while (listItr.hasNext()) { LsUpdate lsupdate = new LsUpdate(); lsupdate.setOspfVer(OspfUtil.OSPF_VERSION); lsupdate.setOspftype(OspfPacketType.LSUPDATE.value()); lsupdate.setRouterId(ospfArea.routerId()); lsupdate.setAreaId(ospfArea.areaId()); lsupdate.setAuthType(OspfUtil.NOT_ASSIGNED); lsupdate.setAuthentication(OspfUtil.NOT_ASSIGNED); lsupdate.setOspfPacLength(OspfUtil.NOT_ASSIGNED); lsupdate.setChecksum(OspfUtil.NOT_ASSIGNED); int currentLength = OspfUtil.OSPF_HEADER_LENGTH + OspfUtil.FOUR_BYTES; int maxSize = mtu() - OspfUtil.LSA_HEADER_LENGTH; int noLsa = 0; while (listItr.hasNext()) { LsRequestPacket lsRequest = (LsRequestPacket) listItr.next(); LsaWrapper wrapper = ospfArea.getLsa(lsRequest.lsType(), lsRequest.linkStateId(), lsRequest.ownRouterId()); OspfLsa ospflsa = wrapper.ospfLsa(); if ((currentLength + ((LsaWrapperImpl) wrapper).lsaHeader().lsPacketLen()) >= maxSize) { listItr.previous(); break; } if (ospflsa != null) { lsupdate.addLsa(ospflsa); noLsa++; currentLength = currentLength + ((LsaWrapperImpl) wrapper).lsaHeader().lsPacketLen(); } else { nbr.badLSReq(channel); } } lsupdate.setNumberOfLsa(noLsa); if (state() == OspfInterfaceState.DR || state() == OspfInterfaceState.BDR || state() == OspfInterfaceState.POINT2POINT) { lsupdate.setDestinationIp(OspfUtil.ALL_SPF_ROUTERS); } else if (state() == OspfInterfaceState.DROTHER) { lsupdate.setDestinationIp(OspfUtil.ALL_DROUTERS); } byte[] messageToWrite = getMessage(lsupdate); ctx.getChannel().write(messageToWrite); } } } } }
OspfInterfaceImpl implements OspfInterface { void processLsRequestMessage(OspfMessage ospfMessage, ChannelHandlerContext ctx) throws Exception { log.debug("OspfChannelHandler::processLsRequestMessage...!!!"); Channel channel = ctx.getChannel(); LsRequest lsrPacket = (LsRequest) ospfMessage; OspfNbr nbr = neighbouringRouter(lsrPacket.routerId().toString()); if (nbr.getState() == OspfNeighborState.EXCHANGE || nbr.getState() == OspfNeighborState.LOADING || nbr.getState() == OspfNeighborState.FULL) { LsRequest reqMsg = (LsRequest) ospfMessage; if (reqMsg.getLinkStateRequests().isEmpty()) { log.debug("Received Link State Request Vector is Empty "); return; } else { ListIterator<LsRequestPacket> listItr = reqMsg.getLinkStateRequests().listIterator(); while (listItr.hasNext()) { LsUpdate lsupdate = new LsUpdate(); lsupdate.setOspfVer(OspfUtil.OSPF_VERSION); lsupdate.setOspftype(OspfPacketType.LSUPDATE.value()); lsupdate.setRouterId(ospfArea.routerId()); lsupdate.setAreaId(ospfArea.areaId()); lsupdate.setAuthType(OspfUtil.NOT_ASSIGNED); lsupdate.setAuthentication(OspfUtil.NOT_ASSIGNED); lsupdate.setOspfPacLength(OspfUtil.NOT_ASSIGNED); lsupdate.setChecksum(OspfUtil.NOT_ASSIGNED); int currentLength = OspfUtil.OSPF_HEADER_LENGTH + OspfUtil.FOUR_BYTES; int maxSize = mtu() - OspfUtil.LSA_HEADER_LENGTH; int noLsa = 0; while (listItr.hasNext()) { LsRequestPacket lsRequest = (LsRequestPacket) listItr.next(); LsaWrapper wrapper = ospfArea.getLsa(lsRequest.lsType(), lsRequest.linkStateId(), lsRequest.ownRouterId()); OspfLsa ospflsa = wrapper.ospfLsa(); if ((currentLength + ((LsaWrapperImpl) wrapper).lsaHeader().lsPacketLen()) >= maxSize) { listItr.previous(); break; } if (ospflsa != null) { lsupdate.addLsa(ospflsa); noLsa++; currentLength = currentLength + ((LsaWrapperImpl) wrapper).lsaHeader().lsPacketLen(); } else { nbr.badLSReq(channel); } } lsupdate.setNumberOfLsa(noLsa); if (state() == OspfInterfaceState.DR || state() == OspfInterfaceState.BDR || state() == OspfInterfaceState.POINT2POINT) { lsupdate.setDestinationIp(OspfUtil.ALL_SPF_ROUTERS); } else if (state() == OspfInterfaceState.DROTHER) { lsupdate.setDestinationIp(OspfUtil.ALL_DROUTERS); } byte[] messageToWrite = getMessage(lsupdate); ctx.getChannel().write(messageToWrite); } } } }  }
OspfInterfaceImpl implements OspfInterface { void processLsRequestMessage(OspfMessage ospfMessage, ChannelHandlerContext ctx) throws Exception { log.debug("OspfChannelHandler::processLsRequestMessage...!!!"); Channel channel = ctx.getChannel(); LsRequest lsrPacket = (LsRequest) ospfMessage; OspfNbr nbr = neighbouringRouter(lsrPacket.routerId().toString()); if (nbr.getState() == OspfNeighborState.EXCHANGE || nbr.getState() == OspfNeighborState.LOADING || nbr.getState() == OspfNeighborState.FULL) { LsRequest reqMsg = (LsRequest) ospfMessage; if (reqMsg.getLinkStateRequests().isEmpty()) { log.debug("Received Link State Request Vector is Empty "); return; } else { ListIterator<LsRequestPacket> listItr = reqMsg.getLinkStateRequests().listIterator(); while (listItr.hasNext()) { LsUpdate lsupdate = new LsUpdate(); lsupdate.setOspfVer(OspfUtil.OSPF_VERSION); lsupdate.setOspftype(OspfPacketType.LSUPDATE.value()); lsupdate.setRouterId(ospfArea.routerId()); lsupdate.setAreaId(ospfArea.areaId()); lsupdate.setAuthType(OspfUtil.NOT_ASSIGNED); lsupdate.setAuthentication(OspfUtil.NOT_ASSIGNED); lsupdate.setOspfPacLength(OspfUtil.NOT_ASSIGNED); lsupdate.setChecksum(OspfUtil.NOT_ASSIGNED); int currentLength = OspfUtil.OSPF_HEADER_LENGTH + OspfUtil.FOUR_BYTES; int maxSize = mtu() - OspfUtil.LSA_HEADER_LENGTH; int noLsa = 0; while (listItr.hasNext()) { LsRequestPacket lsRequest = (LsRequestPacket) listItr.next(); LsaWrapper wrapper = ospfArea.getLsa(lsRequest.lsType(), lsRequest.linkStateId(), lsRequest.ownRouterId()); OspfLsa ospflsa = wrapper.ospfLsa(); if ((currentLength + ((LsaWrapperImpl) wrapper).lsaHeader().lsPacketLen()) >= maxSize) { listItr.previous(); break; } if (ospflsa != null) { lsupdate.addLsa(ospflsa); noLsa++; currentLength = currentLength + ((LsaWrapperImpl) wrapper).lsaHeader().lsPacketLen(); } else { nbr.badLSReq(channel); } } lsupdate.setNumberOfLsa(noLsa); if (state() == OspfInterfaceState.DR || state() == OspfInterfaceState.BDR || state() == OspfInterfaceState.POINT2POINT) { lsupdate.setDestinationIp(OspfUtil.ALL_SPF_ROUTERS); } else if (state() == OspfInterfaceState.DROTHER) { lsupdate.setDestinationIp(OspfUtil.ALL_DROUTERS); } byte[] messageToWrite = getMessage(lsupdate); ctx.getChannel().write(messageToWrite); } } } }  OspfInterfaceState state(); void setState(OspfInterfaceState ospfInterfaceState); void setChannel(Channel channel); OspfArea ospfArea(); void setController(Controller controller); void setOspfArea(OspfArea ospfArea); String interfaceState(); List<LsaHeader> linkStateHeaders(); Ip4Address ipNetworkMask(); @Override void setIpNetworkMask(Ip4Address ipNetworkMask); void addNeighbouringRouter(OspfNbr ospfNbr); OspfNbr neighbouringRouter(String neighborId); void removeNeighbors(); void removeNeighbor(OspfNbr ospfNeighbor); void addLsaHeaderForDelayAck(LsaHeader lsaHeader); void removeLsaFromNeighborMap(String lsaKey); boolean isNeighborInList(String neighborId); Map<String, OspfNbr> listOfNeighbors(); void setListOfNeighbors(HashMap<String, OspfNbr> listOfNeighbors); int interfaceIndex(); void setInterfaceIndex(int interfaceIndex); Ip4Address ipAddress(); void setIpAddress(Ip4Address ipAddress); int routerPriority(); void setRouterPriority(int routerPriority); int helloIntervalTime(); void setHelloIntervalTime(int helloIntervalTime); int routerDeadIntervalTime(); void setRouterDeadIntervalTime(int routerDeadIntervalTime); int interfaceType(); void setInterfaceType(int interfaceType); int mtu(); void setMtu(int mtu); int reTransmitInterval(); void setReTransmitInterval(int reTransmitInterval); Ip4Address dr(); void setDr(Ip4Address dr); Ip4Address bdr(); void setBdr(Ip4Address bdr); void interfaceUp(); void backupSeen(Channel ch); void waitTimer(Channel ch); void callDrElection(Channel ch); void neighborChange(); void interfaceDown(); void processOspfMessage(OspfMessage ospfMessage, ChannelHandlerContext ctx); boolean compareDdPackets(DdPacket receivedDPacket, DdPacket lastDdPacket); void startHelloTimer(); void stopHelloTimer(); void startWaitTimer(); void stopWaitTimer(); void startDelayedAckTimer(); void stopDelayedAckTimer(); void electRouter(Channel ch); List<OspfEligibleRouter> calculateListOfEligibleRouters(OspfEligibleRouter electedDr); OspfEligibleRouter electBdr(List<OspfEligibleRouter> eligibleRouters); OspfEligibleRouter electDr(List<OspfEligibleRouter> eligibleRouters,
                                      OspfEligibleRouter electedBdr); OspfEligibleRouter selectRouterBasedOnPriority(List<OspfEligibleRouter> routersList); void addDeviceInformation(OspfRouter ospfRouter); void removeDeviceInformation(OspfRouter ospfRouter); void addLinkInformation(OspfRouter ospfRouter, OspfLinkTed ospfLinkTed); void removeLinkInformation(OspfRouter ospfRouter, OspfLinkTed ospfLinkTed); @Override boolean equals(Object o); @Override int hashCode(); @Override String toString(); }
OspfInterfaceImpl implements OspfInterface { void processLsRequestMessage(OspfMessage ospfMessage, ChannelHandlerContext ctx) throws Exception { log.debug("OspfChannelHandler::processLsRequestMessage...!!!"); Channel channel = ctx.getChannel(); LsRequest lsrPacket = (LsRequest) ospfMessage; OspfNbr nbr = neighbouringRouter(lsrPacket.routerId().toString()); if (nbr.getState() == OspfNeighborState.EXCHANGE || nbr.getState() == OspfNeighborState.LOADING || nbr.getState() == OspfNeighborState.FULL) { LsRequest reqMsg = (LsRequest) ospfMessage; if (reqMsg.getLinkStateRequests().isEmpty()) { log.debug("Received Link State Request Vector is Empty "); return; } else { ListIterator<LsRequestPacket> listItr = reqMsg.getLinkStateRequests().listIterator(); while (listItr.hasNext()) { LsUpdate lsupdate = new LsUpdate(); lsupdate.setOspfVer(OspfUtil.OSPF_VERSION); lsupdate.setOspftype(OspfPacketType.LSUPDATE.value()); lsupdate.setRouterId(ospfArea.routerId()); lsupdate.setAreaId(ospfArea.areaId()); lsupdate.setAuthType(OspfUtil.NOT_ASSIGNED); lsupdate.setAuthentication(OspfUtil.NOT_ASSIGNED); lsupdate.setOspfPacLength(OspfUtil.NOT_ASSIGNED); lsupdate.setChecksum(OspfUtil.NOT_ASSIGNED); int currentLength = OspfUtil.OSPF_HEADER_LENGTH + OspfUtil.FOUR_BYTES; int maxSize = mtu() - OspfUtil.LSA_HEADER_LENGTH; int noLsa = 0; while (listItr.hasNext()) { LsRequestPacket lsRequest = (LsRequestPacket) listItr.next(); LsaWrapper wrapper = ospfArea.getLsa(lsRequest.lsType(), lsRequest.linkStateId(), lsRequest.ownRouterId()); OspfLsa ospflsa = wrapper.ospfLsa(); if ((currentLength + ((LsaWrapperImpl) wrapper).lsaHeader().lsPacketLen()) >= maxSize) { listItr.previous(); break; } if (ospflsa != null) { lsupdate.addLsa(ospflsa); noLsa++; currentLength = currentLength + ((LsaWrapperImpl) wrapper).lsaHeader().lsPacketLen(); } else { nbr.badLSReq(channel); } } lsupdate.setNumberOfLsa(noLsa); if (state() == OspfInterfaceState.DR || state() == OspfInterfaceState.BDR || state() == OspfInterfaceState.POINT2POINT) { lsupdate.setDestinationIp(OspfUtil.ALL_SPF_ROUTERS); } else if (state() == OspfInterfaceState.DROTHER) { lsupdate.setDestinationIp(OspfUtil.ALL_DROUTERS); } byte[] messageToWrite = getMessage(lsupdate); ctx.getChannel().write(messageToWrite); } } } }  OspfInterfaceState state(); void setState(OspfInterfaceState ospfInterfaceState); void setChannel(Channel channel); OspfArea ospfArea(); void setController(Controller controller); void setOspfArea(OspfArea ospfArea); String interfaceState(); List<LsaHeader> linkStateHeaders(); Ip4Address ipNetworkMask(); @Override void setIpNetworkMask(Ip4Address ipNetworkMask); void addNeighbouringRouter(OspfNbr ospfNbr); OspfNbr neighbouringRouter(String neighborId); void removeNeighbors(); void removeNeighbor(OspfNbr ospfNeighbor); void addLsaHeaderForDelayAck(LsaHeader lsaHeader); void removeLsaFromNeighborMap(String lsaKey); boolean isNeighborInList(String neighborId); Map<String, OspfNbr> listOfNeighbors(); void setListOfNeighbors(HashMap<String, OspfNbr> listOfNeighbors); int interfaceIndex(); void setInterfaceIndex(int interfaceIndex); Ip4Address ipAddress(); void setIpAddress(Ip4Address ipAddress); int routerPriority(); void setRouterPriority(int routerPriority); int helloIntervalTime(); void setHelloIntervalTime(int helloIntervalTime); int routerDeadIntervalTime(); void setRouterDeadIntervalTime(int routerDeadIntervalTime); int interfaceType(); void setInterfaceType(int interfaceType); int mtu(); void setMtu(int mtu); int reTransmitInterval(); void setReTransmitInterval(int reTransmitInterval); Ip4Address dr(); void setDr(Ip4Address dr); Ip4Address bdr(); void setBdr(Ip4Address bdr); void interfaceUp(); void backupSeen(Channel ch); void waitTimer(Channel ch); void callDrElection(Channel ch); void neighborChange(); void interfaceDown(); void processOspfMessage(OspfMessage ospfMessage, ChannelHandlerContext ctx); boolean compareDdPackets(DdPacket receivedDPacket, DdPacket lastDdPacket); void startHelloTimer(); void stopHelloTimer(); void startWaitTimer(); void stopWaitTimer(); void startDelayedAckTimer(); void stopDelayedAckTimer(); void electRouter(Channel ch); List<OspfEligibleRouter> calculateListOfEligibleRouters(OspfEligibleRouter electedDr); OspfEligibleRouter electBdr(List<OspfEligibleRouter> eligibleRouters); OspfEligibleRouter electDr(List<OspfEligibleRouter> eligibleRouters,
                                      OspfEligibleRouter electedBdr); OspfEligibleRouter selectRouterBasedOnPriority(List<OspfEligibleRouter> routersList); void addDeviceInformation(OspfRouter ospfRouter); void removeDeviceInformation(OspfRouter ospfRouter); void addLinkInformation(OspfRouter ospfRouter, OspfLinkTed ospfLinkTed); void removeLinkInformation(OspfRouter ospfRouter, OspfLinkTed ospfLinkTed); @Override boolean equals(Object o); @Override int hashCode(); @Override String toString();  }