8b8a4896c1f7f17718c736fe8ed21a85fdf267c4
[bgpcep.git] / pcep / base-parser / src / main / java / org / opendaylight / protocol / pcep / parser / 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.parser.subobject;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.base.Preconditions;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
15 import org.opendaylight.protocol.pcep.spi.EROSubobjectParser;
16 import org.opendaylight.protocol.pcep.spi.EROSubobjectSerializer;
17 import org.opendaylight.protocol.pcep.spi.EROSubobjectUtil;
18 import org.opendaylight.protocol.pcep.spi.LabelRegistry;
19 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
20 import org.opendaylight.protocol.util.BitArray;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.explicit.route.object.ero.Subobject;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.explicit.route.object.ero.SubobjectBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.LabelCase;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.LabelCaseBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.label._case.Label;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.label._case.LabelBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.LabelType;
28
29 public class EROLabelSubobjectParser implements EROSubobjectParser, EROSubobjectSerializer {
30
31     public static final int TYPE = 3;
32
33     private static final int FLAGS_SIZE = 8;
34
35     private static final int C_TYPE_F_LENGTH = 1;
36
37     private static final int C_TYPE_F_OFFSET = FLAGS_SIZE / Byte.SIZE;
38
39     private static final int HEADER_LENGTH = C_TYPE_F_OFFSET + C_TYPE_F_LENGTH;
40
41     private static final int U_FLAG_OFFSET = 0;
42
43     private final LabelRegistry registry;
44
45     public EROLabelSubobjectParser(final LabelRegistry labelReg) {
46         this.registry = requireNonNull(labelReg);
47     }
48
49     @Override
50     public Subobject parseSubobject(final ByteBuf buffer, final boolean loose) throws PCEPDeserializerException {
51         Preconditions.checkArgument(buffer != null && buffer.isReadable(),
52                 "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()
55                     + "; Expected: >" + HEADER_LENGTH + ".");
56         }
57         final BitArray reserved = BitArray.valueOf(buffer, FLAGS_SIZE);
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 lbuilder = new LabelBuilder()
65                 .setUniDirectional(reserved.get(U_FLAG_OFFSET))
66                 .setLabelType(labelType);
67         final SubobjectBuilder builder = new SubobjectBuilder()
68                 .setLoose(loose)
69                 .setSubobjectType(new LabelCaseBuilder().setLabel(lbuilder.build()).build());
70
71         return builder.build();
72     }
73
74     @Override
75     public void serializeSubobject(final Subobject subobject, final ByteBuf buffer) {
76         Preconditions.checkArgument(subobject.getSubobjectType() instanceof LabelCase,
77                 "Unknown subobject instance. Passed %s. Needed LabelCase.", subobject.getSubobjectType().getClass());
78         final Label label = ((LabelCase) subobject.getSubobjectType()).getLabel();
79         final ByteBuf body = Unpooled.buffer();
80         this.registry.serializeLabel(label.isUniDirectional(), false, label.getLabelType(), body);
81         EROSubobjectUtil.formatSubobject(TYPE, subobject.isLoose(), body, buffer);
82     }
83 }