Create common parent for extensions families
[bgpcep.git] / bgp / extensions / linkstate / src / main / java / org / opendaylight / protocol / bgp / linkstate / impl / 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.impl.attribute.sr;
9
10 import static org.opendaylight.protocol.bgp.linkstate.impl.attribute.sr.binding.sid.sub.tlvs.SIDParser.SID_TYPE;
11
12 import com.google.common.collect.ImmutableMap;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
15 import java.util.Map;
16 import org.opendaylight.protocol.util.BitArray;
17 import org.opendaylight.protocol.util.Ipv6Util;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.segment.routing.ext.rev151014.sid.label.index.SidLabelIndex;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.segment.routing.ext.rev151014.sid.label.index.sid.label.index.Ipv6AddressCase;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.segment.routing.ext.rev151014.sid.label.index.sid.label.index.Ipv6AddressCaseBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.segment.routing.ext.rev151014.sid.label.index.sid.label.index.LocalLabelCase;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.segment.routing.ext.rev151014.sid.label.index.sid.label.index.LocalLabelCaseBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.segment.routing.ext.rev151014.sid.label.index.sid.label.index.SidCase;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.segment.routing.ext.rev151014.sid.label.index.sid.label.index.SidCaseBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.MplsLabel;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 public final class SidLabelIndexParser {
30
31     private static final Logger LOG = LoggerFactory.getLogger(SidLabelIndexParser.class);
32     private static final int LABEL_MASK = 0xfffff;
33
34     private SidLabelIndexParser() {
35         throw new UnsupportedOperationException();
36     }
37
38     public enum Size {
39         LABEL(3), SID(4), IPV6_ADD(16);
40         private static final Map<Integer, Size> VALUE_MAP;
41
42         static {
43             final ImmutableMap.Builder<java.lang.Integer, Size> b = ImmutableMap.builder();
44             for (final Size enumItem : Size.values()) {
45                 b.put(enumItem.length, enumItem);
46             }
47             VALUE_MAP = b.build();
48         }
49
50         private final int length;
51
52         Size(final int length) {
53             this.length = length;
54         }
55
56         public static Size forValue(final int value) {
57             return VALUE_MAP.get(value);
58         }
59     }
60
61     public static ByteBuf serializeSidValue(final SidLabelIndex tlv) {
62         if (tlv instanceof Ipv6AddressCase) {
63             return Ipv6Util.byteBufForAddress(((Ipv6AddressCase) tlv).getIpv6Address());
64         } else if (tlv instanceof LocalLabelCase) {
65             return Unpooled.copyMedium(((LocalLabelCase) tlv).getLocalLabel().getValue().intValue() & LABEL_MASK);
66         } else if (tlv instanceof SidCase) {
67             return Unpooled.copyInt(((SidCase) tlv).getSid().intValue());
68         }
69         return null;
70     }
71
72     static SidLabelIndex parseSidSubTlv(final ByteBuf buffer) {
73         final int type = buffer.readUnsignedShort();
74         if (type != SID_TYPE) {
75             LOG.warn("Unexpected type in SID/index/label field, expected {}, actual {}, ignoring it", SID_TYPE, type);
76             return null;
77         }
78         final int length = buffer.readUnsignedShort();
79         return parseSidLabelIndex(Size.forValue(length), buffer);
80     }
81
82     public static SidLabelIndex parseSidLabelIndex(final Size length, final ByteBuf buffer) {
83         switch (length) {
84         case LABEL:
85             return new LocalLabelCaseBuilder()
86                     .setLocalLabel(new MplsLabel(Long.valueOf(buffer.readUnsignedMedium() & LABEL_MASK))).build();
87         case SID:
88             return new SidCaseBuilder().setSid(buffer.readUnsignedInt()).build();
89         case IPV6_ADD:
90             return new Ipv6AddressCaseBuilder().setIpv6Address(Ipv6Util.addressForByteBuf(buffer)).build();
91         default:
92             return null;
93         }
94     }
95
96     static void setFlags(final SidLabelIndex tlv, final BitArray flags, final int value, final int local) {
97         if (tlv instanceof LocalLabelCase) {
98             flags.set(value, Boolean.TRUE);
99             flags.set(local, Boolean.TRUE);
100         } else if (tlv instanceof SidCase) {
101             flags.set(value, Boolean.FALSE);
102             flags.set(local, Boolean.FALSE);
103         } else if (tlv instanceof Ipv6AddressCase) {
104             flags.set(value, Boolean.TRUE);
105             flags.set(local, Boolean.FALSE);
106         }
107     }
108
109 }