Removed checkstyle warnings.
[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 com.google.common.primitives.UnsignedBytes;
12
13 import io.netty.buffer.ByteBuf;
14
15 import java.util.BitSet;
16
17 import org.opendaylight.protocol.pcep.impl.object.EROSubobjectUtil;
18 import org.opendaylight.protocol.pcep.spi.EROSubobjectParser;
19 import org.opendaylight.protocol.pcep.spi.EROSubobjectSerializer;
20 import org.opendaylight.protocol.pcep.spi.LabelRegistry;
21 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
22 import org.opendaylight.protocol.util.ByteArray;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.Subobject;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.SubobjectBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.LabelCase;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.LabelCaseBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.label._case.Label;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.label._case.LabelBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.label.subobject.LabelType;
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         final LabelType labelType = this.registry.parseLabel(cType, buffer.slice());
64         if (labelType == null) {
65             throw new PCEPDeserializerException("Unknown C-TYPE for ero label subobject. Passed: " + cType);
66         }
67         final LabelBuilder builder = new LabelBuilder();
68         builder.setUniDirectional(reserved.get(U_FLAG_OFFSET));
69         builder.setLabelType(labelType);
70         return new SubobjectBuilder().setLoose(loose).setSubobjectType(new LabelCaseBuilder().setLabel(builder.build()).build()).build();
71     }
72
73     @Override
74     public byte[] serializeSubobject(final Subobject subobject) {
75         Preconditions.checkNotNull(subobject.getSubobjectType(), "Subobject type cannot be empty.");
76         final Label label = ((LabelCase) subobject.getSubobjectType()).getLabel();
77         final byte[] labelbytes = this.registry.serializeLabel(label.isUniDirectional(), false, label.getLabelType());
78         if (labelbytes == null) {
79             throw new IllegalArgumentException("Unknown EROLabelSubobject instance. Passed "
80                     + label.getLabelType().getImplementedInterface());
81         }
82         return EROSubobjectUtil.formatSubobject(TYPE, subobject.isLoose(), labelbytes);
83     }
84 }