2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.protocol.bgp.linkstate.impl.attribute.sr.binding.sid.sub.tlvs;
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.Ipv4Util;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
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.Ipv4EroCase;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.segment.routing.ext.rev151014.binding.sub.tlvs.binding.sub.tlv.Ipv4EroCaseBuilder;
25 public final class Ipv4EroParser implements BindingSubTlvsParser, BindingSubTlvsSerializer {
26 private static final int ERO_IPV4 = 1163;
27 static final int FLAGS_SIZE = 8;
28 static final int LOOSE = 0;
29 static final int RESERVED_ERO = 3;
32 public BindingSubTlv parseSubTlv(final ByteBuf slice, final ProtocolId protocolId) {
33 final Ipv4EroCase ipv4Ero = parseIpv4EroCase(slice);
34 return new Ipv4EroCaseBuilder().setAddress(ipv4Ero.getAddress()).setLoose(ipv4Ero.isLoose()).build();
38 public int getType() {
43 public void serializeSubTlv(final BindingSubTlv bindingSubTlv, final ByteBuf aggregator) {
44 Preconditions.checkArgument(bindingSubTlv instanceof Ipv4EroCase, "Wrong BindingSubTlv instance expected", bindingSubTlv);
45 final Ipv4EroCase ipv4Ero = (Ipv4EroCase) bindingSubTlv;
46 TlvUtil.writeTLV(getType(), serializeIpv4EroCase(ipv4Ero.isLoose(), ipv4Ero.getAddress()), aggregator);
49 static Ipv4EroCase parseIpv4EroCase(final ByteBuf buffer) {
50 final Ipv4EroCaseBuilder builder = new Ipv4EroCaseBuilder();
51 final BitArray flags = BitArray.valueOf(buffer, FLAGS_SIZE);
52 builder.setLoose(flags.get(LOOSE));
53 buffer.skipBytes(RESERVED_ERO);
54 builder.setAddress(Ipv4Util.addressForByteBuf(buffer));
55 return builder.build();
59 static ByteBuf serializeIpv4EroCase(final Boolean loose, final Ipv4Address address) {
60 final ByteBuf buffer = Unpooled.buffer();
61 serializeEroFlags(buffer, loose);
62 buffer.writeBytes(Ipv4Util.byteBufForAddress(address));
66 static void serializeEroFlags(final ByteBuf buffer, final Boolean loose) {
67 final BitArray flags = new BitArray(FLAGS_SIZE);
68 flags.set(LOOSE, loose);
69 flags.toByteBuf(buffer);
70 buffer.writeZero(RESERVED_ERO);