Replace Preconditions.CheckNotNull per RequireNonNull
[bgpcep.git] / bgp / evpn / src / main / java / org / opendaylight / protocol / bgp / evpn / impl / nlri / EthSegRParser.java
1 /*
2  * Copyright (c) 2016 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
9 package org.opendaylight.protocol.bgp.evpn.impl.nlri;
10
11 import static java.util.Objects.requireNonNull;
12 import static org.opendaylight.protocol.bgp.evpn.impl.nlri.NlriModelUtil.extractOrigRouteIp;
13
14 import com.google.common.base.Preconditions;
15 import io.netty.buffer.ByteBuf;
16 import io.netty.buffer.Unpooled;
17 import org.opendaylight.protocol.bgp.evpn.spi.pojo.SimpleEsiTypeRegistry;
18 import org.opendaylight.protocol.util.Ipv4Util;
19 import org.opendaylight.protocol.util.Ipv6Util;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev160321.NlriType;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev160321.es.route.EsRoute;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev160321.es.route.EsRouteBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev160321.esi.Esi;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev160321.evpn.EvpnChoice;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev160321.evpn.evpn.choice.EsRouteCase;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev160321.evpn.evpn.choice.EsRouteCaseBuilder;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
29 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
30
31 final class EthSegRParser extends AbstractEvpnNlri {
32     protected static final NodeIdentifier ES_ROUTE_NID = new NodeIdentifier(EsRoute.QNAME);
33     private static final int CONTENT_LENGTH = 15;
34     private static final int CONTENT_LENGTH2 = 27;
35
36     @Override
37     public EvpnChoice parseEvpn(final ByteBuf buffer) {
38         Preconditions.checkArgument(buffer.readableBytes() == CONTENT_LENGTH || buffer.readableBytes() == CONTENT_LENGTH2,
39             "Wrong length of array of bytes. Passed: %s ;", buffer);
40
41         final Esi esi = SimpleEsiTypeRegistry.getInstance().parseEsi(buffer.readSlice(ESI_SIZE));
42         final IpAddress ip = requireNonNull(parseOrigRouteIp(buffer));
43
44         final EsRouteBuilder builder = new EsRouteBuilder().setEsi(esi).setOrigRouteIp(ip);
45         return new EsRouteCaseBuilder().setEsRoute(builder.build()).build();
46     }
47
48     @Override
49     protected NlriType getType() {
50         return NlriType.EthSeg;
51     }
52
53     @Override
54     public ByteBuf serializeBody(final EvpnChoice evpnInput) {
55         Preconditions.checkArgument(evpnInput instanceof EsRouteCase, "Unknown evpn instance. Passed %s. Needed EsRouteCase.",
56             evpnInput.getClass());
57         return serializeBody(((EsRouteCase) evpnInput).getEsRoute());
58     }
59
60     @Override
61     public EvpnChoice serializeEvpnModel(final ContainerNode evpnCase) {
62         return createRouteKey(evpnCase);
63     }
64
65     @Override
66     public EvpnChoice createRouteKey(final ContainerNode evpn) {
67         final EsRouteBuilder builder = new EsRouteBuilder();
68         builder.setEsi(serializeEsi(evpn));
69         builder.setOrigRouteIp(extractOrigRouteIp(evpn));
70         return new EsRouteCaseBuilder().setEsRoute(builder.build()).build();
71     }
72
73     private static ByteBuf serializeBody(final EsRoute evpn) {
74         final ByteBuf body = Unpooled.buffer();
75         SimpleEsiTypeRegistry.getInstance().serializeEsi(evpn.getEsi(), body);
76         final ByteBuf orig = serializeOrigRouteIp(evpn.getOrigRouteIp());
77         Preconditions.checkArgument(orig.readableBytes() > 0);
78         body.writeBytes(orig);
79         return body;
80     }
81
82     public static IpAddress parseOrigRouteIp(final ByteBuf buffer) {
83         final int ipLength = buffer.readUnsignedByte();
84         if (ipLength == Ipv6Util.IPV6_BITS_LENGTH) {
85             return new IpAddress(Ipv6Util.addressForByteBuf(buffer));
86         } else if (ipLength == Ipv4Util.IP4_BITS_LENGTH) {
87             return new IpAddress(Ipv4Util.addressForByteBuf(buffer));
88         }
89         return null;
90     }
91
92     static ByteBuf serializeOrigRouteIp(final IpAddress origRouteIp) {
93         final ByteBuf body = Unpooled.buffer();
94         if (origRouteIp.getIpv4Address() != null) {
95             body.writeByte(Ipv4Util.IP4_BITS_LENGTH);
96             body.writeBytes(Ipv4Util.bytesForAddress(origRouteIp.getIpv4Address()));
97         } else if (origRouteIp.getIpv6Address() != null) {
98             body.writeByte(Ipv6Util.IPV6_BITS_LENGTH);
99             body.writeBytes(Ipv6Util.bytesForAddress(origRouteIp.getIpv6Address()));
100         } else {
101             body.writeZero(ZERO_BYTE);
102         }
103         return body;
104     }
105 }