2 * Copyright (c) 2016 Brocade Communications Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.bgp.l3vpn;
10 import com.google.common.base.Preconditions;
11 import io.netty.buffer.ByteBuf;
12 import org.opendaylight.bgp.concepts.NextHopUtil;
13 import org.opendaylight.bgp.concepts.RouteDistinguisherUtil;
14 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
15 import org.opendaylight.protocol.bgp.parser.spi.NextHopParserSerializer;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.CNextHop;
21 public abstract class AbstractVpnNextHopParserSerializer implements NextHopParserSerializer {
22 private final int ipAddrLength;
23 private final Class<?> ipNextHopCaseClazz;
25 protected AbstractVpnNextHopParserSerializer(final int ipAddrLength, final Class<?> ipNextHopCaseClazz) {
26 this.ipAddrLength = ipAddrLength;
27 this.ipNextHopCaseClazz = ipNextHopCaseClazz;
31 public CNextHop parseNextHop(final ByteBuf buffer) throws BGPParsingException {
32 Preconditions.checkArgument(buffer.readableBytes() == (this.ipAddrLength + RouteDistinguisherUtil.RD_LENGTH), "Length of byte array for NEXT_HOP should be %s, but is %s", this.ipAddrLength + RouteDistinguisherUtil.RD_LENGTH, buffer.readableBytes());
33 buffer.readBytes(RouteDistinguisherUtil.RD_LENGTH);
34 return NextHopUtil.parseNextHop(buffer.readBytes(this.ipAddrLength));
38 public void serializeNextHop(final CNextHop cNextHop, final ByteBuf byteAggregator) {
39 Preconditions.checkArgument(this.ipNextHopCaseClazz.isInstance(cNextHop), "cNextHop is not a VPN %s NextHop object.", this.ipNextHopCaseClazz.getSimpleName());
40 byteAggregator.writeZero(RouteDistinguisherUtil.RD_LENGTH);
41 NextHopUtil.serializeNextHop(cNextHop, byteAggregator);