5fa2dc9bba70517cafa29e28b6bb2f5ebb2c6b2e
[bgpcep.git] / bgp / extensions / linkstate / src / main / java / org / opendaylight / protocol / bgp / linkstate / impl / attribute / sr / binding / sid / sub / tlvs / Ipv6EroParser.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.linkstate.impl.attribute.sr.binding.sid.sub.tlvs;
10
11 import com.google.common.base.Preconditions;
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.Unpooled;
14 import org.opendaylight.protocol.bgp.linkstate.spi.BindingSubTlvsParser;
15 import org.opendaylight.protocol.bgp.linkstate.spi.BindingSubTlvsSerializer;
16 import org.opendaylight.protocol.bgp.linkstate.spi.TlvUtil;
17 import org.opendaylight.protocol.util.BitArray;
18 import org.opendaylight.protocol.util.Ipv6Util;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Address;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.ProtocolId;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.segment.routing.ext.rev151014.binding.sub.tlvs.BindingSubTlv;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.segment.routing.ext.rev151014.binding.sub.tlvs.binding.sub.tlv.Ipv6EroCase;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.segment.routing.ext.rev151014.binding.sub.tlvs.binding.sub.tlv.Ipv6EroCaseBuilder;
24
25 public final class Ipv6EroParser implements BindingSubTlvsParser, BindingSubTlvsSerializer {
26     private static final int ERO_IPV6 = 1164;
27
28     @Override
29     public BindingSubTlv parseSubTlv(final ByteBuf slice, final ProtocolId protocolId) {
30         final Ipv6EroCase ipv6ero = parseIpv6EroCase(slice);
31         return new Ipv6EroCaseBuilder().setAddress(ipv6ero.getAddress()).setLoose(ipv6ero.isLoose()).build();
32     }
33
34     @Override
35     public int getType() {
36         return ERO_IPV6;
37     }
38
39     @Override
40     public void serializeSubTlv(final BindingSubTlv bindingSubTlv, final ByteBuf aggregator) {
41         Preconditions.checkArgument(bindingSubTlv instanceof Ipv6EroCase, "Wrong BindingSubTlv instance expected", bindingSubTlv);
42         final Ipv6EroCase ipv6Ero = (Ipv6EroCase) bindingSubTlv;
43         TlvUtil.writeTLV(getType(), serializeIpv6EroCase(ipv6Ero.isLoose(), ipv6Ero.getAddress()), aggregator);
44     }
45
46     public static Ipv6EroCase parseIpv6EroCase(final ByteBuf buffer) {
47         final Ipv6EroCaseBuilder builder = new Ipv6EroCaseBuilder();
48         final BitArray flags = BitArray.valueOf(buffer, Ipv4EroParser.FLAGS_SIZE);
49         builder.setLoose(flags.get(Ipv4EroParser.LOOSE));
50         buffer.skipBytes(Ipv4EroParser.RESERVED_ERO);
51         builder.setAddress(Ipv6Util.addressForByteBuf(buffer));
52         return builder.build();
53     }
54
55     static ByteBuf serializeIpv6EroCase(final Boolean loose, final Ipv6Address address) {
56         final ByteBuf buffer = Unpooled.buffer();
57         Ipv4EroParser.serializeEroFlags(buffer, loose);
58         buffer.writeBytes(Ipv6Util.byteBufForAddress(address));
59         return buffer;
60     }
61 }