BUG-47 : switched subobjects to generated source code.
[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 java.util.Arrays;
11 import java.util.BitSet;
12
13 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
14 import org.opendaylight.protocol.pcep.spi.EROSubobjectParser;
15 import org.opendaylight.protocol.pcep.spi.EROSubobjectSerializer;
16 import org.opendaylight.protocol.pcep.spi.LabelHandlerRegistry;
17 import org.opendaylight.protocol.pcep.spi.LabelParser;
18 import org.opendaylight.protocol.pcep.spi.LabelSerializer;
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.Subobjects;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.SubobjectsBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.CLabel;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.LabelSubobject;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.LabelBuilder;
25
26 import com.google.common.base.Preconditions;
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 LabelHandlerRegistry registry;
45
46         public EROLabelSubobjectParser(final LabelHandlerRegistry labelReg) {
47                 this.registry = Preconditions.checkNotNull(labelReg);
48         }
49
50         @Override
51         public Subobjects parseSubobject(final byte[] buffer, final boolean loose) throws PCEPDeserializerException {
52                 if (buffer == null || buffer.length == 0)
53                         throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
54                 if (buffer.length < HEADER_LENGTH)
55                         throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.length + "; Expected: >"
56                                         + HEADER_LENGTH + ".");
57
58                 final BitSet reserved = ByteArray.bytesToBitSet(Arrays.copyOfRange(buffer, RES_F_OFFSET, RES_F_LENGTH));
59
60                 final short c_type = (short) (buffer[C_TYPE_F_OFFSET] & 0xFF);
61
62                 final LabelParser parser = this.registry.getLabelParser(c_type);
63
64                 if (parser == null) {
65                         throw new PCEPDeserializerException("Unknown C-TYPE for ero label subobject. Passed: " + c_type);
66                 }
67
68                 final LabelBuilder builder = new LabelBuilder();
69                 builder.setUniDirectional(reserved.get(U_FLAG_OFFSET));
70                 builder.setLabelType(parser.parseLabel(ByteArray.cutBytes(buffer, HEADER_LENGTH)));
71                 return new SubobjectsBuilder().setLoose(loose).setSubobjectType(builder.build()).build();
72         }
73
74         @Override
75         public byte[] serializeSubobject(final Subobjects subobject) {
76                 Preconditions.checkNotNull(subobject.getSubobjectType(), "Subobject type cannot be empty.");
77
78                 final LabelSubobject label = (LabelSubobject) subobject.getSubobjectType();
79
80                 final LabelSerializer serializer = this.registry.getLabelSerializer((CLabel) label);
81
82                 if (serializer == null)
83                         throw new IllegalArgumentException("Unknown EROLabelSubobject instance. Passed " + label.getClass());
84
85                 final byte[] labelbytes = serializer.serializeSubobject((CLabel) label);
86
87                 final byte[] retBytes = new byte[labelbytes.length + HEADER_LENGTH];
88
89                 System.arraycopy(labelbytes, 0, retBytes, HEADER_LENGTH, labelbytes.length);
90
91                 final BitSet reserved = new BitSet();
92                 reserved.set(U_FLAG_OFFSET, label.isUniDirectional());
93                 System.arraycopy(ByteArray.bitSetToBytes(reserved, RES_F_LENGTH), 0, retBytes, RES_F_OFFSET, RES_F_LENGTH);
94
95                 retBytes[C_TYPE_F_OFFSET] = (byte) serializer.getType();
96
97                 return retBytes;
98         }
99
100         @Override
101         public int getType() {
102                 return TYPE;
103         }
104 }