Move route target ext comm container
[bgpcep.git] / bgp / parser-impl / src / main / java / org / opendaylight / protocol / bgp / parser / impl / message / update / extended / communities / route / target / RouteTargetExtendedCommunityHandler.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.route.target;
10
11 import static org.opendaylight.protocol.bgp.parser.spi.extended.community.AbstractTwoOctetAsExtendedCommunity.AS_LOCAL_ADMIN_LENGTH;
12
13 import com.google.common.primitives.Ints;
14 import io.netty.buffer.ByteBuf;
15 import org.opendaylight.protocol.util.ByteArray;
16 import org.opendaylight.protocol.util.ByteBufWriteUtil;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.ShortAsNumber;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.route.target.extended.community.grouping.RouteTargetExtendedCommunity;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.route.target.extended.community.grouping.RouteTargetExtendedCommunityBuilder;
20
21 /**
22  * Route Target Extended Community Parser / Serializer.
23  *
24  * @author Claudio D. Gasparini
25  */
26 public final class RouteTargetExtendedCommunityHandler {
27     private RouteTargetExtendedCommunityHandler() {
28         throw new UnsupportedOperationException();
29     }
30
31     public static RouteTargetExtendedCommunity parse(final ByteBuf buffer) {
32         return new RouteTargetExtendedCommunityBuilder()
33                 .setGlobalAdministrator(new ShortAsNumber((long) buffer.readUnsignedShort()))
34                 .setLocalAdministrator(ByteArray.readBytes(buffer, AS_LOCAL_ADMIN_LENGTH))
35                 .build();
36     }
37
38     public static void serialize(final RouteTargetExtendedCommunity routeTarget, final ByteBuf byteAggregator) {
39         ByteBufWriteUtil.writeUnsignedShort(Ints.checkedCast(routeTarget.getGlobalAdministrator().getValue()),
40                 byteAggregator);
41         byteAggregator.writeBytes(routeTarget.getLocalAdministrator());
42     }
43 }