BUG-612 : switched ERO to ByteBuf.
[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 io.netty.buffer.ByteBuf;
11
12 import java.util.BitSet;
13
14 import org.opendaylight.protocol.pcep.impl.object.EROSubobjectUtil;
15 import org.opendaylight.protocol.pcep.spi.EROSubobjectParser;
16 import org.opendaylight.protocol.pcep.spi.EROSubobjectSerializer;
17 import org.opendaylight.protocol.pcep.spi.LabelRegistry;
18 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
19 import org.opendaylight.protocol.util.ByteArray;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.Subobject;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.SubobjectBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.LabelCase;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.LabelCaseBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.label._case.Label;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.label._case.LabelBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.label.subobject.LabelType;
27
28 import com.google.common.base.Preconditions;
29 import com.google.common.primitives.UnsignedBytes;
30
31 public class EROLabelSubobjectParser implements EROSubobjectParser, EROSubobjectSerializer {
32
33         public static final int TYPE = 3;
34
35         private static final int RES_F_LENGTH = 1;
36
37         private static final int C_TYPE_F_LENGTH = 1;
38
39         private static final int RES_F_OFFSET = 0;
40
41         private static final int C_TYPE_F_OFFSET = RES_F_OFFSET + RES_F_LENGTH;
42
43         private static final int HEADER_LENGTH = C_TYPE_F_OFFSET + C_TYPE_F_LENGTH;
44
45         private static final int U_FLAG_OFFSET = 0;
46
47         private final LabelRegistry registry;
48
49         public EROLabelSubobjectParser(final LabelRegistry labelReg) {
50                 this.registry = Preconditions.checkNotNull(labelReg);
51         }
52
53         @Override
54         public Subobject parseSubobject(final ByteBuf buffer, final boolean loose) throws PCEPDeserializerException {
55                 Preconditions.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() + "; Expected: >"
58                                         + HEADER_LENGTH + ".");
59                 }
60                 final BitSet reserved = ByteArray.bytesToBitSet(ByteArray.readBytes(buffer, RES_F_LENGTH));
61                 final short cType = (short) UnsignedBytes.toInt(buffer.readByte());
62
63                 //FIXME: switch to ByteBuf
64                 final LabelType labelType = this.registry.parseLabel(cType, ByteArray.readAllBytes(buffer));
65                 if (labelType == null) {
66                         throw new PCEPDeserializerException("Unknown C-TYPE for ero label subobject. Passed: " + cType);
67                 }
68                 final LabelBuilder builder = new LabelBuilder();
69                 builder.setUniDirectional(reserved.get(U_FLAG_OFFSET));
70                 builder.setLabelType(labelType);
71                 return new SubobjectBuilder().setLoose(loose).setSubobjectType(new LabelCaseBuilder().setLabel(builder.build()).build()).build();
72         }
73
74         @Override
75         public byte[] serializeSubobject(final Subobject subobject) {
76                 Preconditions.checkNotNull(subobject.getSubobjectType(), "Subobject type cannot be empty.");
77                 final Label label = ((LabelCase) subobject.getSubobjectType()).getLabel();
78                 final byte[] labelbytes = this.registry.serializeLabel(label.isUniDirectional(), false, label.getLabelType());
79                 if (labelbytes == null) {
80                         throw new IllegalArgumentException("Unknown EROLabelSubobject instance. Passed "
81                                         + label.getLabelType().getImplementedInterface());
82                 }
83                 return EROSubobjectUtil.formatSubobject(TYPE, subobject.isLoose(), labelbytes);
84         }
85 }