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