Code Clean Up
[bgpcep.git] / bgp / l3vpn / src / main / java / org / opendaylight / protocol / bgp / l3vpn / 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;
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.rev130919.next.hop.CNextHop;
17
18 /**
19  * @author Kevin Wang
20  */
21 public abstract class AbstractVpnNextHopParserSerializer implements NextHopParserSerializer {
22     private final int ipAddrLength;
23     private final Class<?> ipNextHopCaseClazz;
24
25     protected AbstractVpnNextHopParserSerializer(final int ipAddrLength, final Class<?> ipNextHopCaseClazz) {
26         this.ipAddrLength = ipAddrLength;
27         this.ipNextHopCaseClazz = ipNextHopCaseClazz;
28     }
29
30     @Override
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));
35     }
36
37     @Override
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);
42     }
43 }