69cd3495e256428fefbc94d4b95b46bbd99744cb
[bgpcep.git] / bgp / linkstate / src / main / java / org / opendaylight / protocol / bgp / linkstate / attribute / sr / SrPrefixAttributesParser.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 org.opendaylight.protocol.bgp.linkstate.attribute.sr.SidLabelIndexParser.Size;
12 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.prefix.state.SrPrefix;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.prefix.state.SrPrefixBuilder;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.segment.routing.ext.rev151014.Algorithm;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.segment.routing.ext.rev151014.sid.label.index.SidLabelIndex;
16
17 public final class SrPrefixAttributesParser {
18
19     private SrPrefixAttributesParser() {
20         throw new UnsupportedOperationException();
21     }
22
23     private static final int RESERVED_PREFIX = 2;
24
25     public static SrPrefix parseSrPrefix(final ByteBuf buffer) {
26         final SrPrefixBuilder builder = new SrPrefixBuilder();
27         builder.setFlags(new byte[] { (byte) buffer.readUnsignedByte() });
28         builder.setAlgorithm(Algorithm.forValue(buffer.readUnsignedByte()));
29         buffer.skipBytes(RESERVED_PREFIX);
30         builder.setSidLabelIndex(SidLabelIndexParser.parseSidLabelIndex(Size.forValue(buffer.readableBytes()), buffer));
31         return builder.build();
32     }
33
34     public static void serializeSrPrefix(final SrPrefix srPrefix, final ByteBuf aggregator) {
35         serializePrefixAttributes(srPrefix.getFlags(), srPrefix.getAlgorithm(), srPrefix.getSidLabelIndex(), aggregator);
36     }
37
38     public static void serializePrefixAttributes(final byte[] flags, final Algorithm algorithm, final SidLabelIndex sidLabelIndex, final ByteBuf buffer) {
39         buffer.writeByte(flags[0]);
40         buffer.writeByte(algorithm.getIntValue());
41         buffer.writeZero(RESERVED_PREFIX);
42         buffer.writeBytes(SidLabelIndexParser.serializeSidValue(sidLabelIndex));
43     }
44
45 }