6e52246160d6a3d63fc4ecd7cf9c3c39584fcb37
[bgpcep.git] / bmp / bmp-parser-impl / src / main / java / org / opendaylight / protocol / bmp / parser / message / PeerUpHandler.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.protocol.bmp.parser.message;
10
11 import static java.util.Objects.requireNonNull;
12
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.rev171207.OpenBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.OpenMessage;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev171207.PeerUpNotification;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev171207.PeerUpNotificationBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev171207.Tlv;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev171207.peer.up.Information;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev171207.peer.up.InformationBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev171207.peer.up.ReceivedOpenBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev171207.peer.up.SentOpenBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev171207.string.informations.StringInformation;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev171207.string.informations.StringInformationBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev171207.string.tlv.StringTlv;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev171207.string.tlv.StringTlvBuilder;
41 import org.opendaylight.yangtools.yang.binding.Notification;
42
43 public class PeerUpHandler extends AbstractBmpPerPeerMessageParser<InformationBuilder> {
44
45     private static final int MESSAGE_TYPE = 3;
46     private final MessageRegistry msgRegistry;
47
48     public PeerUpHandler(final MessageRegistry bgpMssageRegistry, final BmpTlvRegistry tlvRegistry) {
49         super(bgpMssageRegistry, tlvRegistry);
50         this.msgRegistry = getBgpMessageRegistry();
51     }
52
53     @Override
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;
59
60         if (peerUp.getLocalAddress().getIpv4Address() != null) {
61             buffer.writeZero(Ipv6Util.IPV6_LENGTH - Ipv4Util.IP4_LENGTH);
62             ByteBufWriteUtil.writeIpv4Address(peerUp.getLocalAddress().getIpv4Address(), buffer);
63         } else {
64             ByteBufWriteUtil.writeIpv6Address(peerUp.getLocalAddress().getIpv6Address(), buffer);
65         }
66         ByteBufWriteUtil.writeUnsignedShort(peerUp.getLocalPort().getValue(), buffer);
67         ByteBufWriteUtil.writeUnsignedShort(peerUp.getRemotePort().getValue(), buffer);
68
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);
72     }
73
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);
79                 }
80             }
81         }
82     }
83
84     @Override
85     public Notification parseMessageBody(final ByteBuf bytes) throws BmpDeserializationException {
86         final PeerUpNotificationBuilder peerUpNot = new PeerUpNotificationBuilder()
87                 .setPeerHeader(parsePerPeerHeader(bytes));
88
89         if (peerUpNot.getPeerHeader().isIpv4()) {
90             bytes.skipBytes(Ipv6Util.IPV6_LENGTH - Ipv4Util.IP4_LENGTH);
91             peerUpNot.setLocalAddress(new IpAddress(Ipv4Util.addressForByteBuf(bytes)));
92         } else {
93             peerUpNot.setLocalAddress(new IpAddress(Ipv6Util.addressForByteBuf(bytes)));
94         }
95         peerUpNot.setLocalPort(new PortNumber(bytes.readUnsignedShort()));
96         peerUpNot.setRemotePort(new PortNumber(bytes.readUnsignedShort()));
97         try {
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;
105
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;
113
114             peerUpNot.setSentOpen(new SentOpenBuilder(sent).build());
115             peerUpNot.setReceivedOpen(new ReceivedOpenBuilder(received).build());
116
117             final InformationBuilder infos = new InformationBuilder();
118             if (bytes.isReadable()) {
119                 parseTlvs(infos, bytes);
120                 peerUpNot.setInformation(infos.build());
121             }
122
123         } catch (final BGPDocumentedException | BGPParsingException e) {
124             throw new BmpDeserializationException("Error while parsing BGP Open Message.", e);
125         }
126
127         return peerUpNot.build();
128     }
129
130     @Override
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());
136             }
137             builder.setStringInformation(stringInfoListBuilder.add(new StringInformationBuilder().setStringTlv(
138                     new StringTlvBuilder((StringTlv) tlv).build()).build()).build());
139         }
140     }
141
142     @Override
143     public int getBmpMessageType() {
144         return MESSAGE_TYPE;
145     }
146
147     private static int getBgpMessageLength(final ByteBuf buffer) {
148         return buffer.getUnsignedShort(buffer.readerIndex() + MessageUtil.MARKER_LENGTH);
149     }
150 }