2ea5cd67e7532d633c84a0cc4f77bfe0ba2cc794
[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 import io.netty.buffer.Unpooled;
15
16 import java.util.BitSet;
17
18 import org.opendaylight.protocol.pcep.spi.EROSubobjectParser;
19 import org.opendaylight.protocol.pcep.spi.EROSubobjectSerializer;
20 import org.opendaylight.protocol.pcep.spi.EROSubobjectUtil;
21 import org.opendaylight.protocol.pcep.spi.LabelRegistry;
22 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
23 import org.opendaylight.protocol.util.ByteArray;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.Subobject;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.SubobjectBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.LabelCase;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.LabelCaseBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.label._case.Label;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.label._case.LabelBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.label.subobject.LabelType;
31
32 public class EROLabelSubobjectParser implements EROSubobjectParser, EROSubobjectSerializer {
33
34     public static final int TYPE = 3;
35
36     private static final int RES_F_LENGTH = 1;
37
38     private static final int C_TYPE_F_LENGTH = 1;
39
40     private static final int RES_F_OFFSET = 0;
41
42     private static final int C_TYPE_F_OFFSET = RES_F_OFFSET + RES_F_LENGTH;
43
44     private static final int HEADER_LENGTH = C_TYPE_F_OFFSET + C_TYPE_F_LENGTH;
45
46     private static final int U_FLAG_OFFSET = 0;
47
48     private final LabelRegistry registry;
49
50     public EROLabelSubobjectParser(final LabelRegistry labelReg) {
51         this.registry = Preconditions.checkNotNull(labelReg);
52     }
53
54     @Override
55     public Subobject parseSubobject(final ByteBuf buffer, final boolean loose) throws PCEPDeserializerException {
56         Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
57         if (buffer.readableBytes() < HEADER_LENGTH) {
58             throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + "; Expected: >"
59                     + HEADER_LENGTH + ".");
60         }
61         final BitSet reserved = ByteArray.bytesToBitSet(ByteArray.readBytes(buffer, RES_F_LENGTH));
62         final short cType = (short) UnsignedBytes.toInt(buffer.readByte());
63
64         final LabelType labelType = this.registry.parseLabel(cType, buffer.slice());
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 void serializeSubobject(final Subobject subobject, final ByteBuf buffer) {
76         Preconditions.checkArgument(subobject.getSubobjectType() instanceof LabelCase, "Unknown subobject instance. Passed %s. Needed LabelCase.", subobject.getSubobjectType().getClass());
77         final Label label = ((LabelCase) subobject.getSubobjectType()).getLabel();
78         // FIXME: switch to ByteBuf
79         final byte[] labelbytes = this.registry.serializeLabel(label.isUniDirectional(), false, label.getLabelType());
80         if (labelbytes == null) {
81             throw new IllegalArgumentException("Unknown EROLabelSubobject instance. Passed "
82                     + label.getLabelType().getImplementedInterface());
83         }
84         EROSubobjectUtil.formatSubobject(TYPE, subobject.isLoose(), Unpooled.copiedBuffer(labelbytes), buffer);
85     }
86 }