Clean pcep/base-parser code
[bgpcep.git] / pcep / base-parser / src / main / java / org / opendaylight / protocol / pcep / parser / subobject / XROIpv4PrefixSubobjectParser.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.parser.subobject;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeIpv4Prefix;
12 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedByte;
13
14 import com.google.common.base.Preconditions;
15 import io.netty.buffer.ByteBuf;
16 import io.netty.buffer.Unpooled;
17 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
18 import org.opendaylight.protocol.pcep.spi.XROSubobjectParser;
19 import org.opendaylight.protocol.pcep.spi.XROSubobjectSerializer;
20 import org.opendaylight.protocol.pcep.spi.XROSubobjectUtil;
21 import org.opendaylight.protocol.util.ByteArray;
22 import org.opendaylight.protocol.util.Ipv4Util;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.exclude.route.object.xro.Subobject;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.exclude.route.object.xro.SubobjectBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.ExcludeRouteSubobjects.Attribute;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.IpPrefixSubobject;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.IpPrefixCase;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.IpPrefixCaseBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.ip.prefix._case.IpPrefixBuilder;
31 import org.opendaylight.yangtools.yang.common.Uint8;
32
33 /**
34  * Parser for {@link IpPrefixCase}.
35  */
36 public class XROIpv4PrefixSubobjectParser implements XROSubobjectParser, XROSubobjectSerializer {
37
38     public static final int TYPE = 1;
39
40     private static final int PREFIX_F_LENGTH = 1;
41
42     private static final int PREFIX4_F_OFFSET = Ipv4Util.IP4_LENGTH;
43
44     private static final int CONTENT4_LENGTH = PREFIX4_F_OFFSET + PREFIX_F_LENGTH + 1;
45
46     @Override
47     public Subobject parseSubobject(final ByteBuf buffer, final boolean mandatory) throws PCEPDeserializerException {
48         checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Cannot be null or empty.");
49         if (buffer.readableBytes() != CONTENT4_LENGTH) {
50             throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes()
51                 + ";");
52         }
53         final int length = buffer.getUnsignedByte(PREFIX4_F_OFFSET);
54         final IpPrefixBuilder prefix = new IpPrefixBuilder()
55                 .setIpPrefix(new IpPrefix(Ipv4Util.prefixForBytes(ByteArray.readBytes(buffer, Ipv4Util.IP4_LENGTH),
56                     length)));
57         buffer.skipBytes(PREFIX_F_LENGTH);
58         final SubobjectBuilder builder = new SubobjectBuilder()
59                 .setMandatory(mandatory)
60                 .setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(prefix.build()).build())
61                 .setAttribute(Attribute.forValue(buffer.readUnsignedByte()));
62         return builder.build();
63     }
64
65     @Override
66     public void serializeSubobject(final Subobject subobject, final ByteBuf buffer) {
67         checkArgument(subobject.getSubobjectType() instanceof IpPrefixCase,
68             "Unknown subobject instance. Passed %s. Needed IpPrefixCase.", subobject.getSubobjectType().getClass());
69         final IpPrefixSubobject specObj = ((IpPrefixCase) subobject.getSubobjectType()).getIpPrefix();
70         final IpPrefix prefix = specObj.getIpPrefix();
71         checkArgument(prefix.getIpv4Prefix() != null || prefix.getIpv6Prefix() != null,
72                 "Unknown AbstractPrefix instance. Passed %s.", prefix.getClass());
73         if (prefix.getIpv6Prefix() != null) {
74             new XROIpv6PrefixSubobjectParser().serializeSubobject(subobject, buffer);
75         } else {
76             final ByteBuf body = Unpooled.buffer(CONTENT4_LENGTH);
77             Preconditions.checkArgument(prefix.getIpv4Prefix() != null, "Ipv4Prefix is mandatory.");
78             writeIpv4Prefix(prefix.getIpv4Prefix(), body);
79             Preconditions.checkArgument(subobject.getAttribute() != null, "Attribute is mandatory.");
80             writeUnsignedByte(Uint8.valueOf(subobject.getAttribute().getIntValue()), body);
81             XROSubobjectUtil.formatSubobject(TYPE, subobject.isMandatory(), body, buffer);
82         }
83     }
84 }