BUG 5031:
[bgpcep.git] / bgp / linkstate / src / main / java / org / opendaylight / protocol / bgp / linkstate / spi / AbstractNlriTypeCodec.java
1 /*
2  * Copyright (c) 2016 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.linkstate.spi;
10
11 import io.netty.buffer.ByteBuf;
12 import java.math.BigInteger;
13 import org.opendaylight.protocol.bgp.linkstate.spi.NlriTypeCaseParser;
14 import org.opendaylight.protocol.bgp.linkstate.spi.NlriTypeCaseSerializer;
15 import org.opendaylight.protocol.util.ByteBufWriteUtil;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.Identifier;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.ProtocolId;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.ObjectType;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.destination.CLinkstateDestination;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.destination.CLinkstateDestinationBuilder;
21
22 public abstract class AbstractNlriTypeCodec implements NlriTypeCaseParser, NlriTypeCaseSerializer {
23     @Override
24     public final CLinkstateDestination parseTypeNlri(final ByteBuf nlri) {
25         final CLinkstateDestinationBuilder builder = new CLinkstateDestinationBuilder();
26         builder.setProtocolId(ProtocolId.forValue(nlri.readUnsignedByte()));
27         builder.setIdentifier(new Identifier(BigInteger.valueOf(nlri.readLong())));
28         builder.setObjectType(parseObjectType(nlri));
29         return builder.build();
30     }
31
32     @Override
33     public final void serializeTypeNlri(final CLinkstateDestination nlriType, final ByteBuf byteAggregator) {
34         ByteBufWriteUtil.writeUnsignedByte((short) nlriType.getProtocolId().getIntValue(), byteAggregator);
35         ByteBufWriteUtil.writeUnsignedLong(nlriType.getIdentifier().getValue(), byteAggregator);
36         serializeObjectType(nlriType.getObjectType(), byteAggregator);
37     }
38
39     protected abstract ObjectType parseObjectType(ByteBuf buffer);
40
41     protected abstract void serializeObjectType(ObjectType objectType, ByteBuf buffer);
42 }