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.concepts.Ipv6Util;
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.util.ByteArray;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.Subobjects;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.SubobjectsBuilder;
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 EROIpPrefixSubobjectParser implements EROSubobjectParser, EROSubobjectSerializer {
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 PREFIX4_F_LENGTH = 1;
36 public static final int PREFIX4_F_OFFSET = IP4_F_LENGTH;
38 public static final int CONTENT4_LENGTH = PREFIX4_F_OFFSET + PREFIX4_F_LENGTH + 1; // added reserved field of size 1
40 public static final int IP_F_LENGTH = 16;
41 public static final int PREFIX_F_LENGTH = 1;
43 public static final int IP_F_OFFSET = 0;
44 public static final int PREFIX_F_OFFSET = IP_F_OFFSET + IP_F_LENGTH;
46 public static final int CONTENT_LENGTH = PREFIX_F_OFFSET + PREFIX_F_LENGTH + 1; // added reserved field of size 1
50 public Subobjects parseSubobject(final byte[] buffer, final boolean loose) throws PCEPDeserializerException {
51 if (buffer == null || buffer.length == 0) {
52 throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
55 final SubobjectsBuilder builder = new SubobjectsBuilder();
56 builder.setLoose(loose);
58 if (buffer.length == CONTENT4_LENGTH) {
59 final int length = UnsignedBytes.toInt(buffer[PREFIX4_F_OFFSET]);
60 builder.setSubobjectType(new IpPrefixBuilder().setIpPrefix(
61 new IpPrefix(Ipv4Util.prefixForBytes(ByteArray.subByte(buffer, 0, IP4_F_LENGTH), length))).build());
62 return builder.build();
63 } else if (buffer.length == CONTENT_LENGTH) {
64 final int length = UnsignedBytes.toInt(buffer[PREFIX_F_OFFSET]);
65 builder.setSubobjectType(new IpPrefixBuilder().setIpPrefix(
66 new IpPrefix(Ipv6Util.prefixForBytes(ByteArray.subByte(buffer, 0, IP_F_LENGTH), length))).build());
67 return builder.build();
69 throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.length + ";");
74 public byte[] serializeSubobject(final Subobjects subobject) {
75 if (!(subobject.getSubobjectType() instanceof IpPrefixSubobject)) {
76 throw new IllegalArgumentException("Unknown subobject instance. Passed " + subobject.getSubobjectType().getClass()
77 + ". Needed IpPrefixSubobject.");
79 final IpPrefixSubobject specObj = (IpPrefixSubobject) subobject.getSubobjectType();
80 final IpPrefix prefix = specObj.getIpPrefix();
82 if (prefix.getIpv4Prefix() != null) {
83 final byte[] retBytes = new byte[CONTENT4_LENGTH];
84 ByteArray.copyWhole(prefix.getIpv4Prefix().getValue().getBytes(), retBytes, 0);
85 retBytes[PREFIX_F_OFFSET] = ByteArray.intToBytes(Ipv4Util.getPrefixLength(prefix))[Integer.SIZE / Byte.SIZE - 1];
87 } else if (prefix.getIpv6Prefix() != null) {
88 final byte[] retBytes = new byte[CONTENT_LENGTH];
89 ByteArray.copyWhole(prefix.getIpv6Prefix().getValue().getBytes(), retBytes, 0);
90 retBytes[PREFIX_F_OFFSET] = ByteArray.intToBytes(Ipv4Util.getPrefixLength(prefix))[Integer.SIZE / Byte.SIZE - 1];
93 throw new IllegalArgumentException("No valid IpPrefix");
97 public int getType() {
101 public int getType6() {