cb7a65f620738fd64cc716060aad4be81ce3493c
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / subobject / EROIpv4PrefixSubobjectParser.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 com.google.common.base.Preconditions;
11 import com.google.common.primitives.UnsignedBytes;
12
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
15
16 import org.opendaylight.protocol.concepts.Ipv4Util;
17 import org.opendaylight.protocol.pcep.spi.EROSubobjectParser;
18 import org.opendaylight.protocol.pcep.spi.EROSubobjectSerializer;
19 import org.opendaylight.protocol.pcep.spi.EROSubobjectUtil;
20 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
21 import org.opendaylight.protocol.util.ByteArray;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.Subobject;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.SubobjectBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.IpPrefixSubobject;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.IpPrefixCase;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.IpPrefixCaseBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.ip.prefix._case.IpPrefixBuilder;
29
30 /**
31  * Parser for {@link IpPrefixCase}
32  */
33 public class EROIpv4PrefixSubobjectParser implements EROSubobjectParser, EROSubobjectSerializer {
34
35     public static final int TYPE = 1;
36
37     private static final int PREFIX4_F_OFFSET = Ipv4Util.IP4_LENGTH;
38
39     private static final int RESERVED = 1;
40
41     private static final int CONTENT4_LENGTH = PREFIX4_F_OFFSET + RESERVED + 1;
42
43     @Override
44     public Subobject parseSubobject(final ByteBuf buffer, final boolean loose) throws PCEPDeserializerException {
45         Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
46         final SubobjectBuilder builder = new SubobjectBuilder();
47         builder.setLoose(loose);
48         if (buffer.readableBytes() != CONTENT4_LENGTH) {
49             throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + ";");
50         }
51         final int length = UnsignedBytes.toInt(buffer.getByte(PREFIX4_F_OFFSET));
52         IpPrefixBuilder prefix = new IpPrefixBuilder().setIpPrefix(new IpPrefix(Ipv4Util.prefixForBytes(ByteArray.readBytes(buffer,
53                 Ipv4Util.IP4_LENGTH), length)));
54         builder.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(prefix.build()).build());
55         return builder.build();
56     }
57
58     @Override
59     public void serializeSubobject(final Subobject subobject, final ByteBuf buffer) {
60         Preconditions.checkArgument(subobject.getSubobjectType() instanceof IpPrefixCase, "Unknown subobject instance. Passed %s. Needed IpPrefixCase.", subobject.getSubobjectType().getClass());
61         final IpPrefixSubobject specObj = ((IpPrefixCase) subobject.getSubobjectType()).getIpPrefix();
62         final IpPrefix prefix = specObj.getIpPrefix();
63         Preconditions.checkArgument(prefix.getIpv4Prefix() != null || prefix.getIpv6Prefix() != null, "Unknown AbstractPrefix instance. Passed %s.", prefix.getClass());
64         if (prefix.getIpv6Prefix() != null) {
65             new EROIpv6PrefixSubobjectParser().serializeSubobject(subobject, buffer);
66         } else {
67             final ByteBuf body = Unpooled.buffer(CONTENT4_LENGTH);
68             body.writeBytes(Ipv4Util.bytesForPrefix(prefix.getIpv4Prefix()));
69             body.writeZero(RESERVED);
70             EROSubobjectUtil.formatSubobject(TYPE, subobject.isLoose(), body, buffer);
71         }
72     }
73 }