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