Create common parent for extensions families
[bgpcep.git] / bgp / extensions / l3vpn / src / main / java / org / opendaylight / protocol / bgp / l3vpn / unicast / AbstractVpnNextHopParserSerializer.java
1 /*
2  * Copyright (c) 2016 Brocade Communications 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.bgp.l3vpn.unicast;
9
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.rev180329.next.hop.CNextHop;
17
18 public abstract class AbstractVpnNextHopParserSerializer implements NextHopParserSerializer {
19     private final int ipAddrLength;
20     private final Class<?> ipNextHopCaseClazz;
21
22     protected AbstractVpnNextHopParserSerializer(final int ipAddrLength, final Class<?> ipNextHopCaseClazz) {
23         this.ipAddrLength = ipAddrLength;
24         this.ipNextHopCaseClazz = ipNextHopCaseClazz;
25     }
26
27     @Override
28     public CNextHop parseNextHop(final ByteBuf buffer) throws BGPParsingException {
29         Preconditions.checkArgument(buffer.readableBytes() == (this.ipAddrLength + RouteDistinguisherUtil.RD_LENGTH),
30                 "Length of byte array for NEXT_HOP should be %s, but is %s",
31                 this.ipAddrLength + RouteDistinguisherUtil.RD_LENGTH, buffer.readableBytes());
32         buffer.readBytes(RouteDistinguisherUtil.RD_LENGTH);
33         return NextHopUtil.parseNextHop(buffer.readBytes(this.ipAddrLength));
34     }
35
36     @Override
37     public void serializeNextHop(final CNextHop nextHop, final ByteBuf byteAggregator) {
38         Preconditions.checkArgument(this.ipNextHopCaseClazz.isInstance(nextHop),
39                 "cNextHop is not a VPN %s NextHop object.", this.ipNextHopCaseClazz.getSimpleName());
40         byteAggregator.writeZero(RouteDistinguisherUtil.RD_LENGTH);
41         NextHopUtil.serializeNextHop(nextHop, byteAggregator);
42     }
43 }