BUG-4692: Migrate TCP-MD5 support in bgp package to netty's native-epoll
[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.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.segment.routing.ext.rev151014.sid.label.index.SidLabelIndex;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.segment.routing.ext.rev151014.sid.label.index.sid.label.index.Ipv6AddressCase;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.segment.routing.ext.rev151014.sid.label.index.sid.label.index.Ipv6AddressCaseBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.segment.routing.ext.rev151014.sid.label.index.sid.label.index.LocalLabelCase;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.segment.routing.ext.rev151014.sid.label.index.sid.label.index.LocalLabelCaseBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.segment.routing.ext.rev151014.sid.label.index.sid.label.index.SidCase;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.segment.routing.ext.rev151014.sid.label.index.sid.label.index.SidCaseBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.MplsLabel;
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     private static final int LABEL_MASK = 0xfffff;
31
32     private SidLabelIndexParser() {
33         throw new UnsupportedOperationException();
34     }
35
36     enum Size {
37         LABEL(3), SID(4), IPV6_ADD(16);
38         private int size;
39         private static final Map<Integer, Size> VALUE_MAP;
40         static {
41             final ImmutableMap.Builder<java.lang.Integer, Size> b = ImmutableMap.builder();
42             for (final Size enumItem : Size.values()){
43                 b.put(enumItem.size, enumItem);
44             }
45             VALUE_MAP = b.build();
46         }
47         Size(final int size) {
48             this.size = size;
49         }
50         static Size forValue(final int value) {
51             return VALUE_MAP.get(value);
52         }
53     }
54
55     static final int SID_TYPE = 1161;
56
57     static ByteBuf serializeSidValue(final SidLabelIndex tlv) {
58         if (tlv instanceof Ipv6AddressCase) {
59             return Ipv6Util.byteBufForAddress(((Ipv6AddressCase) tlv).getIpv6Address());
60         } else if (tlv instanceof LocalLabelCase) {
61             return Unpooled.copyMedium(((LocalLabelCase) tlv).getLocalLabel().getValue().intValue() & LABEL_MASK);
62         } else if (tlv instanceof SidCase) {
63             return Unpooled.copyInt(((SidCase) tlv).getSid().intValue());
64         }
65         return null;
66     }
67
68     static SidLabelIndex parseSidSubTlv(final ByteBuf buffer) {
69         final int type = buffer.readUnsignedShort();
70         if (type != SID_TYPE) {
71             LOG.warn("Unexpected type in SID/index/label field, expected {}, actual {}, ignoring it", SID_TYPE, type);
72             return null;
73         }
74         final int length = buffer.readUnsignedShort();
75         return parseSidLabelIndex(Size.forValue(length), buffer);
76     }
77
78     static SidLabelIndex parseSidLabelIndex(final Size length, final ByteBuf buffer) {
79         switch (length) {
80         case LABEL:
81             return new LocalLabelCaseBuilder().setLocalLabel(new MplsLabel(new Long(buffer.readUnsignedMedium() & LABEL_MASK))).build();
82         case SID:
83             return new SidCaseBuilder().setSid(buffer.readUnsignedInt()).build();
84         case IPV6_ADD:
85             return new Ipv6AddressCaseBuilder().setIpv6Address(Ipv6Util.addressForByteBuf(buffer)).build();
86         default:
87             return null;
88         }
89     }
90
91     static void setFlags(final SidLabelIndex tlv, final BitArray flags, final int value, final int local) {
92         if (tlv instanceof LocalLabelCase) {
93             flags.set(value, Boolean.TRUE);
94             flags.set(local, Boolean.TRUE);
95         } else if (tlv instanceof SidCase) {
96             flags.set(value, Boolean.FALSE);
97             flags.set(local, Boolean.FALSE);
98         } else if (tlv instanceof Ipv6AddressCase) {
99             flags.set(value, Boolean.TRUE);
100             flags.set(local, Boolean.FALSE);
101         }
102     }
103
104 }