2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.pcep.impl.subobject;
10 import java.util.List;
12 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
13 import org.opendaylight.protocol.pcep.spi.EROSubobjectParser;
14 import org.opendaylight.protocol.pcep.spi.EROSubobjectSerializer;
15 import org.opendaylight.protocol.pcep.spi.XROSubobjectHandlerRegistry;
16 import org.opendaylight.protocol.pcep.spi.XROSubobjectParser;
17 import org.opendaylight.protocol.pcep.spi.XROSubobjectSerializer;
18 import org.opendaylight.protocol.util.ByteArray;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.Subobjects;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.SubobjectsBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.explicit.route.subobjects.subobject.type.Exrs;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.explicit.route.subobjects.subobject.type.exrs.ExrsBuilder;
24 import com.google.common.collect.Lists;
25 import com.google.common.primitives.UnsignedBytes;
27 public class EROExplicitExclusionRouteSubobjectParser implements EROSubobjectParser, EROSubobjectSerializer {
29 public static final int TYPE = 33;
31 private static final int SUB_TYPE_FLAG_F_LENGTH = 1;
32 private static final int SUB_LENGTH_F_LENGTH = 1;
33 private static final int SUB_HEADER_LENGTH = SUB_TYPE_FLAG_F_LENGTH + SUB_LENGTH_F_LENGTH;
35 private static final int TYPE_FLAG_F_OFFSET = 0;
36 private static final int LENGTH_F_OFFSET = TYPE_FLAG_F_OFFSET + SUB_TYPE_FLAG_F_LENGTH;
37 private static final int SO_CONTENTS_OFFSET = LENGTH_F_OFFSET + SUB_LENGTH_F_LENGTH;
39 private final XROSubobjectHandlerRegistry registry;
41 public EROExplicitExclusionRouteSubobjectParser(final XROSubobjectHandlerRegistry registry) {
42 this.registry = registry;
46 public Subobjects parseSubobject(final byte[] buffer, final boolean loose) throws PCEPDeserializerException {
47 if (buffer == null || buffer.length == 0) {
48 throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
50 final SubobjectsBuilder builder = new SubobjectsBuilder();
51 builder.setLoose(loose);
52 final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.Subobjects> list = parseSubobjects(buffer);
53 final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.explicit.route.subobjects.subobject.type.exrs.Exrs> exrss = Lists.newArrayList();
54 for (final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.Subobjects s : list) {
55 final ExrsBuilder b = new ExrsBuilder();
56 b.setAttribute(s.getAttribute());
57 b.setMandatory(s.isMandatory());
58 b.setSubobjectType(s.getSubobjectType());
61 builder.setSubobjectType(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.explicit.route.subobjects.subobject.type.ExrsBuilder().setExrs(
63 return builder.build();
67 public byte[] serializeSubobject(final Subobjects subobject) {
68 if (!(subobject.getSubobjectType() instanceof Exrs)) {
69 throw new IllegalArgumentException("Unknown subobject instance. Passed " + subobject.getSubobjectType().getClass()
72 final Exrs e = (Exrs) subobject.getSubobjectType();
73 final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.Subobjects> list = Lists.newArrayList();
74 for (final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.explicit.route.subobjects.subobject.type.exrs.Exrs ex : e.getExrs()) {
75 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.SubobjectsBuilder b = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.SubobjectsBuilder();
76 b.setAttribute(ex.getAttribute());
77 b.setMandatory(ex.isMandatory());
78 b.setSubobjectType(ex.getSubobjectType());
81 return serializeSubobject(list);
84 private List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.Subobjects> parseSubobjects(
85 final byte[] bytes) throws PCEPDeserializerException {
87 throw new IllegalArgumentException("Byte array is mandatory.");
92 byte[] soContentsBytes;
96 final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.Subobjects> subs = Lists.newArrayList();
98 while (offset < bytes.length) {
100 length = ByteArray.bytesToInt(ByteArray.subByte(bytes, offset + LENGTH_F_OFFSET, SUB_LENGTH_F_LENGTH));
102 final boolean mandatory = ((bytes[offset + TYPE_FLAG_F_OFFSET] & (1 << 7)) != 0) ? true : false;
103 type = (bytes[offset + TYPE_FLAG_F_OFFSET] & 0xff) & ~(1 << 7);
104 if (length > bytes.length - offset) {
105 throw new PCEPDeserializerException("Wrong length specified. Passed: " + length + "; Expected: <= "
106 + (bytes.length - offset));
109 soContentsBytes = new byte[length - SO_CONTENTS_OFFSET];
110 System.arraycopy(bytes, offset + SO_CONTENTS_OFFSET, soContentsBytes, 0, length - SO_CONTENTS_OFFSET);
112 final XROSubobjectParser parser = this.registry.getSubobjectParser(type);
114 subs.add(parser.parseSubobject(soContentsBytes, mandatory));
121 private byte[] serializeSubobject(
122 final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.Subobjects> subobjects) {
124 final List<byte[]> result = Lists.newArrayList();
128 for (final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.Subobjects subobject : subobjects) {
130 final XROSubobjectSerializer serializer = this.registry.getSubobjectSerializer(subobject.getSubobjectType());
132 final byte[] valueBytes = serializer.serializeSubobject(subobject);
134 final byte[] bytes = new byte[SUB_HEADER_LENGTH + valueBytes.length];
136 final byte typeBytes = (byte) (ByteArray.cutBytes(ByteArray.intToBytes(serializer.getType()), (Integer.SIZE / 8) - 1)[0] | (subobject.isMandatory() ? 1 << 7
138 final byte lengthBytes = UnsignedBytes.checkedCast(valueBytes.length);
140 bytes[0] = typeBytes;
141 bytes[1] = lengthBytes;
142 System.arraycopy(valueBytes, 0, bytes, SUB_HEADER_LENGTH, valueBytes.length);
144 finalLength += bytes.length;
148 final byte[] resultBytes = new byte[finalLength];
150 for (final byte[] b : result) {
151 System.arraycopy(b, 0, resultBytes, byteOffset, b.length);
152 byteOffset += b.length;
158 public int getType() {