c8faf968aacf669329c332a7a22d8346b541d209
[bgpcep.git] / bgp / extensions / mvpn / src / main / java / org / opendaylight / protocol / bgp / mvpn / impl / nlri / AbstractMvpnNlri.java
1 /*
2  * Copyright (c) 2018 AT&T Intellectual Property. 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.bgp.mvpn.impl.nlri;
10
11 import io.netty.buffer.ByteBuf;
12 import io.netty.buffer.Unpooled;
13 import org.opendaylight.bgp.concepts.IpAddressUtil;
14 import org.opendaylight.bgp.concepts.RouteDistinguisherUtil;
15 import org.opendaylight.protocol.bgp.mvpn.spi.nlri.MvpnParser;
16 import org.opendaylight.protocol.bgp.mvpn.spi.nlri.MvpnSerializer;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddressNoZone;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.mvpn.rev180417.MulticastSourceRdGrouping;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.mvpn.rev180417.mvpn.MvpnChoice;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.mvpn.rev180417.s.pmsi.a.d.grouping.SPmsiADBuilder;
21
22 /**
23  * Abstract Mvpn Nlri.
24  *
25  * @author Claudio D. Gasparini
26  */
27 abstract class AbstractMvpnNlri<T extends MvpnChoice> implements MvpnSerializer<T>, MvpnParser<T> {
28     static MulticastSourceRdGrouping parseRDMulticastSource(final ByteBuf buffer) {
29         final SPmsiADBuilder builder = new SPmsiADBuilder();
30         builder.setRouteDistinguisher(RouteDistinguisherUtil.parseRouteDistinguisher(buffer));
31         final IpAddressNoZone address = IpAddressUtil.addressForByteBuf(buffer);
32         builder.setMulticastSource(address);
33         return builder.build();
34     }
35
36     static void serializeRDMulticastSource(final MulticastSourceRdGrouping route, final ByteBuf output) {
37         RouteDistinguisherUtil.serializeRouteDistinquisher(route.getRouteDistinguisher(), output);
38         output.writeBytes(IpAddressUtil.bytesFor(route.getMulticastSource()));
39     }
40
41     @Override
42     public final ByteBuf serializeMvpn(final T mvpn) {
43         final ByteBuf output = Unpooled.buffer();
44         final ByteBuf body = serializeBody(mvpn);
45         output.writeByte(getType());
46         output.writeByte(body.readableBytes());
47         output.writeBytes(body);
48         return output;
49     }
50
51     protected abstract ByteBuf serializeBody(T mvpn);
52 }