BUG-64 : initial rewrite, EROSubobjectRegistry.
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / subobject / XROIpv6PrefixSubobjectParser.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.protocol.pcep.impl.subobject;
9
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;
25
26 import com.google.common.primitives.UnsignedBytes;
27
28 /**
29  * Parser for {@link IpPrefixCase}
30  */
31 public class XROIpv6PrefixSubobjectParser implements XROSubobjectParser, XROSubobjectSerializer {
32
33         public static final int TYPE = 2;
34
35         private static final int PREFIX_F_LENGTH = 1;
36         private static final int ATTRIBUTE_LENGTH = 1;
37
38         private static final int IP_F_OFFSET = 0;
39
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;
43
44         private static final int CONTENT6_LENGTH = ATTRIBUTE6_OFFSET + ATTRIBUTE_LENGTH;
45
46         @Override
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.");
50                 }
51                 if (buffer.length != CONTENT6_LENGTH) {
52                         throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.length + ";");
53                 }
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();
62         }
63
64         @Override
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.");
69                 }
70                 final IpPrefixSubobject specObj = ((IpPrefixCase) subobject.getSubobjectType()).getIpPrefix();
71                 final IpPrefix prefix = specObj.getIpPrefix();
72
73                 if (prefix.getIpv6Prefix() == null) {
74                         throw new IllegalArgumentException("Unknown AbstractPrefix instance. Passed " + prefix.getClass() + ".");
75                 }
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);
81         }
82
83         @Override
84         public int getType() {
85                 return TYPE;
86         }
87 }