927eaab0a6494284685acc85455c93702b40f0a3
[bgpcep.git] / bgp / inet / src / main / java / org / opendaylight / protocol / bgp / inet / codec / nexthop / Ipv4NextHopParserSerializer.java
1 /*
2  * Copyright (c) 2015 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
9 package org.opendaylight.protocol.bgp.inet.codec.nexthop;
10
11 import com.google.common.base.Preconditions;
12 import io.netty.buffer.ByteBuf;
13 import org.opendaylight.bgp.concepts.NextHopUtil;
14 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
15 import org.opendaylight.protocol.bgp.parser.spi.NextHopParserSerializer;
16 import org.opendaylight.protocol.util.Ipv4Util;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.next.hop.CNextHop;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.next.hop.c.next.hop.Ipv4NextHopCase;
19
20 public final class Ipv4NextHopParserSerializer implements NextHopParserSerializer {
21     @Override
22     public CNextHop parseNextHop(final ByteBuf buffer) throws BGPParsingException {
23         Preconditions.checkArgument(buffer.readableBytes() == Ipv4Util.IP4_LENGTH,
24                 "Length of byte array for NEXT_HOP should be %s, but is %s",
25                 buffer.readableBytes(), Ipv4Util.IP4_LENGTH);
26         return NextHopUtil.parseNextHop(buffer);
27     }
28
29     @Override
30     public void serializeNextHop(final CNextHop nextHop, final ByteBuf byteAggregator) {
31         Preconditions.checkArgument(nextHop instanceof Ipv4NextHopCase,
32                 "cNextHop is not a Ipv4 NextHop object.");
33         NextHopUtil.serializeNextHop(nextHop, byteAggregator);
34     }
35 }