f6344ec633a7a336b232cf3de167af1a02543d88
[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 java.util.BitSet;
14 import org.opendaylight.protocol.pcep.spi.EROSubobjectParser;
15 import org.opendaylight.protocol.pcep.spi.EROSubobjectSerializer;
16 import org.opendaylight.protocol.pcep.spi.EROSubobjectUtil;
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 public class EROLabelSubobjectParser implements EROSubobjectParser, EROSubobjectSerializer {
29
30     public static final int TYPE = 3;
31
32     private static final int RES_F_LENGTH = 1;
33
34     private static final int C_TYPE_F_LENGTH = 1;
35
36     private static final int RES_F_OFFSET = 0;
37
38     private static final int C_TYPE_F_OFFSET = RES_F_OFFSET + RES_F_LENGTH;
39
40     private static final int HEADER_LENGTH = C_TYPE_F_OFFSET + C_TYPE_F_LENGTH;
41
42     private static final int U_FLAG_OFFSET = 0;
43
44     private final LabelRegistry registry;
45
46     public EROLabelSubobjectParser(final LabelRegistry labelReg) {
47         this.registry = Preconditions.checkNotNull(labelReg);
48     }
49
50     @Override
51     public Subobject parseSubobject(final ByteBuf buffer, final boolean loose) throws PCEPDeserializerException {
52         Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
53         if (buffer.readableBytes() < HEADER_LENGTH) {
54             throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + "; Expected: >"
55                     + HEADER_LENGTH + ".");
56         }
57         final BitSet reserved = ByteArray.bytesToBitSet(ByteArray.readBytes(buffer, RES_F_LENGTH));
58         final short cType = buffer.readUnsignedByte();
59
60         final LabelType labelType = this.registry.parseLabel(cType, buffer.slice());
61         if (labelType == null) {
62             throw new PCEPDeserializerException("Unknown C-TYPE for ero label subobject. Passed: " + cType);
63         }
64         final LabelBuilder builder = new LabelBuilder();
65         builder.setUniDirectional(reserved.get(U_FLAG_OFFSET));
66         builder.setLabelType(labelType);
67         return new SubobjectBuilder().setLoose(loose).setSubobjectType(new LabelCaseBuilder().setLabel(builder.build()).build()).build();
68     }
69
70     @Override
71     public void serializeSubobject(final Subobject subobject, final ByteBuf buffer) {
72         Preconditions.checkArgument(subobject.getSubobjectType() instanceof LabelCase, "Unknown subobject instance. Passed %s. Needed LabelCase.", subobject.getSubobjectType().getClass());
73         final Label label = ((LabelCase) subobject.getSubobjectType()).getLabel();
74         final ByteBuf body = Unpooled.buffer();
75         this.registry.serializeLabel(label.isUniDirectional(), false, label.getLabelType(), body);
76         EROSubobjectUtil.formatSubobject(TYPE, subobject.isLoose(), body, buffer);
77     }
78 }