@Test public void testElectRouter() throws Exception { ospfInterface.setOspfArea(ospfArea); ospfInterface.setDr(Ip4Address.valueOf("3.3.3.3")); ospfInterface.setBdr(Ip4Address.valueOf("3.3.3.3")); ospfInterface.setIpNetworkMask(Ip4Address.valueOf("255.255.255.255")); ChannelConfig channelConfig = EasyMock.createMock(ChannelConfig.class); EasyMock.expect(channelConfig.getBufferFactory()).andReturn(new HeapChannelBufferFactory()); Channel channel = EasyMock.createMock(Channel.class); ospfInterface.electRouter(channel); assertThat(ospfInterface.dr(), is(notNullValue())); }
public void electRouter(Channel ch) throws Exception { Ip4Address currentDr = dr(); Ip4Address currentBdr = bdr(); OspfInterfaceState oldState = state(); OspfInterfaceState newState; log.debug("OSPFInterfaceChannelHandler::electRouter -> currentDr: {}, currentBdr: {}", currentDr, currentBdr); List<OspfEligibleRouter> eligibleRouters = calculateListOfEligibleRouters(new OspfEligibleRouter()); log.debug("OSPFInterfaceChannelHandler::electRouter -> eligibleRouters: {}", eligibleRouters); OspfEligibleRouter electedBdr = electBdr(eligibleRouters); OspfEligibleRouter electedDr = electDr(eligibleRouters, electedBdr); setBdr(electedBdr.getIpAddress()); setDr(electedDr.getIpAddress()); if (electedBdr.getIpAddress().equals(ipAddress()) && !electedBdr.getIpAddress().equals(currentBdr)) { setState(OspfInterfaceState.BDR); } if (electedDr.getIpAddress().equals(ipAddress()) && !electedDr.getIpAddress().equals(currentDr)) { setState(OspfInterfaceState.DR); } if (state() != oldState && !(state() == OspfInterfaceState.DROTHER && oldState.value() < OspfInterfaceState.DROTHER.value())) { log.debug("Recalculating as the State is changed "); log.debug("OSPFInterfaceChannelHandler::electRouter -> currentDr: {}, currentBdr: {}", currentDr, currentBdr); eligibleRouters = calculateListOfEligibleRouters(new OspfEligibleRouter()); log.debug("OSPFInterfaceChannelHandler::electRouter -> eligibleRouters: {}", eligibleRouters); electedBdr = electBdr(eligibleRouters); electedDr = electDr(eligibleRouters, electedBdr); setBdr(electedBdr.getIpAddress()); setDr(electedDr.getIpAddress()); } if (electedBdr.getIpAddress().equals(ipAddress()) && !electedBdr.getIpAddress().equals(currentBdr)) { setState(OspfInterfaceState.BDR); ospfArea.refreshArea(this); } if (electedDr.getIpAddress().equals(ipAddress()) && !electedDr.getIpAddress().equals(currentDr)) { setState(OspfInterfaceState.DR); ospfArea.refreshArea(this); } if (currentDr != electedDr.getIpAddress() || currentBdr != electedBdr.getIpAddress()) { Set<String> negibhorIdList; negibhorIdList = listOfNeighbors().keySet(); for (String routerid : negibhorIdList) { OspfNbrImpl nbr = (OspfNbrImpl) neighbouringRouter(routerid); if (nbr.getState().getValue() >= OspfNeighborState.TWOWAY.getValue()) { nbr.adjOk(ch); } } } log.debug("OSPFInterfaceChannelHandler::electRouter -> ElectedDR: {}, ElectedBDR: {}", electedDr.getIpAddress(), electedBdr.getIpAddress()); }
OspfInterfaceImpl implements OspfInterface { public void electRouter(Channel ch) throws Exception { Ip4Address currentDr = dr(); Ip4Address currentBdr = bdr(); OspfInterfaceState oldState = state(); OspfInterfaceState newState; log.debug("OSPFInterfaceChannelHandler::electRouter -> currentDr: {}, currentBdr: {}", currentDr, currentBdr); List<OspfEligibleRouter> eligibleRouters = calculateListOfEligibleRouters(new OspfEligibleRouter()); log.debug("OSPFInterfaceChannelHandler::electRouter -> eligibleRouters: {}", eligibleRouters); OspfEligibleRouter electedBdr = electBdr(eligibleRouters); OspfEligibleRouter electedDr = electDr(eligibleRouters, electedBdr); setBdr(electedBdr.getIpAddress()); setDr(electedDr.getIpAddress()); if (electedBdr.getIpAddress().equals(ipAddress()) && !electedBdr.getIpAddress().equals(currentBdr)) { setState(OspfInterfaceState.BDR); } if (electedDr.getIpAddress().equals(ipAddress()) && !electedDr.getIpAddress().equals(currentDr)) { setState(OspfInterfaceState.DR); } if (state() != oldState && !(state() == OspfInterfaceState.DROTHER && oldState.value() < OspfInterfaceState.DROTHER.value())) { log.debug("Recalculating as the State is changed "); log.debug("OSPFInterfaceChannelHandler::electRouter -> currentDr: {}, currentBdr: {}", currentDr, currentBdr); eligibleRouters = calculateListOfEligibleRouters(new OspfEligibleRouter()); log.debug("OSPFInterfaceChannelHandler::electRouter -> eligibleRouters: {}", eligibleRouters); electedBdr = electBdr(eligibleRouters); electedDr = electDr(eligibleRouters, electedBdr); setBdr(electedBdr.getIpAddress()); setDr(electedDr.getIpAddress()); } if (electedBdr.getIpAddress().equals(ipAddress()) && !electedBdr.getIpAddress().equals(currentBdr)) { setState(OspfInterfaceState.BDR); ospfArea.refreshArea(this); } if (electedDr.getIpAddress().equals(ipAddress()) && !electedDr.getIpAddress().equals(currentDr)) { setState(OspfInterfaceState.DR); ospfArea.refreshArea(this); } if (currentDr != electedDr.getIpAddress() || currentBdr != electedBdr.getIpAddress()) { Set<String> negibhorIdList; negibhorIdList = listOfNeighbors().keySet(); for (String routerid : negibhorIdList) { OspfNbrImpl nbr = (OspfNbrImpl) neighbouringRouter(routerid); if (nbr.getState().getValue() >= OspfNeighborState.TWOWAY.getValue()) { nbr.adjOk(ch); } } } log.debug("OSPFInterfaceChannelHandler::electRouter -> ElectedDR: {}, ElectedBDR: {}", electedDr.getIpAddress(), electedBdr.getIpAddress()); } }
OspfInterfaceImpl implements OspfInterface { public void electRouter(Channel ch) throws Exception { Ip4Address currentDr = dr(); Ip4Address currentBdr = bdr(); OspfInterfaceState oldState = state(); OspfInterfaceState newState; log.debug("OSPFInterfaceChannelHandler::electRouter -> currentDr: {}, currentBdr: {}", currentDr, currentBdr); List<OspfEligibleRouter> eligibleRouters = calculateListOfEligibleRouters(new OspfEligibleRouter()); log.debug("OSPFInterfaceChannelHandler::electRouter -> eligibleRouters: {}", eligibleRouters); OspfEligibleRouter electedBdr = electBdr(eligibleRouters); OspfEligibleRouter electedDr = electDr(eligibleRouters, electedBdr); setBdr(electedBdr.getIpAddress()); setDr(electedDr.getIpAddress()); if (electedBdr.getIpAddress().equals(ipAddress()) && !electedBdr.getIpAddress().equals(currentBdr)) { setState(OspfInterfaceState.BDR); } if (electedDr.getIpAddress().equals(ipAddress()) && !electedDr.getIpAddress().equals(currentDr)) { setState(OspfInterfaceState.DR); } if (state() != oldState && !(state() == OspfInterfaceState.DROTHER && oldState.value() < OspfInterfaceState.DROTHER.value())) { log.debug("Recalculating as the State is changed "); log.debug("OSPFInterfaceChannelHandler::electRouter -> currentDr: {}, currentBdr: {}", currentDr, currentBdr); eligibleRouters = calculateListOfEligibleRouters(new OspfEligibleRouter()); log.debug("OSPFInterfaceChannelHandler::electRouter -> eligibleRouters: {}", eligibleRouters); electedBdr = electBdr(eligibleRouters); electedDr = electDr(eligibleRouters, electedBdr); setBdr(electedBdr.getIpAddress()); setDr(electedDr.getIpAddress()); } if (electedBdr.getIpAddress().equals(ipAddress()) && !electedBdr.getIpAddress().equals(currentBdr)) { setState(OspfInterfaceState.BDR); ospfArea.refreshArea(this); } if (electedDr.getIpAddress().equals(ipAddress()) && !electedDr.getIpAddress().equals(currentDr)) { setState(OspfInterfaceState.DR); ospfArea.refreshArea(this); } if (currentDr != electedDr.getIpAddress() || currentBdr != electedBdr.getIpAddress()) { Set<String> negibhorIdList; negibhorIdList = listOfNeighbors().keySet(); for (String routerid : negibhorIdList) { OspfNbrImpl nbr = (OspfNbrImpl) neighbouringRouter(routerid); if (nbr.getState().getValue() >= OspfNeighborState.TWOWAY.getValue()) { nbr.adjOk(ch); } } } log.debug("OSPFInterfaceChannelHandler::electRouter -> ElectedDR: {}, ElectedBDR: {}", electedDr.getIpAddress(), electedBdr.getIpAddress()); }  }
OspfInterfaceImpl implements OspfInterface { public void electRouter(Channel ch) throws Exception { Ip4Address currentDr = dr(); Ip4Address currentBdr = bdr(); OspfInterfaceState oldState = state(); OspfInterfaceState newState; log.debug("OSPFInterfaceChannelHandler::electRouter -> currentDr: {}, currentBdr: {}", currentDr, currentBdr); List<OspfEligibleRouter> eligibleRouters = calculateListOfEligibleRouters(new OspfEligibleRouter()); log.debug("OSPFInterfaceChannelHandler::electRouter -> eligibleRouters: {}", eligibleRouters); OspfEligibleRouter electedBdr = electBdr(eligibleRouters); OspfEligibleRouter electedDr = electDr(eligibleRouters, electedBdr); setBdr(electedBdr.getIpAddress()); setDr(electedDr.getIpAddress()); if (electedBdr.getIpAddress().equals(ipAddress()) && !electedBdr.getIpAddress().equals(currentBdr)) { setState(OspfInterfaceState.BDR); } if (electedDr.getIpAddress().equals(ipAddress()) && !electedDr.getIpAddress().equals(currentDr)) { setState(OspfInterfaceState.DR); } if (state() != oldState && !(state() == OspfInterfaceState.DROTHER && oldState.value() < OspfInterfaceState.DROTHER.value())) { log.debug("Recalculating as the State is changed "); log.debug("OSPFInterfaceChannelHandler::electRouter -> currentDr: {}, currentBdr: {}", currentDr, currentBdr); eligibleRouters = calculateListOfEligibleRouters(new OspfEligibleRouter()); log.debug("OSPFInterfaceChannelHandler::electRouter -> eligibleRouters: {}", eligibleRouters); electedBdr = electBdr(eligibleRouters); electedDr = electDr(eligibleRouters, electedBdr); setBdr(electedBdr.getIpAddress()); setDr(electedDr.getIpAddress()); } if (electedBdr.getIpAddress().equals(ipAddress()) && !electedBdr.getIpAddress().equals(currentBdr)) { setState(OspfInterfaceState.BDR); ospfArea.refreshArea(this); } if (electedDr.getIpAddress().equals(ipAddress()) && !electedDr.getIpAddress().equals(currentDr)) { setState(OspfInterfaceState.DR); ospfArea.refreshArea(this); } if (currentDr != electedDr.getIpAddress() || currentBdr != electedBdr.getIpAddress()) { Set<String> negibhorIdList; negibhorIdList = listOfNeighbors().keySet(); for (String routerid : negibhorIdList) { OspfNbrImpl nbr = (OspfNbrImpl) neighbouringRouter(routerid); if (nbr.getState().getValue() >= OspfNeighborState.TWOWAY.getValue()) { nbr.adjOk(ch); } } } log.debug("OSPFInterfaceChannelHandler::electRouter -> ElectedDR: {}, ElectedBDR: {}", electedDr.getIpAddress(), electedBdr.getIpAddress()); }  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 { public void electRouter(Channel ch) throws Exception { Ip4Address currentDr = dr(); Ip4Address currentBdr = bdr(); OspfInterfaceState oldState = state(); OspfInterfaceState newState; log.debug("OSPFInterfaceChannelHandler::electRouter -> currentDr: {}, currentBdr: {}", currentDr, currentBdr); List<OspfEligibleRouter> eligibleRouters = calculateListOfEligibleRouters(new OspfEligibleRouter()); log.debug("OSPFInterfaceChannelHandler::electRouter -> eligibleRouters: {}", eligibleRouters); OspfEligibleRouter electedBdr = electBdr(eligibleRouters); OspfEligibleRouter electedDr = electDr(eligibleRouters, electedBdr); setBdr(electedBdr.getIpAddress()); setDr(electedDr.getIpAddress()); if (electedBdr.getIpAddress().equals(ipAddress()) && !electedBdr.getIpAddress().equals(currentBdr)) { setState(OspfInterfaceState.BDR); } if (electedDr.getIpAddress().equals(ipAddress()) && !electedDr.getIpAddress().equals(currentDr)) { setState(OspfInterfaceState.DR); } if (state() != oldState && !(state() == OspfInterfaceState.DROTHER && oldState.value() < OspfInterfaceState.DROTHER.value())) { log.debug("Recalculating as the State is changed "); log.debug("OSPFInterfaceChannelHandler::electRouter -> currentDr: {}, currentBdr: {}", currentDr, currentBdr); eligibleRouters = calculateListOfEligibleRouters(new OspfEligibleRouter()); log.debug("OSPFInterfaceChannelHandler::electRouter -> eligibleRouters: {}", eligibleRouters); electedBdr = electBdr(eligibleRouters); electedDr = electDr(eligibleRouters, electedBdr); setBdr(electedBdr.getIpAddress()); setDr(electedDr.getIpAddress()); } if (electedBdr.getIpAddress().equals(ipAddress()) && !electedBdr.getIpAddress().equals(currentBdr)) { setState(OspfInterfaceState.BDR); ospfArea.refreshArea(this); } if (electedDr.getIpAddress().equals(ipAddress()) && !electedDr.getIpAddress().equals(currentDr)) { setState(OspfInterfaceState.DR); ospfArea.refreshArea(this); } if (currentDr != electedDr.getIpAddress() || currentBdr != electedBdr.getIpAddress()) { Set<String> negibhorIdList; negibhorIdList = listOfNeighbors().keySet(); for (String routerid : negibhorIdList) { OspfNbrImpl nbr = (OspfNbrImpl) neighbouringRouter(routerid); if (nbr.getState().getValue() >= OspfNeighborState.TWOWAY.getValue()) { nbr.adjOk(ch); } } } log.debug("OSPFInterfaceChannelHandler::electRouter -> ElectedDR: {}, ElectedBDR: {}", electedDr.getIpAddress(), electedBdr.getIpAddress()); }  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();  }