ed0f8c2dd025db4a2ea9ec4fa57842e5873424a7
[bgpcep.git] / bgp / parser-impl / src / main / java / org / opendaylight / protocol / bgp / parser / impl / message / update / extended / communities / EncapsulationEC.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
9 package org.opendaylight.protocol.bgp.parser.impl.message.update.extended.communities;
10
11 import com.google.common.annotations.VisibleForTesting;
12 import com.google.common.base.Preconditions;
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.AbstractOpaqueExtendedCommunity;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.EncapsulationTunnelType;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.extended.community.ExtendedCommunity;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.extended.community.extended.community.EncapsulationCase;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.extended.community.extended.community.EncapsulationCaseBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.extended.community.extended.community.encapsulation._case.EncapsulationExtendedCommunity;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.extended.community.extended.community.encapsulation._case.EncapsulationExtendedCommunityBuilder;
23
24 /**
25  * Parser for BGP Encapsulation extended community attribute.
26  *
27  * @see <a href="https://tools.ietf.org/html/rfc5512#section-4.5">BGP Encapsulation Extended Community</a>
28  */
29
30 public class EncapsulationEC extends AbstractOpaqueExtendedCommunity {
31     @VisibleForTesting
32     public static final int SUBTYPE = 12;
33     private static final int RESERVED_SIZE = 4;
34     private static final int CONTENT_SIZE = 6;
35
36     @Override
37     public ExtendedCommunity parseExtendedCommunity(final ByteBuf buffer)
38             throws BGPDocumentedException, BGPParsingException {
39         Preconditions.checkArgument(buffer != null && buffer.isReadable(),
40                 "Array of bytes is mandatory. Can't be null or empty.");
41         Preconditions.checkArgument(buffer.readableBytes() == CONTENT_SIZE,
42                 "Wrong length of array of bytes. Passed: " + buffer.readableBytes() + ".");
43         buffer.skipBytes(RESERVED_SIZE);
44         final EncapsulationExtendedCommunity encap = new EncapsulationExtendedCommunityBuilder()
45             .setTunnelType(EncapsulationTunnelType.forValue(buffer.readUnsignedShort()))
46             .build();
47         return new EncapsulationCaseBuilder().setEncapsulationExtendedCommunity(encap).build();
48     }
49
50     @Override
51     public void serializeExtendedCommunity(final ExtendedCommunity extendedCommunity, final ByteBuf body) {
52         Preconditions.checkArgument(extendedCommunity instanceof EncapsulationCase,
53             "The extended community %s is not EncapsulationCase type.", extendedCommunity);
54         final EncapsulationExtendedCommunity encap
55                 = ((EncapsulationCase) extendedCommunity).getEncapsulationExtendedCommunity();
56         body.writeZero(RESERVED_SIZE);
57         body.writeShort(encap.getTunnelType().getIntValue());
58     }
59
60     @Override
61     public int getSubType() {
62         return SUBTYPE;
63     }
64 }