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