BUG-130 : introduced RROSubobjectUtil.
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / subobject / RROLabelSubobjectParser.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 java.util.Arrays;
11 import java.util.BitSet;
12
13 import org.opendaylight.protocol.pcep.impl.object.RROSubobjectUtil;
14 import org.opendaylight.protocol.pcep.spi.LabelHandlerRegistry;
15 import org.opendaylight.protocol.pcep.spi.LabelParser;
16 import org.opendaylight.protocol.pcep.spi.LabelSerializer;
17 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
18 import org.opendaylight.protocol.pcep.spi.RROSubobjectParser;
19 import org.opendaylight.protocol.pcep.spi.RROSubobjectSerializer;
20 import org.opendaylight.protocol.util.ByteArray;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.reported.route.object.rro.Subobjects;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.reported.route.object.rro.SubobjectsBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.label.subobject.LabelType;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.record.route.subobjects.subobject.type.LabelCase;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.record.route.subobjects.subobject.type.LabelCaseBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.record.route.subobjects.subobject.type.label._case.Label;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.record.route.subobjects.subobject.type.label._case.LabelBuilder;
28
29 import com.google.common.base.Preconditions;
30 import com.google.common.primitives.UnsignedBytes;
31
32 public class RROLabelSubobjectParser implements RROSubobjectParser, RROSubobjectSerializer {
33
34         public static final int TYPE = 3;
35
36         public static final int RES_F_LENGTH = 1;
37
38         public static final int C_TYPE_F_LENGTH = 1;
39
40         public static final int RES_F_OFFSET = 0;
41
42         public static final int C_TYPE_F_OFFSET = RES_F_OFFSET + RES_F_LENGTH;
43
44         public static final int HEADER_LENGTH = C_TYPE_F_OFFSET + C_TYPE_F_LENGTH;
45
46         public static final int U_FLAG_OFFSET = 0;
47         public static final int G_FLAG_OFFSET = 7;
48
49         private final LabelHandlerRegistry registry;
50
51         public RROLabelSubobjectParser(final LabelHandlerRegistry labelReg) {
52                 this.registry = Preconditions.checkNotNull(labelReg);
53         }
54
55         @Override
56         public Subobjects parseSubobject(final byte[] buffer) throws PCEPDeserializerException {
57                 if (buffer == null || buffer.length == 0) {
58                         throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
59                 }
60                 if (buffer.length < HEADER_LENGTH) {
61                         throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.length + "; Expected: >"
62                                         + HEADER_LENGTH + ".");
63                 }
64                 final BitSet reserved = ByteArray.bytesToBitSet(Arrays.copyOfRange(buffer, RES_F_OFFSET, RES_F_LENGTH));
65
66                 final short cType = (short) UnsignedBytes.toInt(buffer[C_TYPE_F_OFFSET]);
67
68                 final LabelParser parser = this.registry.getLabelParser(cType);
69
70                 if (parser == null) {
71                         throw new PCEPDeserializerException("Unknown C-TYPE for ero label subobject. Passed: " + cType);
72                 }
73                 final LabelBuilder builder = new LabelBuilder();
74                 builder.setUniDirectional(reserved.get(U_FLAG_OFFSET));
75                 builder.setGlobal(reserved.get(G_FLAG_OFFSET));
76                 builder.setLabelType(parser.parseLabel(ByteArray.cutBytes(buffer, HEADER_LENGTH)));
77                 return new SubobjectsBuilder().setSubobjectType(new LabelCaseBuilder().setLabel(builder.build()).build()).build();
78         }
79
80         @Override
81         public byte[] serializeSubobject(final Subobjects subobject) {
82                 final Label label = ((LabelCase) subobject.getSubobjectType()).getLabel();
83                 final LabelType l = label.getLabelType();
84                 final LabelSerializer parser = this.registry.getLabelSerializer(l);
85                 if (parser == null) {
86                         throw new IllegalArgumentException("Unknown RROLabelSubobject instance. Passed " + subobject.getSubobjectType().getClass());
87                 }
88                 final byte[] labelbytes = parser.serializeLabel(l);
89                 final byte[] retBytes = new byte[labelbytes.length + HEADER_LENGTH];
90                 System.arraycopy(labelbytes, 0, retBytes, HEADER_LENGTH, labelbytes.length);
91
92                 final BitSet reserved = new BitSet();
93                 reserved.set(U_FLAG_OFFSET, label.isUniDirectional());
94                 reserved.set(G_FLAG_OFFSET, label.isGlobal());
95                 System.arraycopy(ByteArray.bitSetToBytes(reserved, RES_F_LENGTH), 0, retBytes, RES_F_OFFSET, RES_F_LENGTH);
96                 retBytes[C_TYPE_F_OFFSET] = UnsignedBytes.checkedCast(parser.getType());
97                 return RROSubobjectUtil.formatSubobject(TYPE, retBytes);
98         }
99
100         @Override
101         public int getType() {
102                 return TYPE;
103         }
104 }