BUG-2794 : refactored code to use BitArray
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / subobject / EROLabelSubobjectParser.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.impl.subobject;
9
10 import com.google.common.base.Preconditions;
11 import io.netty.buffer.ByteBuf;
12 import io.netty.buffer.Unpooled;
13 import org.opendaylight.protocol.pcep.spi.EROSubobjectParser;
14 import org.opendaylight.protocol.pcep.spi.EROSubobjectSerializer;
15 import org.opendaylight.protocol.pcep.spi.EROSubobjectUtil;
16 import org.opendaylight.protocol.pcep.spi.LabelRegistry;
17 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
18 import org.opendaylight.protocol.util.BitArray;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.Subobject;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.SubobjectBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.LabelCase;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.LabelCaseBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.label._case.Label;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.label._case.LabelBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.label.subobject.LabelType;
26
27 public class EROLabelSubobjectParser implements EROSubobjectParser, EROSubobjectSerializer {
28
29     public static final int TYPE = 3;
30
31     private static final int FLAGS_SIZE = 8;
32
33     private static final int C_TYPE_F_LENGTH = 1;
34
35     private static final int C_TYPE_F_OFFSET = FLAGS_SIZE / Byte.SIZE;
36
37     private static final int HEADER_LENGTH = C_TYPE_F_OFFSET + C_TYPE_F_LENGTH;
38
39     private static final int U_FLAG_OFFSET = 0;
40
41     private final LabelRegistry registry;
42
43     public EROLabelSubobjectParser(final LabelRegistry labelReg) {
44         this.registry = Preconditions.checkNotNull(labelReg);
45     }
46
47     @Override
48     public Subobject parseSubobject(final ByteBuf buffer, final boolean loose) throws PCEPDeserializerException {
49         Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
50         if (buffer.readableBytes() < HEADER_LENGTH) {
51             throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + "; Expected: >"
52                     + HEADER_LENGTH + ".");
53         }
54         final BitArray reserved = BitArray.valueOf(buffer, FLAGS_SIZE);
55         final short cType = buffer.readUnsignedByte();
56
57         final LabelType labelType = this.registry.parseLabel(cType, buffer.slice());
58         if (labelType == null) {
59             throw new PCEPDeserializerException("Unknown C-TYPE for ero label subobject. Passed: " + cType);
60         }
61         final LabelBuilder builder = new LabelBuilder();
62         builder.setUniDirectional(reserved.get(U_FLAG_OFFSET));
63         builder.setLabelType(labelType);
64         return new SubobjectBuilder().setLoose(loose).setSubobjectType(new LabelCaseBuilder().setLabel(builder.build()).build()).build();
65     }
66
67     @Override
68     public void serializeSubobject(final Subobject subobject, final ByteBuf buffer) {
69         Preconditions.checkArgument(subobject.getSubobjectType() instanceof LabelCase, "Unknown subobject instance. Passed %s. Needed LabelCase.", subobject.getSubobjectType().getClass());
70         final Label label = ((LabelCase) subobject.getSubobjectType()).getLabel();
71         final ByteBuf body = Unpooled.buffer();
72         this.registry.serializeLabel(label.isUniDirectional(), false, label.getLabelType(), body);
73         EROSubobjectUtil.formatSubobject(TYPE, subobject.isLoose(), body, buffer);
74     }
75 }