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.impl.object.RROSubobjectUtil;
15 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
16 import org.opendaylight.protocol.pcep.spi.RROSubobjectParser;
17 import org.opendaylight.protocol.pcep.spi.RROSubobjectSerializer;
18 import org.opendaylight.protocol.util.ByteArray;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.reported.route.object.rro.Subobject;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.reported.route.object.rro.SubobjectBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.IpPrefixSubobject;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.record.route.subobjects.subobject.type.IpPrefixCase;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.record.route.subobjects.subobject.type.IpPrefixCaseBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.record.route.subobjects.subobject.type.ip.prefix._case.IpPrefixBuilder;
27 import com.google.common.primitives.UnsignedBytes;
30 * Parser for {@link IpPrefixCase}
32 public class RROIpv4PrefixSubobjectParser implements RROSubobjectParser, RROSubobjectSerializer {
34 public static final int TYPE = 1;
36 private static final int IP4_F_LENGTH = 4;
38 private static final int PREFIX_F_LENGTH = 1;
39 private static final int FLAGS_F_LENGTH = 1;
41 private static final int IP_F_OFFSET = 0;
43 private static final int PREFIX4_F_OFFSET = IP_F_OFFSET + IP4_F_LENGTH;
44 private static final int FLAGS4_F_OFFSET = PREFIX4_F_OFFSET + PREFIX_F_LENGTH;
46 private static final int CONTENT4_LENGTH = IP4_F_LENGTH + PREFIX_F_LENGTH + FLAGS_F_LENGTH;
48 private static final int LPA_F_OFFSET = 7;
49 private static final int LPIU_F_OFFSET = 6;
52 public Subobject parseSubobject(final byte[] buffer) throws PCEPDeserializerException {
53 if (buffer == null || buffer.length == 0) {
54 throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
56 if (buffer.length != CONTENT4_LENGTH) {
57 throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.length + ";");
59 final SubobjectBuilder builder = new SubobjectBuilder();
60 final int length = UnsignedBytes.toInt(buffer[PREFIX4_F_OFFSET]);
61 final BitSet flags = ByteArray.bytesToBitSet(Arrays.copyOfRange(buffer, FLAGS4_F_OFFSET, FLAGS4_F_OFFSET + FLAGS_F_LENGTH));
62 builder.setProtectionAvailable(flags.get(LPA_F_OFFSET));
63 builder.setProtectionInUse(flags.get(LPIU_F_OFFSET));
64 builder.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(
65 new IpPrefixBuilder().setIpPrefix(
66 new IpPrefix(Ipv4Util.prefixForBytes(ByteArray.subByte(buffer, IP_F_OFFSET, IP4_F_LENGTH), length))).build()).build());
67 return builder.build();
71 public byte[] serializeSubobject(final Subobject subobject) {
72 if (!(subobject.getSubobjectType() instanceof IpPrefixCase)) {
73 throw new IllegalArgumentException("Unknown ReportedRouteSubobject instance. Passed " + subobject.getClass()
74 + ". Needed IpPrefixCase.");
76 final IpPrefixSubobject specObj = ((IpPrefixCase) subobject.getSubobjectType()).getIpPrefix();
77 final IpPrefix prefix = specObj.getIpPrefix();
79 if (prefix.getIpv4Prefix() == null && prefix.getIpv6Prefix() == null) {
80 throw new IllegalArgumentException("Unknown AbstractPrefix instance. Passed " + prefix.getClass() + ".");
82 final BitSet flags = new BitSet(FLAGS_F_LENGTH * Byte.SIZE);
83 flags.set(LPA_F_OFFSET, subobject.isProtectionAvailable());
84 flags.set(LPIU_F_OFFSET, subobject.isProtectionInUse());
86 if (prefix.getIpv6Prefix() != null) {
87 return new RROIpv6PrefixSubobjectParser().serializeSubobject(subobject);
89 final byte[] retBytes = new byte[CONTENT4_LENGTH];
90 ByteArray.copyWhole(Ipv4Util.bytesForPrefix(prefix.getIpv4Prefix()), retBytes, IP_F_OFFSET);
91 retBytes[PREFIX4_F_OFFSET] = UnsignedBytes.checkedCast(Ipv4Util.getPrefixLength(prefix));
92 ByteArray.copyWhole(ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH), retBytes, FLAGS4_F_OFFSET);
93 return RROSubobjectUtil.formatSubobject(TYPE, retBytes);
97 public int getType() {