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