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.util.ByteArray;
11 import org.opendaylight.protocol.concepts.Ipv4Util;
12 import org.opendaylight.protocol.concepts.Ipv6Util;
13 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
14 import org.opendaylight.protocol.pcep.spi.SubobjectParser;
15 import org.opendaylight.protocol.pcep.spi.SubobjectSerializer;
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.rsvp.rev130820.CSubobject;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.IpPrefixSubobject;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.route.subobjects.subobject.type.IpPrefixBuilder;
21 import com.google.common.primitives.UnsignedBytes;
24 * Parser for {@link IpPrefixSubobject}
26 public class EROIpPrefixSubobjectParser implements SubobjectParser, SubobjectSerializer {
28 public static final int TYPE = 32;
30 public static final int IP4_F_LENGTH = 4;
31 public static final int PREFIX4_F_LENGTH = 1;
33 public static final int PREFIX4_F_OFFSET = IP4_F_LENGTH;
35 public static final int CONTENT4_LENGTH = PREFIX4_F_OFFSET + PREFIX4_F_LENGTH + 1; //added reserved field of size 1 byte
37 public static final int IP_F_LENGTH = 16;
38 public static final int PREFIX_F_LENGTH = 1;
40 public static final int IP_F_OFFSET = 0;
41 public static final int PREFIX_F_OFFSET = IP_F_OFFSET + IP_F_LENGTH;
43 public static final int CONTENT_LENGTH = PREFIX_F_OFFSET + PREFIX_F_LENGTH + 1; // added reserved field of size 1 byte
45 public IpPrefixSubobject parseSubobject(byte[] buffer) throws PCEPDeserializerException {
46 if (buffer == null || buffer.length == 0)
47 throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
49 if (buffer.length == CONTENT4_LENGTH) {
50 final int length = UnsignedBytes.toInt(buffer[PREFIX4_F_OFFSET]);
51 return new IpPrefixBuilder().setIpPrefix(new IpPrefix(Ipv4Util.prefixForBytes(ByteArray.subByte(buffer, 0, IP4_F_LENGTH), length))).build();
52 } else if (buffer.length == CONTENT_LENGTH) {
53 final int length = UnsignedBytes.toInt(buffer[PREFIX_F_OFFSET]);
54 return new IpPrefixBuilder().setIpPrefix(new IpPrefix(Ipv6Util.prefixForBytes(ByteArray.subByte(buffer, 0, IP_F_LENGTH), length))).build();
56 throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.length + ";");
59 public byte[] serializeSubobject(CSubobject subobject) {
60 if (!(subobject instanceof IpPrefixSubobject))
61 throw new IllegalArgumentException("Unknown subobject instance. Passed " + subobject.getClass()
62 + ". Needed IpPrefixSubobject.");
64 final IpPrefixSubobject specObj = (IpPrefixSubobject) subobject;
65 final IpPrefix prefix = specObj.getIpPrefix();
67 if (prefix.getIpv4Prefix() != null) {
68 final byte[] retBytes = new byte[CONTENT4_LENGTH];
69 ByteArray.copyWhole(prefix.getIpv4Prefix().getValue().getBytes(), retBytes, 0);
71 } else if (prefix.getIpv6Prefix() != null) {
72 final byte[] retBytes = new byte[CONTENT4_LENGTH];
73 ByteArray.copyWhole(prefix.getIpv4Prefix().getValue().getBytes(), retBytes, 0);
75 //retBytes[PREFIX_F_OFFSET] = ByteArray.intToBytes(prefix.getLength())[Integer.SIZE / Byte.SIZE - 1];
81 public int getType() {