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 org.opendaylight.protocol.concepts.Ipv4Util;
11 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
12 import org.opendaylight.protocol.pcep.spi.XROSubobjectParser;
13 import org.opendaylight.protocol.pcep.spi.XROSubobjectSerializer;
14 import org.opendaylight.protocol.util.ByteArray;
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.Subobjects;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.SubobjectsBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.ExcludeRouteSubobjects.Attribute;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.IpPrefixSubobject;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.IpPrefixBuilder;
22 import com.google.common.primitives.UnsignedBytes;
25 * Parser for {@link IpPrefixSubobject}
27 public class XROIpPrefixSubobjectParser implements XROSubobjectParser, XROSubobjectSerializer {
29 public static final int TYPE = 1;
31 public static final int TYPE6 = 2;
33 public static final int IP4_F_LENGTH = 4;
34 public static final int PREFIX_F_LENGTH = 1;
35 public static final int ATTRIBUTE_LENGTH = 1;
37 public static final int IP_F_OFFSET = 0;
38 public static final int PREFIX4_F_OFFSET = IP_F_OFFSET + IP4_F_LENGTH;
39 public static final int ATTRIBUTE4_OFFSET = PREFIX4_F_OFFSET + PREFIX_F_LENGTH;
41 public static final int CONTENT4_LENGTH = ATTRIBUTE4_OFFSET + ATTRIBUTE_LENGTH;
43 public static final int IP6_F_LENGTH = 16;
44 public static final int PREFIX6_F_OFFSET = IP_F_OFFSET + IP6_F_LENGTH;
45 public static final int ATTRIBUTE6_OFFSET = PREFIX6_F_OFFSET + PREFIX_F_LENGTH;
47 public static final int CONTENT6_LENGTH = ATTRIBUTE6_OFFSET + ATTRIBUTE_LENGTH;
50 public Subobjects parseSubobject(final byte[] buffer, final boolean mandatory) throws PCEPDeserializerException {
51 if (buffer == null || buffer.length == 0)
52 throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
53 if (buffer.length != CONTENT4_LENGTH && buffer.length != CONTENT6_LENGTH)
54 throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.length + ";");
56 final int length = UnsignedBytes.toInt(buffer[PREFIX4_F_OFFSET]);
58 final SubobjectsBuilder builder = new SubobjectsBuilder();
59 builder.setMandatory(mandatory);
60 builder.setAttribute(Attribute.forValue(buffer[ATTRIBUTE4_OFFSET] & 0xFF));
61 builder.setSubobjectType(new IpPrefixBuilder().setIpPrefix(
62 new IpPrefix(Ipv4Util.prefixForBytes(ByteArray.subByte(buffer, IP_F_OFFSET, IP4_F_LENGTH), length))).build());
63 return builder.build();
67 public byte[] serializeSubobject(final Subobjects subobject) {
68 if (!(subobject.getSubobjectType() instanceof IpPrefixSubobject))
69 throw new IllegalArgumentException("Unknown PCEPXROSubobject instance. Passed " + subobject.getSubobjectType().getClass()
70 + ". Needed IpPrefixSubobject.");
72 final IpPrefixSubobject specObj = (IpPrefixSubobject) subobject.getSubobjectType();
73 final IpPrefix prefix = specObj.getIpPrefix();
75 if (prefix.getIpv4Prefix() == null && prefix.getIpv6Prefix() == null)
76 throw new IllegalArgumentException("Unknown AbstractPrefix instance. Passed " + prefix.getClass() + ".");
78 if (prefix.getIpv4Prefix() != null) {
79 final byte[] retBytes = new byte[CONTENT4_LENGTH];
80 ByteArray.copyWhole(prefix.getIpv4Prefix().getValue().getBytes(), retBytes, IP_F_OFFSET);
81 retBytes[PREFIX4_F_OFFSET] = ByteArray.intToBytes(Ipv4Util.getPrefixLength(prefix))[Integer.SIZE / Byte.SIZE - 1];
84 final byte[] retBytes = new byte[CONTENT6_LENGTH];
85 ByteArray.copyWhole(prefix.getIpv6Prefix().getValue().getBytes(), retBytes, IP_F_OFFSET);
86 retBytes[PREFIX6_F_OFFSET] = ByteArray.intToBytes(Ipv4Util.getPrefixLength(prefix))[Integer.SIZE / Byte.SIZE - 1];
92 public int getType() {
96 public int getType6() {