d584108fe2d6791f60eb960a94cc7cea88c64a3f
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / subobject / RROIpv4PrefixSubobjectParser.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.pcep.spi.PCEPDeserializerException;
15 import org.opendaylight.protocol.pcep.spi.RROSubobjectParser;
16 import org.opendaylight.protocol.pcep.spi.RROSubobjectSerializer;
17 import org.opendaylight.protocol.util.ByteArray;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.reported.route.object.rro.Subobjects;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.reported.route.object.rro.SubobjectsBuilder;
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.record.route.subobjects.subobject.type.IpPrefixCase;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.record.route.subobjects.subobject.type.IpPrefixCaseBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.record.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 RROIpv4PrefixSubobjectParser implements RROSubobjectParser, RROSubobjectSerializer {
32
33         public static final int TYPE = 1;
34
35         private static final int IP4_F_LENGTH = 4;
36
37         private static final int PREFIX_F_LENGTH = 1;
38         private static final int FLAGS_F_LENGTH = 1;
39
40         private static final int IP_F_OFFSET = 0;
41
42         private static final int PREFIX4_F_OFFSET = IP_F_OFFSET + IP4_F_LENGTH;
43         private static final int FLAGS4_F_OFFSET = PREFIX4_F_OFFSET + PREFIX_F_LENGTH;
44
45         private static final int CONTENT4_LENGTH = IP4_F_LENGTH + PREFIX_F_LENGTH + FLAGS_F_LENGTH;
46
47         private static final int LPA_F_OFFSET = 7;
48         private static final int LPIU_F_OFFSET = 6;
49
50         @Override
51         public Subobjects parseSubobject(final byte[] buffer) throws PCEPDeserializerException {
52                 if (buffer == null || buffer.length == 0) {
53                         throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
54                 }
55                 if (buffer.length != CONTENT4_LENGTH) {
56                         throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.length + ";");
57                 }
58                 final SubobjectsBuilder builder = new SubobjectsBuilder();
59                 final int length = UnsignedBytes.toInt(buffer[PREFIX4_F_OFFSET]);
60                 final BitSet flags = ByteArray.bytesToBitSet(Arrays.copyOfRange(buffer, FLAGS4_F_OFFSET, FLAGS4_F_OFFSET + FLAGS_F_LENGTH));
61                 builder.setProtectionAvailable(flags.get(LPA_F_OFFSET));
62                 builder.setProtectionInUse(flags.get(LPIU_F_OFFSET));
63                 builder.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(
64                                 new IpPrefixBuilder().setIpPrefix(
65                                                 new IpPrefix(Ipv4Util.prefixForBytes(ByteArray.subByte(buffer, IP_F_OFFSET, IP4_F_LENGTH), length))).build()).build());
66                 return builder.build();
67         }
68
69         @Override
70         public byte[] serializeSubobject(final Subobjects subobject) {
71                 if (!(subobject.getSubobjectType() instanceof IpPrefixCase)) {
72                         throw new IllegalArgumentException("Unknown ReportedRouteSubobject instance. Passed " + subobject.getClass()
73                                         + ". Needed IpPrefixCase.");
74                 }
75                 final IpPrefixSubobject specObj = ((IpPrefixCase) subobject.getSubobjectType()).getIpPrefix();
76                 final IpPrefix prefix = specObj.getIpPrefix();
77
78                 if (prefix.getIpv4Prefix() == null && prefix.getIpv6Prefix() == null) {
79                         throw new IllegalArgumentException("Unknown AbstractPrefix instance. Passed " + prefix.getClass() + ".");
80                 }
81                 final BitSet flags = new BitSet(FLAGS_F_LENGTH * Byte.SIZE);
82                 flags.set(LPA_F_OFFSET, subobject.isProtectionAvailable());
83                 flags.set(LPIU_F_OFFSET, subobject.isProtectionInUse());
84
85                 if (prefix.getIpv6Prefix() != null) {
86                         return new RROIpv6PrefixSubobjectParser().serializeSubobject(subobject);
87                 }
88                 final byte[] retBytes = new byte[CONTENT4_LENGTH];
89                 ByteArray.copyWhole(Ipv4Util.bytesForPrefix(prefix.getIpv4Prefix()), retBytes, IP_F_OFFSET);
90                 retBytes[PREFIX4_F_OFFSET] = UnsignedBytes.checkedCast(Ipv4Util.getPrefixLength(prefix));
91                 ByteArray.copyWhole(ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH), retBytes, FLAGS4_F_OFFSET);
92                 return retBytes;
93         }
94
95         @Override
96         public int getType() {
97                 return TYPE;
98         }
99 }