use of org.opendaylight.protocol.bgp.parser.BGPDocumentedException in project bgpcep by opendaylight.
the class EventBusRegistration method sendMessage.
private static void sendMessage(final BGPSessionListener listener, final Notification message) {
if (BGPMock.CONNECTION_LOST_MAGIC_MSG.equals(message)) {
listener.onSessionTerminated(null, new BGPTerminationReason(BGPError.CEASE));
} else if (message instanceof Open) {
final Set<BgpTableType> tts = Sets.newHashSet();
final List<AddressFamilies> addPathCapabilitiesList = Lists.newArrayList();
for (final BgpParameters param : ((Open) message).getBgpParameters()) {
for (final OptionalCapabilities capa : param.getOptionalCapabilities()) {
final CParameters cParam = capa.getCParameters();
if (cParam.getAugmentation(CParameters1.class) == null) {
continue;
}
if (cParam.getAugmentation(CParameters1.class).getMultiprotocolCapability() != null) {
final MultiprotocolCapability p = cParam.getAugmentation(CParameters1.class).getMultiprotocolCapability();
LOG.debug("Adding open parameter {}", p);
final BgpTableType type = new BgpTableTypeImpl(p.getAfi(), p.getSafi());
tts.add(type);
} else if (cParam.getAugmentation(CParameters1.class).getAddPathCapability() != null) {
final AddPathCapability addPathCap = cParam.getAugmentation(CParameters1.class).getAddPathCapability();
addPathCapabilitiesList.addAll(addPathCap.getAddressFamilies());
}
}
}
listener.onSessionUp(new MockBGPSession(tts));
} else if (!(message instanceof Keepalive)) {
try {
listener.onMessage(new MockBGPSession(), message);
} catch (BGPDocumentedException e) {
LOG.warn("Exception encountered while handling message", e);
}
}
}
use of org.opendaylight.protocol.bgp.parser.BGPDocumentedException in project bgpcep by opendaylight.
the class PeerTest method testClassicPeer.
@Test
public void testClassicPeer() throws Exception {
this.classic = new BGPPeer(this.neighborAddress, getRib(), PeerRole.Ibgp, null, Collections.emptySet(), Collections.emptySet());
this.classic.instantiateServiceInstance();
this.mockSession();
assertEquals(this.neighborAddress.getIpv4Address().getValue(), this.classic.getName());
this.classic.onSessionUp(this.session);
Assert.assertArrayEquals(new byte[] { 1, 1, 1, 1 }, this.classic.getRawIdentifier());
assertEquals("BGPPeer{name=127.0.0.1, tables=[TablesKey{_afi=interface org.opendaylight.yang.gen.v1.urn" + ".opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily," + " _safi=interface org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types" + ".rev130919.UnicastSubsequentAddressFamily}]}", this.classic.toString());
final Nlri n1 = new NlriBuilder().setPrefix(new Ipv4Prefix("8.0.1.0/28")).build();
final Nlri n2 = new NlriBuilder().setPrefix(new Ipv4Prefix("127.0.0.1/32")).build();
final Nlri n3 = new NlriBuilder().setPrefix(new Ipv4Prefix("2.2.2.2/24")).build();
final List<Nlri> nlris = Lists.newArrayList(n1, n2, n3);
final UpdateBuilder ub = new UpdateBuilder();
ub.setNlri(nlris);
final Origin origin = new OriginBuilder().setValue(BgpOrigin.Igp).build();
final AsPath asPath = new AsPathBuilder().setSegments(Collections.emptyList()).build();
final CNextHop nextHop = new Ipv4NextHopCaseBuilder().setIpv4NextHop(new Ipv4NextHopBuilder().setGlobal(new Ipv4Address("127.0.0.1")).build()).build();
final AttributesBuilder ab = new AttributesBuilder();
ub.setAttributes(ab.setOrigin(origin).setAsPath(asPath).setCNextHop(nextHop).build());
try {
this.classic.onMessage(this.session, ub.build());
fail();
} catch (final BGPDocumentedException e) {
assertEquals(BGPError.MANDATORY_ATTR_MISSING_MSG + "LOCAL_PREF", e.getMessage());
assertEquals(BGPError.WELL_KNOWN_ATTR_MISSING.getCode(), e.getError().getCode());
assertEquals(BGPError.WELL_KNOWN_ATTR_MISSING.getSubcode(), e.getError().getSubcode());
assertArrayEquals(new byte[] { LocalPreferenceAttributeParser.TYPE }, e.getData());
}
assertEquals(0, this.routes.size());
final LocalPref localPref = new LocalPrefBuilder().setPref((long) 100).build();
ub.setAttributes(ab.setLocalPref(localPref).build());
this.classic.onMessage(this.session, ub.build());
assertEquals(3, this.routes.size());
// create new peer so that it gets advertized routes from RIB
final BGPPeer testingPeer = new BGPPeer(this.neighborAddress, getRib(), PeerRole.Ibgp, null, Collections.emptySet(), Collections.emptySet());
testingPeer.instantiateServiceInstance();
testingPeer.onSessionUp(this.session);
assertEquals(3, this.routes.size());
final Nlri n11 = new NlriBuilder().setPrefix(new Ipv4Prefix("8.0.1.0/28")).build();
final Nlri n22 = new NlriBuilder().setPrefix(new Ipv4Prefix("8.0.1.16/28")).build();
final List<Nlri> nlris2 = Lists.newArrayList(n11, n22);
ub.setNlri(nlris2);
final WithdrawnRoutes w1 = new WithdrawnRoutesBuilder().setPrefix(new Ipv4Prefix("8.0.1.0/28")).build();
final WithdrawnRoutes w2 = new WithdrawnRoutesBuilder().setPrefix(new Ipv4Prefix("127.0.0.1/32")).build();
final WithdrawnRoutes w3 = new WithdrawnRoutesBuilder().setPrefix(new Ipv4Prefix("2.2.2.2/24")).build();
final List<WithdrawnRoutes> wrs = Lists.newArrayList(w1, w2, w3);
ub.setWithdrawnRoutes(wrs);
this.classic.onMessage(this.session, ub.build());
assertEquals(2, this.routes.size());
this.classic.onMessage(this.session, new KeepaliveBuilder().build());
this.classic.onMessage(this.session, new UpdateBuilder().setAttributes(new AttributesBuilder().addAugmentation(Attributes2.class, new Attributes2Builder().setMpUnreachNlri(new MpUnreachNlriBuilder().setAfi(AFI).setSafi(SAFI).build()).build()).build()).build());
this.classic.onMessage(this.session, new RouteRefreshBuilder().setAfi(AFI).setSafi(SAFI).build());
this.classic.onMessage(this.session, new RouteRefreshBuilder().setAfi(Ipv6AddressFamily.class).setSafi(SAFI).build());
assertEquals(2, this.routes.size());
this.classic.releaseConnection();
}
use of org.opendaylight.protocol.bgp.parser.BGPDocumentedException in project bgpcep by opendaylight.
the class StrictBGPPeerRegistryTest method testDropSecondPeer.
@Test
public void testDropSecondPeer() throws Exception {
final Ipv4Address higher = new Ipv4Address("192.168.200.200");
final Ipv4Address lower = new Ipv4Address("10.10.10.10");
final IpAddress remoteIp = new IpAddress(lower);
this.peerRegistry.addPeer(remoteIp, this.peer1, this.mockPreferences);
this.peerRegistry.getPeer(remoteIp, higher, lower, createOpen(lower, LOCAL_AS));
try {
this.peerRegistry.getPeer(remoteIp, lower, higher, createOpen(higher, LOCAL_AS));
} catch (final BGPDocumentedException e) {
assertEquals(BGPError.CEASE, e.getError());
return;
}
fail("Same peer cannot be connected twice");
}
use of org.opendaylight.protocol.bgp.parser.BGPDocumentedException in project bgpcep by opendaylight.
the class BGPSessionImpl method handleException.
/**
* Handle exception occurred in the BGP session. The session in error state should be closed
* properly so that it can be restored later.
*/
@VisibleForTesting
void handleException(final Throwable cause) {
LOG.warn("BGP session encountered error", cause);
final Throwable docCause = cause.getCause();
if (docCause instanceof BGPDocumentedException) {
this.terminate((BGPDocumentedException) docCause);
} else {
this.terminate(new BGPDocumentedException(BGPError.CEASE));
}
}
use of org.opendaylight.protocol.bgp.parser.BGPDocumentedException in project bgpcep by opendaylight.
the class BGPSessionImpl method handleHoldTimer.
/**
* If HoldTimer expires, the session ends. If a message (whichever) was received during this period, the HoldTimer
* will be rescheduled by HOLD_TIMER_VALUE + the time that has passed from the start of the HoldTimer to the time at
* which the message was received. If the session was closed by the time this method starts to execute (the session
* state will become IDLE), then rescheduling won't occur.
*/
private synchronized void handleHoldTimer() {
if (this.state == State.IDLE) {
return;
}
final long ct = System.nanoTime();
final long nextHold = this.lastMessageReceivedAt + TimeUnit.SECONDS.toNanos(this.holdTimerValue);
if (ct >= nextHold) {
LOG.debug("HoldTimer expired. {}", new Date());
this.terminate(new BGPDocumentedException(BGPError.HOLD_TIMER_EXPIRED));
} else {
this.channel.eventLoop().schedule(this::handleHoldTimer, nextHold - ct, TimeUnit.NANOSECONDS);
}
}
Aggregations