Promote MessageRegistry to pcep-api
[bgpcep.git] / pcep / base-parser / src / main / java / org / opendaylight / protocol / pcep / parser / subobject / RROLabelSubobjectParser.java
1 /*
2  * Copyright (c) 2013 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.pcep.parser.subobject;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
12
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
15 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
16 import org.opendaylight.protocol.pcep.spi.LabelRegistry;
17 import org.opendaylight.protocol.pcep.spi.RROSubobjectParser;
18 import org.opendaylight.protocol.pcep.spi.RROSubobjectSerializer;
19 import org.opendaylight.protocol.pcep.spi.RROSubobjectUtil;
20 import org.opendaylight.protocol.util.BitArray;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.reported.route.object.rro.Subobject;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.reported.route.object.rro.SubobjectBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820._record.route.subobjects.subobject.type.LabelCase;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820._record.route.subobjects.subobject.type.LabelCaseBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820._record.route.subobjects.subobject.type.label._case.Label;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820._record.route.subobjects.subobject.type.label._case.LabelBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.LabelType;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 public class RROLabelSubobjectParser implements RROSubobjectParser, RROSubobjectSerializer {
32     private static final Logger LOG = LoggerFactory.getLogger(RROLabelSubobjectParser.class);
33
34     public static final int TYPE = 3;
35
36     public static final int FLAGS_SIZE = 8;
37
38     public static final int C_TYPE_F_LENGTH = 1;
39
40     public static final int C_TYPE_F_OFFSET = FLAGS_SIZE / Byte.SIZE;
41
42     public static final int HEADER_LENGTH = C_TYPE_F_OFFSET + C_TYPE_F_LENGTH;
43
44     public static final int U_FLAG_OFFSET = 0;
45     public static final int G_FLAG_OFFSET = 7;
46
47     private final LabelRegistry registry;
48
49     public RROLabelSubobjectParser(final LabelRegistry labelReg) {
50         registry = requireNonNull(labelReg);
51     }
52
53     @Override
54     public Subobject parseSubobject(final ByteBuf buffer) throws PCEPDeserializerException {
55         checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
56         if (buffer.readableBytes() < HEADER_LENGTH) {
57             throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes()
58                     + "; Expected: >" + HEADER_LENGTH + ".");
59         }
60         final BitArray reserved = BitArray.valueOf(buffer, FLAGS_SIZE);
61
62         final short cType = buffer.readUnsignedByte();
63
64         final LabelType labelType = registry.parseLabel(cType, buffer.slice());
65         if (labelType == null) {
66             LOG.warn("Ignoring RRO label subobject with unknown C-TYPE: {}", cType);
67             return null;
68         }
69         final LabelBuilder builder = new LabelBuilder()
70                 .setUniDirectional(reserved.get(U_FLAG_OFFSET))
71                 .setGlobal(reserved.get(G_FLAG_OFFSET))
72                 .setLabelType(labelType);
73         return new SubobjectBuilder()
74                 .setSubobjectType(new LabelCaseBuilder()
75                 .setLabel(builder.build()).build())
76                 .build();
77     }
78
79     @Override
80     public void serializeSubobject(final Subobject subobject, final ByteBuf buffer) {
81         requireNonNull(subobject.getSubobjectType(), "Subobject type cannot be empty.");
82         final Label label = ((LabelCase) subobject.getSubobjectType()).getLabel();
83         final ByteBuf body = Unpooled.buffer();
84         registry.serializeLabel(label.getUniDirectional(), label.getGlobal(), label.getLabelType(), body);
85         RROSubobjectUtil.formatSubobject(TYPE, body, buffer);
86     }
87 }