Rename lists such that they follow IETF conventions
[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.impl.object.EROSubobjectUtil;
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.pcep.spi.PCEPDeserializerException;
20 import org.opendaylight.protocol.util.ByteArray;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.Subobject;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.SubobjectBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.LabelCase;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.LabelCaseBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.label._case.Label;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.label._case.LabelBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.label.subobject.LabelType;
28
29 import com.google.common.base.Preconditions;
30 import com.google.common.primitives.UnsignedBytes;
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 LabelHandlerRegistry registry;
49
50         public EROLabelSubobjectParser(final LabelHandlerRegistry labelReg) {
51                 this.registry = Preconditions.checkNotNull(labelReg);
52         }
53
54         @Override
55         public Subobject parseSubobject(final byte[] buffer, final boolean loose) throws PCEPDeserializerException {
56                 if (buffer == null || buffer.length == 0) {
57                         throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
58                 }
59                 if (buffer.length < HEADER_LENGTH) {
60                         throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.length + "; Expected: >"
61                                         + HEADER_LENGTH + ".");
62                 }
63                 final BitSet reserved = ByteArray.bytesToBitSet(Arrays.copyOfRange(buffer, RES_F_OFFSET, RES_F_LENGTH));
64
65                 final short cType = (short) UnsignedBytes.toInt(buffer[C_TYPE_F_OFFSET]);
66
67                 final LabelParser parser = this.registry.getLabelParser(cType);
68
69                 if (parser == null) {
70                         throw new PCEPDeserializerException("Unknown C-TYPE for ero label subobject. Passed: " + cType);
71                 }
72                 final LabelBuilder builder = new LabelBuilder();
73                 builder.setUniDirectional(reserved.get(U_FLAG_OFFSET));
74                 builder.setLabelType(parser.parseLabel(ByteArray.cutBytes(buffer, HEADER_LENGTH)));
75                 return new SubobjectBuilder().setLoose(loose).setSubobjectType(new LabelCaseBuilder().setLabel(builder.build()).build()).build();
76         }
77
78         @Override
79         public byte[] serializeSubobject(final Subobject subobject) {
80                 Preconditions.checkNotNull(subobject.getSubobjectType(), "Subobject type cannot be empty.");
81
82                 final Label label = ((LabelCase) subobject.getSubobjectType()).getLabel();
83                 final LabelType l = label.getLabelType();
84
85                 final LabelSerializer serializer = this.registry.getLabelSerializer(l);
86
87                 if (serializer == null) {
88                         throw new IllegalArgumentException("Unknown EROLabelSubobject instance. Passed "
89                                         + label.getLabelType().getImplementedInterface());
90                 }
91                 final byte[] labelbytes = serializer.serializeLabel(l);
92                 final byte[] retBytes = new byte[labelbytes.length + HEADER_LENGTH];
93
94                 System.arraycopy(labelbytes, 0, retBytes, HEADER_LENGTH, labelbytes.length);
95
96                 final BitSet reserved = new BitSet();
97                 reserved.set(U_FLAG_OFFSET, label.isUniDirectional());
98                 System.arraycopy(ByteArray.bitSetToBytes(reserved, RES_F_LENGTH), 0, retBytes, RES_F_OFFSET, RES_F_LENGTH);
99                 retBytes[C_TYPE_F_OFFSET] = UnsignedBytes.checkedCast(serializer.getType());
100                 return EROSubobjectUtil.formatSubobject(TYPE, subobject.isLoose(), retBytes);
101         }
102
103         @Override
104         public int getType() {
105                 return TYPE;
106         }
107 }