764fbff5955f81d91744dae773a2fd2b273761fc
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / subobject / EROIpv6PrefixSubobjectParser.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.EROSubobjectUtil;
13 import org.opendaylight.protocol.pcep.spi.EROSubobjectParser;
14 import org.opendaylight.protocol.pcep.spi.EROSubobjectSerializer;
15 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
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.explicit.route.object.ero.Subobject;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.SubobjectBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.IpPrefixSubobject;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.IpPrefixCase;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.IpPrefixCaseBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.ip.prefix._case.IpPrefixBuilder;
24
25 import com.google.common.primitives.UnsignedBytes;
26
27 /**
28  * Parser for {@link IpPrefixCase}
29  */
30 public class EROIpv6PrefixSubobjectParser implements EROSubobjectParser, EROSubobjectSerializer {
31
32         public static final int TYPE = 2;
33
34         private static final int IP_F_LENGTH = 16;
35         private static final int PREFIX_F_LENGTH = 1;
36
37         private static final int IP_F_OFFSET = 0;
38         private static final int PREFIX_F_OFFSET = IP_F_OFFSET + IP_F_LENGTH;
39
40         private static final int CONTENT_LENGTH = PREFIX_F_OFFSET + PREFIX_F_LENGTH + 1;
41
42         @Override
43         public Subobject parseSubobject(final byte[] buffer, final boolean loose) throws PCEPDeserializerException {
44                 if (buffer == null || buffer.length == 0) {
45                         throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
46                 }
47                 final SubobjectBuilder builder = new SubobjectBuilder();
48                 builder.setLoose(loose);
49
50                 if (buffer.length != CONTENT_LENGTH) {
51                         throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.length + ";");
52                 }
53                 final int length = UnsignedBytes.toInt(buffer[PREFIX_F_OFFSET]);
54                 builder.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(
55                                 new IpPrefixBuilder().setIpPrefix(new IpPrefix(Ipv6Util.prefixForBytes(ByteArray.subByte(buffer, 0, IP_F_LENGTH), length))).build()).build());
56                 return builder.build();
57         }
58
59         @Override
60         public byte[] serializeSubobject(final Subobject subobject) {
61                 if (!(subobject.getSubobjectType() instanceof IpPrefixCase)) {
62                         throw new IllegalArgumentException("Unknown subobject instance. Passed " + subobject.getSubobjectType().getClass()
63                                         + ". Needed IpPrefixCase.");
64                 }
65                 final IpPrefixSubobject specObj = ((IpPrefixCase) subobject.getSubobjectType()).getIpPrefix();
66                 final IpPrefix prefix = specObj.getIpPrefix();
67
68                 final byte[] retBytes = new byte[CONTENT_LENGTH];
69                 ByteArray.copyWhole(Ipv6Util.bytesForPrefix(prefix.getIpv6Prefix()), retBytes, 0);
70                 retBytes[PREFIX_F_OFFSET] = UnsignedBytes.checkedCast(Ipv4Util.getPrefixLength(prefix));
71                 return EROSubobjectUtil.formatSubobject(TYPE, subobject.isLoose(), retBytes);
72         }
73 }