Search in sources :

Example 6 with Notification

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());
}
Also used : NotifyBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.NotifyBuilder) Notify(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.Notify) ByteBuf(io.netty.buffer.ByteBuf) Notification(org.opendaylight.yangtools.yang.binding.Notification) Test(org.junit.Test)

Example 7 with Notification

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));
}
Also used : PeerSpecificParserConstraint(org.opendaylight.protocol.bgp.parser.spi.PeerSpecificParserConstraint) MessageRegistry(org.opendaylight.protocol.bgp.parser.spi.MessageRegistry) ByteBuf(io.netty.buffer.ByteBuf) Notification(org.opendaylight.yangtools.yang.binding.Notification) Test(org.junit.Test)

Example 8 with Notification

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();
    }
}
Also used : BmpSessionListenerFactory(org.opendaylight.protocol.bmp.api.BmpSessionListenerFactory) ChannelFuture(io.netty.channel.ChannelFuture) InetSocketAddress(java.net.InetSocketAddress) Channel(io.netty.channel.Channel) BmpSession(org.opendaylight.protocol.bmp.api.BmpSession) Notification(org.opendaylight.yangtools.yang.binding.Notification) Test(org.junit.Test)

Example 9 with Notification

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);
        }
    }
}
Also used : PeerDownNotification(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev171207.PeerDownNotification) Preconditions(com.google.common.base.Preconditions) PeerDownNotification(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev171207.PeerDownNotification) Notification(org.opendaylight.yangtools.yang.binding.Notification) FsmEventCode(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev171207.peer.down.data.FsmEventCode)

Example 10 with Notification

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();
}
Also used : ReceivedOpenBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev171207.peer.up.ReceivedOpenBuilder) StringInformationBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev171207.string.informations.StringInformationBuilder) InformationBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev171207.peer.up.InformationBuilder) OpenMessage(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.OpenMessage) BGPDocumentedException(org.opendaylight.protocol.bgp.parser.BGPDocumentedException) SentOpenBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev171207.peer.up.SentOpenBuilder) IpAddress(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress) BGPParsingException(org.opendaylight.protocol.bgp.parser.BGPParsingException) BmpDeserializationException(org.opendaylight.protocol.bmp.spi.parser.BmpDeserializationException) PortNumber(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber) PeerUpNotification(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev171207.PeerUpNotification) Notification(org.opendaylight.yangtools.yang.binding.Notification) PeerUpNotificationBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev171207.PeerUpNotificationBuilder)

Aggregations

Notification (org.opendaylight.yangtools.yang.binding.Notification)35 Test (org.junit.Test)18 ByteBuf (io.netty.buffer.ByteBuf)10 OpenBuilder (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.OpenBuilder)8 ProtocolVersion (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.ProtocolVersion)7 ChannelFuture (io.netty.channel.ChannelFuture)6 InetSocketAddress (java.net.InetSocketAddress)6 Ipv4Address (org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address)6 Open (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.Open)6 Before (org.junit.Before)5 BGPDocumentedException (org.opendaylight.protocol.bgp.parser.BGPDocumentedException)5 Notify (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.Notify)5 BgpParameters (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.open.message.BgpParameters)5 ChannelHandler (io.netty.channel.ChannelHandler)4 TimeUnit (java.util.concurrent.TimeUnit)4 BGPParsingException (org.opendaylight.protocol.bgp.parser.BGPParsingException)4 BgpParametersBuilder (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.open.message.BgpParametersBuilder)4 OptionalCapabilities (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.open.message.bgp.parameters.OptionalCapabilities)4 PCEPErrors (org.opendaylight.protocol.pcep.spi.PCEPErrors)3 OptionalCapabilitiesBuilder (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.open.message.bgp.parameters.OptionalCapabilitiesBuilder)3