BUG-4552: Enhance LS-SR - discern protocols
[bgpcep.git] / bgp / linkstate / src / main / java / org / opendaylight / protocol / bgp / linkstate / attribute / sr / SidLabelIndexParser.java
1 /*
2  * Copyright (c) 2015 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 package org.opendaylight.protocol.bgp.linkstate.attribute.sr;
9
10 import com.google.common.collect.ImmutableMap;
11 import io.netty.buffer.ByteBuf;
12 import io.netty.buffer.Unpooled;
13 import java.util.Map;
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;
26
27 public final class SidLabelIndexParser {
28
29     private static final Logger LOG = LoggerFactory.getLogger(SidLabelIndexParser.class);
30
31     private SidLabelIndexParser() {
32         throw new UnsupportedOperationException();
33     }
34
35     enum Size {
36         LABEL(3), SID(4), IPV6_ADD(16);
37         private int size;
38         private static final Map<Integer, Size> VALUE_MAP;
39         static {
40             final ImmutableMap.Builder<java.lang.Integer, Size> b = ImmutableMap.builder();
41             for (final Size enumItem : Size.values()){
42                 b.put(enumItem.size, enumItem);
43             }
44             VALUE_MAP = b.build();
45         }
46         Size(final int size) {
47             this.size = size;
48         }
49         static Size forValue(final int value) {
50             return VALUE_MAP.get(value);
51         }
52     }
53
54     static final int SID_TYPE = 1161;
55
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());
63         }
64         return null;
65     }
66
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);
71             return null;
72         }
73         final int length = buffer.readUnsignedShort();
74         return parseSidLabelIndex(Size.forValue(length), buffer);
75     }
76
77     static SidLabelIndex parseSidLabelIndex(final Size length, final ByteBuf buffer) {
78         switch (length) {
79         case LABEL:
80             return new LocalLabelCaseBuilder().setLocalLabel(MplsLabelUtil.mplsLabelForByteBuf(buffer)).build();
81         case SID:
82             return new SidCaseBuilder().setSid(buffer.readUnsignedInt()).build();
83         case IPV6_ADD:
84             return new Ipv6AddressCaseBuilder().setIpv6Address(Ipv6Util.addressForByteBuf(buffer)).build();
85         default:
86             return null;
87         }
88     }
89
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);
100         }
101     }
102
103 }