Removing deprecated getType() method from serializers.
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / subobject / RROIpv6PrefixSubobjectParser.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 java.util.Arrays;
11 import java.util.BitSet;
12
13 import org.opendaylight.protocol.concepts.Ipv4Util;
14 import org.opendaylight.protocol.concepts.Ipv6Util;
15 import org.opendaylight.protocol.pcep.impl.object.RROSubobjectUtil;
16 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
17 import org.opendaylight.protocol.pcep.spi.RROSubobjectParser;
18 import org.opendaylight.protocol.pcep.spi.RROSubobjectSerializer;
19 import org.opendaylight.protocol.util.ByteArray;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.reported.route.object.rro.Subobject;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.reported.route.object.rro.SubobjectBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.IpPrefixSubobject;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.record.route.subobjects.subobject.type.IpPrefixCase;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.record.route.subobjects.subobject.type.IpPrefixCaseBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.record.route.subobjects.subobject.type.ip.prefix._case.IpPrefixBuilder;
27
28 import com.google.common.primitives.UnsignedBytes;
29
30 /**
31  * Parser for {@link IpPrefixCase}
32  */
33 public class RROIpv6PrefixSubobjectParser implements RROSubobjectParser, RROSubobjectSerializer {
34
35         public static final int TYPE = 2;
36
37         private static final int IP_F_LENGTH = 16;
38
39         private static final int PREFIX_F_LENGTH = 1;
40         private static final int FLAGS_F_LENGTH = 1;
41
42         private static final int IP_F_OFFSET = 0;
43
44         private static final int PREFIX_F_OFFSET = IP_F_OFFSET + IP_F_LENGTH;
45         private static final int FLAGS_F_OFFSET = PREFIX_F_OFFSET + PREFIX_F_LENGTH;
46
47         private static final int CONTENT_LENGTH = IP_F_LENGTH + PREFIX_F_LENGTH + FLAGS_F_LENGTH;
48
49         private static final int LPA_F_OFFSET = 7;
50         private static final int LPIU_F_OFFSET = 6;
51
52         @Override
53         public Subobject parseSubobject(final byte[] buffer) throws PCEPDeserializerException {
54                 if (buffer == null || buffer.length == 0) {
55                         throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
56                 }
57                 final SubobjectBuilder builder = new SubobjectBuilder();
58                 if (buffer.length != CONTENT_LENGTH) {
59                         throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.length + ";");
60                 }
61                 final int length = UnsignedBytes.toInt(buffer[PREFIX_F_OFFSET]);
62                 final BitSet flags = ByteArray.bytesToBitSet(Arrays.copyOfRange(buffer, FLAGS_F_OFFSET, FLAGS_F_OFFSET + FLAGS_F_LENGTH));
63                 builder.setProtectionAvailable(flags.get(LPA_F_OFFSET));
64                 builder.setProtectionInUse(flags.get(LPIU_F_OFFSET));
65                 builder.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(
66                                 new IpPrefixBuilder().setIpPrefix(
67                                                 new IpPrefix(Ipv6Util.prefixForBytes(ByteArray.subByte(buffer, IP_F_OFFSET, IP_F_LENGTH), length))).build()).build());
68                 return builder.build();
69         }
70
71         @Override
72         public byte[] serializeSubobject(final Subobject subobject) {
73                 if (!(subobject.getSubobjectType() instanceof IpPrefixCase)) {
74                         throw new IllegalArgumentException("Unknown ReportedRouteSubobject instance. Passed " + subobject.getClass()
75                                         + ". Needed IpPrefixCase.");
76                 }
77                 final IpPrefixSubobject specObj = ((IpPrefixCase) subobject.getSubobjectType()).getIpPrefix();
78                 final IpPrefix prefix = specObj.getIpPrefix();
79                 final BitSet flags = new BitSet(FLAGS_F_LENGTH * Byte.SIZE);
80                 flags.set(LPA_F_OFFSET, subobject.isProtectionAvailable());
81                 flags.set(LPIU_F_OFFSET, subobject.isProtectionInUse());
82                 final byte[] retBytes = new byte[CONTENT_LENGTH];
83                 ByteArray.copyWhole(Ipv6Util.bytesForPrefix(prefix.getIpv6Prefix()), retBytes, IP_F_OFFSET);
84                 retBytes[PREFIX_F_OFFSET] = UnsignedBytes.checkedCast(Ipv4Util.getPrefixLength(prefix));
85                 ByteArray.copyWhole(ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH), retBytes, FLAGS_F_OFFSET);
86                 return RROSubobjectUtil.formatSubobject(TYPE, retBytes);
87         }
88 }