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.Arrays;
11 import java.util.BitSet;
13 import org.opendaylight.protocol.concepts.Ipv4Util;
14 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
15 import org.opendaylight.protocol.pcep.spi.RROSubobjectParser;
16 import org.opendaylight.protocol.pcep.spi.RROSubobjectSerializer;
17 import org.opendaylight.protocol.util.ByteArray;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.reported.route.object.Subobjects;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.reported.route.object.SubobjectsBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.IpPrefixSubobject;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.record.route.subobjects.subobject.type.IpPrefixBuilder;
24 import com.google.common.primitives.UnsignedBytes;
27 * Parser for {@link org.opendaylight.protocol.pcep.subobject.RROIPAddressSubobject RROIPAddressSubobject<IPv4Prefix>}
29 public class RROIpPrefixSubobjectParser implements RROSubobjectParser, RROSubobjectSerializer {
31 public static final int TYPE = 1;
33 public static final int TYPE6 = 2;
35 public static final int IP4_F_LENGTH = 4;
36 public static final int PREFIX4_F_LENGTH = 1;
37 public static final int FLAGS4_F_LENGTH = 1;
39 public static final int IP4_F_OFFSET = 0;
40 public static final int PREFIX4_F_OFFSET = IP4_F_OFFSET + IP4_F_LENGTH;
41 public static final int FLAGS4_F_OFFSET = PREFIX4_F_OFFSET + PREFIX4_F_LENGTH;
43 public static final int CONTENT4_LENGTH = FLAGS4_F_OFFSET + FLAGS4_F_LENGTH;
45 public static final int IP_F_LENGTH = 16;
46 public static final int PREFIX_F_LENGTH = 1;
47 public static final int FLAGS_F_LENGTH = 1;
49 public static final int IP_F_OFFSET = 0;
50 public static final int PREFIX_F_OFFSET = IP_F_OFFSET + IP_F_LENGTH;
51 public static final int FLAGS_F_OFFSET = PREFIX_F_OFFSET + PREFIX_F_LENGTH;
53 public static final int CONTENT_LENGTH = FLAGS_F_OFFSET + FLAGS_F_LENGTH;
56 * flags offset in bits
58 public static final int LPA_F_OFFSET = 7;
59 public static final int LPIU_F_OFFSET = 6;
62 public Subobjects parseSubobject(final byte[] buffer) throws PCEPDeserializerException {
63 if (buffer == null || buffer.length == 0)
64 throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
65 if (buffer.length != CONTENT4_LENGTH || buffer.length != CONTENT_LENGTH)
66 throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.length + ";");
68 final int length = UnsignedBytes.toInt(buffer[PREFIX4_F_OFFSET]);
70 final BitSet flags = ByteArray.bytesToBitSet(Arrays.copyOfRange(buffer, FLAGS4_F_OFFSET, FLAGS4_F_OFFSET + FLAGS4_F_LENGTH));
72 final SubobjectsBuilder builder = new SubobjectsBuilder();
73 builder.setProtectionAvailable(flags.get(LPA_F_OFFSET));
74 builder.setProtectionInUse(flags.get(LPIU_F_OFFSET));
75 builder.setSubobjectType(new IpPrefixBuilder().setIpPrefix(
76 new IpPrefix(Ipv4Util.prefixForBytes(ByteArray.subByte(buffer, IP4_F_OFFSET, IP4_F_LENGTH), length))).build());
78 return builder.build();
82 public byte[] serializeSubobject(final Subobjects subobject) {
83 if (!(subobject instanceof IpPrefixSubobject))
84 throw new IllegalArgumentException("Unknown ReportedRouteSubobject instance. Passed " + subobject.getClass()
85 + ". Needed RROIPAddressSubobject.");
87 final IpPrefixSubobject specObj = (IpPrefixSubobject) subobject;
88 final IpPrefix prefix = specObj.getIpPrefix();
90 if (prefix.getIpv4Prefix() == null && prefix.getIpv6Prefix() == null)
91 throw new IllegalArgumentException("Unknown AbstractPrefix instance. Passed " + prefix.getClass() + ".");
93 final BitSet flags = new BitSet(FLAGS4_F_LENGTH * Byte.SIZE);
95 flags.set(LPA_F_OFFSET, subobject.isProtectionAvailable());
96 flags.set(LPIU_F_OFFSET, subobject.isProtectionInUse());
98 final byte[] retBytes = new byte[CONTENT4_LENGTH];
100 if (prefix.getIpv4Prefix() != null) {
101 ByteArray.copyWhole(prefix.getIpv4Prefix().getValue().getBytes(), retBytes, IP4_F_OFFSET);
102 retBytes[PREFIX4_F_OFFSET] = ByteArray.intToBytes(Ipv4Util.getPrefixLength(prefix))[Integer.SIZE / Byte.SIZE - 1];
103 ByteArray.copyWhole(ByteArray.bitSetToBytes(flags, FLAGS4_F_LENGTH), retBytes, FLAGS4_F_OFFSET);
105 ByteArray.copyWhole(prefix.getIpv6Prefix().getValue().getBytes(), retBytes, IP_F_OFFSET);
106 retBytes[PREFIX_F_OFFSET] = ByteArray.intToBytes(Ipv4Util.getPrefixLength(prefix))[Integer.SIZE / Byte.SIZE - 1];
107 ByteArray.copyWhole(ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH), retBytes, FLAGS_F_OFFSET);
113 public int getType() {
117 public int getType6() {