b08fe8351ecd45ea16b72c6431bdb63e412bcc08
[bgpcep.git] / bgp / parser-impl / src / main / java / org / opendaylight / protocol / bgp / parser / impl / message / update / extended / communities / LinkBandwidthEC.java
1 /*
2  * Copyright (c) 2016 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 static org.opendaylight.protocol.bgp.parser.impl.message.BGPOpenMessageParser.AS_TRANS;
12
13 import com.google.common.base.Preconditions;
14 import io.netty.buffer.ByteBuf;
15 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
16 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
17 import org.opendaylight.protocol.bgp.parser.spi.extended.community.ExtendedCommunityParser;
18 import org.opendaylight.protocol.bgp.parser.spi.extended.community.ExtendedCommunitySerializer;
19 import org.opendaylight.protocol.util.ByteArray;
20 import org.opendaylight.protocol.util.ByteBufWriteUtil;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.ExtendedCommunity;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.LinkBandwidthCase;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.LinkBandwidthCaseBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.link.bandwidth._case.LinkBandwidthExtendedCommunity;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.link.bandwidth._case.LinkBandwidthExtendedCommunityBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.Bandwidth;
27
28 public class LinkBandwidthEC implements ExtendedCommunityParser, ExtendedCommunitySerializer {
29     private static final int TYPE = 64;
30     private static final int SUBTYPE = 4;
31     private static final int BANDWIDTH_SIZE = 4;
32     private static final int AS_TRANS_LENGTH = 2;
33
34     @Override
35     public ExtendedCommunity parseExtendedCommunity(final ByteBuf buffer) throws BGPDocumentedException, BGPParsingException {
36         buffer.skipBytes(AS_TRANS_LENGTH);
37         final LinkBandwidthExtendedCommunity lb = new LinkBandwidthExtendedCommunityBuilder()
38             .setBandwidth(new Bandwidth(ByteArray.readBytes(buffer, BANDWIDTH_SIZE)))
39             .build();
40         return new LinkBandwidthCaseBuilder().setLinkBandwidthExtendedCommunity(lb).build();
41     }
42
43     @Override
44     public void serializeExtendedCommunity(final ExtendedCommunity extendedCommunity, final ByteBuf body) {
45         Preconditions.checkArgument(extendedCommunity instanceof LinkBandwidthCase,
46             "The extended community %s is not LinkBandwidthCase type.", extendedCommunity);
47         final LinkBandwidthExtendedCommunity lb = ((LinkBandwidthCase) extendedCommunity).getLinkBandwidthExtendedCommunity();
48         body.writeShort(AS_TRANS);
49         ByteBufWriteUtil.writeFloat32(lb.getBandwidth(), body);
50     }
51
52     @Override
53     public int getType(final boolean isTransitive) {
54         return TYPE;
55     }
56
57     @Override
58     public int getSubType() {
59         return SUBTYPE;
60     }
61 }