PeerConfigurationParser { public PeerConfiguration parseConfiguration(HierarchicalConfiguration config) throws ConfigurationException { PeerConfigurationImpl peerConfig = new PeerConfigurationImpl(); List<HierarchicalConfiguration> clientConfigs = config.configurationsAt("Client"); List<HierarchicalConfiguration> capabilityConfigs = config.configurationsAt("Capabilities"); try { peerConfig.setPeerName(config.getString("[@name]")); } catch(NoSuchElementException e) { throw new ConfigurationException("peer name not set", e); } if(clientConfigs.size() > 1) { throw new ConfigurationException("duplicate <Client/> element"); } else if(clientConfigs.size() == 0) { throw new ConfigurationException("missing <Client/> element"); } else peerConfig.setClientConfig(clientConfigurationParser.parseConfig(clientConfigs.get(0))); if(capabilityConfigs.size() > 1) { throw new ConfigurationException("duplicate <Capabilities/> element"); } else if(capabilityConfigs.size() == 1) peerConfig.setCapabilities(capabilityParser.parseConfig(capabilityConfigs.get(0))); try { peerConfig.setLocalAS(config.getInt("AutonomousSystem[@local]")); } catch(NoSuchElementException e) { throw new ConfigurationException("local AS number not given", e); } try { peerConfig.setRemoteAS(config.getInt("AutonomousSystem[@remote]")); } catch(NoSuchElementException e) { throw new ConfigurationException("remote AS number not given", e); } try { long identifier = config.getLong("BgpIdentifier[@local]"); if(!isValidBgpIdentifier(identifier)) throw new ConfigurationException("Invalid local BGP identifier: " + identifier); peerConfig.setLocalBgpIdentifier(identifier); } catch(NoSuchElementException e) { throw new ConfigurationException("local BGP identifier not given", e); } try { long identifier = config.getLong("BgpIdentifier[@remote]"); if(!isValidBgpIdentifier(identifier)) throw new ConfigurationException("Invalid remote BGP identifier: " + identifier); peerConfig.setRemoteBgpIdentifier(identifier); } catch(NoSuchElementException e) { throw new ConfigurationException("remote BGP identifier not given", e); } peerConfig.setHoldTime(config.getInt("Timers[@holdTime]", 0)); peerConfig.setIdleHoldTime(config.getInt("Timers[@idleHoldTime]", 0)); peerConfig.setDelayOpenTime(config.getInt("Timers[@delayOpenTime]", 0)); peerConfig.setConnectRetryTime(config.getInt("Timers[@connectRetryTime]", 0)); peerConfig.setAutomaticStartInterval(config.getInt("Timers[@automaticStartInterval]", 0)); peerConfig.setAllowAutomaticStart(config.getBoolean("Options[@allowAutomaticStart]", true)); peerConfig.setAllowAutomaticStop(config.getBoolean("Options[@allowAutomaticStop]", false)); peerConfig.setDampPeerOscillation(config.getBoolean("Options[@dampPeerOscillation]", false)); peerConfig.setCollisionDetectEstablishedState(config.getBoolean("Options[@collisionDetectEstablishedState]", false)); peerConfig.setDelayOpen(config.getBoolean("Options[@delayOpen]", false)); peerConfig.setPassiveTcpEstablishment(config.getBoolean("Options[@passiveTcpEstablishment]", false)); peerConfig.setHoldTimerDisabled(config.getBoolean("Options[@holdTimerDisabled]", false)); return peerConfig; }  PeerConfiguration parseConfiguration(HierarchicalConfiguration config);  }
@Test public void testAcceptedMinimalConfiguration() throws Exception { PeerConfiguration peerConfig = parser.parseConfiguration(config.configurationAt("BgpPeer(0)")); Assert.assertEquals(InetAddress.getByName("192.168.4.1"), peerConfig.getClientConfig().getRemoteAddress().getAddress()); Assert.assertEquals(179, peerConfig.getClientConfig().getRemoteAddress().getPort()); Assert.assertEquals(10, peerConfig.getLocalAS()); Assert.assertEquals(11, peerConfig.getRemoteAS()); Assert.assertEquals("foo", peerConfig.getPeerName()); Assert.assertEquals(200, peerConfig.getLocalBgpIdentifier()); Assert.assertEquals(300, peerConfig.getRemoteBgpIdentifier()); Assert.assertEquals(0, peerConfig.getHoldTime()); Assert.assertEquals(0, peerConfig.getIdleHoldTime()); }
@Test(expected=ConfigurationException.class) public void testBogusConfigurationMissingClientConfiguration() throws Exception { @SuppressWarnings("unused") PeerConfiguration peerConfig = parser.parseConfiguration(config.configurationAt("BgpPeer(1)")); }
@Test(expected=ConfigurationException.class) public void testBogusConfigurationInvalidLocalAS() throws Exception { @SuppressWarnings("unused") PeerConfiguration peerConfig = parser.parseConfiguration(config.configurationAt("BgpPeer(2)")); }
@Test(expected=ConfigurationException.class) public void testBogusConfigurationInvalidRemoteAS() throws Exception { @SuppressWarnings("unused") PeerConfiguration peerConfig = parser.parseConfiguration(config.configurationAt("BgpPeer(3)")); }
@Test(expected=ConfigurationException.class) public void testBogusConfigurationDuplicateClientConfiguration() throws Exception { @SuppressWarnings("unused") PeerConfiguration peerConfig = parser.parseConfiguration(config.configurationAt("BgpPeer(4)")); }
@Test(expected=ConfigurationException.class) public void testBogusConfigurationBgpIdentifierMissing() throws Exception { @SuppressWarnings("unused") PeerConfiguration peerConfig = parser.parseConfiguration(config.configurationAt("BgpPeer(5)")); }
@Test(expected=ConfigurationException.class) public void testBogusConfigurationLocalBgpIdentifierMissing() throws Exception { @SuppressWarnings("unused") PeerConfiguration peerConfig = parser.parseConfiguration(config.configurationAt("BgpPeer(6)")); }
@Test(expected=ConfigurationException.class) public void testBogusConfigurationRemoteBgpIdentifierMissing() throws Exception { @SuppressWarnings("unused") PeerConfiguration peerConfig = parser.parseConfiguration(config.configurationAt("BgpPeer(7)")); }
@Test public void testAcceptedConfigurationWithHoldTimerAndRetryIntervalAndDelayOpenAndConnectRetryTimeAndAutomaticStartInterval() throws Exception { PeerConfiguration peerConfig = parser.parseConfiguration(config.configurationAt("BgpPeer(8)")); Assert.assertEquals(InetAddress.getByName("192.168.4.1"), peerConfig.getClientConfig().getRemoteAddress().getAddress()); Assert.assertEquals(179, peerConfig.getClientConfig().getRemoteAddress().getPort()); Assert.assertEquals(10, peerConfig.getLocalAS()); Assert.assertEquals(11, peerConfig.getRemoteAS()); Assert.assertEquals("foo", peerConfig.getPeerName()); Assert.assertEquals(200, peerConfig.getLocalBgpIdentifier()); Assert.assertEquals(300, peerConfig.getRemoteBgpIdentifier()); Assert.assertEquals(30, peerConfig.getHoldTime()); Assert.assertEquals(300, peerConfig.getIdleHoldTime()); Assert.assertEquals(45, peerConfig.getDelayOpenTime()); Assert.assertEquals(240, peerConfig.getConnectRetryTime()); Assert.assertEquals(600, peerConfig.getAutomaticStartInterval()); Assert.assertTrue(peerConfig.isAllowAutomaticStart()); Assert.assertFalse(peerConfig.isAllowAutomaticStop()); Assert.assertFalse(peerConfig.isCollisionDetectEstablishedState()); Assert.assertFalse(peerConfig.isDampPeerOscillation()); Assert.assertFalse(peerConfig.isDelayOpen()); Assert.assertFalse(peerConfig.isPassiveTcpEstablishment()); }
@Test public void testAcceptedConfigurationWithOptions() throws Exception { PeerConfiguration peerConfig = parser.parseConfiguration(config.configurationAt("BgpPeer(9)")); Assert.assertEquals(InetAddress.getByName("192.168.4.1"), peerConfig.getClientConfig().getRemoteAddress().getAddress()); Assert.assertEquals(179, peerConfig.getClientConfig().getRemoteAddress().getPort()); Assert.assertEquals(10, peerConfig.getLocalAS()); Assert.assertEquals(11, peerConfig.getRemoteAS()); Assert.assertEquals("foo", peerConfig.getPeerName()); Assert.assertEquals(200, peerConfig.getLocalBgpIdentifier()); Assert.assertEquals(300, peerConfig.getRemoteBgpIdentifier()); Assert.assertFalse(peerConfig.isAllowAutomaticStart()); Assert.assertTrue(peerConfig.isAllowAutomaticStop()); Assert.assertTrue(peerConfig.isCollisionDetectEstablishedState()); Assert.assertTrue(peerConfig.isDampPeerOscillation()); Assert.assertTrue(peerConfig.isDelayOpen()); Assert.assertTrue(peerConfig.isPassiveTcpEstablishment()); Assert.assertTrue(peerConfig.isHoldTimerDisabled()); }
@Test public void testAcceptedConfigurationWithOptionsAndCapabilities() throws Exception { PeerConfiguration peerConfig = parser.parseConfiguration(config.configurationAt("BgpPeer(10)")); Assert.assertEquals(InetAddress.getByName("192.168.4.1"), peerConfig.getClientConfig().getRemoteAddress().getAddress()); Assert.assertEquals(179, peerConfig.getClientConfig().getRemoteAddress().getPort()); Assert.assertEquals(10, peerConfig.getLocalAS()); Assert.assertEquals(11, peerConfig.getRemoteAS()); Assert.assertEquals("foo", peerConfig.getPeerName()); Assert.assertEquals(200, peerConfig.getLocalBgpIdentifier()); Assert.assertEquals(300, peerConfig.getRemoteBgpIdentifier()); Assert.assertFalse(peerConfig.isAllowAutomaticStart()); Assert.assertTrue(peerConfig.isAllowAutomaticStop()); Assert.assertTrue(peerConfig.isCollisionDetectEstablishedState()); Assert.assertTrue(peerConfig.isDampPeerOscillation()); Assert.assertTrue(peerConfig.isDelayOpen()); Assert.assertTrue(peerConfig.isPassiveTcpEstablishment()); Assert.assertTrue(peerConfig.isHoldTimerDisabled()); Iterator<Capability> capIt = peerConfig.getCapabilities().getRequiredCapabilities().iterator(); Assert.assertTrue(capIt.hasNext()); AutonomousSystem4Capability as4cap = (AutonomousSystem4Capability)capIt.next(); Assert.assertEquals(256, as4cap.getAutonomousSystem()); MultiProtocolCapability mpCap = (MultiProtocolCapability)capIt.next(); Assert.assertEquals(AddressFamily.IPv4, mpCap.getAfi()); Assert.assertEquals(SubsequentAddressFamily.NLRI_UNICAST_FORWARDING, mpCap.getSafi()); OutboundRouteFilteringCapability oorCap = (OutboundRouteFilteringCapability)capIt.next(); Map<ORFType, ORFSendReceive> filters = oorCap.getFilters(); Assert.assertEquals(AddressFamily.IPv4, oorCap.getAddressFamily()); Assert.assertEquals(SubsequentAddressFamily.NLRI_UNICAST_FORWARDING, oorCap.getSubsequentAddressFamily()); Assert.assertEquals(1, filters.size()); Assert.assertTrue(filters.containsKey(ORFType.ADDRESS_PREFIX_BASED)); Assert.assertEquals(ORFSendReceive.BOTH, filters.get(ORFType.ADDRESS_PREFIX_BASED)); Assert.assertTrue(capIt.hasNext()); Assert.assertEquals(RouteRefreshCapability.class, capIt.next().getClass()); Assert.assertFalse(capIt.hasNext()); }