2 * Copyright (c) 2015 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
8 package org.opendaylight.protocol.bgp.linkstate.attribute.sr;
10 import io.netty.buffer.ByteBuf;
11 import java.util.ArrayList;
12 import java.util.List;
13 import org.opendaylight.protocol.bgp.linkstate.spi.TlvUtil;
14 import org.opendaylight.protocol.util.BitArray;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.node.state.SrAlgorithm;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.node.state.SrAlgorithmBuilder;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.node.state.SrCapabilities;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.node.state.SrCapabilitiesBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.segment.routing.ext.rev151014.Algorithm;
21 public final class SrNodeAttributesParser {
23 private SrNodeAttributesParser() {
24 throw new UnsupportedOperationException();
27 private static final int FLAGS_SIZE = 8;
29 /* SR Capabilities flags */
30 private static final int MPLS_IPV4 = 0;
31 private static final int MPLS_IPV6 = 1;
32 private static final int SR_IPV6 = 2;
34 private static final int RESERVERED = 1;
36 public static SrCapabilities parseSrCapabilities(final ByteBuf buffer) {
37 final SrCapabilitiesBuilder builder = new SrCapabilitiesBuilder();
38 final BitArray flags = BitArray.valueOf(buffer, FLAGS_SIZE);
39 builder.setMplsIpv4(flags.get(MPLS_IPV4));
40 builder.setMplsIpv6(flags.get(MPLS_IPV6));
41 builder.setSrIpv6(flags.get(SR_IPV6));
42 buffer.skipBytes(RESERVERED);
43 builder.setRangeSize((long)buffer.readUnsignedMedium());
44 builder.setSidLabelIndex(SidLabelIndexParser.parseSidSubTlv(buffer));
45 return builder.build();
48 public static void serializeSrCapabilities(final SrCapabilities caps, final ByteBuf buffer) {
49 final BitArray bs = new BitArray(FLAGS_SIZE);
50 bs.set(MPLS_IPV4, caps.isMplsIpv4());
51 bs.set(MPLS_IPV6, caps.isMplsIpv6());
52 bs.set(SR_IPV6, caps.isSrIpv6());
54 buffer.writeZero(RESERVERED);
55 buffer.writeMedium(caps.getRangeSize().intValue());
56 TlvUtil.writeTLV(SidLabelIndexParser.SID_TYPE, SidLabelIndexParser.serializeSidValue(caps.getSidLabelIndex()), buffer);
59 public static SrAlgorithm parseSrAlgorithms(final ByteBuf buffer) {
60 final SrAlgorithmBuilder builder = new SrAlgorithmBuilder();
61 final List<Algorithm> algs = new ArrayList<>();
62 while (buffer.isReadable()) {
63 algs.add(Algorithm.forValue(buffer.readUnsignedByte()));
65 builder.setAlgorithms(algs);
66 return builder.build();
69 public static void serializeSrAlgorithms(final SrAlgorithm alg, final ByteBuf buffer) {
70 if (alg.getAlgorithms() != null) {
71 for (final Algorithm a : alg.getAlgorithms()) {
72 buffer.writeByte(a.getIntValue());