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;
12 import static java.util.Objects.requireNonNull;
14 import com.google.common.base.Preconditions;
15 import com.google.common.collect.ImmutableList;
16 import io.netty.buffer.ByteBuf;
17 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
18 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
19 import org.opendaylight.protocol.bgp.parser.spi.MessageRegistry;
20 import org.opendaylight.protocol.bgp.parser.spi.MessageUtil;
21 import org.opendaylight.protocol.bmp.spi.parser.AbstractBmpPerPeerMessageParser;
22 import org.opendaylight.protocol.bmp.spi.parser.BmpDeserializationException;
23 import org.opendaylight.protocol.bmp.spi.parser.BmpTlvRegistry;
24 import org.opendaylight.protocol.util.ByteBufWriteUtil;
25 import org.opendaylight.protocol.util.Ipv4Util;
26 import org.opendaylight.protocol.util.Ipv6Util;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.OpenBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.OpenMessage;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev150512.PeerUpNotification;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev150512.PeerUpNotificationBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev150512.Tlv;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev150512.peer.up.Information;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev150512.peer.up.InformationBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev150512.peer.up.ReceivedOpenBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev150512.peer.up.SentOpenBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev150512.string.informations.StringInformation;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev150512.string.informations.StringInformationBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev150512.string.tlv.StringTlv;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev150512.string.tlv.StringTlvBuilder;
42 import org.opendaylight.yangtools.yang.binding.Notification;
44 public class PeerUpHandler extends AbstractBmpPerPeerMessageParser<InformationBuilder> {
46 private static final int MESSAGE_TYPE = 3;
47 private final MessageRegistry msgRegistry;
49 public PeerUpHandler(final MessageRegistry bgpMssageRegistry, final BmpTlvRegistry tlvRegistry) {
50 super(bgpMssageRegistry, tlvRegistry);
51 this.msgRegistry = getBgpMessageRegistry();
55 public void serializeMessageBody(final Notification message, final ByteBuf buffer) {
56 super.serializeMessageBody(message, buffer);
57 Preconditions.checkArgument(message instanceof PeerUpNotification,
58 "An instance of Peer Up notification is required");
59 final PeerUpNotification peerUp = (PeerUpNotification) message;
61 if (peerUp.getLocalAddress().getIpv4Address() != null) {
62 buffer.writeZero(Ipv6Util.IPV6_LENGTH - Ipv4Util.IP4_LENGTH);
63 ByteBufWriteUtil.writeIpv4Address(peerUp.getLocalAddress().getIpv4Address(), buffer);
65 ByteBufWriteUtil.writeIpv6Address(peerUp.getLocalAddress().getIpv6Address(), buffer);
67 ByteBufWriteUtil.writeUnsignedShort(peerUp.getLocalPort().getValue(), buffer);
68 ByteBufWriteUtil.writeUnsignedShort(peerUp.getRemotePort().getValue(), buffer);
70 this.msgRegistry.serializeMessage(new OpenBuilder(peerUp.getSentOpen()).build(), buffer);
71 this.msgRegistry.serializeMessage(new OpenBuilder(peerUp.getReceivedOpen()).build(), buffer);
72 serializeTlvs(peerUp.getInformation(), buffer);
75 private void serializeTlvs(final Information tlvs, final ByteBuf output) {
76 if (tlvs != null && tlvs.getStringInformation() != null) {
77 for (final StringInformation stringInfo : tlvs.getStringInformation()) {
78 if (stringInfo.getStringTlv() != null) {
79 serializeTlv(stringInfo.getStringTlv(), output);
86 public Notification parseMessageBody(final ByteBuf bytes) throws BmpDeserializationException {
87 final PeerUpNotificationBuilder peerUpNot = new PeerUpNotificationBuilder()
88 .setPeerHeader(parsePerPeerHeader(bytes));
90 if (peerUpNot.getPeerHeader().isIpv4()) {
91 bytes.skipBytes(Ipv6Util.IPV6_LENGTH - Ipv4Util.IP4_LENGTH);
92 peerUpNot.setLocalAddress(new IpAddress(Ipv4Util.addressForByteBuf(bytes)));
94 peerUpNot.setLocalAddress(new IpAddress(Ipv6Util.addressForByteBuf(bytes)));
96 peerUpNot.setLocalPort(new PortNumber(bytes.readUnsignedShort()));
97 peerUpNot.setRemotePort(new PortNumber(bytes.readUnsignedShort()));
99 final Notification opSent = this.msgRegistry
100 .parseMessage(bytes.readSlice(getBgpMessageLength(bytes)), null);
101 requireNonNull(opSent,
102 "Error on parse Sent OPEN Message, Sent OPEN Message is null");
103 Preconditions.checkArgument(opSent instanceof OpenMessage,
104 "An instance of OpenMessage notification is required");
105 final OpenMessage sent = (OpenMessage) opSent;
107 final Notification opRec = this.msgRegistry
108 .parseMessage(bytes.readSlice(getBgpMessageLength(bytes)), null);
109 requireNonNull(opRec,
110 "Error on parse Received OPEN Message, Received OPEN Message is null");
111 Preconditions.checkArgument(opRec instanceof OpenMessage,
112 "An instance of OpenMessage notification is required");
113 final OpenMessage received = (OpenMessage) opRec;
115 peerUpNot.setSentOpen(new SentOpenBuilder(sent).build());
116 peerUpNot.setReceivedOpen(new ReceivedOpenBuilder(received).build());
118 final InformationBuilder infos = new InformationBuilder();
119 if ( bytes.isReadable() ) {
120 parseTlvs(infos, bytes);
121 peerUpNot.setInformation(infos.build());
124 } catch (final BGPDocumentedException | BGPParsingException e) {
125 throw new BmpDeserializationException("Error while parsing BGP Open Message.", e);
128 return peerUpNot.build();
132 protected void addTlv(final InformationBuilder builder, final Tlv tlv) {
133 if (tlv instanceof StringTlv) {
134 final ImmutableList.Builder<StringInformation> stringInfoListBuilder = ImmutableList.builder();
135 if ( builder.getStringInformation() != null ) {
136 stringInfoListBuilder.addAll(builder.getStringInformation());
138 builder.setStringInformation(stringInfoListBuilder.add(new StringInformationBuilder().setStringTlv(
139 new StringTlvBuilder((StringTlv) tlv).build()).build()).build());
144 public int getBmpMessageType() {
148 private static int getBgpMessageLength(final ByteBuf buffer) {
149 return buffer.getUnsignedShort(buffer.readerIndex() + MessageUtil.MARKER_LENGTH);