use of org.opendaylight.protocol.bgp.rib.spi.BGPSessionListener in project bgpcep by opendaylight.
the class BGPSessionImplTest method testSessionRecoveryOnException.
@Test
public void testSessionRecoveryOnException() throws Exception {
final BGPSessionListener listener = mock(BGPSessionListener.class);
Mockito.doThrow(new RuntimeException("Mocked runtime exception.")).when(listener).onSessionUp(Matchers.any());
this.bgpSession = Mockito.spy(new BGPSessionImpl(listener, this.speakerListener, this.classicOpen, this.classicOpen.getHoldTimer(), null));
this.bgpSession.setChannelExtMsgCoder(this.classicOpen);
Mockito.verify(this.bgpSession, Mockito.never()).handleException(Matchers.any());
Mockito.verify(this.bgpSession, Mockito.never()).writeAndFlush(Matchers.any(Notification.class));
Mockito.verify(this.bgpSession, Mockito.never()).terminate(Matchers.any(BGPDocumentedException.class));
try {
this.bgpSession.sessionUp();
// expect the exception to be populated
Assert.fail();
} catch (final RuntimeException ignored) {
}
Assert.assertNotEquals(State.UP, this.bgpSession.getState());
Mockito.verify(this.bgpSession).handleException(Matchers.any());
Mockito.verify(this.bgpSession).writeAndFlush(Matchers.any(Notification.class));
Mockito.verify(this.bgpSession).terminate(Matchers.any(BGPDocumentedException.class));
Mockito.verify(listener).onSessionTerminated(this.bgpSession, new BGPTerminationReason(BGPError.CEASE));
}
use of org.opendaylight.protocol.bgp.rib.spi.BGPSessionListener in project bgpcep by opendaylight.
the class StrictBGPPeerRegistryTest method testPeerConnectionSuccessfull.
@Test
public void testPeerConnectionSuccessfull() throws Exception {
final Ipv4Address to2 = new Ipv4Address("255.255.255.254");
final IpAddress remoteIp2 = new IpAddress(to2);
this.peerRegistry.addPeer(REMOTE_IP, this.peer1, this.mockPreferences);
final BGPSessionListener session2 = getMockSession();
this.peerRegistry.addPeer(remoteIp2, session2, this.mockPreferences);
final BGPSessionListener returnedSession1 = this.peerRegistry.getPeer(REMOTE_IP, FROM, TO, this.classicOpen);
assertSame(this.peer1, returnedSession1);
final BGPSessionListener returnedSession2 = this.peerRegistry.getPeer(remoteIp2, FROM, to2, this.classicOpen);
assertSame(session2, returnedSession2);
Mockito.verifyZeroInteractions(this.peer1);
Mockito.verifyZeroInteractions(session2);
}
use of org.opendaylight.protocol.bgp.rib.spi.BGPSessionListener in project bgpcep by opendaylight.
the class StrictBGPPeerRegistryTest method getMockSession.
private static BGPSessionListener getMockSession() {
final BGPSessionListener mock = Mockito.mock(BGPSessionListener.class);
Mockito.doReturn(Futures.immediateFuture(null)).when(mock).releaseConnection();
return mock;
}
use of org.opendaylight.protocol.bgp.rib.spi.BGPSessionListener in project bgpcep by opendaylight.
the class StrictBGPPeerRegistry method getPeer.
@Override
public synchronized BGPSessionListener getPeer(final IpAddress ip, final Ipv4Address sourceId, final Ipv4Address remoteId, final Open openObj) throws BGPDocumentedException {
requireNonNull(ip);
requireNonNull(sourceId);
requireNonNull(remoteId);
final AsNumber remoteAsNumber = AsNumberUtil.advertizedAsNumber(openObj);
requireNonNull(remoteAsNumber);
final BGPSessionPreferences prefs = getPeerPreferences(ip);
checkPeerConfigured(ip);
final BGPSessionId currentConnection = new BGPSessionId(sourceId, remoteId, remoteAsNumber);
final BGPSessionListener p = this.peers.get(ip);
final BGPSessionId previousConnection = this.sessionIds.get(ip);
if (previousConnection != null) {
LOG.warn("Duplicate BGP session established with {}", ip);
// Session reestablished with different ids
if (!previousConnection.equals(currentConnection)) {
LOG.warn("BGP session with {} {} has to be dropped. Same session already present {}", ip, currentConnection, previousConnection);
throw new BGPDocumentedException(String.format("BGP session with %s %s has to be dropped. Same session already present %s", ip, currentConnection, previousConnection), BGPError.CEASE);
// Session reestablished with lower source bgp id, dropping current
} else if (previousConnection.isHigherDirection(currentConnection) || previousConnection.hasHigherAsNumber(currentConnection)) {
LOG.warn("BGP session with {} {} has to be dropped. Opposite session already present", ip, currentConnection);
throw new BGPDocumentedException(String.format("BGP session with %s initiated %s has to be dropped. " + "Opposite session already present", ip, currentConnection), BGPError.CEASE);
// Session reestablished with higher source bgp id, dropping previous
} else if (currentConnection.isHigherDirection(previousConnection) || currentConnection.hasHigherAsNumber(previousConnection)) {
LOG.warn("BGP session with {} {} released. Replaced by opposite session", ip, previousConnection);
this.peers.get(ip).releaseConnection();
return this.peers.get(ip);
// Session reestablished with same source bgp id, dropping current as duplicate
} else {
LOG.warn("BGP session with %s initiated from %s to %s has to be dropped. Same session already present", ip, sourceId, remoteId);
throw new BGPDocumentedException(String.format("BGP session with %s initiated %s has to be dropped. " + "Same session already present", ip, currentConnection), BGPError.CEASE);
}
}
validateAs(remoteAsNumber, openObj, prefs);
// Map session id to peer IP address
this.sessionIds.put(ip, currentConnection);
for (final PeerRegistrySessionListener peerRegistrySessionListener : this.sessionListeners) {
peerRegistrySessionListener.onSessionCreated(ip);
}
return p;
}
use of org.opendaylight.protocol.bgp.rib.spi.BGPSessionListener in project bgpcep by opendaylight.
the class AbstractBGPSessionNegotiator method handleOpen.
private synchronized void handleOpen(final Open openObj) {
final IpAddress remoteIp = getRemoteIp();
final BGPSessionPreferences preferences = this.registry.getPeerPreferences(remoteIp);
try {
final BGPSessionListener peer = this.registry.getPeer(remoteIp, getSourceId(openObj, preferences), getDestinationId(openObj, preferences), openObj);
sendMessage(new KeepaliveBuilder().build());
this.state = State.OPEN_CONFIRM;
this.session = new BGPSessionImpl(peer, this.channel, openObj, preferences, this.registry);
this.session.setChannelExtMsgCoder(openObj);
LOG.debug("Channel {} moved to OPEN_CONFIRM state with remote proposal {}", this.channel, openObj);
} catch (final BGPDocumentedException e) {
LOG.warn("Channel {} negotiation failed", this.channel, e);
negotiationFailed(e);
}
}
Aggregations