Add ByteBufUtils
[bgpcep.git] / bgp / parser-impl / src / main / java / org / opendaylight / protocol / bgp / parser / impl / message / update / extended / communities / four / octect / as / specific / SourceAS4OctectHandler.java
1 /*
2  * Copyright (c) 2018 AT&T Intellectual Property. 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.parser.impl.message.update.extended.communities.four.octect.as.specific;
10
11 import com.google.common.base.Preconditions;
12 import io.netty.buffer.ByteBuf;
13 import org.opendaylight.protocol.bgp.parser.spi.extended.community.Abstract4OctetAsExtendedCommunity;
14 import org.opendaylight.protocol.util.ByteBufUtils;
15 import org.opendaylight.protocol.util.ByteBufWriteUtil;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.extended.community.ExtendedCommunity;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.extended.community.extended.community.SourceAs4ExtendedCommunityCase;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.extended.community.extended.community.SourceAs4ExtendedCommunityCaseBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.extended.community.extended.community.source.as._4.extended.community._case.SourceAs4ExtendedCommunityBuilder;
21
22 public final class SourceAS4OctectHandler extends Abstract4OctetAsExtendedCommunity {
23     private static final short SUBTYPE = 209;
24     private static final int LOCAL_ADMIN = 0;
25     private static final short LOCAL_LENGTH = 2;
26
27     @Override
28     public ExtendedCommunity parseExtendedCommunity(final ByteBuf body) {
29         final SourceAs4ExtendedCommunityBuilder builder = new SourceAs4ExtendedCommunityBuilder();
30         builder.setGlobalAdministrator(new AsNumber(ByteBufUtils.readUint32(body)));
31         body.skipBytes(LOCAL_LENGTH);
32         return new SourceAs4ExtendedCommunityCaseBuilder().setSourceAs4ExtendedCommunity(
33                 builder.build()).build();
34     }
35
36     @Override
37     public void serializeExtendedCommunity(final ExtendedCommunity extendedCommunity, final ByteBuf body) {
38         Preconditions.checkArgument(extendedCommunity instanceof SourceAs4ExtendedCommunityCase,
39                 "The extended community %s is not SourceAs4ExtendedCommunityCase type.",
40                 extendedCommunity);
41
42         body.writeInt(((SourceAs4ExtendedCommunityCase) extendedCommunity).getSourceAs4ExtendedCommunity()
43                 .getGlobalAdministrator().getValue().intValue());
44         ByteBufWriteUtil.writeUnsignedShort(LOCAL_ADMIN, body);
45     }
46
47     @Override
48     public int getSubType() {
49         return SUBTYPE;
50     }
51 }