YANG revision dates mass-update
[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 com.google.common.base.Preconditions;
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.Unpooled;
14 import org.opendaylight.bgp.concepts.IpAddressUtil;
15 import org.opendaylight.bgp.concepts.RouteDistinguisherUtil;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.mvpn.rev200120.NlriType;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.mvpn.rev200120.intra.as.i.pmsi.a.d.grouping.IntraAsIPmsiAD;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.mvpn.rev200120.intra.as.i.pmsi.a.d.grouping.IntraAsIPmsiADBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.mvpn.rev200120.mvpn.MvpnChoice;
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
30     private static final int IPV4_CONTENT_LENGTH = 12;
31     private static final int IPV6_CONTENT_LENGTH = 24;
32
33     @Override
34     public int getType() {
35         return NlriType.IntraAsIPmsiAD.getIntValue();
36     }
37
38     @Override
39     public IntraAsIPmsiADCase parseMvpn(final ByteBuf buffer) {
40         Preconditions.checkArgument(buffer.readableBytes() == IPV4_CONTENT_LENGTH
41                         || buffer.readableBytes() == IPV6_CONTENT_LENGTH,
42                 "Wrong length of array of bytes. Passed: %s ;", buffer);
43         return new IntraAsIPmsiADCaseBuilder()
44                 .setIntraAsIPmsiAD(new IntraAsIPmsiADBuilder()
45                         .setRouteDistinguisher(RouteDistinguisherUtil.parseRouteDistinguisher(buffer))
46                         .setOrigRouteIp(IpAddressUtil.addressForByteBufWOLength(buffer))
47                         .build())
48                 .build();
49     }
50
51     @Override
52     protected ByteBuf serializeBody(final IntraAsIPmsiADCase mvpn) {
53         final IntraAsIPmsiAD route = mvpn.getIntraAsIPmsiAD();
54         final ByteBuf nlriByteBuf = Unpooled.buffer();
55         RouteDistinguisherUtil.serializeRouteDistinquisher(route.getRouteDistinguisher(), nlriByteBuf);
56         final ByteBuf orig = IpAddressUtil.bytesWOLengthFor(route.getOrigRouteIp());
57         Preconditions.checkArgument(orig.readableBytes() > 0);
58         nlriByteBuf.writeBytes(orig);
59         return nlriByteBuf;
60     }
61
62     @Override
63     public Class<? extends MvpnChoice> getClazz() {
64         return IntraAsIPmsiADCase.class;
65     }
66 }