Moved Ipv4Util and Ipv6Util from concepts to util
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / 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.impl.subobject;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.primitives.UnsignedBytes;
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.rev100924.IpPrefix;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.Subobject;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.SubobjectBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.ExcludeRouteSubobjects.Attribute;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.IpPrefixSubobject;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.IpPrefixCase;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.IpPrefixCaseBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.ip.prefix._case.IpPrefixBuilder;
28
29 /**
30  * Parser for {@link IpPrefixCase}
31  */
32 public class XROIpv4PrefixSubobjectParser implements XROSubobjectParser, XROSubobjectSerializer {
33
34     public static final int TYPE = 1;
35
36     private static final int PREFIX_F_LENGTH = 1;
37
38     private static final int PREFIX4_F_OFFSET = Ipv4Util.IP4_LENGTH;
39
40     private static final int CONTENT4_LENGTH = PREFIX4_F_OFFSET + PREFIX_F_LENGTH + 1;
41
42     @Override
43     public Subobject parseSubobject(final ByteBuf buffer, final boolean mandatory) throws PCEPDeserializerException {
44         Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
45         final SubobjectBuilder builder = new SubobjectBuilder();
46         builder.setMandatory(mandatory);
47         if (buffer.readableBytes() != CONTENT4_LENGTH) {
48             throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + ";");
49         }
50         final int length = UnsignedBytes.toInt(buffer.getByte(PREFIX4_F_OFFSET));
51         IpPrefixBuilder prefix = new IpPrefixBuilder().setIpPrefix(new IpPrefix(Ipv4Util.prefixForBytes(ByteArray.readBytes(buffer,
52                 Ipv4Util.IP4_LENGTH), length)));
53         builder.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(prefix.build()).build());
54         buffer.readerIndex(buffer.readerIndex() + PREFIX_F_LENGTH);
55         builder.setAttribute(Attribute.forValue(UnsignedBytes.toInt(buffer.readByte())));
56         return builder.build();
57     }
58
59     @Override
60     public void serializeSubobject(final Subobject subobject, final ByteBuf buffer) {
61         Preconditions.checkArgument(subobject.getSubobjectType() instanceof IpPrefixCase, "Unknown subobject instance. Passed %s. Needed IpPrefixCase.", subobject.getSubobjectType().getClass());
62         final IpPrefixSubobject specObj = ((IpPrefixCase) subobject.getSubobjectType()).getIpPrefix();
63         final IpPrefix prefix = specObj.getIpPrefix();
64         Preconditions.checkArgument(prefix.getIpv4Prefix() != null || prefix.getIpv6Prefix() != null, "Unknown AbstractPrefix instance. Passed %s.", prefix.getClass());
65         if (prefix.getIpv6Prefix() != null) {
66             new XROIpv6PrefixSubobjectParser().serializeSubobject(subobject, buffer);
67         } else {
68             final ByteBuf body = Unpooled.buffer(CONTENT4_LENGTH);
69             body.writeBytes(Ipv4Util.bytesForPrefix(prefix.getIpv4Prefix()));
70             body.writeByte(subobject.getAttribute().getIntValue());
71             XROSubobjectUtil.formatSubobject(TYPE, subobject.isMandatory(), body, buffer);
72         }
73     }
74 }