2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.protocol.bgp.flowspec.extended.communities;
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.BitArray;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.traffic.action.extended.community.TrafficActionExtendedCommunity;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.traffic.action.extended.community.TrafficActionExtendedCommunityBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.update.attributes.extended.communities.extended.community.TrafficActionExtendedCommunityCaseBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.extended.community.ExtendedCommunity;
23 public class TrafficActionEcHandler implements ExtendedCommunityParser, ExtendedCommunitySerializer {
25 private static final int TYPE = 128;
27 private static final int SUBTYPE = 7;
29 private static final int RESERVED = 5;
31 private static final int FLAGS_SIZE = 8;
33 private static final int SAMPLE_BIT = 6;
35 private static final int TERMINAL_BIT = 7;
38 public void serializeExtendedCommunity(final ExtendedCommunity extendedCommunity, final ByteBuf byteAggregator) {
39 Preconditions.checkArgument(extendedCommunity instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.TrafficActionExtendedCommunity,
40 "The extended community %s is not TrafficActionExtendedCommunityCase type.", extendedCommunity);
41 final TrafficActionExtendedCommunity trafficAction = ((org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.TrafficActionExtendedCommunity) extendedCommunity).getTrafficActionExtendedCommunity();
42 byteAggregator.writeZero(RESERVED);
43 final BitArray flags = new BitArray(FLAGS_SIZE);
44 flags.set(SAMPLE_BIT, trafficAction.isSample());
45 flags.set(TERMINAL_BIT, trafficAction.isTerminalAction());
46 flags.toByteBuf(byteAggregator);
50 public int getType(final boolean isTransitive) {
51 //traffic-action is transitive only
56 public int getSubType() {
61 public ExtendedCommunity parseExtendedCommunity(final ByteBuf buffer) throws BGPDocumentedException, BGPParsingException {
62 buffer.skipBytes(RESERVED);
63 final BitArray flags = BitArray.valueOf(buffer, FLAGS_SIZE);
64 final boolean sample = flags.get(SAMPLE_BIT);
65 final boolean terminal = flags.get(TERMINAL_BIT);
66 return new TrafficActionExtendedCommunityCaseBuilder().setTrafficActionExtendedCommunity(
67 new TrafficActionExtendedCommunityBuilder()
69 .setTerminalAction(terminal)