import java.util.List;
import java.util.Map.Entry;
import org.opendaylight.protocol.bgp.linkstate.TlvUtil;
+import org.opendaylight.protocol.bgp.linkstate.attribute.sr.SrNodeAttributesParser;
import org.opendaylight.protocol.util.ByteArray;
import org.opendaylight.protocol.util.Ipv4Util;
import org.opendaylight.protocol.util.Ipv6Util;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.IsisAreaIdentifier;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.NodeFlagBits;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.TopologyIdentifier;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.node.state.SrAlgorithm;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.node.state.SrCapabilities;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.update.path.attributes.linkstate.path.attribute.LinkStateAttribute;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.update.path.attributes.linkstate.path.attribute.link.state.attribute.NodeAttributesCase;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.update.path.attributes.linkstate.path.attribute.link.state.attribute.NodeAttributesCaseBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.update.path.attributes.linkstate.path.attribute.link.state.attribute.node.attributes._case.NodeAttributes;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.update.path.attributes.linkstate.path.attribute.link.state.attribute.node.attributes._case.NodeAttributesBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.segment.routing.rev150206.SidLabelBinding;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
private static final int DYNAMIC_HOSTNAME = 1026;
private static final int ISIS_AREA_IDENTIFIER = 1027;
+ /* Segment routing TLVs */
+ private static final int SID_LABEL_BINDING = 1033;
+ private static final int SR_CAPABILITIES = 1034;
+ private static final int SR_ALGORITHMS = 1035;
+
/**
* Parse Node Attributes.
*
builder.setIpv6RouterId(ip6);
LOG.debug("Parsed IPv6 Router Identifier {}", ip6);
break;
+ case SID_LABEL_BINDING:
+ final SidLabelBinding label = SrNodeAttributesParser.parseSidLabelBinding(value);
+ // builder.set
+ LOG.debug("Parsed SID Label Binding {}", label);
+ break;
+ case SR_CAPABILITIES:
+ final SrCapabilities caps = SrNodeAttributesParser.parseSrCapabilities(value);
+ builder.setSrCapabilities(caps);
+ LOG.debug("Parsed SR Capabilities {}", caps);
+ break;
+ case SR_ALGORITHMS:
+ final SrAlgorithm algs = SrNodeAttributesParser.parseSrAlgorithms(value);
+ builder.setSrAlgorithm(algs);
+ LOG.debug("Parsed SR Algorithms {}", algs);
+ break;
default:
LOG.warn("TLV {} is not a valid node attribute, ignoring it", key);
}
--- /dev/null
+/*
+ * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.protocol.bgp.linkstate.attribute.sr;
+
+import com.google.common.primitives.UnsignedBytes;
+import io.netty.buffer.ByteBuf;
+import java.util.ArrayList;
+import java.util.BitSet;
+import java.util.List;
+import org.opendaylight.protocol.util.ByteArray;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.node.state.SrAlgorithm;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.node.state.SrAlgorithmBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.node.state.SrCapabilities;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.node.state.SrCapabilitiesBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.node.state.SrSidLabel;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.segment.routing.rev150206.Algorithm;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.segment.routing.rev150206.SidLabel;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.segment.routing.rev150206.SrCapabilities.Flags;
+
+public final class SrNodeAttributesParser {
+
+ private static final int FLAGS_SIZE = 1;
+
+ private static final int SID_TLV_TYPE = 1;
+
+ /* SR Capabilities flags */
+ private static final int IPV4 = 7;
+ private static final int IPV6 = 6;
+
+ public static SrSidLabel parseSidLabelBinding(final ByteBuf buffer) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public static SrCapabilities parseSrCapabilities(final ByteBuf buffer) {
+ final SrCapabilitiesBuilder builder = new SrCapabilitiesBuilder();
+ final BitSet flags = BitSet.valueOf(ByteArray.readBytes(buffer, FLAGS_SIZE));
+ builder.setFlags(new Flags(flags.get(IPV4), flags.get(IPV6)));
+ builder.setValueRange((long)buffer.readUnsignedMedium());
+ buffer.skipBytes(2);
+ builder.setSid(new SidLabel(ByteArray.readAllBytes(buffer)));
+ return builder.build();
+ }
+
+ public static void serializeSrCapabilities(final SrCapabilities caps, final ByteBuf buffer) {
+ final Flags flags = caps.getFlags();
+ final BitSet bs = new BitSet(FLAGS_SIZE);
+ if (flags.isIpv4() != null) {
+ bs.set(IPV4, flags.isIpv4());
+ }
+ if (flags.isIpv6() != null) {
+ bs.set(IPV6, flags.isIpv6());
+ }
+ buffer.writeBytes(bs.toByteArray());
+ buffer.writeMedium(caps.getValueRange().intValue());
+ buffer.writeByte(SID_TLV_TYPE);
+ final byte[] sid = caps.getSid().getValue();
+ buffer.writeByte(sid.length);
+ buffer.writeBytes(sid);
+ }
+
+ public static SrAlgorithm parseSrAlgorithms(final ByteBuf buffer) {
+ final SrAlgorithmBuilder builder = new SrAlgorithmBuilder();
+ final List<Algorithm> algs = new ArrayList<>();
+ while (buffer.isReadable()) {
+ algs.add(Algorithm.forValue(UnsignedBytes.toInt(buffer.readByte())));
+ }
+ builder.setAlgorithm(algs);
+ return builder.build();
+ }
+
+ public static void serializeSrAlgorithms(final SrAlgorithm alg, final ByteBuf buffer) {
+ if (alg.getAlgorithm() != null) {
+ for (final Algorithm a : alg.getAlgorithm()) {
+ buffer.writeByte(a.getIntValue());
+ }
+ }
+ }
+}
--- /dev/null
+/*
+ * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.protocol.bgp.linkstate;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+
+import com.google.common.collect.Lists;
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
+import java.util.Collections;
+import org.junit.Test;
+import org.opendaylight.protocol.bgp.linkstate.attribute.sr.SrNodeAttributesParser;
+import org.opendaylight.protocol.util.ByteArray;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.node.state.SrAlgorithm;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.node.state.SrAlgorithmBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.node.state.SrCapabilities;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.node.state.SrCapabilitiesBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.segment.routing.rev150206.Algorithm;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.segment.routing.rev150206.SidLabel;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.segment.routing.rev150206.SrCapabilities.Flags;
+
+public class SrAttributeParserTest {
+
+ @Test
+ public void testSrAlgorithm() {
+ final byte[] bytes = { 0 };
+ final SrAlgorithm alg = new SrAlgorithmBuilder().setAlgorithm(Lists.newArrayList(Algorithm.ShortestPathFirst)).build();
+ final SrAlgorithm empty = new SrAlgorithmBuilder().setAlgorithm(Collections.<Algorithm>emptyList()).build();
+ assertEquals(alg, SrNodeAttributesParser.parseSrAlgorithms(Unpooled.wrappedBuffer(bytes)));
+ final ByteBuf b = Unpooled.buffer();
+ assertEquals(empty, SrNodeAttributesParser.parseSrAlgorithms(b));
+ SrNodeAttributesParser.serializeSrAlgorithms(alg, b);
+ assertArrayEquals(bytes, ByteArray.readAllBytes(b));
+
+ b.clear();
+ SrNodeAttributesParser.serializeSrAlgorithms(empty, b);
+ assertEquals(0, b.readableBytes());
+ }
+
+ @Test
+ public void testSrCapabilities() {
+ final byte[] bytes = { (byte)0xC0, 0, 0, 10, 1, 4, 1, 2, 3, 4 };
+ final SrCapabilities caps = new SrCapabilitiesBuilder().setFlags(new Flags(Boolean.TRUE, Boolean.TRUE)).setSid(new SidLabel(new byte[] {1,2,3,4})).setValueRange((long) 10).build();
+ assertEquals(caps, SrNodeAttributesParser.parseSrCapabilities(Unpooled.wrappedBuffer(bytes)));
+ final ByteBuf b = Unpooled.buffer();
+ SrNodeAttributesParser.serializeSrCapabilities(caps, b);
+ assertArrayEquals(bytes, ByteArray.readAllBytes(b));
+ }
+}