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.impl.object.XROSubobjectUtil;
13 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
14 import org.opendaylight.protocol.pcep.spi.XROSubobjectParser;
15 import org.opendaylight.protocol.pcep.spi.XROSubobjectSerializer;
16 import org.opendaylight.protocol.util.ByteArray;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.Subobject;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.SubobjectBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.ExcludeRouteSubobjects.Attribute;
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.basic.explicit.route.subobjects.subobject.type.IpPrefixCase;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.IpPrefixCaseBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.ip.prefix._case.IpPrefixBuilder;
26 import com.google.common.primitives.UnsignedBytes;
29 * Parser for {@link IpPrefixCase}
31 public class XROIpv6PrefixSubobjectParser implements XROSubobjectParser, XROSubobjectSerializer {
33 public static final int TYPE = 2;
35 private static final int PREFIX_F_LENGTH = 1;
36 private static final int ATTRIBUTE_LENGTH = 1;
38 private static final int IP_F_OFFSET = 0;
40 private static final int IP6_F_LENGTH = 16;
41 private static final int PREFIX6_F_OFFSET = IP_F_OFFSET + IP6_F_LENGTH;
42 private static final int ATTRIBUTE6_OFFSET = PREFIX6_F_OFFSET + PREFIX_F_LENGTH;
44 private static final int CONTENT6_LENGTH = ATTRIBUTE6_OFFSET + ATTRIBUTE_LENGTH;
47 public Subobject parseSubobject(final byte[] buffer, final boolean mandatory) throws PCEPDeserializerException {
48 if (buffer == null || buffer.length == 0) {
49 throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
51 if (buffer.length != CONTENT6_LENGTH) {
52 throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.length + ";");
54 final SubobjectBuilder builder = new SubobjectBuilder();
55 builder.setMandatory(mandatory);
56 final int length = UnsignedBytes.toInt(buffer[PREFIX6_F_OFFSET]);
57 builder.setAttribute(Attribute.forValue(UnsignedBytes.toInt(buffer[ATTRIBUTE6_OFFSET])));
58 builder.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(
59 new IpPrefixBuilder().setIpPrefix(
60 new IpPrefix(Ipv6Util.prefixForBytes(ByteArray.subByte(buffer, IP_F_OFFSET, IP6_F_LENGTH), length))).build()).build());
61 return builder.build();
65 public byte[] serializeSubobject(final Subobject subobject) {
66 if (!(subobject.getSubobjectType() instanceof IpPrefixCase)) {
67 throw new IllegalArgumentException("Unknown PCEPXROSubobject instance. Passed " + subobject.getSubobjectType().getClass()
68 + ". Needed IpPrefixCase.");
70 final IpPrefixSubobject specObj = ((IpPrefixCase) subobject.getSubobjectType()).getIpPrefix();
71 final IpPrefix prefix = specObj.getIpPrefix();
73 if (prefix.getIpv6Prefix() == null) {
74 throw new IllegalArgumentException("Unknown AbstractPrefix instance. Passed " + prefix.getClass() + ".");
76 final byte[] retBytes = new byte[CONTENT6_LENGTH];
77 ByteArray.copyWhole(Ipv6Util.bytesForPrefix(prefix.getIpv6Prefix()), retBytes, IP_F_OFFSET);
78 retBytes[PREFIX6_F_OFFSET] = UnsignedBytes.checkedCast(Ipv4Util.getPrefixLength(prefix));
79 retBytes[ATTRIBUTE6_OFFSET] = UnsignedBytes.checkedCast(subobject.getAttribute().getIntValue());
80 return XROSubobjectUtil.formatSubobject(TYPE, subobject.isMandatory(), retBytes);
84 public int getType() {