efd86bf229540713d5e3248935d1057c7d4adf79
[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.rev130919.EncapsulationTunnelType;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.ExtendedCommunity;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.EncapsulationCase;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.EncapsulationCaseBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.encapsulation._case.EncapsulationExtendedCommunity;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.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) throws BGPDocumentedException, BGPParsingException {
38         Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
39         Preconditions.checkArgument(buffer.readableBytes() == CONTENT_SIZE, "Wrong length of array of bytes. Passed: " + buffer.readableBytes() + ".");
40         buffer.skipBytes(RESERVED_SIZE);
41         final EncapsulationExtendedCommunity encap = new EncapsulationExtendedCommunityBuilder()
42             .setTunnelType(EncapsulationTunnelType.forValue(buffer.readUnsignedShort()))
43             .build();
44         return new EncapsulationCaseBuilder().setEncapsulationExtendedCommunity(encap).build();
45     }
46
47     @Override
48     public void serializeExtendedCommunity(final ExtendedCommunity extendedCommunity, final ByteBuf body) {
49         Preconditions.checkArgument(extendedCommunity instanceof EncapsulationCase,
50             "The extended community %s is not EncapsulationCase type.", extendedCommunity);
51         final EncapsulationExtendedCommunity encap = ((EncapsulationCase) extendedCommunity).getEncapsulationExtendedCommunity();
52         body.writeZero(RESERVED_SIZE);
53         body.writeShort(encap.getTunnelType().getIntValue());
54     }
55
56     @Override
57     public int getSubType() {
58         return SUBTYPE;
59     }
60 }