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 com.google.common.collect.ImmutableMap;
11 import io.netty.buffer.ByteBuf;
12 import io.netty.buffer.Unpooled;
14 import org.opendaylight.protocol.util.BitArray;
15 import org.opendaylight.protocol.util.Ipv6Util;
16 import org.opendaylight.protocol.util.MplsLabelUtil;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.segment.routing.ext.rev151014.sid.label.index.SidLabelIndex;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.segment.routing.ext.rev151014.sid.label.index.sid.label.index.Ipv6AddressCase;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.segment.routing.ext.rev151014.sid.label.index.sid.label.index.Ipv6AddressCaseBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.segment.routing.ext.rev151014.sid.label.index.sid.label.index.LocalLabelCase;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.segment.routing.ext.rev151014.sid.label.index.sid.label.index.LocalLabelCaseBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.segment.routing.ext.rev151014.sid.label.index.sid.label.index.SidCase;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.segment.routing.ext.rev151014.sid.label.index.sid.label.index.SidCaseBuilder;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
27 public final class SidLabelIndexParser {
29 private static final Logger LOG = LoggerFactory.getLogger(SidLabelIndexParser.class);
31 private SidLabelIndexParser() {
32 throw new UnsupportedOperationException();
36 LABEL(3), SID(4), IPV6_ADD(16);
38 private static final Map<Integer, Size> VALUE_MAP;
40 final ImmutableMap.Builder<java.lang.Integer, Size> b = ImmutableMap.builder();
41 for (final Size enumItem : Size.values()){
42 b.put(enumItem.size, enumItem);
44 VALUE_MAP = b.build();
46 Size(final int size) {
49 static Size forValue(final int value) {
50 return VALUE_MAP.get(value);
54 static final int SID_TYPE = 1161;
56 static ByteBuf serializeSidValue(final SidLabelIndex tlv) {
57 if (tlv instanceof Ipv6AddressCase) {
58 return Ipv6Util.byteBufForAddress(((Ipv6AddressCase) tlv).getIpv6Address());
59 } else if (tlv instanceof LocalLabelCase) {
60 return MplsLabelUtil.byteBufForMplsLabel(((LocalLabelCase) tlv).getLocalLabel());
61 } else if (tlv instanceof SidCase) {
62 return Unpooled.copyInt(((SidCase) tlv).getSid().intValue());
67 static SidLabelIndex parseSidSubTlv(final ByteBuf buffer) {
68 final int type = buffer.readUnsignedShort();
69 if (type != SID_TYPE) {
70 LOG.warn("Unexpected type in SID/index/label field, expected {}, actual {}, ignoring it", SID_TYPE, type);
73 final int length = buffer.readUnsignedShort();
74 return parseSidLabelIndex(Size.forValue(length), buffer);
77 static SidLabelIndex parseSidLabelIndex(final Size length, final ByteBuf buffer) {
80 return new LocalLabelCaseBuilder().setLocalLabel(MplsLabelUtil.mplsLabelForByteBuf(buffer)).build();
82 return new SidCaseBuilder().setSid(buffer.readUnsignedInt()).build();
84 return new Ipv6AddressCaseBuilder().setIpv6Address(Ipv6Util.addressForByteBuf(buffer)).build();
90 static void setFlags(final SidLabelIndex tlv, final BitArray flags, final int value, final int local) {
91 if (tlv instanceof LocalLabelCase) {
92 flags.set(value, Boolean.TRUE);
93 flags.set(local, Boolean.TRUE);
94 } else if (tlv instanceof SidCase) {
95 flags.set(value, Boolean.FALSE);
96 flags.set(local, Boolean.FALSE);
97 } else if (tlv instanceof Ipv6AddressCase) {
98 flags.set(value, Boolean.TRUE);
99 flags.set(local, Boolean.FALSE);