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