2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.protocol.bmp.parser.message;
11 import static java.util.Objects.requireNonNull;
13 import com.google.common.base.Preconditions;
14 import com.google.common.collect.ImmutableList;
15 import io.netty.buffer.ByteBuf;
16 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
17 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
18 import org.opendaylight.protocol.bgp.parser.spi.MessageRegistry;
19 import org.opendaylight.protocol.bgp.parser.spi.MessageUtil;
20 import org.opendaylight.protocol.bmp.spi.parser.AbstractBmpPerPeerMessageParser;
21 import org.opendaylight.protocol.bmp.spi.parser.BmpDeserializationException;
22 import org.opendaylight.protocol.bmp.spi.parser.BmpTlvRegistry;
23 import org.opendaylight.protocol.util.ByteBufWriteUtil;
24 import org.opendaylight.protocol.util.Ipv4Util;
25 import org.opendaylight.protocol.util.Ipv6Util;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.OpenBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.OpenMessage;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev180329.PeerUpNotification;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev180329.PeerUpNotificationBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev180329.Tlv;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev180329.peer.up.Information;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev180329.peer.up.InformationBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev180329.peer.up.ReceivedOpenBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev180329.peer.up.SentOpenBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev180329.string.informations.StringInformation;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev180329.string.informations.StringInformationBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev180329.string.tlv.StringTlv;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev180329.string.tlv.StringTlvBuilder;
41 import org.opendaylight.yangtools.yang.binding.Notification;
43 public class PeerUpHandler extends AbstractBmpPerPeerMessageParser<InformationBuilder> {
45 private static final int MESSAGE_TYPE = 3;
46 private final MessageRegistry msgRegistry;
48 public PeerUpHandler(final MessageRegistry bgpMssageRegistry, final BmpTlvRegistry tlvRegistry) {
49 super(bgpMssageRegistry, tlvRegistry);
50 this.msgRegistry = getBgpMessageRegistry();
54 public void serializeMessageBody(final Notification message, final ByteBuf buffer) {
55 super.serializeMessageBody(message, buffer);
56 Preconditions.checkArgument(message instanceof PeerUpNotification,
57 "An instance of Peer Up notification is required");
58 final PeerUpNotification peerUp = (PeerUpNotification) message;
60 if (peerUp.getLocalAddress().getIpv4Address() != null) {
61 buffer.writeZero(Ipv6Util.IPV6_LENGTH - Ipv4Util.IP4_LENGTH);
62 ByteBufWriteUtil.writeIpv4Address(peerUp.getLocalAddress().getIpv4Address(), buffer);
64 ByteBufWriteUtil.writeIpv6Address(peerUp.getLocalAddress().getIpv6Address(), buffer);
66 ByteBufWriteUtil.writeUnsignedShort(peerUp.getLocalPort().getValue(), buffer);
67 ByteBufWriteUtil.writeUnsignedShort(peerUp.getRemotePort().getValue(), buffer);
69 this.msgRegistry.serializeMessage(new OpenBuilder(peerUp.getSentOpen()).build(), buffer);
70 this.msgRegistry.serializeMessage(new OpenBuilder(peerUp.getReceivedOpen()).build(), buffer);
71 serializeTlvs(peerUp.getInformation(), buffer);
74 private void serializeTlvs(final Information tlvs, final ByteBuf output) {
75 if (tlvs != null && tlvs.getStringInformation() != null) {
76 for (final StringInformation stringInfo : tlvs.getStringInformation()) {
77 if (stringInfo.getStringTlv() != null) {
78 serializeTlv(stringInfo.getStringTlv(), output);
85 public Notification parseMessageBody(final ByteBuf bytes) throws BmpDeserializationException {
86 final PeerUpNotificationBuilder peerUpNot = new PeerUpNotificationBuilder()
87 .setPeerHeader(parsePerPeerHeader(bytes));
89 if (peerUpNot.getPeerHeader().isIpv4()) {
90 bytes.skipBytes(Ipv6Util.IPV6_LENGTH - Ipv4Util.IP4_LENGTH);
91 peerUpNot.setLocalAddress(new IpAddress(Ipv4Util.addressForByteBuf(bytes)));
93 peerUpNot.setLocalAddress(new IpAddress(Ipv6Util.addressForByteBuf(bytes)));
95 peerUpNot.setLocalPort(new PortNumber(bytes.readUnsignedShort()));
96 peerUpNot.setRemotePort(new PortNumber(bytes.readUnsignedShort()));
98 final Notification opSent = this.msgRegistry
99 .parseMessage(bytes.readSlice(getBgpMessageLength(bytes)), null);
100 requireNonNull(opSent,
101 "Error on parse Sent OPEN Message, Sent OPEN Message is null");
102 Preconditions.checkArgument(opSent instanceof OpenMessage,
103 "An instance of OpenMessage notification is required");
104 final OpenMessage sent = (OpenMessage) opSent;
106 final Notification opRec = this.msgRegistry
107 .parseMessage(bytes.readSlice(getBgpMessageLength(bytes)), null);
108 requireNonNull(opRec,
109 "Error on parse Received OPEN Message, Received OPEN Message is null");
110 Preconditions.checkArgument(opRec instanceof OpenMessage,
111 "An instance of OpenMessage notification is required");
112 final OpenMessage received = (OpenMessage) opRec;
114 peerUpNot.setSentOpen(new SentOpenBuilder(sent).build());
115 peerUpNot.setReceivedOpen(new ReceivedOpenBuilder(received).build());
117 final InformationBuilder infos = new InformationBuilder();
118 if (bytes.isReadable()) {
119 parseTlvs(infos, bytes);
120 peerUpNot.setInformation(infos.build());
123 } catch (final BGPDocumentedException | BGPParsingException e) {
124 throw new BmpDeserializationException("Error while parsing BGP Open Message.", e);
127 return peerUpNot.build();
131 protected void addTlv(final InformationBuilder builder, final Tlv tlv) {
132 if (tlv instanceof StringTlv) {
133 final ImmutableList.Builder<StringInformation> stringInfoListBuilder = ImmutableList.builder();
134 if (builder.getStringInformation() != null) {
135 stringInfoListBuilder.addAll(builder.getStringInformation());
137 builder.setStringInformation(stringInfoListBuilder.add(new StringInformationBuilder().setStringTlv(
138 new StringTlvBuilder((StringTlv) tlv).build()).build()).build());
143 public int getBmpMessageType() {
147 private static int getBgpMessageLength(final ByteBuf buffer) {
148 return buffer.getUnsignedShort(buffer.readerIndex() + MessageUtil.MARKER_LENGTH);