Improve mvpn parsing and serialization
[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 package org.opendaylight.protocol.bgp.mvpn.impl.nlri;
9
10 import static java.util.Objects.requireNonNull;
11
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.Unpooled;
14 import org.eclipse.jdt.annotation.NonNull;
15 import org.opendaylight.bgp.concepts.IpAddressUtil;
16 import org.opendaylight.bgp.concepts.RouteDistinguisherUtil;
17 import org.opendaylight.protocol.bgp.mvpn.spi.nlri.MvpnParser;
18 import org.opendaylight.protocol.bgp.mvpn.spi.nlri.MvpnSerializer;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.mvpn.rev200120.MulticastSourceRdGrouping;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.mvpn.rev200120.NlriType;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.mvpn.rev200120.mvpn.MvpnChoice;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.mvpn.rev200120.s.pmsi.a.d.grouping.SPmsiADBuilder;
23
24 /**
25  * Abstract Mvpn Nlri.
26  *
27  * @author Claudio D. Gasparini
28  */
29 public abstract class AbstractMvpnNlri<T extends MvpnChoice> implements MvpnSerializer<T>, MvpnParser<T> {
30     private final @NonNull Class<T> choice;
31     private final @NonNull NlriType type;
32     private final int intType;
33
34     protected AbstractMvpnNlri(final Class<T> choice, final NlriType type) {
35         this.choice = requireNonNull(choice);
36         this.type = requireNonNull(type);
37         intType = type.getIntValue();
38     }
39
40     @Override
41     public final Class<T> getClazz() {
42         return choice;
43     }
44
45     @Override
46     public final NlriType getType() {
47         return type;
48     }
49
50     @Override
51     public final ByteBuf serializeMvpn(final T mvpn) {
52         final ByteBuf output = Unpooled.buffer();
53         final ByteBuf body = serializeBody(mvpn);
54         output.writeByte(intType);
55         output.writeByte(body.readableBytes());
56         output.writeBytes(body);
57         return output;
58     }
59
60     protected abstract ByteBuf serializeBody(T mvpn);
61
62     static final MulticastSourceRdGrouping parseRDMulticastSource(final ByteBuf buffer) {
63         return new SPmsiADBuilder()
64             .setRouteDistinguisher(RouteDistinguisherUtil.parseRouteDistinguisher(buffer))
65             .setMulticastSource(IpAddressUtil.addressForByteBuf(buffer))
66             .build();
67     }
68
69     static final void serializeRDMulticastSource(final MulticastSourceRdGrouping route, final ByteBuf output) {
70         RouteDistinguisherUtil.serializeRouteDistinquisher(route.getRouteDistinguisher(), output);
71         output.writeBytes(IpAddressUtil.bytesFor(route.getMulticastSource()));
72     }
73 }