402f7e76a3805e709f5c3e7d65caab8503102cd5
[bgpcep.git] / bgp / extensions / linkstate / src / main / java / org / opendaylight / protocol / bgp / linkstate / impl / attribute / sr / SrNodeAttributesParser.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 io.netty.buffer.ByteBuf;
13 import java.util.ArrayList;
14 import java.util.List;
15 import org.opendaylight.mdsal.uint24.netty.Uint24ByteBufUtils;
16 import org.opendaylight.protocol.bgp.linkstate.spi.TlvUtil;
17 import org.opendaylight.protocol.util.BitArray;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.ProtocolId;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.node.state.SrAlgorithm;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.node.state.SrAlgorithmBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.node.state.SrCapabilities;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.node.state.SrCapabilitiesBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.segment.routing.ext.rev151014.Algorithm;
24
25 public final class SrNodeAttributesParser {
26
27     private static final int FLAGS_SIZE = 8;
28     /* SR Capabilities flags */
29     private static final int MPLS_IPV4 = 0;
30     private static final int MPLS_IPV6 = 1;
31     private static final int SR_IPV6 = 2;
32     private static final int RESERVERED = 1;
33
34     private SrNodeAttributesParser() {
35
36     }
37
38     public static SrCapabilities parseSrCapabilities(final ByteBuf buffer, final ProtocolId protocol) {
39         final SrCapabilitiesBuilder builder = new SrCapabilitiesBuilder();
40         final BitArray flags = BitArray.valueOf(buffer, FLAGS_SIZE);
41         setFlags(flags, protocol, builder);
42         buffer.skipBytes(RESERVERED);
43         builder.setRangeSize(Uint24ByteBufUtils.readUint24(buffer));
44         builder.setSidLabelIndex(SidLabelIndexParser.parseSidSubTlv(buffer));
45         return builder.build();
46     }
47
48     private static void setFlags(final BitArray flags, final ProtocolId protocol, final SrCapabilitiesBuilder builder) {
49         if (protocol.equals(ProtocolId.IsisLevel1) || protocol.equals(ProtocolId.IsisLevel2)) {
50             builder.setMplsIpv4(flags.get(MPLS_IPV4));
51             builder.setMplsIpv6(flags.get(MPLS_IPV6));
52             builder.setSrIpv6(flags.get(SR_IPV6));
53         } else {
54             builder.setMplsIpv4(Boolean.FALSE);
55             builder.setMplsIpv6(Boolean.FALSE);
56             builder.setSrIpv6(Boolean.FALSE);
57         }
58     }
59
60     public static void serializeSrCapabilities(final SrCapabilities caps, final ByteBuf buffer) {
61         final BitArray bs = new BitArray(FLAGS_SIZE);
62         bs.set(MPLS_IPV4, caps.isMplsIpv4());
63         bs.set(MPLS_IPV6, caps.isMplsIpv6());
64         bs.set(SR_IPV6, caps.isSrIpv6());
65         bs.toByteBuf(buffer);
66         buffer.writeZero(RESERVERED);
67         Uint24ByteBufUtils.writeUint24(buffer, caps.getRangeSize());
68         TlvUtil.writeTLV(SID_TYPE, SidLabelIndexParser.serializeSidValue(caps.getSidLabelIndex()), buffer);
69     }
70
71     public static SrAlgorithm parseSrAlgorithms(final ByteBuf buffer) {
72         final SrAlgorithmBuilder builder = new SrAlgorithmBuilder();
73         final List<Algorithm> algs = new ArrayList<>();
74         while (buffer.isReadable()) {
75             algs.add(Algorithm.forValue(buffer.readUnsignedByte()));
76         }
77         builder.setAlgorithms(algs);
78         return builder.build();
79     }
80
81     public static void serializeSrAlgorithms(final SrAlgorithm alg, final ByteBuf buffer) {
82         if (alg.getAlgorithms() != null) {
83             for (final Algorithm a : alg.getAlgorithms()) {
84                 buffer.writeByte(a.getIntValue());
85             }
86         }
87     }
88 }