Create common parent for extensions families
[bgpcep.git] / bgp / extensions / flowspec / src / main / java / org / opendaylight / protocol / bgp / flowspec / extended / communities / TrafficRateEcHandler.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.flowspec.extended.communities;
10
11 import com.google.common.base.Preconditions;
12 import io.netty.buffer.ByteBuf;
13 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
14 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
15 import org.opendaylight.protocol.bgp.parser.spi.extended.community.ExtendedCommunityParser;
16 import org.opendaylight.protocol.bgp.parser.spi.extended.community.ExtendedCommunitySerializer;
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.flowspec.rev180329.traffic.rate.extended.community.TrafficRateExtendedCommunity;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.traffic.rate.extended.community.TrafficRateExtendedCommunityBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.update.attributes.extended.communities.extended.community.TrafficRateExtendedCommunityCaseBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.ShortAsNumber;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.extended.community.ExtendedCommunity;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.Bandwidth;
25
26 public class TrafficRateEcHandler implements ExtendedCommunityParser, ExtendedCommunitySerializer {
27
28     private static final int TYPE = 128;
29
30     private static final int SUBTYPE = 6;
31
32     private static final int TRAFFIC_RATE_SIZE = 4;
33
34     @Override
35     public void serializeExtendedCommunity(final ExtendedCommunity extendedCommunity, final ByteBuf byteAggregator) {
36         Preconditions.checkArgument(extendedCommunity instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.TrafficRateExtendedCommunity,
37                 "The extended community %s is not TrafficRateExtendedCommunity type.", extendedCommunity);
38         final TrafficRateExtendedCommunity trafficRate = ((org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.TrafficRateExtendedCommunity) extendedCommunity).getTrafficRateExtendedCommunity();
39         ByteBufWriteUtil.writeShort(trafficRate.getInformativeAs().getValue().shortValue(), byteAggregator);
40         byteAggregator.writeBytes(trafficRate.getLocalAdministrator().getValue());
41     }
42
43     @Override
44     public int getType(final boolean isTransitive) {
45         //traffic-rate is transitive only
46         return TYPE;
47     }
48
49     @Override
50     public int getSubType() {
51         return SUBTYPE;
52     }
53
54     @Override
55     public ExtendedCommunity parseExtendedCommunity(final ByteBuf buffer) throws BGPDocumentedException, BGPParsingException {
56         final ShortAsNumber as = new ShortAsNumber((long) buffer.readUnsignedShort());
57         final Bandwidth value = new Bandwidth(ByteArray.readBytes(buffer, TRAFFIC_RATE_SIZE));
58         return new TrafficRateExtendedCommunityCaseBuilder().setTrafficRateExtendedCommunity(
59                 new TrafficRateExtendedCommunityBuilder()
60                     .setInformativeAs(as)
61                     .setLocalAdministrator(value)
62                     .build()).build();
63     }
64
65 }