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