Bump versions to 0.21.7-SNAPSHOT
[bgpcep.git] / bgp / extensions / mvpn / src / main / java / org / opendaylight / protocol / bgp / mvpn / impl / nlri / IntraAsIPmsiADHandler.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 static com.google.common.base.Preconditions.checkArgument;
12
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
15 import org.opendaylight.bgp.concepts.IpAddressUtil;
16 import org.opendaylight.bgp.concepts.RouteDistinguisherUtil;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.mvpn.rev200120.NlriType;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.mvpn.rev200120.intra.as.i.pmsi.a.d.grouping.IntraAsIPmsiAD;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.mvpn.rev200120.intra.as.i.pmsi.a.d.grouping.IntraAsIPmsiADBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.mvpn.rev200120.mvpn.mvpn.choice.IntraAsIPmsiADCase;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.mvpn.rev200120.mvpn.mvpn.choice.IntraAsIPmsiADCaseBuilder;
22
23 /**
24  * https://tools.ietf.org/html/rfc6514#section-4.1.
25  *
26  * @author Claudio D. Gasparini
27  */
28 public final class IntraAsIPmsiADHandler extends AbstractMvpnNlri<IntraAsIPmsiADCase> {
29     private static final int IPV4_CONTENT_LENGTH = 12;
30     private static final int IPV6_CONTENT_LENGTH = 24;
31
32     public IntraAsIPmsiADHandler() {
33         super(IntraAsIPmsiADCase.class, NlriType.IntraAsIPmsiAD);
34     }
35
36     @Override
37     public IntraAsIPmsiADCase parseMvpn(final ByteBuf buffer) {
38         checkArgument(buffer.readableBytes() == IPV4_CONTENT_LENGTH || buffer.readableBytes() == IPV6_CONTENT_LENGTH,
39             "Wrong length of array of bytes. Passed: %s ;", buffer);
40         return new IntraAsIPmsiADCaseBuilder()
41             .setIntraAsIPmsiAD(new IntraAsIPmsiADBuilder()
42                 .setRouteDistinguisher(RouteDistinguisherUtil.parseRouteDistinguisher(buffer))
43                 .setOrigRouteIp(IpAddressUtil.addressForByteBufWOLength(buffer))
44                 .build())
45             .build();
46     }
47
48     @Override
49     protected ByteBuf serializeBody(final IntraAsIPmsiADCase mvpn) {
50         final IntraAsIPmsiAD route = mvpn.getIntraAsIPmsiAD();
51         final ByteBuf nlriByteBuf = Unpooled.buffer();
52         RouteDistinguisherUtil.serializeRouteDistinquisher(route.getRouteDistinguisher(), nlriByteBuf);
53         serializeAddress(route.getOrigRouteIp(), nlriByteBuf);
54         return nlriByteBuf;
55     }
56 }