use of org.opendaylight.yangtools.yang.binding.Notification in project bgpcep by opendaylight.
the class ParserTest method testNotificationMsg.
@Test
public void testNotificationMsg() throws BGPParsingException, BGPDocumentedException {
Notification notMsg = new NotifyBuilder().setErrorCode(BGPError.OPT_PARAM_NOT_SUPPORTED.getCode()).setErrorSubcode(BGPError.OPT_PARAM_NOT_SUPPORTED.getSubcode()).setData(new byte[] { 4, 9 }).build();
final ByteBuf bytes = Unpooled.buffer();
ParserTest.reg.serializeMessage(notMsg, bytes);
assertArrayEquals(notificationBMsg, ByteArray.subByte(bytes.array(), 0, bytes.writerIndex()));
Notification m = ParserTest.reg.parseMessage(Unpooled.copiedBuffer(bytes), null);
assertTrue(m instanceof Notify);
assertEquals(BGPError.OPT_PARAM_NOT_SUPPORTED, BGPError.forValue(((Notify) m).getErrorCode(), ((Notify) m).getErrorSubcode()));
assertArrayEquals(new byte[] { 4, 9 }, ((Notify) m).getData());
notMsg = new NotifyBuilder().setErrorCode(BGPError.CONNECTION_NOT_SYNC.getCode()).setErrorSubcode(BGPError.CONNECTION_NOT_SYNC.getSubcode()).build();
bytes.clear();
ParserTest.reg.serializeMessage(notMsg, bytes);
m = ParserTest.reg.parseMessage(Unpooled.copiedBuffer(bytes), null);
assertTrue(m instanceof Notify);
assertEquals(BGPError.CONNECTION_NOT_SYNC, BGPError.forValue(((Notify) m).getErrorCode(), ((Notify) m).getErrorSubcode()));
assertNull(((Notify) m).getData());
}
use of org.opendaylight.yangtools.yang.binding.Notification in project bgpcep by opendaylight.
the class SimpleRegistryTest method testSimpleMessageRegistry.
@Test
public void testSimpleMessageRegistry() throws Exception {
final MessageRegistry msgRegistry = this.ctx.getMessageRegistry();
final byte[] msgBytes = { (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0x00, (byte) 0x13, (byte) 0x00 };
final Notification msg = mock(Notification.class);
doReturn(Notification.class).when(msg).getImplementedInterface();
final ByteBuf buffer = Unpooled.buffer(msgBytes.length);
msgRegistry.serializeMessage(msg, buffer);
msgRegistry.parseMessage(Unpooled.wrappedBuffer(msgBytes), CONSTRAINT);
verify(this.activator.msgParser, times(1)).parseMessageBody(Mockito.any(ByteBuf.class), Mockito.anyInt(), Mockito.any(PeerSpecificParserConstraint.class));
verify(this.activator.msgSerializer, times(1)).serializeMessage(Mockito.any(Notification.class), Mockito.any(ByteBuf.class));
}
use of org.opendaylight.yangtools.yang.binding.Notification in project bgpcep by opendaylight.
the class BmpMockTest method testMainInPassiveMode.
@Test
public void testMainInPassiveMode() throws Exception {
final InetSocketAddress serverAddr = InetSocketAddressUtil.getRandomLoopbackInetSocketAddress();
final BmpSessionListenerFactory bmpSessionListenerFactory = () -> BmpMockTest.this.sessionListener;
// create a local server in passive mode instead
BmpMock.main(new String[] { "--local_address", InetSocketAddressUtil.toHostAndPort(serverAddr).toString(), "--peers_count", "3", "--pre_policy_routes", "3", "--passive" });
final ChannelFuture futureServer = this.bmpDispatcher.createClient(serverAddr, bmpSessionListenerFactory, KeyMapping.getKeyMapping());
waitFutureSuccess(futureServer);
final Channel serverChannel;
final int sessionUpWait;
if (futureServer.isSuccess()) {
serverChannel = futureServer.channel();
sessionUpWait = 10;
} else {
serverChannel = null;
// wait longer for the reconnection attempt
sessionUpWait = 40;
}
verify(this.sessionListener, Mockito.timeout(TimeUnit.SECONDS.toMillis(sessionUpWait))).onSessionUp(Mockito.any(BmpSession.class));
// 1 * Initiate message + 3 * PeerUp Notification + 9 * Route Monitoring message
verify(this.sessionListener, Mockito.timeout(TimeUnit.SECONDS.toMillis(10)).times(13)).onMessage(Mockito.any(Notification.class));
if (serverChannel != null) {
serverChannel.close().sync();
}
}
use of org.opendaylight.yangtools.yang.binding.Notification in project bgpcep by opendaylight.
the class PeerDownHandler method serializeMessageBody.
@Override
public void serializeMessageBody(final Notification message, final ByteBuf buffer) {
super.serializeMessageBody(message, buffer);
Preconditions.checkArgument(message instanceof PeerDownNotification, "An instance of PeerDownNotification is required");
final PeerDownNotification peerDown = (PeerDownNotification) message;
if (peerDown.isLocalSystemClosed()) {
if (peerDown.getData() instanceof FsmEventCode) {
ByteBufWriteUtil.writeUnsignedByte(REASON_TWO.getValue(), buffer);
ByteBufWriteUtil.writeUnsignedShort(((FsmEventCode) peerDown.getData()).getFsmEventCode(), buffer);
} else if (peerDown.getData() instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev171207.peer.down.data.Notification) {
ByteBufWriteUtil.writeUnsignedByte(REASON_ONE.getValue(), buffer);
serializePDU(peerDown.getData(), buffer);
}
} else {
if (peerDown.getData() instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev171207.peer.down.data.Notification) {
ByteBufWriteUtil.writeUnsignedByte(REASON_THREE.getValue(), buffer);
serializePDU(peerDown.getData(), buffer);
} else {
ByteBufWriteUtil.writeUnsignedByte(REASON_FOUR.getValue(), buffer);
}
}
}
use of org.opendaylight.yangtools.yang.binding.Notification in project bgpcep by opendaylight.
the class PeerUpHandler method parseMessageBody.
@Override
public Notification parseMessageBody(final ByteBuf bytes) throws BmpDeserializationException {
final PeerUpNotificationBuilder peerUpNot = new PeerUpNotificationBuilder().setPeerHeader(parsePerPeerHeader(bytes));
if (peerUpNot.getPeerHeader().isIpv4()) {
bytes.skipBytes(Ipv6Util.IPV6_LENGTH - Ipv4Util.IP4_LENGTH);
peerUpNot.setLocalAddress(new IpAddress(Ipv4Util.addressForByteBuf(bytes)));
} else {
peerUpNot.setLocalAddress(new IpAddress(Ipv6Util.addressForByteBuf(bytes)));
}
peerUpNot.setLocalPort(new PortNumber(bytes.readUnsignedShort()));
peerUpNot.setRemotePort(new PortNumber(bytes.readUnsignedShort()));
try {
final Notification opSent = this.msgRegistry.parseMessage(bytes.readSlice(getBgpMessageLength(bytes)), null);
requireNonNull(opSent, "Error on parse Sent OPEN Message, Sent OPEN Message is null");
Preconditions.checkArgument(opSent instanceof OpenMessage, "An instance of OpenMessage notification is required");
final OpenMessage sent = (OpenMessage) opSent;
final Notification opRec = this.msgRegistry.parseMessage(bytes.readSlice(getBgpMessageLength(bytes)), null);
requireNonNull(opRec, "Error on parse Received OPEN Message, Received OPEN Message is null");
Preconditions.checkArgument(opRec instanceof OpenMessage, "An instance of OpenMessage notification is required");
final OpenMessage received = (OpenMessage) opRec;
peerUpNot.setSentOpen(new SentOpenBuilder(sent).build());
peerUpNot.setReceivedOpen(new ReceivedOpenBuilder(received).build());
final InformationBuilder infos = new InformationBuilder();
if (bytes.isReadable()) {
parseTlvs(infos, bytes);
peerUpNot.setInformation(infos.build());
}
} catch (final BGPDocumentedException | BGPParsingException e) {
throw new BmpDeserializationException("Error while parsing BGP Open Message.", e);
}
return peerUpNot.build();
}
Aggregations