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.spi.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.rro.Subobjects;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.reported.route.object.rro.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.IpPrefixCase;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.record.route.subobjects.subobject.type.IpPrefixCaseBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.record.route.subobjects.subobject.type.ip.prefix._case.IpPrefixBuilder;
26 import com.google.common.primitives.UnsignedBytes;
29 * Parser for {@link IpPrefixCase}
31 public class RROIpv4PrefixSubobjectParser implements RROSubobjectParser, RROSubobjectSerializer {
33 public static final int TYPE = 1;
35 private static final int IP4_F_LENGTH = 4;
37 private static final int PREFIX_F_LENGTH = 1;
38 private static final int FLAGS_F_LENGTH = 1;
40 private static final int IP_F_OFFSET = 0;
42 private static final int PREFIX4_F_OFFSET = IP_F_OFFSET + IP4_F_LENGTH;
43 private static final int FLAGS4_F_OFFSET = PREFIX4_F_OFFSET + PREFIX_F_LENGTH;
45 private static final int CONTENT4_LENGTH = IP4_F_LENGTH + PREFIX_F_LENGTH + FLAGS_F_LENGTH;
47 private static final int LPA_F_OFFSET = 7;
48 private static final int LPIU_F_OFFSET = 6;
51 public Subobjects parseSubobject(final byte[] buffer) throws PCEPDeserializerException {
52 if (buffer == null || buffer.length == 0) {
53 throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
55 if (buffer.length != CONTENT4_LENGTH) {
56 throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.length + ";");
58 final SubobjectsBuilder builder = new SubobjectsBuilder();
59 final int length = UnsignedBytes.toInt(buffer[PREFIX4_F_OFFSET]);
60 final BitSet flags = ByteArray.bytesToBitSet(Arrays.copyOfRange(buffer, FLAGS4_F_OFFSET, FLAGS4_F_OFFSET + FLAGS_F_LENGTH));
61 builder.setProtectionAvailable(flags.get(LPA_F_OFFSET));
62 builder.setProtectionInUse(flags.get(LPIU_F_OFFSET));
63 builder.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(
64 new IpPrefixBuilder().setIpPrefix(
65 new IpPrefix(Ipv4Util.prefixForBytes(ByteArray.subByte(buffer, IP_F_OFFSET, IP4_F_LENGTH), length))).build()).build());
66 return builder.build();
70 public byte[] serializeSubobject(final Subobjects subobject) {
71 if (!(subobject.getSubobjectType() instanceof IpPrefixCase)) {
72 throw new IllegalArgumentException("Unknown ReportedRouteSubobject instance. Passed " + subobject.getClass()
73 + ". Needed IpPrefixCase.");
75 final IpPrefixSubobject specObj = ((IpPrefixCase) subobject.getSubobjectType()).getIpPrefix();
76 final IpPrefix prefix = specObj.getIpPrefix();
78 if (prefix.getIpv4Prefix() == null && prefix.getIpv6Prefix() == null) {
79 throw new IllegalArgumentException("Unknown AbstractPrefix instance. Passed " + prefix.getClass() + ".");
81 final BitSet flags = new BitSet(FLAGS_F_LENGTH * Byte.SIZE);
82 flags.set(LPA_F_OFFSET, subobject.isProtectionAvailable());
83 flags.set(LPIU_F_OFFSET, subobject.isProtectionInUse());
85 if (prefix.getIpv6Prefix() != null) {
86 return new RROIpv6PrefixSubobjectParser().serializeSubobject(subobject);
88 final byte[] retBytes = new byte[CONTENT4_LENGTH];
89 ByteArray.copyWhole(Ipv4Util.bytesForPrefix(prefix.getIpv4Prefix()), retBytes, IP_F_OFFSET);
90 retBytes[PREFIX4_F_OFFSET] = UnsignedBytes.checkedCast(Ipv4Util.getPrefixLength(prefix));
91 ByteArray.copyWhole(ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH), retBytes, FLAGS4_F_OFFSET);
96 public int getType() {