8179a395745df70698befeb247f48ebb511cc47f
[bgpcep.git] / bgp / parser-impl / src / main / java / org / opendaylight / protocol / bgp / parser / impl / message / update / extended / communities / RouteOriginAsTwoOctetEcHandler.java
1 /*
2  * Copyright (c) 2015 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.parser.impl.message.update.extended.communities;
10
11 import com.google.common.base.Preconditions;
12 import com.google.common.primitives.Ints;
13 import io.netty.buffer.ByteBuf;
14 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
15 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
16 import org.opendaylight.protocol.bgp.parser.spi.extended.community.AbstractTwoOctetAsExtendedCommunity;
17 import org.opendaylight.protocol.util.ByteArray;
18 import org.opendaylight.protocol.util.ByteBufWriteUtil;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.ShortAsNumber;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.ExtendedCommunity;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.RouteOriginExtendedCommunityCase;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.RouteOriginExtendedCommunityCaseBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.route.origin.extended.community._case.RouteOriginExtendedCommunity;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.route.origin.extended.community._case.RouteOriginExtendedCommunityBuilder;
25
26 public final class RouteOriginAsTwoOctetEcHandler extends AbstractTwoOctetAsExtendedCommunity {
27
28     private static final int SUBTYPE = 3;
29
30     @Override
31     public ExtendedCommunity parseExtendedCommunity(final ByteBuf buffer) throws BGPDocumentedException, BGPParsingException {
32         final RouteOriginExtendedCommunity targetOrigin = new RouteOriginExtendedCommunityBuilder()
33             .setGlobalAdministrator(new ShortAsNumber((long) buffer.readUnsignedShort()))
34             .setLocalAdministrator(ByteArray.readBytes(buffer, AS_LOCAL_ADMIN_LENGTH))
35             .build();
36         return new RouteOriginExtendedCommunityCaseBuilder().setRouteOriginExtendedCommunity(targetOrigin).build();
37     }
38
39     @Override
40     public void serializeExtendedCommunity(final ExtendedCommunity extendedCommunity, final ByteBuf byteAggregator) {
41         Preconditions.checkArgument(extendedCommunity instanceof RouteOriginExtendedCommunityCase,
42                 "The extended community %s is not RouteOriginExtendedCommunity type.", extendedCommunity);
43         final RouteOriginExtendedCommunity routeOrigin = ((RouteOriginExtendedCommunityCase) extendedCommunity).getRouteOriginExtendedCommunity();
44         ByteBufWriteUtil.writeUnsignedShort(Ints.checkedCast(routeOrigin.getGlobalAdministrator().getValue()), byteAggregator);
45         byteAggregator.writeBytes(routeOrigin.getLocalAdministrator());
46     }
47
48     @Override
49     public int getSubType() {
50         return SUBTYPE;
51     }
52 }